blob: e3ef66ff3273ff7856866c06e946c3421b679c2f [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{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800409 if (!strcmp("zoom", animation))
410 return ANIMATION_ZOOM;
411 else if (!strcmp("fade", animation))
412 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100413 else if (!strcmp("dim-layer", animation))
414 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800415 else
416 return ANIMATION_NONE;
417}
418
Alex Wu4539b082012-03-01 12:57:46 +0800419static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400420shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200421{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400422 struct weston_config_section *section;
423 int duration;
424 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200425
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400426 section = weston_config_get_section(shell->compositor->config,
427 "screensaver", NULL, NULL);
428 weston_config_section_get_string(section,
429 "path", &shell->screensaver.path, NULL);
430 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200431 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400432
433 section = weston_config_get_section(shell->compositor->config,
434 "shell", NULL, NULL);
435 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100436 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100437 shell->client = s;
438 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400439 "binding-modifier", &s, "super");
440 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200441 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800442
443 weston_config_section_get_string(section,
444 "exposay-modifier", &s, "none");
445 if (strcmp(s, "none") == 0)
446 shell->exposay_modifier = 0;
447 else
448 shell->exposay_modifier = get_modifier(s);
449 free(s);
450
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400451 weston_config_section_get_string(section, "animation", &s, "none");
452 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200453 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700454 weston_config_section_get_string(section,
455 "startup-animation", &s, "fade");
456 shell->startup_animation_type = get_animation_type(s);
457 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700458 if (shell->startup_animation_type == ANIMATION_ZOOM)
459 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100460 weston_config_section_get_string(section, "focus-animation", &s, "none");
461 shell->focus_animation_type = get_animation_type(s);
462 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400463 weston_config_section_get_uint(section, "num-workspaces",
464 &shell->workspaces.num,
465 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200466}
467
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800468struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100469get_default_output(struct weston_compositor *compositor)
470{
471 return container_of(compositor->output_list.next,
472 struct weston_output, link);
473}
474
475
476/* no-op func for checking focus surface */
477static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600478focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100479{
480}
481
482static struct focus_surface *
483get_focus_surface(struct weston_surface *surface)
484{
485 if (surface->configure == focus_surface_configure)
486 return surface->configure_private;
487 else
488 return NULL;
489}
490
491static bool
492is_focus_surface (struct weston_surface *es)
493{
494 return (es->configure == focus_surface_configure);
495}
496
497static bool
498is_focus_view (struct weston_view *view)
499{
500 return is_focus_surface (view->surface);
501}
502
503static struct focus_surface *
504create_focus_surface(struct weston_compositor *ec,
505 struct weston_output *output)
506{
507 struct focus_surface *fsurf = NULL;
508 struct weston_surface *surface = NULL;
509
510 fsurf = malloc(sizeof *fsurf);
511 if (!fsurf)
512 return NULL;
513
514 fsurf->surface = weston_surface_create(ec);
515 surface = fsurf->surface;
516 if (surface == NULL) {
517 free(fsurf);
518 return NULL;
519 }
520
521 surface->configure = focus_surface_configure;
522 surface->output = output;
523 surface->configure_private = fsurf;
524
525 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100526 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100527
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600528 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600529 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100530 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
531 pixman_region32_fini(&surface->opaque);
532 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
533 output->width, output->height);
534 pixman_region32_fini(&surface->input);
535 pixman_region32_init(&surface->input);
536
537 wl_list_init(&fsurf->workspace_transform.link);
538
539 return fsurf;
540}
541
542static void
543focus_surface_destroy(struct focus_surface *fsurf)
544{
545 weston_surface_destroy(fsurf->surface);
546 free(fsurf);
547}
548
549static void
550focus_animation_done(struct weston_view_animation *animation, void *data)
551{
552 struct workspace *ws = data;
553
554 ws->focus_animation = NULL;
555}
556
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200557static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200558focus_state_destroy(struct focus_state *state)
559{
560 wl_list_remove(&state->seat_destroy_listener.link);
561 wl_list_remove(&state->surface_destroy_listener.link);
562 free(state);
563}
564
565static void
566focus_state_seat_destroy(struct wl_listener *listener, void *data)
567{
568 struct focus_state *state = container_of(listener,
569 struct focus_state,
570 seat_destroy_listener);
571
572 wl_list_remove(&state->link);
573 focus_state_destroy(state);
574}
575
576static void
577focus_state_surface_destroy(struct wl_listener *listener, void *data)
578{
579 struct focus_state *state = container_of(listener,
580 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400581 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400582 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583 struct weston_surface *main_surface, *next;
584 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200585
Pekka Paalanen01388e22013-04-25 13:57:44 +0300586 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
587
Kristian Høgsberge3778222012-07-31 17:29:30 -0400588 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
590 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400591 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100592 if (is_focus_view(view))
593 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400594
Jason Ekstranda7af7042013-10-12 22:38:11 -0500595 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400596 break;
597 }
598
Pekka Paalanen01388e22013-04-25 13:57:44 +0300599 /* if the focus was a sub-surface, activate its main surface */
600 if (main_surface != state->keyboard_focus)
601 next = main_surface;
602
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100603 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400604 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100605 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400606 activate(shell, next, state->seat);
607 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100608 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
609 if (state->ws->focus_animation)
610 weston_view_animation_destroy(state->ws->focus_animation);
611
612 state->ws->focus_animation = weston_fade_run(
613 state->ws->fsurf_front->view,
614 state->ws->fsurf_front->view->alpha, 0.0, 300,
615 focus_animation_done, state->ws);
616 }
617
Kristian Høgsberge3778222012-07-31 17:29:30 -0400618 wl_list_remove(&state->link);
619 focus_state_destroy(state);
620 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200621}
622
623static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400624focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200625{
Jonas Ådahl04769742012-06-13 00:01:24 +0200626 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200627
628 state = malloc(sizeof *state);
629 if (state == NULL)
630 return NULL;
631
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100632 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400633 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200634 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400635 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200636
637 state->seat_destroy_listener.notify = focus_state_seat_destroy;
638 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400639 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200640 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400641 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
643 return state;
644}
645
Jonas Ådahl8538b222012-08-29 22:13:03 +0200646static struct focus_state *
647ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
648{
649 struct workspace *ws = get_current_workspace(shell);
650 struct focus_state *state;
651
652 wl_list_for_each(state, &ws->focus_list, link)
653 if (state->seat == seat)
654 break;
655
656 if (&state->link == &ws->focus_list)
657 state = focus_state_create(seat, ws);
658
659 return state;
660}
661
Jonas Ådahl04769742012-06-13 00:01:24 +0200662static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400663restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200664{
665 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400666 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200667
668 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800669 if (state->seat->keyboard == NULL)
670 continue;
671
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400672 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200673
Kristian Høgsberge3148752013-05-06 23:19:49 -0400674 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200675 }
676}
677
678static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200679replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
680 struct weston_seat *seat)
681{
682 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400683 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200684
685 wl_list_for_each(state, &ws->focus_list, link) {
686 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400687 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500688 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200689 return;
690 }
691 }
692}
693
694static void
695drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
696 struct weston_surface *surface)
697{
698 struct focus_state *state;
699
700 wl_list_for_each(state, &ws->focus_list, link)
701 if (state->keyboard_focus == surface)
702 state->keyboard_focus = NULL;
703}
704
705static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100706animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
707 struct weston_view *from, struct weston_view *to)
708{
709 struct weston_output *output;
710 bool focus_surface_created = false;
711
712 /* FIXME: Only support dim animation using two layers */
713 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
714 return;
715
716 output = get_default_output(shell->compositor);
717 if (ws->fsurf_front == NULL && (from || to)) {
718 ws->fsurf_front = create_focus_surface(shell->compositor, output);
719 ws->fsurf_back = create_focus_surface(shell->compositor, output);
720 ws->fsurf_front->view->alpha = 0.0;
721 ws->fsurf_back->view->alpha = 0.0;
722 focus_surface_created = true;
723 } else {
724 wl_list_remove(&ws->fsurf_front->view->layer_link);
725 wl_list_remove(&ws->fsurf_back->view->layer_link);
726 }
727
728 if (ws->focus_animation) {
729 weston_view_animation_destroy(ws->focus_animation);
730 ws->focus_animation = NULL;
731 }
732
733 if (to)
734 wl_list_insert(&to->layer_link,
735 &ws->fsurf_front->view->layer_link);
736 else if (from)
737 wl_list_insert(&ws->layer.view_list,
738 &ws->fsurf_front->view->layer_link);
739
740 if (focus_surface_created) {
741 ws->focus_animation = weston_fade_run(
742 ws->fsurf_front->view,
743 ws->fsurf_front->view->alpha, 0.6, 300,
744 focus_animation_done, ws);
745 } else if (from) {
746 wl_list_insert(&from->layer_link,
747 &ws->fsurf_back->view->layer_link);
748 ws->focus_animation = weston_stable_fade_run(
749 ws->fsurf_front->view, 0.0,
750 ws->fsurf_back->view, 0.6,
751 focus_animation_done, ws);
752 } else if (to) {
753 wl_list_insert(&ws->layer.view_list,
754 &ws->fsurf_back->view->layer_link);
755 ws->focus_animation = weston_stable_fade_run(
756 ws->fsurf_front->view, 0.0,
757 ws->fsurf_back->view, 0.6,
758 focus_animation_done, ws);
759 }
760}
761
762static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200763workspace_destroy(struct workspace *ws)
764{
Jonas Ådahl04769742012-06-13 00:01:24 +0200765 struct focus_state *state, *next;
766
767 wl_list_for_each_safe(state, next, &ws->focus_list, link)
768 focus_state_destroy(state);
769
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100770 if (ws->fsurf_front)
771 focus_surface_destroy(ws->fsurf_front);
772 if (ws->fsurf_back)
773 focus_surface_destroy(ws->fsurf_back);
774
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200775 free(ws);
776}
777
Jonas Ådahl04769742012-06-13 00:01:24 +0200778static void
779seat_destroyed(struct wl_listener *listener, void *data)
780{
781 struct weston_seat *seat = data;
782 struct focus_state *state, *next;
783 struct workspace *ws = container_of(listener,
784 struct workspace,
785 seat_destroyed_listener);
786
787 wl_list_for_each_safe(state, next, &ws->focus_list, link)
788 if (state->seat == seat)
789 wl_list_remove(&state->link);
790}
791
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200792static struct workspace *
793workspace_create(void)
794{
795 struct workspace *ws = malloc(sizeof *ws);
796 if (ws == NULL)
797 return NULL;
798
799 weston_layer_init(&ws->layer, NULL);
800
Jonas Ådahl04769742012-06-13 00:01:24 +0200801 wl_list_init(&ws->focus_list);
802 wl_list_init(&ws->seat_destroyed_listener.link);
803 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100804 ws->fsurf_front = NULL;
805 ws->fsurf_back = NULL;
806 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200807
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200808 return ws;
809}
810
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200811static int
812workspace_is_empty(struct workspace *ws)
813{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500814 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200815}
816
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200817static struct workspace *
818get_workspace(struct desktop_shell *shell, unsigned int index)
819{
820 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200821 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200822 pws += index;
823 return *pws;
824}
825
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800826struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200827get_current_workspace(struct desktop_shell *shell)
828{
829 return get_workspace(shell, shell->workspaces.current);
830}
831
832static void
833activate_workspace(struct desktop_shell *shell, unsigned int index)
834{
835 struct workspace *ws;
836
837 ws = get_workspace(shell, index);
838 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
839
840 shell->workspaces.current = index;
841}
842
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200843static unsigned int
844get_output_height(struct weston_output *output)
845{
846 return abs(output->region.extents.y1 - output->region.extents.y2);
847}
848
849static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200852 struct weston_transform *transform;
853
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100854 if (is_focus_view(view)) {
855 struct focus_surface *fsurf = get_focus_surface(view->surface);
856 transform = &fsurf->workspace_transform;
857 } else {
858 struct shell_surface *shsurf = get_shell_surface(view->surface);
859 transform = &shsurf->workspace_transform;
860 }
861
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200862 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500863 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100864 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200865
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866 weston_matrix_init(&transform->matrix);
867 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200868 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500869 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200870}
871
872static void
873workspace_translate_out(struct workspace *ws, double fraction)
874{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200876 unsigned int height;
877 double d;
878
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
880 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200881 d = height * fraction;
882
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100883 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200884 }
885}
886
887static void
888workspace_translate_in(struct workspace *ws, double fraction)
889{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500890 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200891 unsigned int height;
892 double d;
893
Jason Ekstranda7af7042013-10-12 22:38:11 -0500894 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
895 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200896
897 if (fraction > 0)
898 d = -(height - height * fraction);
899 else
900 d = height + height * fraction;
901
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100902 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200903 }
904}
905
906static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200907broadcast_current_workspace_state(struct desktop_shell *shell)
908{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700909 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200910
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700911 wl_resource_for_each(resource, &shell->workspaces.client_list)
912 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200913 shell->workspaces.current,
914 shell->workspaces.num);
915}
916
917static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200918reverse_workspace_change_animation(struct desktop_shell *shell,
919 unsigned int index,
920 struct workspace *from,
921 struct workspace *to)
922{
923 shell->workspaces.current = index;
924
925 shell->workspaces.anim_to = to;
926 shell->workspaces.anim_from = from;
927 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
928 shell->workspaces.anim_timestamp = 0;
929
Scott Moreau4272e632012-08-13 09:58:41 -0600930 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200931}
932
933static void
934workspace_deactivate_transforms(struct workspace *ws)
935{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100937 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200938
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100940 if (is_focus_view(view)) {
941 struct focus_surface *fsurf = get_focus_surface(view->surface);
942 transform = &fsurf->workspace_transform;
943 } else {
944 struct shell_surface *shsurf = get_shell_surface(view->surface);
945 transform = &shsurf->workspace_transform;
946 }
947
948 if (!wl_list_empty(&transform->link)) {
949 wl_list_remove(&transform->link);
950 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200951 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953 }
954}
955
956static void
957finish_workspace_change_animation(struct desktop_shell *shell,
958 struct workspace *from,
959 struct workspace *to)
960{
Scott Moreau4272e632012-08-13 09:58:41 -0600961 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962
963 wl_list_remove(&shell->workspaces.animation.link);
964 workspace_deactivate_transforms(from);
965 workspace_deactivate_transforms(to);
966 shell->workspaces.anim_to = NULL;
967
968 wl_list_remove(&shell->workspaces.anim_from->layer.link);
969}
970
971static void
972animate_workspace_change_frame(struct weston_animation *animation,
973 struct weston_output *output, uint32_t msecs)
974{
975 struct desktop_shell *shell =
976 container_of(animation, struct desktop_shell,
977 workspaces.animation);
978 struct workspace *from = shell->workspaces.anim_from;
979 struct workspace *to = shell->workspaces.anim_to;
980 uint32_t t;
981 double x, y;
982
983 if (workspace_is_empty(from) && workspace_is_empty(to)) {
984 finish_workspace_change_animation(shell, from, to);
985 return;
986 }
987
988 if (shell->workspaces.anim_timestamp == 0) {
989 if (shell->workspaces.anim_current == 0.0)
990 shell->workspaces.anim_timestamp = msecs;
991 else
992 shell->workspaces.anim_timestamp =
993 msecs -
994 /* Invers of movement function 'y' below. */
995 (asin(1.0 - shell->workspaces.anim_current) *
996 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
997 M_2_PI);
998 }
999
1000 t = msecs - shell->workspaces.anim_timestamp;
1001
1002 /*
1003 * x = [0, π/2]
1004 * y(x) = sin(x)
1005 */
1006 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1007 y = sin(x);
1008
1009 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001010 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001011
1012 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1013 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1014 shell->workspaces.anim_current = y;
1015
Scott Moreau4272e632012-08-13 09:58:41 -06001016 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001018 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020}
1021
1022static void
1023animate_workspace_change(struct desktop_shell *shell,
1024 unsigned int index,
1025 struct workspace *from,
1026 struct workspace *to)
1027{
1028 struct weston_output *output;
1029
1030 int dir;
1031
1032 if (index > shell->workspaces.current)
1033 dir = -1;
1034 else
1035 dir = 1;
1036
1037 shell->workspaces.current = index;
1038
1039 shell->workspaces.anim_dir = dir;
1040 shell->workspaces.anim_from = from;
1041 shell->workspaces.anim_to = to;
1042 shell->workspaces.anim_current = 0.0;
1043 shell->workspaces.anim_timestamp = 0;
1044
1045 output = container_of(shell->compositor->output_list.next,
1046 struct weston_output, link);
1047 wl_list_insert(&output->animation_list,
1048 &shell->workspaces.animation.link);
1049
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001050 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051
1052 workspace_translate_in(to, 0);
1053
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001054 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001055
Scott Moreau4272e632012-08-13 09:58:41 -06001056 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001057}
1058
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001059static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001060update_workspace(struct desktop_shell *shell, unsigned int index,
1061 struct workspace *from, struct workspace *to)
1062{
1063 shell->workspaces.current = index;
1064 wl_list_insert(&from->layer.link, &to->layer.link);
1065 wl_list_remove(&from->layer.link);
1066}
1067
1068static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001069change_workspace(struct desktop_shell *shell, unsigned int index)
1070{
1071 struct workspace *from;
1072 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001073 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001074
1075 if (index == shell->workspaces.current)
1076 return;
1077
1078 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001080 return;
1081
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001082 from = get_current_workspace(shell);
1083 to = get_workspace(shell, index);
1084
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001085 if (shell->workspaces.anim_from == to &&
1086 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001087 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001088 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001089 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001090 return;
1091 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001092
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093 if (shell->workspaces.anim_to != NULL)
1094 finish_workspace_change_animation(shell,
1095 shell->workspaces.anim_from,
1096 shell->workspaces.anim_to);
1097
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001098 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001099
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001100 if (shell->focus_animation_type != ANIMATION_NONE) {
1101 wl_list_for_each(state, &from->focus_list, link)
1102 if (state->keyboard_focus)
1103 animate_focus_change(shell, from,
1104 get_default_view(state->keyboard_focus), NULL);
1105
1106 wl_list_for_each(state, &to->focus_list, link)
1107 if (state->keyboard_focus)
1108 animate_focus_change(shell, to,
1109 NULL, get_default_view(state->keyboard_focus));
1110 }
1111
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001112 if (workspace_is_empty(to) && workspace_is_empty(from))
1113 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 else
1115 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001116
1117 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001118}
1119
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001120static bool
1121workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1122{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001124 struct wl_list *e;
1125
1126 if (wl_list_empty(list))
1127 return false;
1128
1129 e = list->next;
1130
1131 if (e->next != list)
1132 return false;
1133
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001135}
1136
1137static void
Philip Withnall659163d2013-11-25 18:01:36 +00001138move_surface_to_workspace(struct desktop_shell *shell,
1139 struct shell_surface *shsurf,
1140 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001141{
1142 struct workspace *from;
1143 struct workspace *to;
1144 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001145 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001146 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001147
1148 if (workspace == shell->workspaces.current)
1149 return;
1150
Philip Withnall659163d2013-11-25 18:01:36 +00001151 view = get_default_view(shsurf->surface);
1152 if (!view)
1153 return;
1154
1155 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1156
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001157 if (workspace >= shell->workspaces.num)
1158 workspace = shell->workspaces.num - 1;
1159
Jonas Ådahle9d22502012-08-29 22:13:01 +02001160 from = get_current_workspace(shell);
1161 to = get_workspace(shell, workspace);
1162
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 wl_list_remove(&view->layer_link);
1164 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001165
Philip Withnall648a4dd2013-11-25 18:01:44 +00001166 shell_surface_update_child_surface_layers(shsurf);
1167
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001169 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1170 if (!seat->keyboard)
1171 continue;
1172
1173 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001175 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001176 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001179}
1180
1181static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001182take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001183 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001184 unsigned int index)
1185{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001186 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001188 struct shell_surface *shsurf;
1189 struct workspace *from;
1190 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001191 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001192
Pekka Paalanen01388e22013-04-25 13:57:44 +03001193 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 view = get_default_view(surface);
1195 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001196 index == shell->workspaces.current ||
1197 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198 return;
1199
1200 from = get_current_workspace(shell);
1201 to = get_workspace(shell, index);
1202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 wl_list_remove(&view->layer_link);
1204 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001205
Philip Withnall659163d2013-11-25 18:01:36 +00001206 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001207 if (shsurf != NULL)
1208 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001209
Jonas Ådahle9d22502012-08-29 22:13:01 +02001210 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001211 drop_focus_state(shell, from, surface);
1212
1213 if (shell->workspaces.anim_from == to &&
1214 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001215 wl_list_remove(&to->layer.link);
1216 wl_list_insert(from->layer.link.prev, &to->layer.link);
1217
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001218 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001219 broadcast_current_workspace_state(shell);
1220
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 return;
1222 }
1223
1224 if (shell->workspaces.anim_to != NULL)
1225 finish_workspace_change_animation(shell,
1226 shell->workspaces.anim_from,
1227 shell->workspaces.anim_to);
1228
1229 if (workspace_is_empty(from) &&
1230 workspace_has_only(to, surface))
1231 update_workspace(shell, index, from, to);
1232 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001233 if (shsurf != NULL &&
1234 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001235 wl_list_insert(&shell->workspaces.anim_sticky_list,
1236 &shsurf->workspace_transform.link);
1237
1238 animate_workspace_change(shell, index, from, to);
1239 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001240
1241 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001242
1243 state = ensure_focus_state(shell, seat);
1244 if (state != NULL)
1245 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001246}
1247
1248static void
1249workspace_manager_move_surface(struct wl_client *client,
1250 struct wl_resource *resource,
1251 struct wl_resource *surface_resource,
1252 uint32_t workspace)
1253{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001254 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001255 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001256 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001257 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001258 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001259
Pekka Paalanen01388e22013-04-25 13:57:44 +03001260 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001261 shell_surface = get_shell_surface(main_surface);
1262 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001264
1265 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001266}
1267
1268static const struct workspace_manager_interface workspace_manager_implementation = {
1269 workspace_manager_move_surface,
1270};
1271
1272static void
1273unbind_resource(struct wl_resource *resource)
1274{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001275 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001276}
1277
1278static void
1279bind_workspace_manager(struct wl_client *client,
1280 void *data, uint32_t version, uint32_t id)
1281{
1282 struct desktop_shell *shell = data;
1283 struct wl_resource *resource;
1284
Jason Ekstranda85118c2013-06-27 20:17:02 -05001285 resource = wl_resource_create(client,
1286 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001287
1288 if (resource == NULL) {
1289 weston_log("couldn't add workspace manager object");
1290 return;
1291 }
1292
Jason Ekstranda85118c2013-06-27 20:17:02 -05001293 wl_resource_set_implementation(resource,
1294 &workspace_manager_implementation,
1295 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001296 wl_list_insert(&shell->workspaces.client_list,
1297 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001298
1299 workspace_manager_send_state(resource,
1300 shell->workspaces.current,
1301 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001302}
1303
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001304static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001305touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1306 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1307{
1308}
1309
1310static void
1311touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1312{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001313 struct weston_touch_move_grab *move =
1314 (struct weston_touch_move_grab *) container_of(
1315 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001316
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001317 if (touch_id == 0)
1318 move->active = 0;
1319
Jonas Ådahl9484b692013-12-02 22:05:03 +01001320 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001321 shell_touch_grab_end(&move->base);
1322 free(move);
1323 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001324}
1325
1326static void
1327touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1328 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1329{
1330 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1331 struct shell_surface *shsurf = move->base.shsurf;
1332 struct weston_surface *es;
1333 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1334 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1335
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001336 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001337 return;
1338
1339 es = shsurf->surface;
1340
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001341 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001342
1343 weston_compositor_schedule_repaint(es->compositor);
1344}
1345
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001346static void
1347touch_move_grab_cancel(struct weston_touch_grab *grab)
1348{
1349 struct weston_touch_move_grab *move =
1350 (struct weston_touch_move_grab *) container_of(
1351 grab, struct shell_touch_grab, grab);
1352
1353 shell_touch_grab_end(&move->base);
1354 free(move);
1355}
1356
Rusty Lynch1084da52013-08-15 09:10:08 -07001357static const struct weston_touch_grab_interface touch_move_grab_interface = {
1358 touch_move_grab_down,
1359 touch_move_grab_up,
1360 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001361 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001362};
1363
1364static int
1365surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1366{
1367 struct weston_touch_move_grab *move;
1368
1369 if (!shsurf)
1370 return -1;
1371
Rafael Antognolli03b16592013-12-03 15:35:42 -02001372 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001373 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001374 if (shsurf->grabbed)
1375 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001376
1377 move = malloc(sizeof *move);
1378 if (!move)
1379 return -1;
1380
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001381 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001383 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001385 seat->touch->grab_y;
1386
1387 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1388 seat->touch);
1389
1390 return 0;
1391}
1392
1393static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001394noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001395{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001396}
1397
1398static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001399move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1400 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001401{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001402 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001403 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001404 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001405 int dx, dy;
1406
1407 weston_pointer_move(pointer, x, y);
1408 dx = wl_fixed_to_int(pointer->x + move->dx);
1409 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001410
1411 if (!shsurf)
1412 return;
1413
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001414 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001415
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001417}
1418
1419static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001420move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001421 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001422{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001423 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1424 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001425 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001426 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001427
Daniel Stone4dbadb12012-05-30 16:31:51 +01001428 if (pointer->button_count == 0 &&
1429 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001430 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001431 free(grab);
1432 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001433}
1434
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001435static void
1436move_grab_cancel(struct weston_pointer_grab *grab)
1437{
1438 struct shell_grab *shell_grab =
1439 container_of(grab, struct shell_grab, grab);
1440
1441 shell_grab_end(shell_grab);
1442 free(grab);
1443}
1444
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001445static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001446 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001447 move_grab_motion,
1448 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001449 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001450};
1451
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001452static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001453surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001454{
1455 struct weston_move_grab *move;
1456
1457 if (!shsurf)
1458 return -1;
1459
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001460 if (shsurf->grabbed)
1461 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001462 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001463 return 0;
1464
1465 move = malloc(sizeof *move);
1466 if (!move)
1467 return -1;
1468
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001470 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001472 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001473
1474 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001475 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001476
1477 return 0;
1478}
1479
1480static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001481common_surface_move(struct wl_resource *resource,
1482 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001483{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001484 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001485 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001486 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001487
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001488 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001489 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001490 seat->pointer->button_count > 0 &&
1491 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001493 if ((surface == shsurf->surface) &&
1494 (surface_move(shsurf, seat) < 0))
1495 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001496 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001497 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001498 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001499 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001500 if ((surface == shsurf->surface) &&
1501 (surface_touch_move(shsurf, seat) < 0))
1502 wl_resource_post_no_memory(resource);
1503 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001504}
1505
Rafael Antognollie2a34552013-12-03 15:35:45 -02001506static void
1507shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1508 struct wl_resource *seat_resource, uint32_t serial)
1509{
1510 common_surface_move(resource, seat_resource, serial);
1511}
1512
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001513struct weston_resize_grab {
1514 struct shell_grab base;
1515 uint32_t edges;
1516 int32_t width, height;
1517};
1518
1519static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001520resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1521 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001522{
1523 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001524 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001525 struct shell_surface *shsurf = resize->base.shsurf;
1526 int32_t width, height;
1527 wl_fixed_t from_x, from_y;
1528 wl_fixed_t to_x, to_y;
1529
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001530 weston_pointer_move(pointer, x, y);
1531
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001532 if (!shsurf)
1533 return;
1534
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 weston_view_from_global_fixed(shsurf->view,
1536 pointer->grab_x, pointer->grab_y,
1537 &from_x, &from_y);
1538 weston_view_from_global_fixed(shsurf->view,
1539 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001540
1541 width = resize->width;
1542 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1543 width += wl_fixed_to_int(from_x - to_x);
1544 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1545 width += wl_fixed_to_int(to_x - from_x);
1546 }
1547
1548 height = resize->height;
1549 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1550 height += wl_fixed_to_int(from_y - to_y);
1551 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1552 height += wl_fixed_to_int(to_y - from_y);
1553 }
1554
1555 shsurf->client->send_configure(shsurf->surface,
1556 resize->edges, width, height);
1557}
1558
1559static void
1560send_configure(struct weston_surface *surface,
1561 uint32_t edges, int32_t width, int32_t height)
1562{
1563 struct shell_surface *shsurf = get_shell_surface(surface);
1564
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001565 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001566 edges, width, height);
1567}
1568
1569static const struct weston_shell_client shell_client = {
1570 send_configure
1571};
1572
1573static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001574resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001575 uint32_t time, uint32_t button, uint32_t state_w)
1576{
1577 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001578 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001579 enum wl_pointer_button_state state = state_w;
1580
1581 if (pointer->button_count == 0 &&
1582 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1583 shell_grab_end(&resize->base);
1584 free(grab);
1585 }
1586}
1587
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001588static void
1589resize_grab_cancel(struct weston_pointer_grab *grab)
1590{
1591 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1592
1593 shell_grab_end(&resize->base);
1594 free(grab);
1595}
1596
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001597static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598 noop_grab_focus,
1599 resize_grab_motion,
1600 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001601 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001602};
1603
Giulio Camuffob8366642013-04-25 13:57:46 +03001604/*
1605 * Returns the bounding box of a surface and all its sub-surfaces,
1606 * in the surface coordinates system. */
1607static void
1608surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1609 int32_t *y, int32_t *w, int32_t *h) {
1610 pixman_region32_t region;
1611 pixman_box32_t *box;
1612 struct weston_subsurface *subsurface;
1613
1614 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615 surface->width,
1616 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001617
1618 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1619 pixman_region32_union_rect(&region, &region,
1620 subsurface->position.x,
1621 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622 subsurface->surface->width,
1623 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001624 }
1625
1626 box = pixman_region32_extents(&region);
1627 if (x)
1628 *x = box->x1;
1629 if (y)
1630 *y = box->y1;
1631 if (w)
1632 *w = box->x2 - box->x1;
1633 if (h)
1634 *h = box->y2 - box->y1;
1635
1636 pixman_region32_fini(&region);
1637}
1638
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001639static int
1640surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001641 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642{
1643 struct weston_resize_grab *resize;
1644
Rafael Antognolli03b16592013-12-03 15:35:42 -02001645 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001646 return 0;
1647
1648 if (edges == 0 || edges > 15 ||
1649 (edges & 3) == 3 || (edges & 12) == 12)
1650 return 0;
1651
1652 resize = malloc(sizeof *resize);
1653 if (!resize)
1654 return -1;
1655
1656 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001657 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1658 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001659
1660 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001661 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001662
1663 return 0;
1664}
1665
1666static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001667common_surface_resize(struct wl_resource *resource,
1668 struct wl_resource *seat_resource, uint32_t serial,
1669 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001670{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001671 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001672 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001673 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001674
Rafael Antognolli03b16592013-12-03 15:35:42 -02001675 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001676 return;
1677
Kristian Høgsberge3148752013-05-06 23:19:49 -04001678 if (seat->pointer->button_count == 0 ||
1679 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001680 seat->pointer->focus == NULL)
1681 return;
1682
1683 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1684 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001685 return;
1686
Kristian Høgsberge3148752013-05-06 23:19:49 -04001687 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688 wl_resource_post_no_memory(resource);
1689}
1690
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001691static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001692shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1693 struct wl_resource *seat_resource, uint32_t serial,
1694 uint32_t edges)
1695{
1696 common_surface_resize(resource, seat_resource, serial, edges);
1697}
1698
1699static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001700end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1701
1702static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001703busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001704{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001705 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001706 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001708 wl_fixed_t sx, sy;
1709
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710 view = weston_compositor_pick_view(pointer->seat->compositor,
1711 pointer->x, pointer->y,
1712 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001713
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001715 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001716}
1717
1718static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001719busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1720 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001721{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001722 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001723}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001724
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001725static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001726busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001727 uint32_t time, uint32_t button, uint32_t state)
1728{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001729 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001730 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001731 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001732
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001733 if (shsurf && button == BTN_LEFT && state) {
1734 activate(shsurf->shell, shsurf->surface, seat);
1735 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001736 } else if (shsurf && button == BTN_RIGHT && state) {
1737 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001738 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001739 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001740}
1741
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001742static void
1743busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1744{
1745 struct shell_grab *grab = (struct shell_grab *) base;
1746
1747 shell_grab_end(grab);
1748 free(grab);
1749}
1750
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001751static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001752 busy_cursor_grab_focus,
1753 busy_cursor_grab_motion,
1754 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001755 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001756};
1757
1758static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001759set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001760{
1761 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001762
1763 grab = malloc(sizeof *grab);
1764 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001765 return;
1766
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001767 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1768 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001769}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001770
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001771static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001772end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001773{
1774 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1775
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001776 if (grab->grab.interface == &busy_cursor_grab_interface &&
1777 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001778 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001779 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001780 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001781}
1782
Scott Moreau9521d5e2012-04-19 13:06:17 -06001783static void
1784ping_timer_destroy(struct shell_surface *shsurf)
1785{
1786 if (!shsurf || !shsurf->ping_timer)
1787 return;
1788
1789 if (shsurf->ping_timer->source)
1790 wl_event_source_remove(shsurf->ping_timer->source);
1791
1792 free(shsurf->ping_timer);
1793 shsurf->ping_timer = NULL;
1794}
1795
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001796static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001797ping_timeout_handler(void *data)
1798{
1799 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001800 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001801
Scott Moreau9521d5e2012-04-19 13:06:17 -06001802 /* Client is not responding */
1803 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804
1805 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08001806 if (seat->pointer->focus &&
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001807 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001808 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001809
1810 return 1;
1811}
1812
1813static void
1814ping_handler(struct weston_surface *surface, uint32_t serial)
1815{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001816 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001817 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001818 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001819
1820 if (!shsurf)
1821 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001822 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001823 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001824
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001825 if (shsurf->surface == shsurf->shell->grab_surface)
1826 return;
1827
Scott Moreauff1db4a2012-04-17 19:06:18 -06001828 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001829 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001830 if (!shsurf->ping_timer)
1831 return;
1832
1833 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001834 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1835 shsurf->ping_timer->source =
1836 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1837 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1838
Rafael Antognollie2a34552013-12-03 15:35:45 -02001839 if (shell_surface_is_wl_shell_surface(shsurf))
1840 wl_shell_surface_send_ping(shsurf->resource, serial);
1841 else if (shell_surface_is_xdg_surface(shsurf))
1842 xdg_surface_send_ping(shsurf->resource, serial);
1843 else if (shell_surface_is_xdg_popup(shsurf))
1844 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001845 }
1846}
1847
1848static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001849handle_pointer_focus(struct wl_listener *listener, void *data)
1850{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001851 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001853 struct weston_compositor *compositor;
1854 struct shell_surface *shsurf;
1855 uint32_t serial;
1856
Jason Ekstranda7af7042013-10-12 22:38:11 -05001857 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001858 return;
1859
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860 compositor = view->surface->compositor;
1861 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001862
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001863 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001864 set_busy_cursor(shsurf, pointer);
1865 } else {
1866 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001867 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001868 }
1869}
1870
1871static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001872create_pointer_focus_listener(struct weston_seat *seat)
1873{
1874 struct wl_listener *listener;
1875
Kristian Høgsberge3148752013-05-06 23:19:49 -04001876 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001877 return;
1878
1879 listener = malloc(sizeof *listener);
1880 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001881 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001882}
1883
1884static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001885xdg_surface_set_transient_for(struct wl_client *client,
1886 struct wl_resource *resource,
1887 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001888{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001889 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001890 struct weston_surface *parent;
1891
1892 if (parent_resource)
1893 parent = wl_resource_get_user_data(parent_resource);
1894 else
1895 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001896
1897 shell_surface_set_parent(shsurf, parent);
1898}
1899
1900static void
1901surface_pong(struct shell_surface *shsurf, uint32_t serial)
1902{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001903 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001904 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001905
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001906 if (shsurf->ping_timer == NULL)
1907 /* Just ignore unsolicited pong. */
1908 return;
1909
Scott Moreauff1db4a2012-04-17 19:06:18 -06001910 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001911 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001912 wl_list_for_each(seat, &ec->seat_list, link) {
1913 if(seat->pointer)
1914 end_busy_cursor(shsurf, seat->pointer);
1915 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001916 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001917 }
1918}
1919
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001920static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001921shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1922 uint32_t serial)
1923{
1924 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1925
1926 surface_pong(shsurf, serial);
1927
1928}
1929
1930static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001931set_title(struct shell_surface *shsurf, const char *title)
1932{
1933 free(shsurf->title);
1934 shsurf->title = strdup(title);
1935}
1936
1937static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001938shell_surface_set_title(struct wl_client *client,
1939 struct wl_resource *resource, const char *title)
1940{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001941 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001942
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001943 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001944}
1945
1946static void
1947shell_surface_set_class(struct wl_client *client,
1948 struct wl_resource *resource, const char *class)
1949{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001950 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001951
1952 free(shsurf->class);
1953 shsurf->class = strdup(class);
1954}
1955
Alex Wu4539b082012-03-01 12:57:46 +08001956static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001957restore_output_mode(struct weston_output *output)
1958{
Hardening57388e42013-09-18 23:56:36 +02001959 if (output->current_mode != output->original_mode ||
1960 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001961 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001962 output->original_mode,
1963 output->original_scale,
1964 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001965}
1966
1967static void
1968restore_all_output_modes(struct weston_compositor *compositor)
1969{
1970 struct weston_output *output;
1971
1972 wl_list_for_each(output, &compositor->output_list, link)
1973 restore_output_mode(output);
1974}
1975
Philip Withnallbecb77e2013-11-25 18:01:30 +00001976static int
1977get_output_panel_height(struct desktop_shell *shell,
1978 struct weston_output *output)
1979{
1980 struct weston_view *view;
1981 int panel_height = 0;
1982
1983 if (!output)
1984 return 0;
1985
1986 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1987 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001988 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00001989 break;
1990 }
1991 }
1992
1993 return panel_height;
1994}
1995
Philip Withnall07926d92013-11-25 18:01:40 +00001996/* The surface will be inserted into the list immediately after the link
1997 * returned by this function (i.e. will be stacked immediately above the
1998 * returned link). */
1999static struct wl_list *
2000shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2001{
2002 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002003 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002004
2005 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002006 case SHELL_SURFACE_POPUP:
2007 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002008 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002009 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002010 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002011 /* Move the surface to its parent layer so
2012 * that surfaces which are transient for
2013 * fullscreen surfaces don't get hidden by the
2014 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002015
2016 /* TODO: Handle a parent with multiple views */
2017 parent = get_default_view(shsurf->parent);
2018 if (parent)
2019 return parent->layer_link.prev;
2020 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002021 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002022
2023 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002024 return &shsurf->shell->fullscreen_layer.view_list;
2025
Philip Withnall07926d92013-11-25 18:01:40 +00002026 case SHELL_SURFACE_NONE:
2027 default:
2028 /* Go to the fallback, below. */
2029 break;
2030 }
2031
2032 /* Move the surface to a normal workspace layer so that surfaces
2033 * which were previously fullscreen or transient are no longer
2034 * rendered on top. */
2035 ws = get_current_workspace(shsurf->shell);
2036 return &ws->layer.view_list;
2037}
2038
Philip Withnall648a4dd2013-11-25 18:01:44 +00002039static void
2040shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2041{
2042 struct shell_surface *child;
2043
2044 /* Move the child layers to the same workspace as shsurf. They will be
2045 * stacked above shsurf. */
2046 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2047 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2048 weston_view_geometry_dirty(child->view);
2049 wl_list_remove(&child->view->layer_link);
2050 wl_list_insert(shsurf->view->layer_link.prev,
2051 &child->view->layer_link);
2052 weston_view_geometry_dirty(child->view);
2053 weston_surface_damage(child->surface);
2054
2055 /* Recurse. We don’t expect this to recurse very far (if
2056 * at all) because that would imply we have transient
2057 * (or popup) children of transient surfaces, which
2058 * would be unusual. */
2059 shell_surface_update_child_surface_layers(child);
2060 }
2061 }
2062}
2063
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002064/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002065 * geometry to ensure the changes are redrawn.
2066 *
2067 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2068 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002069static void
2070shell_surface_update_layer(struct shell_surface *shsurf)
2071{
2072 struct wl_list *new_layer_link;
2073
2074 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2075
2076 if (new_layer_link == &shsurf->view->layer_link)
2077 return;
2078
2079 weston_view_geometry_dirty(shsurf->view);
2080 wl_list_remove(&shsurf->view->layer_link);
2081 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2082 weston_view_geometry_dirty(shsurf->view);
2083 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002084
2085 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002086}
2087
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002088static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002089shell_surface_set_parent(struct shell_surface *shsurf,
2090 struct weston_surface *parent)
2091{
2092 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002093
2094 wl_list_remove(&shsurf->children_link);
2095 wl_list_init(&shsurf->children_link);
2096
2097 /* Insert into the parent surface’s child list. */
2098 if (parent != NULL) {
2099 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2100 if (parent_shsurf != NULL)
2101 wl_list_insert(&parent_shsurf->children_list,
2102 &shsurf->children_link);
2103 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002104}
2105
2106static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002107shell_surface_set_output(struct shell_surface *shsurf,
2108 struct weston_output *output)
2109{
2110 struct weston_surface *es = shsurf->surface;
2111
2112 /* get the default output, if the client set it as NULL
2113 check whether the ouput is available */
2114 if (output)
2115 shsurf->output = output;
2116 else if (es->output)
2117 shsurf->output = es->output;
2118 else
2119 shsurf->output = get_default_output(es->compositor);
2120}
2121
2122static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002123surface_clear_next_states(struct shell_surface *shsurf)
2124{
2125 shsurf->next_state.maximized = false;
2126 shsurf->next_state.fullscreen = false;
2127
2128 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2129 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2130 shsurf->state_changed = true;
2131}
2132
2133static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002134set_toplevel(struct shell_surface *shsurf)
2135{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002136 shell_surface_set_parent(shsurf, NULL);
2137 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002138 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002139
2140 /* The layer_link is updated in set_surface_type(),
2141 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002142}
2143
2144static void
2145shell_surface_set_toplevel(struct wl_client *client,
2146 struct wl_resource *resource)
2147{
2148 struct shell_surface *surface = wl_resource_get_user_data(resource);
2149
2150 set_toplevel(surface);
2151}
2152
2153static void
2154set_transient(struct shell_surface *shsurf,
2155 struct weston_surface *parent, int x, int y, uint32_t flags)
2156{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002157 assert(parent != NULL);
2158
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002159 shell_surface_set_parent(shsurf, parent);
2160
2161 surface_clear_next_states(shsurf);
2162
Philip Withnallbecb77e2013-11-25 18:01:30 +00002163 shsurf->transient.x = x;
2164 shsurf->transient.y = y;
2165 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002166
Rafael Antognollied207b42013-12-03 15:35:43 -02002167 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002168 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002169 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002170
2171 /* The layer_link is updated in set_surface_type(),
2172 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002173}
2174
2175static void
2176shell_surface_set_transient(struct wl_client *client,
2177 struct wl_resource *resource,
2178 struct wl_resource *parent_resource,
2179 int x, int y, uint32_t flags)
2180{
2181 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2182 struct weston_surface *parent =
2183 wl_resource_get_user_data(parent_resource);
2184
2185 set_transient(shsurf, parent, x, y, flags);
2186}
2187
2188static void
2189set_fullscreen(struct shell_surface *shsurf,
2190 uint32_t method,
2191 uint32_t framerate,
2192 struct weston_output *output)
2193{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002194 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002195
2196 shsurf->fullscreen_output = shsurf->output;
2197 shsurf->fullscreen.type = method;
2198 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002199
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002200 shsurf->next_state.fullscreen = true;
2201 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002202 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002203
2204 shsurf->client->send_configure(shsurf->surface, 0,
2205 shsurf->output->width,
2206 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002207
2208 /* The layer_link is updated in set_surface_type(),
2209 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002210}
2211
2212static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002213weston_view_set_initial_position(struct weston_view *view,
2214 struct desktop_shell *shell);
2215
2216static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002217unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002218{
Philip Withnallf85fe842013-11-25 18:01:37 +00002219 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002220 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2221 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002222 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002223 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002224 shsurf->fullscreen_output = NULL;
2225
Alex Wu4539b082012-03-01 12:57:46 +08002226 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2227 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002228
Alex Wu4539b082012-03-01 12:57:46 +08002229 wl_list_remove(&shsurf->fullscreen.transform.link);
2230 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002231
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 if (shsurf->fullscreen.black_view)
2233 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2234 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002235
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002236 if (shsurf->saved_position_valid)
2237 weston_view_set_position(shsurf->view,
2238 shsurf->saved_x, shsurf->saved_y);
2239 else
2240 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2241
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002242 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002244 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002245 shsurf->saved_rotation_valid = false;
2246 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002247
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002248 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002249}
2250
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002251static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002252shell_surface_set_fullscreen(struct wl_client *client,
2253 struct wl_resource *resource,
2254 uint32_t method,
2255 uint32_t framerate,
2256 struct wl_resource *output_resource)
2257{
2258 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2259 struct weston_output *output;
2260
2261 if (output_resource)
2262 output = wl_resource_get_user_data(output_resource);
2263 else
2264 output = NULL;
2265
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002266 shell_surface_set_parent(shsurf, NULL);
2267
Rafael Antognolli03b16592013-12-03 15:35:42 -02002268 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002269 set_fullscreen(shsurf, method, framerate, output);
2270}
2271
2272static void
2273set_popup(struct shell_surface *shsurf,
2274 struct weston_surface *parent,
2275 struct weston_seat *seat,
2276 uint32_t serial,
2277 int32_t x,
2278 int32_t y)
2279{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002280 assert(parent != NULL);
2281
Philip Withnallbecb77e2013-11-25 18:01:30 +00002282 shsurf->popup.shseat = get_shell_seat(seat);
2283 shsurf->popup.serial = serial;
2284 shsurf->popup.x = x;
2285 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002286
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002287 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002288}
2289
2290static void
2291shell_surface_set_popup(struct wl_client *client,
2292 struct wl_resource *resource,
2293 struct wl_resource *seat_resource,
2294 uint32_t serial,
2295 struct wl_resource *parent_resource,
2296 int32_t x, int32_t y, uint32_t flags)
2297{
2298 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002299 struct weston_surface *parent =
2300 wl_resource_get_user_data(parent_resource);
2301
2302 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002303
Rafael Antognolli03b16592013-12-03 15:35:42 -02002304 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002305 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002306 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002307 wl_resource_get_user_data(seat_resource),
2308 serial, x, y);
2309}
2310
2311static void
2312set_maximized(struct shell_surface *shsurf,
2313 struct weston_output *output)
2314{
2315 struct desktop_shell *shell;
2316 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002317
Philip Withnall352e7ed2013-11-25 18:01:35 +00002318 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002319
2320 shell = shell_surface_get_shell(shsurf);
2321 panel_height = get_output_panel_height(shell, shsurf->output);
2322 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2323
2324 shsurf->client->send_configure(shsurf->surface, edges,
2325 shsurf->output->width,
2326 shsurf->output->height - panel_height);
2327
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002328 shsurf->next_state.maximized = true;
2329 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002330 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002331}
2332
2333static void
2334unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002335{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002336 /* undo all maximized things here */
2337 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002338
2339 if (shsurf->saved_position_valid)
2340 weston_view_set_position(shsurf->view,
2341 shsurf->saved_x, shsurf->saved_y);
2342 else
2343 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002344
2345 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346 wl_list_insert(&shsurf->view->geometry.transformation_list,
2347 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002348 shsurf->saved_rotation_valid = false;
2349 }
2350
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002351 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002352}
2353
Philip Withnallbecb77e2013-11-25 18:01:30 +00002354static void
2355shell_surface_set_maximized(struct wl_client *client,
2356 struct wl_resource *resource,
2357 struct wl_resource *output_resource)
2358{
2359 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2360 struct weston_output *output;
2361
2362 if (output_resource)
2363 output = wl_resource_get_user_data(output_resource);
2364 else
2365 output = NULL;
2366
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002367 shell_surface_set_parent(shsurf, NULL);
2368
Rafael Antognolli03b16592013-12-03 15:35:42 -02002369 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002370 set_maximized(shsurf, output);
2371}
2372
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002373/* This is only ever called from set_surface_type(), so there’s no need to
2374 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002375static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002376reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002377{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002378 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002379 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002380 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002381 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002382
Pekka Paalanen98262232011-12-01 10:42:22 +02002383 return 0;
2384}
2385
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002386static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002387set_full_output(struct shell_surface *shsurf)
2388{
2389 shsurf->saved_x = shsurf->view->geometry.x;
2390 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002391 shsurf->saved_width = shsurf->surface->width;
2392 shsurf->saved_height = shsurf->surface->height;
2393 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002394 shsurf->saved_position_valid = true;
2395
2396 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2397 wl_list_remove(&shsurf->rotation.transform.link);
2398 wl_list_init(&shsurf->rotation.transform.link);
2399 weston_view_geometry_dirty(shsurf->view);
2400 shsurf->saved_rotation_valid = true;
2401 }
2402}
2403
2404static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002405set_surface_type(struct shell_surface *shsurf)
2406{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002407 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002408 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002409
Philip Withnallbecb77e2013-11-25 18:01:30 +00002410 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002411
Rafael Antognolli03b16592013-12-03 15:35:42 -02002412 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002413 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002414
2415 switch (shsurf->type) {
2416 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002417 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002418 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002419 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002420 weston_view_set_position(shsurf->view,
2421 pev->geometry.x + shsurf->transient.x,
2422 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002423 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002424 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002425
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002426 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002427 weston_view_set_position(shsurf->view, shsurf->transient.x,
2428 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002429 break;
2430
Philip Withnall0f640e22013-11-25 18:01:31 +00002431 case SHELL_SURFACE_POPUP:
2432 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002433 default:
2434 break;
2435 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002436
2437 /* Update the surface’s layer. */
2438 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002439}
2440
Tiago Vignattibe143262012-04-16 17:31:41 +03002441static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002442shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002443{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002444 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002445}
2446
Alex Wu21858432012-04-01 20:13:08 +08002447static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002448black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002449
Jason Ekstranda7af7042013-10-12 22:38:11 -05002450static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002451create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002452 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002453 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002454{
2455 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002456 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002457
2458 surface = weston_surface_create(ec);
2459 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002460 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002461 return NULL;
2462 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002463 view = weston_view_create(surface);
2464 if (surface == NULL) {
2465 weston_log("no memory\n");
2466 weston_surface_destroy(surface);
2467 return NULL;
2468 }
Alex Wu4539b082012-03-01 12:57:46 +08002469
Alex Wu21858432012-04-01 20:13:08 +08002470 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002471 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002472 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002473 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002474 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002475 pixman_region32_fini(&surface->input);
2476 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002477
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002478 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002479 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002480
2481 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002482}
2483
Philip Withnalled8f1a92013-11-25 18:01:43 +00002484static void
2485shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2486{
2487 struct weston_output *output = shsurf->fullscreen_output;
2488
Rafael Antognolli03b16592013-12-03 15:35:42 -02002489 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002490
2491 if (!shsurf->fullscreen.black_view)
2492 shsurf->fullscreen.black_view =
2493 create_black_surface(shsurf->surface->compositor,
2494 shsurf->surface,
2495 output->x, output->y,
2496 output->width,
2497 output->height);
2498
2499 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2500 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2501 wl_list_insert(&shsurf->view->layer_link,
2502 &shsurf->fullscreen.black_view->layer_link);
2503 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2504 weston_surface_damage(shsurf->surface);
2505}
2506
Alex Wu4539b082012-03-01 12:57:46 +08002507/* Create black surface and append it to the associated fullscreen surface.
2508 * Handle size dismatch and positioning according to the method. */
2509static void
2510shell_configure_fullscreen(struct shell_surface *shsurf)
2511{
2512 struct weston_output *output = shsurf->fullscreen_output;
2513 struct weston_surface *surface = shsurf->surface;
2514 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002515 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002516 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002517
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002518 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2519 restore_output_mode(output);
2520
Philip Withnalled8f1a92013-11-25 18:01:43 +00002521 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002522
Jason Ekstranda7af7042013-10-12 22:38:11 -05002523 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002524 &surf_width, &surf_height);
2525
Alex Wu4539b082012-03-01 12:57:46 +08002526 switch (shsurf->fullscreen.type) {
2527 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002528 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002529 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002530 break;
2531 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002532 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002533 if (output->width == surf_width &&
2534 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002535 weston_view_set_position(shsurf->view,
2536 output->x - surf_x,
2537 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002538 break;
2539 }
2540
Alex Wu4539b082012-03-01 12:57:46 +08002541 matrix = &shsurf->fullscreen.transform.matrix;
2542 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002543
Scott Moreau1bad5db2012-08-18 01:04:05 -06002544 output_aspect = (float) output->width /
2545 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546 /* XXX: Use surf_width and surf_height here? */
2547 surface_aspect = (float) surface->width /
2548 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002549 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002550 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002551 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002552 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002553 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002554 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002555
Alex Wu4539b082012-03-01 12:57:46 +08002556 weston_matrix_scale(matrix, scale, scale, 1);
2557 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002558 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002559 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002560 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2561 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002562 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002563
Alex Wu4539b082012-03-01 12:57:46 +08002564 break;
2565 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002566 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002567 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002568 surf_width * surface->buffer_viewport.scale,
2569 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002570 shsurf->fullscreen.framerate};
2571
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002572 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002573 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002574 weston_view_set_position(shsurf->view,
2575 output->x - surf_x,
2576 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002577 shsurf->fullscreen.black_view->surface->width = output->width;
2578 shsurf->fullscreen.black_view->surface->height = output->height;
2579 weston_view_set_position(shsurf->fullscreen.black_view,
2580 output->x - surf_x,
2581 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002582 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002583 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002584 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002585 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002586 }
Alex Wubd3354b2012-04-17 17:20:49 +08002587 }
Alex Wu4539b082012-03-01 12:57:46 +08002588 break;
2589 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002590 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002591 break;
2592 default:
2593 break;
2594 }
2595}
2596
Alex Wu4539b082012-03-01 12:57:46 +08002597static void
2598shell_map_fullscreen(struct shell_surface *shsurf)
2599{
Alex Wubd3354b2012-04-17 17:20:49 +08002600 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002601}
2602
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002603static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002604set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2605{
2606 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002607 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002608 shsurf->transient.x = x;
2609 shsurf->transient.y = y;
2610 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002611
2612 shell_surface_set_parent(shsurf, NULL);
2613
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002614 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002615 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002616}
2617
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002618static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002619
2620static void
2621destroy_shell_seat(struct wl_listener *listener, void *data)
2622{
2623 struct shell_seat *shseat =
2624 container_of(listener,
2625 struct shell_seat, seat_destroy_listener);
2626 struct shell_surface *shsurf, *prev = NULL;
2627
2628 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002629 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002630 shseat->popup_grab.client = NULL;
2631
2632 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2633 shsurf->popup.shseat = NULL;
2634 if (prev) {
2635 wl_list_init(&prev->popup.grab_link);
2636 }
2637 prev = shsurf;
2638 }
2639 wl_list_init(&prev->popup.grab_link);
2640 }
2641
2642 wl_list_remove(&shseat->seat_destroy_listener.link);
2643 free(shseat);
2644}
2645
2646static struct shell_seat *
2647create_shell_seat(struct weston_seat *seat)
2648{
2649 struct shell_seat *shseat;
2650
2651 shseat = calloc(1, sizeof *shseat);
2652 if (!shseat) {
2653 weston_log("no memory to allocate shell seat\n");
2654 return NULL;
2655 }
2656
2657 shseat->seat = seat;
2658 wl_list_init(&shseat->popup_grab.surfaces_list);
2659
2660 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2661 wl_signal_add(&seat->destroy_signal,
2662 &shseat->seat_destroy_listener);
2663
2664 return shseat;
2665}
2666
2667static struct shell_seat *
2668get_shell_seat(struct weston_seat *seat)
2669{
2670 struct wl_listener *listener;
2671
2672 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2673 if (listener == NULL)
2674 return create_shell_seat(seat);
2675
2676 return container_of(listener,
2677 struct shell_seat, seat_destroy_listener);
2678}
2679
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002680static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002681popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002682{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002683 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002684 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002685 struct shell_seat *shseat =
2686 container_of(grab, struct shell_seat, popup_grab.grab);
2687 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002688 wl_fixed_t sx, sy;
2689
Jason Ekstranda7af7042013-10-12 22:38:11 -05002690 view = weston_compositor_pick_view(pointer->seat->compositor,
2691 pointer->x, pointer->y,
2692 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002693
Jason Ekstranda7af7042013-10-12 22:38:11 -05002694 if (view && view->surface->resource &&
2695 wl_resource_get_client(view->surface->resource) == client) {
2696 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002697 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002698 weston_pointer_set_focus(pointer, NULL,
2699 wl_fixed_from_int(0),
2700 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002701 }
2702}
2703
2704static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002705popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2706 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002707{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002708 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002709 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002710 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002711
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002712 weston_pointer_move(pointer, x, y);
2713
Neil Roberts96d790e2013-09-19 17:32:00 +01002714 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 weston_view_from_global_fixed(pointer->focus,
2716 pointer->x, pointer->y,
2717 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002718 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002719 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002720}
2721
2722static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002723popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002724 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002725{
2726 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002727 struct shell_seat *shseat =
2728 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002729 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002730 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002731 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002732 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002733
Neil Roberts96d790e2013-09-19 17:32:00 +01002734 resource_list = &grab->pointer->focus_resource_list;
2735 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002736 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002737 wl_resource_for_each(resource, resource_list) {
2738 wl_pointer_send_button(resource, serial,
2739 time, button, state);
2740 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002741 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002742 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002743 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002744 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002745 }
2746
Daniel Stone4dbadb12012-05-30 16:31:51 +01002747 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002748 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002749}
2750
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002751static void
2752popup_grab_cancel(struct weston_pointer_grab *grab)
2753{
2754 popup_grab_end(grab->pointer);
2755}
2756
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002757static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002758 popup_grab_focus,
2759 popup_grab_motion,
2760 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002761 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002762};
2763
2764static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002765shell_surface_send_popup_done(struct shell_surface *shsurf)
2766{
2767 if (shell_surface_is_wl_shell_surface(shsurf))
2768 wl_shell_surface_send_popup_done(shsurf->resource);
2769 else if (shell_surface_is_xdg_popup(shsurf))
2770 xdg_popup_send_popup_done(shsurf->resource,
2771 shsurf->popup.serial);
2772}
2773
2774static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002775popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002776{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002777 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002778 struct shell_seat *shseat =
2779 container_of(grab, struct shell_seat, popup_grab.grab);
2780 struct shell_surface *shsurf;
2781 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002782
2783 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002784 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002785 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002786 shseat->popup_grab.grab.interface = NULL;
2787 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002788 /* Send the popup_done event to all the popups open */
2789 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002790 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002791 shsurf->popup.shseat = NULL;
2792 if (prev) {
2793 wl_list_init(&prev->popup.grab_link);
2794 }
2795 prev = shsurf;
2796 }
2797 wl_list_init(&prev->popup.grab_link);
2798 wl_list_init(&shseat->popup_grab.surfaces_list);
2799 }
2800}
2801
2802static void
2803add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2804{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002805 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002806
2807 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002808 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002809 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002810 /* We must make sure here that this popup was opened after
2811 * a mouse press, and not just by moving around with other
2812 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002813 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002814 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002815
Rob Bradforddfe31052013-06-26 19:49:11 +01002816 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002817 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002818 } else {
2819 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002820 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002821}
2822
2823static void
2824remove_popup_grab(struct shell_surface *shsurf)
2825{
2826 struct shell_seat *shseat = shsurf->popup.shseat;
2827
2828 wl_list_remove(&shsurf->popup.grab_link);
2829 wl_list_init(&shsurf->popup.grab_link);
2830 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002831 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002832 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002833 }
2834}
2835
2836static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002837shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002838{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002839 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002840 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002841
Jason Ekstranda7af7042013-10-12 22:38:11 -05002842 shsurf->surface->output = parent_view->output;
2843 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002844
Jason Ekstranda7af7042013-10-12 22:38:11 -05002845 weston_view_set_transform_parent(shsurf->view, parent_view);
2846 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2847 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002848
Kristian Høgsberge3148752013-05-06 23:19:49 -04002849 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002850 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002851 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002852 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002853 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002854 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002855}
2856
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002857static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002858 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002859 shell_surface_move,
2860 shell_surface_resize,
2861 shell_surface_set_toplevel,
2862 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002863 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002864 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002865 shell_surface_set_maximized,
2866 shell_surface_set_title,
2867 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002868};
2869
2870static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002871destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002872{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002873 struct shell_surface *child, *next;
2874
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002875 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2876
Giulio Camuffo5085a752013-03-25 21:42:45 +01002877 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2878 remove_popup_grab(shsurf);
2879 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002880
Alex Wubd3354b2012-04-17 17:20:49 +08002881 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002882 shell_surface_is_top_fullscreen(shsurf))
2883 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002884
Jason Ekstranda7af7042013-10-12 22:38:11 -05002885 if (shsurf->fullscreen.black_view)
2886 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002887
Alex Wubd3354b2012-04-17 17:20:49 +08002888 /* As destroy_resource() use wl_list_for_each_safe(),
2889 * we can always remove the listener.
2890 */
2891 wl_list_remove(&shsurf->surface_destroy_listener.link);
2892 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002893 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002894 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002895
Jason Ekstranda7af7042013-10-12 22:38:11 -05002896 weston_view_destroy(shsurf->view);
2897
Philip Withnall648a4dd2013-11-25 18:01:44 +00002898 wl_list_remove(&shsurf->children_link);
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002899 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link) {
2900 wl_list_remove(&child->children_link);
2901 child->parent = NULL;
2902 }
Philip Withnall648a4dd2013-11-25 18:01:44 +00002903
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002904 wl_list_remove(&shsurf->link);
2905 free(shsurf);
2906}
2907
2908static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002909shell_destroy_shell_surface(struct wl_resource *resource)
2910{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002911 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002912
2913 destroy_shell_surface(shsurf);
2914}
2915
2916static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002917shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002918{
2919 struct shell_surface *shsurf = container_of(listener,
2920 struct shell_surface,
2921 surface_destroy_listener);
2922
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002923 if (shsurf->resource)
2924 wl_resource_destroy(shsurf->resource);
2925 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002926 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002927}
2928
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002929static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002930shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002931static void
2932shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002933
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002934struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002935get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002936{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002937 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002938 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002939 else
2940 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002941}
2942
Rafael Antognollie2a34552013-12-03 15:35:45 -02002943static struct shell_surface *
2944create_common_surface(void *shell, struct weston_surface *surface,
2945 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002946{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002947 struct shell_surface *shsurf;
2948
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002949 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002950 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002951 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002952 }
2953
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002954 shsurf = calloc(1, sizeof *shsurf);
2955 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002956 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002957 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002958 }
2959
Jason Ekstranda7af7042013-10-12 22:38:11 -05002960 shsurf->view = weston_view_create(surface);
2961 if (!shsurf->view) {
2962 weston_log("no memory to allocate shell surface\n");
2963 free(shsurf);
2964 return NULL;
2965 }
2966
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002967 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002968 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002969 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002970
Tiago Vignattibc052c92012-04-19 16:18:18 +03002971 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002972 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002973 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002974 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002975 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002976 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002977 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2978 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002979 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002980 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002981 wl_list_init(&shsurf->fullscreen.transform.link);
2982
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002983 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002984 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002985 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002986 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002987
2988 /* init link so its safe to always remove it in destroy_shell_surface */
2989 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002990 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002991
Pekka Paalanen460099f2012-01-20 16:48:25 +02002992 /* empty when not in use */
2993 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002994 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002995
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002996 wl_list_init(&shsurf->workspace_transform.link);
2997
Philip Withnall648a4dd2013-11-25 18:01:44 +00002998 wl_list_init(&shsurf->children_link);
2999 wl_list_init(&shsurf->children_list);
3000 shsurf->parent = NULL;
3001
Pekka Paalanen98262232011-12-01 10:42:22 +02003002 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003003
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003004 shsurf->client = client;
3005
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003006 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003007}
3008
Rafael Antognollie2a34552013-12-03 15:35:45 -02003009static struct shell_surface *
3010create_shell_surface(void *shell, struct weston_surface *surface,
3011 const struct weston_shell_client *client)
3012{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003013 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003014}
3015
Jason Ekstranda7af7042013-10-12 22:38:11 -05003016static struct weston_view *
3017get_primary_view(void *shell, struct shell_surface *shsurf)
3018{
3019 return shsurf->view;
3020}
3021
Tiago Vignattibc052c92012-04-19 16:18:18 +03003022static void
3023shell_get_shell_surface(struct wl_client *client,
3024 struct wl_resource *resource,
3025 uint32_t id,
3026 struct wl_resource *surface_resource)
3027{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003028 struct weston_surface *surface =
3029 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003030 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003031 struct shell_surface *shsurf;
3032
3033 if (get_shell_surface(surface)) {
3034 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003035 WL_DISPLAY_ERROR_INVALID_OBJECT,
3036 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003037 return;
3038 }
3039
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003040 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003041 if (!shsurf) {
3042 wl_resource_post_error(surface_resource,
3043 WL_DISPLAY_ERROR_INVALID_OBJECT,
3044 "surface->configure already set");
3045 return;
3046 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003047
Jason Ekstranda85118c2013-06-27 20:17:02 -05003048 shsurf->resource =
3049 wl_resource_create(client,
3050 &wl_shell_surface_interface, 1, id);
3051 wl_resource_set_implementation(shsurf->resource,
3052 &shell_surface_implementation,
3053 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003054}
3055
Rafael Antognollie2a34552013-12-03 15:35:45 -02003056static bool
3057shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3058{
3059 return wl_resource_instance_of(shsurf->resource,
3060 &wl_shell_surface_interface,
3061 &shell_surface_implementation);
3062}
3063
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003064static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003065 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003066};
3067
Rafael Antognollie2a34552013-12-03 15:35:45 -02003068/****************************
3069 * xdg-shell implementation */
3070
3071static void
3072xdg_surface_destroy(struct wl_client *client,
3073 struct wl_resource *resource)
3074{
3075 wl_resource_destroy(resource);
3076}
3077
3078static void
3079xdg_surface_pong(struct wl_client *client,
3080 struct wl_resource *resource,
3081 uint32_t serial)
3082{
3083 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3084
3085 surface_pong(shsurf, serial);
3086}
3087
3088static void
3089xdg_surface_set_app_id(struct wl_client *client,
3090 struct wl_resource *resource,
3091 const char *app_id)
3092{
3093 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3094
3095 free(shsurf->class);
3096 shsurf->class = strdup(app_id);
3097}
3098
3099static void
3100xdg_surface_set_title(struct wl_client *client,
3101 struct wl_resource *resource, const char *title)
3102{
3103 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3104
3105 set_title(shsurf, title);
3106}
3107
3108static void
3109xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3110 struct wl_resource *seat_resource, uint32_t serial)
3111{
3112 common_surface_move(resource, seat_resource, serial);
3113}
3114
3115static void
3116xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3117 struct wl_resource *seat_resource, uint32_t serial,
3118 uint32_t edges)
3119{
3120 common_surface_resize(resource, seat_resource, serial, edges);
3121}
3122
3123static void
3124xdg_surface_set_output(struct wl_client *client,
3125 struct wl_resource *resource,
3126 struct wl_resource *output_resource)
3127{
3128 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3129 struct weston_output *output;
3130
3131 if (output_resource)
3132 output = wl_resource_get_user_data(output_resource);
3133 else
3134 output = NULL;
3135
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003136 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003137}
3138
3139static void
3140xdg_surface_set_fullscreen(struct wl_client *client,
3141 struct wl_resource *resource)
3142{
3143 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3144
3145 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3146 return;
3147
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003148 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003149 set_fullscreen(shsurf,
3150 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003151 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003152}
3153
3154static void
3155xdg_surface_unset_fullscreen(struct wl_client *client,
3156 struct wl_resource *resource)
3157{
3158 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003159 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003160
3161 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3162 return;
3163
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003164 if (!shsurf->next_state.fullscreen)
3165 return;
3166
Rafael Antognollie2a34552013-12-03 15:35:45 -02003167 shsurf->next_state.fullscreen = false;
3168 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003169
3170 if (shsurf->saved_size_valid) {
3171 width = shsurf->saved_width;
3172 height = shsurf->saved_height;
3173 shsurf->saved_size_valid = false;
3174 } else {
3175 width = shsurf->surface->width;
3176 height = shsurf->surface->height;
3177 }
3178
3179 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003180}
3181
3182static void
3183xdg_surface_set_maximized(struct wl_client *client,
3184 struct wl_resource *resource)
3185{
3186 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3187
3188 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3189 return;
3190
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003191 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003192 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003193}
3194
3195static void
3196xdg_surface_unset_maximized(struct wl_client *client,
3197 struct wl_resource *resource)
3198{
3199 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003200 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003201
3202 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3203 return;
3204
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003205 if (!shsurf->next_state.maximized)
3206 return;
3207
Rafael Antognollie2a34552013-12-03 15:35:45 -02003208 shsurf->next_state.maximized = false;
3209 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003210
3211 if (shsurf->saved_size_valid) {
3212 width = shsurf->saved_width;
3213 height = shsurf->saved_height;
3214 shsurf->saved_size_valid = false;
3215 } else {
3216 width = shsurf->surface->width;
3217 height = shsurf->surface->height;
3218 }
3219
3220 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003221}
3222
3223static const struct xdg_surface_interface xdg_surface_implementation = {
3224 xdg_surface_destroy,
3225 xdg_surface_set_transient_for,
3226 xdg_surface_set_title,
3227 xdg_surface_set_app_id,
3228 xdg_surface_pong,
3229 xdg_surface_move,
3230 xdg_surface_resize,
3231 xdg_surface_set_output,
3232 xdg_surface_set_fullscreen,
3233 xdg_surface_unset_fullscreen,
3234 xdg_surface_set_maximized,
3235 xdg_surface_unset_maximized,
3236 NULL /* set_minimized */
3237};
3238
3239static void
3240xdg_send_configure(struct weston_surface *surface,
3241 uint32_t edges, int32_t width, int32_t height)
3242{
3243 struct shell_surface *shsurf = get_shell_surface(surface);
3244
3245 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3246}
3247
3248static const struct weston_shell_client xdg_client = {
3249 xdg_send_configure
3250};
3251
3252static void
3253xdg_use_unstable_version(struct wl_client *client,
3254 struct wl_resource *resource,
3255 int32_t version)
3256{
3257 if (version > 1) {
3258 wl_resource_post_error(resource,
3259 1,
3260 "xdg-shell:: version not implemented yet.");
3261 return;
3262 }
3263}
3264
3265static struct shell_surface *
3266create_xdg_surface(void *shell, struct weston_surface *surface,
3267 const struct weston_shell_client *client)
3268{
3269 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003270
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003271 shsurf = create_common_surface(shell, surface, client);
3272 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003273
3274 return shsurf;
3275}
3276
3277static void
3278xdg_get_xdg_surface(struct wl_client *client,
3279 struct wl_resource *resource,
3280 uint32_t id,
3281 struct wl_resource *surface_resource)
3282{
3283 struct weston_surface *surface =
3284 wl_resource_get_user_data(surface_resource);
3285 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3286 struct shell_surface *shsurf;
3287
3288 if (get_shell_surface(surface)) {
3289 wl_resource_post_error(surface_resource,
3290 WL_DISPLAY_ERROR_INVALID_OBJECT,
3291 "desktop_shell::get_shell_surface already requested");
3292 return;
3293 }
3294
3295 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3296 if (!shsurf) {
3297 wl_resource_post_error(surface_resource,
3298 WL_DISPLAY_ERROR_INVALID_OBJECT,
3299 "surface->configure already set");
3300 return;
3301 }
3302
3303 shsurf->resource =
3304 wl_resource_create(client,
3305 &xdg_surface_interface, 1, id);
3306 wl_resource_set_implementation(shsurf->resource,
3307 &xdg_surface_implementation,
3308 shsurf, shell_destroy_shell_surface);
3309}
3310
3311static bool
3312shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3313{
3314 return wl_resource_instance_of(shsurf->resource,
3315 &xdg_surface_interface,
3316 &xdg_surface_implementation);
3317}
3318
3319/* xdg-popup implementation */
3320
3321static void
3322xdg_popup_destroy(struct wl_client *client,
3323 struct wl_resource *resource)
3324{
3325 wl_resource_destroy(resource);
3326}
3327
3328static void
3329xdg_popup_pong(struct wl_client *client,
3330 struct wl_resource *resource,
3331 uint32_t serial)
3332{
3333 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3334
3335 surface_pong(shsurf, serial);
3336}
3337
3338static const struct xdg_popup_interface xdg_popup_implementation = {
3339 xdg_popup_destroy,
3340 xdg_popup_pong
3341};
3342
3343static void
3344xdg_popup_send_configure(struct weston_surface *surface,
3345 uint32_t edges, int32_t width, int32_t height)
3346{
3347}
3348
3349static const struct weston_shell_client xdg_popup_client = {
3350 xdg_popup_send_configure
3351};
3352
3353static struct shell_surface *
3354create_xdg_popup(void *shell, struct weston_surface *surface,
3355 const struct weston_shell_client *client,
3356 struct weston_surface *parent,
3357 struct shell_seat *seat,
3358 uint32_t serial,
3359 int32_t x, int32_t y)
3360{
3361 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003362
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003363 shsurf = create_common_surface(shell, surface, client);
3364 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003365 shsurf->popup.shseat = seat;
3366 shsurf->popup.serial = serial;
3367 shsurf->popup.x = x;
3368 shsurf->popup.y = y;
3369 shell_surface_set_parent(shsurf, parent);
3370
3371 return shsurf;
3372}
3373
3374static void
3375xdg_get_xdg_popup(struct wl_client *client,
3376 struct wl_resource *resource,
3377 uint32_t id,
3378 struct wl_resource *surface_resource,
3379 struct wl_resource *parent_resource,
3380 struct wl_resource *seat_resource,
3381 uint32_t serial,
3382 int32_t x, int32_t y, uint32_t flags)
3383{
3384 struct weston_surface *surface =
3385 wl_resource_get_user_data(surface_resource);
3386 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3387 struct shell_surface *shsurf;
3388 struct weston_surface *parent;
3389 struct shell_seat *seat;
3390
3391 if (get_shell_surface(surface)) {
3392 wl_resource_post_error(surface_resource,
3393 WL_DISPLAY_ERROR_INVALID_OBJECT,
3394 "desktop_shell::get_shell_surface already requested");
3395 return;
3396 }
3397
3398 if (!parent_resource) {
3399 wl_resource_post_error(surface_resource,
3400 WL_DISPLAY_ERROR_INVALID_OBJECT,
3401 "xdg_shell::get_xdg_popup requires a parent shell surface");
3402 }
3403
3404 parent = wl_resource_get_user_data(parent_resource);
3405 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3406
3407 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3408 parent, seat, serial, x, y);
3409 if (!shsurf) {
3410 wl_resource_post_error(surface_resource,
3411 WL_DISPLAY_ERROR_INVALID_OBJECT,
3412 "surface->configure already set");
3413 return;
3414 }
3415
3416 shsurf->resource =
3417 wl_resource_create(client,
3418 &xdg_popup_interface, 1, id);
3419 wl_resource_set_implementation(shsurf->resource,
3420 &xdg_popup_implementation,
3421 shsurf, shell_destroy_shell_surface);
3422}
3423
3424static bool
3425shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3426{
3427 return wl_resource_instance_of(shsurf->resource,
3428 &xdg_popup_interface,
3429 &xdg_popup_implementation);
3430}
3431
3432static const struct xdg_shell_interface xdg_implementation = {
3433 xdg_use_unstable_version,
3434 xdg_get_xdg_surface,
3435 xdg_get_xdg_popup
3436};
3437
3438static int
3439xdg_shell_unversioned_dispatch(const void *implementation,
3440 void *_target, uint32_t opcode,
3441 const struct wl_message *message,
3442 union wl_argument *args)
3443{
3444 struct wl_resource *resource = _target;
3445 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3446
3447 if (opcode != 0) {
3448 wl_resource_post_error(resource,
3449 WL_DISPLAY_ERROR_INVALID_OBJECT,
3450 "must call use_unstable_version first");
3451 return 0;
3452 }
3453
3454#define XDG_SERVER_VERSION 1
3455
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003456 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3457 "shell implementation doesn't match protocol version");
3458
Rafael Antognollie2a34552013-12-03 15:35:45 -02003459 if (args[0].i != XDG_SERVER_VERSION) {
3460 wl_resource_post_error(resource,
3461 WL_DISPLAY_ERROR_INVALID_OBJECT,
3462 "incompatible version, server is %d "
3463 "client wants %d",
3464 XDG_SERVER_VERSION, args[0].i);
3465 return 0;
3466 }
3467
3468 wl_resource_set_implementation(resource, &xdg_implementation,
3469 shell, NULL);
3470
3471 return 1;
3472}
3473
3474/* end of xdg-shell implementation */
3475/***********************************/
3476
Kristian Høgsberg07937562011-04-12 17:25:42 -04003477static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003478shell_fade(struct desktop_shell *shell, enum fade_type type);
3479
3480static int
3481screensaver_timeout(void *data)
3482{
3483 struct desktop_shell *shell = data;
3484
3485 shell_fade(shell, FADE_OUT);
3486
3487 return 1;
3488}
3489
3490static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003491handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003492{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003493 struct desktop_shell *shell =
3494 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003495
Pekka Paalanen18027e52011-12-02 16:31:49 +02003496 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003497
3498 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003499 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003500}
3501
3502static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003503launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003504{
3505 if (shell->screensaver.binding)
3506 return;
3507
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003508 if (!shell->screensaver.path) {
3509 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003510 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003511 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003512
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003513 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003514 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003515 return;
3516 }
3517
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003518 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003519 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003520 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003521 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003522}
3523
3524static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003525terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003526{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003527 if (shell->screensaver.process.pid == 0)
3528 return;
3529
3530 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003531}
3532
3533static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003534configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003535{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003536 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003537
Jason Ekstranda7af7042013-10-12 22:38:11 -05003538 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3539 if (v->output == ev->output && v != ev) {
3540 weston_view_unmap(v);
3541 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003542 }
3543 }
3544
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003545 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003546
Jason Ekstranda7af7042013-10-12 22:38:11 -05003547 if (wl_list_empty(&ev->layer_link)) {
3548 wl_list_insert(&layer->view_list, &ev->layer_link);
3549 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003550 }
3551}
3552
3553static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003554background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003555{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003556 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003557 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003558
Jason Ekstranda7af7042013-10-12 22:38:11 -05003559 view = container_of(es->views.next, struct weston_view, surface_link);
3560
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003561 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003562}
3563
3564static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003565desktop_shell_set_background(struct wl_client *client,
3566 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003567 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003568 struct wl_resource *surface_resource)
3569{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003570 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003571 struct weston_surface *surface =
3572 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003573 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003574
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003575 if (surface->configure) {
3576 wl_resource_post_error(surface_resource,
3577 WL_DISPLAY_ERROR_INVALID_OBJECT,
3578 "surface role already assigned");
3579 return;
3580 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003581
Jason Ekstranda7af7042013-10-12 22:38:11 -05003582 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3583 weston_view_destroy(view);
3584 view = weston_view_create(surface);
3585
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003586 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003587 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003588 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003589 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003590 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003591 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003592 surface->output->width,
3593 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003594}
3595
3596static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003597panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003598{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003599 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003600 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003601
Jason Ekstranda7af7042013-10-12 22:38:11 -05003602 view = container_of(es->views.next, struct weston_view, surface_link);
3603
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003604 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003605}
3606
3607static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003608desktop_shell_set_panel(struct wl_client *client,
3609 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003610 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003611 struct wl_resource *surface_resource)
3612{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003613 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003614 struct weston_surface *surface =
3615 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003616 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003617
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003618 if (surface->configure) {
3619 wl_resource_post_error(surface_resource,
3620 WL_DISPLAY_ERROR_INVALID_OBJECT,
3621 "surface role already assigned");
3622 return;
3623 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003624
Jason Ekstranda7af7042013-10-12 22:38:11 -05003625 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3626 weston_view_destroy(view);
3627 view = weston_view_create(surface);
3628
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003629 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003630 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003631 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003632 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003633 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003634 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003635 surface->output->width,
3636 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003637}
3638
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003639static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003640lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003641{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003642 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003643 struct weston_view *view;
3644
3645 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003646
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003647 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003648 return;
3649
Jason Ekstranda7af7042013-10-12 22:38:11 -05003650 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003651
3652 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003653 wl_list_insert(&shell->lock_layer.view_list,
3654 &view->layer_link);
3655 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003656 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003657 }
3658}
3659
3660static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003661handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003662{
Tiago Vignattibe143262012-04-16 17:31:41 +03003663 struct desktop_shell *shell =
3664 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003665
Martin Minarik6d118362012-06-07 18:01:59 +02003666 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003667 shell->lock_surface = NULL;
3668}
3669
3670static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003671desktop_shell_set_lock_surface(struct wl_client *client,
3672 struct wl_resource *resource,
3673 struct wl_resource *surface_resource)
3674{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003675 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003676 struct weston_surface *surface =
3677 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003678
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003679 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003680
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003681 if (!shell->locked)
3682 return;
3683
Pekka Paalanen98262232011-12-01 10:42:22 +02003684 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003685
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003686 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003687 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003688 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003689
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003690 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003691 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003692 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003693}
3694
3695static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003696resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003697{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003698 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003699
Pekka Paalanen77346a62011-11-30 16:26:35 +02003700 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003701
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003702 wl_list_remove(&shell->lock_layer.link);
3703 wl_list_insert(&shell->compositor->cursor_layer.link,
3704 &shell->fullscreen_layer.link);
3705 wl_list_insert(&shell->fullscreen_layer.link,
3706 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003707 if (shell->showing_input_panels) {
3708 wl_list_insert(&shell->panel_layer.link,
3709 &shell->input_panel_layer.link);
3710 wl_list_insert(&shell->input_panel_layer.link,
3711 &ws->layer.link);
3712 } else {
3713 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3714 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003715
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003716 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003717
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003718 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003719 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003720 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003721}
3722
3723static void
3724desktop_shell_unlock(struct wl_client *client,
3725 struct wl_resource *resource)
3726{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003727 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003728
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003729 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003730
3731 if (shell->locked)
3732 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003733}
3734
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003735static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003736desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003737 struct wl_resource *resource,
3738 struct wl_resource *surface_resource)
3739{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003740 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003741
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003742 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003743 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003744}
3745
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003746static void
3747desktop_shell_desktop_ready(struct wl_client *client,
3748 struct wl_resource *resource)
3749{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003750 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003751
3752 shell_fade_startup(shell);
3753}
3754
Kristian Høgsberg75840622011-09-06 13:48:16 -04003755static const struct desktop_shell_interface desktop_shell_implementation = {
3756 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003757 desktop_shell_set_panel,
3758 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003759 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003760 desktop_shell_set_grab_surface,
3761 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003762};
3763
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003764static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003765get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003766{
3767 struct shell_surface *shsurf;
3768
3769 shsurf = get_shell_surface(surface);
3770 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003771 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003772 return shsurf->type;
3773}
3774
Kristian Høgsberg75840622011-09-06 13:48:16 -04003775static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003776move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003777{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003778 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003779 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003780 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003781
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003782 if (seat->pointer->focus == NULL)
3783 return;
3784
3785 focus = seat->pointer->focus->surface;
3786
Pekka Paalanen01388e22013-04-25 13:57:44 +03003787 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003788 if (surface == NULL)
3789 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003790
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003791 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003792 if (shsurf == NULL || shsurf->state.fullscreen ||
3793 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003794 return;
3795
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003796 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003797}
3798
3799static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003800maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3801{
3802 struct weston_surface *focus = seat->pointer->focus->surface;
3803 struct weston_surface *surface;
3804 struct shell_surface *shsurf;
3805
3806 surface = weston_surface_get_main_surface(focus);
3807 if (surface == NULL)
3808 return;
3809
3810 shsurf = get_shell_surface(surface);
3811 if (shsurf == NULL)
3812 return;
3813
3814 if (!shell_surface_is_xdg_surface(shsurf))
3815 return;
3816
3817 if (shsurf->state.maximized)
3818 xdg_surface_send_request_unset_maximized(shsurf->resource);
3819 else
3820 xdg_surface_send_request_set_maximized(shsurf->resource);
3821}
3822
3823static void
3824fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3825{
3826 struct weston_surface *focus = seat->pointer->focus->surface;
3827 struct weston_surface *surface;
3828 struct shell_surface *shsurf;
3829
3830 surface = weston_surface_get_main_surface(focus);
3831 if (surface == NULL)
3832 return;
3833
3834 shsurf = get_shell_surface(surface);
3835 if (shsurf == NULL)
3836 return;
3837
3838 if (!shell_surface_is_xdg_surface(shsurf))
3839 return;
3840
3841 if (shsurf->state.fullscreen)
3842 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3843 else
3844 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3845}
3846
3847static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003848touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3849{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003850 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003851 struct weston_surface *surface;
3852 struct shell_surface *shsurf;
3853
3854 surface = weston_surface_get_main_surface(focus);
3855 if (surface == NULL)
3856 return;
3857
3858 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003859 if (shsurf == NULL || shsurf->state.fullscreen ||
3860 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003861 return;
3862
3863 surface_touch_move(shsurf, (struct weston_seat *) seat);
3864}
3865
3866static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003867resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003868{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003869 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003870 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003871 uint32_t edges = 0;
3872 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003873 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003874
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003875 if (seat->pointer->focus == NULL)
3876 return;
3877
3878 focus = seat->pointer->focus->surface;
3879
Pekka Paalanen01388e22013-04-25 13:57:44 +03003880 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003881 if (surface == NULL)
3882 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003883
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003884 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003885 if (shsurf == NULL || shsurf->state.fullscreen ||
3886 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003887 return;
3888
Jason Ekstranda7af7042013-10-12 22:38:11 -05003889 weston_view_from_global(shsurf->view,
3890 wl_fixed_to_int(seat->pointer->grab_x),
3891 wl_fixed_to_int(seat->pointer->grab_y),
3892 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003893
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003894 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003895 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003896 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003897 edges |= 0;
3898 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003899 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003900
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003901 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003902 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003903 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003904 edges |= 0;
3905 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003906 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003907
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003908 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003909}
3910
3911static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003912surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003913 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003914{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003915 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003916 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003917 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003918 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003919
Pekka Paalanen01388e22013-04-25 13:57:44 +03003920 /* XXX: broken for windows containing sub-surfaces */
3921 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003922 if (surface == NULL)
3923 return;
3924
3925 shsurf = get_shell_surface(surface);
3926 if (!shsurf)
3927 return;
3928
Jason Ekstranda7af7042013-10-12 22:38:11 -05003929 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003930
Jason Ekstranda7af7042013-10-12 22:38:11 -05003931 if (shsurf->view->alpha > 1.0)
3932 shsurf->view->alpha = 1.0;
3933 if (shsurf->view->alpha < step)
3934 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003935
Jason Ekstranda7af7042013-10-12 22:38:11 -05003936 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003937 weston_surface_damage(surface);
3938}
3939
3940static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003941do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003942 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003943{
Daniel Stone37816df2012-05-16 18:45:18 +01003944 struct weston_seat *ws = (struct weston_seat *) seat;
3945 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003946 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003947 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003948
3949 wl_list_for_each(output, &compositor->output_list, link) {
3950 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003951 wl_fixed_to_double(seat->pointer->x),
3952 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003953 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003954 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003955 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003956 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003957 increment = -output->zoom.increment;
3958 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003959 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003960 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003961 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003962 else
3963 increment = 0;
3964
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003965 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003966
Scott Moreaue6603982012-06-11 13:07:51 -06003967 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003968 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003969 else if (output->zoom.level > output->zoom.max_level)
3970 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003971 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003972 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003973 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003974
Scott Moreaue6603982012-06-11 13:07:51 -06003975 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003976
Jason Ekstranda7af7042013-10-12 22:38:11 -05003977 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003978 }
3979 }
3980}
3981
Scott Moreauccbf29d2012-02-22 14:21:41 -07003982static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003983zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003984 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003985{
3986 do_zoom(seat, time, 0, axis, value);
3987}
3988
3989static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003990zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003991 void *data)
3992{
3993 do_zoom(seat, time, key, 0, 0);
3994}
3995
3996static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003997terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003998 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003999{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004000 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004001
Daniel Stone325fc2d2012-05-30 16:31:58 +01004002 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004003}
4004
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004005static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004006rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4007 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004008{
4009 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004010 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004011 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004012 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004013 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004014
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004015 weston_pointer_move(pointer, x, y);
4016
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004017 if (!shsurf)
4018 return;
4019
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004020 cx = 0.5f * shsurf->surface->width;
4021 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004022
Daniel Stone37816df2012-05-16 18:45:18 +01004023 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4024 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004025 r = sqrtf(dx * dx + dy * dy);
4026
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004027 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004028 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004029
4030 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004031 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004032 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004033
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004034 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004035 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004036
4037 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004038 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004039 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004040 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004041 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004042
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004043 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004044 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004045 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004046 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004047 wl_list_init(&shsurf->rotation.transform.link);
4048 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004049 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004050 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004051
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004052 /* We need to adjust the position of the surface
4053 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004054 cposx = shsurf->view->geometry.x + cx;
4055 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004056 dposx = rotate->center.x - cposx;
4057 dposy = rotate->center.y - cposy;
4058 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004059 weston_view_set_position(shsurf->view,
4060 shsurf->view->geometry.x + dposx,
4061 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004062 }
4063
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004064 /* Repaint implies weston_surface_update_transform(), which
4065 * lazily applies the damage due to rotation update.
4066 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004067 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004068}
4069
4070static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004071rotate_grab_button(struct weston_pointer_grab *grab,
4072 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004073{
4074 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004075 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004076 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004077 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004078 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004079
Daniel Stone4dbadb12012-05-30 16:31:51 +01004080 if (pointer->button_count == 0 &&
4081 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004082 if (shsurf)
4083 weston_matrix_multiply(&shsurf->rotation.rotation,
4084 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004085 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004086 free(rotate);
4087 }
4088}
4089
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004090static void
4091rotate_grab_cancel(struct weston_pointer_grab *grab)
4092{
4093 struct rotate_grab *rotate =
4094 container_of(grab, struct rotate_grab, base.grab);
4095
4096 shell_grab_end(&rotate->base);
4097 free(rotate);
4098}
4099
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004100static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004101 noop_grab_focus,
4102 rotate_grab_motion,
4103 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004104 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004105};
4106
4107static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004108surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004109{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004110 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004111 float dx, dy;
4112 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004113
Pekka Paalanen460099f2012-01-20 16:48:25 +02004114 rotate = malloc(sizeof *rotate);
4115 if (!rotate)
4116 return;
4117
Jason Ekstranda7af7042013-10-12 22:38:11 -05004118 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004119 surface->surface->width * 0.5f,
4120 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004121 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004122
Daniel Stone37816df2012-05-16 18:45:18 +01004123 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4124 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004125 r = sqrtf(dx * dx + dy * dy);
4126 if (r > 20.0f) {
4127 struct weston_matrix inverse;
4128
4129 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004130 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004131 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004132
4133 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004134 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004135 } else {
4136 weston_matrix_init(&surface->rotation.rotation);
4137 weston_matrix_init(&rotate->rotation);
4138 }
4139
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004140 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4141 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004142}
4143
4144static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004145rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004146 void *data)
4147{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004148 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004149 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004150 struct shell_surface *surface;
4151
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004152 if (seat->pointer->focus == NULL)
4153 return;
4154
4155 focus = seat->pointer->focus->surface;
4156
Pekka Paalanen01388e22013-04-25 13:57:44 +03004157 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004158 if (base_surface == NULL)
4159 return;
4160
4161 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004162 if (surface == NULL || surface->state.fullscreen ||
4163 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004164 return;
4165
4166 surface_rotate(surface, seat);
4167}
4168
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004169/* Move all fullscreen layers down to the current workspace in a non-reversible
4170 * manner. This should be used when implementing shell-wide overlays, such as
4171 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004172void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004173lower_fullscreen_layer(struct desktop_shell *shell)
4174{
4175 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004176 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004177
4178 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004179 wl_list_for_each_reverse_safe(view, prev,
4180 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004181 layer_link) {
4182 wl_list_remove(&view->layer_link);
4183 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4184 weston_view_damage_below(view);
4185 weston_surface_damage(view->surface);
4186 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004187}
4188
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004189void
Tiago Vignattibe143262012-04-16 17:31:41 +03004190activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004191 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004192{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004193 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004194 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004195 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004196 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004197 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004198
Pekka Paalanen01388e22013-04-25 13:57:44 +03004199 main_surface = weston_surface_get_main_surface(es);
4200
Daniel Stone37816df2012-05-16 18:45:18 +01004201 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004202
Jonas Ådahl8538b222012-08-29 22:13:03 +02004203 state = ensure_focus_state(shell, seat);
4204 if (state == NULL)
4205 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004206
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004207 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004208 state->keyboard_focus = es;
4209 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004210 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004211
Rafael Antognolli03b16592013-12-03 15:35:42 -02004212 shsurf = get_shell_surface(main_surface);
4213 if (shsurf->state.fullscreen)
4214 shell_configure_fullscreen(shsurf);
4215 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004216 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004217
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004218 if (shell->focus_animation_type != ANIMATION_NONE) {
4219 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004220 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004221 }
4222
4223 /* Update the surface’s layer. This brings it to the top of the stacking
4224 * order as appropriate. */
4225 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004226}
4227
Alex Wu21858432012-04-01 20:13:08 +08004228/* no-op func for checking black surface */
4229static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004230black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004231{
4232}
4233
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004234static bool
Alex Wu21858432012-04-01 20:13:08 +08004235is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4236{
4237 if (es->configure == black_surface_configure) {
4238 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004239 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004240 return true;
4241 }
4242 return false;
4243}
4244
Kristian Høgsberg75840622011-09-06 13:48:16 -04004245static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004246activate_binding(struct weston_seat *seat,
4247 struct desktop_shell *shell,
4248 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004249{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004250 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004251
Alex Wu9c35e6b2012-03-05 14:13:13 +08004252 if (!focus)
4253 return;
4254
Pekka Paalanen01388e22013-04-25 13:57:44 +03004255 if (is_black_surface(focus, &main_surface))
4256 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004257
Pekka Paalanen01388e22013-04-25 13:57:44 +03004258 main_surface = weston_surface_get_main_surface(focus);
4259 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004260 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004261
Neil Robertsa28c6932013-10-03 16:43:04 +01004262 activate(shell, focus, seat);
4263}
4264
4265static void
4266click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4267 void *data)
4268{
4269 if (seat->pointer->grab != &seat->pointer->default_grab)
4270 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004271 if (seat->pointer->focus == NULL)
4272 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004273
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004274 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004275}
4276
4277static void
4278touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4279{
4280 if (seat->touch->grab != &seat->touch->default_grab)
4281 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004282 if (seat->touch->focus == NULL)
4283 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004284
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004285 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004286}
4287
4288static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004289lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004290{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004291 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004292
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004293 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004294 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004295 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004296 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004297
4298 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004299
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004300 /* Hide all surfaces by removing the fullscreen, panel and
4301 * toplevel layers. This way nothing else can show or receive
4302 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004303
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004304 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004305 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004306 if (shell->showing_input_panels)
4307 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004308 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004309 wl_list_insert(&shell->compositor->cursor_layer.link,
4310 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004311
Pekka Paalanen77346a62011-11-30 16:26:35 +02004312 launch_screensaver(shell);
4313
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004314 /* TODO: disable bindings that should not work while locked. */
4315
4316 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004317}
4318
4319static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004320unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004321{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004322 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004323 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004324 return;
4325 }
4326
4327 /* If desktop-shell client has gone away, unlock immediately. */
4328 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004329 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004330 return;
4331 }
4332
4333 if (shell->prepare_event_sent)
4334 return;
4335
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004336 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004337 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004338}
4339
4340static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004341shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004342{
4343 struct desktop_shell *shell = data;
4344
4345 shell->fade.animation = NULL;
4346
4347 switch (shell->fade.type) {
4348 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004349 weston_surface_destroy(shell->fade.view->surface);
4350 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004351 break;
4352 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004353 lock(shell);
4354 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004355 default:
4356 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004357 }
4358}
4359
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004361shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004362{
4363 struct weston_compositor *compositor = shell->compositor;
4364 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004365 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004366
4367 surface = weston_surface_create(compositor);
4368 if (!surface)
4369 return NULL;
4370
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 view = weston_view_create(surface);
4372 if (!view) {
4373 weston_surface_destroy(surface);
4374 return NULL;
4375 }
4376
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004377 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004378 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004379 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004380 wl_list_insert(&compositor->fade_layer.view_list,
4381 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004382 pixman_region32_init(&surface->input);
4383
Jason Ekstranda7af7042013-10-12 22:38:11 -05004384 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004385}
4386
4387static void
4388shell_fade(struct desktop_shell *shell, enum fade_type type)
4389{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004390 float tint;
4391
4392 switch (type) {
4393 case FADE_IN:
4394 tint = 0.0;
4395 break;
4396 case FADE_OUT:
4397 tint = 1.0;
4398 break;
4399 default:
4400 weston_log("shell: invalid fade type\n");
4401 return;
4402 }
4403
4404 shell->fade.type = type;
4405
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 if (shell->fade.view == NULL) {
4407 shell->fade.view = shell_fade_create_surface(shell);
4408 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004409 return;
4410
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 shell->fade.view->alpha = 1.0 - tint;
4412 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004413 }
4414
4415 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004416 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004417 else
4418 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004419 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004420 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004421 shell_fade_done, shell);
4422}
4423
4424static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004425do_shell_fade_startup(void *data)
4426{
4427 struct desktop_shell *shell = data;
4428
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004429 if (shell->startup_animation_type == ANIMATION_FADE)
4430 shell_fade(shell, FADE_IN);
4431 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004432 weston_surface_destroy(shell->fade.view->surface);
4433 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004434 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004435}
4436
4437static void
4438shell_fade_startup(struct desktop_shell *shell)
4439{
4440 struct wl_event_loop *loop;
4441
4442 if (!shell->fade.startup_timer)
4443 return;
4444
4445 wl_event_source_remove(shell->fade.startup_timer);
4446 shell->fade.startup_timer = NULL;
4447
4448 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4449 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4450}
4451
4452static int
4453fade_startup_timeout(void *data)
4454{
4455 struct desktop_shell *shell = data;
4456
4457 shell_fade_startup(shell);
4458 return 0;
4459}
4460
4461static void
4462shell_fade_init(struct desktop_shell *shell)
4463{
4464 /* Make compositor output all black, and wait for the desktop-shell
4465 * client to signal it is ready, then fade in. The timer triggers a
4466 * fade-in, in case the desktop-shell client takes too long.
4467 */
4468
4469 struct wl_event_loop *loop;
4470
Jason Ekstranda7af7042013-10-12 22:38:11 -05004471 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004472 weston_log("%s: warning: fade surface already exists\n",
4473 __func__);
4474 return;
4475 }
4476
Jason Ekstranda7af7042013-10-12 22:38:11 -05004477 shell->fade.view = shell_fade_create_surface(shell);
4478 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004479 return;
4480
Jason Ekstranda7af7042013-10-12 22:38:11 -05004481 weston_view_update_transform(shell->fade.view);
4482 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004483
4484 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4485 shell->fade.startup_timer =
4486 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4487 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4488}
4489
4490static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004491idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004492{
4493 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004494 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004495
4496 shell_fade(shell, FADE_OUT);
4497 /* lock() is called from shell_fade_done() */
4498}
4499
4500static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004501wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004502{
4503 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004504 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004505
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004506 unlock(shell);
4507}
4508
4509static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004510center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004511{
Giulio Camuffob8366642013-04-25 13:57:46 +03004512 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004513 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004514
Jason Ekstranda7af7042013-10-12 22:38:11 -05004515 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004516
4517 x = output->x + (output->width - width) / 2 - surf_x / 2;
4518 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004519
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004520 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004521}
4522
4523static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004524weston_view_set_initial_position(struct weston_view *view,
4525 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004526{
4527 struct weston_compositor *compositor = shell->compositor;
4528 int ix = 0, iy = 0;
4529 int range_x, range_y;
4530 int dx, dy, x, y, panel_height;
4531 struct weston_output *output, *target_output = NULL;
4532 struct weston_seat *seat;
4533
4534 /* As a heuristic place the new window on the same output as the
4535 * pointer. Falling back to the output containing 0, 0.
4536 *
4537 * TODO: Do something clever for touch too?
4538 */
4539 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004540 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004541 ix = wl_fixed_to_int(seat->pointer->x);
4542 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004543 break;
4544 }
4545 }
4546
4547 wl_list_for_each(output, &compositor->output_list, link) {
4548 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4549 target_output = output;
4550 break;
4551 }
4552 }
4553
4554 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004555 weston_view_set_position(view, 10 + random() % 400,
4556 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004557 return;
4558 }
4559
4560 /* Valid range within output where the surface will still be onscreen.
4561 * If this is negative it means that the surface is bigger than
4562 * output.
4563 */
4564 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004565 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004566 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004567 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004568
Rob Bradford4cb88c72012-08-13 15:18:44 +01004569 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004570 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004571 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004572 dx = 0;
4573
4574 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004575 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004576 else
4577 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004578
4579 x = target_output->x + dx;
4580 y = target_output->y + dy;
4581
Jason Ekstranda7af7042013-10-12 22:38:11 -05004582 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004583}
4584
4585static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004586map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004587 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004588{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004589 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004590 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004591 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004592 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004593
Pekka Paalanen77346a62011-11-30 16:26:35 +02004594 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004595 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004596 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004597 if (shsurf->state.fullscreen) {
4598 center_on_output(shsurf->view, shsurf->fullscreen_output);
4599 shell_map_fullscreen(shsurf);
4600 } else if (shsurf->state.maximized) {
4601 /* use surface configure to set the geometry */
4602 panel_height = get_output_panel_height(shell, shsurf->output);
4603 surface_subsurfaces_boundingbox(shsurf->surface,
4604 &surf_x, &surf_y, NULL, NULL);
4605 weston_view_set_position(shsurf->view,
4606 shsurf->output->x - surf_x,
4607 shsurf->output->y +
4608 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004609 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004610 weston_view_set_initial_position(shsurf->view, shell);
4611 }
Juan Zhao96879df2012-02-07 08:45:41 +08004612 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004613 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004614 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004615 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004616 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004617 weston_view_set_position(shsurf->view,
4618 shsurf->view->geometry.x + sx,
4619 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004620 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004621 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004622 default:
4623 ;
4624 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004625
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004626 /* Surface stacking order, see also activate(). */
4627 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004628
Jason Ekstranda7af7042013-10-12 22:38:11 -05004629 if (shsurf->type != SHELL_SURFACE_NONE) {
4630 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004631 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004632 shsurf->surface->output = shsurf->output;
4633 shsurf->view->output = shsurf->output;
4634 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004635 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004636
Rafael Antognollied207b42013-12-03 15:35:43 -02004637 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4638 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4639 }
4640
Jason Ekstranda7af7042013-10-12 22:38:11 -05004641 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004642 /* XXX: xwayland's using the same fields for transient type */
4643 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004644 if (shsurf->transient.flags ==
4645 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4646 break;
4647 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004648 if (shsurf->state.relative &&
4649 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4650 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004651 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004652 break;
4653 wl_list_for_each(seat, &compositor->seat_list, link)
4654 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004655 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004656 case SHELL_SURFACE_POPUP:
4657 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004658 default:
4659 break;
4660 }
4661
Rafael Antognolli03b16592013-12-03 15:35:42 -02004662 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4663 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004664 {
4665 switch (shell->win_animation_type) {
4666 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004667 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004668 break;
4669 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004670 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004671 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004672 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004673 default:
4674 break;
4675 }
4676 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004677}
4678
4679static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004680configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004681 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004682{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004683 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004684 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004685 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004686
Pekka Paalanen77346a62011-11-30 16:26:35 +02004687 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004688
Rafael Antognolli03b16592013-12-03 15:35:42 -02004689 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004690 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004691 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004692 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004693 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4694 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004695 mx = shsurf->output->x - surf_x;
4696 my = shsurf->output->y +
4697 get_output_panel_height(shell,shsurf->output) - surf_y;
4698 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004699 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004700 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004701 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004702
Alex Wu4539b082012-03-01 12:57:46 +08004703 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004704 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004705 wl_list_for_each(view, &surface->views, surface_link)
4706 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004707
Rafael Antognolli03b16592013-12-03 15:35:42 -02004708 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004709 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004710 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004711}
4712
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004713static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004714shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004715{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004716 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004717 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004718
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004719 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004720
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004721 if (!weston_surface_is_mapped(es) &&
4722 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004723 remove_popup_grab(shsurf);
4724 }
4725
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004726 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004727 return;
4728
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004729 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004730 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004731 type_changed = 1;
4732 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004733
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004734 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004735 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004736 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004737 shsurf->last_width != es->width ||
4738 shsurf->last_height != es->height) {
4739 shsurf->last_width = es->width;
4740 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004741 float from_x, from_y;
4742 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004743
Jason Ekstranda7af7042013-10-12 22:38:11 -05004744 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4745 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004746 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004747 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004748 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004749 }
4750}
4751
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004752static void
4753shell_surface_output_destroyed(struct weston_surface *es)
4754{
4755 struct shell_surface *shsurf = get_shell_surface(es);
4756
4757 shsurf->saved_position_valid = false;
Ander Conselvan de Oliveirafc63fdd2013-12-13 22:10:58 +02004758 shsurf->next_state.maximized = false;
4759 shsurf->next_state.fullscreen = false;
4760 shsurf->state_changed = true;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004761}
4762
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004763static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004764
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004765static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004766desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004767{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004768 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004769 struct desktop_shell *shell =
4770 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004771
4772 shell->child.process.pid = 0;
4773 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004774
4775 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4776 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004777 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004778 shell->child.deathstamp = time;
4779 shell->child.deathcount = 0;
4780 }
4781
4782 shell->child.deathcount++;
4783 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004784 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004785 return;
4786 }
4787
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004788 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004789 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004790 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004791}
4792
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004793static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004794desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4795{
4796 struct desktop_shell *shell;
4797
4798 shell = container_of(listener, struct desktop_shell,
4799 child.client_destroy_listener);
4800
4801 shell->child.client = NULL;
4802}
4803
4804static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004805launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004806{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004807 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004808
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004809 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004810 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004811 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004812 desktop_shell_sigchld);
4813
4814 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004815 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004816
4817 shell->child.client_destroy_listener.notify =
4818 desktop_shell_client_destroy;
4819 wl_client_add_destroy_listener(shell->child.client,
4820 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004821}
4822
4823static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004824bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4825{
Tiago Vignattibe143262012-04-16 17:31:41 +03004826 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004827 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004828
Jason Ekstranda85118c2013-06-27 20:17:02 -05004829 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4830 if (resource)
4831 wl_resource_set_implementation(resource, &shell_implementation,
4832 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004833}
4834
Kristian Høgsberg75840622011-09-06 13:48:16 -04004835static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004836bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4837{
4838 struct desktop_shell *shell = data;
4839 struct wl_resource *resource;
4840
4841 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
4842 if (resource)
4843 wl_resource_set_dispatcher(resource,
4844 xdg_shell_unversioned_dispatch,
4845 NULL, shell, NULL);
4846}
4847
4848static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004849unbind_desktop_shell(struct wl_resource *resource)
4850{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004851 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004852
4853 if (shell->locked)
4854 resume_desktop(shell);
4855
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004856 shell->child.desktop_shell = NULL;
4857 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004858}
4859
4860static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004861bind_desktop_shell(struct wl_client *client,
4862 void *data, uint32_t version, uint32_t id)
4863{
Tiago Vignattibe143262012-04-16 17:31:41 +03004864 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004865 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004866
Jason Ekstranda85118c2013-06-27 20:17:02 -05004867 resource = wl_resource_create(client, &desktop_shell_interface,
4868 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004869
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004870 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004871 wl_resource_set_implementation(resource,
4872 &desktop_shell_implementation,
4873 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004874 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004875
4876 if (version < 2)
4877 shell_fade_startup(shell);
4878
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004879 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004880 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004881
4882 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4883 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004884 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004885}
4886
Pekka Paalanen6e168112011-11-24 11:34:05 +02004887static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004888screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004889{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004890 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004891 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004892
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004893 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004894 return;
4895
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004896 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004897 if (!shell->locked)
4898 return;
4899
Jason Ekstranda7af7042013-10-12 22:38:11 -05004900 view = container_of(surface->views.next, struct weston_view, surface_link);
4901 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004902
Jason Ekstranda7af7042013-10-12 22:38:11 -05004903 if (wl_list_empty(&view->layer_link)) {
4904 wl_list_insert(shell->lock_layer.view_list.prev,
4905 &view->layer_link);
4906 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004907 wl_event_source_timer_update(shell->screensaver.timer,
4908 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004909 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004910 }
4911}
4912
4913static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004914screensaver_set_surface(struct wl_client *client,
4915 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004916 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004917 struct wl_resource *output_resource)
4918{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004919 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004920 struct weston_surface *surface =
4921 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004922 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004923 struct weston_view *view, *next;
4924
4925 /* Make sure we only have one view */
4926 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4927 weston_view_destroy(view);
4928 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004929
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004930 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004931 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004932 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004933}
4934
4935static const struct screensaver_interface screensaver_implementation = {
4936 screensaver_set_surface
4937};
4938
4939static void
4940unbind_screensaver(struct wl_resource *resource)
4941{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004942 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004943
Pekka Paalanen77346a62011-11-30 16:26:35 +02004944 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004945}
4946
4947static void
4948bind_screensaver(struct wl_client *client,
4949 void *data, uint32_t version, uint32_t id)
4950{
Tiago Vignattibe143262012-04-16 17:31:41 +03004951 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004952 struct wl_resource *resource;
4953
Jason Ekstranda85118c2013-06-27 20:17:02 -05004954 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004955
Pekka Paalanen77346a62011-11-30 16:26:35 +02004956 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004957 wl_resource_set_implementation(resource,
4958 &screensaver_implementation,
4959 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004960 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004961 return;
4962 }
4963
4964 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4965 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004966 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004967}
4968
Kristian Høgsberg07045392012-02-19 18:52:44 -05004969struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004970 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004971 struct weston_surface *current;
4972 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004973 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004974};
4975
4976static void
4977switcher_next(struct switcher *switcher)
4978{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004979 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004980 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004981 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004982 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004983
Jason Ekstranda7af7042013-10-12 22:38:11 -05004984 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004985 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02004986 if (shsurf &&
4987 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4988 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004989 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004990 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004991 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004992 next = view->surface;
4993 prev = view->surface;
4994 view->alpha = 0.25;
4995 weston_view_geometry_dirty(view);
4996 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004997 }
Alex Wu1659daa2012-04-01 20:13:09 +08004998
Jason Ekstranda7af7042013-10-12 22:38:11 -05004999 if (is_black_surface(view->surface, NULL)) {
5000 view->alpha = 0.25;
5001 weston_view_geometry_dirty(view);
5002 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005003 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005004 }
5005
5006 if (next == NULL)
5007 next = first;
5008
Alex Wu07b26062012-03-12 16:06:01 +08005009 if (next == NULL)
5010 return;
5011
Kristian Høgsberg07045392012-02-19 18:52:44 -05005012 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005013 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005014
5015 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005016 wl_list_for_each(view, &next->views, surface_link)
5017 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005018
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005019 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005020 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005021 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005022}
5023
5024static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005025switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005026{
5027 struct switcher *switcher =
5028 container_of(listener, struct switcher, listener);
5029
5030 switcher_next(switcher);
5031}
5032
5033static void
Daniel Stone351eb612012-05-31 15:27:47 -04005034switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005035{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005036 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005037 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005038 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005039
Jason Ekstranda7af7042013-10-12 22:38:11 -05005040 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005041 if (is_focus_view(view))
5042 continue;
5043
Jason Ekstranda7af7042013-10-12 22:38:11 -05005044 view->alpha = 1.0;
5045 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005046 }
5047
Alex Wu07b26062012-03-12 16:06:01 +08005048 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005049 activate(switcher->shell, switcher->current,
5050 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005051 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005052 weston_keyboard_end_grab(keyboard);
5053 if (keyboard->input_method_resource)
5054 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005055 free(switcher);
5056}
5057
5058static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005059switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005060 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005061{
5062 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005063 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005064
Daniel Stonec9785ea2012-05-30 16:31:52 +01005065 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005066 switcher_next(switcher);
5067}
5068
5069static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005070switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005071 uint32_t mods_depressed, uint32_t mods_latched,
5072 uint32_t mods_locked, uint32_t group)
5073{
5074 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005075 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005076
Daniel Stone351eb612012-05-31 15:27:47 -04005077 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5078 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005079}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005080
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005081static void
5082switcher_cancel(struct weston_keyboard_grab *grab)
5083{
5084 struct switcher *switcher = container_of(grab, struct switcher, grab);
5085
5086 switcher_destroy(switcher);
5087}
5088
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005089static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005090 switcher_key,
5091 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005092 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005093};
5094
5095static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005096switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005097 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005098{
Tiago Vignattibe143262012-04-16 17:31:41 +03005099 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005100 struct switcher *switcher;
5101
5102 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005103 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005104 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005105 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005106 wl_list_init(&switcher->listener.link);
5107
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005108 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005109 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005110 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005111 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5112 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005113 switcher_next(switcher);
5114}
5115
Pekka Paalanen3c647232011-12-22 13:43:43 +02005116static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005117backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005118 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005119{
5120 struct weston_compositor *compositor = data;
5121 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005122 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005123
5124 /* TODO: we're limiting to simple use cases, where we assume just
5125 * control on the primary display. We'd have to extend later if we
5126 * ever get support for setting backlights on random desktop LCD
5127 * panels though */
5128 output = get_default_output(compositor);
5129 if (!output)
5130 return;
5131
5132 if (!output->set_backlight)
5133 return;
5134
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005135 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5136 backlight_new = output->backlight_current - 25;
5137 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5138 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005139
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005140 if (backlight_new < 5)
5141 backlight_new = 5;
5142 if (backlight_new > 255)
5143 backlight_new = 255;
5144
5145 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005146 output->set_backlight(output, output->backlight_current);
5147}
5148
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005149struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005150 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005151 struct weston_seat *seat;
5152 uint32_t key[2];
5153 int key_released[2];
5154};
5155
5156static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005157debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005158 uint32_t key, uint32_t state)
5159{
5160 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005161 struct weston_compositor *ec = db->seat->compositor;
5162 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005163 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005164 uint32_t serial;
5165 int send = 0, terminate = 0;
5166 int check_binding = 1;
5167 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005168 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005169
5170 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5171 /* Do not run bindings on key releases */
5172 check_binding = 0;
5173
5174 for (i = 0; i < 2; i++)
5175 if (key == db->key[i])
5176 db->key_released[i] = 1;
5177
5178 if (db->key_released[0] && db->key_released[1]) {
5179 /* All key releases been swalled so end the grab */
5180 terminate = 1;
5181 } else if (key != db->key[0] && key != db->key[1]) {
5182 /* Should not swallow release of other keys */
5183 send = 1;
5184 }
5185 } else if (key == db->key[0] && !db->key_released[0]) {
5186 /* Do not check bindings for the first press of the binding
5187 * key. This allows it to be used as a debug shortcut.
5188 * We still need to swallow this event. */
5189 check_binding = 0;
5190 } else if (db->key[1]) {
5191 /* If we already ran a binding don't process another one since
5192 * we can't keep track of all the binding keys that were
5193 * pressed in order to swallow the release events. */
5194 send = 1;
5195 check_binding = 0;
5196 }
5197
5198 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005199 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5200 key, state)) {
5201 /* We ran a binding so swallow the press and keep the
5202 * grab to swallow the released too. */
5203 send = 0;
5204 terminate = 0;
5205 db->key[1] = key;
5206 } else {
5207 /* Terminate the grab since the key pressed is not a
5208 * debug binding key. */
5209 send = 1;
5210 terminate = 1;
5211 }
5212 }
5213
5214 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005215 serial = wl_display_next_serial(display);
5216 resource_list = &grab->keyboard->focus_resource_list;
5217 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005218 wl_keyboard_send_key(resource, serial, time, key, state);
5219 }
5220 }
5221
5222 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005223 weston_keyboard_end_grab(grab->keyboard);
5224 if (grab->keyboard->input_method_resource)
5225 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005226 free(db);
5227 }
5228}
5229
5230static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005231debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005232 uint32_t mods_depressed, uint32_t mods_latched,
5233 uint32_t mods_locked, uint32_t group)
5234{
5235 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005236 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005237
Neil Roberts96d790e2013-09-19 17:32:00 +01005238 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005239
Neil Roberts96d790e2013-09-19 17:32:00 +01005240 wl_resource_for_each(resource, resource_list) {
5241 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5242 mods_latched, mods_locked, group);
5243 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005244}
5245
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005246static void
5247debug_binding_cancel(struct weston_keyboard_grab *grab)
5248{
5249 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5250
5251 weston_keyboard_end_grab(grab->keyboard);
5252 free(db);
5253}
5254
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005255struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005256 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005257 debug_binding_modifiers,
5258 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005259};
5260
5261static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005262debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005263{
5264 struct debug_binding_grab *grab;
5265
5266 grab = calloc(1, sizeof *grab);
5267 if (!grab)
5268 return;
5269
5270 grab->seat = (struct weston_seat *) seat;
5271 grab->key[0] = key;
5272 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005273 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005274}
5275
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005276static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005277force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005278 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005279{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005280 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005281 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005282 struct desktop_shell *shell = data;
5283 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005284 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005285
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005286 focus_surface = seat->keyboard->focus;
5287 if (!focus_surface)
5288 return;
5289
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005290 wl_signal_emit(&compositor->kill_signal, focus_surface);
5291
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005292 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005293 wl_client_get_credentials(client, &pid, NULL, NULL);
5294
5295 /* Skip clients that we launched ourselves (the credentials of
5296 * the socketpair is ours) */
5297 if (pid == getpid())
5298 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005299
Daniel Stone325fc2d2012-05-30 16:31:58 +01005300 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005301}
5302
5303static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005304workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005305 uint32_t key, void *data)
5306{
5307 struct desktop_shell *shell = data;
5308 unsigned int new_index = shell->workspaces.current;
5309
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005310 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005311 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005312 if (new_index != 0)
5313 new_index--;
5314
5315 change_workspace(shell, new_index);
5316}
5317
5318static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005319workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005320 uint32_t key, void *data)
5321{
5322 struct desktop_shell *shell = data;
5323 unsigned int new_index = shell->workspaces.current;
5324
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005325 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005326 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005327 if (new_index < shell->workspaces.num - 1)
5328 new_index++;
5329
5330 change_workspace(shell, new_index);
5331}
5332
5333static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005334workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005335 uint32_t key, void *data)
5336{
5337 struct desktop_shell *shell = data;
5338 unsigned int new_index;
5339
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005340 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005341 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005342 new_index = key - KEY_F1;
5343 if (new_index >= shell->workspaces.num)
5344 new_index = shell->workspaces.num - 1;
5345
5346 change_workspace(shell, new_index);
5347}
5348
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005349static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005350workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005351 uint32_t key, void *data)
5352{
5353 struct desktop_shell *shell = data;
5354 unsigned int new_index = shell->workspaces.current;
5355
5356 if (shell->locked)
5357 return;
5358
5359 if (new_index != 0)
5360 new_index--;
5361
5362 take_surface_to_workspace_by_seat(shell, seat, new_index);
5363}
5364
5365static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005366workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005367 uint32_t key, void *data)
5368{
5369 struct desktop_shell *shell = data;
5370 unsigned int new_index = shell->workspaces.current;
5371
5372 if (shell->locked)
5373 return;
5374
5375 if (new_index < shell->workspaces.num - 1)
5376 new_index++;
5377
5378 take_surface_to_workspace_by_seat(shell, seat, new_index);
5379}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005380
5381static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005382handle_output_destroy(struct wl_listener *listener, void *data)
5383{
5384 struct shell_output *output_listener =
5385 container_of(listener, struct shell_output, destroy_listener);
5386
5387 wl_list_remove(&output_listener->destroy_listener.link);
5388 wl_list_remove(&output_listener->link);
5389 free(output_listener);
5390}
5391
5392static void
5393create_shell_output(struct desktop_shell *shell,
5394 struct weston_output *output)
5395{
5396 struct shell_output *shell_output;
5397
5398 shell_output = zalloc(sizeof *shell_output);
5399 if (shell_output == NULL)
5400 return;
5401
5402 shell_output->output = output;
5403 shell_output->shell = shell;
5404 shell_output->destroy_listener.notify = handle_output_destroy;
5405 wl_signal_add(&output->destroy_signal,
5406 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005407 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005408}
5409
5410static void
5411handle_output_create(struct wl_listener *listener, void *data)
5412{
5413 struct desktop_shell *shell =
5414 container_of(listener, struct desktop_shell, output_create_listener);
5415 struct weston_output *output = (struct weston_output *)data;
5416
5417 create_shell_output(shell, output);
5418}
5419
5420static void
5421setup_output_destroy_handler(struct weston_compositor *ec,
5422 struct desktop_shell *shell)
5423{
5424 struct weston_output *output;
5425
5426 wl_list_init(&shell->output_list);
5427 wl_list_for_each(output, &ec->output_list, link)
5428 create_shell_output(shell, output);
5429
5430 shell->output_create_listener.notify = handle_output_create;
5431 wl_signal_add(&ec->output_created_signal,
5432 &shell->output_create_listener);
5433}
5434
5435static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005436shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005437{
Tiago Vignattibe143262012-04-16 17:31:41 +03005438 struct desktop_shell *shell =
5439 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005440 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005441 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005442
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005443 /* Force state to unlocked so we don't try to fade */
5444 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005445 if (shell->child.client)
5446 wl_client_destroy(shell->child.client);
5447
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005448 wl_list_remove(&shell->idle_listener.link);
5449 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005450
5451 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005452
Xiong Zhang6b481422013-10-23 13:58:32 +08005453 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5454 wl_list_remove(&shell_output->destroy_listener.link);
5455 wl_list_remove(&shell_output->link);
5456 free(shell_output);
5457 }
5458
5459 wl_list_remove(&shell->output_create_listener.link);
5460
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005461 wl_array_for_each(ws, &shell->workspaces.array)
5462 workspace_destroy(*ws);
5463 wl_array_release(&shell->workspaces.array);
5464
Pekka Paalanen3c647232011-12-22 13:43:43 +02005465 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005466 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005467 free(shell);
5468}
5469
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005470static void
5471shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5472{
5473 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005474 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005475
5476 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005477 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5478 MODIFIER_CTRL | MODIFIER_ALT,
5479 terminate_binding, ec);
5480 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5481 click_to_activate_binding,
5482 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005483 weston_compositor_add_touch_binding(ec, 0,
5484 touch_to_activate_binding,
5485 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005486 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5487 MODIFIER_SUPER | MODIFIER_ALT,
5488 surface_opacity_binding, NULL);
5489 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5490 MODIFIER_SUPER, zoom_axis_binding,
5491 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005492
5493 /* configurable bindings */
5494 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005495 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5496 zoom_key_binding, NULL);
5497 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5498 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005499 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5500 maximize_binding, NULL);
5501 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5502 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005503 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5504 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005505 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005506 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5507 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005508
5509 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5510 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5511 rotate_binding, NULL);
5512
Daniel Stone325fc2d2012-05-30 16:31:58 +01005513 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5514 shell);
5515 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5516 ec);
5517 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5518 backlight_binding, ec);
5519 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5520 ec);
5521 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5522 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005523 weston_compositor_add_key_binding(ec, KEY_K, mod,
5524 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005525 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5526 workspace_up_binding, shell);
5527 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5528 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005529 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5530 workspace_move_surface_up_binding,
5531 shell);
5532 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5533 workspace_move_surface_down_binding,
5534 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005535
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005536 if (shell->exposay_modifier)
5537 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5538 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005539
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005540 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5541 if (shell->workspaces.num > 1) {
5542 num_workspace_bindings = shell->workspaces.num;
5543 if (num_workspace_bindings > 6)
5544 num_workspace_bindings = 6;
5545 for (i = 0; i < num_workspace_bindings; i++)
5546 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5547 workspace_f_binding,
5548 shell);
5549 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005550
5551 /* Debug bindings */
5552 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5553 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005554}
5555
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005556WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005557module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005558 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005559{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005560 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005561 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005562 struct workspace **pws;
5563 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005564 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005565
Peter Huttererf3d62272013-08-08 11:57:05 +10005566 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005567 if (shell == NULL)
5568 return -1;
5569
Kristian Høgsberg75840622011-09-06 13:48:16 -04005570 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005571
5572 shell->destroy_listener.notify = shell_destroy;
5573 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005574 shell->idle_listener.notify = idle_handler;
5575 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5576 shell->wake_listener.notify = wake_handler;
5577 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005578
Scott Moreauff1db4a2012-04-17 19:06:18 -06005579 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005580 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005581 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005582 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005583 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005584 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005585 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005586 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005587 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005588 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005589 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005590
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005591 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5592 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005593 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5594 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005595 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005596
5597 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005598 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005599
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005600 if (input_panel_setup(shell) < 0)
5601 return -1;
5602
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005603 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005604
Daniel Stonedf8133b2013-11-19 11:37:14 +01005605 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5606 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5607
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005608 for (i = 0; i < shell->workspaces.num; i++) {
5609 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5610 if (pws == NULL)
5611 return -1;
5612
5613 *pws = workspace_create();
5614 if (*pws == NULL)
5615 return -1;
5616 }
5617 activate_workspace(shell, 0);
5618
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005619 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005620 wl_list_init(&shell->workspaces.animation.link);
5621 shell->workspaces.animation.frame = animate_workspace_change_frame;
5622
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005623 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005624 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005625 return -1;
5626
Rafael Antognollie2a34552013-12-03 15:35:45 -02005627 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5628 shell, bind_xdg_shell) == NULL)
5629 return -1;
5630
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005631 if (wl_global_create(ec->wl_display,
5632 &desktop_shell_interface, 2,
5633 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005634 return -1;
5635
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005636 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5637 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005638 return -1;
5639
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005640 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5641 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005642 return -1;
5643
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005644 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005645
Xiong Zhang6b481422013-10-23 13:58:32 +08005646 setup_output_destroy_handler(ec, shell);
5647
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005648 loop = wl_display_get_event_loop(ec->wl_display);
5649 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005650
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005651 shell->screensaver.timer =
5652 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5653
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005654 wl_list_for_each(seat, &ec->seat_list, link)
5655 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005656
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005657 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005658
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005659 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005660
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005661 return 0;
5662}