blob: f70b310aea845ede49ee7cdf8caea2a788717aab [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
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002209weston_view_set_initial_position(struct weston_view *view,
2210 struct desktop_shell *shell);
2211
2212static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002213unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002214{
Philip Withnallf85fe842013-11-25 18:01:37 +00002215 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002216 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2217 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002218 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002219 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002220 shsurf->fullscreen_output = NULL;
2221
Alex Wu4539b082012-03-01 12:57:46 +08002222 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2223 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002224
Alex Wu4539b082012-03-01 12:57:46 +08002225 wl_list_remove(&shsurf->fullscreen.transform.link);
2226 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002227
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228 if (shsurf->fullscreen.black_view)
2229 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2230 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002231
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002232 if (shsurf->saved_position_valid)
2233 weston_view_set_position(shsurf->view,
2234 shsurf->saved_x, shsurf->saved_y);
2235 else
2236 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2237
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002238 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002239 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002240 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002241 shsurf->saved_rotation_valid = false;
2242 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002243
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002244 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002245}
2246
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002247static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002248shell_surface_set_fullscreen(struct wl_client *client,
2249 struct wl_resource *resource,
2250 uint32_t method,
2251 uint32_t framerate,
2252 struct wl_resource *output_resource)
2253{
2254 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2255 struct weston_output *output;
2256
2257 if (output_resource)
2258 output = wl_resource_get_user_data(output_resource);
2259 else
2260 output = NULL;
2261
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002262 shell_surface_set_parent(shsurf, NULL);
2263
Rafael Antognolli03b16592013-12-03 15:35:42 -02002264 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002265 set_fullscreen(shsurf, method, framerate, output);
2266}
2267
2268static void
2269set_popup(struct shell_surface *shsurf,
2270 struct weston_surface *parent,
2271 struct weston_seat *seat,
2272 uint32_t serial,
2273 int32_t x,
2274 int32_t y)
2275{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002276 assert(parent != NULL);
2277
Philip Withnallbecb77e2013-11-25 18:01:30 +00002278 shsurf->popup.shseat = get_shell_seat(seat);
2279 shsurf->popup.serial = serial;
2280 shsurf->popup.x = x;
2281 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002282
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002283 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002284}
2285
2286static void
2287shell_surface_set_popup(struct wl_client *client,
2288 struct wl_resource *resource,
2289 struct wl_resource *seat_resource,
2290 uint32_t serial,
2291 struct wl_resource *parent_resource,
2292 int32_t x, int32_t y, uint32_t flags)
2293{
2294 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002295 struct weston_surface *parent =
2296 wl_resource_get_user_data(parent_resource);
2297
2298 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002299
Rafael Antognolli03b16592013-12-03 15:35:42 -02002300 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002301 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002302 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002303 wl_resource_get_user_data(seat_resource),
2304 serial, x, y);
2305}
2306
2307static void
2308set_maximized(struct shell_surface *shsurf,
2309 struct weston_output *output)
2310{
2311 struct desktop_shell *shell;
2312 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002313
Philip Withnall352e7ed2013-11-25 18:01:35 +00002314 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002315
2316 shell = shell_surface_get_shell(shsurf);
2317 panel_height = get_output_panel_height(shell, shsurf->output);
2318 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2319
2320 shsurf->client->send_configure(shsurf->surface, edges,
2321 shsurf->output->width,
2322 shsurf->output->height - panel_height);
2323
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002324 shsurf->next_state.maximized = true;
2325 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002326 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002327}
2328
2329static void
2330unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002331{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002332 /* undo all maximized things here */
2333 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002334
2335 if (shsurf->saved_position_valid)
2336 weston_view_set_position(shsurf->view,
2337 shsurf->saved_x, shsurf->saved_y);
2338 else
2339 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002340
2341 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002342 wl_list_insert(&shsurf->view->geometry.transformation_list,
2343 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002344 shsurf->saved_rotation_valid = false;
2345 }
2346
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002347 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002348}
2349
Philip Withnallbecb77e2013-11-25 18:01:30 +00002350static void
2351shell_surface_set_maximized(struct wl_client *client,
2352 struct wl_resource *resource,
2353 struct wl_resource *output_resource)
2354{
2355 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2356 struct weston_output *output;
2357
2358 if (output_resource)
2359 output = wl_resource_get_user_data(output_resource);
2360 else
2361 output = NULL;
2362
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002363 shell_surface_set_parent(shsurf, NULL);
2364
Rafael Antognolli03b16592013-12-03 15:35:42 -02002365 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002366 set_maximized(shsurf, output);
2367}
2368
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002369/* This is only ever called from set_surface_type(), so there’s no need to
2370 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002371static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002372reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002373{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002374 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002375 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002376 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002377 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002378
Pekka Paalanen98262232011-12-01 10:42:22 +02002379 return 0;
2380}
2381
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002382static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002383set_full_output(struct shell_surface *shsurf)
2384{
2385 shsurf->saved_x = shsurf->view->geometry.x;
2386 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002387 shsurf->saved_width = shsurf->surface->width;
2388 shsurf->saved_height = shsurf->surface->height;
2389 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002390 shsurf->saved_position_valid = true;
2391
2392 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2393 wl_list_remove(&shsurf->rotation.transform.link);
2394 wl_list_init(&shsurf->rotation.transform.link);
2395 weston_view_geometry_dirty(shsurf->view);
2396 shsurf->saved_rotation_valid = true;
2397 }
2398}
2399
2400static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002401set_surface_type(struct shell_surface *shsurf)
2402{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002403 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002404 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002405
Philip Withnallbecb77e2013-11-25 18:01:30 +00002406 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002407
Rafael Antognolli03b16592013-12-03 15:35:42 -02002408 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002409 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002410
2411 switch (shsurf->type) {
2412 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002413 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002414 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002415 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002416 weston_view_set_position(shsurf->view,
2417 pev->geometry.x + shsurf->transient.x,
2418 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002419 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002420 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002421
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002422 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002423 weston_view_set_position(shsurf->view, shsurf->transient.x,
2424 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002425 break;
2426
Philip Withnall0f640e22013-11-25 18:01:31 +00002427 case SHELL_SURFACE_POPUP:
2428 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002429 default:
2430 break;
2431 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002432
2433 /* Update the surface’s layer. */
2434 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002435}
2436
Tiago Vignattibe143262012-04-16 17:31:41 +03002437static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002438shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002439{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002440 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002441}
2442
Alex Wu21858432012-04-01 20:13:08 +08002443static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002444black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002445
Jason Ekstranda7af7042013-10-12 22:38:11 -05002446static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002447create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002448 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002449 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002450{
2451 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002452 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002453
2454 surface = weston_surface_create(ec);
2455 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002456 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002457 return NULL;
2458 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002459 view = weston_view_create(surface);
2460 if (surface == NULL) {
2461 weston_log("no memory\n");
2462 weston_surface_destroy(surface);
2463 return NULL;
2464 }
Alex Wu4539b082012-03-01 12:57:46 +08002465
Alex Wu21858432012-04-01 20:13:08 +08002466 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002467 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002468 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002469 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002470 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002471 pixman_region32_fini(&surface->input);
2472 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002473
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002474 surface->width = w;
2475 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002476 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002477
2478 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002479}
2480
Philip Withnalled8f1a92013-11-25 18:01:43 +00002481static void
2482shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2483{
2484 struct weston_output *output = shsurf->fullscreen_output;
2485
Rafael Antognolli03b16592013-12-03 15:35:42 -02002486 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002487
2488 if (!shsurf->fullscreen.black_view)
2489 shsurf->fullscreen.black_view =
2490 create_black_surface(shsurf->surface->compositor,
2491 shsurf->surface,
2492 output->x, output->y,
2493 output->width,
2494 output->height);
2495
2496 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2497 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2498 wl_list_insert(&shsurf->view->layer_link,
2499 &shsurf->fullscreen.black_view->layer_link);
2500 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2501 weston_surface_damage(shsurf->surface);
2502}
2503
Alex Wu4539b082012-03-01 12:57:46 +08002504/* Create black surface and append it to the associated fullscreen surface.
2505 * Handle size dismatch and positioning according to the method. */
2506static void
2507shell_configure_fullscreen(struct shell_surface *shsurf)
2508{
2509 struct weston_output *output = shsurf->fullscreen_output;
2510 struct weston_surface *surface = shsurf->surface;
2511 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002512 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002513 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002514
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002515 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2516 restore_output_mode(output);
2517
Philip Withnalled8f1a92013-11-25 18:01:43 +00002518 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002519
Jason Ekstranda7af7042013-10-12 22:38:11 -05002520 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002521 &surf_width, &surf_height);
2522
Alex Wu4539b082012-03-01 12:57:46 +08002523 switch (shsurf->fullscreen.type) {
2524 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002525 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002526 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002527 break;
2528 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002529 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002530 if (output->width == surf_width &&
2531 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002532 weston_view_set_position(shsurf->view,
2533 output->x - surf_x,
2534 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002535 break;
2536 }
2537
Alex Wu4539b082012-03-01 12:57:46 +08002538 matrix = &shsurf->fullscreen.transform.matrix;
2539 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002540
Scott Moreau1bad5db2012-08-18 01:04:05 -06002541 output_aspect = (float) output->width /
2542 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002543 /* XXX: Use surf_width and surf_height here? */
2544 surface_aspect = (float) surface->width /
2545 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002546 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002547 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002548 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002549 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002550 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002551 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002552
Alex Wu4539b082012-03-01 12:57:46 +08002553 weston_matrix_scale(matrix, scale, scale, 1);
2554 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002556 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002557 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2558 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002559 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002560
Alex Wu4539b082012-03-01 12:57:46 +08002561 break;
2562 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002563 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002564 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002565 surf_width * surface->buffer_viewport.scale,
2566 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002567 shsurf->fullscreen.framerate};
2568
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002569 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002570 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571 weston_view_set_position(shsurf->view,
2572 output->x - surf_x,
2573 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002574 shsurf->fullscreen.black_view->surface->width = output->width;
2575 shsurf->fullscreen.black_view->surface->height = output->height;
2576 weston_view_set_position(shsurf->fullscreen.black_view,
2577 output->x - surf_x,
2578 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002579 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002580 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002581 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002582 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002583 }
Alex Wubd3354b2012-04-17 17:20:49 +08002584 }
Alex Wu4539b082012-03-01 12:57:46 +08002585 break;
2586 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002587 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002588 break;
2589 default:
2590 break;
2591 }
2592}
2593
Alex Wu4539b082012-03-01 12:57:46 +08002594static void
2595shell_map_fullscreen(struct shell_surface *shsurf)
2596{
Alex Wubd3354b2012-04-17 17:20:49 +08002597 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002598}
2599
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002600static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002601set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2602{
2603 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002604 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002605 shsurf->transient.x = x;
2606 shsurf->transient.y = y;
2607 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002608
2609 shell_surface_set_parent(shsurf, NULL);
2610
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002611 shsurf->type = SHELL_SURFACE_XWAYLAND;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002612}
2613
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002614static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002615
2616static void
2617destroy_shell_seat(struct wl_listener *listener, void *data)
2618{
2619 struct shell_seat *shseat =
2620 container_of(listener,
2621 struct shell_seat, seat_destroy_listener);
2622 struct shell_surface *shsurf, *prev = NULL;
2623
2624 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002625 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002626 shseat->popup_grab.client = NULL;
2627
2628 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2629 shsurf->popup.shseat = NULL;
2630 if (prev) {
2631 wl_list_init(&prev->popup.grab_link);
2632 }
2633 prev = shsurf;
2634 }
2635 wl_list_init(&prev->popup.grab_link);
2636 }
2637
2638 wl_list_remove(&shseat->seat_destroy_listener.link);
2639 free(shseat);
2640}
2641
2642static struct shell_seat *
2643create_shell_seat(struct weston_seat *seat)
2644{
2645 struct shell_seat *shseat;
2646
2647 shseat = calloc(1, sizeof *shseat);
2648 if (!shseat) {
2649 weston_log("no memory to allocate shell seat\n");
2650 return NULL;
2651 }
2652
2653 shseat->seat = seat;
2654 wl_list_init(&shseat->popup_grab.surfaces_list);
2655
2656 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2657 wl_signal_add(&seat->destroy_signal,
2658 &shseat->seat_destroy_listener);
2659
2660 return shseat;
2661}
2662
2663static struct shell_seat *
2664get_shell_seat(struct weston_seat *seat)
2665{
2666 struct wl_listener *listener;
2667
2668 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2669 if (listener == NULL)
2670 return create_shell_seat(seat);
2671
2672 return container_of(listener,
2673 struct shell_seat, seat_destroy_listener);
2674}
2675
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002676static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002677popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002678{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002679 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002680 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002681 struct shell_seat *shseat =
2682 container_of(grab, struct shell_seat, popup_grab.grab);
2683 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002684 wl_fixed_t sx, sy;
2685
Jason Ekstranda7af7042013-10-12 22:38:11 -05002686 view = weston_compositor_pick_view(pointer->seat->compositor,
2687 pointer->x, pointer->y,
2688 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002689
Jason Ekstranda7af7042013-10-12 22:38:11 -05002690 if (view && view->surface->resource &&
2691 wl_resource_get_client(view->surface->resource) == client) {
2692 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002693 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002694 weston_pointer_set_focus(pointer, NULL,
2695 wl_fixed_from_int(0),
2696 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002697 }
2698}
2699
2700static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002701popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2702 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002703{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002704 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002705 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002706 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002707
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002708 weston_pointer_move(pointer, x, y);
2709
Neil Roberts96d790e2013-09-19 17:32:00 +01002710 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002711 weston_view_from_global_fixed(pointer->focus,
2712 pointer->x, pointer->y,
2713 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002714 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002715 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002716}
2717
2718static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002719popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002720 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002721{
2722 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002723 struct shell_seat *shseat =
2724 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002725 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002726 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002727 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002728 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002729
Neil Roberts96d790e2013-09-19 17:32:00 +01002730 resource_list = &grab->pointer->focus_resource_list;
2731 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002732 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002733 wl_resource_for_each(resource, resource_list) {
2734 wl_pointer_send_button(resource, serial,
2735 time, button, state);
2736 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002737 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002738 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002739 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002740 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002741 }
2742
Daniel Stone4dbadb12012-05-30 16:31:51 +01002743 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002744 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002745}
2746
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002747static void
2748popup_grab_cancel(struct weston_pointer_grab *grab)
2749{
2750 popup_grab_end(grab->pointer);
2751}
2752
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002753static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002754 popup_grab_focus,
2755 popup_grab_motion,
2756 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002757 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002758};
2759
2760static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002761shell_surface_send_popup_done(struct shell_surface *shsurf)
2762{
2763 if (shell_surface_is_wl_shell_surface(shsurf))
2764 wl_shell_surface_send_popup_done(shsurf->resource);
2765 else if (shell_surface_is_xdg_popup(shsurf))
2766 xdg_popup_send_popup_done(shsurf->resource,
2767 shsurf->popup.serial);
2768}
2769
2770static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002771popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002772{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002773 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002774 struct shell_seat *shseat =
2775 container_of(grab, struct shell_seat, popup_grab.grab);
2776 struct shell_surface *shsurf;
2777 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002778
2779 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002780 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002781 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002782 shseat->popup_grab.grab.interface = NULL;
2783 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002784 /* Send the popup_done event to all the popups open */
2785 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002786 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002787 shsurf->popup.shseat = NULL;
2788 if (prev) {
2789 wl_list_init(&prev->popup.grab_link);
2790 }
2791 prev = shsurf;
2792 }
2793 wl_list_init(&prev->popup.grab_link);
2794 wl_list_init(&shseat->popup_grab.surfaces_list);
2795 }
2796}
2797
2798static void
2799add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2800{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002801 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002802
2803 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002804 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002805 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002806 /* We must make sure here that this popup was opened after
2807 * a mouse press, and not just by moving around with other
2808 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002809 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002810 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002811
Rob Bradforddfe31052013-06-26 19:49:11 +01002812 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002813 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002814 } else {
2815 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002816 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002817}
2818
2819static void
2820remove_popup_grab(struct shell_surface *shsurf)
2821{
2822 struct shell_seat *shseat = shsurf->popup.shseat;
2823
2824 wl_list_remove(&shsurf->popup.grab_link);
2825 wl_list_init(&shsurf->popup.grab_link);
2826 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002827 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002828 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002829 }
2830}
2831
2832static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002833shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002834{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002835 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002836 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002837
Jason Ekstranda7af7042013-10-12 22:38:11 -05002838 shsurf->surface->output = parent_view->output;
2839 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002840
Jason Ekstranda7af7042013-10-12 22:38:11 -05002841 weston_view_set_transform_parent(shsurf->view, parent_view);
2842 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2843 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002844
Kristian Høgsberge3148752013-05-06 23:19:49 -04002845 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002846 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002847 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002848 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002849 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002850 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002851}
2852
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002853static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002854 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002855 shell_surface_move,
2856 shell_surface_resize,
2857 shell_surface_set_toplevel,
2858 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002859 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002860 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002861 shell_surface_set_maximized,
2862 shell_surface_set_title,
2863 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002864};
2865
2866static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002867destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002868{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002869 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2870
Giulio Camuffo5085a752013-03-25 21:42:45 +01002871 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2872 remove_popup_grab(shsurf);
2873 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002874
Alex Wubd3354b2012-04-17 17:20:49 +08002875 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002876 shell_surface_is_top_fullscreen(shsurf))
2877 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002878
Jason Ekstranda7af7042013-10-12 22:38:11 -05002879 if (shsurf->fullscreen.black_view)
2880 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002881
Alex Wubd3354b2012-04-17 17:20:49 +08002882 /* As destroy_resource() use wl_list_for_each_safe(),
2883 * we can always remove the listener.
2884 */
2885 wl_list_remove(&shsurf->surface_destroy_listener.link);
2886 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002887 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002888 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002889
Jason Ekstranda7af7042013-10-12 22:38:11 -05002890 weston_view_destroy(shsurf->view);
2891
Philip Withnall648a4dd2013-11-25 18:01:44 +00002892 wl_list_remove(&shsurf->children_link);
2893
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002894 wl_list_remove(&shsurf->link);
2895 free(shsurf);
2896}
2897
2898static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002899shell_destroy_shell_surface(struct wl_resource *resource)
2900{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002901 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002902
2903 destroy_shell_surface(shsurf);
2904}
2905
2906static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002907shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002908{
2909 struct shell_surface *shsurf = container_of(listener,
2910 struct shell_surface,
2911 surface_destroy_listener);
2912
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002913 if (shsurf->resource)
2914 wl_resource_destroy(shsurf->resource);
2915 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002916 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002917}
2918
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002919static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002920shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002921static void
2922shell_surface_output_destroyed(struct weston_surface *);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002923
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002924struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002925get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002926{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002927 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002928 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002929 else
2930 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002931}
2932
Rafael Antognollie2a34552013-12-03 15:35:45 -02002933static struct shell_surface *
2934create_common_surface(void *shell, struct weston_surface *surface,
2935 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002936{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002937 struct shell_surface *shsurf;
2938
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002939 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002940 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002941 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002942 }
2943
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002944 shsurf = calloc(1, sizeof *shsurf);
2945 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002946 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002947 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002948 }
2949
Jason Ekstranda7af7042013-10-12 22:38:11 -05002950 shsurf->view = weston_view_create(surface);
2951 if (!shsurf->view) {
2952 weston_log("no memory to allocate shell surface\n");
2953 free(shsurf);
2954 return NULL;
2955 }
2956
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002957 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002958 surface->configure_private = shsurf;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002959 surface->output_destroyed = shell_surface_output_destroyed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002960
Tiago Vignattibc052c92012-04-19 16:18:18 +03002961 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002962 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002963 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002964 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002965 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002966 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002967 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2968 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002969 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002970 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002971 wl_list_init(&shsurf->fullscreen.transform.link);
2972
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002973 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002974 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002975 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002976 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002977
2978 /* init link so its safe to always remove it in destroy_shell_surface */
2979 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002980 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002981
Pekka Paalanen460099f2012-01-20 16:48:25 +02002982 /* empty when not in use */
2983 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002984 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002985
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002986 wl_list_init(&shsurf->workspace_transform.link);
2987
Philip Withnall648a4dd2013-11-25 18:01:44 +00002988 wl_list_init(&shsurf->children_link);
2989 wl_list_init(&shsurf->children_list);
2990 shsurf->parent = NULL;
2991
Pekka Paalanen98262232011-12-01 10:42:22 +02002992 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002993
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002994 shsurf->client = client;
2995
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002996 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002997}
2998
Rafael Antognollie2a34552013-12-03 15:35:45 -02002999static struct shell_surface *
3000create_shell_surface(void *shell, struct weston_surface *surface,
3001 const struct weston_shell_client *client)
3002{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003003 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003004}
3005
Jason Ekstranda7af7042013-10-12 22:38:11 -05003006static struct weston_view *
3007get_primary_view(void *shell, struct shell_surface *shsurf)
3008{
3009 return shsurf->view;
3010}
3011
Tiago Vignattibc052c92012-04-19 16:18:18 +03003012static void
3013shell_get_shell_surface(struct wl_client *client,
3014 struct wl_resource *resource,
3015 uint32_t id,
3016 struct wl_resource *surface_resource)
3017{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003018 struct weston_surface *surface =
3019 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003020 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003021 struct shell_surface *shsurf;
3022
3023 if (get_shell_surface(surface)) {
3024 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003025 WL_DISPLAY_ERROR_INVALID_OBJECT,
3026 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003027 return;
3028 }
3029
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003030 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003031 if (!shsurf) {
3032 wl_resource_post_error(surface_resource,
3033 WL_DISPLAY_ERROR_INVALID_OBJECT,
3034 "surface->configure already set");
3035 return;
3036 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003037
Jason Ekstranda85118c2013-06-27 20:17:02 -05003038 shsurf->resource =
3039 wl_resource_create(client,
3040 &wl_shell_surface_interface, 1, id);
3041 wl_resource_set_implementation(shsurf->resource,
3042 &shell_surface_implementation,
3043 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003044}
3045
Rafael Antognollie2a34552013-12-03 15:35:45 -02003046static bool
3047shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3048{
3049 return wl_resource_instance_of(shsurf->resource,
3050 &wl_shell_surface_interface,
3051 &shell_surface_implementation);
3052}
3053
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003054static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003055 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003056};
3057
Rafael Antognollie2a34552013-12-03 15:35:45 -02003058/****************************
3059 * xdg-shell implementation */
3060
3061static void
3062xdg_surface_destroy(struct wl_client *client,
3063 struct wl_resource *resource)
3064{
3065 wl_resource_destroy(resource);
3066}
3067
3068static void
3069xdg_surface_pong(struct wl_client *client,
3070 struct wl_resource *resource,
3071 uint32_t serial)
3072{
3073 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3074
3075 surface_pong(shsurf, serial);
3076}
3077
3078static void
3079xdg_surface_set_app_id(struct wl_client *client,
3080 struct wl_resource *resource,
3081 const char *app_id)
3082{
3083 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3084
3085 free(shsurf->class);
3086 shsurf->class = strdup(app_id);
3087}
3088
3089static void
3090xdg_surface_set_title(struct wl_client *client,
3091 struct wl_resource *resource, const char *title)
3092{
3093 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3094
3095 set_title(shsurf, title);
3096}
3097
3098static void
3099xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3100 struct wl_resource *seat_resource, uint32_t serial)
3101{
3102 common_surface_move(resource, seat_resource, serial);
3103}
3104
3105static void
3106xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3107 struct wl_resource *seat_resource, uint32_t serial,
3108 uint32_t edges)
3109{
3110 common_surface_resize(resource, seat_resource, serial, edges);
3111}
3112
3113static void
3114xdg_surface_set_output(struct wl_client *client,
3115 struct wl_resource *resource,
3116 struct wl_resource *output_resource)
3117{
3118 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3119 struct weston_output *output;
3120
3121 if (output_resource)
3122 output = wl_resource_get_user_data(output_resource);
3123 else
3124 output = NULL;
3125
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003126 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003127}
3128
3129static void
3130xdg_surface_set_fullscreen(struct wl_client *client,
3131 struct wl_resource *resource)
3132{
3133 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3134
3135 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3136 return;
3137
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003138 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003139 set_fullscreen(shsurf,
3140 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003141 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003142}
3143
3144static void
3145xdg_surface_unset_fullscreen(struct wl_client *client,
3146 struct wl_resource *resource)
3147{
3148 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003149 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003150
3151 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3152 return;
3153
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003154 if (!shsurf->next_state.fullscreen)
3155 return;
3156
Rafael Antognollie2a34552013-12-03 15:35:45 -02003157 shsurf->next_state.fullscreen = false;
3158 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003159
3160 if (shsurf->saved_size_valid) {
3161 width = shsurf->saved_width;
3162 height = shsurf->saved_height;
3163 shsurf->saved_size_valid = false;
3164 } else {
3165 width = shsurf->surface->width;
3166 height = shsurf->surface->height;
3167 }
3168
3169 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003170}
3171
3172static void
3173xdg_surface_set_maximized(struct wl_client *client,
3174 struct wl_resource *resource)
3175{
3176 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3177
3178 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3179 return;
3180
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003181 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003182 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003183}
3184
3185static void
3186xdg_surface_unset_maximized(struct wl_client *client,
3187 struct wl_resource *resource)
3188{
3189 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003190 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003191
3192 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3193 return;
3194
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003195 if (!shsurf->next_state.maximized)
3196 return;
3197
Rafael Antognollie2a34552013-12-03 15:35:45 -02003198 shsurf->next_state.maximized = false;
3199 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003200
3201 if (shsurf->saved_size_valid) {
3202 width = shsurf->saved_width;
3203 height = shsurf->saved_height;
3204 shsurf->saved_size_valid = false;
3205 } else {
3206 width = shsurf->surface->width;
3207 height = shsurf->surface->height;
3208 }
3209
3210 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003211}
3212
3213static const struct xdg_surface_interface xdg_surface_implementation = {
3214 xdg_surface_destroy,
3215 xdg_surface_set_transient_for,
3216 xdg_surface_set_title,
3217 xdg_surface_set_app_id,
3218 xdg_surface_pong,
3219 xdg_surface_move,
3220 xdg_surface_resize,
3221 xdg_surface_set_output,
3222 xdg_surface_set_fullscreen,
3223 xdg_surface_unset_fullscreen,
3224 xdg_surface_set_maximized,
3225 xdg_surface_unset_maximized,
3226 NULL /* set_minimized */
3227};
3228
3229static void
3230xdg_send_configure(struct weston_surface *surface,
3231 uint32_t edges, int32_t width, int32_t height)
3232{
3233 struct shell_surface *shsurf = get_shell_surface(surface);
3234
3235 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3236}
3237
3238static const struct weston_shell_client xdg_client = {
3239 xdg_send_configure
3240};
3241
3242static void
3243xdg_use_unstable_version(struct wl_client *client,
3244 struct wl_resource *resource,
3245 int32_t version)
3246{
3247 if (version > 1) {
3248 wl_resource_post_error(resource,
3249 1,
3250 "xdg-shell:: version not implemented yet.");
3251 return;
3252 }
3253}
3254
3255static struct shell_surface *
3256create_xdg_surface(void *shell, struct weston_surface *surface,
3257 const struct weston_shell_client *client)
3258{
3259 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003260
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003261 shsurf = create_common_surface(shell, surface, client);
3262 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003263
3264 return shsurf;
3265}
3266
3267static void
3268xdg_get_xdg_surface(struct wl_client *client,
3269 struct wl_resource *resource,
3270 uint32_t id,
3271 struct wl_resource *surface_resource)
3272{
3273 struct weston_surface *surface =
3274 wl_resource_get_user_data(surface_resource);
3275 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3276 struct shell_surface *shsurf;
3277
3278 if (get_shell_surface(surface)) {
3279 wl_resource_post_error(surface_resource,
3280 WL_DISPLAY_ERROR_INVALID_OBJECT,
3281 "desktop_shell::get_shell_surface already requested");
3282 return;
3283 }
3284
3285 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3286 if (!shsurf) {
3287 wl_resource_post_error(surface_resource,
3288 WL_DISPLAY_ERROR_INVALID_OBJECT,
3289 "surface->configure already set");
3290 return;
3291 }
3292
3293 shsurf->resource =
3294 wl_resource_create(client,
3295 &xdg_surface_interface, 1, id);
3296 wl_resource_set_implementation(shsurf->resource,
3297 &xdg_surface_implementation,
3298 shsurf, shell_destroy_shell_surface);
3299}
3300
3301static bool
3302shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3303{
3304 return wl_resource_instance_of(shsurf->resource,
3305 &xdg_surface_interface,
3306 &xdg_surface_implementation);
3307}
3308
3309/* xdg-popup implementation */
3310
3311static void
3312xdg_popup_destroy(struct wl_client *client,
3313 struct wl_resource *resource)
3314{
3315 wl_resource_destroy(resource);
3316}
3317
3318static void
3319xdg_popup_pong(struct wl_client *client,
3320 struct wl_resource *resource,
3321 uint32_t serial)
3322{
3323 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3324
3325 surface_pong(shsurf, serial);
3326}
3327
3328static const struct xdg_popup_interface xdg_popup_implementation = {
3329 xdg_popup_destroy,
3330 xdg_popup_pong
3331};
3332
3333static void
3334xdg_popup_send_configure(struct weston_surface *surface,
3335 uint32_t edges, int32_t width, int32_t height)
3336{
3337}
3338
3339static const struct weston_shell_client xdg_popup_client = {
3340 xdg_popup_send_configure
3341};
3342
3343static struct shell_surface *
3344create_xdg_popup(void *shell, struct weston_surface *surface,
3345 const struct weston_shell_client *client,
3346 struct weston_surface *parent,
3347 struct shell_seat *seat,
3348 uint32_t serial,
3349 int32_t x, int32_t y)
3350{
3351 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003352
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003353 shsurf = create_common_surface(shell, surface, client);
3354 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003355 shsurf->popup.shseat = seat;
3356 shsurf->popup.serial = serial;
3357 shsurf->popup.x = x;
3358 shsurf->popup.y = y;
3359 shell_surface_set_parent(shsurf, parent);
3360
3361 return shsurf;
3362}
3363
3364static void
3365xdg_get_xdg_popup(struct wl_client *client,
3366 struct wl_resource *resource,
3367 uint32_t id,
3368 struct wl_resource *surface_resource,
3369 struct wl_resource *parent_resource,
3370 struct wl_resource *seat_resource,
3371 uint32_t serial,
3372 int32_t x, int32_t y, uint32_t flags)
3373{
3374 struct weston_surface *surface =
3375 wl_resource_get_user_data(surface_resource);
3376 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3377 struct shell_surface *shsurf;
3378 struct weston_surface *parent;
3379 struct shell_seat *seat;
3380
3381 if (get_shell_surface(surface)) {
3382 wl_resource_post_error(surface_resource,
3383 WL_DISPLAY_ERROR_INVALID_OBJECT,
3384 "desktop_shell::get_shell_surface already requested");
3385 return;
3386 }
3387
3388 if (!parent_resource) {
3389 wl_resource_post_error(surface_resource,
3390 WL_DISPLAY_ERROR_INVALID_OBJECT,
3391 "xdg_shell::get_xdg_popup requires a parent shell surface");
3392 }
3393
3394 parent = wl_resource_get_user_data(parent_resource);
3395 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3396
3397 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3398 parent, seat, serial, x, y);
3399 if (!shsurf) {
3400 wl_resource_post_error(surface_resource,
3401 WL_DISPLAY_ERROR_INVALID_OBJECT,
3402 "surface->configure already set");
3403 return;
3404 }
3405
3406 shsurf->resource =
3407 wl_resource_create(client,
3408 &xdg_popup_interface, 1, id);
3409 wl_resource_set_implementation(shsurf->resource,
3410 &xdg_popup_implementation,
3411 shsurf, shell_destroy_shell_surface);
3412}
3413
3414static bool
3415shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3416{
3417 return wl_resource_instance_of(shsurf->resource,
3418 &xdg_popup_interface,
3419 &xdg_popup_implementation);
3420}
3421
3422static const struct xdg_shell_interface xdg_implementation = {
3423 xdg_use_unstable_version,
3424 xdg_get_xdg_surface,
3425 xdg_get_xdg_popup
3426};
3427
3428static int
3429xdg_shell_unversioned_dispatch(const void *implementation,
3430 void *_target, uint32_t opcode,
3431 const struct wl_message *message,
3432 union wl_argument *args)
3433{
3434 struct wl_resource *resource = _target;
3435 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3436
3437 if (opcode != 0) {
3438 wl_resource_post_error(resource,
3439 WL_DISPLAY_ERROR_INVALID_OBJECT,
3440 "must call use_unstable_version first");
3441 return 0;
3442 }
3443
3444#define XDG_SERVER_VERSION 1
3445
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003446 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3447 "shell implementation doesn't match protocol version");
3448
Rafael Antognollie2a34552013-12-03 15:35:45 -02003449 if (args[0].i != XDG_SERVER_VERSION) {
3450 wl_resource_post_error(resource,
3451 WL_DISPLAY_ERROR_INVALID_OBJECT,
3452 "incompatible version, server is %d "
3453 "client wants %d",
3454 XDG_SERVER_VERSION, args[0].i);
3455 return 0;
3456 }
3457
3458 wl_resource_set_implementation(resource, &xdg_implementation,
3459 shell, NULL);
3460
3461 return 1;
3462}
3463
3464/* end of xdg-shell implementation */
3465/***********************************/
3466
Kristian Høgsberg07937562011-04-12 17:25:42 -04003467static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003468shell_fade(struct desktop_shell *shell, enum fade_type type);
3469
3470static int
3471screensaver_timeout(void *data)
3472{
3473 struct desktop_shell *shell = data;
3474
3475 shell_fade(shell, FADE_OUT);
3476
3477 return 1;
3478}
3479
3480static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003481handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003482{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003483 struct desktop_shell *shell =
3484 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003485
Pekka Paalanen18027e52011-12-02 16:31:49 +02003486 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003487
3488 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003489 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003490}
3491
3492static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003493launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003494{
3495 if (shell->screensaver.binding)
3496 return;
3497
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003498 if (!shell->screensaver.path) {
3499 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003500 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003501 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003502
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003503 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003504 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003505 return;
3506 }
3507
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003508 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003509 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003510 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003511 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003512}
3513
3514static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003515terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003516{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003517 if (shell->screensaver.process.pid == 0)
3518 return;
3519
3520 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003521}
3522
3523static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003524configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003525{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003526 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003527
Jason Ekstranda7af7042013-10-12 22:38:11 -05003528 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3529 if (v->output == ev->output && v != ev) {
3530 weston_view_unmap(v);
3531 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003532 }
3533 }
3534
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003535 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003536
Jason Ekstranda7af7042013-10-12 22:38:11 -05003537 if (wl_list_empty(&ev->layer_link)) {
3538 wl_list_insert(&layer->view_list, &ev->layer_link);
3539 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003540 }
3541}
3542
3543static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003544background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003545{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003546 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003547 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003548
Jason Ekstranda7af7042013-10-12 22:38:11 -05003549 view = container_of(es->views.next, struct weston_view, surface_link);
3550
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003551 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003552}
3553
3554static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003555desktop_shell_set_background(struct wl_client *client,
3556 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003557 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003558 struct wl_resource *surface_resource)
3559{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003560 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003561 struct weston_surface *surface =
3562 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003563 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003564
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003565 if (surface->configure) {
3566 wl_resource_post_error(surface_resource,
3567 WL_DISPLAY_ERROR_INVALID_OBJECT,
3568 "surface role already assigned");
3569 return;
3570 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003571
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3573 weston_view_destroy(view);
3574 view = weston_view_create(surface);
3575
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003576 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003577 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003578 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003580 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003581 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003582 surface->output->width,
3583 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003584}
3585
3586static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003587panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003588{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003589 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003590 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003591
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 view = container_of(es->views.next, struct weston_view, surface_link);
3593
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003594 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003595}
3596
3597static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003598desktop_shell_set_panel(struct wl_client *client,
3599 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003600 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003601 struct wl_resource *surface_resource)
3602{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003603 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003604 struct weston_surface *surface =
3605 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003606 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003607
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003608 if (surface->configure) {
3609 wl_resource_post_error(surface_resource,
3610 WL_DISPLAY_ERROR_INVALID_OBJECT,
3611 "surface role already assigned");
3612 return;
3613 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003614
Jason Ekstranda7af7042013-10-12 22:38:11 -05003615 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3616 weston_view_destroy(view);
3617 view = weston_view_create(surface);
3618
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003619 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003620 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003621 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003622 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003623 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003624 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003625 surface->output->width,
3626 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003627}
3628
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003629static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003630lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003631{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003632 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003633 struct weston_view *view;
3634
3635 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003636
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003637 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003638 return;
3639
Jason Ekstranda7af7042013-10-12 22:38:11 -05003640 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003641
3642 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003643 wl_list_insert(&shell->lock_layer.view_list,
3644 &view->layer_link);
3645 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003646 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003647 }
3648}
3649
3650static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003651handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003652{
Tiago Vignattibe143262012-04-16 17:31:41 +03003653 struct desktop_shell *shell =
3654 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003655
Martin Minarik6d118362012-06-07 18:01:59 +02003656 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003657 shell->lock_surface = NULL;
3658}
3659
3660static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003661desktop_shell_set_lock_surface(struct wl_client *client,
3662 struct wl_resource *resource,
3663 struct wl_resource *surface_resource)
3664{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003665 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003666 struct weston_surface *surface =
3667 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003668
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003669 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003670
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003671 if (!shell->locked)
3672 return;
3673
Pekka Paalanen98262232011-12-01 10:42:22 +02003674 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003675
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003676 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003677 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003678 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003679
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003680 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003681 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003682 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003683}
3684
3685static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003686resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003687{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003688 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003689
Pekka Paalanen77346a62011-11-30 16:26:35 +02003690 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003691
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003692 wl_list_remove(&shell->lock_layer.link);
3693 wl_list_insert(&shell->compositor->cursor_layer.link,
3694 &shell->fullscreen_layer.link);
3695 wl_list_insert(&shell->fullscreen_layer.link,
3696 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003697 if (shell->showing_input_panels) {
3698 wl_list_insert(&shell->panel_layer.link,
3699 &shell->input_panel_layer.link);
3700 wl_list_insert(&shell->input_panel_layer.link,
3701 &ws->layer.link);
3702 } else {
3703 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3704 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003705
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003706 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003707
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003708 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003709 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003710 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003711}
3712
3713static void
3714desktop_shell_unlock(struct wl_client *client,
3715 struct wl_resource *resource)
3716{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003717 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003718
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003719 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003720
3721 if (shell->locked)
3722 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003723}
3724
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003725static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003726desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003727 struct wl_resource *resource,
3728 struct wl_resource *surface_resource)
3729{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003730 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003731
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003732 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003733 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003734}
3735
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003736static void
3737desktop_shell_desktop_ready(struct wl_client *client,
3738 struct wl_resource *resource)
3739{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003740 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003741
3742 shell_fade_startup(shell);
3743}
3744
Kristian Høgsberg75840622011-09-06 13:48:16 -04003745static const struct desktop_shell_interface desktop_shell_implementation = {
3746 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003747 desktop_shell_set_panel,
3748 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003749 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003750 desktop_shell_set_grab_surface,
3751 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003752};
3753
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003754static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003755get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003756{
3757 struct shell_surface *shsurf;
3758
3759 shsurf = get_shell_surface(surface);
3760 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003761 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003762 return shsurf->type;
3763}
3764
Kristian Høgsberg75840622011-09-06 13:48:16 -04003765static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003766move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003767{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003768 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003769 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003770 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003771
Pekka Paalanen01388e22013-04-25 13:57:44 +03003772 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003773 if (surface == NULL)
3774 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003775
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003776 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003777 if (shsurf == NULL || shsurf->state.fullscreen ||
3778 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003779 return;
3780
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003781 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003782}
3783
3784static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003785maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3786{
3787 struct weston_surface *focus = seat->pointer->focus->surface;
3788 struct weston_surface *surface;
3789 struct shell_surface *shsurf;
3790
3791 surface = weston_surface_get_main_surface(focus);
3792 if (surface == NULL)
3793 return;
3794
3795 shsurf = get_shell_surface(surface);
3796 if (shsurf == NULL)
3797 return;
3798
3799 if (!shell_surface_is_xdg_surface(shsurf))
3800 return;
3801
3802 if (shsurf->state.maximized)
3803 xdg_surface_send_request_unset_maximized(shsurf->resource);
3804 else
3805 xdg_surface_send_request_set_maximized(shsurf->resource);
3806}
3807
3808static void
3809fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3810{
3811 struct weston_surface *focus = seat->pointer->focus->surface;
3812 struct weston_surface *surface;
3813 struct shell_surface *shsurf;
3814
3815 surface = weston_surface_get_main_surface(focus);
3816 if (surface == NULL)
3817 return;
3818
3819 shsurf = get_shell_surface(surface);
3820 if (shsurf == NULL)
3821 return;
3822
3823 if (!shell_surface_is_xdg_surface(shsurf))
3824 return;
3825
3826 if (shsurf->state.fullscreen)
3827 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3828 else
3829 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3830}
3831
3832static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003833touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3834{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003835 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003836 struct weston_surface *surface;
3837 struct shell_surface *shsurf;
3838
3839 surface = weston_surface_get_main_surface(focus);
3840 if (surface == NULL)
3841 return;
3842
3843 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003844 if (shsurf == NULL || shsurf->state.fullscreen ||
3845 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003846 return;
3847
3848 surface_touch_move(shsurf, (struct weston_seat *) seat);
3849}
3850
3851static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003852resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003853{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003854 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003855 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003856 uint32_t edges = 0;
3857 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003858 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003859
Pekka Paalanen01388e22013-04-25 13:57:44 +03003860 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003861 if (surface == NULL)
3862 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003863
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003864 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003865 if (shsurf == NULL || shsurf->state.fullscreen ||
3866 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003867 return;
3868
Jason Ekstranda7af7042013-10-12 22:38:11 -05003869 weston_view_from_global(shsurf->view,
3870 wl_fixed_to_int(seat->pointer->grab_x),
3871 wl_fixed_to_int(seat->pointer->grab_y),
3872 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003873
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003874 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003875 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003876 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003877 edges |= 0;
3878 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003879 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003880
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003881 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003882 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003883 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003884 edges |= 0;
3885 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003886 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003887
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003888 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003889}
3890
3891static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003892surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003893 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003894{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003895 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003896 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003897 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003898 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003899
Pekka Paalanen01388e22013-04-25 13:57:44 +03003900 /* XXX: broken for windows containing sub-surfaces */
3901 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003902 if (surface == NULL)
3903 return;
3904
3905 shsurf = get_shell_surface(surface);
3906 if (!shsurf)
3907 return;
3908
Jason Ekstranda7af7042013-10-12 22:38:11 -05003909 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003910
Jason Ekstranda7af7042013-10-12 22:38:11 -05003911 if (shsurf->view->alpha > 1.0)
3912 shsurf->view->alpha = 1.0;
3913 if (shsurf->view->alpha < step)
3914 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003915
Jason Ekstranda7af7042013-10-12 22:38:11 -05003916 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003917 weston_surface_damage(surface);
3918}
3919
3920static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003921do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003922 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003923{
Daniel Stone37816df2012-05-16 18:45:18 +01003924 struct weston_seat *ws = (struct weston_seat *) seat;
3925 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003926 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003927 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003928
3929 wl_list_for_each(output, &compositor->output_list, link) {
3930 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003931 wl_fixed_to_double(seat->pointer->x),
3932 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003933 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003934 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003935 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003936 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003937 increment = -output->zoom.increment;
3938 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003939 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003940 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003941 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003942 else
3943 increment = 0;
3944
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003945 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003946
Scott Moreaue6603982012-06-11 13:07:51 -06003947 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003948 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003949 else if (output->zoom.level > output->zoom.max_level)
3950 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003951 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003952 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003953 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003954
Scott Moreaue6603982012-06-11 13:07:51 -06003955 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003956
Jason Ekstranda7af7042013-10-12 22:38:11 -05003957 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003958 }
3959 }
3960}
3961
Scott Moreauccbf29d2012-02-22 14:21:41 -07003962static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003963zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003964 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003965{
3966 do_zoom(seat, time, 0, axis, value);
3967}
3968
3969static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003970zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003971 void *data)
3972{
3973 do_zoom(seat, time, key, 0, 0);
3974}
3975
3976static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003977terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003978 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003979{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003980 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003981
Daniel Stone325fc2d2012-05-30 16:31:58 +01003982 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003983}
3984
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003985struct alt_tab {
3986 struct desktop_shell *shell;
3987 struct weston_keyboard_grab grab;
3988
3989 struct wl_list *current;
3990
3991 struct wl_list preview_list;
3992};
3993
3994struct alt_tab_preview {
3995 struct alt_tab *alt_tab;
3996
3997 struct weston_view *view;
3998 struct weston_transform transform;
3999
4000 struct wl_listener listener;
4001
4002 struct wl_list link;
4003};
4004
4005static void
4006alt_tab_next(struct alt_tab *alt_tab)
4007{
4008 alt_tab->current = alt_tab->current->next;
4009
4010 /* Make sure we're not pointing to the list header e.g. after
4011 * cycling through the whole list. */
4012 if (alt_tab->current->next == alt_tab->preview_list.next)
4013 alt_tab->current = alt_tab->current->next;
4014}
4015
4016static void
4017alt_tab_destroy(struct alt_tab *alt_tab)
4018{
4019 struct alt_tab_preview *preview, *next;
4020 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
4021
4022 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
4023 preview = wl_container_of(alt_tab->current, preview, link);
4024
4025 activate(alt_tab->shell, preview->view->surface,
4026 (struct weston_seat *) keyboard->seat);
4027 }
4028
4029 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
4030 wl_list_remove(&preview->link);
4031 wl_list_remove(&preview->listener.link);
4032 weston_view_destroy(preview->view);
4033 free(preview);
4034 }
4035
4036 weston_keyboard_end_grab(keyboard);
4037 if (keyboard->input_method_resource)
4038 keyboard->grab = &keyboard->input_method_grab;
4039
4040 free(alt_tab);
4041}
4042
4043static void
4044alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
4045{
4046 struct alt_tab_preview *preview =
4047 container_of(listener, struct alt_tab_preview, listener);
4048 struct alt_tab *alt_tab = preview->alt_tab;
4049 int advance = 0;
4050
4051 /* If the preview that we're removing is the currently selected one,
4052 * we want to move to the next one. So we move to ->prev and then
4053 * call _next() after removing the preview. */
4054 if (alt_tab->current == &preview->link) {
4055 alt_tab->current = alt_tab->current->prev;
4056 advance = 1;
4057 }
4058
4059 wl_list_remove(&preview->listener.link);
4060 wl_list_remove(&preview->link);
4061
4062 free(preview);
4063
4064 if (advance)
4065 alt_tab_next(alt_tab);
4066
4067 /* If the last preview goes away, stop the alt-tab */
4068 if (wl_list_empty(alt_tab->current))
4069 alt_tab_destroy(alt_tab);
4070}
4071
4072static void
4073alt_tab_key(struct weston_keyboard_grab *grab,
4074 uint32_t time, uint32_t key, uint32_t state_w)
4075{
4076 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4077 enum wl_keyboard_key_state state = state_w;
4078
4079 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4080 alt_tab_next(alt_tab);
4081}
4082
4083static void
4084alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4085 uint32_t mods_depressed, uint32_t mods_latched,
4086 uint32_t mods_locked, uint32_t group)
4087{
4088 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4089 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
4090
4091 if ((seat->modifier_state & MODIFIER_ALT) == 0)
4092 alt_tab_destroy(alt_tab);
4093}
4094
4095static void
4096alt_tab_cancel(struct weston_keyboard_grab *grab)
4097{
4098 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4099
4100 alt_tab_destroy(alt_tab);
4101}
4102
4103static const struct weston_keyboard_grab_interface alt_tab_grab = {
4104 alt_tab_key,
4105 alt_tab_modifier,
4106 alt_tab_cancel,
4107};
4108
4109static int
4110view_for_alt_tab(struct weston_view *view)
4111{
Rafael Antognollied207b42013-12-03 15:35:43 -02004112 struct shell_surface *shsurf = get_shell_surface(view->surface);
4113 if (!shsurf)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004114 return 0;
4115
Rafael Antognollied207b42013-12-03 15:35:43 -02004116 if (shsurf->parent)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004117 return 0;
4118
4119 if (view != get_default_view(view->surface))
4120 return 0;
4121
4122 return 1;
4123}
4124
4125static void
4126alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4127 void *data)
4128{
4129 struct alt_tab *alt_tab;
4130 struct desktop_shell *shell = data;
4131 struct weston_output *output = get_default_output(shell->compositor);
4132 struct workspace *ws;
4133 struct weston_view *view;
4134 int num_surfaces = 0;
4135 int x, y, side, margin;
4136
4137 wl_list_for_each(view, &shell->compositor->view_list, link) {
4138
4139 if (view_for_alt_tab(view))
4140 num_surfaces++;
4141 }
4142
4143 if (!num_surfaces)
4144 return;
4145
4146 alt_tab = malloc(sizeof *alt_tab);
4147 if (!alt_tab)
4148 return;
4149
4150 alt_tab->shell = shell;
4151 wl_list_init(&alt_tab->preview_list);
4152 alt_tab->current = &alt_tab->preview_list;
4153
4154 restore_all_output_modes(shell->compositor);
4155 lower_fullscreen_layer(alt_tab->shell);
4156
4157 alt_tab->grab.interface = &alt_tab_grab;
4158 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
4159 weston_keyboard_set_focus(seat->keyboard, NULL);
4160
4161 ws = get_current_workspace(shell);
4162
4163 /* FIXME: add some visual candy e.g. prelight the selected view
4164 * and/or add a black surrounding background à la gnome-shell */
4165
4166 /* Determine the size for each preview */
4167 side = output->width / num_surfaces;
4168 if (side > 200)
4169 side = 200;
4170 margin = side / 4;
4171 side -= margin;
4172
4173 x = margin;
4174 y = (output->height - side) / 2;
4175
4176 /* Create a view for each surface */
4177 wl_list_for_each(view, &shell->compositor->view_list, link) {
4178 struct alt_tab_preview *preview;
4179 struct weston_view *v;
4180 float scale;
4181
4182 if (!view_for_alt_tab(view))
4183 continue;
4184
4185 preview = malloc(sizeof *preview);
4186 if (!preview) {
4187 alt_tab_destroy(alt_tab);
4188 return;
4189 }
4190
4191 preview->alt_tab = alt_tab;
4192
4193 preview->view = v = weston_view_create(view->surface);
4194 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004195
4196 wl_list_remove(&v->layer_link);
4197 wl_list_insert(&ws->layer.view_list, &v->layer_link);
4198 weston_view_damage_below(v);
4199 weston_surface_damage(v->surface);
4200
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004201 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004202
4203 preview->listener.notify = alt_tab_handle_surface_destroy;
4204 wl_signal_add(&v->destroy_signal, &preview->listener);
4205
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004206 if (view->surface->width > view->surface->height)
4207 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004208 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004209 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004210
4211 wl_list_insert(&v->geometry.transformation_list,
4212 &preview->transform.link);
4213 weston_matrix_init(&preview->transform.matrix);
4214 weston_matrix_scale(&preview->transform.matrix,
4215 scale, scale, 1.0f);
4216
4217 weston_view_geometry_dirty(v);
4218 weston_compositor_schedule_repaint(v->surface->compositor);
4219
4220 /* Insert at the end of the list */
4221 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
4222
4223 x += side + margin;
4224 }
4225
4226 /* Start at the second preview so a simple <alt>tab changes window.
4227 * We set `current' to the first preview and not the second because
4228 * we're going to receive a key press callback for the initial
4229 * <alt>tab which will make `current' point to the second element. */
4230 alt_tab_next(alt_tab);
4231}
4232
4233static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004234rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4235 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004236{
4237 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004238 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004239 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004240 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004241 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004242
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004243 weston_pointer_move(pointer, x, y);
4244
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004245 if (!shsurf)
4246 return;
4247
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004248 cx = 0.5f * shsurf->surface->width;
4249 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004250
Daniel Stone37816df2012-05-16 18:45:18 +01004251 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4252 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004253 r = sqrtf(dx * dx + dy * dy);
4254
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004255 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004256 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004257
4258 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004259 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004260 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004261
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004262 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004263 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004264
4265 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004266 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004267 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004268 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004269 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004270
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004271 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004272 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004273 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004274 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004275 wl_list_init(&shsurf->rotation.transform.link);
4276 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004277 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004278 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004279
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004280 /* We need to adjust the position of the surface
4281 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004282 cposx = shsurf->view->geometry.x + cx;
4283 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004284 dposx = rotate->center.x - cposx;
4285 dposy = rotate->center.y - cposy;
4286 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004287 weston_view_set_position(shsurf->view,
4288 shsurf->view->geometry.x + dposx,
4289 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004290 }
4291
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004292 /* Repaint implies weston_surface_update_transform(), which
4293 * lazily applies the damage due to rotation update.
4294 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004295 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004296}
4297
4298static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004299rotate_grab_button(struct weston_pointer_grab *grab,
4300 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004301{
4302 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004303 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004304 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004305 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004306 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004307
Daniel Stone4dbadb12012-05-30 16:31:51 +01004308 if (pointer->button_count == 0 &&
4309 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004310 if (shsurf)
4311 weston_matrix_multiply(&shsurf->rotation.rotation,
4312 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004313 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004314 free(rotate);
4315 }
4316}
4317
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004318static void
4319rotate_grab_cancel(struct weston_pointer_grab *grab)
4320{
4321 struct rotate_grab *rotate =
4322 container_of(grab, struct rotate_grab, base.grab);
4323
4324 shell_grab_end(&rotate->base);
4325 free(rotate);
4326}
4327
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004328static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004329 noop_grab_focus,
4330 rotate_grab_motion,
4331 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004332 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004333};
4334
4335static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004336surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004337{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004338 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004339 float dx, dy;
4340 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004341
Pekka Paalanen460099f2012-01-20 16:48:25 +02004342 rotate = malloc(sizeof *rotate);
4343 if (!rotate)
4344 return;
4345
Jason Ekstranda7af7042013-10-12 22:38:11 -05004346 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004347 surface->surface->width * 0.5f,
4348 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004349 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004350
Daniel Stone37816df2012-05-16 18:45:18 +01004351 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4352 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004353 r = sqrtf(dx * dx + dy * dy);
4354 if (r > 20.0f) {
4355 struct weston_matrix inverse;
4356
4357 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004358 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004359 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004360
4361 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004362 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004363 } else {
4364 weston_matrix_init(&surface->rotation.rotation);
4365 weston_matrix_init(&rotate->rotation);
4366 }
4367
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004368 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4369 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004370}
4371
4372static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004373rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004374 void *data)
4375{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004376 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004377 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004378 struct shell_surface *surface;
4379
Pekka Paalanen01388e22013-04-25 13:57:44 +03004380 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004381 if (base_surface == NULL)
4382 return;
4383
4384 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004385 if (surface == NULL || surface->state.fullscreen ||
4386 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004387 return;
4388
4389 surface_rotate(surface, seat);
4390}
4391
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004392/* Move all fullscreen layers down to the current workspace in a non-reversible
4393 * manner. This should be used when implementing shell-wide overlays, such as
4394 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004395void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004396lower_fullscreen_layer(struct desktop_shell *shell)
4397{
4398 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004399 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004400
4401 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 wl_list_for_each_reverse_safe(view, prev,
4403 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004404 layer_link) {
4405 wl_list_remove(&view->layer_link);
4406 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4407 weston_view_damage_below(view);
4408 weston_surface_damage(view->surface);
4409 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004410}
4411
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004412void
Tiago Vignattibe143262012-04-16 17:31:41 +03004413activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004414 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004415{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004416 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004417 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004418 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004419 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004420 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004421
Pekka Paalanen01388e22013-04-25 13:57:44 +03004422 main_surface = weston_surface_get_main_surface(es);
4423
Daniel Stone37816df2012-05-16 18:45:18 +01004424 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004425
Jonas Ådahl8538b222012-08-29 22:13:03 +02004426 state = ensure_focus_state(shell, seat);
4427 if (state == NULL)
4428 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004429
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004430 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004431 state->keyboard_focus = es;
4432 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004433 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004434
Rafael Antognolli03b16592013-12-03 15:35:42 -02004435 shsurf = get_shell_surface(main_surface);
4436 if (shsurf->state.fullscreen)
4437 shell_configure_fullscreen(shsurf);
4438 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004439 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004440
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004441 if (shell->focus_animation_type != ANIMATION_NONE) {
4442 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004443 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004444 }
4445
4446 /* Update the surface’s layer. This brings it to the top of the stacking
4447 * order as appropriate. */
4448 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004449}
4450
Alex Wu21858432012-04-01 20:13:08 +08004451/* no-op func for checking black surface */
4452static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004453black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004454{
4455}
4456
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004457static bool
Alex Wu21858432012-04-01 20:13:08 +08004458is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4459{
4460 if (es->configure == black_surface_configure) {
4461 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004462 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004463 return true;
4464 }
4465 return false;
4466}
4467
Kristian Høgsberg75840622011-09-06 13:48:16 -04004468static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004469activate_binding(struct weston_seat *seat,
4470 struct desktop_shell *shell,
4471 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004472{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004473 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004474
Alex Wu9c35e6b2012-03-05 14:13:13 +08004475 if (!focus)
4476 return;
4477
Pekka Paalanen01388e22013-04-25 13:57:44 +03004478 if (is_black_surface(focus, &main_surface))
4479 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004480
Pekka Paalanen01388e22013-04-25 13:57:44 +03004481 main_surface = weston_surface_get_main_surface(focus);
4482 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004483 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004484
Neil Robertsa28c6932013-10-03 16:43:04 +01004485 activate(shell, focus, seat);
4486}
4487
4488static void
4489click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4490 void *data)
4491{
4492 if (seat->pointer->grab != &seat->pointer->default_grab)
4493 return;
4494
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004495 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004496}
4497
4498static void
4499touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4500{
4501 if (seat->touch->grab != &seat->touch->default_grab)
4502 return;
4503
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004504 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004505}
4506
4507static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004508lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004509{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004510 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004511
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004512 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004513 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004514 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004515 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004516
4517 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004518
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004519 /* Hide all surfaces by removing the fullscreen, panel and
4520 * toplevel layers. This way nothing else can show or receive
4521 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004522
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004523 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004524 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004525 if (shell->showing_input_panels)
4526 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004527 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004528 wl_list_insert(&shell->compositor->cursor_layer.link,
4529 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004530
Pekka Paalanen77346a62011-11-30 16:26:35 +02004531 launch_screensaver(shell);
4532
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004533 /* TODO: disable bindings that should not work while locked. */
4534
4535 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004536}
4537
4538static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004539unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004540{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004541 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004542 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004543 return;
4544 }
4545
4546 /* If desktop-shell client has gone away, unlock immediately. */
4547 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004548 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004549 return;
4550 }
4551
4552 if (shell->prepare_event_sent)
4553 return;
4554
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004555 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004556 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004557}
4558
4559static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004560shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004561{
4562 struct desktop_shell *shell = data;
4563
4564 shell->fade.animation = NULL;
4565
4566 switch (shell->fade.type) {
4567 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004568 weston_surface_destroy(shell->fade.view->surface);
4569 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004570 break;
4571 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004572 lock(shell);
4573 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004574 default:
4575 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004576 }
4577}
4578
Jason Ekstranda7af7042013-10-12 22:38:11 -05004579static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004580shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004581{
4582 struct weston_compositor *compositor = shell->compositor;
4583 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004584 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004585
4586 surface = weston_surface_create(compositor);
4587 if (!surface)
4588 return NULL;
4589
Jason Ekstranda7af7042013-10-12 22:38:11 -05004590 view = weston_view_create(surface);
4591 if (!view) {
4592 weston_surface_destroy(surface);
4593 return NULL;
4594 }
4595
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004596 surface->width = 8192;
4597 surface->height = 8192;
4598 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004599 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004600 wl_list_insert(&compositor->fade_layer.view_list,
4601 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004602 pixman_region32_init(&surface->input);
4603
Jason Ekstranda7af7042013-10-12 22:38:11 -05004604 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004605}
4606
4607static void
4608shell_fade(struct desktop_shell *shell, enum fade_type type)
4609{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004610 float tint;
4611
4612 switch (type) {
4613 case FADE_IN:
4614 tint = 0.0;
4615 break;
4616 case FADE_OUT:
4617 tint = 1.0;
4618 break;
4619 default:
4620 weston_log("shell: invalid fade type\n");
4621 return;
4622 }
4623
4624 shell->fade.type = type;
4625
Jason Ekstranda7af7042013-10-12 22:38:11 -05004626 if (shell->fade.view == NULL) {
4627 shell->fade.view = shell_fade_create_surface(shell);
4628 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004629 return;
4630
Jason Ekstranda7af7042013-10-12 22:38:11 -05004631 shell->fade.view->alpha = 1.0 - tint;
4632 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004633 }
4634
4635 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004636 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004637 else
4638 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004639 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004640 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004641 shell_fade_done, shell);
4642}
4643
4644static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004645do_shell_fade_startup(void *data)
4646{
4647 struct desktop_shell *shell = data;
4648
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004649 if (shell->startup_animation_type == ANIMATION_FADE)
4650 shell_fade(shell, FADE_IN);
4651 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004652 weston_surface_destroy(shell->fade.view->surface);
4653 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004654 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004655}
4656
4657static void
4658shell_fade_startup(struct desktop_shell *shell)
4659{
4660 struct wl_event_loop *loop;
4661
4662 if (!shell->fade.startup_timer)
4663 return;
4664
4665 wl_event_source_remove(shell->fade.startup_timer);
4666 shell->fade.startup_timer = NULL;
4667
4668 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4669 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4670}
4671
4672static int
4673fade_startup_timeout(void *data)
4674{
4675 struct desktop_shell *shell = data;
4676
4677 shell_fade_startup(shell);
4678 return 0;
4679}
4680
4681static void
4682shell_fade_init(struct desktop_shell *shell)
4683{
4684 /* Make compositor output all black, and wait for the desktop-shell
4685 * client to signal it is ready, then fade in. The timer triggers a
4686 * fade-in, in case the desktop-shell client takes too long.
4687 */
4688
4689 struct wl_event_loop *loop;
4690
Jason Ekstranda7af7042013-10-12 22:38:11 -05004691 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004692 weston_log("%s: warning: fade surface already exists\n",
4693 __func__);
4694 return;
4695 }
4696
Jason Ekstranda7af7042013-10-12 22:38:11 -05004697 shell->fade.view = shell_fade_create_surface(shell);
4698 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004699 return;
4700
Jason Ekstranda7af7042013-10-12 22:38:11 -05004701 weston_view_update_transform(shell->fade.view);
4702 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004703
4704 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4705 shell->fade.startup_timer =
4706 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4707 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4708}
4709
4710static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004711idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004712{
4713 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004714 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004715
4716 shell_fade(shell, FADE_OUT);
4717 /* lock() is called from shell_fade_done() */
4718}
4719
4720static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004721wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004722{
4723 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004724 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004725
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004726 unlock(shell);
4727}
4728
4729static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004730center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004731{
Giulio Camuffob8366642013-04-25 13:57:46 +03004732 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004733 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004734
Jason Ekstranda7af7042013-10-12 22:38:11 -05004735 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004736
4737 x = output->x + (output->width - width) / 2 - surf_x / 2;
4738 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004739
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004740 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004741}
4742
4743static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004744weston_view_set_initial_position(struct weston_view *view,
4745 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004746{
4747 struct weston_compositor *compositor = shell->compositor;
4748 int ix = 0, iy = 0;
4749 int range_x, range_y;
4750 int dx, dy, x, y, panel_height;
4751 struct weston_output *output, *target_output = NULL;
4752 struct weston_seat *seat;
4753
4754 /* As a heuristic place the new window on the same output as the
4755 * pointer. Falling back to the output containing 0, 0.
4756 *
4757 * TODO: Do something clever for touch too?
4758 */
4759 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004760 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004761 ix = wl_fixed_to_int(seat->pointer->x);
4762 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004763 break;
4764 }
4765 }
4766
4767 wl_list_for_each(output, &compositor->output_list, link) {
4768 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4769 target_output = output;
4770 break;
4771 }
4772 }
4773
4774 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004775 weston_view_set_position(view, 10 + random() % 400,
4776 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004777 return;
4778 }
4779
4780 /* Valid range within output where the surface will still be onscreen.
4781 * If this is negative it means that the surface is bigger than
4782 * output.
4783 */
4784 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004785 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004786 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004787 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004788
Rob Bradford4cb88c72012-08-13 15:18:44 +01004789 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004790 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004791 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004792 dx = 0;
4793
4794 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004795 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004796 else
4797 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004798
4799 x = target_output->x + dx;
4800 y = target_output->y + dy;
4801
Jason Ekstranda7af7042013-10-12 22:38:11 -05004802 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004803}
4804
4805static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004806map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004807 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004808{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004809 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004810 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004811 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004812 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004813
Pekka Paalanen77346a62011-11-30 16:26:35 +02004814 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004815 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004816 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004817 if (shsurf->state.fullscreen) {
4818 center_on_output(shsurf->view, shsurf->fullscreen_output);
4819 shell_map_fullscreen(shsurf);
4820 } else if (shsurf->state.maximized) {
4821 /* use surface configure to set the geometry */
4822 panel_height = get_output_panel_height(shell, shsurf->output);
4823 surface_subsurfaces_boundingbox(shsurf->surface,
4824 &surf_x, &surf_y, NULL, NULL);
4825 weston_view_set_position(shsurf->view,
4826 shsurf->output->x - surf_x,
4827 shsurf->output->y +
4828 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004829 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004830 weston_view_set_initial_position(shsurf->view, shell);
4831 }
Juan Zhao96879df2012-02-07 08:45:41 +08004832 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004833 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004834 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004835 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004836 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004837 weston_view_set_position(shsurf->view,
4838 shsurf->view->geometry.x + sx,
4839 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004840 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004841 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004842 default:
4843 ;
4844 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004845
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004846 /* Surface stacking order, see also activate(). */
4847 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004848
Jason Ekstranda7af7042013-10-12 22:38:11 -05004849 if (shsurf->type != SHELL_SURFACE_NONE) {
4850 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004851 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004852 shsurf->surface->output = shsurf->output;
4853 shsurf->view->output = shsurf->output;
4854 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004855 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004856
Rafael Antognollied207b42013-12-03 15:35:43 -02004857 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4858 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4859 }
4860
Jason Ekstranda7af7042013-10-12 22:38:11 -05004861 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004862 /* XXX: xwayland's using the same fields for transient type */
4863 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004864 if (shsurf->transient.flags ==
4865 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4866 break;
4867 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004868 if (shsurf->state.relative &&
4869 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4870 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004871 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004872 break;
4873 wl_list_for_each(seat, &compositor->seat_list, link)
4874 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004875 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004876 case SHELL_SURFACE_POPUP:
4877 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004878 default:
4879 break;
4880 }
4881
Rafael Antognolli03b16592013-12-03 15:35:42 -02004882 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4883 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004884 {
4885 switch (shell->win_animation_type) {
4886 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004887 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004888 break;
4889 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004890 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004891 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004892 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004893 default:
4894 break;
4895 }
4896 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004897}
4898
4899static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004900configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004901 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004902{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004903 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004904 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004905 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004906
Pekka Paalanen77346a62011-11-30 16:26:35 +02004907 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004908
Rafael Antognolli03b16592013-12-03 15:35:42 -02004909 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004910 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004911 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004912 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004913 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4914 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004915 mx = shsurf->output->x - surf_x;
4916 my = shsurf->output->y +
4917 get_output_panel_height(shell,shsurf->output) - surf_y;
4918 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004919 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004920 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004921 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004922
Alex Wu4539b082012-03-01 12:57:46 +08004923 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004924 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004925 wl_list_for_each(view, &surface->views, surface_link)
4926 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004927
Rafael Antognolli03b16592013-12-03 15:35:42 -02004928 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004929 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004930 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004931}
4932
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004933static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004934shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004935{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004936 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004937 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004938
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004939 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004940
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004941 if (!weston_surface_is_mapped(es) &&
4942 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004943 remove_popup_grab(shsurf);
4944 }
4945
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004946 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004947 return;
4948
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004949 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004950 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004951 type_changed = 1;
4952 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004953
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004954 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004955 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004956 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004957 shsurf->last_width != es->width ||
4958 shsurf->last_height != es->height) {
4959 shsurf->last_width = es->width;
4960 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004961 float from_x, from_y;
4962 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004963
Jason Ekstranda7af7042013-10-12 22:38:11 -05004964 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4965 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004966 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004967 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004968 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004969 }
4970}
4971
Zhang, Xiong Y31236932013-12-13 22:10:57 +02004972static void
4973shell_surface_output_destroyed(struct weston_surface *es)
4974{
4975 struct shell_surface *shsurf = get_shell_surface(es);
4976
4977 shsurf->saved_position_valid = false;
4978}
4979
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004980static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004981
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004982static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004983desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004984{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004985 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004986 struct desktop_shell *shell =
4987 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004988
4989 shell->child.process.pid = 0;
4990 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004991
4992 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4993 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004994 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004995 shell->child.deathstamp = time;
4996 shell->child.deathcount = 0;
4997 }
4998
4999 shell->child.deathcount++;
5000 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005001 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005002 return;
5003 }
5004
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005005 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005006 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005007 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005008}
5009
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005010static void
5011launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005012{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005013 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005014
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005015 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005016 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005017 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005018 desktop_shell_sigchld);
5019
5020 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005021 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005022}
5023
5024static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005025bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5026{
Tiago Vignattibe143262012-04-16 17:31:41 +03005027 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005028 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005029
Jason Ekstranda85118c2013-06-27 20:17:02 -05005030 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
5031 if (resource)
5032 wl_resource_set_implementation(resource, &shell_implementation,
5033 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005034}
5035
Kristian Høgsberg75840622011-09-06 13:48:16 -04005036static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02005037bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5038{
5039 struct desktop_shell *shell = data;
5040 struct wl_resource *resource;
5041
5042 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
5043 if (resource)
5044 wl_resource_set_dispatcher(resource,
5045 xdg_shell_unversioned_dispatch,
5046 NULL, shell, NULL);
5047}
5048
5049static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005050unbind_desktop_shell(struct wl_resource *resource)
5051{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005052 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005053
5054 if (shell->locked)
5055 resume_desktop(shell);
5056
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005057 shell->child.desktop_shell = NULL;
5058 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005059}
5060
5061static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005062bind_desktop_shell(struct wl_client *client,
5063 void *data, uint32_t version, uint32_t id)
5064{
Tiago Vignattibe143262012-04-16 17:31:41 +03005065 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005066 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005067
Jason Ekstranda85118c2013-06-27 20:17:02 -05005068 resource = wl_resource_create(client, &desktop_shell_interface,
5069 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005070
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005071 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005072 wl_resource_set_implementation(resource,
5073 &desktop_shell_implementation,
5074 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005075 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005076
5077 if (version < 2)
5078 shell_fade_startup(shell);
5079
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005080 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005081 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005082
5083 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5084 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005085 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005086}
5087
Pekka Paalanen6e168112011-11-24 11:34:05 +02005088static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005089screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005090{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005091 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005092 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005093
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005094 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005095 return;
5096
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005097 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005098 if (!shell->locked)
5099 return;
5100
Jason Ekstranda7af7042013-10-12 22:38:11 -05005101 view = container_of(surface->views.next, struct weston_view, surface_link);
5102 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005103
Jason Ekstranda7af7042013-10-12 22:38:11 -05005104 if (wl_list_empty(&view->layer_link)) {
5105 wl_list_insert(shell->lock_layer.view_list.prev,
5106 &view->layer_link);
5107 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005108 wl_event_source_timer_update(shell->screensaver.timer,
5109 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005110 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005111 }
5112}
5113
5114static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005115screensaver_set_surface(struct wl_client *client,
5116 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005117 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005118 struct wl_resource *output_resource)
5119{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005120 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005121 struct weston_surface *surface =
5122 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005123 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005124 struct weston_view *view, *next;
5125
5126 /* Make sure we only have one view */
5127 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5128 weston_view_destroy(view);
5129 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005130
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005131 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005132 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005133 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005134}
5135
5136static const struct screensaver_interface screensaver_implementation = {
5137 screensaver_set_surface
5138};
5139
5140static void
5141unbind_screensaver(struct wl_resource *resource)
5142{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005143 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005144
Pekka Paalanen77346a62011-11-30 16:26:35 +02005145 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005146}
5147
5148static void
5149bind_screensaver(struct wl_client *client,
5150 void *data, uint32_t version, uint32_t id)
5151{
Tiago Vignattibe143262012-04-16 17:31:41 +03005152 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005153 struct wl_resource *resource;
5154
Jason Ekstranda85118c2013-06-27 20:17:02 -05005155 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005156
Pekka Paalanen77346a62011-11-30 16:26:35 +02005157 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005158 wl_resource_set_implementation(resource,
5159 &screensaver_implementation,
5160 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005161 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005162 return;
5163 }
5164
5165 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5166 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005167 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005168}
5169
Kristian Høgsberg07045392012-02-19 18:52:44 -05005170struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005171 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005172 struct weston_surface *current;
5173 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005174 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005175};
5176
5177static void
5178switcher_next(struct switcher *switcher)
5179{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005180 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005181 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005182 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005183 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005184
Jason Ekstranda7af7042013-10-12 22:38:11 -05005185 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005186 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005187 if (shsurf &&
5188 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5189 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005190 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005191 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005192 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005193 next = view->surface;
5194 prev = view->surface;
5195 view->alpha = 0.25;
5196 weston_view_geometry_dirty(view);
5197 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005198 }
Alex Wu1659daa2012-04-01 20:13:09 +08005199
Jason Ekstranda7af7042013-10-12 22:38:11 -05005200 if (is_black_surface(view->surface, NULL)) {
5201 view->alpha = 0.25;
5202 weston_view_geometry_dirty(view);
5203 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005204 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005205 }
5206
5207 if (next == NULL)
5208 next = first;
5209
Alex Wu07b26062012-03-12 16:06:01 +08005210 if (next == NULL)
5211 return;
5212
Kristian Høgsberg07045392012-02-19 18:52:44 -05005213 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005214 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005215
5216 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005217 wl_list_for_each(view, &next->views, surface_link)
5218 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005219
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005220 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005221 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005222 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005223}
5224
5225static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005226switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005227{
5228 struct switcher *switcher =
5229 container_of(listener, struct switcher, listener);
5230
5231 switcher_next(switcher);
5232}
5233
5234static void
Daniel Stone351eb612012-05-31 15:27:47 -04005235switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005236{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005237 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005238 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005239 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005240
Jason Ekstranda7af7042013-10-12 22:38:11 -05005241 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005242 if (is_focus_view(view))
5243 continue;
5244
Jason Ekstranda7af7042013-10-12 22:38:11 -05005245 view->alpha = 1.0;
5246 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005247 }
5248
Alex Wu07b26062012-03-12 16:06:01 +08005249 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005250 activate(switcher->shell, switcher->current,
5251 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005252 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005253 weston_keyboard_end_grab(keyboard);
5254 if (keyboard->input_method_resource)
5255 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005256 free(switcher);
5257}
5258
5259static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005260switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005261 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005262{
5263 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005264 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005265
Daniel Stonec9785ea2012-05-30 16:31:52 +01005266 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005267 switcher_next(switcher);
5268}
5269
5270static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005271switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005272 uint32_t mods_depressed, uint32_t mods_latched,
5273 uint32_t mods_locked, uint32_t group)
5274{
5275 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005276 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005277
Daniel Stone351eb612012-05-31 15:27:47 -04005278 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5279 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005280}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005281
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005282static void
5283switcher_cancel(struct weston_keyboard_grab *grab)
5284{
5285 struct switcher *switcher = container_of(grab, struct switcher, grab);
5286
5287 switcher_destroy(switcher);
5288}
5289
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005290static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005291 switcher_key,
5292 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005293 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005294};
5295
5296static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005297switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005298 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005299{
Tiago Vignattibe143262012-04-16 17:31:41 +03005300 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005301 struct switcher *switcher;
5302
5303 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005304 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005305 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005306 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005307 wl_list_init(&switcher->listener.link);
5308
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005309 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005310 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005311 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005312 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5313 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005314 switcher_next(switcher);
5315}
5316
Pekka Paalanen3c647232011-12-22 13:43:43 +02005317static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005318backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005319 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005320{
5321 struct weston_compositor *compositor = data;
5322 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005323 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005324
5325 /* TODO: we're limiting to simple use cases, where we assume just
5326 * control on the primary display. We'd have to extend later if we
5327 * ever get support for setting backlights on random desktop LCD
5328 * panels though */
5329 output = get_default_output(compositor);
5330 if (!output)
5331 return;
5332
5333 if (!output->set_backlight)
5334 return;
5335
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005336 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5337 backlight_new = output->backlight_current - 25;
5338 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5339 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005340
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005341 if (backlight_new < 5)
5342 backlight_new = 5;
5343 if (backlight_new > 255)
5344 backlight_new = 255;
5345
5346 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005347 output->set_backlight(output, output->backlight_current);
5348}
5349
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005350struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005351 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005352 struct weston_seat *seat;
5353 uint32_t key[2];
5354 int key_released[2];
5355};
5356
5357static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005358debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005359 uint32_t key, uint32_t state)
5360{
5361 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005362 struct weston_compositor *ec = db->seat->compositor;
5363 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005364 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005365 uint32_t serial;
5366 int send = 0, terminate = 0;
5367 int check_binding = 1;
5368 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005369 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005370
5371 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5372 /* Do not run bindings on key releases */
5373 check_binding = 0;
5374
5375 for (i = 0; i < 2; i++)
5376 if (key == db->key[i])
5377 db->key_released[i] = 1;
5378
5379 if (db->key_released[0] && db->key_released[1]) {
5380 /* All key releases been swalled so end the grab */
5381 terminate = 1;
5382 } else if (key != db->key[0] && key != db->key[1]) {
5383 /* Should not swallow release of other keys */
5384 send = 1;
5385 }
5386 } else if (key == db->key[0] && !db->key_released[0]) {
5387 /* Do not check bindings for the first press of the binding
5388 * key. This allows it to be used as a debug shortcut.
5389 * We still need to swallow this event. */
5390 check_binding = 0;
5391 } else if (db->key[1]) {
5392 /* If we already ran a binding don't process another one since
5393 * we can't keep track of all the binding keys that were
5394 * pressed in order to swallow the release events. */
5395 send = 1;
5396 check_binding = 0;
5397 }
5398
5399 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005400 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5401 key, state)) {
5402 /* We ran a binding so swallow the press and keep the
5403 * grab to swallow the released too. */
5404 send = 0;
5405 terminate = 0;
5406 db->key[1] = key;
5407 } else {
5408 /* Terminate the grab since the key pressed is not a
5409 * debug binding key. */
5410 send = 1;
5411 terminate = 1;
5412 }
5413 }
5414
5415 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005416 serial = wl_display_next_serial(display);
5417 resource_list = &grab->keyboard->focus_resource_list;
5418 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005419 wl_keyboard_send_key(resource, serial, time, key, state);
5420 }
5421 }
5422
5423 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005424 weston_keyboard_end_grab(grab->keyboard);
5425 if (grab->keyboard->input_method_resource)
5426 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005427 free(db);
5428 }
5429}
5430
5431static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005432debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005433 uint32_t mods_depressed, uint32_t mods_latched,
5434 uint32_t mods_locked, uint32_t group)
5435{
5436 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005437 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005438
Neil Roberts96d790e2013-09-19 17:32:00 +01005439 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005440
Neil Roberts96d790e2013-09-19 17:32:00 +01005441 wl_resource_for_each(resource, resource_list) {
5442 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5443 mods_latched, mods_locked, group);
5444 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005445}
5446
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005447static void
5448debug_binding_cancel(struct weston_keyboard_grab *grab)
5449{
5450 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5451
5452 weston_keyboard_end_grab(grab->keyboard);
5453 free(db);
5454}
5455
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005456struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005457 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005458 debug_binding_modifiers,
5459 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005460};
5461
5462static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005463debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005464{
5465 struct debug_binding_grab *grab;
5466
5467 grab = calloc(1, sizeof *grab);
5468 if (!grab)
5469 return;
5470
5471 grab->seat = (struct weston_seat *) seat;
5472 grab->key[0] = key;
5473 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005474 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005475}
5476
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005477static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005478force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005479 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005480{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005481 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005482 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005483 struct desktop_shell *shell = data;
5484 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005485 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005486
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005487 focus_surface = seat->keyboard->focus;
5488 if (!focus_surface)
5489 return;
5490
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005491 wl_signal_emit(&compositor->kill_signal, focus_surface);
5492
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005493 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005494 wl_client_get_credentials(client, &pid, NULL, NULL);
5495
5496 /* Skip clients that we launched ourselves (the credentials of
5497 * the socketpair is ours) */
5498 if (pid == getpid())
5499 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005500
Daniel Stone325fc2d2012-05-30 16:31:58 +01005501 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005502}
5503
5504static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005505workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005506 uint32_t key, void *data)
5507{
5508 struct desktop_shell *shell = data;
5509 unsigned int new_index = shell->workspaces.current;
5510
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005511 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005512 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005513 if (new_index != 0)
5514 new_index--;
5515
5516 change_workspace(shell, new_index);
5517}
5518
5519static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005520workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005521 uint32_t key, void *data)
5522{
5523 struct desktop_shell *shell = data;
5524 unsigned int new_index = shell->workspaces.current;
5525
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005526 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005527 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005528 if (new_index < shell->workspaces.num - 1)
5529 new_index++;
5530
5531 change_workspace(shell, new_index);
5532}
5533
5534static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005535workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005536 uint32_t key, void *data)
5537{
5538 struct desktop_shell *shell = data;
5539 unsigned int new_index;
5540
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005541 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005542 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005543 new_index = key - KEY_F1;
5544 if (new_index >= shell->workspaces.num)
5545 new_index = shell->workspaces.num - 1;
5546
5547 change_workspace(shell, new_index);
5548}
5549
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005550static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005551workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005552 uint32_t key, void *data)
5553{
5554 struct desktop_shell *shell = data;
5555 unsigned int new_index = shell->workspaces.current;
5556
5557 if (shell->locked)
5558 return;
5559
5560 if (new_index != 0)
5561 new_index--;
5562
5563 take_surface_to_workspace_by_seat(shell, seat, new_index);
5564}
5565
5566static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005567workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005568 uint32_t key, void *data)
5569{
5570 struct desktop_shell *shell = data;
5571 unsigned int new_index = shell->workspaces.current;
5572
5573 if (shell->locked)
5574 return;
5575
5576 if (new_index < shell->workspaces.num - 1)
5577 new_index++;
5578
5579 take_surface_to_workspace_by_seat(shell, seat, new_index);
5580}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005581
5582static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005583handle_output_destroy(struct wl_listener *listener, void *data)
5584{
5585 struct shell_output *output_listener =
5586 container_of(listener, struct shell_output, destroy_listener);
5587
5588 wl_list_remove(&output_listener->destroy_listener.link);
5589 wl_list_remove(&output_listener->link);
5590 free(output_listener);
5591}
5592
5593static void
5594create_shell_output(struct desktop_shell *shell,
5595 struct weston_output *output)
5596{
5597 struct shell_output *shell_output;
5598
5599 shell_output = zalloc(sizeof *shell_output);
5600 if (shell_output == NULL)
5601 return;
5602
5603 shell_output->output = output;
5604 shell_output->shell = shell;
5605 shell_output->destroy_listener.notify = handle_output_destroy;
5606 wl_signal_add(&output->destroy_signal,
5607 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005608 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005609}
5610
5611static void
5612handle_output_create(struct wl_listener *listener, void *data)
5613{
5614 struct desktop_shell *shell =
5615 container_of(listener, struct desktop_shell, output_create_listener);
5616 struct weston_output *output = (struct weston_output *)data;
5617
5618 create_shell_output(shell, output);
5619}
5620
5621static void
5622setup_output_destroy_handler(struct weston_compositor *ec,
5623 struct desktop_shell *shell)
5624{
5625 struct weston_output *output;
5626
5627 wl_list_init(&shell->output_list);
5628 wl_list_for_each(output, &ec->output_list, link)
5629 create_shell_output(shell, output);
5630
5631 shell->output_create_listener.notify = handle_output_create;
5632 wl_signal_add(&ec->output_created_signal,
5633 &shell->output_create_listener);
5634}
5635
5636static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005637shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005638{
Tiago Vignattibe143262012-04-16 17:31:41 +03005639 struct desktop_shell *shell =
5640 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005641 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005642 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005643
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005644 if (shell->child.client)
5645 wl_client_destroy(shell->child.client);
5646
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005647 wl_list_remove(&shell->idle_listener.link);
5648 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005649
5650 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005651
Xiong Zhang6b481422013-10-23 13:58:32 +08005652 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5653 wl_list_remove(&shell_output->destroy_listener.link);
5654 wl_list_remove(&shell_output->link);
5655 free(shell_output);
5656 }
5657
5658 wl_list_remove(&shell->output_create_listener.link);
5659
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005660 wl_array_for_each(ws, &shell->workspaces.array)
5661 workspace_destroy(*ws);
5662 wl_array_release(&shell->workspaces.array);
5663
Pekka Paalanen3c647232011-12-22 13:43:43 +02005664 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005665 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005666 free(shell);
5667}
5668
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005669static void
5670shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5671{
5672 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005673 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005674
5675 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005676 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5677 MODIFIER_CTRL | MODIFIER_ALT,
5678 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005679 weston_compositor_add_key_binding(ec, KEY_TAB,
5680 MODIFIER_ALT,
5681 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005682 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5683 click_to_activate_binding,
5684 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005685 weston_compositor_add_touch_binding(ec, 0,
5686 touch_to_activate_binding,
5687 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005688 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5689 MODIFIER_SUPER | MODIFIER_ALT,
5690 surface_opacity_binding, NULL);
5691 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5692 MODIFIER_SUPER, zoom_axis_binding,
5693 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005694
5695 /* configurable bindings */
5696 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005697 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5698 zoom_key_binding, NULL);
5699 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5700 zoom_key_binding, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02005701 weston_compositor_add_key_binding(ec, KEY_M, mod, maximize_binding,
5702 NULL);
5703 weston_compositor_add_key_binding(ec, KEY_F, mod, fullscreen_binding,
5704 NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005705 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5706 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005707 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005708 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5709 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005710
5711 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5712 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5713 rotate_binding, NULL);
5714
Daniel Stone325fc2d2012-05-30 16:31:58 +01005715 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5716 shell);
5717 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5718 ec);
5719 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5720 backlight_binding, ec);
5721 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5722 ec);
5723 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5724 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005725 weston_compositor_add_key_binding(ec, KEY_K, mod,
5726 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005727 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5728 workspace_up_binding, shell);
5729 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5730 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005731 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5732 workspace_move_surface_up_binding,
5733 shell);
5734 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5735 workspace_move_surface_down_binding,
5736 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005737
Daniel Stonedf8133b2013-11-19 11:37:14 +01005738 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5739
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005740 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5741 if (shell->workspaces.num > 1) {
5742 num_workspace_bindings = shell->workspaces.num;
5743 if (num_workspace_bindings > 6)
5744 num_workspace_bindings = 6;
5745 for (i = 0; i < num_workspace_bindings; i++)
5746 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5747 workspace_f_binding,
5748 shell);
5749 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005750
5751 /* Debug bindings */
5752 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5753 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005754}
5755
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005756WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005757module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005758 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005759{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005760 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005761 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005762 struct workspace **pws;
5763 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005764 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005765
Peter Huttererf3d62272013-08-08 11:57:05 +10005766 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005767 if (shell == NULL)
5768 return -1;
5769
Kristian Høgsberg75840622011-09-06 13:48:16 -04005770 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005771
5772 shell->destroy_listener.notify = shell_destroy;
5773 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005774 shell->idle_listener.notify = idle_handler;
5775 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5776 shell->wake_listener.notify = wake_handler;
5777 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005778
Scott Moreauff1db4a2012-04-17 19:06:18 -06005779 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005780 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005781 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005782 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005783 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005784 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005785 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005786 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005787 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005788 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005789 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005790
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005791 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5792 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005793 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5794 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005795 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005796
5797 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005798 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005799
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005800 if (input_panel_setup(shell) < 0)
5801 return -1;
5802
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005803 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005804
Daniel Stonedf8133b2013-11-19 11:37:14 +01005805 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5806 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5807
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005808 for (i = 0; i < shell->workspaces.num; i++) {
5809 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5810 if (pws == NULL)
5811 return -1;
5812
5813 *pws = workspace_create();
5814 if (*pws == NULL)
5815 return -1;
5816 }
5817 activate_workspace(shell, 0);
5818
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005819 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005820 wl_list_init(&shell->workspaces.animation.link);
5821 shell->workspaces.animation.frame = animate_workspace_change_frame;
5822
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005823 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005824 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005825 return -1;
5826
Rafael Antognollie2a34552013-12-03 15:35:45 -02005827 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5828 shell, bind_xdg_shell) == NULL)
5829 return -1;
5830
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005831 if (wl_global_create(ec->wl_display,
5832 &desktop_shell_interface, 2,
5833 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005834 return -1;
5835
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005836 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5837 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005838 return -1;
5839
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005840 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5841 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005842 return -1;
5843
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005844 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005845
Xiong Zhang6b481422013-10-23 13:58:32 +08005846 setup_output_destroy_handler(ec, shell);
5847
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005848 loop = wl_display_get_event_loop(ec->wl_display);
5849 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005850
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005851 shell->screensaver.timer =
5852 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5853
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005854 wl_list_for_each(seat, &ec->seat_list, link)
5855 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005856
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005857 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005858
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005859 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005860
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005861 return 0;
5862}