blob: 9fbac000ada8cfc0a6b706a33b5216432d322e9a [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Xiong Zhang6b481422013-10-23 13:58:32 +080059struct shell_output {
60 struct desktop_shell *shell;
61 struct weston_output *output;
62 struct wl_listener destroy_listener;
63 struct wl_list link;
64};
65
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050066enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020067 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050068 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030069 SHELL_SURFACE_POPUP,
70 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020071};
72
Scott Moreauff1db4a2012-04-17 19:06:18 -060073struct ping_timer {
74 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -060075 uint32_t serial;
76};
77
Philip Withnall648a4dd2013-11-25 18:01:44 +000078/*
79 * Surface stacking and ordering.
80 *
81 * This is handled using several linked lists of surfaces, organised into
82 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
83 * above all of the surfaces in the layer below. The set of layers is static and
84 * in the following order (top-most first):
85 * • Lock layer (only ever displayed on its own)
86 * • Cursor layer
87 * • Fullscreen layer
88 * • Panel layer
89 * • Input panel layer
90 * • Workspace layers
91 * • Background layer
92 *
93 * The list of layers may be manipulated to remove whole layers of surfaces from
94 * display. For example, when locking the screen, all layers except the lock
95 * layer are removed.
96 *
97 * A surface’s layer is modified on configuring the surface, in
98 * set_surface_type() (which is only called when the surface’s type change is
99 * _committed_). If a surface’s type changes (e.g. when making a window
100 * fullscreen) its layer changes too.
101 *
102 * In order to allow popup and transient surfaces to be correctly stacked above
103 * their parent surfaces, each surface tracks both its parent surface, and a
104 * linked list of its children. When a surface’s layer is updated, so are the
105 * layers of its children. Note that child surfaces are *not* the same as
106 * subsurfaces — child/parent surfaces are purely for maintaining stacking
107 * order.
108 *
109 * The children_link list of siblings of a surface (i.e. those surfaces which
110 * have the same parent) only contains weston_surfaces which have a
111 * shell_surface. Stacking is not implemented for non-shell_surface
112 * weston_surfaces. This means that the following implication does *not* hold:
113 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
114 */
115
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200116struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500117 struct wl_resource *resource;
118 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200119
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500120 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500121 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600122 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200123 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400124 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000125 struct wl_list children_list; /* child surfaces of this one */
126 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300127 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200128
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800129 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400130 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200132 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800133 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200134 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800135 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700136 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100137
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500138 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200139 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500140 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200141 } rotation;
142
143 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100144 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100146 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400147 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500148 } popup;
149
Alex Wu4539b082012-03-01 12:57:46 +0800150 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300151 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400152 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300153 } transient;
154
155 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800156 enum wl_shell_surface_fullscreen_method type;
157 struct weston_transform transform; /* matrix from x, y */
158 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500159 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800160 } fullscreen;
161
Scott Moreauff1db4a2012-04-17 19:06:18 -0600162 struct ping_timer *ping_timer;
163
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200164 struct weston_transform workspace_transform;
165
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500166 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500167 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200168 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100169 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400170
171 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200172
173 struct {
174 bool maximized;
175 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200176 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200177 } state, next_state; /* surface states */
178 bool state_changed;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179};
180
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300181struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400182 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
185};
186
Rusty Lynch1084da52013-08-15 09:10:08 -0700187struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
192};
193
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300194struct weston_move_grab {
195 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100196 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500197};
198
Rusty Lynch1084da52013-08-15 09:10:08 -0700199struct weston_touch_move_grab {
200 struct shell_touch_grab base;
201 wl_fixed_t dx, dy;
202};
203
Pekka Paalanen460099f2012-01-20 16:48:25 +0200204struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500206 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200207 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200208 float x;
209 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200210 } center;
211};
212
Giulio Camuffo5085a752013-03-25 21:42:45 +0100213struct shell_seat {
214 struct weston_seat *seat;
215 struct wl_listener seat_destroy_listener;
216
217 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400218 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219 struct wl_list surfaces_list;
220 struct wl_client *client;
221 int32_t initial_up;
222 } popup_grab;
223};
224
Alex Wubd3354b2012-04-17 17:20:49 +0800225static struct desktop_shell *
226shell_surface_get_shell(struct shell_surface *shsurf);
227
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500228static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400229surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500230
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300231static void
232shell_fade_startup(struct desktop_shell *shell);
233
Philip Withnallbecb77e2013-11-25 18:01:30 +0000234static struct shell_seat *
235get_shell_seat(struct weston_seat *seat);
236
Philip Withnall648a4dd2013-11-25 18:01:44 +0000237static void
238shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
239
Alex Wubd3354b2012-04-17 17:20:49 +0800240static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200241shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
242
243static bool
244shell_surface_is_xdg_surface(struct shell_surface *shsurf);
245
246static bool
247shell_surface_is_xdg_popup(struct shell_surface *shsurf);
248
249static void
250shell_surface_set_parent(struct shell_surface *shsurf,
251 struct weston_surface *parent);
252
253static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800254shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
255{
256 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500257 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800258
259 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100260
Jason Ekstranda7af7042013-10-12 22:38:11 -0500261 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800262 return false;
263
Jason Ekstranda7af7042013-10-12 22:38:11 -0500264 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
265 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800266 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500267 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800268}
269
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500270static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400271destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300272{
273 struct shell_grab *grab;
274
275 grab = container_of(listener, struct shell_grab,
276 shsurf_destroy_listener);
277
278 grab->shsurf = NULL;
279}
280
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800281struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282get_default_view(struct weston_surface *surface)
283{
284 struct shell_surface *shsurf;
285 struct weston_view *view;
286
287 if (!surface || wl_list_empty(&surface->views))
288 return NULL;
289
290 shsurf = get_shell_surface(surface);
291 if (shsurf)
292 return shsurf->view;
293
294 wl_list_for_each(view, &surface->views, surface_link)
295 if (weston_view_is_mapped(view))
296 return view;
297
298 return container_of(surface->views.next, struct weston_view, surface_link);
299}
300
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300301static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400302popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400303
304static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300305shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400306 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300307 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400308 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300309 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300310{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300311 struct desktop_shell *shell = shsurf->shell;
312
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400313 popup_grab_end(pointer);
314
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300315 grab->grab.interface = interface;
316 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400317 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500318 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400319 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300320
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700321 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400323 if (shell->child.desktop_shell) {
324 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
325 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326 weston_pointer_set_focus(pointer,
327 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400328 wl_fixed_from_int(0),
329 wl_fixed_from_int(0));
330 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300331}
332
333static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300334shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300335{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700336 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400337 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700338 grab->shsurf->grabbed = 0;
339 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300340
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700341 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342}
343
344static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700345shell_touch_grab_start(struct shell_touch_grab *grab,
346 const struct weston_touch_grab_interface *interface,
347 struct shell_surface *shsurf,
348 struct weston_touch *touch)
349{
350 struct desktop_shell *shell = shsurf->shell;
351
352 grab->grab.interface = interface;
353 grab->shsurf = shsurf;
354 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
355 wl_signal_add(&shsurf->destroy_signal,
356 &grab->shsurf_destroy_listener);
357
358 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700359 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700360
361 weston_touch_start_grab(touch, &grab->grab);
362 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500363 weston_touch_set_focus(touch->seat,
364 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700365}
366
367static void
368shell_touch_grab_end(struct shell_touch_grab *grab)
369{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700370 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700371 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700372 grab->shsurf->grabbed = 0;
373 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700374
375 weston_touch_end_grab(grab->touch);
376}
377
378static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500379center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800380 struct weston_output *output);
381
Daniel Stone496ca172012-05-30 16:31:42 +0100382static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300383get_modifier(char *modifier)
384{
385 if (!modifier)
386 return MODIFIER_SUPER;
387
388 if (!strcmp("ctrl", modifier))
389 return MODIFIER_CTRL;
390 else if (!strcmp("alt", modifier))
391 return MODIFIER_ALT;
392 else if (!strcmp("super", modifier))
393 return MODIFIER_SUPER;
394 else
395 return MODIFIER_SUPER;
396}
397
Juan Zhaoe10d2792012-04-25 19:09:52 +0800398static enum animation_type
399get_animation_type(char *animation)
400{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800401 if (!strcmp("zoom", animation))
402 return ANIMATION_ZOOM;
403 else if (!strcmp("fade", animation))
404 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100405 else if (!strcmp("dim-layer", animation))
406 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800407 else
408 return ANIMATION_NONE;
409}
410
Alex Wu4539b082012-03-01 12:57:46 +0800411static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400412shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200413{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400414 struct weston_config_section *section;
415 int duration;
416 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200417
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400418 section = weston_config_get_section(shell->compositor->config,
419 "screensaver", NULL, NULL);
420 weston_config_section_get_string(section,
421 "path", &shell->screensaver.path, NULL);
422 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200423 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400424
425 section = weston_config_get_section(shell->compositor->config,
426 "shell", NULL, NULL);
427 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100428 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100429 shell->client = s;
430 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400431 "binding-modifier", &s, "super");
432 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200433 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400434 weston_config_section_get_string(section, "animation", &s, "none");
435 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200436 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700437 weston_config_section_get_string(section,
438 "startup-animation", &s, "fade");
439 shell->startup_animation_type = get_animation_type(s);
440 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700441 if (shell->startup_animation_type == ANIMATION_ZOOM)
442 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100443 weston_config_section_get_string(section, "focus-animation", &s, "none");
444 shell->focus_animation_type = get_animation_type(s);
445 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400446 weston_config_section_get_uint(section, "num-workspaces",
447 &shell->workspaces.num,
448 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200449}
450
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800451struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100452get_default_output(struct weston_compositor *compositor)
453{
454 return container_of(compositor->output_list.next,
455 struct weston_output, link);
456}
457
458
459/* no-op func for checking focus surface */
460static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600461focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100462{
463}
464
465static struct focus_surface *
466get_focus_surface(struct weston_surface *surface)
467{
468 if (surface->configure == focus_surface_configure)
469 return surface->configure_private;
470 else
471 return NULL;
472}
473
474static bool
475is_focus_surface (struct weston_surface *es)
476{
477 return (es->configure == focus_surface_configure);
478}
479
480static bool
481is_focus_view (struct weston_view *view)
482{
483 return is_focus_surface (view->surface);
484}
485
486static struct focus_surface *
487create_focus_surface(struct weston_compositor *ec,
488 struct weston_output *output)
489{
490 struct focus_surface *fsurf = NULL;
491 struct weston_surface *surface = NULL;
492
493 fsurf = malloc(sizeof *fsurf);
494 if (!fsurf)
495 return NULL;
496
497 fsurf->surface = weston_surface_create(ec);
498 surface = fsurf->surface;
499 if (surface == NULL) {
500 free(fsurf);
501 return NULL;
502 }
503
504 surface->configure = focus_surface_configure;
505 surface->output = output;
506 surface->configure_private = fsurf;
507
508 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100509 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100510
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600511 surface->width = output->width;
512 surface->height = output->height;
513 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100514 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
515 pixman_region32_fini(&surface->opaque);
516 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
517 output->width, output->height);
518 pixman_region32_fini(&surface->input);
519 pixman_region32_init(&surface->input);
520
521 wl_list_init(&fsurf->workspace_transform.link);
522
523 return fsurf;
524}
525
526static void
527focus_surface_destroy(struct focus_surface *fsurf)
528{
529 weston_surface_destroy(fsurf->surface);
530 free(fsurf);
531}
532
533static void
534focus_animation_done(struct weston_view_animation *animation, void *data)
535{
536 struct workspace *ws = data;
537
538 ws->focus_animation = NULL;
539}
540
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200541static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200542focus_state_destroy(struct focus_state *state)
543{
544 wl_list_remove(&state->seat_destroy_listener.link);
545 wl_list_remove(&state->surface_destroy_listener.link);
546 free(state);
547}
548
549static void
550focus_state_seat_destroy(struct wl_listener *listener, void *data)
551{
552 struct focus_state *state = container_of(listener,
553 struct focus_state,
554 seat_destroy_listener);
555
556 wl_list_remove(&state->link);
557 focus_state_destroy(state);
558}
559
560static void
561focus_state_surface_destroy(struct wl_listener *listener, void *data)
562{
563 struct focus_state *state = container_of(listener,
564 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400565 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400566 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500567 struct weston_surface *main_surface, *next;
568 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200569
Pekka Paalanen01388e22013-04-25 13:57:44 +0300570 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
571
Kristian Høgsberge3778222012-07-31 17:29:30 -0400572 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500573 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
574 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400575 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100576 if (is_focus_view(view))
577 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400578
Jason Ekstranda7af7042013-10-12 22:38:11 -0500579 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400580 break;
581 }
582
Pekka Paalanen01388e22013-04-25 13:57:44 +0300583 /* if the focus was a sub-surface, activate its main surface */
584 if (main_surface != state->keyboard_focus)
585 next = main_surface;
586
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100587 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400588 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100589 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400590 activate(shell, next, state->seat);
591 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100592 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
593 if (state->ws->focus_animation)
594 weston_view_animation_destroy(state->ws->focus_animation);
595
596 state->ws->focus_animation = weston_fade_run(
597 state->ws->fsurf_front->view,
598 state->ws->fsurf_front->view->alpha, 0.0, 300,
599 focus_animation_done, state->ws);
600 }
601
Kristian Høgsberge3778222012-07-31 17:29:30 -0400602 wl_list_remove(&state->link);
603 focus_state_destroy(state);
604 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200605}
606
607static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400608focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200609{
Jonas Ådahl04769742012-06-13 00:01:24 +0200610 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200611
612 state = malloc(sizeof *state);
613 if (state == NULL)
614 return NULL;
615
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100616 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400617 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200618 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400619 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200620
621 state->seat_destroy_listener.notify = focus_state_seat_destroy;
622 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400623 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200624 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400625 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200626
627 return state;
628}
629
Jonas Ådahl8538b222012-08-29 22:13:03 +0200630static struct focus_state *
631ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
632{
633 struct workspace *ws = get_current_workspace(shell);
634 struct focus_state *state;
635
636 wl_list_for_each(state, &ws->focus_list, link)
637 if (state->seat == seat)
638 break;
639
640 if (&state->link == &ws->focus_list)
641 state = focus_state_create(seat, ws);
642
643 return state;
644}
645
Jonas Ådahl04769742012-06-13 00:01:24 +0200646static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400647restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200648{
649 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400650 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200651
652 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400653 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200654
Kristian Høgsberge3148752013-05-06 23:19:49 -0400655 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200656 }
657}
658
659static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200660replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
661 struct weston_seat *seat)
662{
663 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400664 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200665
666 wl_list_for_each(state, &ws->focus_list, link) {
667 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400668 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500669 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200670 return;
671 }
672 }
673}
674
675static void
676drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
677 struct weston_surface *surface)
678{
679 struct focus_state *state;
680
681 wl_list_for_each(state, &ws->focus_list, link)
682 if (state->keyboard_focus == surface)
683 state->keyboard_focus = NULL;
684}
685
686static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100687animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
688 struct weston_view *from, struct weston_view *to)
689{
690 struct weston_output *output;
691 bool focus_surface_created = false;
692
693 /* FIXME: Only support dim animation using two layers */
694 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
695 return;
696
697 output = get_default_output(shell->compositor);
698 if (ws->fsurf_front == NULL && (from || to)) {
699 ws->fsurf_front = create_focus_surface(shell->compositor, output);
700 ws->fsurf_back = create_focus_surface(shell->compositor, output);
701 ws->fsurf_front->view->alpha = 0.0;
702 ws->fsurf_back->view->alpha = 0.0;
703 focus_surface_created = true;
704 } else {
705 wl_list_remove(&ws->fsurf_front->view->layer_link);
706 wl_list_remove(&ws->fsurf_back->view->layer_link);
707 }
708
709 if (ws->focus_animation) {
710 weston_view_animation_destroy(ws->focus_animation);
711 ws->focus_animation = NULL;
712 }
713
714 if (to)
715 wl_list_insert(&to->layer_link,
716 &ws->fsurf_front->view->layer_link);
717 else if (from)
718 wl_list_insert(&ws->layer.view_list,
719 &ws->fsurf_front->view->layer_link);
720
721 if (focus_surface_created) {
722 ws->focus_animation = weston_fade_run(
723 ws->fsurf_front->view,
724 ws->fsurf_front->view->alpha, 0.6, 300,
725 focus_animation_done, ws);
726 } else if (from) {
727 wl_list_insert(&from->layer_link,
728 &ws->fsurf_back->view->layer_link);
729 ws->focus_animation = weston_stable_fade_run(
730 ws->fsurf_front->view, 0.0,
731 ws->fsurf_back->view, 0.6,
732 focus_animation_done, ws);
733 } else if (to) {
734 wl_list_insert(&ws->layer.view_list,
735 &ws->fsurf_back->view->layer_link);
736 ws->focus_animation = weston_stable_fade_run(
737 ws->fsurf_front->view, 0.0,
738 ws->fsurf_back->view, 0.6,
739 focus_animation_done, ws);
740 }
741}
742
743static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200744workspace_destroy(struct workspace *ws)
745{
Jonas Ådahl04769742012-06-13 00:01:24 +0200746 struct focus_state *state, *next;
747
748 wl_list_for_each_safe(state, next, &ws->focus_list, link)
749 focus_state_destroy(state);
750
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100751 if (ws->fsurf_front)
752 focus_surface_destroy(ws->fsurf_front);
753 if (ws->fsurf_back)
754 focus_surface_destroy(ws->fsurf_back);
755
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200756 free(ws);
757}
758
Jonas Ådahl04769742012-06-13 00:01:24 +0200759static void
760seat_destroyed(struct wl_listener *listener, void *data)
761{
762 struct weston_seat *seat = data;
763 struct focus_state *state, *next;
764 struct workspace *ws = container_of(listener,
765 struct workspace,
766 seat_destroyed_listener);
767
768 wl_list_for_each_safe(state, next, &ws->focus_list, link)
769 if (state->seat == seat)
770 wl_list_remove(&state->link);
771}
772
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200773static struct workspace *
774workspace_create(void)
775{
776 struct workspace *ws = malloc(sizeof *ws);
777 if (ws == NULL)
778 return NULL;
779
780 weston_layer_init(&ws->layer, NULL);
781
Jonas Ådahl04769742012-06-13 00:01:24 +0200782 wl_list_init(&ws->focus_list);
783 wl_list_init(&ws->seat_destroyed_listener.link);
784 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100785 ws->fsurf_front = NULL;
786 ws->fsurf_back = NULL;
787 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200788
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200789 return ws;
790}
791
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200792static int
793workspace_is_empty(struct workspace *ws)
794{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200796}
797
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200798static struct workspace *
799get_workspace(struct desktop_shell *shell, unsigned int index)
800{
801 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200802 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200803 pws += index;
804 return *pws;
805}
806
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800807struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200808get_current_workspace(struct desktop_shell *shell)
809{
810 return get_workspace(shell, shell->workspaces.current);
811}
812
813static void
814activate_workspace(struct desktop_shell *shell, unsigned int index)
815{
816 struct workspace *ws;
817
818 ws = get_workspace(shell, index);
819 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
820
821 shell->workspaces.current = index;
822}
823
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200824static unsigned int
825get_output_height(struct weston_output *output)
826{
827 return abs(output->region.extents.y1 - output->region.extents.y2);
828}
829
830static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100831view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200832{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200833 struct weston_transform *transform;
834
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 if (is_focus_view(view)) {
836 struct focus_surface *fsurf = get_focus_surface(view->surface);
837 transform = &fsurf->workspace_transform;
838 } else {
839 struct shell_surface *shsurf = get_shell_surface(view->surface);
840 transform = &shsurf->workspace_transform;
841 }
842
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200843 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500844 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100845 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200846
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100847 weston_matrix_init(&transform->matrix);
848 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200849 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500850 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851}
852
853static void
854workspace_translate_out(struct workspace *ws, double fraction)
855{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500856 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857 unsigned int height;
858 double d;
859
Jason Ekstranda7af7042013-10-12 22:38:11 -0500860 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
861 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200862 d = height * fraction;
863
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100864 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200865 }
866}
867
868static void
869workspace_translate_in(struct workspace *ws, double fraction)
870{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500871 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200872 unsigned int height;
873 double d;
874
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
876 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200877
878 if (fraction > 0)
879 d = -(height - height * fraction);
880 else
881 d = height + 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
Jonas Ådahle9d22502012-08-29 22:13:01 +0200888broadcast_current_workspace_state(struct desktop_shell *shell)
889{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700890 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200891
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700892 wl_resource_for_each(resource, &shell->workspaces.client_list)
893 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200894 shell->workspaces.current,
895 shell->workspaces.num);
896}
897
898static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200899reverse_workspace_change_animation(struct desktop_shell *shell,
900 unsigned int index,
901 struct workspace *from,
902 struct workspace *to)
903{
904 shell->workspaces.current = index;
905
906 shell->workspaces.anim_to = to;
907 shell->workspaces.anim_from = from;
908 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
909 shell->workspaces.anim_timestamp = 0;
910
Scott Moreau4272e632012-08-13 09:58:41 -0600911 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912}
913
914static void
915workspace_deactivate_transforms(struct workspace *ws)
916{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100918 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200919
Jason Ekstranda7af7042013-10-12 22:38:11 -0500920 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100921 if (is_focus_view(view)) {
922 struct focus_surface *fsurf = get_focus_surface(view->surface);
923 transform = &fsurf->workspace_transform;
924 } else {
925 struct shell_surface *shsurf = get_shell_surface(view->surface);
926 transform = &shsurf->workspace_transform;
927 }
928
929 if (!wl_list_empty(&transform->link)) {
930 wl_list_remove(&transform->link);
931 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200934 }
935}
936
937static void
938finish_workspace_change_animation(struct desktop_shell *shell,
939 struct workspace *from,
940 struct workspace *to)
941{
Scott Moreau4272e632012-08-13 09:58:41 -0600942 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200943
944 wl_list_remove(&shell->workspaces.animation.link);
945 workspace_deactivate_transforms(from);
946 workspace_deactivate_transforms(to);
947 shell->workspaces.anim_to = NULL;
948
949 wl_list_remove(&shell->workspaces.anim_from->layer.link);
950}
951
952static void
953animate_workspace_change_frame(struct weston_animation *animation,
954 struct weston_output *output, uint32_t msecs)
955{
956 struct desktop_shell *shell =
957 container_of(animation, struct desktop_shell,
958 workspaces.animation);
959 struct workspace *from = shell->workspaces.anim_from;
960 struct workspace *to = shell->workspaces.anim_to;
961 uint32_t t;
962 double x, y;
963
964 if (workspace_is_empty(from) && workspace_is_empty(to)) {
965 finish_workspace_change_animation(shell, from, to);
966 return;
967 }
968
969 if (shell->workspaces.anim_timestamp == 0) {
970 if (shell->workspaces.anim_current == 0.0)
971 shell->workspaces.anim_timestamp = msecs;
972 else
973 shell->workspaces.anim_timestamp =
974 msecs -
975 /* Invers of movement function 'y' below. */
976 (asin(1.0 - shell->workspaces.anim_current) *
977 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
978 M_2_PI);
979 }
980
981 t = msecs - shell->workspaces.anim_timestamp;
982
983 /*
984 * x = [0, π/2]
985 * y(x) = sin(x)
986 */
987 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
988 y = sin(x);
989
990 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600991 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200992
993 workspace_translate_out(from, shell->workspaces.anim_dir * y);
994 workspace_translate_in(to, shell->workspaces.anim_dir * y);
995 shell->workspaces.anim_current = y;
996
Scott Moreau4272e632012-08-13 09:58:41 -0600997 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200999 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001001}
1002
1003static void
1004animate_workspace_change(struct desktop_shell *shell,
1005 unsigned int index,
1006 struct workspace *from,
1007 struct workspace *to)
1008{
1009 struct weston_output *output;
1010
1011 int dir;
1012
1013 if (index > shell->workspaces.current)
1014 dir = -1;
1015 else
1016 dir = 1;
1017
1018 shell->workspaces.current = index;
1019
1020 shell->workspaces.anim_dir = dir;
1021 shell->workspaces.anim_from = from;
1022 shell->workspaces.anim_to = to;
1023 shell->workspaces.anim_current = 0.0;
1024 shell->workspaces.anim_timestamp = 0;
1025
1026 output = container_of(shell->compositor->output_list.next,
1027 struct weston_output, link);
1028 wl_list_insert(&output->animation_list,
1029 &shell->workspaces.animation.link);
1030
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001031 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001032
1033 workspace_translate_in(to, 0);
1034
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001035 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001036
Scott Moreau4272e632012-08-13 09:58:41 -06001037 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001038}
1039
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001040static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001041update_workspace(struct desktop_shell *shell, unsigned int index,
1042 struct workspace *from, struct workspace *to)
1043{
1044 shell->workspaces.current = index;
1045 wl_list_insert(&from->layer.link, &to->layer.link);
1046 wl_list_remove(&from->layer.link);
1047}
1048
1049static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001050change_workspace(struct desktop_shell *shell, unsigned int index)
1051{
1052 struct workspace *from;
1053 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001054 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001055
1056 if (index == shell->workspaces.current)
1057 return;
1058
1059 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001061 return;
1062
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001063 from = get_current_workspace(shell);
1064 to = get_workspace(shell, index);
1065
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001066 if (shell->workspaces.anim_from == to &&
1067 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001068 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001069 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001070 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001071 return;
1072 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001073
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001074 if (shell->workspaces.anim_to != NULL)
1075 finish_workspace_change_animation(shell,
1076 shell->workspaces.anim_from,
1077 shell->workspaces.anim_to);
1078
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001079 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001080
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001081 if (shell->focus_animation_type != ANIMATION_NONE) {
1082 wl_list_for_each(state, &from->focus_list, link)
1083 if (state->keyboard_focus)
1084 animate_focus_change(shell, from,
1085 get_default_view(state->keyboard_focus), NULL);
1086
1087 wl_list_for_each(state, &to->focus_list, link)
1088 if (state->keyboard_focus)
1089 animate_focus_change(shell, to,
1090 NULL, get_default_view(state->keyboard_focus));
1091 }
1092
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001093 if (workspace_is_empty(to) && workspace_is_empty(from))
1094 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095 else
1096 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001097
1098 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001099}
1100
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001101static bool
1102workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1103{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001105 struct wl_list *e;
1106
1107 if (wl_list_empty(list))
1108 return false;
1109
1110 e = list->next;
1111
1112 if (e->next != list)
1113 return false;
1114
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001116}
1117
1118static void
Philip Withnall659163d2013-11-25 18:01:36 +00001119move_surface_to_workspace(struct desktop_shell *shell,
1120 struct shell_surface *shsurf,
1121 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001122{
1123 struct workspace *from;
1124 struct workspace *to;
1125 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001126 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001127 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001128
1129 if (workspace == shell->workspaces.current)
1130 return;
1131
Philip Withnall659163d2013-11-25 18:01:36 +00001132 view = get_default_view(shsurf->surface);
1133 if (!view)
1134 return;
1135
1136 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1137
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001138 if (workspace >= shell->workspaces.num)
1139 workspace = shell->workspaces.num - 1;
1140
Jonas Ådahle9d22502012-08-29 22:13:01 +02001141 from = get_current_workspace(shell);
1142 to = get_workspace(shell, workspace);
1143
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 wl_list_remove(&view->layer_link);
1145 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001146
Philip Withnall648a4dd2013-11-25 18:01:44 +00001147 shell_surface_update_child_surface_layers(shsurf);
1148
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001150 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1151 if (!seat->keyboard)
1152 continue;
1153
1154 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001156 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001157 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001158
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001160}
1161
1162static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001163take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001164 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001165 unsigned int index)
1166{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001167 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001169 struct shell_surface *shsurf;
1170 struct workspace *from;
1171 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001172 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001173
Pekka Paalanen01388e22013-04-25 13:57:44 +03001174 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 view = get_default_view(surface);
1176 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001177 index == shell->workspaces.current ||
1178 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001179 return;
1180
1181 from = get_current_workspace(shell);
1182 to = get_workspace(shell, index);
1183
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 wl_list_remove(&view->layer_link);
1185 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001186
Philip Withnall659163d2013-11-25 18:01:36 +00001187 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001188 if (shsurf != NULL)
1189 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001190
Jonas Ådahle9d22502012-08-29 22:13:01 +02001191 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001192 drop_focus_state(shell, from, surface);
1193
1194 if (shell->workspaces.anim_from == to &&
1195 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001196 wl_list_remove(&to->layer.link);
1197 wl_list_insert(from->layer.link.prev, &to->layer.link);
1198
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001199 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001200 broadcast_current_workspace_state(shell);
1201
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001202 return;
1203 }
1204
1205 if (shell->workspaces.anim_to != NULL)
1206 finish_workspace_change_animation(shell,
1207 shell->workspaces.anim_from,
1208 shell->workspaces.anim_to);
1209
1210 if (workspace_is_empty(from) &&
1211 workspace_has_only(to, surface))
1212 update_workspace(shell, index, from, to);
1213 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001214 if (shsurf != NULL &&
1215 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001216 wl_list_insert(&shell->workspaces.anim_sticky_list,
1217 &shsurf->workspace_transform.link);
1218
1219 animate_workspace_change(shell, index, from, to);
1220 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001221
1222 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001223
1224 state = ensure_focus_state(shell, seat);
1225 if (state != NULL)
1226 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001227}
1228
1229static void
1230workspace_manager_move_surface(struct wl_client *client,
1231 struct wl_resource *resource,
1232 struct wl_resource *surface_resource,
1233 uint32_t workspace)
1234{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001235 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001236 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001237 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001238 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001239 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001240
Pekka Paalanen01388e22013-04-25 13:57:44 +03001241 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001242 shell_surface = get_shell_surface(main_surface);
1243 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001245
1246 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001247}
1248
1249static const struct workspace_manager_interface workspace_manager_implementation = {
1250 workspace_manager_move_surface,
1251};
1252
1253static void
1254unbind_resource(struct wl_resource *resource)
1255{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001256 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001257}
1258
1259static void
1260bind_workspace_manager(struct wl_client *client,
1261 void *data, uint32_t version, uint32_t id)
1262{
1263 struct desktop_shell *shell = data;
1264 struct wl_resource *resource;
1265
Jason Ekstranda85118c2013-06-27 20:17:02 -05001266 resource = wl_resource_create(client,
1267 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001268
1269 if (resource == NULL) {
1270 weston_log("couldn't add workspace manager object");
1271 return;
1272 }
1273
Jason Ekstranda85118c2013-06-27 20:17:02 -05001274 wl_resource_set_implementation(resource,
1275 &workspace_manager_implementation,
1276 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001277 wl_list_insert(&shell->workspaces.client_list,
1278 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001279
1280 workspace_manager_send_state(resource,
1281 shell->workspaces.current,
1282 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283}
1284
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001285static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001286touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1287 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1288{
1289}
1290
1291static void
1292touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1293{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001294 struct weston_touch_move_grab *move =
1295 (struct weston_touch_move_grab *) container_of(
1296 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001297
Jonas Ådahl9484b692013-12-02 22:05:03 +01001298 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001299 shell_touch_grab_end(&move->base);
1300 free(move);
1301 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001302}
1303
1304static void
1305touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1306 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1307{
1308 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1309 struct shell_surface *shsurf = move->base.shsurf;
1310 struct weston_surface *es;
1311 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1312 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1313
1314 if (!shsurf)
1315 return;
1316
1317 es = shsurf->surface;
1318
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001319 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001320
1321 weston_compositor_schedule_repaint(es->compositor);
1322}
1323
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001324static void
1325touch_move_grab_cancel(struct weston_touch_grab *grab)
1326{
1327 struct weston_touch_move_grab *move =
1328 (struct weston_touch_move_grab *) container_of(
1329 grab, struct shell_touch_grab, grab);
1330
1331 shell_touch_grab_end(&move->base);
1332 free(move);
1333}
1334
Rusty Lynch1084da52013-08-15 09:10:08 -07001335static const struct weston_touch_grab_interface touch_move_grab_interface = {
1336 touch_move_grab_down,
1337 touch_move_grab_up,
1338 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001339 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001340};
1341
1342static int
1343surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1344{
1345 struct weston_touch_move_grab *move;
1346
1347 if (!shsurf)
1348 return -1;
1349
Rafael Antognolli03b16592013-12-03 15:35:42 -02001350 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001351 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001352 if (shsurf->grabbed)
1353 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001354
1355 move = malloc(sizeof *move);
1356 if (!move)
1357 return -1;
1358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001360 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001362 seat->touch->grab_y;
1363
1364 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1365 seat->touch);
1366
1367 return 0;
1368}
1369
1370static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001371noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001372{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001373}
1374
1375static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001376move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1377 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001378{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001379 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001380 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001381 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001382 int dx, dy;
1383
1384 weston_pointer_move(pointer, x, y);
1385 dx = wl_fixed_to_int(pointer->x + move->dx);
1386 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001387
1388 if (!shsurf)
1389 return;
1390
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001391 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001392
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001394}
1395
1396static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001397move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001398 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001399{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001400 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1401 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001402 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001403 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001404
Daniel Stone4dbadb12012-05-30 16:31:51 +01001405 if (pointer->button_count == 0 &&
1406 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001407 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001408 free(grab);
1409 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001410}
1411
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001412static void
1413move_grab_cancel(struct weston_pointer_grab *grab)
1414{
1415 struct shell_grab *shell_grab =
1416 container_of(grab, struct shell_grab, grab);
1417
1418 shell_grab_end(shell_grab);
1419 free(grab);
1420}
1421
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001422static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001423 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001424 move_grab_motion,
1425 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001426 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001427};
1428
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001429static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001430surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001431{
1432 struct weston_move_grab *move;
1433
1434 if (!shsurf)
1435 return -1;
1436
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001437 if (shsurf->grabbed)
1438 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001439 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001440 return 0;
1441
1442 move = malloc(sizeof *move);
1443 if (!move)
1444 return -1;
1445
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001447 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001449 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001450
1451 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001452 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001453
1454 return 0;
1455}
1456
1457static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001458common_surface_move(struct wl_resource *resource,
1459 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001460{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001461 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001462 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001463 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001464
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001465 if (seat->pointer &&
1466 seat->pointer->button_count > 0 &&
1467 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001469 if ((surface == shsurf->surface) &&
1470 (surface_move(shsurf, seat) < 0))
1471 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001472 } else if (seat->touch &&
1473 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001475 if ((surface == shsurf->surface) &&
1476 (surface_touch_move(shsurf, seat) < 0))
1477 wl_resource_post_no_memory(resource);
1478 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001479}
1480
Rafael Antognollie2a34552013-12-03 15:35:45 -02001481static void
1482shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1483 struct wl_resource *seat_resource, uint32_t serial)
1484{
1485 common_surface_move(resource, seat_resource, serial);
1486}
1487
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001488struct weston_resize_grab {
1489 struct shell_grab base;
1490 uint32_t edges;
1491 int32_t width, height;
1492};
1493
1494static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001495resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1496 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001497{
1498 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001499 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001500 struct shell_surface *shsurf = resize->base.shsurf;
1501 int32_t width, height;
1502 wl_fixed_t from_x, from_y;
1503 wl_fixed_t to_x, to_y;
1504
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001505 weston_pointer_move(pointer, x, y);
1506
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001507 if (!shsurf)
1508 return;
1509
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 weston_view_from_global_fixed(shsurf->view,
1511 pointer->grab_x, pointer->grab_y,
1512 &from_x, &from_y);
1513 weston_view_from_global_fixed(shsurf->view,
1514 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001515
1516 width = resize->width;
1517 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1518 width += wl_fixed_to_int(from_x - to_x);
1519 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1520 width += wl_fixed_to_int(to_x - from_x);
1521 }
1522
1523 height = resize->height;
1524 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1525 height += wl_fixed_to_int(from_y - to_y);
1526 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1527 height += wl_fixed_to_int(to_y - from_y);
1528 }
1529
1530 shsurf->client->send_configure(shsurf->surface,
1531 resize->edges, width, height);
1532}
1533
1534static void
1535send_configure(struct weston_surface *surface,
1536 uint32_t edges, int32_t width, int32_t height)
1537{
1538 struct shell_surface *shsurf = get_shell_surface(surface);
1539
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001540 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001541 edges, width, height);
1542}
1543
1544static const struct weston_shell_client shell_client = {
1545 send_configure
1546};
1547
1548static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001549resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001550 uint32_t time, uint32_t button, uint32_t state_w)
1551{
1552 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001553 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001554 enum wl_pointer_button_state state = state_w;
1555
1556 if (pointer->button_count == 0 &&
1557 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1558 shell_grab_end(&resize->base);
1559 free(grab);
1560 }
1561}
1562
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001563static void
1564resize_grab_cancel(struct weston_pointer_grab *grab)
1565{
1566 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1567
1568 shell_grab_end(&resize->base);
1569 free(grab);
1570}
1571
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001572static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001573 noop_grab_focus,
1574 resize_grab_motion,
1575 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001576 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001577};
1578
Giulio Camuffob8366642013-04-25 13:57:46 +03001579/*
1580 * Returns the bounding box of a surface and all its sub-surfaces,
1581 * in the surface coordinates system. */
1582static void
1583surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1584 int32_t *y, int32_t *w, int32_t *h) {
1585 pixman_region32_t region;
1586 pixman_box32_t *box;
1587 struct weston_subsurface *subsurface;
1588
1589 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 surface->width,
1591 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001592
1593 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1594 pixman_region32_union_rect(&region, &region,
1595 subsurface->position.x,
1596 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 subsurface->surface->width,
1598 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001599 }
1600
1601 box = pixman_region32_extents(&region);
1602 if (x)
1603 *x = box->x1;
1604 if (y)
1605 *y = box->y1;
1606 if (w)
1607 *w = box->x2 - box->x1;
1608 if (h)
1609 *h = box->y2 - box->y1;
1610
1611 pixman_region32_fini(&region);
1612}
1613
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001614static int
1615surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001616 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617{
1618 struct weston_resize_grab *resize;
1619
Rafael Antognolli03b16592013-12-03 15:35:42 -02001620 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621 return 0;
1622
1623 if (edges == 0 || edges > 15 ||
1624 (edges & 3) == 3 || (edges & 12) == 12)
1625 return 0;
1626
1627 resize = malloc(sizeof *resize);
1628 if (!resize)
1629 return -1;
1630
1631 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001632 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1633 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001634
1635 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001636 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001637
1638 return 0;
1639}
1640
1641static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001642common_surface_resize(struct wl_resource *resource,
1643 struct wl_resource *seat_resource, uint32_t serial,
1644 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001646 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001647 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001648 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001649
Rafael Antognolli03b16592013-12-03 15:35:42 -02001650 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001651 return;
1652
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001654 if (seat->pointer->button_count == 0 ||
1655 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001656 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657 return;
1658
Kristian Høgsberge3148752013-05-06 23:19:49 -04001659 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001660 wl_resource_post_no_memory(resource);
1661}
1662
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001663static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001664shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1665 struct wl_resource *seat_resource, uint32_t serial,
1666 uint32_t edges)
1667{
1668 common_surface_resize(resource, seat_resource, serial, edges);
1669}
1670
1671static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001672end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1673
1674static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001675busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001676{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001677 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001678 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001679 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001680 wl_fixed_t sx, sy;
1681
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 view = weston_compositor_pick_view(pointer->seat->compositor,
1683 pointer->x, pointer->y,
1684 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001685
Jason Ekstranda7af7042013-10-12 22:38:11 -05001686 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001687 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001688}
1689
1690static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001691busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1692 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001693{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001694 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001695}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001696
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001697static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001698busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001699 uint32_t time, uint32_t button, uint32_t state)
1700{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001701 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001702 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001703 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001704
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001705 if (shsurf && button == BTN_LEFT && state) {
1706 activate(shsurf->shell, shsurf->surface, seat);
1707 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001708 } else if (shsurf && button == BTN_RIGHT && state) {
1709 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001710 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001711 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001712}
1713
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001714static void
1715busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1716{
1717 struct shell_grab *grab = (struct shell_grab *) base;
1718
1719 shell_grab_end(grab);
1720 free(grab);
1721}
1722
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001723static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001724 busy_cursor_grab_focus,
1725 busy_cursor_grab_motion,
1726 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001727 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001728};
1729
1730static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001731set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001732{
1733 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001734
1735 grab = malloc(sizeof *grab);
1736 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001737 return;
1738
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001739 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1740 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001741}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001742
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001743static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001744end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001745{
1746 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1747
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001748 if (grab->grab.interface == &busy_cursor_grab_interface &&
1749 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001750 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001751 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001752 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001753}
1754
Scott Moreau9521d5e2012-04-19 13:06:17 -06001755static void
1756ping_timer_destroy(struct shell_surface *shsurf)
1757{
1758 if (!shsurf || !shsurf->ping_timer)
1759 return;
1760
1761 if (shsurf->ping_timer->source)
1762 wl_event_source_remove(shsurf->ping_timer->source);
1763
1764 free(shsurf->ping_timer);
1765 shsurf->ping_timer = NULL;
1766}
1767
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001768static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001769ping_timeout_handler(void *data)
1770{
1771 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001772 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001773
Scott Moreau9521d5e2012-04-19 13:06:17 -06001774 /* Client is not responding */
1775 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001776
1777 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001778 if (seat->pointer->focus &&
1779 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001780 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001781
1782 return 1;
1783}
1784
1785static void
1786ping_handler(struct weston_surface *surface, uint32_t serial)
1787{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001788 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001789 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001790 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001791
1792 if (!shsurf)
1793 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001794 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001795 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001796
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001797 if (shsurf->surface == shsurf->shell->grab_surface)
1798 return;
1799
Scott Moreauff1db4a2012-04-17 19:06:18 -06001800 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001801 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001802 if (!shsurf->ping_timer)
1803 return;
1804
1805 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001806 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1807 shsurf->ping_timer->source =
1808 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1809 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1810
Rafael Antognollie2a34552013-12-03 15:35:45 -02001811 if (shell_surface_is_wl_shell_surface(shsurf))
1812 wl_shell_surface_send_ping(shsurf->resource, serial);
1813 else if (shell_surface_is_xdg_surface(shsurf))
1814 xdg_surface_send_ping(shsurf->resource, serial);
1815 else if (shell_surface_is_xdg_popup(shsurf))
1816 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001817 }
1818}
1819
1820static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001821handle_pointer_focus(struct wl_listener *listener, void *data)
1822{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001823 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825 struct weston_compositor *compositor;
1826 struct shell_surface *shsurf;
1827 uint32_t serial;
1828
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001830 return;
1831
Jason Ekstranda7af7042013-10-12 22:38:11 -05001832 compositor = view->surface->compositor;
1833 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001834
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001835 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836 set_busy_cursor(shsurf, pointer);
1837 } else {
1838 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001839 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001840 }
1841}
1842
1843static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001844create_pointer_focus_listener(struct weston_seat *seat)
1845{
1846 struct wl_listener *listener;
1847
Kristian Høgsberge3148752013-05-06 23:19:49 -04001848 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001849 return;
1850
1851 listener = malloc(sizeof *listener);
1852 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001853 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001854}
1855
1856static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001857xdg_surface_set_transient_for(struct wl_client *client,
1858 struct wl_resource *resource,
1859 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001860{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001861 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001862 struct weston_surface *parent;
1863
1864 if (parent_resource)
1865 parent = wl_resource_get_user_data(parent_resource);
1866 else
1867 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001868
1869 shell_surface_set_parent(shsurf, parent);
1870}
1871
1872static void
1873surface_pong(struct shell_surface *shsurf, uint32_t serial)
1874{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001875 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001876 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001877
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001878 if (shsurf->ping_timer == NULL)
1879 /* Just ignore unsolicited pong. */
1880 return;
1881
Scott Moreauff1db4a2012-04-17 19:06:18 -06001882 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001883 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001884 wl_list_for_each(seat, &ec->seat_list, link) {
1885 if(seat->pointer)
1886 end_busy_cursor(shsurf, seat->pointer);
1887 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001888 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001889 }
1890}
1891
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001892static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001893shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1894 uint32_t serial)
1895{
1896 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1897
1898 surface_pong(shsurf, serial);
1899
1900}
1901
1902static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001903set_title(struct shell_surface *shsurf, const char *title)
1904{
1905 free(shsurf->title);
1906 shsurf->title = strdup(title);
1907}
1908
1909static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001910shell_surface_set_title(struct wl_client *client,
1911 struct wl_resource *resource, const char *title)
1912{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001913 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001914
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001915 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001916}
1917
1918static void
1919shell_surface_set_class(struct wl_client *client,
1920 struct wl_resource *resource, const char *class)
1921{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001922 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001923
1924 free(shsurf->class);
1925 shsurf->class = strdup(class);
1926}
1927
Alex Wu4539b082012-03-01 12:57:46 +08001928static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001929restore_output_mode(struct weston_output *output)
1930{
Hardening57388e42013-09-18 23:56:36 +02001931 if (output->current_mode != output->original_mode ||
1932 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001933 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001934 output->original_mode,
1935 output->original_scale,
1936 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001937}
1938
1939static void
1940restore_all_output_modes(struct weston_compositor *compositor)
1941{
1942 struct weston_output *output;
1943
1944 wl_list_for_each(output, &compositor->output_list, link)
1945 restore_output_mode(output);
1946}
1947
Philip Withnallbecb77e2013-11-25 18:01:30 +00001948static int
1949get_output_panel_height(struct desktop_shell *shell,
1950 struct weston_output *output)
1951{
1952 struct weston_view *view;
1953 int panel_height = 0;
1954
1955 if (!output)
1956 return 0;
1957
1958 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1959 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001960 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00001961 break;
1962 }
1963 }
1964
1965 return panel_height;
1966}
1967
Philip Withnall07926d92013-11-25 18:01:40 +00001968/* The surface will be inserted into the list immediately after the link
1969 * returned by this function (i.e. will be stacked immediately above the
1970 * returned link). */
1971static struct wl_list *
1972shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1973{
1974 struct workspace *ws;
1975
1976 switch (shsurf->type) {
Philip Withnallda704d92013-11-25 18:01:46 +00001977 case SHELL_SURFACE_POPUP: {
1978 /* Popups should go at the front of the workspace of their
1979 * parent surface, rather than just in front of the parent. This
1980 * fixes the situation where there are two top-level windows:
1981 * - Above
1982 * - Below
1983 * and a pop-up menu is created for 'Below'. We want:
1984 * - Popup
1985 * - Above
1986 * - Below
1987 * not:
1988 * - Above
1989 * - Popup
1990 * - Below
1991 */
1992 struct shell_surface *parent_shsurf;
1993
1994 parent_shsurf = get_shell_surface(shsurf->parent);
1995 if (parent_shsurf != NULL)
1996 return shell_surface_calculate_layer_link(parent_shsurf);
1997
1998 break;
1999 }
2000
Rafael Antognolli03b16592013-12-03 15:35:42 -02002001 case SHELL_SURFACE_TOPLEVEL: {
Rafael Antognollied207b42013-12-03 15:35:43 -02002002 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002003 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002004 } else if (shsurf->parent) {
2005 /* Move the surface to its parent layer so that
2006 * surfaces which are transient for fullscreen surfaces
2007 * don't get hidden by the fullscreen surfaces.
2008 * However, unlike popups, transient surfaces are
2009 * stacked in front of their parent but not in front of
2010 * other surfaces of the same type. */
2011 struct weston_view *parent;
2012
2013 /* TODO: Handle a parent with multiple views */
2014 parent = get_default_view(shsurf->parent);
2015 if (parent)
2016 return parent->layer_link.prev;
2017 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002018 break;
2019 }
Philip Withnall07926d92013-11-25 18:01:40 +00002020
2021 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002022 return &shsurf->shell->fullscreen_layer.view_list;
2023
Philip Withnall07926d92013-11-25 18:01:40 +00002024 case SHELL_SURFACE_NONE:
2025 default:
2026 /* Go to the fallback, below. */
2027 break;
2028 }
2029
2030 /* Move the surface to a normal workspace layer so that surfaces
2031 * which were previously fullscreen or transient are no longer
2032 * rendered on top. */
2033 ws = get_current_workspace(shsurf->shell);
2034 return &ws->layer.view_list;
2035}
2036
Philip Withnall648a4dd2013-11-25 18:01:44 +00002037static void
2038shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2039{
2040 struct shell_surface *child;
2041
2042 /* Move the child layers to the same workspace as shsurf. They will be
2043 * stacked above shsurf. */
2044 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2045 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2046 weston_view_geometry_dirty(child->view);
2047 wl_list_remove(&child->view->layer_link);
2048 wl_list_insert(shsurf->view->layer_link.prev,
2049 &child->view->layer_link);
2050 weston_view_geometry_dirty(child->view);
2051 weston_surface_damage(child->surface);
2052
2053 /* Recurse. We don’t expect this to recurse very far (if
2054 * at all) because that would imply we have transient
2055 * (or popup) children of transient surfaces, which
2056 * would be unusual. */
2057 shell_surface_update_child_surface_layers(child);
2058 }
2059 }
2060}
2061
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002062/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002063 * geometry to ensure the changes are redrawn.
2064 *
2065 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2066 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002067static void
2068shell_surface_update_layer(struct shell_surface *shsurf)
2069{
2070 struct wl_list *new_layer_link;
2071
2072 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2073
2074 if (new_layer_link == &shsurf->view->layer_link)
2075 return;
2076
2077 weston_view_geometry_dirty(shsurf->view);
2078 wl_list_remove(&shsurf->view->layer_link);
2079 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2080 weston_view_geometry_dirty(shsurf->view);
2081 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002082
2083 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002084}
2085
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002086static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002087shell_surface_set_parent(struct shell_surface *shsurf,
2088 struct weston_surface *parent)
2089{
2090 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002091
2092 wl_list_remove(&shsurf->children_link);
2093 wl_list_init(&shsurf->children_link);
2094
2095 /* Insert into the parent surface’s child list. */
2096 if (parent != NULL) {
2097 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2098 if (parent_shsurf != NULL)
2099 wl_list_insert(&parent_shsurf->children_list,
2100 &shsurf->children_link);
2101 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002102}
2103
2104static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002105shell_surface_set_output(struct shell_surface *shsurf,
2106 struct weston_output *output)
2107{
2108 struct weston_surface *es = shsurf->surface;
2109
2110 /* get the default output, if the client set it as NULL
2111 check whether the ouput is available */
2112 if (output)
2113 shsurf->output = output;
2114 else if (es->output)
2115 shsurf->output = es->output;
2116 else
2117 shsurf->output = get_default_output(es->compositor);
2118}
2119
2120static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002121surface_clear_next_states(struct shell_surface *shsurf)
2122{
2123 shsurf->next_state.maximized = false;
2124 shsurf->next_state.fullscreen = false;
2125
2126 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2127 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2128 shsurf->state_changed = true;
2129}
2130
2131static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002132set_toplevel(struct shell_surface *shsurf)
2133{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002134 shell_surface_set_parent(shsurf, NULL);
2135 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002136 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002137
2138 /* The layer_link is updated in set_surface_type(),
2139 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002140}
2141
2142static void
2143shell_surface_set_toplevel(struct wl_client *client,
2144 struct wl_resource *resource)
2145{
2146 struct shell_surface *surface = wl_resource_get_user_data(resource);
2147
2148 set_toplevel(surface);
2149}
2150
2151static void
2152set_transient(struct shell_surface *shsurf,
2153 struct weston_surface *parent, int x, int y, uint32_t flags)
2154{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002155 assert(parent != NULL);
2156
Philip Withnallbecb77e2013-11-25 18:01:30 +00002157 shsurf->transient.x = x;
2158 shsurf->transient.y = y;
2159 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002160
Rafael Antognollied207b42013-12-03 15:35:43 -02002161 shsurf->next_state.relative = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002162 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002163
2164 /* The layer_link is updated in set_surface_type(),
2165 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002166}
2167
2168static void
2169shell_surface_set_transient(struct wl_client *client,
2170 struct wl_resource *resource,
2171 struct wl_resource *parent_resource,
2172 int x, int y, uint32_t flags)
2173{
2174 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2175 struct weston_surface *parent =
2176 wl_resource_get_user_data(parent_resource);
2177
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002178 shell_surface_set_parent(shsurf, parent);
2179
Rafael Antognolli03b16592013-12-03 15:35:42 -02002180 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002181 set_transient(shsurf, parent, x, y, flags);
2182}
2183
2184static void
2185set_fullscreen(struct shell_surface *shsurf,
2186 uint32_t method,
2187 uint32_t framerate,
2188 struct weston_output *output)
2189{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002190 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002191
2192 shsurf->fullscreen_output = shsurf->output;
2193 shsurf->fullscreen.type = method;
2194 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002195
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002196 shsurf->next_state.fullscreen = true;
2197 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002198 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002199
2200 shsurf->client->send_configure(shsurf->surface, 0,
2201 shsurf->output->width,
2202 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002203
2204 /* The layer_link is updated in set_surface_type(),
2205 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002206}
2207
2208static void
2209unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002210{
Philip Withnallf85fe842013-11-25 18:01:37 +00002211 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002212 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2213 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002214 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002215 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002216 shsurf->fullscreen_output = NULL;
2217
Alex Wu4539b082012-03-01 12:57:46 +08002218 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2219 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002220
Alex Wu4539b082012-03-01 12:57:46 +08002221 wl_list_remove(&shsurf->fullscreen.transform.link);
2222 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002223
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224 if (shsurf->fullscreen.black_view)
2225 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2226 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002227
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228 weston_view_set_position(shsurf->view,
2229 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002230 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002232 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002233 shsurf->saved_rotation_valid = false;
2234 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002235
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002236 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002237}
2238
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002239static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002240shell_surface_set_fullscreen(struct wl_client *client,
2241 struct wl_resource *resource,
2242 uint32_t method,
2243 uint32_t framerate,
2244 struct wl_resource *output_resource)
2245{
2246 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2247 struct weston_output *output;
2248
2249 if (output_resource)
2250 output = wl_resource_get_user_data(output_resource);
2251 else
2252 output = NULL;
2253
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002254 shell_surface_set_parent(shsurf, NULL);
2255
Rafael Antognolli03b16592013-12-03 15:35:42 -02002256 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002257 set_fullscreen(shsurf, method, framerate, output);
2258}
2259
2260static void
2261set_popup(struct shell_surface *shsurf,
2262 struct weston_surface *parent,
2263 struct weston_seat *seat,
2264 uint32_t serial,
2265 int32_t x,
2266 int32_t y)
2267{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002268 assert(parent != NULL);
2269
Philip Withnallbecb77e2013-11-25 18:01:30 +00002270 shsurf->popup.shseat = get_shell_seat(seat);
2271 shsurf->popup.serial = serial;
2272 shsurf->popup.x = x;
2273 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002274
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002275 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002276}
2277
2278static void
2279shell_surface_set_popup(struct wl_client *client,
2280 struct wl_resource *resource,
2281 struct wl_resource *seat_resource,
2282 uint32_t serial,
2283 struct wl_resource *parent_resource,
2284 int32_t x, int32_t y, uint32_t flags)
2285{
2286 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002287 struct weston_surface *parent =
2288 wl_resource_get_user_data(parent_resource);
2289
2290 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002291
Rafael Antognolli03b16592013-12-03 15:35:42 -02002292 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002293 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002294 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002295 wl_resource_get_user_data(seat_resource),
2296 serial, x, y);
2297}
2298
2299static void
2300set_maximized(struct shell_surface *shsurf,
2301 struct weston_output *output)
2302{
2303 struct desktop_shell *shell;
2304 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002305
Philip Withnall352e7ed2013-11-25 18:01:35 +00002306 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002307
2308 shell = shell_surface_get_shell(shsurf);
2309 panel_height = get_output_panel_height(shell, shsurf->output);
2310 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2311
2312 shsurf->client->send_configure(shsurf->surface, edges,
2313 shsurf->output->width,
2314 shsurf->output->height - panel_height);
2315
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002316 shsurf->next_state.maximized = true;
2317 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002318 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002319}
2320
2321static void
2322unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002323{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002324 /* undo all maximized things here */
2325 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002326 weston_view_set_position(shsurf->view,
2327 shsurf->saved_x,
2328 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002329
2330 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002331 wl_list_insert(&shsurf->view->geometry.transformation_list,
2332 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002333 shsurf->saved_rotation_valid = false;
2334 }
2335
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002336 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002337}
2338
Philip Withnallbecb77e2013-11-25 18:01:30 +00002339static void
2340shell_surface_set_maximized(struct wl_client *client,
2341 struct wl_resource *resource,
2342 struct wl_resource *output_resource)
2343{
2344 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2345 struct weston_output *output;
2346
2347 if (output_resource)
2348 output = wl_resource_get_user_data(output_resource);
2349 else
2350 output = NULL;
2351
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002352 shell_surface_set_parent(shsurf, NULL);
2353
Rafael Antognolli03b16592013-12-03 15:35:42 -02002354 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002355 set_maximized(shsurf, output);
2356}
2357
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002358/* This is only ever called from set_surface_type(), so there’s no need to
2359 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002360static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002361reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002362{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002363 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002364 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002365 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002366 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002367
Pekka Paalanen98262232011-12-01 10:42:22 +02002368 return 0;
2369}
2370
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002371static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002372set_full_output(struct shell_surface *shsurf)
2373{
2374 shsurf->saved_x = shsurf->view->geometry.x;
2375 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002376 shsurf->saved_width = shsurf->surface->width;
2377 shsurf->saved_height = shsurf->surface->height;
2378 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002379 shsurf->saved_position_valid = true;
2380
2381 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2382 wl_list_remove(&shsurf->rotation.transform.link);
2383 wl_list_init(&shsurf->rotation.transform.link);
2384 weston_view_geometry_dirty(shsurf->view);
2385 shsurf->saved_rotation_valid = true;
2386 }
2387}
2388
2389static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002390set_surface_type(struct shell_surface *shsurf)
2391{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002392 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002393 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002394
Philip Withnallbecb77e2013-11-25 18:01:30 +00002395 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002396
Rafael Antognolli03b16592013-12-03 15:35:42 -02002397 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002398 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002399
2400 switch (shsurf->type) {
2401 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002402 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002403 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002404 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002405 weston_view_set_position(shsurf->view,
2406 pev->geometry.x + shsurf->transient.x,
2407 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002408 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002409 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002410
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002411 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002412 weston_view_set_position(shsurf->view, shsurf->transient.x,
2413 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002414 break;
2415
Philip Withnall0f640e22013-11-25 18:01:31 +00002416 case SHELL_SURFACE_POPUP:
2417 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002418 default:
2419 break;
2420 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002421
2422 /* Update the surface’s layer. */
2423 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002424}
2425
Tiago Vignattibe143262012-04-16 17:31:41 +03002426static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002427shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002428{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002429 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002430}
2431
Alex Wu21858432012-04-01 20:13:08 +08002432static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002433black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002434
Jason Ekstranda7af7042013-10-12 22:38:11 -05002435static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002436create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002437 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002438 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002439{
2440 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002441 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002442
2443 surface = weston_surface_create(ec);
2444 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002445 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002446 return NULL;
2447 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002448 view = weston_view_create(surface);
2449 if (surface == NULL) {
2450 weston_log("no memory\n");
2451 weston_surface_destroy(surface);
2452 return NULL;
2453 }
Alex Wu4539b082012-03-01 12:57:46 +08002454
Alex Wu21858432012-04-01 20:13:08 +08002455 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002456 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002457 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002458 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002459 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002460 pixman_region32_fini(&surface->input);
2461 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002462
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002463 surface->width = w;
2464 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002465 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002466
2467 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002468}
2469
Philip Withnalled8f1a92013-11-25 18:01:43 +00002470static void
2471shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2472{
2473 struct weston_output *output = shsurf->fullscreen_output;
2474
Rafael Antognolli03b16592013-12-03 15:35:42 -02002475 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002476
2477 if (!shsurf->fullscreen.black_view)
2478 shsurf->fullscreen.black_view =
2479 create_black_surface(shsurf->surface->compositor,
2480 shsurf->surface,
2481 output->x, output->y,
2482 output->width,
2483 output->height);
2484
2485 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2486 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2487 wl_list_insert(&shsurf->view->layer_link,
2488 &shsurf->fullscreen.black_view->layer_link);
2489 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2490 weston_surface_damage(shsurf->surface);
2491}
2492
Alex Wu4539b082012-03-01 12:57:46 +08002493/* Create black surface and append it to the associated fullscreen surface.
2494 * Handle size dismatch and positioning according to the method. */
2495static void
2496shell_configure_fullscreen(struct shell_surface *shsurf)
2497{
2498 struct weston_output *output = shsurf->fullscreen_output;
2499 struct weston_surface *surface = shsurf->surface;
2500 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002501 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002502 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002503
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002504 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2505 restore_output_mode(output);
2506
Philip Withnalled8f1a92013-11-25 18:01:43 +00002507 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002508
Jason Ekstranda7af7042013-10-12 22:38:11 -05002509 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002510 &surf_width, &surf_height);
2511
Alex Wu4539b082012-03-01 12:57:46 +08002512 switch (shsurf->fullscreen.type) {
2513 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002514 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002515 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002516 break;
2517 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002518 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002519 if (output->width == surf_width &&
2520 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002521 weston_view_set_position(shsurf->view,
2522 output->x - surf_x,
2523 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002524 break;
2525 }
2526
Alex Wu4539b082012-03-01 12:57:46 +08002527 matrix = &shsurf->fullscreen.transform.matrix;
2528 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002529
Scott Moreau1bad5db2012-08-18 01:04:05 -06002530 output_aspect = (float) output->width /
2531 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002532 /* XXX: Use surf_width and surf_height here? */
2533 surface_aspect = (float) surface->width /
2534 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002535 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002536 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002537 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002538 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002539 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002540 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002541
Alex Wu4539b082012-03-01 12:57:46 +08002542 weston_matrix_scale(matrix, scale, scale, 1);
2543 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002544 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002545 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002546 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2547 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002548 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002549
Alex Wu4539b082012-03-01 12:57:46 +08002550 break;
2551 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002552 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002553 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002554 surf_width * surface->buffer_viewport.scale,
2555 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002556 shsurf->fullscreen.framerate};
2557
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002558 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002559 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002560 weston_view_set_position(shsurf->view,
2561 output->x - surf_x,
2562 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002563 shsurf->fullscreen.black_view->surface->width = output->width;
2564 shsurf->fullscreen.black_view->surface->height = output->height;
2565 weston_view_set_position(shsurf->fullscreen.black_view,
2566 output->x - surf_x,
2567 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002568 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002569 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002570 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002572 }
Alex Wubd3354b2012-04-17 17:20:49 +08002573 }
Alex Wu4539b082012-03-01 12:57:46 +08002574 break;
2575 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002576 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002577 break;
2578 default:
2579 break;
2580 }
2581}
2582
Alex Wu4539b082012-03-01 12:57:46 +08002583static void
2584shell_map_fullscreen(struct shell_surface *shsurf)
2585{
Alex Wubd3354b2012-04-17 17:20:49 +08002586 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002587}
2588
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002589static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002590set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2591{
2592 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002593 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002594 shsurf->transient.x = x;
2595 shsurf->transient.y = y;
2596 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002597
2598 shell_surface_set_parent(shsurf, NULL);
2599
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002600 shsurf->type = SHELL_SURFACE_XWAYLAND;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002601}
2602
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002603static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002604
2605static void
2606destroy_shell_seat(struct wl_listener *listener, void *data)
2607{
2608 struct shell_seat *shseat =
2609 container_of(listener,
2610 struct shell_seat, seat_destroy_listener);
2611 struct shell_surface *shsurf, *prev = NULL;
2612
2613 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002614 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002615 shseat->popup_grab.client = NULL;
2616
2617 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2618 shsurf->popup.shseat = NULL;
2619 if (prev) {
2620 wl_list_init(&prev->popup.grab_link);
2621 }
2622 prev = shsurf;
2623 }
2624 wl_list_init(&prev->popup.grab_link);
2625 }
2626
2627 wl_list_remove(&shseat->seat_destroy_listener.link);
2628 free(shseat);
2629}
2630
2631static struct shell_seat *
2632create_shell_seat(struct weston_seat *seat)
2633{
2634 struct shell_seat *shseat;
2635
2636 shseat = calloc(1, sizeof *shseat);
2637 if (!shseat) {
2638 weston_log("no memory to allocate shell seat\n");
2639 return NULL;
2640 }
2641
2642 shseat->seat = seat;
2643 wl_list_init(&shseat->popup_grab.surfaces_list);
2644
2645 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2646 wl_signal_add(&seat->destroy_signal,
2647 &shseat->seat_destroy_listener);
2648
2649 return shseat;
2650}
2651
2652static struct shell_seat *
2653get_shell_seat(struct weston_seat *seat)
2654{
2655 struct wl_listener *listener;
2656
2657 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2658 if (listener == NULL)
2659 return create_shell_seat(seat);
2660
2661 return container_of(listener,
2662 struct shell_seat, seat_destroy_listener);
2663}
2664
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002665static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002666popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002667{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002668 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002669 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002670 struct shell_seat *shseat =
2671 container_of(grab, struct shell_seat, popup_grab.grab);
2672 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002673 wl_fixed_t sx, sy;
2674
Jason Ekstranda7af7042013-10-12 22:38:11 -05002675 view = weston_compositor_pick_view(pointer->seat->compositor,
2676 pointer->x, pointer->y,
2677 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002678
Jason Ekstranda7af7042013-10-12 22:38:11 -05002679 if (view && view->surface->resource &&
2680 wl_resource_get_client(view->surface->resource) == client) {
2681 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002682 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002683 weston_pointer_set_focus(pointer, NULL,
2684 wl_fixed_from_int(0),
2685 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002686 }
2687}
2688
2689static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002690popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2691 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002692{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002693 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002694 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002695 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002696
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002697 weston_pointer_move(pointer, x, y);
2698
Neil Roberts96d790e2013-09-19 17:32:00 +01002699 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002700 weston_view_from_global_fixed(pointer->focus,
2701 pointer->x, pointer->y,
2702 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002703 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002704 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002705}
2706
2707static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002708popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002709 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002710{
2711 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002712 struct shell_seat *shseat =
2713 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002714 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002715 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002716 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002717 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002718
Neil Roberts96d790e2013-09-19 17:32:00 +01002719 resource_list = &grab->pointer->focus_resource_list;
2720 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002721 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002722 wl_resource_for_each(resource, resource_list) {
2723 wl_pointer_send_button(resource, serial,
2724 time, button, state);
2725 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002726 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002727 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002728 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002729 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002730 }
2731
Daniel Stone4dbadb12012-05-30 16:31:51 +01002732 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002733 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002734}
2735
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002736static void
2737popup_grab_cancel(struct weston_pointer_grab *grab)
2738{
2739 popup_grab_end(grab->pointer);
2740}
2741
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002742static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002743 popup_grab_focus,
2744 popup_grab_motion,
2745 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002746 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002747};
2748
2749static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002750shell_surface_send_popup_done(struct shell_surface *shsurf)
2751{
2752 if (shell_surface_is_wl_shell_surface(shsurf))
2753 wl_shell_surface_send_popup_done(shsurf->resource);
2754 else if (shell_surface_is_xdg_popup(shsurf))
2755 xdg_popup_send_popup_done(shsurf->resource,
2756 shsurf->popup.serial);
2757}
2758
2759static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002760popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002761{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002762 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002763 struct shell_seat *shseat =
2764 container_of(grab, struct shell_seat, popup_grab.grab);
2765 struct shell_surface *shsurf;
2766 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002767
2768 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002769 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002770 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002771 shseat->popup_grab.grab.interface = NULL;
2772 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002773 /* Send the popup_done event to all the popups open */
2774 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002775 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002776 shsurf->popup.shseat = NULL;
2777 if (prev) {
2778 wl_list_init(&prev->popup.grab_link);
2779 }
2780 prev = shsurf;
2781 }
2782 wl_list_init(&prev->popup.grab_link);
2783 wl_list_init(&shseat->popup_grab.surfaces_list);
2784 }
2785}
2786
2787static void
2788add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2789{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002790 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002791
2792 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002793 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002794 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002795 /* We must make sure here that this popup was opened after
2796 * a mouse press, and not just by moving around with other
2797 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002798 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002799 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002800
Rob Bradforddfe31052013-06-26 19:49:11 +01002801 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002802 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002803 } else {
2804 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002805 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002806}
2807
2808static void
2809remove_popup_grab(struct shell_surface *shsurf)
2810{
2811 struct shell_seat *shseat = shsurf->popup.shseat;
2812
2813 wl_list_remove(&shsurf->popup.grab_link);
2814 wl_list_init(&shsurf->popup.grab_link);
2815 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002816 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002817 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002818 }
2819}
2820
2821static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002822shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002823{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002824 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002826
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827 shsurf->surface->output = parent_view->output;
2828 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002829
Jason Ekstranda7af7042013-10-12 22:38:11 -05002830 weston_view_set_transform_parent(shsurf->view, parent_view);
2831 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2832 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002833
Kristian Høgsberge3148752013-05-06 23:19:49 -04002834 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002835 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002836 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002837 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002838 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002839 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002840}
2841
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002842static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002843 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002844 shell_surface_move,
2845 shell_surface_resize,
2846 shell_surface_set_toplevel,
2847 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002848 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002849 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002850 shell_surface_set_maximized,
2851 shell_surface_set_title,
2852 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002853};
2854
2855static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002856destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002857{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002858 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2859
Giulio Camuffo5085a752013-03-25 21:42:45 +01002860 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2861 remove_popup_grab(shsurf);
2862 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002863
Alex Wubd3354b2012-04-17 17:20:49 +08002864 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002865 shell_surface_is_top_fullscreen(shsurf))
2866 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002867
Jason Ekstranda7af7042013-10-12 22:38:11 -05002868 if (shsurf->fullscreen.black_view)
2869 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002870
Alex Wubd3354b2012-04-17 17:20:49 +08002871 /* As destroy_resource() use wl_list_for_each_safe(),
2872 * we can always remove the listener.
2873 */
2874 wl_list_remove(&shsurf->surface_destroy_listener.link);
2875 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002876 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002877 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002878
Jason Ekstranda7af7042013-10-12 22:38:11 -05002879 weston_view_destroy(shsurf->view);
2880
Philip Withnall648a4dd2013-11-25 18:01:44 +00002881 wl_list_remove(&shsurf->children_link);
2882
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002883 wl_list_remove(&shsurf->link);
2884 free(shsurf);
2885}
2886
2887static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002888shell_destroy_shell_surface(struct wl_resource *resource)
2889{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002890 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002891
2892 destroy_shell_surface(shsurf);
2893}
2894
2895static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002896shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002897{
2898 struct shell_surface *shsurf = container_of(listener,
2899 struct shell_surface,
2900 surface_destroy_listener);
2901
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002902 if (shsurf->resource)
2903 wl_resource_destroy(shsurf->resource);
2904 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002905 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002906}
2907
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002908static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002909shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002910
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002911struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002912get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002913{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002914 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002915 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002916 else
2917 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002918}
2919
Rafael Antognollie2a34552013-12-03 15:35:45 -02002920static struct shell_surface *
2921create_common_surface(void *shell, struct weston_surface *surface,
2922 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002923{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002924 struct shell_surface *shsurf;
2925
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002926 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002927 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002928 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002929 }
2930
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002931 shsurf = calloc(1, sizeof *shsurf);
2932 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002933 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002934 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002935 }
2936
Jason Ekstranda7af7042013-10-12 22:38:11 -05002937 shsurf->view = weston_view_create(surface);
2938 if (!shsurf->view) {
2939 weston_log("no memory to allocate shell surface\n");
2940 free(shsurf);
2941 return NULL;
2942 }
2943
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002944 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002945 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002946
Tiago Vignattibc052c92012-04-19 16:18:18 +03002947 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002948 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002949 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002950 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002951 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002952 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002953 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2954 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002955 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002956 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002957 wl_list_init(&shsurf->fullscreen.transform.link);
2958
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002959 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002960 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002961 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002962 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002963
2964 /* init link so its safe to always remove it in destroy_shell_surface */
2965 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002966 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002967
Pekka Paalanen460099f2012-01-20 16:48:25 +02002968 /* empty when not in use */
2969 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002970 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002971
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002972 wl_list_init(&shsurf->workspace_transform.link);
2973
Philip Withnall648a4dd2013-11-25 18:01:44 +00002974 wl_list_init(&shsurf->children_link);
2975 wl_list_init(&shsurf->children_list);
2976 shsurf->parent = NULL;
2977
Pekka Paalanen98262232011-12-01 10:42:22 +02002978 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002979
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002980 shsurf->client = client;
2981
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002982 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002983}
2984
Rafael Antognollie2a34552013-12-03 15:35:45 -02002985static struct shell_surface *
2986create_shell_surface(void *shell, struct weston_surface *surface,
2987 const struct weston_shell_client *client)
2988{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002989 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002990}
2991
Jason Ekstranda7af7042013-10-12 22:38:11 -05002992static struct weston_view *
2993get_primary_view(void *shell, struct shell_surface *shsurf)
2994{
2995 return shsurf->view;
2996}
2997
Tiago Vignattibc052c92012-04-19 16:18:18 +03002998static void
2999shell_get_shell_surface(struct wl_client *client,
3000 struct wl_resource *resource,
3001 uint32_t id,
3002 struct wl_resource *surface_resource)
3003{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003004 struct weston_surface *surface =
3005 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003006 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003007 struct shell_surface *shsurf;
3008
3009 if (get_shell_surface(surface)) {
3010 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003011 WL_DISPLAY_ERROR_INVALID_OBJECT,
3012 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003013 return;
3014 }
3015
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003016 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003017 if (!shsurf) {
3018 wl_resource_post_error(surface_resource,
3019 WL_DISPLAY_ERROR_INVALID_OBJECT,
3020 "surface->configure already set");
3021 return;
3022 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003023
Jason Ekstranda85118c2013-06-27 20:17:02 -05003024 shsurf->resource =
3025 wl_resource_create(client,
3026 &wl_shell_surface_interface, 1, id);
3027 wl_resource_set_implementation(shsurf->resource,
3028 &shell_surface_implementation,
3029 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003030}
3031
Rafael Antognollie2a34552013-12-03 15:35:45 -02003032static bool
3033shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3034{
3035 return wl_resource_instance_of(shsurf->resource,
3036 &wl_shell_surface_interface,
3037 &shell_surface_implementation);
3038}
3039
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003040static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003041 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003042};
3043
Rafael Antognollie2a34552013-12-03 15:35:45 -02003044/****************************
3045 * xdg-shell implementation */
3046
3047static void
3048xdg_surface_destroy(struct wl_client *client,
3049 struct wl_resource *resource)
3050{
3051 wl_resource_destroy(resource);
3052}
3053
3054static void
3055xdg_surface_pong(struct wl_client *client,
3056 struct wl_resource *resource,
3057 uint32_t serial)
3058{
3059 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3060
3061 surface_pong(shsurf, serial);
3062}
3063
3064static void
3065xdg_surface_set_app_id(struct wl_client *client,
3066 struct wl_resource *resource,
3067 const char *app_id)
3068{
3069 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3070
3071 free(shsurf->class);
3072 shsurf->class = strdup(app_id);
3073}
3074
3075static void
3076xdg_surface_set_title(struct wl_client *client,
3077 struct wl_resource *resource, const char *title)
3078{
3079 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3080
3081 set_title(shsurf, title);
3082}
3083
3084static void
3085xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3086 struct wl_resource *seat_resource, uint32_t serial)
3087{
3088 common_surface_move(resource, seat_resource, serial);
3089}
3090
3091static void
3092xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3093 struct wl_resource *seat_resource, uint32_t serial,
3094 uint32_t edges)
3095{
3096 common_surface_resize(resource, seat_resource, serial, edges);
3097}
3098
3099static void
3100xdg_surface_set_output(struct wl_client *client,
3101 struct wl_resource *resource,
3102 struct wl_resource *output_resource)
3103{
3104 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3105 struct weston_output *output;
3106
3107 if (output_resource)
3108 output = wl_resource_get_user_data(output_resource);
3109 else
3110 output = NULL;
3111
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003112 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003113}
3114
3115static void
3116xdg_surface_set_fullscreen(struct wl_client *client,
3117 struct wl_resource *resource)
3118{
3119 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3120
3121 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3122 return;
3123
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003124 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003125 set_fullscreen(shsurf,
3126 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003127 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003128}
3129
3130static void
3131xdg_surface_unset_fullscreen(struct wl_client *client,
3132 struct wl_resource *resource)
3133{
3134 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003135 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003136
3137 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3138 return;
3139
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003140 if (!shsurf->next_state.fullscreen)
3141 return;
3142
Rafael Antognollie2a34552013-12-03 15:35:45 -02003143 shsurf->next_state.fullscreen = false;
3144 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003145
3146 if (shsurf->saved_size_valid) {
3147 width = shsurf->saved_width;
3148 height = shsurf->saved_height;
3149 shsurf->saved_size_valid = false;
3150 } else {
3151 width = shsurf->surface->width;
3152 height = shsurf->surface->height;
3153 }
3154
3155 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003156}
3157
3158static void
3159xdg_surface_set_maximized(struct wl_client *client,
3160 struct wl_resource *resource)
3161{
3162 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3163
3164 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3165 return;
3166
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003167 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003168 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003169}
3170
3171static void
3172xdg_surface_unset_maximized(struct wl_client *client,
3173 struct wl_resource *resource)
3174{
3175 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003176 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003177
3178 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3179 return;
3180
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003181 if (!shsurf->next_state.maximized)
3182 return;
3183
Rafael Antognollie2a34552013-12-03 15:35:45 -02003184 shsurf->next_state.maximized = false;
3185 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003186
3187 if (shsurf->saved_size_valid) {
3188 width = shsurf->saved_width;
3189 height = shsurf->saved_height;
3190 shsurf->saved_size_valid = false;
3191 } else {
3192 width = shsurf->surface->width;
3193 height = shsurf->surface->height;
3194 }
3195
3196 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003197}
3198
3199static const struct xdg_surface_interface xdg_surface_implementation = {
3200 xdg_surface_destroy,
3201 xdg_surface_set_transient_for,
3202 xdg_surface_set_title,
3203 xdg_surface_set_app_id,
3204 xdg_surface_pong,
3205 xdg_surface_move,
3206 xdg_surface_resize,
3207 xdg_surface_set_output,
3208 xdg_surface_set_fullscreen,
3209 xdg_surface_unset_fullscreen,
3210 xdg_surface_set_maximized,
3211 xdg_surface_unset_maximized,
3212 NULL /* set_minimized */
3213};
3214
3215static void
3216xdg_send_configure(struct weston_surface *surface,
3217 uint32_t edges, int32_t width, int32_t height)
3218{
3219 struct shell_surface *shsurf = get_shell_surface(surface);
3220
3221 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3222}
3223
3224static const struct weston_shell_client xdg_client = {
3225 xdg_send_configure
3226};
3227
3228static void
3229xdg_use_unstable_version(struct wl_client *client,
3230 struct wl_resource *resource,
3231 int32_t version)
3232{
3233 if (version > 1) {
3234 wl_resource_post_error(resource,
3235 1,
3236 "xdg-shell:: version not implemented yet.");
3237 return;
3238 }
3239}
3240
3241static struct shell_surface *
3242create_xdg_surface(void *shell, struct weston_surface *surface,
3243 const struct weston_shell_client *client)
3244{
3245 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003246
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003247 shsurf = create_common_surface(shell, surface, client);
3248 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003249
3250 return shsurf;
3251}
3252
3253static void
3254xdg_get_xdg_surface(struct wl_client *client,
3255 struct wl_resource *resource,
3256 uint32_t id,
3257 struct wl_resource *surface_resource)
3258{
3259 struct weston_surface *surface =
3260 wl_resource_get_user_data(surface_resource);
3261 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3262 struct shell_surface *shsurf;
3263
3264 if (get_shell_surface(surface)) {
3265 wl_resource_post_error(surface_resource,
3266 WL_DISPLAY_ERROR_INVALID_OBJECT,
3267 "desktop_shell::get_shell_surface already requested");
3268 return;
3269 }
3270
3271 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3272 if (!shsurf) {
3273 wl_resource_post_error(surface_resource,
3274 WL_DISPLAY_ERROR_INVALID_OBJECT,
3275 "surface->configure already set");
3276 return;
3277 }
3278
3279 shsurf->resource =
3280 wl_resource_create(client,
3281 &xdg_surface_interface, 1, id);
3282 wl_resource_set_implementation(shsurf->resource,
3283 &xdg_surface_implementation,
3284 shsurf, shell_destroy_shell_surface);
3285}
3286
3287static bool
3288shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3289{
3290 return wl_resource_instance_of(shsurf->resource,
3291 &xdg_surface_interface,
3292 &xdg_surface_implementation);
3293}
3294
3295/* xdg-popup implementation */
3296
3297static void
3298xdg_popup_destroy(struct wl_client *client,
3299 struct wl_resource *resource)
3300{
3301 wl_resource_destroy(resource);
3302}
3303
3304static void
3305xdg_popup_pong(struct wl_client *client,
3306 struct wl_resource *resource,
3307 uint32_t serial)
3308{
3309 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3310
3311 surface_pong(shsurf, serial);
3312}
3313
3314static const struct xdg_popup_interface xdg_popup_implementation = {
3315 xdg_popup_destroy,
3316 xdg_popup_pong
3317};
3318
3319static void
3320xdg_popup_send_configure(struct weston_surface *surface,
3321 uint32_t edges, int32_t width, int32_t height)
3322{
3323}
3324
3325static const struct weston_shell_client xdg_popup_client = {
3326 xdg_popup_send_configure
3327};
3328
3329static struct shell_surface *
3330create_xdg_popup(void *shell, struct weston_surface *surface,
3331 const struct weston_shell_client *client,
3332 struct weston_surface *parent,
3333 struct shell_seat *seat,
3334 uint32_t serial,
3335 int32_t x, int32_t y)
3336{
3337 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003338
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003339 shsurf = create_common_surface(shell, surface, client);
3340 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003341 shsurf->popup.shseat = seat;
3342 shsurf->popup.serial = serial;
3343 shsurf->popup.x = x;
3344 shsurf->popup.y = y;
3345 shell_surface_set_parent(shsurf, parent);
3346
3347 return shsurf;
3348}
3349
3350static void
3351xdg_get_xdg_popup(struct wl_client *client,
3352 struct wl_resource *resource,
3353 uint32_t id,
3354 struct wl_resource *surface_resource,
3355 struct wl_resource *parent_resource,
3356 struct wl_resource *seat_resource,
3357 uint32_t serial,
3358 int32_t x, int32_t y, uint32_t flags)
3359{
3360 struct weston_surface *surface =
3361 wl_resource_get_user_data(surface_resource);
3362 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3363 struct shell_surface *shsurf;
3364 struct weston_surface *parent;
3365 struct shell_seat *seat;
3366
3367 if (get_shell_surface(surface)) {
3368 wl_resource_post_error(surface_resource,
3369 WL_DISPLAY_ERROR_INVALID_OBJECT,
3370 "desktop_shell::get_shell_surface already requested");
3371 return;
3372 }
3373
3374 if (!parent_resource) {
3375 wl_resource_post_error(surface_resource,
3376 WL_DISPLAY_ERROR_INVALID_OBJECT,
3377 "xdg_shell::get_xdg_popup requires a parent shell surface");
3378 }
3379
3380 parent = wl_resource_get_user_data(parent_resource);
3381 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3382
3383 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3384 parent, seat, serial, x, y);
3385 if (!shsurf) {
3386 wl_resource_post_error(surface_resource,
3387 WL_DISPLAY_ERROR_INVALID_OBJECT,
3388 "surface->configure already set");
3389 return;
3390 }
3391
3392 shsurf->resource =
3393 wl_resource_create(client,
3394 &xdg_popup_interface, 1, id);
3395 wl_resource_set_implementation(shsurf->resource,
3396 &xdg_popup_implementation,
3397 shsurf, shell_destroy_shell_surface);
3398}
3399
3400static bool
3401shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3402{
3403 return wl_resource_instance_of(shsurf->resource,
3404 &xdg_popup_interface,
3405 &xdg_popup_implementation);
3406}
3407
3408static const struct xdg_shell_interface xdg_implementation = {
3409 xdg_use_unstable_version,
3410 xdg_get_xdg_surface,
3411 xdg_get_xdg_popup
3412};
3413
3414static int
3415xdg_shell_unversioned_dispatch(const void *implementation,
3416 void *_target, uint32_t opcode,
3417 const struct wl_message *message,
3418 union wl_argument *args)
3419{
3420 struct wl_resource *resource = _target;
3421 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3422
3423 if (opcode != 0) {
3424 wl_resource_post_error(resource,
3425 WL_DISPLAY_ERROR_INVALID_OBJECT,
3426 "must call use_unstable_version first");
3427 return 0;
3428 }
3429
3430#define XDG_SERVER_VERSION 1
3431
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003432 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3433 "shell implementation doesn't match protocol version");
3434
Rafael Antognollie2a34552013-12-03 15:35:45 -02003435 if (args[0].i != XDG_SERVER_VERSION) {
3436 wl_resource_post_error(resource,
3437 WL_DISPLAY_ERROR_INVALID_OBJECT,
3438 "incompatible version, server is %d "
3439 "client wants %d",
3440 XDG_SERVER_VERSION, args[0].i);
3441 return 0;
3442 }
3443
3444 wl_resource_set_implementation(resource, &xdg_implementation,
3445 shell, NULL);
3446
3447 return 1;
3448}
3449
3450/* end of xdg-shell implementation */
3451/***********************************/
3452
Kristian Høgsberg07937562011-04-12 17:25:42 -04003453static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003454shell_fade(struct desktop_shell *shell, enum fade_type type);
3455
3456static int
3457screensaver_timeout(void *data)
3458{
3459 struct desktop_shell *shell = data;
3460
3461 shell_fade(shell, FADE_OUT);
3462
3463 return 1;
3464}
3465
3466static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003467handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003468{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003469 struct desktop_shell *shell =
3470 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003471
Pekka Paalanen18027e52011-12-02 16:31:49 +02003472 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003473
3474 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003475 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003476}
3477
3478static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003479launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003480{
3481 if (shell->screensaver.binding)
3482 return;
3483
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003484 if (!shell->screensaver.path) {
3485 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003486 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003487 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003488
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003489 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003490 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003491 return;
3492 }
3493
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003494 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003495 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003496 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003497 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003498}
3499
3500static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003501terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003502{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003503 if (shell->screensaver.process.pid == 0)
3504 return;
3505
3506 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003507}
3508
3509static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003510configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003511{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003512 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003513
Jason Ekstranda7af7042013-10-12 22:38:11 -05003514 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3515 if (v->output == ev->output && v != ev) {
3516 weston_view_unmap(v);
3517 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003518 }
3519 }
3520
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003521 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003522
Jason Ekstranda7af7042013-10-12 22:38:11 -05003523 if (wl_list_empty(&ev->layer_link)) {
3524 wl_list_insert(&layer->view_list, &ev->layer_link);
3525 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003526 }
3527}
3528
3529static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003530background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003531{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003532 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003534
Jason Ekstranda7af7042013-10-12 22:38:11 -05003535 view = container_of(es->views.next, struct weston_view, surface_link);
3536
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003537 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003538}
3539
3540static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003541desktop_shell_set_background(struct wl_client *client,
3542 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003543 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003544 struct wl_resource *surface_resource)
3545{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003546 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003547 struct weston_surface *surface =
3548 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003549 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003550
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003551 if (surface->configure) {
3552 wl_resource_post_error(surface_resource,
3553 WL_DISPLAY_ERROR_INVALID_OBJECT,
3554 "surface role already assigned");
3555 return;
3556 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003557
Jason Ekstranda7af7042013-10-12 22:38:11 -05003558 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3559 weston_view_destroy(view);
3560 view = weston_view_create(surface);
3561
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003562 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003563 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003564 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003565 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003566 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003567 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003568 surface->output->width,
3569 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003570}
3571
3572static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003573panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003574{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003575 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003576 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003577
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578 view = container_of(es->views.next, struct weston_view, surface_link);
3579
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003580 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003581}
3582
3583static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003584desktop_shell_set_panel(struct wl_client *client,
3585 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003586 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003587 struct wl_resource *surface_resource)
3588{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003589 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003590 struct weston_surface *surface =
3591 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003593
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003594 if (surface->configure) {
3595 wl_resource_post_error(surface_resource,
3596 WL_DISPLAY_ERROR_INVALID_OBJECT,
3597 "surface role already assigned");
3598 return;
3599 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003600
Jason Ekstranda7af7042013-10-12 22:38:11 -05003601 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3602 weston_view_destroy(view);
3603 view = weston_view_create(surface);
3604
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003605 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003606 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003607 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003608 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003609 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003610 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003611 surface->output->width,
3612 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003613}
3614
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003615static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003616lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003617{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003618 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003619 struct weston_view *view;
3620
3621 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003622
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003623 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003624 return;
3625
Jason Ekstranda7af7042013-10-12 22:38:11 -05003626 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003627
3628 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003629 wl_list_insert(&shell->lock_layer.view_list,
3630 &view->layer_link);
3631 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003632 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003633 }
3634}
3635
3636static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003637handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003638{
Tiago Vignattibe143262012-04-16 17:31:41 +03003639 struct desktop_shell *shell =
3640 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003641
Martin Minarik6d118362012-06-07 18:01:59 +02003642 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003643 shell->lock_surface = NULL;
3644}
3645
3646static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003647desktop_shell_set_lock_surface(struct wl_client *client,
3648 struct wl_resource *resource,
3649 struct wl_resource *surface_resource)
3650{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003651 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003652 struct weston_surface *surface =
3653 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003654
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003655 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003656
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003657 if (!shell->locked)
3658 return;
3659
Pekka Paalanen98262232011-12-01 10:42:22 +02003660 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003661
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003662 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003663 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003664 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003665
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003666 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003667 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003668 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003669}
3670
3671static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003672resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003673{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003674 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003675
Pekka Paalanen77346a62011-11-30 16:26:35 +02003676 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003677
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003678 wl_list_remove(&shell->lock_layer.link);
3679 wl_list_insert(&shell->compositor->cursor_layer.link,
3680 &shell->fullscreen_layer.link);
3681 wl_list_insert(&shell->fullscreen_layer.link,
3682 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003683 if (shell->showing_input_panels) {
3684 wl_list_insert(&shell->panel_layer.link,
3685 &shell->input_panel_layer.link);
3686 wl_list_insert(&shell->input_panel_layer.link,
3687 &ws->layer.link);
3688 } else {
3689 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3690 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003691
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003692 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003693
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003694 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003695 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003696 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003697}
3698
3699static void
3700desktop_shell_unlock(struct wl_client *client,
3701 struct wl_resource *resource)
3702{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003703 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003704
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003705 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003706
3707 if (shell->locked)
3708 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003709}
3710
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003711static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003712desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003713 struct wl_resource *resource,
3714 struct wl_resource *surface_resource)
3715{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003716 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003717
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003718 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003719 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003720}
3721
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003722static void
3723desktop_shell_desktop_ready(struct wl_client *client,
3724 struct wl_resource *resource)
3725{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003726 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003727
3728 shell_fade_startup(shell);
3729}
3730
Kristian Høgsberg75840622011-09-06 13:48:16 -04003731static const struct desktop_shell_interface desktop_shell_implementation = {
3732 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003733 desktop_shell_set_panel,
3734 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003735 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003736 desktop_shell_set_grab_surface,
3737 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003738};
3739
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003740static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003741get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003742{
3743 struct shell_surface *shsurf;
3744
3745 shsurf = get_shell_surface(surface);
3746 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003747 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003748 return shsurf->type;
3749}
3750
Kristian Høgsberg75840622011-09-06 13:48:16 -04003751static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003752move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003753{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003754 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003755 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003756 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003757
Pekka Paalanen01388e22013-04-25 13:57:44 +03003758 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003759 if (surface == NULL)
3760 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003761
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003762 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003763 if (shsurf == NULL || shsurf->state.fullscreen ||
3764 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003765 return;
3766
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003767 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003768}
3769
3770static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003771maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3772{
3773 struct weston_surface *focus = seat->pointer->focus->surface;
3774 struct weston_surface *surface;
3775 struct shell_surface *shsurf;
3776
3777 surface = weston_surface_get_main_surface(focus);
3778 if (surface == NULL)
3779 return;
3780
3781 shsurf = get_shell_surface(surface);
3782 if (shsurf == NULL)
3783 return;
3784
3785 if (!shell_surface_is_xdg_surface(shsurf))
3786 return;
3787
3788 if (shsurf->state.maximized)
3789 xdg_surface_send_request_unset_maximized(shsurf->resource);
3790 else
3791 xdg_surface_send_request_set_maximized(shsurf->resource);
3792}
3793
3794static void
3795fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3796{
3797 struct weston_surface *focus = seat->pointer->focus->surface;
3798 struct weston_surface *surface;
3799 struct shell_surface *shsurf;
3800
3801 surface = weston_surface_get_main_surface(focus);
3802 if (surface == NULL)
3803 return;
3804
3805 shsurf = get_shell_surface(surface);
3806 if (shsurf == NULL)
3807 return;
3808
3809 if (!shell_surface_is_xdg_surface(shsurf))
3810 return;
3811
3812 if (shsurf->state.fullscreen)
3813 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3814 else
3815 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3816}
3817
3818static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003819touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3820{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003821 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003822 struct weston_surface *surface;
3823 struct shell_surface *shsurf;
3824
3825 surface = weston_surface_get_main_surface(focus);
3826 if (surface == NULL)
3827 return;
3828
3829 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003830 if (shsurf == NULL || shsurf->state.fullscreen ||
3831 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003832 return;
3833
3834 surface_touch_move(shsurf, (struct weston_seat *) seat);
3835}
3836
3837static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003838resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003839{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003840 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003841 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003842 uint32_t edges = 0;
3843 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003844 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003845
Pekka Paalanen01388e22013-04-25 13:57:44 +03003846 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003847 if (surface == NULL)
3848 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003849
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003850 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003851 if (shsurf == NULL || shsurf->state.fullscreen ||
3852 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003853 return;
3854
Jason Ekstranda7af7042013-10-12 22:38:11 -05003855 weston_view_from_global(shsurf->view,
3856 wl_fixed_to_int(seat->pointer->grab_x),
3857 wl_fixed_to_int(seat->pointer->grab_y),
3858 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003859
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003860 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003861 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003862 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003863 edges |= 0;
3864 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003865 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003866
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003867 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003868 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003869 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003870 edges |= 0;
3871 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003872 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003873
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003874 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003875}
3876
3877static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003878surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003879 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003880{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003881 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003882 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003883 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003884 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003885
Pekka Paalanen01388e22013-04-25 13:57:44 +03003886 /* XXX: broken for windows containing sub-surfaces */
3887 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003888 if (surface == NULL)
3889 return;
3890
3891 shsurf = get_shell_surface(surface);
3892 if (!shsurf)
3893 return;
3894
Jason Ekstranda7af7042013-10-12 22:38:11 -05003895 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003896
Jason Ekstranda7af7042013-10-12 22:38:11 -05003897 if (shsurf->view->alpha > 1.0)
3898 shsurf->view->alpha = 1.0;
3899 if (shsurf->view->alpha < step)
3900 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003901
Jason Ekstranda7af7042013-10-12 22:38:11 -05003902 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003903 weston_surface_damage(surface);
3904}
3905
3906static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003907do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003908 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003909{
Daniel Stone37816df2012-05-16 18:45:18 +01003910 struct weston_seat *ws = (struct weston_seat *) seat;
3911 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003912 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003913 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003914
3915 wl_list_for_each(output, &compositor->output_list, link) {
3916 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003917 wl_fixed_to_double(seat->pointer->x),
3918 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003919 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003920 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003921 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003922 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003923 increment = -output->zoom.increment;
3924 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003925 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003926 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003927 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003928 else
3929 increment = 0;
3930
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003931 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003932
Scott Moreaue6603982012-06-11 13:07:51 -06003933 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003934 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003935 else if (output->zoom.level > output->zoom.max_level)
3936 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003937 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003938 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003939 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003940
Scott Moreaue6603982012-06-11 13:07:51 -06003941 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003942
Jason Ekstranda7af7042013-10-12 22:38:11 -05003943 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003944 }
3945 }
3946}
3947
Scott Moreauccbf29d2012-02-22 14:21:41 -07003948static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003949zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003950 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003951{
3952 do_zoom(seat, time, 0, axis, value);
3953}
3954
3955static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003956zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003957 void *data)
3958{
3959 do_zoom(seat, time, key, 0, 0);
3960}
3961
3962static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003963terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003964 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003965{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003966 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003967
Daniel Stone325fc2d2012-05-30 16:31:58 +01003968 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003969}
3970
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003971struct alt_tab {
3972 struct desktop_shell *shell;
3973 struct weston_keyboard_grab grab;
3974
3975 struct wl_list *current;
3976
3977 struct wl_list preview_list;
3978};
3979
3980struct alt_tab_preview {
3981 struct alt_tab *alt_tab;
3982
3983 struct weston_view *view;
3984 struct weston_transform transform;
3985
3986 struct wl_listener listener;
3987
3988 struct wl_list link;
3989};
3990
3991static void
3992alt_tab_next(struct alt_tab *alt_tab)
3993{
3994 alt_tab->current = alt_tab->current->next;
3995
3996 /* Make sure we're not pointing to the list header e.g. after
3997 * cycling through the whole list. */
3998 if (alt_tab->current->next == alt_tab->preview_list.next)
3999 alt_tab->current = alt_tab->current->next;
4000}
4001
4002static void
4003alt_tab_destroy(struct alt_tab *alt_tab)
4004{
4005 struct alt_tab_preview *preview, *next;
4006 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
4007
4008 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
4009 preview = wl_container_of(alt_tab->current, preview, link);
4010
4011 activate(alt_tab->shell, preview->view->surface,
4012 (struct weston_seat *) keyboard->seat);
4013 }
4014
4015 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
4016 wl_list_remove(&preview->link);
4017 wl_list_remove(&preview->listener.link);
4018 weston_view_destroy(preview->view);
4019 free(preview);
4020 }
4021
4022 weston_keyboard_end_grab(keyboard);
4023 if (keyboard->input_method_resource)
4024 keyboard->grab = &keyboard->input_method_grab;
4025
4026 free(alt_tab);
4027}
4028
4029static void
4030alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
4031{
4032 struct alt_tab_preview *preview =
4033 container_of(listener, struct alt_tab_preview, listener);
4034 struct alt_tab *alt_tab = preview->alt_tab;
4035 int advance = 0;
4036
4037 /* If the preview that we're removing is the currently selected one,
4038 * we want to move to the next one. So we move to ->prev and then
4039 * call _next() after removing the preview. */
4040 if (alt_tab->current == &preview->link) {
4041 alt_tab->current = alt_tab->current->prev;
4042 advance = 1;
4043 }
4044
4045 wl_list_remove(&preview->listener.link);
4046 wl_list_remove(&preview->link);
4047
4048 free(preview);
4049
4050 if (advance)
4051 alt_tab_next(alt_tab);
4052
4053 /* If the last preview goes away, stop the alt-tab */
4054 if (wl_list_empty(alt_tab->current))
4055 alt_tab_destroy(alt_tab);
4056}
4057
4058static void
4059alt_tab_key(struct weston_keyboard_grab *grab,
4060 uint32_t time, uint32_t key, uint32_t state_w)
4061{
4062 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4063 enum wl_keyboard_key_state state = state_w;
4064
4065 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4066 alt_tab_next(alt_tab);
4067}
4068
4069static void
4070alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4071 uint32_t mods_depressed, uint32_t mods_latched,
4072 uint32_t mods_locked, uint32_t group)
4073{
4074 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4075 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
4076
4077 if ((seat->modifier_state & MODIFIER_ALT) == 0)
4078 alt_tab_destroy(alt_tab);
4079}
4080
4081static void
4082alt_tab_cancel(struct weston_keyboard_grab *grab)
4083{
4084 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4085
4086 alt_tab_destroy(alt_tab);
4087}
4088
4089static const struct weston_keyboard_grab_interface alt_tab_grab = {
4090 alt_tab_key,
4091 alt_tab_modifier,
4092 alt_tab_cancel,
4093};
4094
4095static int
4096view_for_alt_tab(struct weston_view *view)
4097{
Rafael Antognollied207b42013-12-03 15:35:43 -02004098 struct shell_surface *shsurf = get_shell_surface(view->surface);
4099 if (!shsurf)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004100 return 0;
4101
Rafael Antognollied207b42013-12-03 15:35:43 -02004102 if (shsurf->parent)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004103 return 0;
4104
4105 if (view != get_default_view(view->surface))
4106 return 0;
4107
4108 return 1;
4109}
4110
4111static void
4112alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4113 void *data)
4114{
4115 struct alt_tab *alt_tab;
4116 struct desktop_shell *shell = data;
4117 struct weston_output *output = get_default_output(shell->compositor);
4118 struct workspace *ws;
4119 struct weston_view *view;
4120 int num_surfaces = 0;
4121 int x, y, side, margin;
4122
4123 wl_list_for_each(view, &shell->compositor->view_list, link) {
4124
4125 if (view_for_alt_tab(view))
4126 num_surfaces++;
4127 }
4128
4129 if (!num_surfaces)
4130 return;
4131
4132 alt_tab = malloc(sizeof *alt_tab);
4133 if (!alt_tab)
4134 return;
4135
4136 alt_tab->shell = shell;
4137 wl_list_init(&alt_tab->preview_list);
4138 alt_tab->current = &alt_tab->preview_list;
4139
4140 restore_all_output_modes(shell->compositor);
4141 lower_fullscreen_layer(alt_tab->shell);
4142
4143 alt_tab->grab.interface = &alt_tab_grab;
4144 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
4145 weston_keyboard_set_focus(seat->keyboard, NULL);
4146
4147 ws = get_current_workspace(shell);
4148
4149 /* FIXME: add some visual candy e.g. prelight the selected view
4150 * and/or add a black surrounding background à la gnome-shell */
4151
4152 /* Determine the size for each preview */
4153 side = output->width / num_surfaces;
4154 if (side > 200)
4155 side = 200;
4156 margin = side / 4;
4157 side -= margin;
4158
4159 x = margin;
4160 y = (output->height - side) / 2;
4161
4162 /* Create a view for each surface */
4163 wl_list_for_each(view, &shell->compositor->view_list, link) {
4164 struct alt_tab_preview *preview;
4165 struct weston_view *v;
4166 float scale;
4167
4168 if (!view_for_alt_tab(view))
4169 continue;
4170
4171 preview = malloc(sizeof *preview);
4172 if (!preview) {
4173 alt_tab_destroy(alt_tab);
4174 return;
4175 }
4176
4177 preview->alt_tab = alt_tab;
4178
4179 preview->view = v = weston_view_create(view->surface);
4180 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004181
4182 wl_list_remove(&v->layer_link);
4183 wl_list_insert(&ws->layer.view_list, &v->layer_link);
4184 weston_view_damage_below(v);
4185 weston_surface_damage(v->surface);
4186
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004187 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004188
4189 preview->listener.notify = alt_tab_handle_surface_destroy;
4190 wl_signal_add(&v->destroy_signal, &preview->listener);
4191
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004192 if (view->surface->width > view->surface->height)
4193 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004194 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004195 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004196
4197 wl_list_insert(&v->geometry.transformation_list,
4198 &preview->transform.link);
4199 weston_matrix_init(&preview->transform.matrix);
4200 weston_matrix_scale(&preview->transform.matrix,
4201 scale, scale, 1.0f);
4202
4203 weston_view_geometry_dirty(v);
4204 weston_compositor_schedule_repaint(v->surface->compositor);
4205
4206 /* Insert at the end of the list */
4207 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
4208
4209 x += side + margin;
4210 }
4211
4212 /* Start at the second preview so a simple <alt>tab changes window.
4213 * We set `current' to the first preview and not the second because
4214 * we're going to receive a key press callback for the initial
4215 * <alt>tab which will make `current' point to the second element. */
4216 alt_tab_next(alt_tab);
4217}
4218
4219static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004220rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4221 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004222{
4223 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004224 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004225 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004226 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004227 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004228
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004229 weston_pointer_move(pointer, x, y);
4230
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004231 if (!shsurf)
4232 return;
4233
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004234 cx = 0.5f * shsurf->surface->width;
4235 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004236
Daniel Stone37816df2012-05-16 18:45:18 +01004237 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4238 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004239 r = sqrtf(dx * dx + dy * dy);
4240
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004241 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004242 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004243
4244 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004245 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004246 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004247
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004248 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004249 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004250
4251 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004252 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004253 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004254 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004255 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004256
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004257 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004258 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004259 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004260 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004261 wl_list_init(&shsurf->rotation.transform.link);
4262 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004263 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004264 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004265
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004266 /* We need to adjust the position of the surface
4267 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268 cposx = shsurf->view->geometry.x + cx;
4269 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004270 dposx = rotate->center.x - cposx;
4271 dposy = rotate->center.y - cposy;
4272 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004273 weston_view_set_position(shsurf->view,
4274 shsurf->view->geometry.x + dposx,
4275 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004276 }
4277
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004278 /* Repaint implies weston_surface_update_transform(), which
4279 * lazily applies the damage due to rotation update.
4280 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004281 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004282}
4283
4284static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004285rotate_grab_button(struct weston_pointer_grab *grab,
4286 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004287{
4288 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004289 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004290 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004291 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004292 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004293
Daniel Stone4dbadb12012-05-30 16:31:51 +01004294 if (pointer->button_count == 0 &&
4295 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004296 if (shsurf)
4297 weston_matrix_multiply(&shsurf->rotation.rotation,
4298 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004299 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004300 free(rotate);
4301 }
4302}
4303
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004304static void
4305rotate_grab_cancel(struct weston_pointer_grab *grab)
4306{
4307 struct rotate_grab *rotate =
4308 container_of(grab, struct rotate_grab, base.grab);
4309
4310 shell_grab_end(&rotate->base);
4311 free(rotate);
4312}
4313
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004314static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004315 noop_grab_focus,
4316 rotate_grab_motion,
4317 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004318 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004319};
4320
4321static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004322surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004323{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004324 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004325 float dx, dy;
4326 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004327
Pekka Paalanen460099f2012-01-20 16:48:25 +02004328 rotate = malloc(sizeof *rotate);
4329 if (!rotate)
4330 return;
4331
Jason Ekstranda7af7042013-10-12 22:38:11 -05004332 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004333 surface->surface->width * 0.5f,
4334 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004335 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004336
Daniel Stone37816df2012-05-16 18:45:18 +01004337 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4338 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004339 r = sqrtf(dx * dx + dy * dy);
4340 if (r > 20.0f) {
4341 struct weston_matrix inverse;
4342
4343 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004344 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004345 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004346
4347 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004348 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004349 } else {
4350 weston_matrix_init(&surface->rotation.rotation);
4351 weston_matrix_init(&rotate->rotation);
4352 }
4353
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004354 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4355 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004356}
4357
4358static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004359rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004360 void *data)
4361{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004362 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004363 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004364 struct shell_surface *surface;
4365
Pekka Paalanen01388e22013-04-25 13:57:44 +03004366 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004367 if (base_surface == NULL)
4368 return;
4369
4370 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004371 if (surface == NULL || surface->state.fullscreen ||
4372 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004373 return;
4374
4375 surface_rotate(surface, seat);
4376}
4377
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004378/* Move all fullscreen layers down to the current workspace in a non-reversible
4379 * manner. This should be used when implementing shell-wide overlays, such as
4380 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004381void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004382lower_fullscreen_layer(struct desktop_shell *shell)
4383{
4384 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004385 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004386
4387 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 wl_list_for_each_reverse_safe(view, prev,
4389 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004390 layer_link) {
4391 wl_list_remove(&view->layer_link);
4392 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4393 weston_view_damage_below(view);
4394 weston_surface_damage(view->surface);
4395 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004396}
4397
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004398void
Tiago Vignattibe143262012-04-16 17:31:41 +03004399activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004400 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004401{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004402 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004403 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004404 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004405 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004406 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004407
Pekka Paalanen01388e22013-04-25 13:57:44 +03004408 main_surface = weston_surface_get_main_surface(es);
4409
Daniel Stone37816df2012-05-16 18:45:18 +01004410 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004411
Jonas Ådahl8538b222012-08-29 22:13:03 +02004412 state = ensure_focus_state(shell, seat);
4413 if (state == NULL)
4414 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004415
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004416 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004417 state->keyboard_focus = es;
4418 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004419 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004420
Rafael Antognolli03b16592013-12-03 15:35:42 -02004421 shsurf = get_shell_surface(main_surface);
4422 if (shsurf->state.fullscreen)
4423 shell_configure_fullscreen(shsurf);
4424 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004425 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004426
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004427 if (shell->focus_animation_type != ANIMATION_NONE) {
4428 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004429 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004430 }
4431
4432 /* Update the surface’s layer. This brings it to the top of the stacking
4433 * order as appropriate. */
4434 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004435}
4436
Alex Wu21858432012-04-01 20:13:08 +08004437/* no-op func for checking black surface */
4438static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004439black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004440{
4441}
4442
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004443static bool
Alex Wu21858432012-04-01 20:13:08 +08004444is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4445{
4446 if (es->configure == black_surface_configure) {
4447 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004448 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004449 return true;
4450 }
4451 return false;
4452}
4453
Kristian Høgsberg75840622011-09-06 13:48:16 -04004454static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004455activate_binding(struct weston_seat *seat,
4456 struct desktop_shell *shell,
4457 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004458{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004459 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004460
Alex Wu9c35e6b2012-03-05 14:13:13 +08004461 if (!focus)
4462 return;
4463
Pekka Paalanen01388e22013-04-25 13:57:44 +03004464 if (is_black_surface(focus, &main_surface))
4465 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004466
Pekka Paalanen01388e22013-04-25 13:57:44 +03004467 main_surface = weston_surface_get_main_surface(focus);
4468 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004469 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004470
Neil Robertsa28c6932013-10-03 16:43:04 +01004471 activate(shell, focus, seat);
4472}
4473
4474static void
4475click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4476 void *data)
4477{
4478 if (seat->pointer->grab != &seat->pointer->default_grab)
4479 return;
4480
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004481 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004482}
4483
4484static void
4485touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4486{
4487 if (seat->touch->grab != &seat->touch->default_grab)
4488 return;
4489
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004490 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004491}
4492
4493static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004494lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004495{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004496 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004497
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004498 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004499 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004500 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004501 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004502
4503 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004504
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004505 /* Hide all surfaces by removing the fullscreen, panel and
4506 * toplevel layers. This way nothing else can show or receive
4507 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004508
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004509 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004510 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004511 if (shell->showing_input_panels)
4512 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004513 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004514 wl_list_insert(&shell->compositor->cursor_layer.link,
4515 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004516
Pekka Paalanen77346a62011-11-30 16:26:35 +02004517 launch_screensaver(shell);
4518
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004519 /* TODO: disable bindings that should not work while locked. */
4520
4521 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004522}
4523
4524static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004525unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004526{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004527 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004528 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004529 return;
4530 }
4531
4532 /* If desktop-shell client has gone away, unlock immediately. */
4533 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004534 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004535 return;
4536 }
4537
4538 if (shell->prepare_event_sent)
4539 return;
4540
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004541 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004542 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004543}
4544
4545static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004546shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004547{
4548 struct desktop_shell *shell = data;
4549
4550 shell->fade.animation = NULL;
4551
4552 switch (shell->fade.type) {
4553 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004554 weston_surface_destroy(shell->fade.view->surface);
4555 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004556 break;
4557 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004558 lock(shell);
4559 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004560 default:
4561 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004562 }
4563}
4564
Jason Ekstranda7af7042013-10-12 22:38:11 -05004565static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004566shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004567{
4568 struct weston_compositor *compositor = shell->compositor;
4569 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004570 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004571
4572 surface = weston_surface_create(compositor);
4573 if (!surface)
4574 return NULL;
4575
Jason Ekstranda7af7042013-10-12 22:38:11 -05004576 view = weston_view_create(surface);
4577 if (!view) {
4578 weston_surface_destroy(surface);
4579 return NULL;
4580 }
4581
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004582 surface->width = 8192;
4583 surface->height = 8192;
4584 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004585 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004586 wl_list_insert(&compositor->fade_layer.view_list,
4587 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004588 pixman_region32_init(&surface->input);
4589
Jason Ekstranda7af7042013-10-12 22:38:11 -05004590 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004591}
4592
4593static void
4594shell_fade(struct desktop_shell *shell, enum fade_type type)
4595{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004596 float tint;
4597
4598 switch (type) {
4599 case FADE_IN:
4600 tint = 0.0;
4601 break;
4602 case FADE_OUT:
4603 tint = 1.0;
4604 break;
4605 default:
4606 weston_log("shell: invalid fade type\n");
4607 return;
4608 }
4609
4610 shell->fade.type = type;
4611
Jason Ekstranda7af7042013-10-12 22:38:11 -05004612 if (shell->fade.view == NULL) {
4613 shell->fade.view = shell_fade_create_surface(shell);
4614 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004615 return;
4616
Jason Ekstranda7af7042013-10-12 22:38:11 -05004617 shell->fade.view->alpha = 1.0 - tint;
4618 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004619 }
4620
4621 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004622 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004623 else
4624 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004625 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004626 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004627 shell_fade_done, shell);
4628}
4629
4630static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004631do_shell_fade_startup(void *data)
4632{
4633 struct desktop_shell *shell = data;
4634
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004635 if (shell->startup_animation_type == ANIMATION_FADE)
4636 shell_fade(shell, FADE_IN);
4637 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004638 weston_surface_destroy(shell->fade.view->surface);
4639 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004640 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004641}
4642
4643static void
4644shell_fade_startup(struct desktop_shell *shell)
4645{
4646 struct wl_event_loop *loop;
4647
4648 if (!shell->fade.startup_timer)
4649 return;
4650
4651 wl_event_source_remove(shell->fade.startup_timer);
4652 shell->fade.startup_timer = NULL;
4653
4654 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4655 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4656}
4657
4658static int
4659fade_startup_timeout(void *data)
4660{
4661 struct desktop_shell *shell = data;
4662
4663 shell_fade_startup(shell);
4664 return 0;
4665}
4666
4667static void
4668shell_fade_init(struct desktop_shell *shell)
4669{
4670 /* Make compositor output all black, and wait for the desktop-shell
4671 * client to signal it is ready, then fade in. The timer triggers a
4672 * fade-in, in case the desktop-shell client takes too long.
4673 */
4674
4675 struct wl_event_loop *loop;
4676
Jason Ekstranda7af7042013-10-12 22:38:11 -05004677 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004678 weston_log("%s: warning: fade surface already exists\n",
4679 __func__);
4680 return;
4681 }
4682
Jason Ekstranda7af7042013-10-12 22:38:11 -05004683 shell->fade.view = shell_fade_create_surface(shell);
4684 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004685 return;
4686
Jason Ekstranda7af7042013-10-12 22:38:11 -05004687 weston_view_update_transform(shell->fade.view);
4688 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004689
4690 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4691 shell->fade.startup_timer =
4692 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4693 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4694}
4695
4696static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004697idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004698{
4699 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004700 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004701
4702 shell_fade(shell, FADE_OUT);
4703 /* lock() is called from shell_fade_done() */
4704}
4705
4706static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004707wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004708{
4709 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004710 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004711
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004712 unlock(shell);
4713}
4714
4715static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004716center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004717{
Giulio Camuffob8366642013-04-25 13:57:46 +03004718 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004719 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004720
Jason Ekstranda7af7042013-10-12 22:38:11 -05004721 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004722
4723 x = output->x + (output->width - width) / 2 - surf_x / 2;
4724 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004725
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004726 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004727}
4728
4729static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004730weston_view_set_initial_position(struct weston_view *view,
4731 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004732{
4733 struct weston_compositor *compositor = shell->compositor;
4734 int ix = 0, iy = 0;
4735 int range_x, range_y;
4736 int dx, dy, x, y, panel_height;
4737 struct weston_output *output, *target_output = NULL;
4738 struct weston_seat *seat;
4739
4740 /* As a heuristic place the new window on the same output as the
4741 * pointer. Falling back to the output containing 0, 0.
4742 *
4743 * TODO: Do something clever for touch too?
4744 */
4745 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004746 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004747 ix = wl_fixed_to_int(seat->pointer->x);
4748 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004749 break;
4750 }
4751 }
4752
4753 wl_list_for_each(output, &compositor->output_list, link) {
4754 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4755 target_output = output;
4756 break;
4757 }
4758 }
4759
4760 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 weston_view_set_position(view, 10 + random() % 400,
4762 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004763 return;
4764 }
4765
4766 /* Valid range within output where the surface will still be onscreen.
4767 * If this is negative it means that the surface is bigger than
4768 * output.
4769 */
4770 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004771 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004772 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004773 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004774
Rob Bradford4cb88c72012-08-13 15:18:44 +01004775 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004776 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004777 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004778 dx = 0;
4779
4780 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004781 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004782 else
4783 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004784
4785 x = target_output->x + dx;
4786 y = target_output->y + dy;
4787
Jason Ekstranda7af7042013-10-12 22:38:11 -05004788 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004789}
4790
4791static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004792map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004793 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004794{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004795 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004796 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004797 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004798 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004799
Pekka Paalanen77346a62011-11-30 16:26:35 +02004800 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004801 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004802 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004803 if (shsurf->state.fullscreen) {
4804 center_on_output(shsurf->view, shsurf->fullscreen_output);
4805 shell_map_fullscreen(shsurf);
4806 } else if (shsurf->state.maximized) {
4807 /* use surface configure to set the geometry */
4808 panel_height = get_output_panel_height(shell, shsurf->output);
4809 surface_subsurfaces_boundingbox(shsurf->surface,
4810 &surf_x, &surf_y, NULL, NULL);
4811 weston_view_set_position(shsurf->view,
4812 shsurf->output->x - surf_x,
4813 shsurf->output->y +
4814 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004815 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004816 weston_view_set_initial_position(shsurf->view, shell);
4817 }
Juan Zhao96879df2012-02-07 08:45:41 +08004818 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004819 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004820 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004821 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004822 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004823 weston_view_set_position(shsurf->view,
4824 shsurf->view->geometry.x + sx,
4825 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004826 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004827 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004828 default:
4829 ;
4830 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004831
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004832 /* Surface stacking order, see also activate(). */
4833 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004834
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 if (shsurf->type != SHELL_SURFACE_NONE) {
4836 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004837 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004838 shsurf->surface->output = shsurf->output;
4839 shsurf->view->output = shsurf->output;
4840 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004841 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004842
Rafael Antognollied207b42013-12-03 15:35:43 -02004843 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4844 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4845 }
4846
Jason Ekstranda7af7042013-10-12 22:38:11 -05004847 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004848 /* XXX: xwayland's using the same fields for transient type */
4849 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004850 if (shsurf->transient.flags ==
4851 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4852 break;
4853 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004854 if (shsurf->state.relative &&
4855 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4856 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004857 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004858 break;
4859 wl_list_for_each(seat, &compositor->seat_list, link)
4860 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004861 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004862 case SHELL_SURFACE_POPUP:
4863 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004864 default:
4865 break;
4866 }
4867
Rafael Antognolli03b16592013-12-03 15:35:42 -02004868 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4869 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004870 {
4871 switch (shell->win_animation_type) {
4872 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004873 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004874 break;
4875 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004876 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004877 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004878 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004879 default:
4880 break;
4881 }
4882 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004883}
4884
4885static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004886configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004887 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004888{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004889 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004890 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004891 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004892
Pekka Paalanen77346a62011-11-30 16:26:35 +02004893 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004894
Rafael Antognolli03b16592013-12-03 15:35:42 -02004895 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004896 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004897 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004898 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004899 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4900 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004901 mx = shsurf->output->x - surf_x;
4902 my = shsurf->output->y +
4903 get_output_panel_height(shell,shsurf->output) - surf_y;
4904 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004905 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004906 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004907 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004908
Alex Wu4539b082012-03-01 12:57:46 +08004909 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004910 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004911 wl_list_for_each(view, &surface->views, surface_link)
4912 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004913
Rafael Antognolli03b16592013-12-03 15:35:42 -02004914 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004915 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004916 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004917}
4918
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004919static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004920shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004921{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004922 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004923 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004924
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004925 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004926
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004927 if (!weston_surface_is_mapped(es) &&
4928 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004929 remove_popup_grab(shsurf);
4930 }
4931
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004932 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004933 return;
4934
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004935 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004936 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004937 type_changed = 1;
4938 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004939
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004940 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004941 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004942 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004943 shsurf->last_width != es->width ||
4944 shsurf->last_height != es->height) {
4945 shsurf->last_width = es->width;
4946 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004947 float from_x, from_y;
4948 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004949
Jason Ekstranda7af7042013-10-12 22:38:11 -05004950 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4951 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004952 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004953 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004954 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004955 }
4956}
4957
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004958static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004959
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004960static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004961desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004962{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004963 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004964 struct desktop_shell *shell =
4965 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004966
4967 shell->child.process.pid = 0;
4968 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004969
4970 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4971 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004972 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004973 shell->child.deathstamp = time;
4974 shell->child.deathcount = 0;
4975 }
4976
4977 shell->child.deathcount++;
4978 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004979 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004980 return;
4981 }
4982
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004983 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004984 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004985 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004986}
4987
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004988static void
4989launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004990{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004991 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004992
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004993 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004994 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004995 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004996 desktop_shell_sigchld);
4997
4998 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004999 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005000}
5001
5002static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005003bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5004{
Tiago Vignattibe143262012-04-16 17:31:41 +03005005 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005006 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005007
Jason Ekstranda85118c2013-06-27 20:17:02 -05005008 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
5009 if (resource)
5010 wl_resource_set_implementation(resource, &shell_implementation,
5011 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005012}
5013
Kristian Høgsberg75840622011-09-06 13:48:16 -04005014static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02005015bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5016{
5017 struct desktop_shell *shell = data;
5018 struct wl_resource *resource;
5019
5020 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
5021 if (resource)
5022 wl_resource_set_dispatcher(resource,
5023 xdg_shell_unversioned_dispatch,
5024 NULL, shell, NULL);
5025}
5026
5027static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005028unbind_desktop_shell(struct wl_resource *resource)
5029{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005030 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005031
5032 if (shell->locked)
5033 resume_desktop(shell);
5034
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005035 shell->child.desktop_shell = NULL;
5036 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005037}
5038
5039static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005040bind_desktop_shell(struct wl_client *client,
5041 void *data, uint32_t version, uint32_t id)
5042{
Tiago Vignattibe143262012-04-16 17:31:41 +03005043 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005044 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005045
Jason Ekstranda85118c2013-06-27 20:17:02 -05005046 resource = wl_resource_create(client, &desktop_shell_interface,
5047 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005048
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005049 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005050 wl_resource_set_implementation(resource,
5051 &desktop_shell_implementation,
5052 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005053 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005054
5055 if (version < 2)
5056 shell_fade_startup(shell);
5057
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005058 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005059 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005060
5061 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5062 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005063 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005064}
5065
Pekka Paalanen6e168112011-11-24 11:34:05 +02005066static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005067screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005068{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005069 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005070 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005071
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005072 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005073 return;
5074
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005075 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005076 if (!shell->locked)
5077 return;
5078
Jason Ekstranda7af7042013-10-12 22:38:11 -05005079 view = container_of(surface->views.next, struct weston_view, surface_link);
5080 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005081
Jason Ekstranda7af7042013-10-12 22:38:11 -05005082 if (wl_list_empty(&view->layer_link)) {
5083 wl_list_insert(shell->lock_layer.view_list.prev,
5084 &view->layer_link);
5085 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005086 wl_event_source_timer_update(shell->screensaver.timer,
5087 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005088 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005089 }
5090}
5091
5092static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005093screensaver_set_surface(struct wl_client *client,
5094 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005095 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005096 struct wl_resource *output_resource)
5097{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005098 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005099 struct weston_surface *surface =
5100 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005101 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005102 struct weston_view *view, *next;
5103
5104 /* Make sure we only have one view */
5105 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5106 weston_view_destroy(view);
5107 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005108
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005109 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005110 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005111 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005112}
5113
5114static const struct screensaver_interface screensaver_implementation = {
5115 screensaver_set_surface
5116};
5117
5118static void
5119unbind_screensaver(struct wl_resource *resource)
5120{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005121 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005122
Pekka Paalanen77346a62011-11-30 16:26:35 +02005123 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005124}
5125
5126static void
5127bind_screensaver(struct wl_client *client,
5128 void *data, uint32_t version, uint32_t id)
5129{
Tiago Vignattibe143262012-04-16 17:31:41 +03005130 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005131 struct wl_resource *resource;
5132
Jason Ekstranda85118c2013-06-27 20:17:02 -05005133 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005134
Pekka Paalanen77346a62011-11-30 16:26:35 +02005135 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005136 wl_resource_set_implementation(resource,
5137 &screensaver_implementation,
5138 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005139 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005140 return;
5141 }
5142
5143 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5144 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005145 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005146}
5147
Kristian Høgsberg07045392012-02-19 18:52:44 -05005148struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005149 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005150 struct weston_surface *current;
5151 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005152 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005153};
5154
5155static void
5156switcher_next(struct switcher *switcher)
5157{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005158 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005159 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005160 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005161 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005162
Jason Ekstranda7af7042013-10-12 22:38:11 -05005163 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005164 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005165 if (shsurf &&
5166 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5167 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005168 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005169 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005170 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005171 next = view->surface;
5172 prev = view->surface;
5173 view->alpha = 0.25;
5174 weston_view_geometry_dirty(view);
5175 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005176 }
Alex Wu1659daa2012-04-01 20:13:09 +08005177
Jason Ekstranda7af7042013-10-12 22:38:11 -05005178 if (is_black_surface(view->surface, NULL)) {
5179 view->alpha = 0.25;
5180 weston_view_geometry_dirty(view);
5181 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005182 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005183 }
5184
5185 if (next == NULL)
5186 next = first;
5187
Alex Wu07b26062012-03-12 16:06:01 +08005188 if (next == NULL)
5189 return;
5190
Kristian Høgsberg07045392012-02-19 18:52:44 -05005191 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005192 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005193
5194 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005195 wl_list_for_each(view, &next->views, surface_link)
5196 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005197
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005198 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005199 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005200 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005201}
5202
5203static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005204switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005205{
5206 struct switcher *switcher =
5207 container_of(listener, struct switcher, listener);
5208
5209 switcher_next(switcher);
5210}
5211
5212static void
Daniel Stone351eb612012-05-31 15:27:47 -04005213switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005214{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005215 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005216 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005217 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005218
Jason Ekstranda7af7042013-10-12 22:38:11 -05005219 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005220 if (is_focus_view(view))
5221 continue;
5222
Jason Ekstranda7af7042013-10-12 22:38:11 -05005223 view->alpha = 1.0;
5224 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005225 }
5226
Alex Wu07b26062012-03-12 16:06:01 +08005227 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005228 activate(switcher->shell, switcher->current,
5229 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005230 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005231 weston_keyboard_end_grab(keyboard);
5232 if (keyboard->input_method_resource)
5233 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005234 free(switcher);
5235}
5236
5237static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005238switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005239 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005240{
5241 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005242 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005243
Daniel Stonec9785ea2012-05-30 16:31:52 +01005244 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005245 switcher_next(switcher);
5246}
5247
5248static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005249switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005250 uint32_t mods_depressed, uint32_t mods_latched,
5251 uint32_t mods_locked, uint32_t group)
5252{
5253 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005254 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005255
Daniel Stone351eb612012-05-31 15:27:47 -04005256 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5257 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005258}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005259
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005260static void
5261switcher_cancel(struct weston_keyboard_grab *grab)
5262{
5263 struct switcher *switcher = container_of(grab, struct switcher, grab);
5264
5265 switcher_destroy(switcher);
5266}
5267
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005268static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005269 switcher_key,
5270 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005271 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005272};
5273
5274static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005275switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005276 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005277{
Tiago Vignattibe143262012-04-16 17:31:41 +03005278 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005279 struct switcher *switcher;
5280
5281 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005282 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005283 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005284 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005285 wl_list_init(&switcher->listener.link);
5286
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005287 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005288 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005289 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005290 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5291 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005292 switcher_next(switcher);
5293}
5294
Pekka Paalanen3c647232011-12-22 13:43:43 +02005295static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005296backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005297 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005298{
5299 struct weston_compositor *compositor = data;
5300 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005301 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005302
5303 /* TODO: we're limiting to simple use cases, where we assume just
5304 * control on the primary display. We'd have to extend later if we
5305 * ever get support for setting backlights on random desktop LCD
5306 * panels though */
5307 output = get_default_output(compositor);
5308 if (!output)
5309 return;
5310
5311 if (!output->set_backlight)
5312 return;
5313
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005314 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5315 backlight_new = output->backlight_current - 25;
5316 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5317 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005318
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005319 if (backlight_new < 5)
5320 backlight_new = 5;
5321 if (backlight_new > 255)
5322 backlight_new = 255;
5323
5324 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005325 output->set_backlight(output, output->backlight_current);
5326}
5327
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005328struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005329 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005330 struct weston_seat *seat;
5331 uint32_t key[2];
5332 int key_released[2];
5333};
5334
5335static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005336debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005337 uint32_t key, uint32_t state)
5338{
5339 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005340 struct weston_compositor *ec = db->seat->compositor;
5341 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005342 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005343 uint32_t serial;
5344 int send = 0, terminate = 0;
5345 int check_binding = 1;
5346 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005347 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005348
5349 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5350 /* Do not run bindings on key releases */
5351 check_binding = 0;
5352
5353 for (i = 0; i < 2; i++)
5354 if (key == db->key[i])
5355 db->key_released[i] = 1;
5356
5357 if (db->key_released[0] && db->key_released[1]) {
5358 /* All key releases been swalled so end the grab */
5359 terminate = 1;
5360 } else if (key != db->key[0] && key != db->key[1]) {
5361 /* Should not swallow release of other keys */
5362 send = 1;
5363 }
5364 } else if (key == db->key[0] && !db->key_released[0]) {
5365 /* Do not check bindings for the first press of the binding
5366 * key. This allows it to be used as a debug shortcut.
5367 * We still need to swallow this event. */
5368 check_binding = 0;
5369 } else if (db->key[1]) {
5370 /* If we already ran a binding don't process another one since
5371 * we can't keep track of all the binding keys that were
5372 * pressed in order to swallow the release events. */
5373 send = 1;
5374 check_binding = 0;
5375 }
5376
5377 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005378 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5379 key, state)) {
5380 /* We ran a binding so swallow the press and keep the
5381 * grab to swallow the released too. */
5382 send = 0;
5383 terminate = 0;
5384 db->key[1] = key;
5385 } else {
5386 /* Terminate the grab since the key pressed is not a
5387 * debug binding key. */
5388 send = 1;
5389 terminate = 1;
5390 }
5391 }
5392
5393 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005394 serial = wl_display_next_serial(display);
5395 resource_list = &grab->keyboard->focus_resource_list;
5396 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005397 wl_keyboard_send_key(resource, serial, time, key, state);
5398 }
5399 }
5400
5401 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005402 weston_keyboard_end_grab(grab->keyboard);
5403 if (grab->keyboard->input_method_resource)
5404 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005405 free(db);
5406 }
5407}
5408
5409static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005410debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005411 uint32_t mods_depressed, uint32_t mods_latched,
5412 uint32_t mods_locked, uint32_t group)
5413{
5414 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005415 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005416
Neil Roberts96d790e2013-09-19 17:32:00 +01005417 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005418
Neil Roberts96d790e2013-09-19 17:32:00 +01005419 wl_resource_for_each(resource, resource_list) {
5420 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5421 mods_latched, mods_locked, group);
5422 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005423}
5424
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005425static void
5426debug_binding_cancel(struct weston_keyboard_grab *grab)
5427{
5428 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5429
5430 weston_keyboard_end_grab(grab->keyboard);
5431 free(db);
5432}
5433
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005434struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005435 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005436 debug_binding_modifiers,
5437 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005438};
5439
5440static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005441debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005442{
5443 struct debug_binding_grab *grab;
5444
5445 grab = calloc(1, sizeof *grab);
5446 if (!grab)
5447 return;
5448
5449 grab->seat = (struct weston_seat *) seat;
5450 grab->key[0] = key;
5451 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005452 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005453}
5454
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005455static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005456force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005457 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005458{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005459 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005460 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005461 struct desktop_shell *shell = data;
5462 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005463 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005464
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005465 focus_surface = seat->keyboard->focus;
5466 if (!focus_surface)
5467 return;
5468
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005469 wl_signal_emit(&compositor->kill_signal, focus_surface);
5470
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005471 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005472 wl_client_get_credentials(client, &pid, NULL, NULL);
5473
5474 /* Skip clients that we launched ourselves (the credentials of
5475 * the socketpair is ours) */
5476 if (pid == getpid())
5477 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005478
Daniel Stone325fc2d2012-05-30 16:31:58 +01005479 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005480}
5481
5482static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005483workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005484 uint32_t key, void *data)
5485{
5486 struct desktop_shell *shell = data;
5487 unsigned int new_index = shell->workspaces.current;
5488
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005489 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005490 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005491 if (new_index != 0)
5492 new_index--;
5493
5494 change_workspace(shell, new_index);
5495}
5496
5497static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005498workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005499 uint32_t key, void *data)
5500{
5501 struct desktop_shell *shell = data;
5502 unsigned int new_index = shell->workspaces.current;
5503
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005504 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005505 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005506 if (new_index < shell->workspaces.num - 1)
5507 new_index++;
5508
5509 change_workspace(shell, new_index);
5510}
5511
5512static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005513workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005514 uint32_t key, void *data)
5515{
5516 struct desktop_shell *shell = data;
5517 unsigned int new_index;
5518
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005519 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005520 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005521 new_index = key - KEY_F1;
5522 if (new_index >= shell->workspaces.num)
5523 new_index = shell->workspaces.num - 1;
5524
5525 change_workspace(shell, new_index);
5526}
5527
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005528static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005529workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005530 uint32_t key, void *data)
5531{
5532 struct desktop_shell *shell = data;
5533 unsigned int new_index = shell->workspaces.current;
5534
5535 if (shell->locked)
5536 return;
5537
5538 if (new_index != 0)
5539 new_index--;
5540
5541 take_surface_to_workspace_by_seat(shell, seat, new_index);
5542}
5543
5544static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005545workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005546 uint32_t key, void *data)
5547{
5548 struct desktop_shell *shell = data;
5549 unsigned int new_index = shell->workspaces.current;
5550
5551 if (shell->locked)
5552 return;
5553
5554 if (new_index < shell->workspaces.num - 1)
5555 new_index++;
5556
5557 take_surface_to_workspace_by_seat(shell, seat, new_index);
5558}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005559
5560static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005561handle_output_destroy(struct wl_listener *listener, void *data)
5562{
5563 struct shell_output *output_listener =
5564 container_of(listener, struct shell_output, destroy_listener);
5565
5566 wl_list_remove(&output_listener->destroy_listener.link);
5567 wl_list_remove(&output_listener->link);
5568 free(output_listener);
5569}
5570
5571static void
5572create_shell_output(struct desktop_shell *shell,
5573 struct weston_output *output)
5574{
5575 struct shell_output *shell_output;
5576
5577 shell_output = zalloc(sizeof *shell_output);
5578 if (shell_output == NULL)
5579 return;
5580
5581 shell_output->output = output;
5582 shell_output->shell = shell;
5583 shell_output->destroy_listener.notify = handle_output_destroy;
5584 wl_signal_add(&output->destroy_signal,
5585 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005586 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005587}
5588
5589static void
5590handle_output_create(struct wl_listener *listener, void *data)
5591{
5592 struct desktop_shell *shell =
5593 container_of(listener, struct desktop_shell, output_create_listener);
5594 struct weston_output *output = (struct weston_output *)data;
5595
5596 create_shell_output(shell, output);
5597}
5598
5599static void
5600setup_output_destroy_handler(struct weston_compositor *ec,
5601 struct desktop_shell *shell)
5602{
5603 struct weston_output *output;
5604
5605 wl_list_init(&shell->output_list);
5606 wl_list_for_each(output, &ec->output_list, link)
5607 create_shell_output(shell, output);
5608
5609 shell->output_create_listener.notify = handle_output_create;
5610 wl_signal_add(&ec->output_created_signal,
5611 &shell->output_create_listener);
5612}
5613
5614static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005615shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005616{
Tiago Vignattibe143262012-04-16 17:31:41 +03005617 struct desktop_shell *shell =
5618 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005619 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005620 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005621
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005622 if (shell->child.client)
5623 wl_client_destroy(shell->child.client);
5624
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005625 wl_list_remove(&shell->idle_listener.link);
5626 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005627
5628 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005629
Xiong Zhang6b481422013-10-23 13:58:32 +08005630 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5631 wl_list_remove(&shell_output->destroy_listener.link);
5632 wl_list_remove(&shell_output->link);
5633 free(shell_output);
5634 }
5635
5636 wl_list_remove(&shell->output_create_listener.link);
5637
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005638 wl_array_for_each(ws, &shell->workspaces.array)
5639 workspace_destroy(*ws);
5640 wl_array_release(&shell->workspaces.array);
5641
Pekka Paalanen3c647232011-12-22 13:43:43 +02005642 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005643 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005644 free(shell);
5645}
5646
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005647static void
5648shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5649{
5650 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005651 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005652
5653 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005654 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5655 MODIFIER_CTRL | MODIFIER_ALT,
5656 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005657 weston_compositor_add_key_binding(ec, KEY_TAB,
5658 MODIFIER_ALT,
5659 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005660 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5661 click_to_activate_binding,
5662 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005663 weston_compositor_add_touch_binding(ec, 0,
5664 touch_to_activate_binding,
5665 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005666 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5667 MODIFIER_SUPER | MODIFIER_ALT,
5668 surface_opacity_binding, NULL);
5669 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5670 MODIFIER_SUPER, zoom_axis_binding,
5671 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005672
5673 /* configurable bindings */
5674 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005675 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5676 zoom_key_binding, NULL);
5677 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5678 zoom_key_binding, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02005679 weston_compositor_add_key_binding(ec, KEY_M, mod, maximize_binding,
5680 NULL);
5681 weston_compositor_add_key_binding(ec, KEY_F, mod, fullscreen_binding,
5682 NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005683 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5684 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005685 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005686 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5687 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005688
5689 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5690 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5691 rotate_binding, NULL);
5692
Daniel Stone325fc2d2012-05-30 16:31:58 +01005693 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5694 shell);
5695 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5696 ec);
5697 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5698 backlight_binding, ec);
5699 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5700 ec);
5701 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5702 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005703 weston_compositor_add_key_binding(ec, KEY_K, mod,
5704 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005705 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5706 workspace_up_binding, shell);
5707 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5708 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005709 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5710 workspace_move_surface_up_binding,
5711 shell);
5712 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5713 workspace_move_surface_down_binding,
5714 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005715
Daniel Stonedf8133b2013-11-19 11:37:14 +01005716 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5717
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005718 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5719 if (shell->workspaces.num > 1) {
5720 num_workspace_bindings = shell->workspaces.num;
5721 if (num_workspace_bindings > 6)
5722 num_workspace_bindings = 6;
5723 for (i = 0; i < num_workspace_bindings; i++)
5724 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5725 workspace_f_binding,
5726 shell);
5727 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005728
5729 /* Debug bindings */
5730 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5731 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005732}
5733
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005734WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005735module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005736 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005737{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005738 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005739 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005740 struct workspace **pws;
5741 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005742 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005743
Peter Huttererf3d62272013-08-08 11:57:05 +10005744 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005745 if (shell == NULL)
5746 return -1;
5747
Kristian Høgsberg75840622011-09-06 13:48:16 -04005748 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005749
5750 shell->destroy_listener.notify = shell_destroy;
5751 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005752 shell->idle_listener.notify = idle_handler;
5753 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5754 shell->wake_listener.notify = wake_handler;
5755 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005756
Scott Moreauff1db4a2012-04-17 19:06:18 -06005757 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005758 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005759 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005760 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005761 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005762 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005763 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005764 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005765 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005766 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005767 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005768
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005769 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5770 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005771 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5772 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005773 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005774
5775 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005776 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005777
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005778 if (input_panel_setup(shell) < 0)
5779 return -1;
5780
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005781 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005782
Daniel Stonedf8133b2013-11-19 11:37:14 +01005783 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5784 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5785
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005786 for (i = 0; i < shell->workspaces.num; i++) {
5787 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5788 if (pws == NULL)
5789 return -1;
5790
5791 *pws = workspace_create();
5792 if (*pws == NULL)
5793 return -1;
5794 }
5795 activate_workspace(shell, 0);
5796
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005797 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005798 wl_list_init(&shell->workspaces.animation.link);
5799 shell->workspaces.animation.frame = animate_workspace_change_frame;
5800
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005801 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005802 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005803 return -1;
5804
Rafael Antognollie2a34552013-12-03 15:35:45 -02005805 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5806 shell, bind_xdg_shell) == NULL)
5807 return -1;
5808
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005809 if (wl_global_create(ec->wl_display,
5810 &desktop_shell_interface, 2,
5811 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005812 return -1;
5813
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005814 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5815 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005816 return -1;
5817
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005818 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5819 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005820 return -1;
5821
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005822 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005823
Xiong Zhang6b481422013-10-23 13:58:32 +08005824 setup_output_destroy_handler(ec, shell);
5825
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005826 loop = wl_display_get_event_loop(ec->wl_display);
5827 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005828
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005829 shell->screensaver.timer =
5830 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5831
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005832 wl_list_for_each(seat, &ec->seat_list, link)
5833 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005834
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005835 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005836
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005837 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005838
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005839 return 0;
5840}