blob: b3e1c4f8626bb5bf192cb147af3aa20dd00c7a43 [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
528 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100529 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100530
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600531 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600532 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100533 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
534 pixman_region32_fini(&surface->opaque);
535 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
536 output->width, output->height);
537 pixman_region32_fini(&surface->input);
538 pixman_region32_init(&surface->input);
539
540 wl_list_init(&fsurf->workspace_transform.link);
541
542 return fsurf;
543}
544
545static void
546focus_surface_destroy(struct focus_surface *fsurf)
547{
548 weston_surface_destroy(fsurf->surface);
549 free(fsurf);
550}
551
552static void
553focus_animation_done(struct weston_view_animation *animation, void *data)
554{
555 struct workspace *ws = data;
556
557 ws->focus_animation = NULL;
558}
559
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200560static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200561focus_state_destroy(struct focus_state *state)
562{
563 wl_list_remove(&state->seat_destroy_listener.link);
564 wl_list_remove(&state->surface_destroy_listener.link);
565 free(state);
566}
567
568static void
569focus_state_seat_destroy(struct wl_listener *listener, void *data)
570{
571 struct focus_state *state = container_of(listener,
572 struct focus_state,
573 seat_destroy_listener);
574
575 wl_list_remove(&state->link);
576 focus_state_destroy(state);
577}
578
579static void
580focus_state_surface_destroy(struct wl_listener *listener, void *data)
581{
582 struct focus_state *state = container_of(listener,
583 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400584 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400585 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 struct weston_surface *main_surface, *next;
587 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200588
Pekka Paalanen01388e22013-04-25 13:57:44 +0300589 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
590
Kristian Høgsberge3778222012-07-31 17:29:30 -0400591 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500592 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
593 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400594 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100595 if (is_focus_view(view))
596 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400597
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400599 break;
600 }
601
Pekka Paalanen01388e22013-04-25 13:57:44 +0300602 /* if the focus was a sub-surface, activate its main surface */
603 if (main_surface != state->keyboard_focus)
604 next = main_surface;
605
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100606 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400607 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100608 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400609 activate(shell, next, state->seat);
610 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100611 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
612 if (state->ws->focus_animation)
613 weston_view_animation_destroy(state->ws->focus_animation);
614
615 state->ws->focus_animation = weston_fade_run(
616 state->ws->fsurf_front->view,
617 state->ws->fsurf_front->view->alpha, 0.0, 300,
618 focus_animation_done, state->ws);
619 }
620
Kristian Høgsberge3778222012-07-31 17:29:30 -0400621 wl_list_remove(&state->link);
622 focus_state_destroy(state);
623 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200624}
625
626static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400627focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200628{
Jonas Ådahl04769742012-06-13 00:01:24 +0200629 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200630
631 state = malloc(sizeof *state);
632 if (state == NULL)
633 return NULL;
634
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100635 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400636 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200637 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400638 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200639
640 state->seat_destroy_listener.notify = focus_state_seat_destroy;
641 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400642 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200643 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400644 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200645
646 return state;
647}
648
Jonas Ådahl8538b222012-08-29 22:13:03 +0200649static struct focus_state *
650ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
651{
652 struct workspace *ws = get_current_workspace(shell);
653 struct focus_state *state;
654
655 wl_list_for_each(state, &ws->focus_list, link)
656 if (state->seat == seat)
657 break;
658
659 if (&state->link == &ws->focus_list)
660 state = focus_state_create(seat, ws);
661
662 return state;
663}
664
Jonas Ådahl04769742012-06-13 00:01:24 +0200665static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400666restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200667{
668 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400669 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200670
671 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800672 if (state->seat->keyboard == NULL)
673 continue;
674
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400675 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200676
Kristian Høgsberge3148752013-05-06 23:19:49 -0400677 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200678 }
679}
680
681static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200682replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
683 struct weston_seat *seat)
684{
685 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400686 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200687
688 wl_list_for_each(state, &ws->focus_list, link) {
689 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400690 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500691 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200692 return;
693 }
694 }
695}
696
697static void
698drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
699 struct weston_surface *surface)
700{
701 struct focus_state *state;
702
703 wl_list_for_each(state, &ws->focus_list, link)
704 if (state->keyboard_focus == surface)
705 state->keyboard_focus = NULL;
706}
707
708static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100709animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
710 struct weston_view *from, struct weston_view *to)
711{
712 struct weston_output *output;
713 bool focus_surface_created = false;
714
715 /* FIXME: Only support dim animation using two layers */
716 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
717 return;
718
719 output = get_default_output(shell->compositor);
720 if (ws->fsurf_front == NULL && (from || to)) {
721 ws->fsurf_front = create_focus_surface(shell->compositor, output);
722 ws->fsurf_back = create_focus_surface(shell->compositor, output);
723 ws->fsurf_front->view->alpha = 0.0;
724 ws->fsurf_back->view->alpha = 0.0;
725 focus_surface_created = true;
726 } else {
727 wl_list_remove(&ws->fsurf_front->view->layer_link);
728 wl_list_remove(&ws->fsurf_back->view->layer_link);
729 }
730
731 if (ws->focus_animation) {
732 weston_view_animation_destroy(ws->focus_animation);
733 ws->focus_animation = NULL;
734 }
735
736 if (to)
737 wl_list_insert(&to->layer_link,
738 &ws->fsurf_front->view->layer_link);
739 else if (from)
740 wl_list_insert(&ws->layer.view_list,
741 &ws->fsurf_front->view->layer_link);
742
743 if (focus_surface_created) {
744 ws->focus_animation = weston_fade_run(
745 ws->fsurf_front->view,
746 ws->fsurf_front->view->alpha, 0.6, 300,
747 focus_animation_done, ws);
748 } else if (from) {
749 wl_list_insert(&from->layer_link,
750 &ws->fsurf_back->view->layer_link);
751 ws->focus_animation = weston_stable_fade_run(
752 ws->fsurf_front->view, 0.0,
753 ws->fsurf_back->view, 0.6,
754 focus_animation_done, ws);
755 } else if (to) {
756 wl_list_insert(&ws->layer.view_list,
757 &ws->fsurf_back->view->layer_link);
758 ws->focus_animation = weston_stable_fade_run(
759 ws->fsurf_front->view, 0.0,
760 ws->fsurf_back->view, 0.6,
761 focus_animation_done, ws);
762 }
763}
764
765static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200766workspace_destroy(struct workspace *ws)
767{
Jonas Ådahl04769742012-06-13 00:01:24 +0200768 struct focus_state *state, *next;
769
770 wl_list_for_each_safe(state, next, &ws->focus_list, link)
771 focus_state_destroy(state);
772
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100773 if (ws->fsurf_front)
774 focus_surface_destroy(ws->fsurf_front);
775 if (ws->fsurf_back)
776 focus_surface_destroy(ws->fsurf_back);
777
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200778 free(ws);
779}
780
Jonas Ådahl04769742012-06-13 00:01:24 +0200781static void
782seat_destroyed(struct wl_listener *listener, void *data)
783{
784 struct weston_seat *seat = data;
785 struct focus_state *state, *next;
786 struct workspace *ws = container_of(listener,
787 struct workspace,
788 seat_destroyed_listener);
789
790 wl_list_for_each_safe(state, next, &ws->focus_list, link)
791 if (state->seat == seat)
792 wl_list_remove(&state->link);
793}
794
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200795static struct workspace *
796workspace_create(void)
797{
798 struct workspace *ws = malloc(sizeof *ws);
799 if (ws == NULL)
800 return NULL;
801
802 weston_layer_init(&ws->layer, NULL);
803
Jonas Ådahl04769742012-06-13 00:01:24 +0200804 wl_list_init(&ws->focus_list);
805 wl_list_init(&ws->seat_destroyed_listener.link);
806 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100807 ws->fsurf_front = NULL;
808 ws->fsurf_back = NULL;
809 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200810
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200811 return ws;
812}
813
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200814static int
815workspace_is_empty(struct workspace *ws)
816{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500817 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200818}
819
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200820static struct workspace *
821get_workspace(struct desktop_shell *shell, unsigned int index)
822{
823 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200824 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200825 pws += index;
826 return *pws;
827}
828
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800829struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200830get_current_workspace(struct desktop_shell *shell)
831{
832 return get_workspace(shell, shell->workspaces.current);
833}
834
835static void
836activate_workspace(struct desktop_shell *shell, unsigned int index)
837{
838 struct workspace *ws;
839
840 ws = get_workspace(shell, index);
841 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
842
843 shell->workspaces.current = index;
844}
845
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200846static unsigned int
847get_output_height(struct weston_output *output)
848{
849 return abs(output->region.extents.y1 - output->region.extents.y2);
850}
851
852static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200854{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855 struct weston_transform *transform;
856
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100857 if (is_focus_view(view)) {
858 struct focus_surface *fsurf = get_focus_surface(view->surface);
859 transform = &fsurf->workspace_transform;
860 } else {
861 struct shell_surface *shsurf = get_shell_surface(view->surface);
862 transform = &shsurf->workspace_transform;
863 }
864
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200865 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500866 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100867 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200868
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100869 weston_matrix_init(&transform->matrix);
870 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200871 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500872 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200873}
874
875static void
876workspace_translate_out(struct workspace *ws, double fraction)
877{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500878 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200879 unsigned int height;
880 double d;
881
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
883 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200884 d = height * fraction;
885
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100886 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200887 }
888}
889
890static void
891workspace_translate_in(struct workspace *ws, double fraction)
892{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500893 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200894 unsigned int height;
895 double d;
896
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
898 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200899
900 if (fraction > 0)
901 d = -(height - height * fraction);
902 else
903 d = height + height * fraction;
904
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100905 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200906 }
907}
908
909static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200910broadcast_current_workspace_state(struct desktop_shell *shell)
911{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700912 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200913
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700914 wl_resource_for_each(resource, &shell->workspaces.client_list)
915 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200916 shell->workspaces.current,
917 shell->workspaces.num);
918}
919
920static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200921reverse_workspace_change_animation(struct desktop_shell *shell,
922 unsigned int index,
923 struct workspace *from,
924 struct workspace *to)
925{
926 shell->workspaces.current = index;
927
928 shell->workspaces.anim_to = to;
929 shell->workspaces.anim_from = from;
930 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
931 shell->workspaces.anim_timestamp = 0;
932
Scott Moreau4272e632012-08-13 09:58:41 -0600933 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200934}
935
936static void
937workspace_deactivate_transforms(struct workspace *ws)
938{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100940 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200941
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100943 if (is_focus_view(view)) {
944 struct focus_surface *fsurf = get_focus_surface(view->surface);
945 transform = &fsurf->workspace_transform;
946 } else {
947 struct shell_surface *shsurf = get_shell_surface(view->surface);
948 transform = &shsurf->workspace_transform;
949 }
950
951 if (!wl_list_empty(&transform->link)) {
952 wl_list_remove(&transform->link);
953 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200954 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200956 }
957}
958
959static void
960finish_workspace_change_animation(struct desktop_shell *shell,
961 struct workspace *from,
962 struct workspace *to)
963{
Scott Moreau4272e632012-08-13 09:58:41 -0600964 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965
966 wl_list_remove(&shell->workspaces.animation.link);
967 workspace_deactivate_transforms(from);
968 workspace_deactivate_transforms(to);
969 shell->workspaces.anim_to = NULL;
970
971 wl_list_remove(&shell->workspaces.anim_from->layer.link);
972}
973
974static void
975animate_workspace_change_frame(struct weston_animation *animation,
976 struct weston_output *output, uint32_t msecs)
977{
978 struct desktop_shell *shell =
979 container_of(animation, struct desktop_shell,
980 workspaces.animation);
981 struct workspace *from = shell->workspaces.anim_from;
982 struct workspace *to = shell->workspaces.anim_to;
983 uint32_t t;
984 double x, y;
985
986 if (workspace_is_empty(from) && workspace_is_empty(to)) {
987 finish_workspace_change_animation(shell, from, to);
988 return;
989 }
990
991 if (shell->workspaces.anim_timestamp == 0) {
992 if (shell->workspaces.anim_current == 0.0)
993 shell->workspaces.anim_timestamp = msecs;
994 else
995 shell->workspaces.anim_timestamp =
996 msecs -
997 /* Invers of movement function 'y' below. */
998 (asin(1.0 - shell->workspaces.anim_current) *
999 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1000 M_2_PI);
1001 }
1002
1003 t = msecs - shell->workspaces.anim_timestamp;
1004
1005 /*
1006 * x = [0, π/2]
1007 * y(x) = sin(x)
1008 */
1009 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1010 y = sin(x);
1011
1012 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001013 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001014
1015 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1016 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1017 shell->workspaces.anim_current = y;
1018
Scott Moreau4272e632012-08-13 09:58:41 -06001019 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001021 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001022 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001023}
1024
1025static void
1026animate_workspace_change(struct desktop_shell *shell,
1027 unsigned int index,
1028 struct workspace *from,
1029 struct workspace *to)
1030{
1031 struct weston_output *output;
1032
1033 int dir;
1034
1035 if (index > shell->workspaces.current)
1036 dir = -1;
1037 else
1038 dir = 1;
1039
1040 shell->workspaces.current = index;
1041
1042 shell->workspaces.anim_dir = dir;
1043 shell->workspaces.anim_from = from;
1044 shell->workspaces.anim_to = to;
1045 shell->workspaces.anim_current = 0.0;
1046 shell->workspaces.anim_timestamp = 0;
1047
1048 output = container_of(shell->compositor->output_list.next,
1049 struct weston_output, link);
1050 wl_list_insert(&output->animation_list,
1051 &shell->workspaces.animation.link);
1052
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001053 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001054
1055 workspace_translate_in(to, 0);
1056
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001057 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001058
Scott Moreau4272e632012-08-13 09:58:41 -06001059 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001060}
1061
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001062static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001063update_workspace(struct desktop_shell *shell, unsigned int index,
1064 struct workspace *from, struct workspace *to)
1065{
1066 shell->workspaces.current = index;
1067 wl_list_insert(&from->layer.link, &to->layer.link);
1068 wl_list_remove(&from->layer.link);
1069}
1070
1071static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001072change_workspace(struct desktop_shell *shell, unsigned int index)
1073{
1074 struct workspace *from;
1075 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001076 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001077
1078 if (index == shell->workspaces.current)
1079 return;
1080
1081 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001083 return;
1084
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001085 from = get_current_workspace(shell);
1086 to = get_workspace(shell, index);
1087
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001088 if (shell->workspaces.anim_from == to &&
1089 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001090 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001091 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001092 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093 return;
1094 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001095
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001096 if (shell->workspaces.anim_to != NULL)
1097 finish_workspace_change_animation(shell,
1098 shell->workspaces.anim_from,
1099 shell->workspaces.anim_to);
1100
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001101 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001102
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001103 if (shell->focus_animation_type != ANIMATION_NONE) {
1104 wl_list_for_each(state, &from->focus_list, link)
1105 if (state->keyboard_focus)
1106 animate_focus_change(shell, from,
1107 get_default_view(state->keyboard_focus), NULL);
1108
1109 wl_list_for_each(state, &to->focus_list, link)
1110 if (state->keyboard_focus)
1111 animate_focus_change(shell, to,
1112 NULL, get_default_view(state->keyboard_focus));
1113 }
1114
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001115 if (workspace_is_empty(to) && workspace_is_empty(from))
1116 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 else
1118 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001119
1120 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001121}
1122
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001123static bool
1124workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1125{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001127 struct wl_list *e;
1128
1129 if (wl_list_empty(list))
1130 return false;
1131
1132 e = list->next;
1133
1134 if (e->next != list)
1135 return false;
1136
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001138}
1139
1140static void
Philip Withnall659163d2013-11-25 18:01:36 +00001141move_surface_to_workspace(struct desktop_shell *shell,
1142 struct shell_surface *shsurf,
1143 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001144{
1145 struct workspace *from;
1146 struct workspace *to;
1147 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001148 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001149 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001150
1151 if (workspace == shell->workspaces.current)
1152 return;
1153
Philip Withnall659163d2013-11-25 18:01:36 +00001154 view = get_default_view(shsurf->surface);
1155 if (!view)
1156 return;
1157
1158 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1159
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001160 if (workspace >= shell->workspaces.num)
1161 workspace = shell->workspaces.num - 1;
1162
Jonas Ådahle9d22502012-08-29 22:13:01 +02001163 from = get_current_workspace(shell);
1164 to = get_workspace(shell, workspace);
1165
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 wl_list_remove(&view->layer_link);
1167 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001168
Philip Withnall648a4dd2013-11-25 18:01:44 +00001169 shell_surface_update_child_surface_layers(shsurf);
1170
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001172 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1173 if (!seat->keyboard)
1174 continue;
1175
1176 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001178 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001179 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001180
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001182}
1183
1184static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001185take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001186 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001187 unsigned int index)
1188{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001189 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001191 struct shell_surface *shsurf;
1192 struct workspace *from;
1193 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001194 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001195
Pekka Paalanen01388e22013-04-25 13:57:44 +03001196 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 view = get_default_view(surface);
1198 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001199 index == shell->workspaces.current ||
1200 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001201 return;
1202
1203 from = get_current_workspace(shell);
1204 to = get_workspace(shell, index);
1205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 wl_list_remove(&view->layer_link);
1207 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208
Philip Withnall659163d2013-11-25 18:01:36 +00001209 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001210 if (shsurf != NULL)
1211 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001212
Jonas Ådahle9d22502012-08-29 22:13:01 +02001213 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214 drop_focus_state(shell, from, surface);
1215
1216 if (shell->workspaces.anim_from == to &&
1217 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001218 wl_list_remove(&to->layer.link);
1219 wl_list_insert(from->layer.link.prev, &to->layer.link);
1220
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001222 broadcast_current_workspace_state(shell);
1223
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 return;
1225 }
1226
1227 if (shell->workspaces.anim_to != NULL)
1228 finish_workspace_change_animation(shell,
1229 shell->workspaces.anim_from,
1230 shell->workspaces.anim_to);
1231
1232 if (workspace_is_empty(from) &&
1233 workspace_has_only(to, surface))
1234 update_workspace(shell, index, from, to);
1235 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001236 if (shsurf != NULL &&
1237 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001238 wl_list_insert(&shell->workspaces.anim_sticky_list,
1239 &shsurf->workspace_transform.link);
1240
1241 animate_workspace_change(shell, index, from, to);
1242 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001243
1244 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001245
1246 state = ensure_focus_state(shell, seat);
1247 if (state != NULL)
1248 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001249}
1250
1251static void
1252workspace_manager_move_surface(struct wl_client *client,
1253 struct wl_resource *resource,
1254 struct wl_resource *surface_resource,
1255 uint32_t workspace)
1256{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001257 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001258 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001259 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001260 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001261 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001262
Pekka Paalanen01388e22013-04-25 13:57:44 +03001263 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001264 shell_surface = get_shell_surface(main_surface);
1265 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001267
1268 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001269}
1270
1271static const struct workspace_manager_interface workspace_manager_implementation = {
1272 workspace_manager_move_surface,
1273};
1274
1275static void
1276unbind_resource(struct wl_resource *resource)
1277{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001278 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001279}
1280
1281static void
1282bind_workspace_manager(struct wl_client *client,
1283 void *data, uint32_t version, uint32_t id)
1284{
1285 struct desktop_shell *shell = data;
1286 struct wl_resource *resource;
1287
Jason Ekstranda85118c2013-06-27 20:17:02 -05001288 resource = wl_resource_create(client,
1289 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001290
1291 if (resource == NULL) {
1292 weston_log("couldn't add workspace manager object");
1293 return;
1294 }
1295
Jason Ekstranda85118c2013-06-27 20:17:02 -05001296 wl_resource_set_implementation(resource,
1297 &workspace_manager_implementation,
1298 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001299 wl_list_insert(&shell->workspaces.client_list,
1300 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001301
1302 workspace_manager_send_state(resource,
1303 shell->workspaces.current,
1304 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001305}
1306
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001307static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001308touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1309 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1310{
1311}
1312
1313static void
1314touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1315{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001316 struct weston_touch_move_grab *move =
1317 (struct weston_touch_move_grab *) container_of(
1318 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001319
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001320 if (touch_id == 0)
1321 move->active = 0;
1322
Jonas Ådahl9484b692013-12-02 22:05:03 +01001323 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001324 shell_touch_grab_end(&move->base);
1325 free(move);
1326 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001327}
1328
1329static void
1330touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1331 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1332{
1333 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1334 struct shell_surface *shsurf = move->base.shsurf;
1335 struct weston_surface *es;
1336 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1337 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1338
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001339 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001340 return;
1341
1342 es = shsurf->surface;
1343
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001344 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001345
1346 weston_compositor_schedule_repaint(es->compositor);
1347}
1348
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001349static void
1350touch_move_grab_cancel(struct weston_touch_grab *grab)
1351{
1352 struct weston_touch_move_grab *move =
1353 (struct weston_touch_move_grab *) container_of(
1354 grab, struct shell_touch_grab, grab);
1355
1356 shell_touch_grab_end(&move->base);
1357 free(move);
1358}
1359
Rusty Lynch1084da52013-08-15 09:10:08 -07001360static const struct weston_touch_grab_interface touch_move_grab_interface = {
1361 touch_move_grab_down,
1362 touch_move_grab_up,
1363 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001364 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001365};
1366
1367static int
1368surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1369{
1370 struct weston_touch_move_grab *move;
1371
1372 if (!shsurf)
1373 return -1;
1374
Rafael Antognolli03b16592013-12-03 15:35:42 -02001375 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001376 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001377 if (shsurf->grabbed)
1378 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001379
1380 move = malloc(sizeof *move);
1381 if (!move)
1382 return -1;
1383
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001384 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001386 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001388 seat->touch->grab_y;
1389
1390 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1391 seat->touch);
1392
1393 return 0;
1394}
1395
1396static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001397noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001398{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001399}
1400
1401static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001402move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1403 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001404{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001405 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001406 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001407 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001408 int dx, dy;
1409
1410 weston_pointer_move(pointer, x, y);
1411 dx = wl_fixed_to_int(pointer->x + move->dx);
1412 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001413
1414 if (!shsurf)
1415 return;
1416
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001417 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001418
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001420}
1421
1422static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001423move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001424 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001425{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001426 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1427 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001428 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001429 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001430
Daniel Stone4dbadb12012-05-30 16:31:51 +01001431 if (pointer->button_count == 0 &&
1432 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001433 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001434 free(grab);
1435 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001436}
1437
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001438static void
1439move_grab_cancel(struct weston_pointer_grab *grab)
1440{
1441 struct shell_grab *shell_grab =
1442 container_of(grab, struct shell_grab, grab);
1443
1444 shell_grab_end(shell_grab);
1445 free(grab);
1446}
1447
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001448static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001449 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001450 move_grab_motion,
1451 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001452 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001453};
1454
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001455static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001456surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001457{
1458 struct weston_move_grab *move;
1459
1460 if (!shsurf)
1461 return -1;
1462
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001463 if (shsurf->grabbed)
1464 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001465 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001466 return 0;
1467
1468 move = malloc(sizeof *move);
1469 if (!move)
1470 return -1;
1471
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001473 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001475 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001476
1477 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001478 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001479
1480 return 0;
1481}
1482
1483static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001484common_surface_move(struct wl_resource *resource,
1485 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001486{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001487 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001488 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001489 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001490
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001491 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001492 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001493 seat->pointer->button_count > 0 &&
1494 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001496 if ((surface == shsurf->surface) &&
1497 (surface_move(shsurf, seat) < 0))
1498 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001499 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001500 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001501 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001503 if ((surface == shsurf->surface) &&
1504 (surface_touch_move(shsurf, seat) < 0))
1505 wl_resource_post_no_memory(resource);
1506 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001507}
1508
Rafael Antognollie2a34552013-12-03 15:35:45 -02001509static void
1510shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1511 struct wl_resource *seat_resource, uint32_t serial)
1512{
1513 common_surface_move(resource, seat_resource, serial);
1514}
1515
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001516struct weston_resize_grab {
1517 struct shell_grab base;
1518 uint32_t edges;
1519 int32_t width, height;
1520};
1521
1522static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001523resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1524 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001525{
1526 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001527 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001528 struct shell_surface *shsurf = resize->base.shsurf;
1529 int32_t width, height;
1530 wl_fixed_t from_x, from_y;
1531 wl_fixed_t to_x, to_y;
1532
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001533 weston_pointer_move(pointer, x, y);
1534
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001535 if (!shsurf)
1536 return;
1537
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 weston_view_from_global_fixed(shsurf->view,
1539 pointer->grab_x, pointer->grab_y,
1540 &from_x, &from_y);
1541 weston_view_from_global_fixed(shsurf->view,
1542 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001543
1544 width = resize->width;
1545 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1546 width += wl_fixed_to_int(from_x - to_x);
1547 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1548 width += wl_fixed_to_int(to_x - from_x);
1549 }
1550
1551 height = resize->height;
1552 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1553 height += wl_fixed_to_int(from_y - to_y);
1554 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1555 height += wl_fixed_to_int(to_y - from_y);
1556 }
1557
1558 shsurf->client->send_configure(shsurf->surface,
1559 resize->edges, width, height);
1560}
1561
1562static void
1563send_configure(struct weston_surface *surface,
1564 uint32_t edges, int32_t width, int32_t height)
1565{
1566 struct shell_surface *shsurf = get_shell_surface(surface);
1567
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001568 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569 edges, width, height);
1570}
1571
1572static const struct weston_shell_client shell_client = {
1573 send_configure
1574};
1575
1576static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001577resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001578 uint32_t time, uint32_t button, uint32_t state_w)
1579{
1580 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001581 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001582 enum wl_pointer_button_state state = state_w;
1583
1584 if (pointer->button_count == 0 &&
1585 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1586 shell_grab_end(&resize->base);
1587 free(grab);
1588 }
1589}
1590
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001591static void
1592resize_grab_cancel(struct weston_pointer_grab *grab)
1593{
1594 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1595
1596 shell_grab_end(&resize->base);
1597 free(grab);
1598}
1599
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001600static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001601 noop_grab_focus,
1602 resize_grab_motion,
1603 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001604 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001605};
1606
Giulio Camuffob8366642013-04-25 13:57:46 +03001607/*
1608 * Returns the bounding box of a surface and all its sub-surfaces,
1609 * in the surface coordinates system. */
1610static void
1611surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1612 int32_t *y, int32_t *w, int32_t *h) {
1613 pixman_region32_t region;
1614 pixman_box32_t *box;
1615 struct weston_subsurface *subsurface;
1616
1617 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618 surface->width,
1619 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001620
1621 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1622 pixman_region32_union_rect(&region, &region,
1623 subsurface->position.x,
1624 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001625 subsurface->surface->width,
1626 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001627 }
1628
1629 box = pixman_region32_extents(&region);
1630 if (x)
1631 *x = box->x1;
1632 if (y)
1633 *y = box->y1;
1634 if (w)
1635 *w = box->x2 - box->x1;
1636 if (h)
1637 *h = box->y2 - box->y1;
1638
1639 pixman_region32_fini(&region);
1640}
1641
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642static int
1643surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001644 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645{
1646 struct weston_resize_grab *resize;
1647
Rafael Antognolli03b16592013-12-03 15:35:42 -02001648 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001649 return 0;
1650
1651 if (edges == 0 || edges > 15 ||
1652 (edges & 3) == 3 || (edges & 12) == 12)
1653 return 0;
1654
1655 resize = malloc(sizeof *resize);
1656 if (!resize)
1657 return -1;
1658
1659 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001660 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1661 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001662
1663 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001664 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001665
1666 return 0;
1667}
1668
1669static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001670common_surface_resize(struct wl_resource *resource,
1671 struct wl_resource *seat_resource, uint32_t serial,
1672 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001673{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001674 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001675 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001676 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677
Rafael Antognolli03b16592013-12-03 15:35:42 -02001678 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001679 return;
1680
Kristian Høgsberge3148752013-05-06 23:19:49 -04001681 if (seat->pointer->button_count == 0 ||
1682 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001683 seat->pointer->focus == NULL)
1684 return;
1685
1686 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1687 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688 return;
1689
Kristian Høgsberge3148752013-05-06 23:19:49 -04001690 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001691 wl_resource_post_no_memory(resource);
1692}
1693
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001694static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001695shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1696 struct wl_resource *seat_resource, uint32_t serial,
1697 uint32_t edges)
1698{
1699 common_surface_resize(resource, seat_resource, serial, edges);
1700}
1701
1702static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001703end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1704
1705static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001706busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001707{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001708 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001709 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001711 wl_fixed_t sx, sy;
1712
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713 view = weston_compositor_pick_view(pointer->seat->compositor,
1714 pointer->x, pointer->y,
1715 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001716
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001718 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001719}
1720
1721static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001722busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1723 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001724{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001725 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001726}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001727
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001728static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001729busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001730 uint32_t time, uint32_t button, uint32_t state)
1731{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001732 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001733 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001734 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001735
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001736 if (shsurf && button == BTN_LEFT && state) {
1737 activate(shsurf->shell, shsurf->surface, seat);
1738 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001739 } else if (shsurf && button == BTN_RIGHT && state) {
1740 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001741 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001742 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001743}
1744
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001745static void
1746busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1747{
1748 struct shell_grab *grab = (struct shell_grab *) base;
1749
1750 shell_grab_end(grab);
1751 free(grab);
1752}
1753
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001754static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001755 busy_cursor_grab_focus,
1756 busy_cursor_grab_motion,
1757 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001758 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001759};
1760
1761static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001762set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763{
1764 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765
1766 grab = malloc(sizeof *grab);
1767 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001768 return;
1769
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001770 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1771 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001772}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001773
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001774static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001775end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001776{
1777 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1778
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001779 if (grab->grab.interface == &busy_cursor_grab_interface &&
1780 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001781 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001782 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001783 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001784}
1785
Scott Moreau9521d5e2012-04-19 13:06:17 -06001786static void
1787ping_timer_destroy(struct shell_surface *shsurf)
1788{
1789 if (!shsurf || !shsurf->ping_timer)
1790 return;
1791
1792 if (shsurf->ping_timer->source)
1793 wl_event_source_remove(shsurf->ping_timer->source);
1794
1795 free(shsurf->ping_timer);
1796 shsurf->ping_timer = NULL;
1797}
1798
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001799static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001800ping_timeout_handler(void *data)
1801{
1802 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001804
Scott Moreau9521d5e2012-04-19 13:06:17 -06001805 /* Client is not responding */
1806 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001807
1808 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberg8fe412d2014-01-17 16:39:17 -08001809 if (seat->pointer && seat->pointer->focus &&
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001810 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001811 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001812
1813 return 1;
1814}
1815
1816static void
1817ping_handler(struct weston_surface *surface, uint32_t serial)
1818{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001819 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001820 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001821 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001822
1823 if (!shsurf)
1824 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001825 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001826 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001827
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001828 if (shsurf->surface == shsurf->shell->grab_surface)
1829 return;
1830
Scott Moreauff1db4a2012-04-17 19:06:18 -06001831 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001832 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001833 if (!shsurf->ping_timer)
1834 return;
1835
1836 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001837 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1838 shsurf->ping_timer->source =
1839 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1840 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1841
Rafael Antognollie2a34552013-12-03 15:35:45 -02001842 if (shell_surface_is_wl_shell_surface(shsurf))
1843 wl_shell_surface_send_ping(shsurf->resource, serial);
1844 else if (shell_surface_is_xdg_surface(shsurf))
1845 xdg_surface_send_ping(shsurf->resource, serial);
1846 else if (shell_surface_is_xdg_popup(shsurf))
1847 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001848 }
1849}
1850
1851static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001852handle_pointer_focus(struct wl_listener *listener, void *data)
1853{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001854 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001856 struct weston_compositor *compositor;
1857 struct shell_surface *shsurf;
1858 uint32_t serial;
1859
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001861 return;
1862
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863 compositor = view->surface->compositor;
1864 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001865
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001866 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001867 set_busy_cursor(shsurf, pointer);
1868 } else {
1869 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001870 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001871 }
1872}
1873
1874static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001875create_pointer_focus_listener(struct weston_seat *seat)
1876{
1877 struct wl_listener *listener;
1878
Kristian Høgsberge3148752013-05-06 23:19:49 -04001879 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001880 return;
1881
1882 listener = malloc(sizeof *listener);
1883 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001884 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001885}
1886
1887static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001888xdg_surface_set_transient_for(struct wl_client *client,
1889 struct wl_resource *resource,
1890 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001891{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001892 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001893 struct weston_surface *parent;
1894
1895 if (parent_resource)
1896 parent = wl_resource_get_user_data(parent_resource);
1897 else
1898 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001899
1900 shell_surface_set_parent(shsurf, parent);
1901}
1902
1903static void
1904surface_pong(struct shell_surface *shsurf, uint32_t serial)
1905{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001906 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001907 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001908
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001909 if (shsurf->ping_timer == NULL)
1910 /* Just ignore unsolicited pong. */
1911 return;
1912
Scott Moreauff1db4a2012-04-17 19:06:18 -06001913 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001914 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001915 wl_list_for_each(seat, &ec->seat_list, link) {
1916 if(seat->pointer)
1917 end_busy_cursor(shsurf, seat->pointer);
1918 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001919 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001920 }
1921}
1922
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001923static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001924shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1925 uint32_t serial)
1926{
1927 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1928
1929 surface_pong(shsurf, serial);
1930
1931}
1932
1933static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001934set_title(struct shell_surface *shsurf, const char *title)
1935{
1936 free(shsurf->title);
1937 shsurf->title = strdup(title);
1938}
1939
1940static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001941shell_surface_set_title(struct wl_client *client,
1942 struct wl_resource *resource, const char *title)
1943{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001944 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001945
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001946 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001947}
1948
1949static void
1950shell_surface_set_class(struct wl_client *client,
1951 struct wl_resource *resource, const char *class)
1952{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001953 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001954
1955 free(shsurf->class);
1956 shsurf->class = strdup(class);
1957}
1958
Alex Wu4539b082012-03-01 12:57:46 +08001959static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001960restore_output_mode(struct weston_output *output)
1961{
Hardening57388e42013-09-18 23:56:36 +02001962 if (output->current_mode != output->original_mode ||
1963 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001964 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001965 output->original_mode,
1966 output->original_scale,
1967 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001968}
1969
1970static void
1971restore_all_output_modes(struct weston_compositor *compositor)
1972{
1973 struct weston_output *output;
1974
1975 wl_list_for_each(output, &compositor->output_list, link)
1976 restore_output_mode(output);
1977}
1978
Philip Withnallbecb77e2013-11-25 18:01:30 +00001979static int
1980get_output_panel_height(struct desktop_shell *shell,
1981 struct weston_output *output)
1982{
1983 struct weston_view *view;
1984 int panel_height = 0;
1985
1986 if (!output)
1987 return 0;
1988
1989 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1990 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001991 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00001992 break;
1993 }
1994 }
1995
1996 return panel_height;
1997}
1998
Philip Withnall07926d92013-11-25 18:01:40 +00001999/* The surface will be inserted into the list immediately after the link
2000 * returned by this function (i.e. will be stacked immediately above the
2001 * returned link). */
2002static struct wl_list *
2003shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2004{
2005 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002006 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002007
2008 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002009 case SHELL_SURFACE_POPUP:
2010 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002011 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002012 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002013 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002014 /* Move the surface to its parent layer so
2015 * that surfaces which are transient for
2016 * fullscreen surfaces don't get hidden by the
2017 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002018
2019 /* TODO: Handle a parent with multiple views */
2020 parent = get_default_view(shsurf->parent);
2021 if (parent)
2022 return parent->layer_link.prev;
2023 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002024 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002025
2026 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002027 return &shsurf->shell->fullscreen_layer.view_list;
2028
Philip Withnall07926d92013-11-25 18:01:40 +00002029 case SHELL_SURFACE_NONE:
2030 default:
2031 /* Go to the fallback, below. */
2032 break;
2033 }
2034
2035 /* Move the surface to a normal workspace layer so that surfaces
2036 * which were previously fullscreen or transient are no longer
2037 * rendered on top. */
2038 ws = get_current_workspace(shsurf->shell);
2039 return &ws->layer.view_list;
2040}
2041
Philip Withnall648a4dd2013-11-25 18:01:44 +00002042static void
2043shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2044{
2045 struct shell_surface *child;
2046
2047 /* Move the child layers to the same workspace as shsurf. They will be
2048 * stacked above shsurf. */
2049 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2050 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2051 weston_view_geometry_dirty(child->view);
2052 wl_list_remove(&child->view->layer_link);
2053 wl_list_insert(shsurf->view->layer_link.prev,
2054 &child->view->layer_link);
2055 weston_view_geometry_dirty(child->view);
2056 weston_surface_damage(child->surface);
2057
2058 /* Recurse. We don’t expect this to recurse very far (if
2059 * at all) because that would imply we have transient
2060 * (or popup) children of transient surfaces, which
2061 * would be unusual. */
2062 shell_surface_update_child_surface_layers(child);
2063 }
2064 }
2065}
2066
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002067/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002068 * geometry to ensure the changes are redrawn.
2069 *
2070 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2071 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002072static void
2073shell_surface_update_layer(struct shell_surface *shsurf)
2074{
2075 struct wl_list *new_layer_link;
2076
2077 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2078
2079 if (new_layer_link == &shsurf->view->layer_link)
2080 return;
2081
2082 weston_view_geometry_dirty(shsurf->view);
2083 wl_list_remove(&shsurf->view->layer_link);
2084 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2085 weston_view_geometry_dirty(shsurf->view);
2086 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002087
2088 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002089}
2090
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002091static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002092shell_surface_set_parent(struct shell_surface *shsurf,
2093 struct weston_surface *parent)
2094{
2095 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002096
2097 wl_list_remove(&shsurf->children_link);
2098 wl_list_init(&shsurf->children_link);
2099
2100 /* Insert into the parent surface’s child list. */
2101 if (parent != NULL) {
2102 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2103 if (parent_shsurf != NULL)
2104 wl_list_insert(&parent_shsurf->children_list,
2105 &shsurf->children_link);
2106 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002107}
2108
2109static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002110shell_surface_set_output(struct shell_surface *shsurf,
2111 struct weston_output *output)
2112{
2113 struct weston_surface *es = shsurf->surface;
2114
2115 /* get the default output, if the client set it as NULL
2116 check whether the ouput is available */
2117 if (output)
2118 shsurf->output = output;
2119 else if (es->output)
2120 shsurf->output = es->output;
2121 else
2122 shsurf->output = get_default_output(es->compositor);
2123}
2124
2125static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002126surface_clear_next_states(struct shell_surface *shsurf)
2127{
2128 shsurf->next_state.maximized = false;
2129 shsurf->next_state.fullscreen = false;
2130
2131 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2132 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2133 shsurf->state_changed = true;
2134}
2135
2136static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002137set_toplevel(struct shell_surface *shsurf)
2138{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002139 shell_surface_set_parent(shsurf, NULL);
2140 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002141 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002142
2143 /* The layer_link is updated in set_surface_type(),
2144 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002145}
2146
2147static void
2148shell_surface_set_toplevel(struct wl_client *client,
2149 struct wl_resource *resource)
2150{
2151 struct shell_surface *surface = wl_resource_get_user_data(resource);
2152
2153 set_toplevel(surface);
2154}
2155
2156static void
2157set_transient(struct shell_surface *shsurf,
2158 struct weston_surface *parent, int x, int y, uint32_t flags)
2159{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002160 assert(parent != NULL);
2161
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002162 shell_surface_set_parent(shsurf, parent);
2163
2164 surface_clear_next_states(shsurf);
2165
Philip Withnallbecb77e2013-11-25 18:01:30 +00002166 shsurf->transient.x = x;
2167 shsurf->transient.y = y;
2168 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002169
Rafael Antognollied207b42013-12-03 15:35:43 -02002170 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002171 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002172 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002173
2174 /* The layer_link is updated in set_surface_type(),
2175 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002176}
2177
2178static void
2179shell_surface_set_transient(struct wl_client *client,
2180 struct wl_resource *resource,
2181 struct wl_resource *parent_resource,
2182 int x, int y, uint32_t flags)
2183{
2184 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2185 struct weston_surface *parent =
2186 wl_resource_get_user_data(parent_resource);
2187
2188 set_transient(shsurf, parent, x, y, flags);
2189}
2190
2191static void
2192set_fullscreen(struct shell_surface *shsurf,
2193 uint32_t method,
2194 uint32_t framerate,
2195 struct weston_output *output)
2196{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002197 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002198
2199 shsurf->fullscreen_output = shsurf->output;
2200 shsurf->fullscreen.type = method;
2201 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002202
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002203 shsurf->next_state.fullscreen = true;
2204 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002205 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002206
2207 shsurf->client->send_configure(shsurf->surface, 0,
2208 shsurf->output->width,
2209 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002210
2211 /* The layer_link is updated in set_surface_type(),
2212 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002213}
2214
2215static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002216weston_view_set_initial_position(struct weston_view *view,
2217 struct desktop_shell *shell);
2218
2219static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002220unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002221{
Philip Withnallf85fe842013-11-25 18:01:37 +00002222 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002223 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2224 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002225 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002226 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002227 shsurf->fullscreen_output = NULL;
2228
Alex Wu4539b082012-03-01 12:57:46 +08002229 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2230 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002231
Alex Wu4539b082012-03-01 12:57:46 +08002232 wl_list_remove(&shsurf->fullscreen.transform.link);
2233 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002234
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 if (shsurf->fullscreen.black_view)
2236 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2237 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002238
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002239 if (shsurf->saved_position_valid)
2240 weston_view_set_position(shsurf->view,
2241 shsurf->saved_x, shsurf->saved_y);
2242 else
2243 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2244
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002245 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002247 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002248 shsurf->saved_rotation_valid = false;
2249 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002250
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002251 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002252}
2253
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002254static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002255shell_surface_set_fullscreen(struct wl_client *client,
2256 struct wl_resource *resource,
2257 uint32_t method,
2258 uint32_t framerate,
2259 struct wl_resource *output_resource)
2260{
2261 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2262 struct weston_output *output;
2263
2264 if (output_resource)
2265 output = wl_resource_get_user_data(output_resource);
2266 else
2267 output = NULL;
2268
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002269 shell_surface_set_parent(shsurf, NULL);
2270
Rafael Antognolli03b16592013-12-03 15:35:42 -02002271 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002272 set_fullscreen(shsurf, method, framerate, output);
2273}
2274
2275static void
2276set_popup(struct shell_surface *shsurf,
2277 struct weston_surface *parent,
2278 struct weston_seat *seat,
2279 uint32_t serial,
2280 int32_t x,
2281 int32_t y)
2282{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002283 assert(parent != NULL);
2284
Philip Withnallbecb77e2013-11-25 18:01:30 +00002285 shsurf->popup.shseat = get_shell_seat(seat);
2286 shsurf->popup.serial = serial;
2287 shsurf->popup.x = x;
2288 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002289
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002290 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002291}
2292
2293static void
2294shell_surface_set_popup(struct wl_client *client,
2295 struct wl_resource *resource,
2296 struct wl_resource *seat_resource,
2297 uint32_t serial,
2298 struct wl_resource *parent_resource,
2299 int32_t x, int32_t y, uint32_t flags)
2300{
2301 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002302 struct weston_surface *parent =
2303 wl_resource_get_user_data(parent_resource);
2304
2305 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002306
Rafael Antognolli03b16592013-12-03 15:35:42 -02002307 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002308 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002309 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002310 wl_resource_get_user_data(seat_resource),
2311 serial, x, y);
2312}
2313
2314static void
2315set_maximized(struct shell_surface *shsurf,
2316 struct weston_output *output)
2317{
2318 struct desktop_shell *shell;
2319 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002320
Philip Withnall352e7ed2013-11-25 18:01:35 +00002321 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002322
2323 shell = shell_surface_get_shell(shsurf);
2324 panel_height = get_output_panel_height(shell, shsurf->output);
2325 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2326
2327 shsurf->client->send_configure(shsurf->surface, edges,
2328 shsurf->output->width,
2329 shsurf->output->height - panel_height);
2330
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002331 shsurf->next_state.maximized = true;
2332 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002333 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002334}
2335
2336static void
2337unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002338{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002339 /* undo all maximized things here */
2340 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002341
2342 if (shsurf->saved_position_valid)
2343 weston_view_set_position(shsurf->view,
2344 shsurf->saved_x, shsurf->saved_y);
2345 else
2346 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002347
2348 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 wl_list_insert(&shsurf->view->geometry.transformation_list,
2350 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002351 shsurf->saved_rotation_valid = false;
2352 }
2353
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002354 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002355}
2356
Philip Withnallbecb77e2013-11-25 18:01:30 +00002357static void
2358shell_surface_set_maximized(struct wl_client *client,
2359 struct wl_resource *resource,
2360 struct wl_resource *output_resource)
2361{
2362 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2363 struct weston_output *output;
2364
2365 if (output_resource)
2366 output = wl_resource_get_user_data(output_resource);
2367 else
2368 output = NULL;
2369
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002370 shell_surface_set_parent(shsurf, NULL);
2371
Rafael Antognolli03b16592013-12-03 15:35:42 -02002372 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002373 set_maximized(shsurf, output);
2374}
2375
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002376/* This is only ever called from set_surface_type(), so there’s no need to
2377 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002378static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002379reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002380{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002381 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002382 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002383 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002384 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002385
Pekka Paalanen98262232011-12-01 10:42:22 +02002386 return 0;
2387}
2388
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002389static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002390set_full_output(struct shell_surface *shsurf)
2391{
2392 shsurf->saved_x = shsurf->view->geometry.x;
2393 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002394 shsurf->saved_width = shsurf->surface->width;
2395 shsurf->saved_height = shsurf->surface->height;
2396 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002397 shsurf->saved_position_valid = true;
2398
2399 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2400 wl_list_remove(&shsurf->rotation.transform.link);
2401 wl_list_init(&shsurf->rotation.transform.link);
2402 weston_view_geometry_dirty(shsurf->view);
2403 shsurf->saved_rotation_valid = true;
2404 }
2405}
2406
2407static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002408set_surface_type(struct shell_surface *shsurf)
2409{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002410 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002411 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002412
Philip Withnallbecb77e2013-11-25 18:01:30 +00002413 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002414
Rafael Antognolli03b16592013-12-03 15:35:42 -02002415 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002416 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002417
2418 switch (shsurf->type) {
2419 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002420 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002421 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002422 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002423 weston_view_set_position(shsurf->view,
2424 pev->geometry.x + shsurf->transient.x,
2425 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002426 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002427 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002428
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002429 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002430 weston_view_set_position(shsurf->view, shsurf->transient.x,
2431 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002432 break;
2433
Philip Withnall0f640e22013-11-25 18:01:31 +00002434 case SHELL_SURFACE_POPUP:
2435 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002436 default:
2437 break;
2438 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002439
2440 /* Update the surface’s layer. */
2441 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002442}
2443
Tiago Vignattibe143262012-04-16 17:31:41 +03002444static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002445shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002446{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002447 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002448}
2449
Alex Wu21858432012-04-01 20:13:08 +08002450static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002451black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002452
Jason Ekstranda7af7042013-10-12 22:38:11 -05002453static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002454create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002455 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002456 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002457{
2458 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002459 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002460
2461 surface = weston_surface_create(ec);
2462 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002463 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002464 return NULL;
2465 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002466 view = weston_view_create(surface);
2467 if (surface == NULL) {
2468 weston_log("no memory\n");
2469 weston_surface_destroy(surface);
2470 return NULL;
2471 }
Alex Wu4539b082012-03-01 12:57:46 +08002472
Alex Wu21858432012-04-01 20:13:08 +08002473 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002474 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002475 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002476 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002477 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002478 pixman_region32_fini(&surface->input);
2479 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002480
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002481 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002482 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002483
2484 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002485}
2486
Philip Withnalled8f1a92013-11-25 18:01:43 +00002487static void
2488shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2489{
2490 struct weston_output *output = shsurf->fullscreen_output;
2491
Rafael Antognolli03b16592013-12-03 15:35:42 -02002492 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002493
2494 if (!shsurf->fullscreen.black_view)
2495 shsurf->fullscreen.black_view =
2496 create_black_surface(shsurf->surface->compositor,
2497 shsurf->surface,
2498 output->x, output->y,
2499 output->width,
2500 output->height);
2501
2502 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2503 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2504 wl_list_insert(&shsurf->view->layer_link,
2505 &shsurf->fullscreen.black_view->layer_link);
2506 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2507 weston_surface_damage(shsurf->surface);
2508}
2509
Alex Wu4539b082012-03-01 12:57:46 +08002510/* Create black surface and append it to the associated fullscreen surface.
2511 * Handle size dismatch and positioning according to the method. */
2512static void
2513shell_configure_fullscreen(struct shell_surface *shsurf)
2514{
2515 struct weston_output *output = shsurf->fullscreen_output;
2516 struct weston_surface *surface = shsurf->surface;
2517 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002518 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002519 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002520
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002521 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2522 restore_output_mode(output);
2523
Philip Withnalled8f1a92013-11-25 18:01:43 +00002524 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002525
Jason Ekstranda7af7042013-10-12 22:38:11 -05002526 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002527 &surf_width, &surf_height);
2528
Alex Wu4539b082012-03-01 12:57:46 +08002529 switch (shsurf->fullscreen.type) {
2530 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002531 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002532 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002533 break;
2534 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002535 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002536 if (output->width == surf_width &&
2537 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002538 weston_view_set_position(shsurf->view,
2539 output->x - surf_x,
2540 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002541 break;
2542 }
2543
Alex Wu4539b082012-03-01 12:57:46 +08002544 matrix = &shsurf->fullscreen.transform.matrix;
2545 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002546
Scott Moreau1bad5db2012-08-18 01:04:05 -06002547 output_aspect = (float) output->width /
2548 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002549 /* XXX: Use surf_width and surf_height here? */
2550 surface_aspect = (float) surface->width /
2551 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002552 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002553 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002554 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002555 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002556 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002557 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002558
Alex Wu4539b082012-03-01 12:57:46 +08002559 weston_matrix_scale(matrix, scale, scale, 1);
2560 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002561 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002562 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002563 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2564 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002565 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002566
Alex Wu4539b082012-03-01 12:57:46 +08002567 break;
2568 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002569 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002570 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002571 surf_width * surface->buffer_viewport.scale,
2572 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002573 shsurf->fullscreen.framerate};
2574
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002575 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002576 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 weston_view_set_position(shsurf->view,
2578 output->x - surf_x,
2579 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002580 shsurf->fullscreen.black_view->surface->width = output->width;
2581 shsurf->fullscreen.black_view->surface->height = output->height;
2582 weston_view_set_position(shsurf->fullscreen.black_view,
2583 output->x - surf_x,
2584 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002585 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002586 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002587 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002588 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002589 }
Alex Wubd3354b2012-04-17 17:20:49 +08002590 }
Alex Wu4539b082012-03-01 12:57:46 +08002591 break;
2592 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002594 break;
2595 default:
2596 break;
2597 }
2598}
2599
Alex Wu4539b082012-03-01 12:57:46 +08002600static void
2601shell_map_fullscreen(struct shell_surface *shsurf)
2602{
Alex Wubd3354b2012-04-17 17:20:49 +08002603 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002604}
2605
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002606static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002607set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2608{
2609 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002610 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002611 shsurf->transient.x = x;
2612 shsurf->transient.y = y;
2613 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002614
2615 shell_surface_set_parent(shsurf, NULL);
2616
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002617 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002618 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002619}
2620
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002621static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002622
2623static void
2624destroy_shell_seat(struct wl_listener *listener, void *data)
2625{
2626 struct shell_seat *shseat =
2627 container_of(listener,
2628 struct shell_seat, seat_destroy_listener);
2629 struct shell_surface *shsurf, *prev = NULL;
2630
2631 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002632 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002633 shseat->popup_grab.client = NULL;
2634
2635 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2636 shsurf->popup.shseat = NULL;
2637 if (prev) {
2638 wl_list_init(&prev->popup.grab_link);
2639 }
2640 prev = shsurf;
2641 }
2642 wl_list_init(&prev->popup.grab_link);
2643 }
2644
2645 wl_list_remove(&shseat->seat_destroy_listener.link);
2646 free(shseat);
2647}
2648
2649static struct shell_seat *
2650create_shell_seat(struct weston_seat *seat)
2651{
2652 struct shell_seat *shseat;
2653
2654 shseat = calloc(1, sizeof *shseat);
2655 if (!shseat) {
2656 weston_log("no memory to allocate shell seat\n");
2657 return NULL;
2658 }
2659
2660 shseat->seat = seat;
2661 wl_list_init(&shseat->popup_grab.surfaces_list);
2662
2663 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2664 wl_signal_add(&seat->destroy_signal,
2665 &shseat->seat_destroy_listener);
2666
2667 return shseat;
2668}
2669
2670static struct shell_seat *
2671get_shell_seat(struct weston_seat *seat)
2672{
2673 struct wl_listener *listener;
2674
2675 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2676 if (listener == NULL)
2677 return create_shell_seat(seat);
2678
2679 return container_of(listener,
2680 struct shell_seat, seat_destroy_listener);
2681}
2682
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002683static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002684popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002685{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002686 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002688 struct shell_seat *shseat =
2689 container_of(grab, struct shell_seat, popup_grab.grab);
2690 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002691 wl_fixed_t sx, sy;
2692
Jason Ekstranda7af7042013-10-12 22:38:11 -05002693 view = weston_compositor_pick_view(pointer->seat->compositor,
2694 pointer->x, pointer->y,
2695 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002696
Jason Ekstranda7af7042013-10-12 22:38:11 -05002697 if (view && view->surface->resource &&
2698 wl_resource_get_client(view->surface->resource) == client) {
2699 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002700 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002701 weston_pointer_set_focus(pointer, NULL,
2702 wl_fixed_from_int(0),
2703 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002704 }
2705}
2706
2707static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002708popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2709 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002710{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002711 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002712 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002713 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002714
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002715 weston_pointer_move(pointer, x, y);
2716
Neil Roberts96d790e2013-09-19 17:32:00 +01002717 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002718 weston_view_from_global_fixed(pointer->focus,
2719 pointer->x, pointer->y,
2720 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002721 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002722 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002723}
2724
2725static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002726popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002727 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002728{
2729 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002730 struct shell_seat *shseat =
2731 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002732 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002733 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002734 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002735 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002736
Neil Roberts96d790e2013-09-19 17:32:00 +01002737 resource_list = &grab->pointer->focus_resource_list;
2738 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002739 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002740 wl_resource_for_each(resource, resource_list) {
2741 wl_pointer_send_button(resource, serial,
2742 time, button, state);
2743 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002744 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002745 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002746 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002747 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002748 }
2749
Daniel Stone4dbadb12012-05-30 16:31:51 +01002750 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002751 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002752}
2753
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002754static void
2755popup_grab_cancel(struct weston_pointer_grab *grab)
2756{
2757 popup_grab_end(grab->pointer);
2758}
2759
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002760static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002761 popup_grab_focus,
2762 popup_grab_motion,
2763 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002764 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002765};
2766
2767static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002768shell_surface_send_popup_done(struct shell_surface *shsurf)
2769{
2770 if (shell_surface_is_wl_shell_surface(shsurf))
2771 wl_shell_surface_send_popup_done(shsurf->resource);
2772 else if (shell_surface_is_xdg_popup(shsurf))
2773 xdg_popup_send_popup_done(shsurf->resource,
2774 shsurf->popup.serial);
2775}
2776
2777static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002778popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002779{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002780 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002781 struct shell_seat *shseat =
2782 container_of(grab, struct shell_seat, popup_grab.grab);
2783 struct shell_surface *shsurf;
2784 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002785
2786 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002787 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002788 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002789 shseat->popup_grab.grab.interface = NULL;
2790 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002791 /* Send the popup_done event to all the popups open */
2792 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002793 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002794 shsurf->popup.shseat = NULL;
2795 if (prev) {
2796 wl_list_init(&prev->popup.grab_link);
2797 }
2798 prev = shsurf;
2799 }
2800 wl_list_init(&prev->popup.grab_link);
2801 wl_list_init(&shseat->popup_grab.surfaces_list);
2802 }
2803}
2804
2805static void
2806add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2807{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002808 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002809
2810 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002811 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002812 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002813 /* We must make sure here that this popup was opened after
2814 * a mouse press, and not just by moving around with other
2815 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002816 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002817 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002818
Rob Bradforddfe31052013-06-26 19:49:11 +01002819 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002820 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002821 } else {
2822 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002823 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002824}
2825
2826static void
2827remove_popup_grab(struct shell_surface *shsurf)
2828{
2829 struct shell_seat *shseat = shsurf->popup.shseat;
2830
2831 wl_list_remove(&shsurf->popup.grab_link);
2832 wl_list_init(&shsurf->popup.grab_link);
2833 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002834 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002835 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002836 }
2837}
2838
2839static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002840shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002841{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002842 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002843 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002844
Jason Ekstranda7af7042013-10-12 22:38:11 -05002845 shsurf->surface->output = parent_view->output;
2846 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002847
Jason Ekstranda7af7042013-10-12 22:38:11 -05002848 weston_view_set_transform_parent(shsurf->view, parent_view);
2849 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2850 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002851
Kristian Høgsberge3148752013-05-06 23:19:49 -04002852 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002853 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002854 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002855 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002856 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002857 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002858}
2859
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002860static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002861 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002862 shell_surface_move,
2863 shell_surface_resize,
2864 shell_surface_set_toplevel,
2865 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002866 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002867 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002868 shell_surface_set_maximized,
2869 shell_surface_set_title,
2870 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002871};
2872
2873static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002874destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002875{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002876 struct shell_surface *child, *next;
2877
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002878 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2879
Giulio Camuffo5085a752013-03-25 21:42:45 +01002880 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2881 remove_popup_grab(shsurf);
2882 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002883
Alex Wubd3354b2012-04-17 17:20:49 +08002884 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002885 shell_surface_is_top_fullscreen(shsurf))
2886 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002887
Jason Ekstranda7af7042013-10-12 22:38:11 -05002888 if (shsurf->fullscreen.black_view)
2889 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002890
Alex Wubd3354b2012-04-17 17:20:49 +08002891 /* As destroy_resource() use wl_list_for_each_safe(),
2892 * we can always remove the listener.
2893 */
2894 wl_list_remove(&shsurf->surface_destroy_listener.link);
2895 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002896 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002897 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002898
Jason Ekstranda7af7042013-10-12 22:38:11 -05002899 weston_view_destroy(shsurf->view);
2900
Philip Withnall648a4dd2013-11-25 18:01:44 +00002901 wl_list_remove(&shsurf->children_link);
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002902 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link) {
2903 wl_list_remove(&child->children_link);
2904 child->parent = NULL;
2905 }
Philip Withnall648a4dd2013-11-25 18:01:44 +00002906
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002907 wl_list_remove(&shsurf->link);
2908 free(shsurf);
2909}
2910
2911static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002912shell_destroy_shell_surface(struct wl_resource *resource)
2913{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002914 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002915
2916 destroy_shell_surface(shsurf);
2917}
2918
2919static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002920shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002921{
2922 struct shell_surface *shsurf = container_of(listener,
2923 struct shell_surface,
2924 surface_destroy_listener);
2925
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002926 if (shsurf->resource)
2927 wl_resource_destroy(shsurf->resource);
2928 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002929 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002930}
2931
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002932static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002933shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002934static void
2935shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002936
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002937struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002938get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002939{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002940 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002941 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002942 else
2943 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002944}
2945
Rafael Antognollie2a34552013-12-03 15:35:45 -02002946static struct shell_surface *
2947create_common_surface(void *shell, struct weston_surface *surface,
2948 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002949{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002950 struct shell_surface *shsurf;
2951
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002952 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002953 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002954 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002955 }
2956
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002957 shsurf = calloc(1, sizeof *shsurf);
2958 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002959 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002960 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002961 }
2962
Jason Ekstranda7af7042013-10-12 22:38:11 -05002963 shsurf->view = weston_view_create(surface);
2964 if (!shsurf->view) {
2965 weston_log("no memory to allocate shell surface\n");
2966 free(shsurf);
2967 return NULL;
2968 }
2969
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002970 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002971 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002972 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002973
Tiago Vignattibc052c92012-04-19 16:18:18 +03002974 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002975 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002976 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002977 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002978 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002979 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002980 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2981 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002982 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002983 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002984 wl_list_init(&shsurf->fullscreen.transform.link);
2985
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002986 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002987 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002988 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002989 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002990
2991 /* init link so its safe to always remove it in destroy_shell_surface */
2992 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002993 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002994
Pekka Paalanen460099f2012-01-20 16:48:25 +02002995 /* empty when not in use */
2996 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002997 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002998
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002999 wl_list_init(&shsurf->workspace_transform.link);
3000
Philip Withnall648a4dd2013-11-25 18:01:44 +00003001 wl_list_init(&shsurf->children_link);
3002 wl_list_init(&shsurf->children_list);
3003 shsurf->parent = NULL;
3004
Pekka Paalanen98262232011-12-01 10:42:22 +02003005 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003006
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003007 shsurf->client = client;
3008
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003009 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003010}
3011
Rafael Antognollie2a34552013-12-03 15:35:45 -02003012static struct shell_surface *
3013create_shell_surface(void *shell, struct weston_surface *surface,
3014 const struct weston_shell_client *client)
3015{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003016 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003017}
3018
Jason Ekstranda7af7042013-10-12 22:38:11 -05003019static struct weston_view *
3020get_primary_view(void *shell, struct shell_surface *shsurf)
3021{
3022 return shsurf->view;
3023}
3024
Tiago Vignattibc052c92012-04-19 16:18:18 +03003025static void
3026shell_get_shell_surface(struct wl_client *client,
3027 struct wl_resource *resource,
3028 uint32_t id,
3029 struct wl_resource *surface_resource)
3030{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003031 struct weston_surface *surface =
3032 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003033 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003034 struct shell_surface *shsurf;
3035
3036 if (get_shell_surface(surface)) {
3037 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003038 WL_DISPLAY_ERROR_INVALID_OBJECT,
3039 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003040 return;
3041 }
3042
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003043 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003044 if (!shsurf) {
3045 wl_resource_post_error(surface_resource,
3046 WL_DISPLAY_ERROR_INVALID_OBJECT,
3047 "surface->configure already set");
3048 return;
3049 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003050
Jason Ekstranda85118c2013-06-27 20:17:02 -05003051 shsurf->resource =
3052 wl_resource_create(client,
3053 &wl_shell_surface_interface, 1, id);
3054 wl_resource_set_implementation(shsurf->resource,
3055 &shell_surface_implementation,
3056 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003057}
3058
Rafael Antognollie2a34552013-12-03 15:35:45 -02003059static bool
3060shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3061{
3062 return wl_resource_instance_of(shsurf->resource,
3063 &wl_shell_surface_interface,
3064 &shell_surface_implementation);
3065}
3066
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003067static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003068 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003069};
3070
Rafael Antognollie2a34552013-12-03 15:35:45 -02003071/****************************
3072 * xdg-shell implementation */
3073
3074static void
3075xdg_surface_destroy(struct wl_client *client,
3076 struct wl_resource *resource)
3077{
3078 wl_resource_destroy(resource);
3079}
3080
3081static void
3082xdg_surface_pong(struct wl_client *client,
3083 struct wl_resource *resource,
3084 uint32_t serial)
3085{
3086 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3087
3088 surface_pong(shsurf, serial);
3089}
3090
3091static void
3092xdg_surface_set_app_id(struct wl_client *client,
3093 struct wl_resource *resource,
3094 const char *app_id)
3095{
3096 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3097
3098 free(shsurf->class);
3099 shsurf->class = strdup(app_id);
3100}
3101
3102static void
3103xdg_surface_set_title(struct wl_client *client,
3104 struct wl_resource *resource, const char *title)
3105{
3106 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3107
3108 set_title(shsurf, title);
3109}
3110
3111static void
3112xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3113 struct wl_resource *seat_resource, uint32_t serial)
3114{
3115 common_surface_move(resource, seat_resource, serial);
3116}
3117
3118static void
3119xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3120 struct wl_resource *seat_resource, uint32_t serial,
3121 uint32_t edges)
3122{
3123 common_surface_resize(resource, seat_resource, serial, edges);
3124}
3125
3126static void
3127xdg_surface_set_output(struct wl_client *client,
3128 struct wl_resource *resource,
3129 struct wl_resource *output_resource)
3130{
3131 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3132 struct weston_output *output;
3133
3134 if (output_resource)
3135 output = wl_resource_get_user_data(output_resource);
3136 else
3137 output = NULL;
3138
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003139 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003140}
3141
3142static void
3143xdg_surface_set_fullscreen(struct wl_client *client,
3144 struct wl_resource *resource)
3145{
3146 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3147
3148 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3149 return;
3150
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003151 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003152 set_fullscreen(shsurf,
3153 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003154 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003155}
3156
3157static void
3158xdg_surface_unset_fullscreen(struct wl_client *client,
3159 struct wl_resource *resource)
3160{
3161 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003162 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003163
3164 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3165 return;
3166
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003167 if (!shsurf->next_state.fullscreen)
3168 return;
3169
Rafael Antognollie2a34552013-12-03 15:35:45 -02003170 shsurf->next_state.fullscreen = false;
3171 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003172
3173 if (shsurf->saved_size_valid) {
3174 width = shsurf->saved_width;
3175 height = shsurf->saved_height;
3176 shsurf->saved_size_valid = false;
3177 } else {
3178 width = shsurf->surface->width;
3179 height = shsurf->surface->height;
3180 }
3181
3182 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003183}
3184
3185static void
3186xdg_surface_set_maximized(struct wl_client *client,
3187 struct wl_resource *resource)
3188{
3189 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3190
3191 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3192 return;
3193
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003194 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003195 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003196}
3197
3198static void
3199xdg_surface_unset_maximized(struct wl_client *client,
3200 struct wl_resource *resource)
3201{
3202 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003203 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003204
3205 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3206 return;
3207
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003208 if (!shsurf->next_state.maximized)
3209 return;
3210
Rafael Antognollie2a34552013-12-03 15:35:45 -02003211 shsurf->next_state.maximized = false;
3212 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003213
3214 if (shsurf->saved_size_valid) {
3215 width = shsurf->saved_width;
3216 height = shsurf->saved_height;
3217 shsurf->saved_size_valid = false;
3218 } else {
3219 width = shsurf->surface->width;
3220 height = shsurf->surface->height;
3221 }
3222
3223 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003224}
3225
3226static const struct xdg_surface_interface xdg_surface_implementation = {
3227 xdg_surface_destroy,
3228 xdg_surface_set_transient_for,
3229 xdg_surface_set_title,
3230 xdg_surface_set_app_id,
3231 xdg_surface_pong,
3232 xdg_surface_move,
3233 xdg_surface_resize,
3234 xdg_surface_set_output,
3235 xdg_surface_set_fullscreen,
3236 xdg_surface_unset_fullscreen,
3237 xdg_surface_set_maximized,
3238 xdg_surface_unset_maximized,
3239 NULL /* set_minimized */
3240};
3241
3242static void
3243xdg_send_configure(struct weston_surface *surface,
3244 uint32_t edges, int32_t width, int32_t height)
3245{
3246 struct shell_surface *shsurf = get_shell_surface(surface);
3247
3248 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3249}
3250
3251static const struct weston_shell_client xdg_client = {
3252 xdg_send_configure
3253};
3254
3255static void
3256xdg_use_unstable_version(struct wl_client *client,
3257 struct wl_resource *resource,
3258 int32_t version)
3259{
3260 if (version > 1) {
3261 wl_resource_post_error(resource,
3262 1,
3263 "xdg-shell:: version not implemented yet.");
3264 return;
3265 }
3266}
3267
3268static struct shell_surface *
3269create_xdg_surface(void *shell, struct weston_surface *surface,
3270 const struct weston_shell_client *client)
3271{
3272 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003273
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003274 shsurf = create_common_surface(shell, surface, client);
3275 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003276
3277 return shsurf;
3278}
3279
3280static void
3281xdg_get_xdg_surface(struct wl_client *client,
3282 struct wl_resource *resource,
3283 uint32_t id,
3284 struct wl_resource *surface_resource)
3285{
3286 struct weston_surface *surface =
3287 wl_resource_get_user_data(surface_resource);
3288 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3289 struct shell_surface *shsurf;
3290
3291 if (get_shell_surface(surface)) {
3292 wl_resource_post_error(surface_resource,
3293 WL_DISPLAY_ERROR_INVALID_OBJECT,
3294 "desktop_shell::get_shell_surface already requested");
3295 return;
3296 }
3297
3298 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3299 if (!shsurf) {
3300 wl_resource_post_error(surface_resource,
3301 WL_DISPLAY_ERROR_INVALID_OBJECT,
3302 "surface->configure already set");
3303 return;
3304 }
3305
3306 shsurf->resource =
3307 wl_resource_create(client,
3308 &xdg_surface_interface, 1, id);
3309 wl_resource_set_implementation(shsurf->resource,
3310 &xdg_surface_implementation,
3311 shsurf, shell_destroy_shell_surface);
3312}
3313
3314static bool
3315shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3316{
3317 return wl_resource_instance_of(shsurf->resource,
3318 &xdg_surface_interface,
3319 &xdg_surface_implementation);
3320}
3321
3322/* xdg-popup implementation */
3323
3324static void
3325xdg_popup_destroy(struct wl_client *client,
3326 struct wl_resource *resource)
3327{
3328 wl_resource_destroy(resource);
3329}
3330
3331static void
3332xdg_popup_pong(struct wl_client *client,
3333 struct wl_resource *resource,
3334 uint32_t serial)
3335{
3336 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3337
3338 surface_pong(shsurf, serial);
3339}
3340
3341static const struct xdg_popup_interface xdg_popup_implementation = {
3342 xdg_popup_destroy,
3343 xdg_popup_pong
3344};
3345
3346static void
3347xdg_popup_send_configure(struct weston_surface *surface,
3348 uint32_t edges, int32_t width, int32_t height)
3349{
3350}
3351
3352static const struct weston_shell_client xdg_popup_client = {
3353 xdg_popup_send_configure
3354};
3355
3356static struct shell_surface *
3357create_xdg_popup(void *shell, struct weston_surface *surface,
3358 const struct weston_shell_client *client,
3359 struct weston_surface *parent,
3360 struct shell_seat *seat,
3361 uint32_t serial,
3362 int32_t x, int32_t y)
3363{
3364 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003365
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003366 shsurf = create_common_surface(shell, surface, client);
3367 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003368 shsurf->popup.shseat = seat;
3369 shsurf->popup.serial = serial;
3370 shsurf->popup.x = x;
3371 shsurf->popup.y = y;
3372 shell_surface_set_parent(shsurf, parent);
3373
3374 return shsurf;
3375}
3376
3377static void
3378xdg_get_xdg_popup(struct wl_client *client,
3379 struct wl_resource *resource,
3380 uint32_t id,
3381 struct wl_resource *surface_resource,
3382 struct wl_resource *parent_resource,
3383 struct wl_resource *seat_resource,
3384 uint32_t serial,
3385 int32_t x, int32_t y, uint32_t flags)
3386{
3387 struct weston_surface *surface =
3388 wl_resource_get_user_data(surface_resource);
3389 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3390 struct shell_surface *shsurf;
3391 struct weston_surface *parent;
3392 struct shell_seat *seat;
3393
3394 if (get_shell_surface(surface)) {
3395 wl_resource_post_error(surface_resource,
3396 WL_DISPLAY_ERROR_INVALID_OBJECT,
3397 "desktop_shell::get_shell_surface already requested");
3398 return;
3399 }
3400
3401 if (!parent_resource) {
3402 wl_resource_post_error(surface_resource,
3403 WL_DISPLAY_ERROR_INVALID_OBJECT,
3404 "xdg_shell::get_xdg_popup requires a parent shell surface");
3405 }
3406
3407 parent = wl_resource_get_user_data(parent_resource);
3408 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3409
3410 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3411 parent, seat, serial, x, y);
3412 if (!shsurf) {
3413 wl_resource_post_error(surface_resource,
3414 WL_DISPLAY_ERROR_INVALID_OBJECT,
3415 "surface->configure already set");
3416 return;
3417 }
3418
3419 shsurf->resource =
3420 wl_resource_create(client,
3421 &xdg_popup_interface, 1, id);
3422 wl_resource_set_implementation(shsurf->resource,
3423 &xdg_popup_implementation,
3424 shsurf, shell_destroy_shell_surface);
3425}
3426
3427static bool
3428shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3429{
3430 return wl_resource_instance_of(shsurf->resource,
3431 &xdg_popup_interface,
3432 &xdg_popup_implementation);
3433}
3434
3435static const struct xdg_shell_interface xdg_implementation = {
3436 xdg_use_unstable_version,
3437 xdg_get_xdg_surface,
3438 xdg_get_xdg_popup
3439};
3440
3441static int
3442xdg_shell_unversioned_dispatch(const void *implementation,
3443 void *_target, uint32_t opcode,
3444 const struct wl_message *message,
3445 union wl_argument *args)
3446{
3447 struct wl_resource *resource = _target;
3448 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3449
3450 if (opcode != 0) {
3451 wl_resource_post_error(resource,
3452 WL_DISPLAY_ERROR_INVALID_OBJECT,
3453 "must call use_unstable_version first");
3454 return 0;
3455 }
3456
3457#define XDG_SERVER_VERSION 1
3458
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003459 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3460 "shell implementation doesn't match protocol version");
3461
Rafael Antognollie2a34552013-12-03 15:35:45 -02003462 if (args[0].i != XDG_SERVER_VERSION) {
3463 wl_resource_post_error(resource,
3464 WL_DISPLAY_ERROR_INVALID_OBJECT,
3465 "incompatible version, server is %d "
3466 "client wants %d",
3467 XDG_SERVER_VERSION, args[0].i);
3468 return 0;
3469 }
3470
3471 wl_resource_set_implementation(resource, &xdg_implementation,
3472 shell, NULL);
3473
3474 return 1;
3475}
3476
3477/* end of xdg-shell implementation */
3478/***********************************/
3479
Kristian Høgsberg07937562011-04-12 17:25:42 -04003480static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003481shell_fade(struct desktop_shell *shell, enum fade_type type);
3482
3483static int
3484screensaver_timeout(void *data)
3485{
3486 struct desktop_shell *shell = data;
3487
3488 shell_fade(shell, FADE_OUT);
3489
3490 return 1;
3491}
3492
3493static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003494handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003495{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003496 struct desktop_shell *shell =
3497 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003498
Pekka Paalanen18027e52011-12-02 16:31:49 +02003499 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003500
3501 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003502 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003503}
3504
3505static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003506launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003507{
3508 if (shell->screensaver.binding)
3509 return;
3510
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003511 if (!shell->screensaver.path) {
3512 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003513 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003514 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003515
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003516 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003517 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003518 return;
3519 }
3520
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003521 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003522 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003523 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003524 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003525}
3526
3527static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003528terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003529{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003530 if (shell->screensaver.process.pid == 0)
3531 return;
3532
3533 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003534}
3535
3536static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003537configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003538{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003539 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003540
Jason Ekstranda7af7042013-10-12 22:38:11 -05003541 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3542 if (v->output == ev->output && v != ev) {
3543 weston_view_unmap(v);
3544 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003545 }
3546 }
3547
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003548 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003549
Jason Ekstranda7af7042013-10-12 22:38:11 -05003550 if (wl_list_empty(&ev->layer_link)) {
3551 wl_list_insert(&layer->view_list, &ev->layer_link);
3552 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003553 }
3554}
3555
3556static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003557background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003558{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003559 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003560 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003561
Jason Ekstranda7af7042013-10-12 22:38:11 -05003562 view = container_of(es->views.next, struct weston_view, surface_link);
3563
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003564 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003565}
3566
3567static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003568desktop_shell_set_background(struct wl_client *client,
3569 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003570 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003571 struct wl_resource *surface_resource)
3572{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003573 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003574 struct weston_surface *surface =
3575 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003576 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003577
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003578 if (surface->configure) {
3579 wl_resource_post_error(surface_resource,
3580 WL_DISPLAY_ERROR_INVALID_OBJECT,
3581 "surface role already assigned");
3582 return;
3583 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003584
Jason Ekstranda7af7042013-10-12 22:38:11 -05003585 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3586 weston_view_destroy(view);
3587 view = weston_view_create(surface);
3588
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003589 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003590 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003591 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003593 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003594 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003595 surface->output->width,
3596 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003597}
3598
3599static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003600panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003601{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003602 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003603 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003604
Jason Ekstranda7af7042013-10-12 22:38:11 -05003605 view = container_of(es->views.next, struct weston_view, surface_link);
3606
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003607 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003608}
3609
3610static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003611desktop_shell_set_panel(struct wl_client *client,
3612 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003613 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003614 struct wl_resource *surface_resource)
3615{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003616 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003617 struct weston_surface *surface =
3618 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003619 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003620
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003621 if (surface->configure) {
3622 wl_resource_post_error(surface_resource,
3623 WL_DISPLAY_ERROR_INVALID_OBJECT,
3624 "surface role already assigned");
3625 return;
3626 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003627
Jason Ekstranda7af7042013-10-12 22:38:11 -05003628 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3629 weston_view_destroy(view);
3630 view = weston_view_create(surface);
3631
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003632 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003633 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003634 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003635 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003636 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003637 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003638 surface->output->width,
3639 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003640}
3641
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003642static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003643lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003644{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003645 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003646 struct weston_view *view;
3647
3648 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003649
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003650 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003651 return;
3652
Jason Ekstranda7af7042013-10-12 22:38:11 -05003653 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003654
3655 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003656 wl_list_insert(&shell->lock_layer.view_list,
3657 &view->layer_link);
3658 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003659 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003660 }
3661}
3662
3663static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003664handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003665{
Tiago Vignattibe143262012-04-16 17:31:41 +03003666 struct desktop_shell *shell =
3667 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003668
Martin Minarik6d118362012-06-07 18:01:59 +02003669 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003670 shell->lock_surface = NULL;
3671}
3672
3673static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003674desktop_shell_set_lock_surface(struct wl_client *client,
3675 struct wl_resource *resource,
3676 struct wl_resource *surface_resource)
3677{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003678 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003679 struct weston_surface *surface =
3680 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003681
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003682 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003683
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003684 if (!shell->locked)
3685 return;
3686
Pekka Paalanen98262232011-12-01 10:42:22 +02003687 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003688
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003689 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003690 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003691 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003692
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003693 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003694 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003695 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003696}
3697
3698static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003699resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003700{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003701 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003702
Pekka Paalanen77346a62011-11-30 16:26:35 +02003703 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003704
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003705 wl_list_remove(&shell->lock_layer.link);
3706 wl_list_insert(&shell->compositor->cursor_layer.link,
3707 &shell->fullscreen_layer.link);
3708 wl_list_insert(&shell->fullscreen_layer.link,
3709 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003710 if (shell->showing_input_panels) {
3711 wl_list_insert(&shell->panel_layer.link,
3712 &shell->input_panel_layer.link);
3713 wl_list_insert(&shell->input_panel_layer.link,
3714 &ws->layer.link);
3715 } else {
3716 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3717 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003718
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003719 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003720
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003721 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003722 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003723 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003724}
3725
3726static void
3727desktop_shell_unlock(struct wl_client *client,
3728 struct wl_resource *resource)
3729{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003730 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003731
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003732 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003733
3734 if (shell->locked)
3735 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003736}
3737
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003738static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003739desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003740 struct wl_resource *resource,
3741 struct wl_resource *surface_resource)
3742{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003743 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003744
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003745 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003746 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003747}
3748
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003749static void
3750desktop_shell_desktop_ready(struct wl_client *client,
3751 struct wl_resource *resource)
3752{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003753 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003754
3755 shell_fade_startup(shell);
3756}
3757
Kristian Høgsberg75840622011-09-06 13:48:16 -04003758static const struct desktop_shell_interface desktop_shell_implementation = {
3759 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003760 desktop_shell_set_panel,
3761 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003762 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003763 desktop_shell_set_grab_surface,
3764 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003765};
3766
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003767static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003768get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003769{
3770 struct shell_surface *shsurf;
3771
3772 shsurf = get_shell_surface(surface);
3773 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003774 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003775 return shsurf->type;
3776}
3777
Kristian Høgsberg75840622011-09-06 13:48:16 -04003778static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003779move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003780{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003781 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003782 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003783 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003784
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003785 if (seat->pointer->focus == NULL)
3786 return;
3787
3788 focus = seat->pointer->focus->surface;
3789
Pekka Paalanen01388e22013-04-25 13:57:44 +03003790 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003791 if (surface == NULL)
3792 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003793
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003794 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003795 if (shsurf == NULL || shsurf->state.fullscreen ||
3796 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003797 return;
3798
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003799 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003800}
3801
3802static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003803maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3804{
3805 struct weston_surface *focus = seat->pointer->focus->surface;
3806 struct weston_surface *surface;
3807 struct shell_surface *shsurf;
3808
3809 surface = weston_surface_get_main_surface(focus);
3810 if (surface == NULL)
3811 return;
3812
3813 shsurf = get_shell_surface(surface);
3814 if (shsurf == NULL)
3815 return;
3816
3817 if (!shell_surface_is_xdg_surface(shsurf))
3818 return;
3819
3820 if (shsurf->state.maximized)
3821 xdg_surface_send_request_unset_maximized(shsurf->resource);
3822 else
3823 xdg_surface_send_request_set_maximized(shsurf->resource);
3824}
3825
3826static void
3827fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3828{
3829 struct weston_surface *focus = seat->pointer->focus->surface;
3830 struct weston_surface *surface;
3831 struct shell_surface *shsurf;
3832
3833 surface = weston_surface_get_main_surface(focus);
3834 if (surface == NULL)
3835 return;
3836
3837 shsurf = get_shell_surface(surface);
3838 if (shsurf == NULL)
3839 return;
3840
3841 if (!shell_surface_is_xdg_surface(shsurf))
3842 return;
3843
3844 if (shsurf->state.fullscreen)
3845 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3846 else
3847 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3848}
3849
3850static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003851touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3852{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003853 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003854 struct weston_surface *surface;
3855 struct shell_surface *shsurf;
3856
3857 surface = weston_surface_get_main_surface(focus);
3858 if (surface == NULL)
3859 return;
3860
3861 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003862 if (shsurf == NULL || shsurf->state.fullscreen ||
3863 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003864 return;
3865
3866 surface_touch_move(shsurf, (struct weston_seat *) seat);
3867}
3868
3869static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003870resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003871{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003872 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003873 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003874 uint32_t edges = 0;
3875 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003876 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003877
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003878 if (seat->pointer->focus == NULL)
3879 return;
3880
3881 focus = seat->pointer->focus->surface;
3882
Pekka Paalanen01388e22013-04-25 13:57:44 +03003883 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003884 if (surface == NULL)
3885 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003886
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003887 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003888 if (shsurf == NULL || shsurf->state.fullscreen ||
3889 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003890 return;
3891
Jason Ekstranda7af7042013-10-12 22:38:11 -05003892 weston_view_from_global(shsurf->view,
3893 wl_fixed_to_int(seat->pointer->grab_x),
3894 wl_fixed_to_int(seat->pointer->grab_y),
3895 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003896
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003897 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003898 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003899 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003900 edges |= 0;
3901 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003902 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003903
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003904 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003905 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003906 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003907 edges |= 0;
3908 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003909 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003910
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003911 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003912}
3913
3914static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003915surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003916 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003917{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003918 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003919 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003920 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003921 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003922
Pekka Paalanen01388e22013-04-25 13:57:44 +03003923 /* XXX: broken for windows containing sub-surfaces */
3924 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003925 if (surface == NULL)
3926 return;
3927
3928 shsurf = get_shell_surface(surface);
3929 if (!shsurf)
3930 return;
3931
Jason Ekstranda7af7042013-10-12 22:38:11 -05003932 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003933
Jason Ekstranda7af7042013-10-12 22:38:11 -05003934 if (shsurf->view->alpha > 1.0)
3935 shsurf->view->alpha = 1.0;
3936 if (shsurf->view->alpha < step)
3937 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003938
Jason Ekstranda7af7042013-10-12 22:38:11 -05003939 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003940 weston_surface_damage(surface);
3941}
3942
3943static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003944do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003945 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003946{
Daniel Stone37816df2012-05-16 18:45:18 +01003947 struct weston_seat *ws = (struct weston_seat *) seat;
3948 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003949 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003950 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003951
3952 wl_list_for_each(output, &compositor->output_list, link) {
3953 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003954 wl_fixed_to_double(seat->pointer->x),
3955 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003956 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003957 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003958 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003959 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003960 increment = -output->zoom.increment;
3961 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003962 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003963 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003964 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003965 else
3966 increment = 0;
3967
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003968 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003969
Scott Moreaue6603982012-06-11 13:07:51 -06003970 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003971 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003972 else if (output->zoom.level > output->zoom.max_level)
3973 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003974 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003975 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003976 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003977
Scott Moreaue6603982012-06-11 13:07:51 -06003978 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003979
Jason Ekstranda7af7042013-10-12 22:38:11 -05003980 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003981 }
3982 }
3983}
3984
Scott Moreauccbf29d2012-02-22 14:21:41 -07003985static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003986zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003987 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003988{
3989 do_zoom(seat, time, 0, axis, value);
3990}
3991
3992static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003993zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003994 void *data)
3995{
3996 do_zoom(seat, time, key, 0, 0);
3997}
3998
3999static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004000terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004001 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004002{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004003 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004004
Daniel Stone325fc2d2012-05-30 16:31:58 +01004005 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004006}
4007
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004008static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004009rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4010 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004011{
4012 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004013 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004014 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004015 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004016 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004017
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004018 weston_pointer_move(pointer, x, y);
4019
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004020 if (!shsurf)
4021 return;
4022
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004023 cx = 0.5f * shsurf->surface->width;
4024 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004025
Daniel Stone37816df2012-05-16 18:45:18 +01004026 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4027 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004028 r = sqrtf(dx * dx + dy * dy);
4029
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004030 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004031 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004032
4033 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004034 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004035 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004036
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004037 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004038 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004039
4040 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004041 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004042 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004043 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004044 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004045
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004046 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004047 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004048 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004049 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004050 wl_list_init(&shsurf->rotation.transform.link);
4051 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004052 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004053 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004054
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004055 /* We need to adjust the position of the surface
4056 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004057 cposx = shsurf->view->geometry.x + cx;
4058 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004059 dposx = rotate->center.x - cposx;
4060 dposy = rotate->center.y - cposy;
4061 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004062 weston_view_set_position(shsurf->view,
4063 shsurf->view->geometry.x + dposx,
4064 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004065 }
4066
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004067 /* Repaint implies weston_surface_update_transform(), which
4068 * lazily applies the damage due to rotation update.
4069 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004070 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004071}
4072
4073static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004074rotate_grab_button(struct weston_pointer_grab *grab,
4075 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004076{
4077 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004078 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004079 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004080 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004081 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004082
Daniel Stone4dbadb12012-05-30 16:31:51 +01004083 if (pointer->button_count == 0 &&
4084 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004085 if (shsurf)
4086 weston_matrix_multiply(&shsurf->rotation.rotation,
4087 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004088 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004089 free(rotate);
4090 }
4091}
4092
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004093static void
4094rotate_grab_cancel(struct weston_pointer_grab *grab)
4095{
4096 struct rotate_grab *rotate =
4097 container_of(grab, struct rotate_grab, base.grab);
4098
4099 shell_grab_end(&rotate->base);
4100 free(rotate);
4101}
4102
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004103static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004104 noop_grab_focus,
4105 rotate_grab_motion,
4106 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004107 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004108};
4109
4110static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004111surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004112{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004113 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004114 float dx, dy;
4115 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004116
Pekka Paalanen460099f2012-01-20 16:48:25 +02004117 rotate = malloc(sizeof *rotate);
4118 if (!rotate)
4119 return;
4120
Jason Ekstranda7af7042013-10-12 22:38:11 -05004121 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004122 surface->surface->width * 0.5f,
4123 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004124 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004125
Daniel Stone37816df2012-05-16 18:45:18 +01004126 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4127 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004128 r = sqrtf(dx * dx + dy * dy);
4129 if (r > 20.0f) {
4130 struct weston_matrix inverse;
4131
4132 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004133 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004134 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004135
4136 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004137 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004138 } else {
4139 weston_matrix_init(&surface->rotation.rotation);
4140 weston_matrix_init(&rotate->rotation);
4141 }
4142
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004143 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4144 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004145}
4146
4147static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004148rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004149 void *data)
4150{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004151 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004152 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004153 struct shell_surface *surface;
4154
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004155 if (seat->pointer->focus == NULL)
4156 return;
4157
4158 focus = seat->pointer->focus->surface;
4159
Pekka Paalanen01388e22013-04-25 13:57:44 +03004160 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004161 if (base_surface == NULL)
4162 return;
4163
4164 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004165 if (surface == NULL || surface->state.fullscreen ||
4166 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004167 return;
4168
4169 surface_rotate(surface, seat);
4170}
4171
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004172/* Move all fullscreen layers down to the current workspace in a non-reversible
4173 * manner. This should be used when implementing shell-wide overlays, such as
4174 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004175void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004176lower_fullscreen_layer(struct desktop_shell *shell)
4177{
4178 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004179 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004180
4181 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004182 wl_list_for_each_reverse_safe(view, prev,
4183 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004184 layer_link) {
4185 wl_list_remove(&view->layer_link);
4186 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4187 weston_view_damage_below(view);
4188 weston_surface_damage(view->surface);
4189 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004190}
4191
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004192void
Tiago Vignattibe143262012-04-16 17:31:41 +03004193activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004194 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004195{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004196 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004197 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004198 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004199 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004200 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004201
Pekka Paalanen01388e22013-04-25 13:57:44 +03004202 main_surface = weston_surface_get_main_surface(es);
4203
Daniel Stone37816df2012-05-16 18:45:18 +01004204 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004205
Jonas Ådahl8538b222012-08-29 22:13:03 +02004206 state = ensure_focus_state(shell, seat);
4207 if (state == NULL)
4208 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004209
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004210 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004211 state->keyboard_focus = es;
4212 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004213 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004214
Rafael Antognolli03b16592013-12-03 15:35:42 -02004215 shsurf = get_shell_surface(main_surface);
4216 if (shsurf->state.fullscreen)
4217 shell_configure_fullscreen(shsurf);
4218 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004219 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004220
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004221 if (shell->focus_animation_type != ANIMATION_NONE) {
4222 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004223 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004224 }
4225
4226 /* Update the surface’s layer. This brings it to the top of the stacking
4227 * order as appropriate. */
4228 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004229}
4230
Alex Wu21858432012-04-01 20:13:08 +08004231/* no-op func for checking black surface */
4232static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004233black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004234{
4235}
4236
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004237static bool
Alex Wu21858432012-04-01 20:13:08 +08004238is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4239{
4240 if (es->configure == black_surface_configure) {
4241 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004242 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004243 return true;
4244 }
4245 return false;
4246}
4247
Kristian Høgsberg75840622011-09-06 13:48:16 -04004248static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004249activate_binding(struct weston_seat *seat,
4250 struct desktop_shell *shell,
4251 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004252{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004253 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004254
Alex Wu9c35e6b2012-03-05 14:13:13 +08004255 if (!focus)
4256 return;
4257
Pekka Paalanen01388e22013-04-25 13:57:44 +03004258 if (is_black_surface(focus, &main_surface))
4259 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004260
Pekka Paalanen01388e22013-04-25 13:57:44 +03004261 main_surface = weston_surface_get_main_surface(focus);
4262 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004263 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004264
Neil Robertsa28c6932013-10-03 16:43:04 +01004265 activate(shell, focus, seat);
4266}
4267
4268static void
4269click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4270 void *data)
4271{
4272 if (seat->pointer->grab != &seat->pointer->default_grab)
4273 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004274 if (seat->pointer->focus == NULL)
4275 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004276
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004277 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004278}
4279
4280static void
4281touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4282{
4283 if (seat->touch->grab != &seat->touch->default_grab)
4284 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004285 if (seat->touch->focus == NULL)
4286 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004287
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004288 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004289}
4290
4291static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004292lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004293{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004294 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004295
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004296 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004297 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004298 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004299 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004300
4301 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004302
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004303 /* Hide all surfaces by removing the fullscreen, panel and
4304 * toplevel layers. This way nothing else can show or receive
4305 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004306
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004307 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004308 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004309 if (shell->showing_input_panels)
4310 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004311 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004312 wl_list_insert(&shell->compositor->cursor_layer.link,
4313 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004314
Pekka Paalanen77346a62011-11-30 16:26:35 +02004315 launch_screensaver(shell);
4316
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004317 /* TODO: disable bindings that should not work while locked. */
4318
4319 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004320}
4321
4322static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004323unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004324{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004325 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004326 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004327 return;
4328 }
4329
4330 /* If desktop-shell client has gone away, unlock immediately. */
4331 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004332 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004333 return;
4334 }
4335
4336 if (shell->prepare_event_sent)
4337 return;
4338
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004339 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004340 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004341}
4342
4343static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004344shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004345{
4346 struct desktop_shell *shell = data;
4347
4348 shell->fade.animation = NULL;
4349
4350 switch (shell->fade.type) {
4351 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004352 weston_surface_destroy(shell->fade.view->surface);
4353 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004354 break;
4355 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004356 lock(shell);
4357 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004358 default:
4359 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004360 }
4361}
4362
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004364shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004365{
4366 struct weston_compositor *compositor = shell->compositor;
4367 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004368 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004369
4370 surface = weston_surface_create(compositor);
4371 if (!surface)
4372 return NULL;
4373
Jason Ekstranda7af7042013-10-12 22:38:11 -05004374 view = weston_view_create(surface);
4375 if (!view) {
4376 weston_surface_destroy(surface);
4377 return NULL;
4378 }
4379
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004380 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004381 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004382 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004383 wl_list_insert(&compositor->fade_layer.view_list,
4384 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004385 pixman_region32_init(&surface->input);
4386
Jason Ekstranda7af7042013-10-12 22:38:11 -05004387 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004388}
4389
4390static void
4391shell_fade(struct desktop_shell *shell, enum fade_type type)
4392{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004393 float tint;
4394
4395 switch (type) {
4396 case FADE_IN:
4397 tint = 0.0;
4398 break;
4399 case FADE_OUT:
4400 tint = 1.0;
4401 break;
4402 default:
4403 weston_log("shell: invalid fade type\n");
4404 return;
4405 }
4406
4407 shell->fade.type = type;
4408
Jason Ekstranda7af7042013-10-12 22:38:11 -05004409 if (shell->fade.view == NULL) {
4410 shell->fade.view = shell_fade_create_surface(shell);
4411 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004412 return;
4413
Jason Ekstranda7af7042013-10-12 22:38:11 -05004414 shell->fade.view->alpha = 1.0 - tint;
4415 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004416 }
4417
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004418 if (shell->fade.view->output == NULL) {
4419 /* If the black view gets a NULL output, we lost the
4420 * last output and we'll just cancel the fade. This
4421 * happens when you close the last window under the
4422 * X11 or Wayland backends. */
4423 shell->locked = false;
4424 weston_surface_destroy(shell->fade.view->surface);
4425 shell->fade.view = NULL;
4426 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004427 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004428 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004429 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004430 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004431 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004432 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004433 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004434}
4435
4436static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004437do_shell_fade_startup(void *data)
4438{
4439 struct desktop_shell *shell = data;
4440
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004441 if (shell->startup_animation_type == ANIMATION_FADE)
4442 shell_fade(shell, FADE_IN);
4443 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004444 weston_surface_destroy(shell->fade.view->surface);
4445 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004446 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004447}
4448
4449static void
4450shell_fade_startup(struct desktop_shell *shell)
4451{
4452 struct wl_event_loop *loop;
4453
4454 if (!shell->fade.startup_timer)
4455 return;
4456
4457 wl_event_source_remove(shell->fade.startup_timer);
4458 shell->fade.startup_timer = NULL;
4459
4460 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4461 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4462}
4463
4464static int
4465fade_startup_timeout(void *data)
4466{
4467 struct desktop_shell *shell = data;
4468
4469 shell_fade_startup(shell);
4470 return 0;
4471}
4472
4473static void
4474shell_fade_init(struct desktop_shell *shell)
4475{
4476 /* Make compositor output all black, and wait for the desktop-shell
4477 * client to signal it is ready, then fade in. The timer triggers a
4478 * fade-in, in case the desktop-shell client takes too long.
4479 */
4480
4481 struct wl_event_loop *loop;
4482
Jason Ekstranda7af7042013-10-12 22:38:11 -05004483 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004484 weston_log("%s: warning: fade surface already exists\n",
4485 __func__);
4486 return;
4487 }
4488
Jason Ekstranda7af7042013-10-12 22:38:11 -05004489 shell->fade.view = shell_fade_create_surface(shell);
4490 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004491 return;
4492
Jason Ekstranda7af7042013-10-12 22:38:11 -05004493 weston_view_update_transform(shell->fade.view);
4494 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004495
4496 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4497 shell->fade.startup_timer =
4498 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4499 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4500}
4501
4502static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004503idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004504{
4505 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004506 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004507 struct weston_seat *seat;
4508
4509 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4510 if (seat->pointer)
4511 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004512
4513 shell_fade(shell, FADE_OUT);
4514 /* lock() is called from shell_fade_done() */
4515}
4516
4517static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004518wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004519{
4520 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004521 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004522
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004523 unlock(shell);
4524}
4525
4526static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004527center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004528{
Giulio Camuffob8366642013-04-25 13:57:46 +03004529 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004530 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004531
Jason Ekstranda7af7042013-10-12 22:38:11 -05004532 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004533
4534 x = output->x + (output->width - width) / 2 - surf_x / 2;
4535 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004536
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004537 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004538}
4539
4540static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004541weston_view_set_initial_position(struct weston_view *view,
4542 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004543{
4544 struct weston_compositor *compositor = shell->compositor;
4545 int ix = 0, iy = 0;
4546 int range_x, range_y;
4547 int dx, dy, x, y, panel_height;
4548 struct weston_output *output, *target_output = NULL;
4549 struct weston_seat *seat;
4550
4551 /* As a heuristic place the new window on the same output as the
4552 * pointer. Falling back to the output containing 0, 0.
4553 *
4554 * TODO: Do something clever for touch too?
4555 */
4556 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004557 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004558 ix = wl_fixed_to_int(seat->pointer->x);
4559 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004560 break;
4561 }
4562 }
4563
4564 wl_list_for_each(output, &compositor->output_list, link) {
4565 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4566 target_output = output;
4567 break;
4568 }
4569 }
4570
4571 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004572 weston_view_set_position(view, 10 + random() % 400,
4573 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004574 return;
4575 }
4576
4577 /* Valid range within output where the surface will still be onscreen.
4578 * If this is negative it means that the surface is bigger than
4579 * output.
4580 */
4581 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004582 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004583 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004584 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004585
Rob Bradford4cb88c72012-08-13 15:18:44 +01004586 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004587 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004588 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004589 dx = 0;
4590
4591 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004592 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004593 else
4594 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004595
4596 x = target_output->x + dx;
4597 y = target_output->y + dy;
4598
Jason Ekstranda7af7042013-10-12 22:38:11 -05004599 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004600}
4601
4602static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004603map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004604 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004605{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004606 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004607 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004608 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004609 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004610
Pekka Paalanen77346a62011-11-30 16:26:35 +02004611 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004612 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004613 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004614 if (shsurf->state.fullscreen) {
4615 center_on_output(shsurf->view, shsurf->fullscreen_output);
4616 shell_map_fullscreen(shsurf);
4617 } else if (shsurf->state.maximized) {
4618 /* use surface configure to set the geometry */
4619 panel_height = get_output_panel_height(shell, shsurf->output);
4620 surface_subsurfaces_boundingbox(shsurf->surface,
4621 &surf_x, &surf_y, NULL, NULL);
4622 weston_view_set_position(shsurf->view,
4623 shsurf->output->x - surf_x,
4624 shsurf->output->y +
4625 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004626 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004627 weston_view_set_initial_position(shsurf->view, shell);
4628 }
Juan Zhao96879df2012-02-07 08:45:41 +08004629 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004630 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004631 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004632 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004633 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004634 weston_view_set_position(shsurf->view,
4635 shsurf->view->geometry.x + sx,
4636 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004637 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004638 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004639 default:
4640 ;
4641 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004642
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004643 /* Surface stacking order, see also activate(). */
4644 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004645
Jason Ekstranda7af7042013-10-12 22:38:11 -05004646 if (shsurf->type != SHELL_SURFACE_NONE) {
4647 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004648 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004649 shsurf->surface->output = shsurf->output;
4650 shsurf->view->output = shsurf->output;
4651 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004652 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004653
Rafael Antognollied207b42013-12-03 15:35:43 -02004654 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4655 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4656 }
4657
Jason Ekstranda7af7042013-10-12 22:38:11 -05004658 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004659 /* XXX: xwayland's using the same fields for transient type */
4660 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004661 if (shsurf->transient.flags ==
4662 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4663 break;
4664 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004665 if (shsurf->state.relative &&
4666 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4667 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004668 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004669 break;
4670 wl_list_for_each(seat, &compositor->seat_list, link)
4671 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004672 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004673 case SHELL_SURFACE_POPUP:
4674 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004675 default:
4676 break;
4677 }
4678
Rafael Antognolli03b16592013-12-03 15:35:42 -02004679 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4680 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004681 {
4682 switch (shell->win_animation_type) {
4683 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004684 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004685 break;
4686 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004687 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004688 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004689 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004690 default:
4691 break;
4692 }
4693 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004694}
4695
4696static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004697configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004698 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004699{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004700 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004701 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004702 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004703
Pekka Paalanen77346a62011-11-30 16:26:35 +02004704 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004705
Rafael Antognolli03b16592013-12-03 15:35:42 -02004706 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004707 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004708 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004709 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004710 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4711 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004712 mx = shsurf->output->x - surf_x;
4713 my = shsurf->output->y +
4714 get_output_panel_height(shell,shsurf->output) - surf_y;
4715 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004716 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004717 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004718 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004719
Alex Wu4539b082012-03-01 12:57:46 +08004720 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004721 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004722 wl_list_for_each(view, &surface->views, surface_link)
4723 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004724
Rafael Antognolli03b16592013-12-03 15:35:42 -02004725 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004726 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004727 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004728}
4729
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004730static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004731shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004732{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004733 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004734 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004735
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004736 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004737
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004738 if (!weston_surface_is_mapped(es) &&
4739 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004740 remove_popup_grab(shsurf);
4741 }
4742
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004743 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004744 return;
4745
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004746 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004747 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004748 type_changed = 1;
4749 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004750
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004751 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004752 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004753 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004754 shsurf->last_width != es->width ||
4755 shsurf->last_height != es->height) {
4756 shsurf->last_width = es->width;
4757 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004758 float from_x, from_y;
4759 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004760
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4762 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004763 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004764 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004765 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004766 }
4767}
4768
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004769static void
4770shell_surface_output_destroyed(struct weston_surface *es)
4771{
4772 struct shell_surface *shsurf = get_shell_surface(es);
4773
4774 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004775 shsurf->next_state.maximized = false;
4776 shsurf->next_state.fullscreen = false;
4777 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004778}
4779
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004780static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004781
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004782static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004783desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004784{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004785 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004786 struct desktop_shell *shell =
4787 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004788
4789 shell->child.process.pid = 0;
4790 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004791
4792 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4793 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004794 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004795 shell->child.deathstamp = time;
4796 shell->child.deathcount = 0;
4797 }
4798
4799 shell->child.deathcount++;
4800 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004801 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004802 return;
4803 }
4804
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004805 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004806 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004807 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004808}
4809
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004810static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004811desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4812{
4813 struct desktop_shell *shell;
4814
4815 shell = container_of(listener, struct desktop_shell,
4816 child.client_destroy_listener);
4817
4818 shell->child.client = NULL;
4819}
4820
4821static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004822launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004823{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004824 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004825
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004826 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004827 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004828 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004829 desktop_shell_sigchld);
4830
4831 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004832 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004833
4834 shell->child.client_destroy_listener.notify =
4835 desktop_shell_client_destroy;
4836 wl_client_add_destroy_listener(shell->child.client,
4837 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004838}
4839
4840static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004841bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4842{
Tiago Vignattibe143262012-04-16 17:31:41 +03004843 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004844 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004845
Jason Ekstranda85118c2013-06-27 20:17:02 -05004846 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4847 if (resource)
4848 wl_resource_set_implementation(resource, &shell_implementation,
4849 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004850}
4851
Kristian Høgsberg75840622011-09-06 13:48:16 -04004852static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004853bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4854{
4855 struct desktop_shell *shell = data;
4856 struct wl_resource *resource;
4857
4858 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4859 if (resource)
4860 wl_resource_set_dispatcher(resource,
4861 xdg_shell_unversioned_dispatch,
4862 NULL, shell, NULL);
4863}
4864
4865static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004866unbind_desktop_shell(struct wl_resource *resource)
4867{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004868 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004869
4870 if (shell->locked)
4871 resume_desktop(shell);
4872
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004873 shell->child.desktop_shell = NULL;
4874 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004875}
4876
4877static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004878bind_desktop_shell(struct wl_client *client,
4879 void *data, uint32_t version, uint32_t id)
4880{
Tiago Vignattibe143262012-04-16 17:31:41 +03004881 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004882 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004883
Jason Ekstranda85118c2013-06-27 20:17:02 -05004884 resource = wl_resource_create(client, &desktop_shell_interface,
4885 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004886
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004887 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004888 wl_resource_set_implementation(resource,
4889 &desktop_shell_implementation,
4890 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004891 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004892
4893 if (version < 2)
4894 shell_fade_startup(shell);
4895
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004896 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004897 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004898
4899 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4900 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004901 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004902}
4903
Pekka Paalanen6e168112011-11-24 11:34:05 +02004904static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004905screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004906{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004907 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004908 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004909
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004910 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004911 return;
4912
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004913 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004914 if (!shell->locked)
4915 return;
4916
Jason Ekstranda7af7042013-10-12 22:38:11 -05004917 view = container_of(surface->views.next, struct weston_view, surface_link);
4918 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004919
Jason Ekstranda7af7042013-10-12 22:38:11 -05004920 if (wl_list_empty(&view->layer_link)) {
4921 wl_list_insert(shell->lock_layer.view_list.prev,
4922 &view->layer_link);
4923 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004924 wl_event_source_timer_update(shell->screensaver.timer,
4925 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004926 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004927 }
4928}
4929
4930static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004931screensaver_set_surface(struct wl_client *client,
4932 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004933 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004934 struct wl_resource *output_resource)
4935{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004936 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004937 struct weston_surface *surface =
4938 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004939 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004940 struct weston_view *view, *next;
4941
4942 /* Make sure we only have one view */
4943 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4944 weston_view_destroy(view);
4945 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004946
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004947 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004948 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004949 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004950}
4951
4952static const struct screensaver_interface screensaver_implementation = {
4953 screensaver_set_surface
4954};
4955
4956static void
4957unbind_screensaver(struct wl_resource *resource)
4958{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004959 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004960
Pekka Paalanen77346a62011-11-30 16:26:35 +02004961 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004962}
4963
4964static void
4965bind_screensaver(struct wl_client *client,
4966 void *data, uint32_t version, uint32_t id)
4967{
Tiago Vignattibe143262012-04-16 17:31:41 +03004968 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004969 struct wl_resource *resource;
4970
Jason Ekstranda85118c2013-06-27 20:17:02 -05004971 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004972
Pekka Paalanen77346a62011-11-30 16:26:35 +02004973 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004974 wl_resource_set_implementation(resource,
4975 &screensaver_implementation,
4976 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004977 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004978 return;
4979 }
4980
4981 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4982 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004983 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004984}
4985
Kristian Høgsberg07045392012-02-19 18:52:44 -05004986struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004987 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004988 struct weston_surface *current;
4989 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004990 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004991};
4992
4993static void
4994switcher_next(struct switcher *switcher)
4995{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004996 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004997 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004998 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004999 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005000
Jason Ekstranda7af7042013-10-12 22:38:11 -05005001 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005002 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005003 if (shsurf &&
5004 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5005 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005006 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005007 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005008 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005009 next = view->surface;
5010 prev = view->surface;
5011 view->alpha = 0.25;
5012 weston_view_geometry_dirty(view);
5013 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005014 }
Alex Wu1659daa2012-04-01 20:13:09 +08005015
Jason Ekstranda7af7042013-10-12 22:38:11 -05005016 if (is_black_surface(view->surface, NULL)) {
5017 view->alpha = 0.25;
5018 weston_view_geometry_dirty(view);
5019 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005020 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005021 }
5022
5023 if (next == NULL)
5024 next = first;
5025
Alex Wu07b26062012-03-12 16:06:01 +08005026 if (next == NULL)
5027 return;
5028
Kristian Høgsberg07045392012-02-19 18:52:44 -05005029 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005030 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005031
5032 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005033 wl_list_for_each(view, &next->views, surface_link)
5034 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005035
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005036 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005037 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005038 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005039}
5040
5041static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005042switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005043{
5044 struct switcher *switcher =
5045 container_of(listener, struct switcher, listener);
5046
5047 switcher_next(switcher);
5048}
5049
5050static void
Daniel Stone351eb612012-05-31 15:27:47 -04005051switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005052{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005053 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005054 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005055 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005056
Jason Ekstranda7af7042013-10-12 22:38:11 -05005057 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005058 if (is_focus_view(view))
5059 continue;
5060
Jason Ekstranda7af7042013-10-12 22:38:11 -05005061 view->alpha = 1.0;
5062 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005063 }
5064
Alex Wu07b26062012-03-12 16:06:01 +08005065 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005066 activate(switcher->shell, switcher->current,
5067 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005068 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005069 weston_keyboard_end_grab(keyboard);
5070 if (keyboard->input_method_resource)
5071 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005072 free(switcher);
5073}
5074
5075static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005076switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005077 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005078{
5079 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005080 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005081
Daniel Stonec9785ea2012-05-30 16:31:52 +01005082 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005083 switcher_next(switcher);
5084}
5085
5086static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005087switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005088 uint32_t mods_depressed, uint32_t mods_latched,
5089 uint32_t mods_locked, uint32_t group)
5090{
5091 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005092 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005093
Daniel Stone351eb612012-05-31 15:27:47 -04005094 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5095 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005096}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005097
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005098static void
5099switcher_cancel(struct weston_keyboard_grab *grab)
5100{
5101 struct switcher *switcher = container_of(grab, struct switcher, grab);
5102
5103 switcher_destroy(switcher);
5104}
5105
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005106static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005107 switcher_key,
5108 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005109 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005110};
5111
5112static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005113switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005114 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005115{
Tiago Vignattibe143262012-04-16 17:31:41 +03005116 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005117 struct switcher *switcher;
5118
5119 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005120 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005121 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005122 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005123 wl_list_init(&switcher->listener.link);
5124
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005125 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005126 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005127 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005128 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5129 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005130 switcher_next(switcher);
5131}
5132
Pekka Paalanen3c647232011-12-22 13:43:43 +02005133static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005134backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005135 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005136{
5137 struct weston_compositor *compositor = data;
5138 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005139 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005140
5141 /* TODO: we're limiting to simple use cases, where we assume just
5142 * control on the primary display. We'd have to extend later if we
5143 * ever get support for setting backlights on random desktop LCD
5144 * panels though */
5145 output = get_default_output(compositor);
5146 if (!output)
5147 return;
5148
5149 if (!output->set_backlight)
5150 return;
5151
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005152 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5153 backlight_new = output->backlight_current - 25;
5154 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5155 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005156
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005157 if (backlight_new < 5)
5158 backlight_new = 5;
5159 if (backlight_new > 255)
5160 backlight_new = 255;
5161
5162 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005163 output->set_backlight(output, output->backlight_current);
5164}
5165
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005166struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005167 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005168 struct weston_seat *seat;
5169 uint32_t key[2];
5170 int key_released[2];
5171};
5172
5173static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005174debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005175 uint32_t key, uint32_t state)
5176{
5177 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005178 struct weston_compositor *ec = db->seat->compositor;
5179 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005180 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005181 uint32_t serial;
5182 int send = 0, terminate = 0;
5183 int check_binding = 1;
5184 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005185 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005186
5187 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5188 /* Do not run bindings on key releases */
5189 check_binding = 0;
5190
5191 for (i = 0; i < 2; i++)
5192 if (key == db->key[i])
5193 db->key_released[i] = 1;
5194
5195 if (db->key_released[0] && db->key_released[1]) {
5196 /* All key releases been swalled so end the grab */
5197 terminate = 1;
5198 } else if (key != db->key[0] && key != db->key[1]) {
5199 /* Should not swallow release of other keys */
5200 send = 1;
5201 }
5202 } else if (key == db->key[0] && !db->key_released[0]) {
5203 /* Do not check bindings for the first press of the binding
5204 * key. This allows it to be used as a debug shortcut.
5205 * We still need to swallow this event. */
5206 check_binding = 0;
5207 } else if (db->key[1]) {
5208 /* If we already ran a binding don't process another one since
5209 * we can't keep track of all the binding keys that were
5210 * pressed in order to swallow the release events. */
5211 send = 1;
5212 check_binding = 0;
5213 }
5214
5215 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005216 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5217 key, state)) {
5218 /* We ran a binding so swallow the press and keep the
5219 * grab to swallow the released too. */
5220 send = 0;
5221 terminate = 0;
5222 db->key[1] = key;
5223 } else {
5224 /* Terminate the grab since the key pressed is not a
5225 * debug binding key. */
5226 send = 1;
5227 terminate = 1;
5228 }
5229 }
5230
5231 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005232 serial = wl_display_next_serial(display);
5233 resource_list = &grab->keyboard->focus_resource_list;
5234 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005235 wl_keyboard_send_key(resource, serial, time, key, state);
5236 }
5237 }
5238
5239 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005240 weston_keyboard_end_grab(grab->keyboard);
5241 if (grab->keyboard->input_method_resource)
5242 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005243 free(db);
5244 }
5245}
5246
5247static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005248debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005249 uint32_t mods_depressed, uint32_t mods_latched,
5250 uint32_t mods_locked, uint32_t group)
5251{
5252 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005253 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005254
Neil Roberts96d790e2013-09-19 17:32:00 +01005255 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005256
Neil Roberts96d790e2013-09-19 17:32:00 +01005257 wl_resource_for_each(resource, resource_list) {
5258 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5259 mods_latched, mods_locked, group);
5260 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005261}
5262
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005263static void
5264debug_binding_cancel(struct weston_keyboard_grab *grab)
5265{
5266 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5267
5268 weston_keyboard_end_grab(grab->keyboard);
5269 free(db);
5270}
5271
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005272struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005273 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005274 debug_binding_modifiers,
5275 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005276};
5277
5278static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005279debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005280{
5281 struct debug_binding_grab *grab;
5282
5283 grab = calloc(1, sizeof *grab);
5284 if (!grab)
5285 return;
5286
5287 grab->seat = (struct weston_seat *) seat;
5288 grab->key[0] = key;
5289 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005290 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005291}
5292
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005293static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005294force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005295 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005296{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005297 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005298 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005299 struct desktop_shell *shell = data;
5300 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005301 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005302
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005303 focus_surface = seat->keyboard->focus;
5304 if (!focus_surface)
5305 return;
5306
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005307 wl_signal_emit(&compositor->kill_signal, focus_surface);
5308
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005309 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005310 wl_client_get_credentials(client, &pid, NULL, NULL);
5311
5312 /* Skip clients that we launched ourselves (the credentials of
5313 * the socketpair is ours) */
5314 if (pid == getpid())
5315 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005316
Daniel Stone325fc2d2012-05-30 16:31:58 +01005317 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005318}
5319
5320static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005321workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005322 uint32_t key, void *data)
5323{
5324 struct desktop_shell *shell = data;
5325 unsigned int new_index = shell->workspaces.current;
5326
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005327 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005328 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005329 if (new_index != 0)
5330 new_index--;
5331
5332 change_workspace(shell, new_index);
5333}
5334
5335static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005336workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005337 uint32_t key, void *data)
5338{
5339 struct desktop_shell *shell = data;
5340 unsigned int new_index = shell->workspaces.current;
5341
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005342 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005343 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005344 if (new_index < shell->workspaces.num - 1)
5345 new_index++;
5346
5347 change_workspace(shell, new_index);
5348}
5349
5350static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005351workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005352 uint32_t key, void *data)
5353{
5354 struct desktop_shell *shell = data;
5355 unsigned int new_index;
5356
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005357 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005358 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005359 new_index = key - KEY_F1;
5360 if (new_index >= shell->workspaces.num)
5361 new_index = shell->workspaces.num - 1;
5362
5363 change_workspace(shell, new_index);
5364}
5365
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005366static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005367workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005368 uint32_t key, void *data)
5369{
5370 struct desktop_shell *shell = data;
5371 unsigned int new_index = shell->workspaces.current;
5372
5373 if (shell->locked)
5374 return;
5375
5376 if (new_index != 0)
5377 new_index--;
5378
5379 take_surface_to_workspace_by_seat(shell, seat, new_index);
5380}
5381
5382static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005383workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005384 uint32_t key, void *data)
5385{
5386 struct desktop_shell *shell = data;
5387 unsigned int new_index = shell->workspaces.current;
5388
5389 if (shell->locked)
5390 return;
5391
5392 if (new_index < shell->workspaces.num - 1)
5393 new_index++;
5394
5395 take_surface_to_workspace_by_seat(shell, seat, new_index);
5396}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005397
5398static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005399handle_output_destroy(struct wl_listener *listener, void *data)
5400{
5401 struct shell_output *output_listener =
5402 container_of(listener, struct shell_output, destroy_listener);
5403
5404 wl_list_remove(&output_listener->destroy_listener.link);
5405 wl_list_remove(&output_listener->link);
5406 free(output_listener);
5407}
5408
5409static void
5410create_shell_output(struct desktop_shell *shell,
5411 struct weston_output *output)
5412{
5413 struct shell_output *shell_output;
5414
5415 shell_output = zalloc(sizeof *shell_output);
5416 if (shell_output == NULL)
5417 return;
5418
5419 shell_output->output = output;
5420 shell_output->shell = shell;
5421 shell_output->destroy_listener.notify = handle_output_destroy;
5422 wl_signal_add(&output->destroy_signal,
5423 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005424 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005425}
5426
5427static void
5428handle_output_create(struct wl_listener *listener, void *data)
5429{
5430 struct desktop_shell *shell =
5431 container_of(listener, struct desktop_shell, output_create_listener);
5432 struct weston_output *output = (struct weston_output *)data;
5433
5434 create_shell_output(shell, output);
5435}
5436
5437static void
5438setup_output_destroy_handler(struct weston_compositor *ec,
5439 struct desktop_shell *shell)
5440{
5441 struct weston_output *output;
5442
5443 wl_list_init(&shell->output_list);
5444 wl_list_for_each(output, &ec->output_list, link)
5445 create_shell_output(shell, output);
5446
5447 shell->output_create_listener.notify = handle_output_create;
5448 wl_signal_add(&ec->output_created_signal,
5449 &shell->output_create_listener);
5450}
5451
5452static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005453shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005454{
Tiago Vignattibe143262012-04-16 17:31:41 +03005455 struct desktop_shell *shell =
5456 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005457 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005458 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005459
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005460 /* Force state to unlocked so we don't try to fade */
5461 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005462 if (shell->child.client)
5463 wl_client_destroy(shell->child.client);
5464
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005465 wl_list_remove(&shell->idle_listener.link);
5466 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005467
5468 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005469
Xiong Zhang6b481422013-10-23 13:58:32 +08005470 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5471 wl_list_remove(&shell_output->destroy_listener.link);
5472 wl_list_remove(&shell_output->link);
5473 free(shell_output);
5474 }
5475
5476 wl_list_remove(&shell->output_create_listener.link);
5477
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005478 wl_array_for_each(ws, &shell->workspaces.array)
5479 workspace_destroy(*ws);
5480 wl_array_release(&shell->workspaces.array);
5481
Pekka Paalanen3c647232011-12-22 13:43:43 +02005482 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005483 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005484 free(shell);
5485}
5486
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005487static void
5488shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5489{
5490 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005491 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005492
5493 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005494 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5495 MODIFIER_CTRL | MODIFIER_ALT,
5496 terminate_binding, ec);
5497 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5498 click_to_activate_binding,
5499 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005500 weston_compositor_add_touch_binding(ec, 0,
5501 touch_to_activate_binding,
5502 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005503 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5504 MODIFIER_SUPER | MODIFIER_ALT,
5505 surface_opacity_binding, NULL);
5506 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5507 MODIFIER_SUPER, zoom_axis_binding,
5508 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005509
5510 /* configurable bindings */
5511 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005512 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5513 zoom_key_binding, NULL);
5514 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5515 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005516 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5517 maximize_binding, NULL);
5518 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5519 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005520 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5521 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005522 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005523 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5524 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005525
5526 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5527 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5528 rotate_binding, NULL);
5529
Daniel Stone325fc2d2012-05-30 16:31:58 +01005530 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5531 shell);
5532 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5533 ec);
5534 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5535 backlight_binding, ec);
5536 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5537 ec);
5538 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5539 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005540 weston_compositor_add_key_binding(ec, KEY_K, mod,
5541 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005542 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5543 workspace_up_binding, shell);
5544 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5545 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005546 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5547 workspace_move_surface_up_binding,
5548 shell);
5549 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5550 workspace_move_surface_down_binding,
5551 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005552
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005553 if (shell->exposay_modifier)
5554 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5555 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005556
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005557 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5558 if (shell->workspaces.num > 1) {
5559 num_workspace_bindings = shell->workspaces.num;
5560 if (num_workspace_bindings > 6)
5561 num_workspace_bindings = 6;
5562 for (i = 0; i < num_workspace_bindings; i++)
5563 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5564 workspace_f_binding,
5565 shell);
5566 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005567
5568 /* Debug bindings */
5569 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5570 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005571}
5572
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005573WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005574module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005575 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005576{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005577 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005578 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005579 struct workspace **pws;
5580 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005581 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005582
Peter Huttererf3d62272013-08-08 11:57:05 +10005583 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005584 if (shell == NULL)
5585 return -1;
5586
Kristian Høgsberg75840622011-09-06 13:48:16 -04005587 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005588
5589 shell->destroy_listener.notify = shell_destroy;
5590 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005591 shell->idle_listener.notify = idle_handler;
5592 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5593 shell->wake_listener.notify = wake_handler;
5594 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005595
Scott Moreauff1db4a2012-04-17 19:06:18 -06005596 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005597 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005598 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005599 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005600 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005601 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005602 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005603 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005604 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005605 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005606 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005607
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005608 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5609 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005610 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5611 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005612 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005613
5614 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005615 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005616
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005617 if (input_panel_setup(shell) < 0)
5618 return -1;
5619
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005620 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005621
Daniel Stonedf8133b2013-11-19 11:37:14 +01005622 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5623 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5624
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005625 for (i = 0; i < shell->workspaces.num; i++) {
5626 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5627 if (pws == NULL)
5628 return -1;
5629
5630 *pws = workspace_create();
5631 if (*pws == NULL)
5632 return -1;
5633 }
5634 activate_workspace(shell, 0);
5635
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005636 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005637 wl_list_init(&shell->workspaces.animation.link);
5638 shell->workspaces.animation.frame = animate_workspace_change_frame;
5639
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005640 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005641 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005642 return -1;
5643
Rafael Antognollie2a34552013-12-03 15:35:45 -02005644 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5645 shell, bind_xdg_shell) == NULL)
5646 return -1;
5647
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005648 if (wl_global_create(ec->wl_display,
5649 &desktop_shell_interface, 2,
5650 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005651 return -1;
5652
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005653 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5654 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005655 return -1;
5656
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005657 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5658 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005659 return -1;
5660
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005661 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005662
Xiong Zhang6b481422013-10-23 13:58:32 +08005663 setup_output_destroy_handler(ec, shell);
5664
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005665 loop = wl_display_get_event_loop(ec->wl_display);
5666 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005667
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005668 shell->screensaver.timer =
5669 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5670
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005671 wl_list_for_each(seat, &ec->seat_list, link)
5672 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005673
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005674 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005675
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005676 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005677
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005678 return 0;
5679}