blob: 6bcd9ecfe7350f8bb0280ce06c1519aabb0576bd [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>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020029#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050030#include <string.h>
31#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040032#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020034#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020035#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040036#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010051 ANIMATION_FADE,
52 ANIMATION_DIM_LAYER,
Juan Zhaoe10d2792012-04-25 19:09:52 +080053};
54
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020055enum fade_type {
56 FADE_IN,
57 FADE_OUT
58};
59
Daniel Stonedf8133b2013-11-19 11:37:14 +010060enum exposay_target_state {
61 EXPOSAY_TARGET_OVERVIEW, /* show all windows */
62 EXPOSAY_TARGET_CANCEL, /* return to normal, same focus */
63 EXPOSAY_TARGET_SWITCH, /* return to normal, switch focus */
64};
65
66enum exposay_layout_state {
67 EXPOSAY_LAYOUT_INACTIVE = 0, /* normal desktop */
68 EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE, /* in transition to normal */
69 EXPOSAY_LAYOUT_OVERVIEW, /* show all windows */
70 EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW, /* in transition to all windows */
71};
72
Jonas Ådahl04769742012-06-13 00:01:24 +020073struct focus_state {
74 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040075 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020076 struct weston_surface *keyboard_focus;
77 struct wl_list link;
78 struct wl_listener seat_destroy_listener;
79 struct wl_listener surface_destroy_listener;
80};
81
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010082struct focus_surface {
83 struct weston_surface *surface;
84 struct weston_view *view;
85 struct weston_transform workspace_transform;
86};
87
Jonas Ådahle3cddce2012-06-13 00:01:22 +020088struct workspace {
89 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020090
91 struct wl_list focus_list;
92 struct wl_listener seat_destroyed_listener;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010093
94 struct focus_surface *fsurf_front;
95 struct focus_surface *fsurf_back;
96 struct weston_view_animation *focus_animation;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020097};
98
Philipp Brüschweiler88013572012-08-06 13:44:42 +020099struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -0500100 struct wl_resource *resource;
101 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100102
103 struct desktop_shell *shell;
104
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200105 struct wl_list link;
106 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500107 struct weston_view *view;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100108 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200109
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +0200110 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200111 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200112};
113
Xiong Zhang6b481422013-10-23 13:58:32 +0800114struct shell_output {
115 struct desktop_shell *shell;
116 struct weston_output *output;
117 struct wl_listener destroy_listener;
118 struct wl_list link;
119};
120
Tiago Vignattibe143262012-04-16 17:31:41 +0300121struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400123
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200124 struct wl_listener idle_listener;
125 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400126 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200127 struct wl_listener show_input_panel_listener;
128 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200129 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200130
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500131 struct weston_layer fullscreen_layer;
132 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500133 struct weston_layer background_layer;
134 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200135 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500136
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400137 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300138 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400139
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200140 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500141 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200142 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200143 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200144
145 unsigned deathcount;
146 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200147 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200148
149 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200150 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200151 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200152
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200153 struct {
154 struct weston_surface *surface;
155 pixman_box32_t cursor_rectangle;
156 } text_input;
157
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400158 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500159 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160
Pekka Paalanen77346a62011-11-30 16:26:35 +0200161 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200162 struct wl_array array;
163 unsigned int current;
164 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200165
Jonas Ådahle9d22502012-08-29 22:13:01 +0200166 struct wl_list client_list;
167
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200168 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200169 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200170 int anim_dir;
171 uint32_t anim_timestamp;
172 double anim_current;
173 struct workspace *anim_from;
174 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200175 } workspaces;
176
177 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200178 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200179 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200180 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500181 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200182 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200183 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500184
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200185 struct {
186 struct wl_resource *binding;
187 struct wl_list surfaces;
188 } input_panel;
189
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200190 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500191 struct weston_view *view;
192 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200193 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300194 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200195 } fade;
196
Daniel Stonedf8133b2013-11-19 11:37:14 +0100197 struct exposay {
198 /* XXX: Make these exposay_surfaces. */
199 struct weston_view *focus_prev;
200 struct weston_view *focus_current;
201 struct weston_view *clicked;
202 struct workspace *workspace;
203 struct weston_seat *seat;
204 struct wl_list surface_list;
205
206 struct weston_keyboard_grab grab_kbd;
207 struct weston_pointer_grab grab_ptr;
208
209 enum exposay_target_state state_target;
210 enum exposay_layout_state state_cur;
211 int in_flight; /* number of animations still running */
212
213 int num_surfaces;
214 int grid_size;
215 int surface_size;
216
217 int hpadding_outer;
218 int vpadding_outer;
219 int padding_inner;
220
221 int row_current;
222 int column_current;
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +0100223
224 bool mod_pressed;
225 bool mod_invalid;
Daniel Stonedf8133b2013-11-19 11:37:14 +0100226 } exposay;
227
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300228 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800229 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700230 enum animation_type startup_animation_type;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100231 enum animation_type focus_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800232
233 struct wl_listener output_create_listener;
234 struct wl_list output_list;
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100235
236 char *client;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400237};
238
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500239enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200240 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500241 SHELL_SURFACE_TOPLEVEL,
242 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500243 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800244 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300245 SHELL_SURFACE_POPUP,
246 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200247};
248
Scott Moreauff1db4a2012-04-17 19:06:18 -0600249struct ping_timer {
250 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600251 uint32_t serial;
252};
253
Philip Withnall648a4dd2013-11-25 18:01:44 +0000254/*
255 * Surface stacking and ordering.
256 *
257 * This is handled using several linked lists of surfaces, organised into
258 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
259 * above all of the surfaces in the layer below. The set of layers is static and
260 * in the following order (top-most first):
261 * • Lock layer (only ever displayed on its own)
262 * • Cursor layer
263 * • Fullscreen layer
264 * • Panel layer
265 * • Input panel layer
266 * • Workspace layers
267 * • Background layer
268 *
269 * The list of layers may be manipulated to remove whole layers of surfaces from
270 * display. For example, when locking the screen, all layers except the lock
271 * layer are removed.
272 *
273 * A surface’s layer is modified on configuring the surface, in
274 * set_surface_type() (which is only called when the surface’s type change is
275 * _committed_). If a surface’s type changes (e.g. when making a window
276 * fullscreen) its layer changes too.
277 *
278 * In order to allow popup and transient surfaces to be correctly stacked above
279 * their parent surfaces, each surface tracks both its parent surface, and a
280 * linked list of its children. When a surface’s layer is updated, so are the
281 * layers of its children. Note that child surfaces are *not* the same as
282 * subsurfaces — child/parent surfaces are purely for maintaining stacking
283 * order.
284 *
285 * The children_link list of siblings of a surface (i.e. those surfaces which
286 * have the same parent) only contains weston_surfaces which have a
287 * shell_surface. Stacking is not implemented for non-shell_surface
288 * weston_surfaces. This means that the following implication does *not* hold:
289 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
290 */
291
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200292struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500293 struct wl_resource *resource;
294 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200295
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500296 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600298 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200299 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400300 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000301 struct wl_list children_list; /* child surfaces of this one */
302 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300303 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200304
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400305 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400306 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500307 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800308 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800309 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700310 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100311
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500312 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200313 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500314 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200315 } rotation;
316
317 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100318 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500319 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100320 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400321 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500322 } popup;
323
Alex Wu4539b082012-03-01 12:57:46 +0800324 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300325 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400326 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300327 } transient;
328
329 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800330 enum wl_shell_surface_fullscreen_method type;
331 struct weston_transform transform; /* matrix from x, y */
332 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800334 } fullscreen;
335
Scott Moreauff1db4a2012-04-17 19:06:18 -0600336 struct ping_timer *ping_timer;
337
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200338 struct weston_transform workspace_transform;
339
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500340 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500341 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100342 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400343
344 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200345};
346
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300347struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400348 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349 struct shell_surface *shsurf;
350 struct wl_listener shsurf_destroy_listener;
351};
352
Rusty Lynch1084da52013-08-15 09:10:08 -0700353struct shell_touch_grab {
354 struct weston_touch_grab grab;
355 struct shell_surface *shsurf;
356 struct wl_listener shsurf_destroy_listener;
357 struct weston_touch *touch;
358};
359
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300360struct weston_move_grab {
361 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100362 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500363};
364
Rusty Lynch1084da52013-08-15 09:10:08 -0700365struct weston_touch_move_grab {
366 struct shell_touch_grab base;
367 wl_fixed_t dx, dy;
368};
369
Pekka Paalanen460099f2012-01-20 16:48:25 +0200370struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300371 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500372 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200373 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200374 float x;
375 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200376 } center;
377};
378
Giulio Camuffo5085a752013-03-25 21:42:45 +0100379struct shell_seat {
380 struct weston_seat *seat;
381 struct wl_listener seat_destroy_listener;
382
383 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400384 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100385 struct wl_list surfaces_list;
386 struct wl_client *client;
387 int32_t initial_up;
388 } popup_grab;
389};
390
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400391static void
392activate(struct desktop_shell *shell, struct weston_surface *es,
393 struct weston_seat *seat);
394
395static struct workspace *
396get_current_workspace(struct desktop_shell *shell);
397
Alex Wubd3354b2012-04-17 17:20:49 +0800398static struct shell_surface *
399get_shell_surface(struct weston_surface *surface);
400
401static struct desktop_shell *
402shell_surface_get_shell(struct shell_surface *shsurf);
403
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500404static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400405surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500406
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300407static void
408shell_fade_startup(struct desktop_shell *shell);
409
Philip Withnallbecb77e2013-11-25 18:01:30 +0000410static struct shell_seat *
411get_shell_seat(struct weston_seat *seat);
412
Philip Withnall648a4dd2013-11-25 18:01:44 +0000413static void
414shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
415
Alex Wubd3354b2012-04-17 17:20:49 +0800416static bool
417shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
418{
419 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800421
422 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100423
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800425 return false;
426
Jason Ekstranda7af7042013-10-12 22:38:11 -0500427 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
428 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800429 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500430 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800431}
432
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500433static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400434destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300435{
436 struct shell_grab *grab;
437
438 grab = container_of(listener, struct shell_grab,
439 shsurf_destroy_listener);
440
441 grab->shsurf = NULL;
442}
443
Jason Ekstranda7af7042013-10-12 22:38:11 -0500444static struct weston_view *
445get_default_view(struct weston_surface *surface)
446{
447 struct shell_surface *shsurf;
448 struct weston_view *view;
449
450 if (!surface || wl_list_empty(&surface->views))
451 return NULL;
452
453 shsurf = get_shell_surface(surface);
454 if (shsurf)
455 return shsurf->view;
456
457 wl_list_for_each(view, &surface->views, surface_link)
458 if (weston_view_is_mapped(view))
459 return view;
460
461 return container_of(surface->views.next, struct weston_view, surface_link);
462}
463
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300464static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400465popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400466
467static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300468shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400469 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300470 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400471 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300472 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300473{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300474 struct desktop_shell *shell = shsurf->shell;
475
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400476 popup_grab_end(pointer);
477
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300478 grab->grab.interface = interface;
479 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400480 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500481 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400482 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300483
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700484 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400485 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400486 if (shell->child.desktop_shell) {
487 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
488 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500489 weston_pointer_set_focus(pointer,
490 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400491 wl_fixed_from_int(0),
492 wl_fixed_from_int(0));
493 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300494}
495
496static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300497shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300498{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700499 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400500 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700501 grab->shsurf->grabbed = 0;
502 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300503
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700504 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300505}
506
507static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700508shell_touch_grab_start(struct shell_touch_grab *grab,
509 const struct weston_touch_grab_interface *interface,
510 struct shell_surface *shsurf,
511 struct weston_touch *touch)
512{
513 struct desktop_shell *shell = shsurf->shell;
514
515 grab->grab.interface = interface;
516 grab->shsurf = shsurf;
517 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
518 wl_signal_add(&shsurf->destroy_signal,
519 &grab->shsurf_destroy_listener);
520
521 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700522 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700523
524 weston_touch_start_grab(touch, &grab->grab);
525 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500526 weston_touch_set_focus(touch->seat,
527 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700528}
529
530static void
531shell_touch_grab_end(struct shell_touch_grab *grab)
532{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700533 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700534 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700535 grab->shsurf->grabbed = 0;
536 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700537
538 weston_touch_end_grab(grab->touch);
539}
540
541static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500542center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800543 struct weston_output *output);
544
Daniel Stone496ca172012-05-30 16:31:42 +0100545static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300546get_modifier(char *modifier)
547{
548 if (!modifier)
549 return MODIFIER_SUPER;
550
551 if (!strcmp("ctrl", modifier))
552 return MODIFIER_CTRL;
553 else if (!strcmp("alt", modifier))
554 return MODIFIER_ALT;
555 else if (!strcmp("super", modifier))
556 return MODIFIER_SUPER;
557 else
558 return MODIFIER_SUPER;
559}
560
Juan Zhaoe10d2792012-04-25 19:09:52 +0800561static enum animation_type
562get_animation_type(char *animation)
563{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800564 if (!strcmp("zoom", animation))
565 return ANIMATION_ZOOM;
566 else if (!strcmp("fade", animation))
567 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100568 else if (!strcmp("dim-layer", animation))
569 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800570 else
571 return ANIMATION_NONE;
572}
573
Alex Wu4539b082012-03-01 12:57:46 +0800574static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400575shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200576{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400577 struct weston_config_section *section;
578 int duration;
579 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200580
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400581 section = weston_config_get_section(shell->compositor->config,
582 "screensaver", NULL, NULL);
583 weston_config_section_get_string(section,
584 "path", &shell->screensaver.path, NULL);
585 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200586 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400587
588 section = weston_config_get_section(shell->compositor->config,
589 "shell", NULL, NULL);
590 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100591 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100592 shell->client = s;
593 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400594 "binding-modifier", &s, "super");
595 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200596 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400597 weston_config_section_get_string(section, "animation", &s, "none");
598 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200599 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700600 weston_config_section_get_string(section,
601 "startup-animation", &s, "fade");
602 shell->startup_animation_type = get_animation_type(s);
603 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700604 if (shell->startup_animation_type == ANIMATION_ZOOM)
605 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100606 weston_config_section_get_string(section, "focus-animation", &s, "none");
607 shell->focus_animation_type = get_animation_type(s);
608 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400609 weston_config_section_get_uint(section, "num-workspaces",
610 &shell->workspaces.num,
611 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200612}
613
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100614static struct weston_output *
615get_default_output(struct weston_compositor *compositor)
616{
617 return container_of(compositor->output_list.next,
618 struct weston_output, link);
619}
620
621
622/* no-op func for checking focus surface */
623static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600624focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100625{
626}
627
628static struct focus_surface *
629get_focus_surface(struct weston_surface *surface)
630{
631 if (surface->configure == focus_surface_configure)
632 return surface->configure_private;
633 else
634 return NULL;
635}
636
637static bool
638is_focus_surface (struct weston_surface *es)
639{
640 return (es->configure == focus_surface_configure);
641}
642
643static bool
644is_focus_view (struct weston_view *view)
645{
646 return is_focus_surface (view->surface);
647}
648
649static struct focus_surface *
650create_focus_surface(struct weston_compositor *ec,
651 struct weston_output *output)
652{
653 struct focus_surface *fsurf = NULL;
654 struct weston_surface *surface = NULL;
655
656 fsurf = malloc(sizeof *fsurf);
657 if (!fsurf)
658 return NULL;
659
660 fsurf->surface = weston_surface_create(ec);
661 surface = fsurf->surface;
662 if (surface == NULL) {
663 free(fsurf);
664 return NULL;
665 }
666
667 surface->configure = focus_surface_configure;
668 surface->output = output;
669 surface->configure_private = fsurf;
670
671 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100672 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100673
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600674 surface->width = output->width;
675 surface->height = output->height;
676 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100677 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
678 pixman_region32_fini(&surface->opaque);
679 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
680 output->width, output->height);
681 pixman_region32_fini(&surface->input);
682 pixman_region32_init(&surface->input);
683
684 wl_list_init(&fsurf->workspace_transform.link);
685
686 return fsurf;
687}
688
689static void
690focus_surface_destroy(struct focus_surface *fsurf)
691{
692 weston_surface_destroy(fsurf->surface);
693 free(fsurf);
694}
695
696static void
697focus_animation_done(struct weston_view_animation *animation, void *data)
698{
699 struct workspace *ws = data;
700
701 ws->focus_animation = NULL;
702}
703
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200704static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200705focus_state_destroy(struct focus_state *state)
706{
707 wl_list_remove(&state->seat_destroy_listener.link);
708 wl_list_remove(&state->surface_destroy_listener.link);
709 free(state);
710}
711
712static void
713focus_state_seat_destroy(struct wl_listener *listener, void *data)
714{
715 struct focus_state *state = container_of(listener,
716 struct focus_state,
717 seat_destroy_listener);
718
719 wl_list_remove(&state->link);
720 focus_state_destroy(state);
721}
722
723static void
724focus_state_surface_destroy(struct wl_listener *listener, void *data)
725{
726 struct focus_state *state = container_of(listener,
727 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400728 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400729 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500730 struct weston_surface *main_surface, *next;
731 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200732
Pekka Paalanen01388e22013-04-25 13:57:44 +0300733 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
734
Kristian Høgsberge3778222012-07-31 17:29:30 -0400735 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500736 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
737 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400738 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100739 if (is_focus_view(view))
740 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400741
Jason Ekstranda7af7042013-10-12 22:38:11 -0500742 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400743 break;
744 }
745
Pekka Paalanen01388e22013-04-25 13:57:44 +0300746 /* if the focus was a sub-surface, activate its main surface */
747 if (main_surface != state->keyboard_focus)
748 next = main_surface;
749
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100750 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400751 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100752 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400753 activate(shell, next, state->seat);
754 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100755 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
756 if (state->ws->focus_animation)
757 weston_view_animation_destroy(state->ws->focus_animation);
758
759 state->ws->focus_animation = weston_fade_run(
760 state->ws->fsurf_front->view,
761 state->ws->fsurf_front->view->alpha, 0.0, 300,
762 focus_animation_done, state->ws);
763 }
764
Kristian Høgsberge3778222012-07-31 17:29:30 -0400765 wl_list_remove(&state->link);
766 focus_state_destroy(state);
767 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200768}
769
770static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400771focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200772{
Jonas Ådahl04769742012-06-13 00:01:24 +0200773 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200774
775 state = malloc(sizeof *state);
776 if (state == NULL)
777 return NULL;
778
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100779 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400780 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200781 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400782 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200783
784 state->seat_destroy_listener.notify = focus_state_seat_destroy;
785 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400786 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200787 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400788 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200789
790 return state;
791}
792
Jonas Ådahl8538b222012-08-29 22:13:03 +0200793static struct focus_state *
794ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
795{
796 struct workspace *ws = get_current_workspace(shell);
797 struct focus_state *state;
798
799 wl_list_for_each(state, &ws->focus_list, link)
800 if (state->seat == seat)
801 break;
802
803 if (&state->link == &ws->focus_list)
804 state = focus_state_create(seat, ws);
805
806 return state;
807}
808
Jonas Ådahl04769742012-06-13 00:01:24 +0200809static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400810restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200811{
812 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400813 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200814
815 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400816 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200817
Kristian Høgsberge3148752013-05-06 23:19:49 -0400818 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200819 }
820}
821
822static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200823replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
824 struct weston_seat *seat)
825{
826 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400827 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200828
829 wl_list_for_each(state, &ws->focus_list, link) {
830 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400831 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500832 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200833 return;
834 }
835 }
836}
837
838static void
839drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
840 struct weston_surface *surface)
841{
842 struct focus_state *state;
843
844 wl_list_for_each(state, &ws->focus_list, link)
845 if (state->keyboard_focus == surface)
846 state->keyboard_focus = NULL;
847}
848
849static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
851 struct weston_view *from, struct weston_view *to)
852{
853 struct weston_output *output;
854 bool focus_surface_created = false;
855
856 /* FIXME: Only support dim animation using two layers */
857 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
858 return;
859
860 output = get_default_output(shell->compositor);
861 if (ws->fsurf_front == NULL && (from || to)) {
862 ws->fsurf_front = create_focus_surface(shell->compositor, output);
863 ws->fsurf_back = create_focus_surface(shell->compositor, output);
864 ws->fsurf_front->view->alpha = 0.0;
865 ws->fsurf_back->view->alpha = 0.0;
866 focus_surface_created = true;
867 } else {
868 wl_list_remove(&ws->fsurf_front->view->layer_link);
869 wl_list_remove(&ws->fsurf_back->view->layer_link);
870 }
871
872 if (ws->focus_animation) {
873 weston_view_animation_destroy(ws->focus_animation);
874 ws->focus_animation = NULL;
875 }
876
877 if (to)
878 wl_list_insert(&to->layer_link,
879 &ws->fsurf_front->view->layer_link);
880 else if (from)
881 wl_list_insert(&ws->layer.view_list,
882 &ws->fsurf_front->view->layer_link);
883
884 if (focus_surface_created) {
885 ws->focus_animation = weston_fade_run(
886 ws->fsurf_front->view,
887 ws->fsurf_front->view->alpha, 0.6, 300,
888 focus_animation_done, ws);
889 } else if (from) {
890 wl_list_insert(&from->layer_link,
891 &ws->fsurf_back->view->layer_link);
892 ws->focus_animation = weston_stable_fade_run(
893 ws->fsurf_front->view, 0.0,
894 ws->fsurf_back->view, 0.6,
895 focus_animation_done, ws);
896 } else if (to) {
897 wl_list_insert(&ws->layer.view_list,
898 &ws->fsurf_back->view->layer_link);
899 ws->focus_animation = weston_stable_fade_run(
900 ws->fsurf_front->view, 0.0,
901 ws->fsurf_back->view, 0.6,
902 focus_animation_done, ws);
903 }
904}
905
906static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200907workspace_destroy(struct workspace *ws)
908{
Jonas Ådahl04769742012-06-13 00:01:24 +0200909 struct focus_state *state, *next;
910
911 wl_list_for_each_safe(state, next, &ws->focus_list, link)
912 focus_state_destroy(state);
913
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100914 if (ws->fsurf_front)
915 focus_surface_destroy(ws->fsurf_front);
916 if (ws->fsurf_back)
917 focus_surface_destroy(ws->fsurf_back);
918
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919 free(ws);
920}
921
Jonas Ådahl04769742012-06-13 00:01:24 +0200922static void
923seat_destroyed(struct wl_listener *listener, void *data)
924{
925 struct weston_seat *seat = data;
926 struct focus_state *state, *next;
927 struct workspace *ws = container_of(listener,
928 struct workspace,
929 seat_destroyed_listener);
930
931 wl_list_for_each_safe(state, next, &ws->focus_list, link)
932 if (state->seat == seat)
933 wl_list_remove(&state->link);
934}
935
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200936static struct workspace *
937workspace_create(void)
938{
939 struct workspace *ws = malloc(sizeof *ws);
940 if (ws == NULL)
941 return NULL;
942
943 weston_layer_init(&ws->layer, NULL);
944
Jonas Ådahl04769742012-06-13 00:01:24 +0200945 wl_list_init(&ws->focus_list);
946 wl_list_init(&ws->seat_destroyed_listener.link);
947 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100948 ws->fsurf_front = NULL;
949 ws->fsurf_back = NULL;
950 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200951
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200952 return ws;
953}
954
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200955static int
956workspace_is_empty(struct workspace *ws)
957{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500958 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200959}
960
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200961static struct workspace *
962get_workspace(struct desktop_shell *shell, unsigned int index)
963{
964 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200965 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200966 pws += index;
967 return *pws;
968}
969
970static struct workspace *
971get_current_workspace(struct desktop_shell *shell)
972{
973 return get_workspace(shell, shell->workspaces.current);
974}
975
976static void
977activate_workspace(struct desktop_shell *shell, unsigned int index)
978{
979 struct workspace *ws;
980
981 ws = get_workspace(shell, index);
982 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
983
984 shell->workspaces.current = index;
985}
986
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200987static unsigned int
988get_output_height(struct weston_output *output)
989{
990 return abs(output->region.extents.y1 - output->region.extents.y2);
991}
992
993static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100994view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200995{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996 struct weston_transform *transform;
997
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100998 if (is_focus_view(view)) {
999 struct focus_surface *fsurf = get_focus_surface(view->surface);
1000 transform = &fsurf->workspace_transform;
1001 } else {
1002 struct shell_surface *shsurf = get_shell_surface(view->surface);
1003 transform = &shsurf->workspace_transform;
1004 }
1005
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001008 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001010 weston_matrix_init(&transform->matrix);
1011 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001012 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001014}
1015
1016static void
1017workspace_translate_out(struct workspace *ws, double fraction)
1018{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020 unsigned int height;
1021 double d;
1022
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1024 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001025 d = height * fraction;
1026
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001027 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001028 }
1029}
1030
1031static void
1032workspace_translate_in(struct workspace *ws, double fraction)
1033{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035 unsigned int height;
1036 double d;
1037
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1039 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001040
1041 if (fraction > 0)
1042 d = -(height - height * fraction);
1043 else
1044 d = height + height * fraction;
1045
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001046 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001047 }
1048}
1049
1050static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001051broadcast_current_workspace_state(struct desktop_shell *shell)
1052{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001053 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001054
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001055 wl_resource_for_each(resource, &shell->workspaces.client_list)
1056 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001057 shell->workspaces.current,
1058 shell->workspaces.num);
1059}
1060
1061static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001062reverse_workspace_change_animation(struct desktop_shell *shell,
1063 unsigned int index,
1064 struct workspace *from,
1065 struct workspace *to)
1066{
1067 shell->workspaces.current = index;
1068
1069 shell->workspaces.anim_to = to;
1070 shell->workspaces.anim_from = from;
1071 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1072 shell->workspaces.anim_timestamp = 0;
1073
Scott Moreau4272e632012-08-13 09:58:41 -06001074 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001075}
1076
1077static void
1078workspace_deactivate_transforms(struct workspace *ws)
1079{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001081 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001082
Jason Ekstranda7af7042013-10-12 22:38:11 -05001083 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001084 if (is_focus_view(view)) {
1085 struct focus_surface *fsurf = get_focus_surface(view->surface);
1086 transform = &fsurf->workspace_transform;
1087 } else {
1088 struct shell_surface *shsurf = get_shell_surface(view->surface);
1089 transform = &shsurf->workspace_transform;
1090 }
1091
1092 if (!wl_list_empty(&transform->link)) {
1093 wl_list_remove(&transform->link);
1094 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001095 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001096 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097 }
1098}
1099
1100static void
1101finish_workspace_change_animation(struct desktop_shell *shell,
1102 struct workspace *from,
1103 struct workspace *to)
1104{
Scott Moreau4272e632012-08-13 09:58:41 -06001105 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106
1107 wl_list_remove(&shell->workspaces.animation.link);
1108 workspace_deactivate_transforms(from);
1109 workspace_deactivate_transforms(to);
1110 shell->workspaces.anim_to = NULL;
1111
1112 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1113}
1114
1115static void
1116animate_workspace_change_frame(struct weston_animation *animation,
1117 struct weston_output *output, uint32_t msecs)
1118{
1119 struct desktop_shell *shell =
1120 container_of(animation, struct desktop_shell,
1121 workspaces.animation);
1122 struct workspace *from = shell->workspaces.anim_from;
1123 struct workspace *to = shell->workspaces.anim_to;
1124 uint32_t t;
1125 double x, y;
1126
1127 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1128 finish_workspace_change_animation(shell, from, to);
1129 return;
1130 }
1131
1132 if (shell->workspaces.anim_timestamp == 0) {
1133 if (shell->workspaces.anim_current == 0.0)
1134 shell->workspaces.anim_timestamp = msecs;
1135 else
1136 shell->workspaces.anim_timestamp =
1137 msecs -
1138 /* Invers of movement function 'y' below. */
1139 (asin(1.0 - shell->workspaces.anim_current) *
1140 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1141 M_2_PI);
1142 }
1143
1144 t = msecs - shell->workspaces.anim_timestamp;
1145
1146 /*
1147 * x = [0, π/2]
1148 * y(x) = sin(x)
1149 */
1150 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1151 y = sin(x);
1152
1153 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001154 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155
1156 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1157 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1158 shell->workspaces.anim_current = y;
1159
Scott Moreau4272e632012-08-13 09:58:41 -06001160 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001162 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001164}
1165
1166static void
1167animate_workspace_change(struct desktop_shell *shell,
1168 unsigned int index,
1169 struct workspace *from,
1170 struct workspace *to)
1171{
1172 struct weston_output *output;
1173
1174 int dir;
1175
1176 if (index > shell->workspaces.current)
1177 dir = -1;
1178 else
1179 dir = 1;
1180
1181 shell->workspaces.current = index;
1182
1183 shell->workspaces.anim_dir = dir;
1184 shell->workspaces.anim_from = from;
1185 shell->workspaces.anim_to = to;
1186 shell->workspaces.anim_current = 0.0;
1187 shell->workspaces.anim_timestamp = 0;
1188
1189 output = container_of(shell->compositor->output_list.next,
1190 struct weston_output, link);
1191 wl_list_insert(&output->animation_list,
1192 &shell->workspaces.animation.link);
1193
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001194 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001195
1196 workspace_translate_in(to, 0);
1197
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001198 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001199
Scott Moreau4272e632012-08-13 09:58:41 -06001200 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001201}
1202
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001203static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204update_workspace(struct desktop_shell *shell, unsigned int index,
1205 struct workspace *from, struct workspace *to)
1206{
1207 shell->workspaces.current = index;
1208 wl_list_insert(&from->layer.link, &to->layer.link);
1209 wl_list_remove(&from->layer.link);
1210}
1211
1212static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001213change_workspace(struct desktop_shell *shell, unsigned int index)
1214{
1215 struct workspace *from;
1216 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001217 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001218
1219 if (index == shell->workspaces.current)
1220 return;
1221
1222 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001224 return;
1225
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001226 from = get_current_workspace(shell);
1227 to = get_workspace(shell, index);
1228
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001229 if (shell->workspaces.anim_from == to &&
1230 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001231 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001232 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001233 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001234 return;
1235 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001236
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001237 if (shell->workspaces.anim_to != NULL)
1238 finish_workspace_change_animation(shell,
1239 shell->workspaces.anim_from,
1240 shell->workspaces.anim_to);
1241
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001242 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001243
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001244 if (shell->focus_animation_type != ANIMATION_NONE) {
1245 wl_list_for_each(state, &from->focus_list, link)
1246 if (state->keyboard_focus)
1247 animate_focus_change(shell, from,
1248 get_default_view(state->keyboard_focus), NULL);
1249
1250 wl_list_for_each(state, &to->focus_list, link)
1251 if (state->keyboard_focus)
1252 animate_focus_change(shell, to,
1253 NULL, get_default_view(state->keyboard_focus));
1254 }
1255
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001256 if (workspace_is_empty(to) && workspace_is_empty(from))
1257 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001258 else
1259 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001260
1261 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001262}
1263
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001264static bool
1265workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1266{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001268 struct wl_list *e;
1269
1270 if (wl_list_empty(list))
1271 return false;
1272
1273 e = list->next;
1274
1275 if (e->next != list)
1276 return false;
1277
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279}
1280
1281static void
Philip Withnall659163d2013-11-25 18:01:36 +00001282move_surface_to_workspace(struct desktop_shell *shell,
1283 struct shell_surface *shsurf,
1284 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001285{
1286 struct workspace *from;
1287 struct workspace *to;
1288 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001289 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001290 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001291
1292 if (workspace == shell->workspaces.current)
1293 return;
1294
Philip Withnall659163d2013-11-25 18:01:36 +00001295 view = get_default_view(shsurf->surface);
1296 if (!view)
1297 return;
1298
1299 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1300
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001301 if (workspace >= shell->workspaces.num)
1302 workspace = shell->workspaces.num - 1;
1303
Jonas Ådahle9d22502012-08-29 22:13:01 +02001304 from = get_current_workspace(shell);
1305 to = get_workspace(shell, workspace);
1306
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 wl_list_remove(&view->layer_link);
1308 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001309
Philip Withnall648a4dd2013-11-25 18:01:44 +00001310 shell_surface_update_child_surface_layers(shsurf);
1311
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001313 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1314 if (!seat->keyboard)
1315 continue;
1316
1317 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001319 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001320 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323}
1324
1325static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001326take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001327 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001328 unsigned int index)
1329{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001330 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001332 struct shell_surface *shsurf;
1333 struct workspace *from;
1334 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001335 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001336
Pekka Paalanen01388e22013-04-25 13:57:44 +03001337 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 view = get_default_view(surface);
1339 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001340 index == shell->workspaces.current ||
1341 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001342 return;
1343
1344 from = get_current_workspace(shell);
1345 to = get_workspace(shell, index);
1346
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 wl_list_remove(&view->layer_link);
1348 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001349
Philip Withnall659163d2013-11-25 18:01:36 +00001350 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001351 if (shsurf != NULL)
1352 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001353
Jonas Ådahle9d22502012-08-29 22:13:01 +02001354 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001355 drop_focus_state(shell, from, surface);
1356
1357 if (shell->workspaces.anim_from == to &&
1358 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001359 wl_list_remove(&to->layer.link);
1360 wl_list_insert(from->layer.link.prev, &to->layer.link);
1361
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001362 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001363 broadcast_current_workspace_state(shell);
1364
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001365 return;
1366 }
1367
1368 if (shell->workspaces.anim_to != NULL)
1369 finish_workspace_change_animation(shell,
1370 shell->workspaces.anim_from,
1371 shell->workspaces.anim_to);
1372
1373 if (workspace_is_empty(from) &&
1374 workspace_has_only(to, surface))
1375 update_workspace(shell, index, from, to);
1376 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001377 if (shsurf != NULL &&
1378 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001379 wl_list_insert(&shell->workspaces.anim_sticky_list,
1380 &shsurf->workspace_transform.link);
1381
1382 animate_workspace_change(shell, index, from, to);
1383 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001384
1385 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001386
1387 state = ensure_focus_state(shell, seat);
1388 if (state != NULL)
1389 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001390}
1391
1392static void
1393workspace_manager_move_surface(struct wl_client *client,
1394 struct wl_resource *resource,
1395 struct wl_resource *surface_resource,
1396 uint32_t workspace)
1397{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001398 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001399 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001400 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001401 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001402 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001403
Pekka Paalanen01388e22013-04-25 13:57:44 +03001404 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001405 shell_surface = get_shell_surface(main_surface);
1406 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001408
1409 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001410}
1411
1412static const struct workspace_manager_interface workspace_manager_implementation = {
1413 workspace_manager_move_surface,
1414};
1415
1416static void
1417unbind_resource(struct wl_resource *resource)
1418{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001419 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001420}
1421
1422static void
1423bind_workspace_manager(struct wl_client *client,
1424 void *data, uint32_t version, uint32_t id)
1425{
1426 struct desktop_shell *shell = data;
1427 struct wl_resource *resource;
1428
Jason Ekstranda85118c2013-06-27 20:17:02 -05001429 resource = wl_resource_create(client,
1430 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001431
1432 if (resource == NULL) {
1433 weston_log("couldn't add workspace manager object");
1434 return;
1435 }
1436
Jason Ekstranda85118c2013-06-27 20:17:02 -05001437 wl_resource_set_implementation(resource,
1438 &workspace_manager_implementation,
1439 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001440 wl_list_insert(&shell->workspaces.client_list,
1441 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001442
1443 workspace_manager_send_state(resource,
1444 shell->workspaces.current,
1445 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001446}
1447
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001448static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001449touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1450 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1451{
1452}
1453
1454static void
1455touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1456{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001457 struct weston_touch_move_grab *move =
1458 (struct weston_touch_move_grab *) container_of(
1459 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001460
Jonas Ådahl9484b692013-12-02 22:05:03 +01001461 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001462 shell_touch_grab_end(&move->base);
1463 free(move);
1464 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001465}
1466
1467static void
1468touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1469 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1470{
1471 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1472 struct shell_surface *shsurf = move->base.shsurf;
1473 struct weston_surface *es;
1474 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1475 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1476
1477 if (!shsurf)
1478 return;
1479
1480 es = shsurf->surface;
1481
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001482 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001483
1484 weston_compositor_schedule_repaint(es->compositor);
1485}
1486
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001487static void
1488touch_move_grab_cancel(struct weston_touch_grab *grab)
1489{
1490 struct weston_touch_move_grab *move =
1491 (struct weston_touch_move_grab *) container_of(
1492 grab, struct shell_touch_grab, grab);
1493
1494 shell_touch_grab_end(&move->base);
1495 free(move);
1496}
1497
Rusty Lynch1084da52013-08-15 09:10:08 -07001498static const struct weston_touch_grab_interface touch_move_grab_interface = {
1499 touch_move_grab_down,
1500 touch_move_grab_up,
1501 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001502 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001503};
1504
1505static int
1506surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1507{
1508 struct weston_touch_move_grab *move;
1509
1510 if (!shsurf)
1511 return -1;
1512
1513 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1514 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001515 if (shsurf->grabbed)
1516 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001517
1518 move = malloc(sizeof *move);
1519 if (!move)
1520 return -1;
1521
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001523 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001525 seat->touch->grab_y;
1526
1527 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1528 seat->touch);
1529
1530 return 0;
1531}
1532
1533static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001534noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001535{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001536}
1537
1538static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001539move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1540 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001541{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001542 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001543 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001544 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001545 int dx, dy;
1546
1547 weston_pointer_move(pointer, x, y);
1548 dx = wl_fixed_to_int(pointer->x + move->dx);
1549 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001550
1551 if (!shsurf)
1552 return;
1553
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001554 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001555
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001557}
1558
1559static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001560move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001561 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001562{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001563 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1564 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001565 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001566 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001567
Daniel Stone4dbadb12012-05-30 16:31:51 +01001568 if (pointer->button_count == 0 &&
1569 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001570 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001571 free(grab);
1572 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001573}
1574
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001575static void
1576move_grab_cancel(struct weston_pointer_grab *grab)
1577{
1578 struct shell_grab *shell_grab =
1579 container_of(grab, struct shell_grab, grab);
1580
1581 shell_grab_end(shell_grab);
1582 free(grab);
1583}
1584
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001585static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001586 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001587 move_grab_motion,
1588 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001589 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001590};
1591
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001593surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001594{
1595 struct weston_move_grab *move;
1596
1597 if (!shsurf)
1598 return -1;
1599
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001600 if (shsurf->grabbed)
1601 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001602 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1603 return 0;
1604
1605 move = malloc(sizeof *move);
1606 if (!move)
1607 return -1;
1608
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001610 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001612 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001613
1614 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001615 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001616
1617 return 0;
1618}
1619
1620static void
1621shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1622 struct wl_resource *seat_resource, uint32_t serial)
1623{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001624 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001625 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001626 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001627
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001628 if (seat->pointer &&
1629 seat->pointer->button_count > 0 &&
1630 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001632 if ((surface == shsurf->surface) &&
1633 (surface_move(shsurf, seat) < 0))
1634 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001635 } else if (seat->touch &&
1636 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001637 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001638 if ((surface == shsurf->surface) &&
1639 (surface_touch_move(shsurf, seat) < 0))
1640 wl_resource_post_no_memory(resource);
1641 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642}
1643
1644struct weston_resize_grab {
1645 struct shell_grab base;
1646 uint32_t edges;
1647 int32_t width, height;
1648};
1649
1650static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001651resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1652 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653{
1654 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001655 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001656 struct shell_surface *shsurf = resize->base.shsurf;
1657 int32_t width, height;
1658 wl_fixed_t from_x, from_y;
1659 wl_fixed_t to_x, to_y;
1660
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001661 weston_pointer_move(pointer, x, y);
1662
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001663 if (!shsurf)
1664 return;
1665
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 weston_view_from_global_fixed(shsurf->view,
1667 pointer->grab_x, pointer->grab_y,
1668 &from_x, &from_y);
1669 weston_view_from_global_fixed(shsurf->view,
1670 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001671
1672 width = resize->width;
1673 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1674 width += wl_fixed_to_int(from_x - to_x);
1675 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1676 width += wl_fixed_to_int(to_x - from_x);
1677 }
1678
1679 height = resize->height;
1680 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1681 height += wl_fixed_to_int(from_y - to_y);
1682 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1683 height += wl_fixed_to_int(to_y - from_y);
1684 }
1685
1686 shsurf->client->send_configure(shsurf->surface,
1687 resize->edges, width, height);
1688}
1689
1690static void
1691send_configure(struct weston_surface *surface,
1692 uint32_t edges, int32_t width, int32_t height)
1693{
1694 struct shell_surface *shsurf = get_shell_surface(surface);
1695
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001696 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001697 edges, width, height);
1698}
1699
1700static const struct weston_shell_client shell_client = {
1701 send_configure
1702};
1703
1704static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001705resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706 uint32_t time, uint32_t button, uint32_t state_w)
1707{
1708 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001709 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710 enum wl_pointer_button_state state = state_w;
1711
1712 if (pointer->button_count == 0 &&
1713 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1714 shell_grab_end(&resize->base);
1715 free(grab);
1716 }
1717}
1718
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001719static void
1720resize_grab_cancel(struct weston_pointer_grab *grab)
1721{
1722 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1723
1724 shell_grab_end(&resize->base);
1725 free(grab);
1726}
1727
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001728static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001729 noop_grab_focus,
1730 resize_grab_motion,
1731 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001732 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001733};
1734
Giulio Camuffob8366642013-04-25 13:57:46 +03001735/*
1736 * Returns the bounding box of a surface and all its sub-surfaces,
1737 * in the surface coordinates system. */
1738static void
1739surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1740 int32_t *y, int32_t *w, int32_t *h) {
1741 pixman_region32_t region;
1742 pixman_box32_t *box;
1743 struct weston_subsurface *subsurface;
1744
1745 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 surface->width,
1747 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001748
1749 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1750 pixman_region32_union_rect(&region, &region,
1751 subsurface->position.x,
1752 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 subsurface->surface->width,
1754 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001755 }
1756
1757 box = pixman_region32_extents(&region);
1758 if (x)
1759 *x = box->x1;
1760 if (y)
1761 *y = box->y1;
1762 if (w)
1763 *w = box->x2 - box->x1;
1764 if (h)
1765 *h = box->y2 - box->y1;
1766
1767 pixman_region32_fini(&region);
1768}
1769
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770static int
1771surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001772 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001773{
1774 struct weston_resize_grab *resize;
1775
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001776 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1777 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778 return 0;
1779
1780 if (edges == 0 || edges > 15 ||
1781 (edges & 3) == 3 || (edges & 12) == 12)
1782 return 0;
1783
1784 resize = malloc(sizeof *resize);
1785 if (!resize)
1786 return -1;
1787
1788 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001789 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1790 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001791
1792 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001793 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001794
1795 return 0;
1796}
1797
1798static void
1799shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1800 struct wl_resource *seat_resource, uint32_t serial,
1801 uint32_t edges)
1802{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001803 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001804 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001805 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001806
1807 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1808 return;
1809
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001811 if (seat->pointer->button_count == 0 ||
1812 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001813 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001814 return;
1815
Kristian Høgsberge3148752013-05-06 23:19:49 -04001816 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001817 wl_resource_post_no_memory(resource);
1818}
1819
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001820static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001821end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1822
1823static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001824busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001825{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001827 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001829 wl_fixed_t sx, sy;
1830
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 view = weston_compositor_pick_view(pointer->seat->compositor,
1832 pointer->x, pointer->y,
1833 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001834
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001836 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001837}
1838
1839static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001840busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1841 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001842{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001843 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001844}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001845
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001847busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848 uint32_t time, uint32_t button, uint32_t state)
1849{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001850 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001851 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001852 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001853
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001854 if (shsurf && button == BTN_LEFT && state) {
1855 activate(shsurf->shell, shsurf->surface, seat);
1856 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001857 } else if (shsurf && button == BTN_RIGHT && state) {
1858 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001859 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001860 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001861}
1862
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001863static void
1864busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1865{
1866 struct shell_grab *grab = (struct shell_grab *) base;
1867
1868 shell_grab_end(grab);
1869 free(grab);
1870}
1871
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001872static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001873 busy_cursor_grab_focus,
1874 busy_cursor_grab_motion,
1875 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001876 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001877};
1878
1879static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001880set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001881{
1882 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001883
1884 grab = malloc(sizeof *grab);
1885 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001886 return;
1887
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001888 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1889 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001890}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001891
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001892static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001893end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001894{
1895 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1896
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001897 if (grab->grab.interface == &busy_cursor_grab_interface &&
1898 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001899 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001900 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001901 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001902}
1903
Scott Moreau9521d5e2012-04-19 13:06:17 -06001904static void
1905ping_timer_destroy(struct shell_surface *shsurf)
1906{
1907 if (!shsurf || !shsurf->ping_timer)
1908 return;
1909
1910 if (shsurf->ping_timer->source)
1911 wl_event_source_remove(shsurf->ping_timer->source);
1912
1913 free(shsurf->ping_timer);
1914 shsurf->ping_timer = NULL;
1915}
1916
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001917static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001918ping_timeout_handler(void *data)
1919{
1920 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001921 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001922
Scott Moreau9521d5e2012-04-19 13:06:17 -06001923 /* Client is not responding */
1924 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001925
1926 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001927 if (seat->pointer->focus &&
1928 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001929 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001930
1931 return 1;
1932}
1933
1934static void
1935ping_handler(struct weston_surface *surface, uint32_t serial)
1936{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001937 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001938 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001939 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001940
1941 if (!shsurf)
1942 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001943 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001944 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001945
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001946 if (shsurf->surface == shsurf->shell->grab_surface)
1947 return;
1948
Scott Moreauff1db4a2012-04-17 19:06:18 -06001949 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001950 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001951 if (!shsurf->ping_timer)
1952 return;
1953
1954 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001955 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1956 shsurf->ping_timer->source =
1957 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1958 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1959
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001960 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001961 }
1962}
1963
1964static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001965handle_pointer_focus(struct wl_listener *listener, void *data)
1966{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001967 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001968 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001969 struct weston_compositor *compositor;
1970 struct shell_surface *shsurf;
1971 uint32_t serial;
1972
Jason Ekstranda7af7042013-10-12 22:38:11 -05001973 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001974 return;
1975
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976 compositor = view->surface->compositor;
1977 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001978
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001979 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001980 set_busy_cursor(shsurf, pointer);
1981 } else {
1982 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001983 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001984 }
1985}
1986
1987static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001988create_pointer_focus_listener(struct weston_seat *seat)
1989{
1990 struct wl_listener *listener;
1991
Kristian Høgsberge3148752013-05-06 23:19:49 -04001992 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001993 return;
1994
1995 listener = malloc(sizeof *listener);
1996 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001997 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001998}
1999
2000static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002001shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
2002 uint32_t serial)
2003{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002004 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002005 struct weston_seat *seat;
2006 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002007
Kristian Høgsberg92374e12012-08-11 22:39:12 -04002008 if (shsurf->ping_timer == NULL)
2009 /* Just ignore unsolicited pong. */
2010 return;
2011
Scott Moreauff1db4a2012-04-17 19:06:18 -06002012 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002013 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02002014 wl_list_for_each(seat, &ec->seat_list, link) {
2015 if(seat->pointer)
2016 end_busy_cursor(shsurf, seat->pointer);
2017 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06002018 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002019 }
2020}
2021
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002022static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002023set_title(struct shell_surface *shsurf, const char *title)
2024{
2025 free(shsurf->title);
2026 shsurf->title = strdup(title);
2027}
2028
2029static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002030shell_surface_set_title(struct wl_client *client,
2031 struct wl_resource *resource, const char *title)
2032{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002033 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002034
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002035 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002036}
2037
2038static void
2039shell_surface_set_class(struct wl_client *client,
2040 struct wl_resource *resource, const char *class)
2041{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002042 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002043
2044 free(shsurf->class);
2045 shsurf->class = strdup(class);
2046}
2047
Alex Wu4539b082012-03-01 12:57:46 +08002048static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002049restore_output_mode(struct weston_output *output)
2050{
Hardening57388e42013-09-18 23:56:36 +02002051 if (output->current_mode != output->original_mode ||
2052 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002053 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002054 output->original_mode,
2055 output->original_scale,
2056 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002057}
2058
2059static void
2060restore_all_output_modes(struct weston_compositor *compositor)
2061{
2062 struct weston_output *output;
2063
2064 wl_list_for_each(output, &compositor->output_list, link)
2065 restore_output_mode(output);
2066}
2067
Philip Withnallbecb77e2013-11-25 18:01:30 +00002068static int
2069get_output_panel_height(struct desktop_shell *shell,
2070 struct weston_output *output)
2071{
2072 struct weston_view *view;
2073 int panel_height = 0;
2074
2075 if (!output)
2076 return 0;
2077
2078 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2079 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002080 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002081 break;
2082 }
2083 }
2084
2085 return panel_height;
2086}
2087
Philip Withnall07926d92013-11-25 18:01:40 +00002088/* The surface will be inserted into the list immediately after the link
2089 * returned by this function (i.e. will be stacked immediately above the
2090 * returned link). */
2091static struct wl_list *
2092shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2093{
2094 struct workspace *ws;
2095
2096 switch (shsurf->type) {
Philip Withnallda704d92013-11-25 18:01:46 +00002097 case SHELL_SURFACE_POPUP: {
2098 /* Popups should go at the front of the workspace of their
2099 * parent surface, rather than just in front of the parent. This
2100 * fixes the situation where there are two top-level windows:
2101 * - Above
2102 * - Below
2103 * and a pop-up menu is created for 'Below'. We want:
2104 * - Popup
2105 * - Above
2106 * - Below
2107 * not:
2108 * - Above
2109 * - Popup
2110 * - Below
2111 */
2112 struct shell_surface *parent_shsurf;
2113
2114 parent_shsurf = get_shell_surface(shsurf->parent);
2115 if (parent_shsurf != NULL)
2116 return shell_surface_calculate_layer_link(parent_shsurf);
2117
2118 break;
2119 }
2120
Philip Withnall07926d92013-11-25 18:01:40 +00002121 case SHELL_SURFACE_TRANSIENT: {
2122 /* Move the surface to its parent layer so that surfaces which
2123 * are transient for fullscreen surfaces don't get hidden by the
Philip Withnallda704d92013-11-25 18:01:46 +00002124 * fullscreen surfaces. However, unlike popups, transient
2125 * surfaces are stacked in front of their parent but not in
2126 * front of other surfaces of the same type. */
Philip Withnall07926d92013-11-25 18:01:40 +00002127 struct weston_view *parent;
2128
2129 /* TODO: Handle a parent with multiple views */
2130 parent = get_default_view(shsurf->parent);
2131 if (parent)
2132 return parent->layer_link.prev;
2133
2134 break;
2135 }
2136
2137 case SHELL_SURFACE_FULLSCREEN:
2138 return &shsurf->shell->fullscreen_layer.view_list;
2139
2140 case SHELL_SURFACE_XWAYLAND:
2141 case SHELL_SURFACE_TOPLEVEL:
2142 case SHELL_SURFACE_MAXIMIZED:
2143 case SHELL_SURFACE_NONE:
2144 default:
2145 /* Go to the fallback, below. */
2146 break;
2147 }
2148
2149 /* Move the surface to a normal workspace layer so that surfaces
2150 * which were previously fullscreen or transient are no longer
2151 * rendered on top. */
2152 ws = get_current_workspace(shsurf->shell);
2153 return &ws->layer.view_list;
2154}
2155
Philip Withnall648a4dd2013-11-25 18:01:44 +00002156static void
2157shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2158{
2159 struct shell_surface *child;
2160
2161 /* Move the child layers to the same workspace as shsurf. They will be
2162 * stacked above shsurf. */
2163 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2164 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2165 weston_view_geometry_dirty(child->view);
2166 wl_list_remove(&child->view->layer_link);
2167 wl_list_insert(shsurf->view->layer_link.prev,
2168 &child->view->layer_link);
2169 weston_view_geometry_dirty(child->view);
2170 weston_surface_damage(child->surface);
2171
2172 /* Recurse. We don’t expect this to recurse very far (if
2173 * at all) because that would imply we have transient
2174 * (or popup) children of transient surfaces, which
2175 * would be unusual. */
2176 shell_surface_update_child_surface_layers(child);
2177 }
2178 }
2179}
2180
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002181/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002182 * geometry to ensure the changes are redrawn.
2183 *
2184 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2185 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002186static void
2187shell_surface_update_layer(struct shell_surface *shsurf)
2188{
2189 struct wl_list *new_layer_link;
2190
2191 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2192
2193 if (new_layer_link == &shsurf->view->layer_link)
2194 return;
2195
2196 weston_view_geometry_dirty(shsurf->view);
2197 wl_list_remove(&shsurf->view->layer_link);
2198 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2199 weston_view_geometry_dirty(shsurf->view);
2200 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002201
2202 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002203}
2204
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002205static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002206shell_surface_set_parent(struct shell_surface *shsurf,
2207 struct weston_surface *parent)
2208{
2209 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002210
2211 wl_list_remove(&shsurf->children_link);
2212 wl_list_init(&shsurf->children_link);
2213
2214 /* Insert into the parent surface’s child list. */
2215 if (parent != NULL) {
2216 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2217 if (parent_shsurf != NULL)
2218 wl_list_insert(&parent_shsurf->children_list,
2219 &shsurf->children_link);
2220 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002221}
2222
2223static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002224shell_surface_set_output(struct shell_surface *shsurf,
2225 struct weston_output *output)
2226{
2227 struct weston_surface *es = shsurf->surface;
2228
2229 /* get the default output, if the client set it as NULL
2230 check whether the ouput is available */
2231 if (output)
2232 shsurf->output = output;
2233 else if (es->output)
2234 shsurf->output = es->output;
2235 else
2236 shsurf->output = get_default_output(es->compositor);
2237}
2238
2239static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002240set_toplevel(struct shell_surface *shsurf)
2241{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002242 shell_surface_set_parent(shsurf, NULL);
2243
Philip Withnallbecb77e2013-11-25 18:01:30 +00002244 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002245
2246 /* The layer_link is updated in set_surface_type(),
2247 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002248}
2249
2250static void
2251shell_surface_set_toplevel(struct wl_client *client,
2252 struct wl_resource *resource)
2253{
2254 struct shell_surface *surface = wl_resource_get_user_data(resource);
2255
2256 set_toplevel(surface);
2257}
2258
2259static void
2260set_transient(struct shell_surface *shsurf,
2261 struct weston_surface *parent, int x, int y, uint32_t flags)
2262{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002263 assert(parent != NULL);
2264
Philip Withnallbecb77e2013-11-25 18:01:30 +00002265 shsurf->transient.x = x;
2266 shsurf->transient.y = y;
2267 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002268
2269 shell_surface_set_parent(shsurf, parent);
2270
Philip Withnallbecb77e2013-11-25 18:01:30 +00002271 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002272
2273 /* The layer_link is updated in set_surface_type(),
2274 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002275}
2276
2277static void
2278shell_surface_set_transient(struct wl_client *client,
2279 struct wl_resource *resource,
2280 struct wl_resource *parent_resource,
2281 int x, int y, uint32_t flags)
2282{
2283 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2284 struct weston_surface *parent =
2285 wl_resource_get_user_data(parent_resource);
2286
2287 set_transient(shsurf, parent, x, y, flags);
2288}
2289
2290static void
2291set_fullscreen(struct shell_surface *shsurf,
2292 uint32_t method,
2293 uint32_t framerate,
2294 struct weston_output *output)
2295{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002296 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002297
2298 shsurf->fullscreen_output = shsurf->output;
2299 shsurf->fullscreen.type = method;
2300 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002301
2302 shell_surface_set_parent(shsurf, NULL);
2303
Philip Withnallbecb77e2013-11-25 18:01:30 +00002304 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
2305
2306 shsurf->client->send_configure(shsurf->surface, 0,
2307 shsurf->output->width,
2308 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002309
2310 /* The layer_link is updated in set_surface_type(),
2311 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002312}
2313
2314static void
2315unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002316{
Philip Withnallf85fe842013-11-25 18:01:37 +00002317 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002318 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2319 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002320 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002321 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002322 shsurf->fullscreen_output = NULL;
2323
Alex Wu4539b082012-03-01 12:57:46 +08002324 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2325 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002326
Alex Wu4539b082012-03-01 12:57:46 +08002327 wl_list_remove(&shsurf->fullscreen.transform.link);
2328 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002329
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330 if (shsurf->fullscreen.black_view)
2331 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2332 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002333
Jason Ekstranda7af7042013-10-12 22:38:11 -05002334 weston_view_set_position(shsurf->view,
2335 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002336 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002337 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002338 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002339 shsurf->saved_rotation_valid = false;
2340 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002341
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002342 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002343}
2344
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002345static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002346shell_surface_set_fullscreen(struct wl_client *client,
2347 struct wl_resource *resource,
2348 uint32_t method,
2349 uint32_t framerate,
2350 struct wl_resource *output_resource)
2351{
2352 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2353 struct weston_output *output;
2354
2355 if (output_resource)
2356 output = wl_resource_get_user_data(output_resource);
2357 else
2358 output = NULL;
2359
2360 set_fullscreen(shsurf, method, framerate, output);
2361}
2362
2363static void
2364set_popup(struct shell_surface *shsurf,
2365 struct weston_surface *parent,
2366 struct weston_seat *seat,
2367 uint32_t serial,
2368 int32_t x,
2369 int32_t y)
2370{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002371 assert(parent != NULL);
2372
Philip Withnallbecb77e2013-11-25 18:01:30 +00002373 shsurf->popup.shseat = get_shell_seat(seat);
2374 shsurf->popup.serial = serial;
2375 shsurf->popup.x = x;
2376 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002377
2378 shell_surface_set_parent(shsurf, parent);
Philip Withnallb995e1d2013-11-25 18:01:39 +00002379
2380 shsurf->next_type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002381}
2382
2383static void
2384shell_surface_set_popup(struct wl_client *client,
2385 struct wl_resource *resource,
2386 struct wl_resource *seat_resource,
2387 uint32_t serial,
2388 struct wl_resource *parent_resource,
2389 int32_t x, int32_t y, uint32_t flags)
2390{
2391 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2392
2393 set_popup(shsurf,
2394 wl_resource_get_user_data(parent_resource),
2395 wl_resource_get_user_data(seat_resource),
2396 serial, x, y);
2397}
2398
2399static void
2400set_maximized(struct shell_surface *shsurf,
2401 struct weston_output *output)
2402{
2403 struct desktop_shell *shell;
2404 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002405
Philip Withnall352e7ed2013-11-25 18:01:35 +00002406 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002407
2408 shell = shell_surface_get_shell(shsurf);
2409 panel_height = get_output_panel_height(shell, shsurf->output);
2410 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2411
2412 shsurf->client->send_configure(shsurf->surface, edges,
2413 shsurf->output->width,
2414 shsurf->output->height - panel_height);
2415
Philip Withnalldc4332f2013-11-25 18:01:38 +00002416 shell_surface_set_parent(shsurf, NULL);
2417
Philip Withnallbecb77e2013-11-25 18:01:30 +00002418 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
2419}
2420
2421static void
2422unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002423{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002424 /* undo all maximized things here */
2425 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002426 weston_view_set_position(shsurf->view,
2427 shsurf->saved_x,
2428 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002429
2430 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002431 wl_list_insert(&shsurf->view->geometry.transformation_list,
2432 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002433 shsurf->saved_rotation_valid = false;
2434 }
2435
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002436 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002437}
2438
Philip Withnallbecb77e2013-11-25 18:01:30 +00002439static void
2440shell_surface_set_maximized(struct wl_client *client,
2441 struct wl_resource *resource,
2442 struct wl_resource *output_resource)
2443{
2444 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2445 struct weston_output *output;
2446
2447 if (output_resource)
2448 output = wl_resource_get_user_data(output_resource);
2449 else
2450 output = NULL;
2451
2452 set_maximized(shsurf, output);
2453}
2454
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002455/* This is only ever called from set_surface_type(), so there’s no need to
2456 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002457static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002458reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002459{
2460 switch (surface->type) {
2461 case SHELL_SURFACE_FULLSCREEN:
Philip Withnallbecb77e2013-11-25 18:01:30 +00002462 unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002463 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002464 case SHELL_SURFACE_MAXIMIZED:
Philip Withnallbecb77e2013-11-25 18:01:30 +00002465 unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002466 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002467 case SHELL_SURFACE_NONE:
2468 case SHELL_SURFACE_TOPLEVEL:
2469 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002470 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002471 case SHELL_SURFACE_XWAYLAND:
Philip Withnall0f640e22013-11-25 18:01:31 +00002472 default:
Pekka Paalanen98262232011-12-01 10:42:22 +02002473 break;
2474 }
2475
2476 surface->type = SHELL_SURFACE_NONE;
2477 return 0;
2478}
2479
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002480static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002481set_surface_type(struct shell_surface *shsurf)
2482{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002483 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002484 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002485
Philip Withnallbecb77e2013-11-25 18:01:30 +00002486 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002487
2488 shsurf->type = shsurf->next_type;
2489 shsurf->next_type = SHELL_SURFACE_NONE;
2490
2491 switch (shsurf->type) {
2492 case SHELL_SURFACE_TOPLEVEL:
2493 break;
2494 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002495 if (pev)
2496 weston_view_set_position(shsurf->view,
2497 pev->geometry.x + shsurf->transient.x,
2498 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002499 break;
2500
2501 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002502 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002503 shsurf->saved_x = shsurf->view->geometry.x;
2504 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002505 shsurf->saved_position_valid = true;
2506
2507 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2508 wl_list_remove(&shsurf->rotation.transform.link);
2509 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002510 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002511 shsurf->saved_rotation_valid = true;
2512 }
2513 break;
2514
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002515 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002516 weston_view_set_position(shsurf->view, shsurf->transient.x,
2517 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002518 break;
2519
Philip Withnall0f640e22013-11-25 18:01:31 +00002520 case SHELL_SURFACE_POPUP:
2521 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002522 default:
2523 break;
2524 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002525
2526 /* Update the surface’s layer. */
2527 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002528}
2529
Tiago Vignattibe143262012-04-16 17:31:41 +03002530static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002531shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002532{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002533 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002534}
2535
Alex Wu21858432012-04-01 20:13:08 +08002536static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002537black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002538
Jason Ekstranda7af7042013-10-12 22:38:11 -05002539static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002540create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002541 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002542 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002543{
2544 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002546
2547 surface = weston_surface_create(ec);
2548 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002549 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002550 return NULL;
2551 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 view = weston_view_create(surface);
2553 if (surface == NULL) {
2554 weston_log("no memory\n");
2555 weston_surface_destroy(surface);
2556 return NULL;
2557 }
Alex Wu4539b082012-03-01 12:57:46 +08002558
Alex Wu21858432012-04-01 20:13:08 +08002559 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002560 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002561 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002562 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002563 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002564 pixman_region32_fini(&surface->input);
2565 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002566
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002567 surface->width = w;
2568 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002569 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002570
2571 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002572}
2573
Philip Withnalled8f1a92013-11-25 18:01:43 +00002574static void
2575shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2576{
2577 struct weston_output *output = shsurf->fullscreen_output;
2578
2579 assert(shsurf->type == SHELL_SURFACE_FULLSCREEN);
2580
2581 if (!shsurf->fullscreen.black_view)
2582 shsurf->fullscreen.black_view =
2583 create_black_surface(shsurf->surface->compositor,
2584 shsurf->surface,
2585 output->x, output->y,
2586 output->width,
2587 output->height);
2588
2589 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2590 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2591 wl_list_insert(&shsurf->view->layer_link,
2592 &shsurf->fullscreen.black_view->layer_link);
2593 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2594 weston_surface_damage(shsurf->surface);
2595}
2596
Alex Wu4539b082012-03-01 12:57:46 +08002597/* Create black surface and append it to the associated fullscreen surface.
2598 * Handle size dismatch and positioning according to the method. */
2599static void
2600shell_configure_fullscreen(struct shell_surface *shsurf)
2601{
2602 struct weston_output *output = shsurf->fullscreen_output;
2603 struct weston_surface *surface = shsurf->surface;
2604 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002605 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002606 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002607
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002608 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2609 restore_output_mode(output);
2610
Philip Withnalled8f1a92013-11-25 18:01:43 +00002611 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002612
Jason Ekstranda7af7042013-10-12 22:38:11 -05002613 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002614 &surf_width, &surf_height);
2615
Alex Wu4539b082012-03-01 12:57:46 +08002616 switch (shsurf->fullscreen.type) {
2617 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002618 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002620 break;
2621 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002622 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002623 if (output->width == surf_width &&
2624 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002625 weston_view_set_position(shsurf->view,
2626 output->x - surf_x,
2627 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002628 break;
2629 }
2630
Alex Wu4539b082012-03-01 12:57:46 +08002631 matrix = &shsurf->fullscreen.transform.matrix;
2632 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002633
Scott Moreau1bad5db2012-08-18 01:04:05 -06002634 output_aspect = (float) output->width /
2635 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002636 /* XXX: Use surf_width and surf_height here? */
2637 surface_aspect = (float) surface->width /
2638 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002639 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002640 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002641 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002642 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002643 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002644 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002645
Alex Wu4539b082012-03-01 12:57:46 +08002646 weston_matrix_scale(matrix, scale, scale, 1);
2647 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002648 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002649 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002650 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2651 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002652 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002653
Alex Wu4539b082012-03-01 12:57:46 +08002654 break;
2655 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002656 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002657 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002658 surf_width * surface->buffer_viewport.scale,
2659 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002660 shsurf->fullscreen.framerate};
2661
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002662 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002663 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 weston_view_set_position(shsurf->view,
2665 output->x - surf_x,
2666 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002667 shsurf->fullscreen.black_view->surface->width = output->width;
2668 shsurf->fullscreen.black_view->surface->height = output->height;
2669 weston_view_set_position(shsurf->fullscreen.black_view,
2670 output->x - surf_x,
2671 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002672 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002673 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002674 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002675 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002676 }
Alex Wubd3354b2012-04-17 17:20:49 +08002677 }
Alex Wu4539b082012-03-01 12:57:46 +08002678 break;
2679 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002680 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002681 break;
2682 default:
2683 break;
2684 }
2685}
2686
Alex Wu4539b082012-03-01 12:57:46 +08002687static void
2688shell_map_fullscreen(struct shell_surface *shsurf)
2689{
Alex Wubd3354b2012-04-17 17:20:49 +08002690 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002691}
2692
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002693static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002694set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2695{
2696 /* XXX: using the same fields for transient type */
2697 shsurf->transient.x = x;
2698 shsurf->transient.y = y;
2699 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002700
2701 shell_surface_set_parent(shsurf, NULL);
2702
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002703 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2704}
2705
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002706static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002707
2708static void
2709destroy_shell_seat(struct wl_listener *listener, void *data)
2710{
2711 struct shell_seat *shseat =
2712 container_of(listener,
2713 struct shell_seat, seat_destroy_listener);
2714 struct shell_surface *shsurf, *prev = NULL;
2715
2716 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002717 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002718 shseat->popup_grab.client = NULL;
2719
2720 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2721 shsurf->popup.shseat = NULL;
2722 if (prev) {
2723 wl_list_init(&prev->popup.grab_link);
2724 }
2725 prev = shsurf;
2726 }
2727 wl_list_init(&prev->popup.grab_link);
2728 }
2729
2730 wl_list_remove(&shseat->seat_destroy_listener.link);
2731 free(shseat);
2732}
2733
2734static struct shell_seat *
2735create_shell_seat(struct weston_seat *seat)
2736{
2737 struct shell_seat *shseat;
2738
2739 shseat = calloc(1, sizeof *shseat);
2740 if (!shseat) {
2741 weston_log("no memory to allocate shell seat\n");
2742 return NULL;
2743 }
2744
2745 shseat->seat = seat;
2746 wl_list_init(&shseat->popup_grab.surfaces_list);
2747
2748 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2749 wl_signal_add(&seat->destroy_signal,
2750 &shseat->seat_destroy_listener);
2751
2752 return shseat;
2753}
2754
2755static struct shell_seat *
2756get_shell_seat(struct weston_seat *seat)
2757{
2758 struct wl_listener *listener;
2759
2760 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2761 if (listener == NULL)
2762 return create_shell_seat(seat);
2763
2764 return container_of(listener,
2765 struct shell_seat, seat_destroy_listener);
2766}
2767
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002768static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002769popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002770{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002771 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002772 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002773 struct shell_seat *shseat =
2774 container_of(grab, struct shell_seat, popup_grab.grab);
2775 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002776 wl_fixed_t sx, sy;
2777
Jason Ekstranda7af7042013-10-12 22:38:11 -05002778 view = weston_compositor_pick_view(pointer->seat->compositor,
2779 pointer->x, pointer->y,
2780 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002781
Jason Ekstranda7af7042013-10-12 22:38:11 -05002782 if (view && view->surface->resource &&
2783 wl_resource_get_client(view->surface->resource) == client) {
2784 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002785 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002786 weston_pointer_set_focus(pointer, NULL,
2787 wl_fixed_from_int(0),
2788 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002789 }
2790}
2791
2792static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002793popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2794 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002795{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002796 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002797 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002798 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002799
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002800 weston_pointer_move(pointer, x, y);
2801
Neil Roberts96d790e2013-09-19 17:32:00 +01002802 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002803 weston_view_from_global_fixed(pointer->focus,
2804 pointer->x, pointer->y,
2805 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002806 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002807 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002808}
2809
2810static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002811popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002812 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002813{
2814 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002815 struct shell_seat *shseat =
2816 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002817 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002818 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002819 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002820 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002821
Neil Roberts96d790e2013-09-19 17:32:00 +01002822 resource_list = &grab->pointer->focus_resource_list;
2823 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002824 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002825 wl_resource_for_each(resource, resource_list) {
2826 wl_pointer_send_button(resource, serial,
2827 time, button, state);
2828 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002829 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002830 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002831 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002832 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002833 }
2834
Daniel Stone4dbadb12012-05-30 16:31:51 +01002835 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002836 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002837}
2838
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002839static void
2840popup_grab_cancel(struct weston_pointer_grab *grab)
2841{
2842 popup_grab_end(grab->pointer);
2843}
2844
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002845static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002846 popup_grab_focus,
2847 popup_grab_motion,
2848 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002849 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002850};
2851
2852static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002853popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002854{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002855 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002856 struct shell_seat *shseat =
2857 container_of(grab, struct shell_seat, popup_grab.grab);
2858 struct shell_surface *shsurf;
2859 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002860
2861 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002862 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002863 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002864 shseat->popup_grab.grab.interface = NULL;
2865 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002866 /* Send the popup_done event to all the popups open */
2867 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002868 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002869 shsurf->popup.shseat = NULL;
2870 if (prev) {
2871 wl_list_init(&prev->popup.grab_link);
2872 }
2873 prev = shsurf;
2874 }
2875 wl_list_init(&prev->popup.grab_link);
2876 wl_list_init(&shseat->popup_grab.surfaces_list);
2877 }
2878}
2879
2880static void
2881add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2882{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002883 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002884
2885 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002886 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002887 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002888 /* We must make sure here that this popup was opened after
2889 * a mouse press, and not just by moving around with other
2890 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002891 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002892 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002893
Rob Bradforddfe31052013-06-26 19:49:11 +01002894 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002895 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002896 } else {
2897 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002898 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002899}
2900
2901static void
2902remove_popup_grab(struct shell_surface *shsurf)
2903{
2904 struct shell_seat *shseat = shsurf->popup.shseat;
2905
2906 wl_list_remove(&shsurf->popup.grab_link);
2907 wl_list_init(&shsurf->popup.grab_link);
2908 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002909 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002910 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002911 }
2912}
2913
2914static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002915shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002916{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002917 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002918 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002919
Jason Ekstranda7af7042013-10-12 22:38:11 -05002920 shsurf->surface->output = parent_view->output;
2921 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002922
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 weston_view_set_transform_parent(shsurf->view, parent_view);
2924 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2925 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002926
Kristian Høgsberge3148752013-05-06 23:19:49 -04002927 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002928 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002929 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002930 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002931 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002932 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002933}
2934
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002935static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002936 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002937 shell_surface_move,
2938 shell_surface_resize,
2939 shell_surface_set_toplevel,
2940 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002941 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002942 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002943 shell_surface_set_maximized,
2944 shell_surface_set_title,
2945 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002946};
2947
2948static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002949destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002950{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002951 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2952
Giulio Camuffo5085a752013-03-25 21:42:45 +01002953 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2954 remove_popup_grab(shsurf);
2955 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002956
Alex Wubd3354b2012-04-17 17:20:49 +08002957 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002958 shell_surface_is_top_fullscreen(shsurf))
2959 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002960
Jason Ekstranda7af7042013-10-12 22:38:11 -05002961 if (shsurf->fullscreen.black_view)
2962 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002963
Alex Wubd3354b2012-04-17 17:20:49 +08002964 /* As destroy_resource() use wl_list_for_each_safe(),
2965 * we can always remove the listener.
2966 */
2967 wl_list_remove(&shsurf->surface_destroy_listener.link);
2968 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002969 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002970 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002971
Jason Ekstranda7af7042013-10-12 22:38:11 -05002972 weston_view_destroy(shsurf->view);
2973
Philip Withnall648a4dd2013-11-25 18:01:44 +00002974 wl_list_remove(&shsurf->children_link);
2975
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002976 wl_list_remove(&shsurf->link);
2977 free(shsurf);
2978}
2979
2980static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002981shell_destroy_shell_surface(struct wl_resource *resource)
2982{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002983 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002984
2985 destroy_shell_surface(shsurf);
2986}
2987
2988static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002989shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002990{
2991 struct shell_surface *shsurf = container_of(listener,
2992 struct shell_surface,
2993 surface_destroy_listener);
2994
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002995 if (shsurf->resource)
2996 wl_resource_destroy(shsurf->resource);
2997 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002998 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002999}
3000
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003001static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003002shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003003
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003004static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003005get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003006{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003007 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003008 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003009 else
3010 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003011}
3012
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003013static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003014create_shell_surface(void *shell, struct weston_surface *surface,
3015 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003016{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003017 struct shell_surface *shsurf;
3018
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003019 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003020 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003021 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003022 }
3023
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003024 shsurf = calloc(1, sizeof *shsurf);
3025 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003026 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003027 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003028 }
3029
Jason Ekstranda7af7042013-10-12 22:38:11 -05003030 shsurf->view = weston_view_create(surface);
3031 if (!shsurf->view) {
3032 weston_log("no memory to allocate shell surface\n");
3033 free(shsurf);
3034 return NULL;
3035 }
3036
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003037 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003038 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003039
Tiago Vignattibc052c92012-04-19 16:18:18 +03003040 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003041 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003042 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003043 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003044 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003045 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3046 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003047 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003048 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003049 wl_list_init(&shsurf->fullscreen.transform.link);
3050
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003051 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003052 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003053 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003054 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003055
3056 /* init link so its safe to always remove it in destroy_shell_surface */
3057 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003058 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003059
Pekka Paalanen460099f2012-01-20 16:48:25 +02003060 /* empty when not in use */
3061 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003062 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003063
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003064 wl_list_init(&shsurf->workspace_transform.link);
3065
Philip Withnall648a4dd2013-11-25 18:01:44 +00003066 wl_list_init(&shsurf->children_link);
3067 wl_list_init(&shsurf->children_list);
3068 shsurf->parent = NULL;
3069
Pekka Paalanen98262232011-12-01 10:42:22 +02003070 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003071 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003072
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003073 shsurf->client = client;
3074
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003075 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003076}
3077
Jason Ekstranda7af7042013-10-12 22:38:11 -05003078static struct weston_view *
3079get_primary_view(void *shell, struct shell_surface *shsurf)
3080{
3081 return shsurf->view;
3082}
3083
Tiago Vignattibc052c92012-04-19 16:18:18 +03003084static void
3085shell_get_shell_surface(struct wl_client *client,
3086 struct wl_resource *resource,
3087 uint32_t id,
3088 struct wl_resource *surface_resource)
3089{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003090 struct weston_surface *surface =
3091 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003092 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003093 struct shell_surface *shsurf;
3094
3095 if (get_shell_surface(surface)) {
3096 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003097 WL_DISPLAY_ERROR_INVALID_OBJECT,
3098 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003099 return;
3100 }
3101
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003102 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003103 if (!shsurf) {
3104 wl_resource_post_error(surface_resource,
3105 WL_DISPLAY_ERROR_INVALID_OBJECT,
3106 "surface->configure already set");
3107 return;
3108 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003109
Jason Ekstranda85118c2013-06-27 20:17:02 -05003110 shsurf->resource =
3111 wl_resource_create(client,
3112 &wl_shell_surface_interface, 1, id);
3113 wl_resource_set_implementation(shsurf->resource,
3114 &shell_surface_implementation,
3115 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003116}
3117
3118static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003119 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003120};
3121
Kristian Høgsberg07937562011-04-12 17:25:42 -04003122static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003123shell_fade(struct desktop_shell *shell, enum fade_type type);
3124
3125static int
3126screensaver_timeout(void *data)
3127{
3128 struct desktop_shell *shell = data;
3129
3130 shell_fade(shell, FADE_OUT);
3131
3132 return 1;
3133}
3134
3135static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003136handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003137{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003138 struct desktop_shell *shell =
3139 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003140
Pekka Paalanen18027e52011-12-02 16:31:49 +02003141 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003142
3143 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003144 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003145}
3146
3147static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003148launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003149{
3150 if (shell->screensaver.binding)
3151 return;
3152
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003153 if (!shell->screensaver.path) {
3154 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003155 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003156 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003157
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003158 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003159 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003160 return;
3161 }
3162
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003163 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003164 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003165 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003166 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003167}
3168
3169static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003170terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003171{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003172 if (shell->screensaver.process.pid == 0)
3173 return;
3174
3175 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003176}
3177
3178static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003179configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003180{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003181 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003182
Jason Ekstranda7af7042013-10-12 22:38:11 -05003183 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3184 if (v->output == ev->output && v != ev) {
3185 weston_view_unmap(v);
3186 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003187 }
3188 }
3189
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003190 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003191
Jason Ekstranda7af7042013-10-12 22:38:11 -05003192 if (wl_list_empty(&ev->layer_link)) {
3193 wl_list_insert(&layer->view_list, &ev->layer_link);
3194 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003195 }
3196}
3197
3198static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003199background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003200{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003201 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003202 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003203
Jason Ekstranda7af7042013-10-12 22:38:11 -05003204 view = container_of(es->views.next, struct weston_view, surface_link);
3205
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003206 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003207}
3208
3209static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003210desktop_shell_set_background(struct wl_client *client,
3211 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003212 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003213 struct wl_resource *surface_resource)
3214{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003215 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003216 struct weston_surface *surface =
3217 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003218 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003219
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003220 if (surface->configure) {
3221 wl_resource_post_error(surface_resource,
3222 WL_DISPLAY_ERROR_INVALID_OBJECT,
3223 "surface role already assigned");
3224 return;
3225 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003226
Jason Ekstranda7af7042013-10-12 22:38:11 -05003227 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3228 weston_view_destroy(view);
3229 view = weston_view_create(surface);
3230
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003231 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003232 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003233 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003234 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003235 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003236 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003237 surface->output->width,
3238 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003239}
3240
3241static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003242panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003243{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003244 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003245 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003246
Jason Ekstranda7af7042013-10-12 22:38:11 -05003247 view = container_of(es->views.next, struct weston_view, surface_link);
3248
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003249 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003250}
3251
3252static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003253desktop_shell_set_panel(struct wl_client *client,
3254 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003255 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003256 struct wl_resource *surface_resource)
3257{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003258 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003259 struct weston_surface *surface =
3260 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003261 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003262
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003263 if (surface->configure) {
3264 wl_resource_post_error(surface_resource,
3265 WL_DISPLAY_ERROR_INVALID_OBJECT,
3266 "surface role already assigned");
3267 return;
3268 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003269
Jason Ekstranda7af7042013-10-12 22:38:11 -05003270 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3271 weston_view_destroy(view);
3272 view = weston_view_create(surface);
3273
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003274 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003275 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003276 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003277 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003278 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003279 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003280 surface->output->width,
3281 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003282}
3283
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003284static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003285lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003286{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003287 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003288 struct weston_view *view;
3289
3290 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003291
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003292 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003293 return;
3294
Jason Ekstranda7af7042013-10-12 22:38:11 -05003295 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003296
3297 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003298 wl_list_insert(&shell->lock_layer.view_list,
3299 &view->layer_link);
3300 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003301 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003302 }
3303}
3304
3305static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003306handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003307{
Tiago Vignattibe143262012-04-16 17:31:41 +03003308 struct desktop_shell *shell =
3309 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003310
Martin Minarik6d118362012-06-07 18:01:59 +02003311 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003312 shell->lock_surface = NULL;
3313}
3314
3315static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003316desktop_shell_set_lock_surface(struct wl_client *client,
3317 struct wl_resource *resource,
3318 struct wl_resource *surface_resource)
3319{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003320 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003321 struct weston_surface *surface =
3322 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003323
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003324 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003325
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003326 if (!shell->locked)
3327 return;
3328
Pekka Paalanen98262232011-12-01 10:42:22 +02003329 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003330
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003331 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003332 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003333 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003334
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003335 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003336 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003337 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003338}
3339
3340static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003341resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003342{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003343 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003344
Pekka Paalanen77346a62011-11-30 16:26:35 +02003345 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003346
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003347 wl_list_remove(&shell->lock_layer.link);
3348 wl_list_insert(&shell->compositor->cursor_layer.link,
3349 &shell->fullscreen_layer.link);
3350 wl_list_insert(&shell->fullscreen_layer.link,
3351 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003352 if (shell->showing_input_panels) {
3353 wl_list_insert(&shell->panel_layer.link,
3354 &shell->input_panel_layer.link);
3355 wl_list_insert(&shell->input_panel_layer.link,
3356 &ws->layer.link);
3357 } else {
3358 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3359 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003360
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003361 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003362
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003363 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003364 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003365 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003366}
3367
3368static void
3369desktop_shell_unlock(struct wl_client *client,
3370 struct wl_resource *resource)
3371{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003372 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003373
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003374 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003375
3376 if (shell->locked)
3377 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003378}
3379
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003380static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003381desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003382 struct wl_resource *resource,
3383 struct wl_resource *surface_resource)
3384{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003385 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003386
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003387 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003388 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003389}
3390
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003391static void
3392desktop_shell_desktop_ready(struct wl_client *client,
3393 struct wl_resource *resource)
3394{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003395 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003396
3397 shell_fade_startup(shell);
3398}
3399
Kristian Høgsberg75840622011-09-06 13:48:16 -04003400static const struct desktop_shell_interface desktop_shell_implementation = {
3401 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003402 desktop_shell_set_panel,
3403 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003404 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003405 desktop_shell_set_grab_surface,
3406 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003407};
3408
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003409static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003410get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003411{
3412 struct shell_surface *shsurf;
3413
3414 shsurf = get_shell_surface(surface);
3415 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003416 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003417 return shsurf->type;
3418}
3419
Kristian Høgsberg75840622011-09-06 13:48:16 -04003420static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003421move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003422{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003423 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003424 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003425 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003426
Pekka Paalanen01388e22013-04-25 13:57:44 +03003427 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003428 if (surface == NULL)
3429 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003430
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003431 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003432 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3433 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003434 return;
3435
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003436 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003437}
3438
3439static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003440touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3441{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003442 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003443 struct weston_surface *surface;
3444 struct shell_surface *shsurf;
3445
3446 surface = weston_surface_get_main_surface(focus);
3447 if (surface == NULL)
3448 return;
3449
3450 shsurf = get_shell_surface(surface);
3451 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3452 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3453 return;
3454
3455 surface_touch_move(shsurf, (struct weston_seat *) seat);
3456}
3457
3458static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003459resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003460{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003461 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003462 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003463 uint32_t edges = 0;
3464 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003465 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003466
Pekka Paalanen01388e22013-04-25 13:57:44 +03003467 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003468 if (surface == NULL)
3469 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003470
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003471 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003472 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3473 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003474 return;
3475
Jason Ekstranda7af7042013-10-12 22:38:11 -05003476 weston_view_from_global(shsurf->view,
3477 wl_fixed_to_int(seat->pointer->grab_x),
3478 wl_fixed_to_int(seat->pointer->grab_y),
3479 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003480
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003481 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003482 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003483 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003484 edges |= 0;
3485 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003486 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003487
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003488 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003489 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003490 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003491 edges |= 0;
3492 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003493 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003494
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003495 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003496}
3497
3498static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003499surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003500 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003501{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003502 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003503 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003504 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003505 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003506
Pekka Paalanen01388e22013-04-25 13:57:44 +03003507 /* XXX: broken for windows containing sub-surfaces */
3508 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003509 if (surface == NULL)
3510 return;
3511
3512 shsurf = get_shell_surface(surface);
3513 if (!shsurf)
3514 return;
3515
Jason Ekstranda7af7042013-10-12 22:38:11 -05003516 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003517
Jason Ekstranda7af7042013-10-12 22:38:11 -05003518 if (shsurf->view->alpha > 1.0)
3519 shsurf->view->alpha = 1.0;
3520 if (shsurf->view->alpha < step)
3521 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003522
Jason Ekstranda7af7042013-10-12 22:38:11 -05003523 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003524 weston_surface_damage(surface);
3525}
3526
3527static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003528do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003529 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003530{
Daniel Stone37816df2012-05-16 18:45:18 +01003531 struct weston_seat *ws = (struct weston_seat *) seat;
3532 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003533 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003534 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003535
3536 wl_list_for_each(output, &compositor->output_list, link) {
3537 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003538 wl_fixed_to_double(seat->pointer->x),
3539 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003540 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003541 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003542 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003543 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003544 increment = -output->zoom.increment;
3545 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003546 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003547 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003548 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003549 else
3550 increment = 0;
3551
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003552 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003553
Scott Moreaue6603982012-06-11 13:07:51 -06003554 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003555 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003556 else if (output->zoom.level > output->zoom.max_level)
3557 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003558 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003559 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003560 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003561
Scott Moreaue6603982012-06-11 13:07:51 -06003562 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003563
Jason Ekstranda7af7042013-10-12 22:38:11 -05003564 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003565 }
3566 }
3567}
3568
Scott Moreauccbf29d2012-02-22 14:21:41 -07003569static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003570zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003571 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003572{
3573 do_zoom(seat, time, 0, axis, value);
3574}
3575
3576static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003577zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003578 void *data)
3579{
3580 do_zoom(seat, time, key, 0, 0);
3581}
3582
3583static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003584terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003585 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003586{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003587 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003588
Daniel Stone325fc2d2012-05-30 16:31:58 +01003589 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003590}
3591
3592static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003593lower_fullscreen_layer(struct desktop_shell *shell);
3594
3595struct alt_tab {
3596 struct desktop_shell *shell;
3597 struct weston_keyboard_grab grab;
3598
3599 struct wl_list *current;
3600
3601 struct wl_list preview_list;
3602};
3603
3604struct alt_tab_preview {
3605 struct alt_tab *alt_tab;
3606
3607 struct weston_view *view;
3608 struct weston_transform transform;
3609
3610 struct wl_listener listener;
3611
3612 struct wl_list link;
3613};
3614
3615static void
3616alt_tab_next(struct alt_tab *alt_tab)
3617{
3618 alt_tab->current = alt_tab->current->next;
3619
3620 /* Make sure we're not pointing to the list header e.g. after
3621 * cycling through the whole list. */
3622 if (alt_tab->current->next == alt_tab->preview_list.next)
3623 alt_tab->current = alt_tab->current->next;
3624}
3625
3626static void
3627alt_tab_destroy(struct alt_tab *alt_tab)
3628{
3629 struct alt_tab_preview *preview, *next;
3630 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3631
3632 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3633 preview = wl_container_of(alt_tab->current, preview, link);
3634
3635 activate(alt_tab->shell, preview->view->surface,
3636 (struct weston_seat *) keyboard->seat);
3637 }
3638
3639 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3640 wl_list_remove(&preview->link);
3641 wl_list_remove(&preview->listener.link);
3642 weston_view_destroy(preview->view);
3643 free(preview);
3644 }
3645
3646 weston_keyboard_end_grab(keyboard);
3647 if (keyboard->input_method_resource)
3648 keyboard->grab = &keyboard->input_method_grab;
3649
3650 free(alt_tab);
3651}
3652
3653static void
3654alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3655{
3656 struct alt_tab_preview *preview =
3657 container_of(listener, struct alt_tab_preview, listener);
3658 struct alt_tab *alt_tab = preview->alt_tab;
3659 int advance = 0;
3660
3661 /* If the preview that we're removing is the currently selected one,
3662 * we want to move to the next one. So we move to ->prev and then
3663 * call _next() after removing the preview. */
3664 if (alt_tab->current == &preview->link) {
3665 alt_tab->current = alt_tab->current->prev;
3666 advance = 1;
3667 }
3668
3669 wl_list_remove(&preview->listener.link);
3670 wl_list_remove(&preview->link);
3671
3672 free(preview);
3673
3674 if (advance)
3675 alt_tab_next(alt_tab);
3676
3677 /* If the last preview goes away, stop the alt-tab */
3678 if (wl_list_empty(alt_tab->current))
3679 alt_tab_destroy(alt_tab);
3680}
3681
3682static void
3683alt_tab_key(struct weston_keyboard_grab *grab,
3684 uint32_t time, uint32_t key, uint32_t state_w)
3685{
3686 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3687 enum wl_keyboard_key_state state = state_w;
3688
3689 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3690 alt_tab_next(alt_tab);
3691}
3692
3693static void
3694alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3695 uint32_t mods_depressed, uint32_t mods_latched,
3696 uint32_t mods_locked, uint32_t group)
3697{
3698 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3699 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3700
3701 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3702 alt_tab_destroy(alt_tab);
3703}
3704
3705static void
3706alt_tab_cancel(struct weston_keyboard_grab *grab)
3707{
3708 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3709
3710 alt_tab_destroy(alt_tab);
3711}
3712
3713static const struct weston_keyboard_grab_interface alt_tab_grab = {
3714 alt_tab_key,
3715 alt_tab_modifier,
3716 alt_tab_cancel,
3717};
3718
3719static int
3720view_for_alt_tab(struct weston_view *view)
3721{
3722 if (!get_shell_surface(view->surface))
3723 return 0;
3724
3725 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3726 return 0;
3727
3728 if (view != get_default_view(view->surface))
3729 return 0;
3730
3731 return 1;
3732}
3733
3734static void
3735alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3736 void *data)
3737{
3738 struct alt_tab *alt_tab;
3739 struct desktop_shell *shell = data;
3740 struct weston_output *output = get_default_output(shell->compositor);
3741 struct workspace *ws;
3742 struct weston_view *view;
3743 int num_surfaces = 0;
3744 int x, y, side, margin;
3745
3746 wl_list_for_each(view, &shell->compositor->view_list, link) {
3747
3748 if (view_for_alt_tab(view))
3749 num_surfaces++;
3750 }
3751
3752 if (!num_surfaces)
3753 return;
3754
3755 alt_tab = malloc(sizeof *alt_tab);
3756 if (!alt_tab)
3757 return;
3758
3759 alt_tab->shell = shell;
3760 wl_list_init(&alt_tab->preview_list);
3761 alt_tab->current = &alt_tab->preview_list;
3762
3763 restore_all_output_modes(shell->compositor);
3764 lower_fullscreen_layer(alt_tab->shell);
3765
3766 alt_tab->grab.interface = &alt_tab_grab;
3767 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3768 weston_keyboard_set_focus(seat->keyboard, NULL);
3769
3770 ws = get_current_workspace(shell);
3771
3772 /* FIXME: add some visual candy e.g. prelight the selected view
3773 * and/or add a black surrounding background à la gnome-shell */
3774
3775 /* Determine the size for each preview */
3776 side = output->width / num_surfaces;
3777 if (side > 200)
3778 side = 200;
3779 margin = side / 4;
3780 side -= margin;
3781
3782 x = margin;
3783 y = (output->height - side) / 2;
3784
3785 /* Create a view for each surface */
3786 wl_list_for_each(view, &shell->compositor->view_list, link) {
3787 struct alt_tab_preview *preview;
3788 struct weston_view *v;
3789 float scale;
3790
3791 if (!view_for_alt_tab(view))
3792 continue;
3793
3794 preview = malloc(sizeof *preview);
3795 if (!preview) {
3796 alt_tab_destroy(alt_tab);
3797 return;
3798 }
3799
3800 preview->alt_tab = alt_tab;
3801
3802 preview->view = v = weston_view_create(view->surface);
3803 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003804
3805 wl_list_remove(&v->layer_link);
3806 wl_list_insert(&ws->layer.view_list, &v->layer_link);
3807 weston_view_damage_below(v);
3808 weston_surface_damage(v->surface);
3809
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003810 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003811
3812 preview->listener.notify = alt_tab_handle_surface_destroy;
3813 wl_signal_add(&v->destroy_signal, &preview->listener);
3814
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003815 if (view->surface->width > view->surface->height)
3816 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003817 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003818 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003819
3820 wl_list_insert(&v->geometry.transformation_list,
3821 &preview->transform.link);
3822 weston_matrix_init(&preview->transform.matrix);
3823 weston_matrix_scale(&preview->transform.matrix,
3824 scale, scale, 1.0f);
3825
3826 weston_view_geometry_dirty(v);
3827 weston_compositor_schedule_repaint(v->surface->compositor);
3828
3829 /* Insert at the end of the list */
3830 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3831
3832 x += side + margin;
3833 }
3834
3835 /* Start at the second preview so a simple <alt>tab changes window.
3836 * We set `current' to the first preview and not the second because
3837 * we're going to receive a key press callback for the initial
3838 * <alt>tab which will make `current' point to the second element. */
3839 alt_tab_next(alt_tab);
3840}
3841
3842static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003843rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3844 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003845{
3846 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003847 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003848 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003849 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003850 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003851
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003852 weston_pointer_move(pointer, x, y);
3853
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003854 if (!shsurf)
3855 return;
3856
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003857 cx = 0.5f * shsurf->surface->width;
3858 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003859
Daniel Stone37816df2012-05-16 18:45:18 +01003860 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3861 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003862 r = sqrtf(dx * dx + dy * dy);
3863
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003864 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003865 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003866
3867 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003868 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003869 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003870
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003871 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003872 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003873
3874 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003875 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003876 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003877 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003878 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003879
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003880 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003881 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003882 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003883 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003884 wl_list_init(&shsurf->rotation.transform.link);
3885 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003886 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003887 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003888
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003889 /* We need to adjust the position of the surface
3890 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003891 cposx = shsurf->view->geometry.x + cx;
3892 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003893 dposx = rotate->center.x - cposx;
3894 dposy = rotate->center.y - cposy;
3895 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003896 weston_view_set_position(shsurf->view,
3897 shsurf->view->geometry.x + dposx,
3898 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003899 }
3900
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003901 /* Repaint implies weston_surface_update_transform(), which
3902 * lazily applies the damage due to rotation update.
3903 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003904 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003905}
3906
3907static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003908rotate_grab_button(struct weston_pointer_grab *grab,
3909 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003910{
3911 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003912 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003913 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003914 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003915 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003916
Daniel Stone4dbadb12012-05-30 16:31:51 +01003917 if (pointer->button_count == 0 &&
3918 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003919 if (shsurf)
3920 weston_matrix_multiply(&shsurf->rotation.rotation,
3921 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003922 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003923 free(rotate);
3924 }
3925}
3926
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003927static void
3928rotate_grab_cancel(struct weston_pointer_grab *grab)
3929{
3930 struct rotate_grab *rotate =
3931 container_of(grab, struct rotate_grab, base.grab);
3932
3933 shell_grab_end(&rotate->base);
3934 free(rotate);
3935}
3936
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003937static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003938 noop_grab_focus,
3939 rotate_grab_motion,
3940 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003941 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003942};
3943
3944static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003945surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003946{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003947 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003948 float dx, dy;
3949 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003950
Pekka Paalanen460099f2012-01-20 16:48:25 +02003951 rotate = malloc(sizeof *rotate);
3952 if (!rotate)
3953 return;
3954
Jason Ekstranda7af7042013-10-12 22:38:11 -05003955 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003956 surface->surface->width * 0.5f,
3957 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003958 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003959
Daniel Stone37816df2012-05-16 18:45:18 +01003960 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3961 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003962 r = sqrtf(dx * dx + dy * dy);
3963 if (r > 20.0f) {
3964 struct weston_matrix inverse;
3965
3966 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003967 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003968 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003969
3970 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003971 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003972 } else {
3973 weston_matrix_init(&surface->rotation.rotation);
3974 weston_matrix_init(&rotate->rotation);
3975 }
3976
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003977 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3978 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003979}
3980
3981static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003982rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003983 void *data)
3984{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003985 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003986 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003987 struct shell_surface *surface;
3988
Pekka Paalanen01388e22013-04-25 13:57:44 +03003989 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003990 if (base_surface == NULL)
3991 return;
3992
3993 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003994 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3995 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003996 return;
3997
3998 surface_rotate(surface, seat);
3999}
4000
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004001/* Move all fullscreen layers down to the current workspace in a non-reversible
4002 * manner. This should be used when implementing shell-wide overlays, such as
4003 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004004static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004005lower_fullscreen_layer(struct desktop_shell *shell)
4006{
4007 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004008 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004009
4010 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004011 wl_list_for_each_reverse_safe(view, prev,
4012 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004013 layer_link) {
4014 wl_list_remove(&view->layer_link);
4015 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4016 weston_view_damage_below(view);
4017 weston_surface_damage(view->surface);
4018 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004019}
4020
4021static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004022activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004023 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004024{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004025 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004026 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004027 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004028 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004029
Pekka Paalanen01388e22013-04-25 13:57:44 +03004030 main_surface = weston_surface_get_main_surface(es);
4031
Daniel Stone37816df2012-05-16 18:45:18 +01004032 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004033
Jonas Ådahl8538b222012-08-29 22:13:03 +02004034 state = ensure_focus_state(shell, seat);
4035 if (state == NULL)
4036 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004037
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004038 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004039 state->keyboard_focus = es;
4040 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004041 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004042
Pekka Paalanen01388e22013-04-25 13:57:44 +03004043 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08004044 case SHELL_SURFACE_FULLSCREEN:
4045 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03004046 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004047 return;
Philip Withnall0f640e22013-11-25 18:01:31 +00004048 case SHELL_SURFACE_TOPLEVEL:
4049 case SHELL_SURFACE_TRANSIENT:
4050 case SHELL_SURFACE_MAXIMIZED:
4051 case SHELL_SURFACE_POPUP:
4052 case SHELL_SURFACE_XWAYLAND:
4053 case SHELL_SURFACE_NONE:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004054 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004055 restore_all_output_modes(shell->compositor);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004056 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004057 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004058
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004059 if (shell->focus_animation_type != ANIMATION_NONE) {
4060 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004061 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004062 }
4063
4064 /* Update the surface’s layer. This brings it to the top of the stacking
4065 * order as appropriate. */
4066 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004067}
4068
Alex Wu21858432012-04-01 20:13:08 +08004069/* no-op func for checking black surface */
4070static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004071black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004072{
4073}
4074
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004075static bool
Alex Wu21858432012-04-01 20:13:08 +08004076is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4077{
4078 if (es->configure == black_surface_configure) {
4079 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004080 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004081 return true;
4082 }
4083 return false;
4084}
4085
Kristian Høgsberg75840622011-09-06 13:48:16 -04004086static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004087activate_binding(struct weston_seat *seat,
4088 struct desktop_shell *shell,
4089 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004090{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004091 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004092
Alex Wu9c35e6b2012-03-05 14:13:13 +08004093 if (!focus)
4094 return;
4095
Pekka Paalanen01388e22013-04-25 13:57:44 +03004096 if (is_black_surface(focus, &main_surface))
4097 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004098
Pekka Paalanen01388e22013-04-25 13:57:44 +03004099 main_surface = weston_surface_get_main_surface(focus);
4100 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004101 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004102
Neil Robertsa28c6932013-10-03 16:43:04 +01004103 activate(shell, focus, seat);
4104}
4105
4106static void
4107click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4108 void *data)
4109{
4110 if (seat->pointer->grab != &seat->pointer->default_grab)
4111 return;
4112
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004113 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004114}
4115
4116static void
4117touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4118{
4119 if (seat->touch->grab != &seat->touch->default_grab)
4120 return;
4121
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004122 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004123}
4124
4125static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004126lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004127{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004128 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004129
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004130 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004131 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004132 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004133 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004134
4135 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004136
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004137 /* Hide all surfaces by removing the fullscreen, panel and
4138 * toplevel layers. This way nothing else can show or receive
4139 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004140
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004141 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004142 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004143 if (shell->showing_input_panels)
4144 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004145 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004146 wl_list_insert(&shell->compositor->cursor_layer.link,
4147 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004148
Pekka Paalanen77346a62011-11-30 16:26:35 +02004149 launch_screensaver(shell);
4150
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004151 /* TODO: disable bindings that should not work while locked. */
4152
4153 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004154}
4155
4156static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004157unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004158{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004159 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004160 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004161 return;
4162 }
4163
4164 /* If desktop-shell client has gone away, unlock immediately. */
4165 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004166 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004167 return;
4168 }
4169
4170 if (shell->prepare_event_sent)
4171 return;
4172
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004173 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004174 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004175}
4176
4177static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004178shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004179{
4180 struct desktop_shell *shell = data;
4181
4182 shell->fade.animation = NULL;
4183
4184 switch (shell->fade.type) {
4185 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004186 weston_surface_destroy(shell->fade.view->surface);
4187 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004188 break;
4189 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004190 lock(shell);
4191 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004192 default:
4193 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004194 }
4195}
4196
Jason Ekstranda7af7042013-10-12 22:38:11 -05004197static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004198shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004199{
4200 struct weston_compositor *compositor = shell->compositor;
4201 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004202 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004203
4204 surface = weston_surface_create(compositor);
4205 if (!surface)
4206 return NULL;
4207
Jason Ekstranda7af7042013-10-12 22:38:11 -05004208 view = weston_view_create(surface);
4209 if (!view) {
4210 weston_surface_destroy(surface);
4211 return NULL;
4212 }
4213
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004214 surface->width = 8192;
4215 surface->height = 8192;
4216 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004217 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004218 wl_list_insert(&compositor->fade_layer.view_list,
4219 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004220 pixman_region32_init(&surface->input);
4221
Jason Ekstranda7af7042013-10-12 22:38:11 -05004222 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004223}
4224
4225static void
4226shell_fade(struct desktop_shell *shell, enum fade_type type)
4227{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004228 float tint;
4229
4230 switch (type) {
4231 case FADE_IN:
4232 tint = 0.0;
4233 break;
4234 case FADE_OUT:
4235 tint = 1.0;
4236 break;
4237 default:
4238 weston_log("shell: invalid fade type\n");
4239 return;
4240 }
4241
4242 shell->fade.type = type;
4243
Jason Ekstranda7af7042013-10-12 22:38:11 -05004244 if (shell->fade.view == NULL) {
4245 shell->fade.view = shell_fade_create_surface(shell);
4246 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004247 return;
4248
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249 shell->fade.view->alpha = 1.0 - tint;
4250 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004251 }
4252
4253 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004254 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004255 else
4256 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004257 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004258 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004259 shell_fade_done, shell);
4260}
4261
4262static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004263do_shell_fade_startup(void *data)
4264{
4265 struct desktop_shell *shell = data;
4266
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004267 if (shell->startup_animation_type == ANIMATION_FADE)
4268 shell_fade(shell, FADE_IN);
4269 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004270 weston_surface_destroy(shell->fade.view->surface);
4271 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004272 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004273}
4274
4275static void
4276shell_fade_startup(struct desktop_shell *shell)
4277{
4278 struct wl_event_loop *loop;
4279
4280 if (!shell->fade.startup_timer)
4281 return;
4282
4283 wl_event_source_remove(shell->fade.startup_timer);
4284 shell->fade.startup_timer = NULL;
4285
4286 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4287 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4288}
4289
4290static int
4291fade_startup_timeout(void *data)
4292{
4293 struct desktop_shell *shell = data;
4294
4295 shell_fade_startup(shell);
4296 return 0;
4297}
4298
4299static void
4300shell_fade_init(struct desktop_shell *shell)
4301{
4302 /* Make compositor output all black, and wait for the desktop-shell
4303 * client to signal it is ready, then fade in. The timer triggers a
4304 * fade-in, in case the desktop-shell client takes too long.
4305 */
4306
4307 struct wl_event_loop *loop;
4308
Jason Ekstranda7af7042013-10-12 22:38:11 -05004309 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004310 weston_log("%s: warning: fade surface already exists\n",
4311 __func__);
4312 return;
4313 }
4314
Jason Ekstranda7af7042013-10-12 22:38:11 -05004315 shell->fade.view = shell_fade_create_surface(shell);
4316 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004317 return;
4318
Jason Ekstranda7af7042013-10-12 22:38:11 -05004319 weston_view_update_transform(shell->fade.view);
4320 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004321
4322 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4323 shell->fade.startup_timer =
4324 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4325 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4326}
4327
4328static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004329idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004330{
4331 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004332 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004333
4334 shell_fade(shell, FADE_OUT);
4335 /* lock() is called from shell_fade_done() */
4336}
4337
4338static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004339wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004340{
4341 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004342 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004343
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004344 unlock(shell);
4345}
4346
4347static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004348show_input_panels(struct wl_listener *listener, void *data)
4349{
4350 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004351 container_of(listener, struct desktop_shell,
4352 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004353 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004354
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004355 shell->text_input.surface = (struct weston_surface*)data;
4356
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004357 if (shell->showing_input_panels)
4358 return;
4359
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004360 shell->showing_input_panels = true;
4361
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004362 if (!shell->locked)
4363 wl_list_insert(&shell->panel_layer.link,
4364 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004365
Jason Ekstranda7af7042013-10-12 22:38:11 -05004366 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004367 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004368 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004369 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004370 wl_list_insert(&shell->input_panel_layer.view_list,
4371 &ipsurf->view->layer_link);
4372 weston_view_geometry_dirty(ipsurf->view);
4373 weston_view_update_transform(ipsurf->view);
4374 weston_surface_damage(ipsurf->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004375 weston_slide_run(ipsurf->view, ipsurf->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004377 }
4378}
4379
4380static void
4381hide_input_panels(struct wl_listener *listener, void *data)
4382{
4383 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004384 container_of(listener, struct desktop_shell,
4385 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004386 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004387
Jan Arne Petersen61381972013-01-31 15:52:21 +01004388 if (!shell->showing_input_panels)
4389 return;
4390
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004391 shell->showing_input_panels = false;
4392
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004393 if (!shell->locked)
4394 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004395
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 wl_list_for_each_safe(view, next,
4397 &shell->input_panel_layer.view_list, layer_link)
4398 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004399}
4400
4401static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004402update_input_panels(struct wl_listener *listener, void *data)
4403{
4404 struct desktop_shell *shell =
4405 container_of(listener, struct desktop_shell,
4406 update_input_panel_listener);
4407
4408 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4409}
4410
4411static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004412center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004413{
Giulio Camuffob8366642013-04-25 13:57:46 +03004414 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004415 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004416
Jason Ekstranda7af7042013-10-12 22:38:11 -05004417 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004418
4419 x = output->x + (output->width - width) / 2 - surf_x / 2;
4420 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004421
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004422 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004423}
4424
4425static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004426weston_view_set_initial_position(struct weston_view *view,
4427 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004428{
4429 struct weston_compositor *compositor = shell->compositor;
4430 int ix = 0, iy = 0;
4431 int range_x, range_y;
4432 int dx, dy, x, y, panel_height;
4433 struct weston_output *output, *target_output = NULL;
4434 struct weston_seat *seat;
4435
4436 /* As a heuristic place the new window on the same output as the
4437 * pointer. Falling back to the output containing 0, 0.
4438 *
4439 * TODO: Do something clever for touch too?
4440 */
4441 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004442 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004443 ix = wl_fixed_to_int(seat->pointer->x);
4444 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004445 break;
4446 }
4447 }
4448
4449 wl_list_for_each(output, &compositor->output_list, link) {
4450 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4451 target_output = output;
4452 break;
4453 }
4454 }
4455
4456 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004457 weston_view_set_position(view, 10 + random() % 400,
4458 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004459 return;
4460 }
4461
4462 /* Valid range within output where the surface will still be onscreen.
4463 * If this is negative it means that the surface is bigger than
4464 * output.
4465 */
4466 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004467 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004468 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004469 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004470
Rob Bradford4cb88c72012-08-13 15:18:44 +01004471 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004472 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004473 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004474 dx = 0;
4475
4476 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004477 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004478 else
4479 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004480
4481 x = target_output->x + dx;
4482 y = target_output->y + dy;
4483
Jason Ekstranda7af7042013-10-12 22:38:11 -05004484 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004485}
4486
4487static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004488map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004489 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004490{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004491 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004492 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004493 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004494 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004495
Pekka Paalanen77346a62011-11-30 16:26:35 +02004496 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004497 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004498 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004499 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004500 break;
Alex Wu4539b082012-03-01 12:57:46 +08004501 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004502 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004503 shell_map_fullscreen(shsurf);
4504 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004505 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004506 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004507 panel_height = get_output_panel_height(shell, shsurf->output);
4508 surface_subsurfaces_boundingbox(shsurf->surface,
4509 &surf_x, &surf_y, NULL, NULL);
4510 weston_view_set_position(shsurf->view,
4511 shsurf->output->x - surf_x,
4512 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004513 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004514 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004515 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004516 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004517 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004518 weston_view_set_position(shsurf->view,
4519 shsurf->view->geometry.x + sx,
4520 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004521 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004522 case SHELL_SURFACE_TRANSIENT:
4523 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004524 default:
4525 ;
4526 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004527
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004528 /* Surface stacking order, see also activate(). */
4529 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004530
Jason Ekstranda7af7042013-10-12 22:38:11 -05004531 if (shsurf->type != SHELL_SURFACE_NONE) {
4532 weston_view_update_transform(shsurf->view);
4533 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4534 shsurf->surface->output = shsurf->output;
4535 shsurf->view->output = shsurf->output;
4536 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004537 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004538
Jason Ekstranda7af7042013-10-12 22:38:11 -05004539 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004540 /* XXX: xwayland's using the same fields for transient type */
4541 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004542 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004543 if (shsurf->transient.flags ==
4544 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4545 break;
4546 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004547 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004548 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004549 if (!shell->locked) {
4550 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004551 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004552 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004553 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004554 case SHELL_SURFACE_POPUP:
4555 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004556 default:
4557 break;
4558 }
4559
Jason Ekstranda7af7042013-10-12 22:38:11 -05004560 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004561 {
4562 switch (shell->win_animation_type) {
4563 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004564 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004565 break;
4566 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004567 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004568 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004569 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004570 default:
4571 break;
4572 }
4573 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004574}
4575
4576static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004577configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004578 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004579{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004580 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4581 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004582 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004583 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004584
Pekka Paalanen77346a62011-11-30 16:26:35 +02004585 shsurf = get_shell_surface(surface);
4586 if (shsurf)
4587 surface_type = shsurf->type;
4588
Pekka Paalanen77346a62011-11-30 16:26:35 +02004589 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004590 case SHELL_SURFACE_FULLSCREEN:
4591 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004592 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004593 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004594 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004595 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4596 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004597 mx = shsurf->output->x - surf_x;
4598 my = shsurf->output->y +
4599 get_output_panel_height(shell,shsurf->output) - surf_y;
4600 weston_view_set_position(shsurf->view, mx, my);
Juan Zhao96879df2012-02-07 08:45:41 +08004601 break;
Alex Wu4539b082012-03-01 12:57:46 +08004602 case SHELL_SURFACE_TOPLEVEL:
Philip Withnall0f640e22013-11-25 18:01:31 +00004603 case SHELL_SURFACE_TRANSIENT:
4604 case SHELL_SURFACE_POPUP:
4605 case SHELL_SURFACE_XWAYLAND:
4606 case SHELL_SURFACE_NONE:
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004607 default:
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004608 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004609 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004610 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004611
Alex Wu4539b082012-03-01 12:57:46 +08004612 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004613 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004614 wl_list_for_each(view, &surface->views, surface_link)
4615 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004616
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004617 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004618 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004619 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004620}
4621
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004622static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004623shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004624{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004625 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004626 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004627
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004628 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004629
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004630 if (!weston_surface_is_mapped(es) &&
4631 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004632 remove_popup_grab(shsurf);
4633 }
4634
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004635 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004636 return;
4637
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004638 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004639 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004640 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004641 type_changed = 1;
4642 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004643
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004644 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004645 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004646 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004647 shsurf->last_width != es->width ||
4648 shsurf->last_height != es->height) {
4649 shsurf->last_width = es->width;
4650 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004651 float from_x, from_y;
4652 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004653
Jason Ekstranda7af7042013-10-12 22:38:11 -05004654 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4655 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004656 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004657 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004658 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004659 }
4660}
4661
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004662static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004663
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004664static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004665desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004666{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004667 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004668 struct desktop_shell *shell =
4669 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004670
4671 shell->child.process.pid = 0;
4672 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004673
4674 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4675 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004676 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004677 shell->child.deathstamp = time;
4678 shell->child.deathcount = 0;
4679 }
4680
4681 shell->child.deathcount++;
4682 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004683 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004684 return;
4685 }
4686
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004687 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004688 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004689 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004690}
4691
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004692static void
4693launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004694{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004695 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004696
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004697 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004698 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004699 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004700 desktop_shell_sigchld);
4701
4702 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004703 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004704}
4705
4706static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004707bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4708{
Tiago Vignattibe143262012-04-16 17:31:41 +03004709 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004710 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004711
Jason Ekstranda85118c2013-06-27 20:17:02 -05004712 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4713 if (resource)
4714 wl_resource_set_implementation(resource, &shell_implementation,
4715 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004716}
4717
Kristian Høgsberg75840622011-09-06 13:48:16 -04004718static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004719unbind_desktop_shell(struct wl_resource *resource)
4720{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004721 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004722
4723 if (shell->locked)
4724 resume_desktop(shell);
4725
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004726 shell->child.desktop_shell = NULL;
4727 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004728}
4729
4730static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004731bind_desktop_shell(struct wl_client *client,
4732 void *data, uint32_t version, uint32_t id)
4733{
Tiago Vignattibe143262012-04-16 17:31:41 +03004734 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004735 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004736
Jason Ekstranda85118c2013-06-27 20:17:02 -05004737 resource = wl_resource_create(client, &desktop_shell_interface,
4738 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004739
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004740 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004741 wl_resource_set_implementation(resource,
4742 &desktop_shell_implementation,
4743 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004744 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004745
4746 if (version < 2)
4747 shell_fade_startup(shell);
4748
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004749 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004750 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004751
4752 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4753 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004754 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004755}
4756
Pekka Paalanen6e168112011-11-24 11:34:05 +02004757static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004758screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004759{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004760 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004762
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004763 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004764 return;
4765
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004766 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004767 if (!shell->locked)
4768 return;
4769
Jason Ekstranda7af7042013-10-12 22:38:11 -05004770 view = container_of(surface->views.next, struct weston_view, surface_link);
4771 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004772
Jason Ekstranda7af7042013-10-12 22:38:11 -05004773 if (wl_list_empty(&view->layer_link)) {
4774 wl_list_insert(shell->lock_layer.view_list.prev,
4775 &view->layer_link);
4776 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004777 wl_event_source_timer_update(shell->screensaver.timer,
4778 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004779 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004780 }
4781}
4782
4783static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004784screensaver_set_surface(struct wl_client *client,
4785 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004786 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004787 struct wl_resource *output_resource)
4788{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004789 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004790 struct weston_surface *surface =
4791 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004792 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004793 struct weston_view *view, *next;
4794
4795 /* Make sure we only have one view */
4796 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4797 weston_view_destroy(view);
4798 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004799
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004800 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004801 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004802 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004803}
4804
4805static const struct screensaver_interface screensaver_implementation = {
4806 screensaver_set_surface
4807};
4808
4809static void
4810unbind_screensaver(struct wl_resource *resource)
4811{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004812 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004813
Pekka Paalanen77346a62011-11-30 16:26:35 +02004814 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004815}
4816
4817static void
4818bind_screensaver(struct wl_client *client,
4819 void *data, uint32_t version, uint32_t id)
4820{
Tiago Vignattibe143262012-04-16 17:31:41 +03004821 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004822 struct wl_resource *resource;
4823
Jason Ekstranda85118c2013-06-27 20:17:02 -05004824 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004825
Pekka Paalanen77346a62011-11-30 16:26:35 +02004826 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004827 wl_resource_set_implementation(resource,
4828 &screensaver_implementation,
4829 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004830 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004831 return;
4832 }
4833
4834 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4835 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004836 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004837}
4838
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004839static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004840input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004841{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004842 struct input_panel_surface *ip_surface = surface->configure_private;
4843 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004844 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004845 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004846
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004847 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004848 return;
4849
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004850 if (!weston_surface_is_mapped(surface)) {
4851 if (!shell->showing_input_panels)
4852 return;
4853
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004854 show_surface = 1;
4855 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004856
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004857 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004858
4859 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004860 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4861 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004862 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004863 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
4864 y = ip_surface->output->y + ip_surface->output->height - surface->height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004865 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004866
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004867 weston_view_set_position(ip_surface->view, x, y);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004868
4869 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004870 wl_list_insert(&shell->input_panel_layer.view_list,
4871 &ip_surface->view->layer_link);
4872 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004873 weston_surface_damage(surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004874 weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004875 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004876}
4877
4878static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004879destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004880{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004881 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4882
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004883 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004884 wl_list_remove(&input_panel_surface->link);
4885
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004886 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004887 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004888
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004889 free(input_panel_surface);
4890}
4891
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004892static struct input_panel_surface *
4893get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004894{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004895 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004896 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004897 } else {
4898 return NULL;
4899 }
4900}
4901
4902static void
4903input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4904{
4905 struct input_panel_surface *ipsurface = container_of(listener,
4906 struct input_panel_surface,
4907 surface_destroy_listener);
4908
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004909 if (ipsurface->resource) {
4910 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004911 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004912 destroy_input_panel_surface(ipsurface);
4913 }
4914}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004915
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004916static struct input_panel_surface *
4917create_input_panel_surface(struct desktop_shell *shell,
4918 struct weston_surface *surface)
4919{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004920 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004921
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004922 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4923 if (!input_panel_surface)
4924 return NULL;
4925
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004926 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004927 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004928
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004929 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004930
4931 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004932 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004933
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004934 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004935 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004936 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004937 &input_panel_surface->surface_destroy_listener);
4938
4939 wl_list_init(&input_panel_surface->link);
4940
4941 return input_panel_surface;
4942}
4943
4944static void
4945input_panel_surface_set_toplevel(struct wl_client *client,
4946 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004947 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004948 uint32_t position)
4949{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004950 struct input_panel_surface *input_panel_surface =
4951 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004952 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004953
4954 wl_list_insert(&shell->input_panel.surfaces,
4955 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004956
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004957 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004958 input_panel_surface->panel = 0;
4959}
4960
4961static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004962input_panel_surface_set_overlay_panel(struct wl_client *client,
4963 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004964{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004965 struct input_panel_surface *input_panel_surface =
4966 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004967 struct desktop_shell *shell = input_panel_surface->shell;
4968
4969 wl_list_insert(&shell->input_panel.surfaces,
4970 &input_panel_surface->link);
4971
4972 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004973}
4974
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004975static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004976 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004977 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004978};
4979
4980static void
4981destroy_input_panel_surface_resource(struct wl_resource *resource)
4982{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004983 struct input_panel_surface *ipsurf =
4984 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004985
4986 destroy_input_panel_surface(ipsurf);
4987}
4988
4989static void
4990input_panel_get_input_panel_surface(struct wl_client *client,
4991 struct wl_resource *resource,
4992 uint32_t id,
4993 struct wl_resource *surface_resource)
4994{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004995 struct weston_surface *surface =
4996 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004997 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004998 struct input_panel_surface *ipsurf;
4999
5000 if (get_input_panel_surface(surface)) {
5001 wl_resource_post_error(surface_resource,
5002 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005003 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005004 return;
5005 }
5006
5007 ipsurf = create_input_panel_surface(shell, surface);
5008 if (!ipsurf) {
5009 wl_resource_post_error(surface_resource,
5010 WL_DISPLAY_ERROR_INVALID_OBJECT,
5011 "surface->configure already set");
5012 return;
5013 }
5014
Jason Ekstranda85118c2013-06-27 20:17:02 -05005015 ipsurf->resource =
5016 wl_resource_create(client,
5017 &wl_input_panel_surface_interface, 1, id);
5018 wl_resource_set_implementation(ipsurf->resource,
5019 &input_panel_surface_implementation,
5020 ipsurf,
5021 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005022}
5023
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005024static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005025 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005026};
5027
5028static void
5029unbind_input_panel(struct wl_resource *resource)
5030{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005031 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005032
5033 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005034}
5035
5036static void
5037bind_input_panel(struct wl_client *client,
5038 void *data, uint32_t version, uint32_t id)
5039{
5040 struct desktop_shell *shell = data;
5041 struct wl_resource *resource;
5042
Jason Ekstranda85118c2013-06-27 20:17:02 -05005043 resource = wl_resource_create(client,
5044 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005045
5046 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005047 wl_resource_set_implementation(resource,
5048 &input_panel_implementation,
5049 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005050 shell->input_panel.binding = resource;
5051 return;
5052 }
5053
5054 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5055 "interface object already bound");
5056 wl_resource_destroy(resource);
5057}
5058
Kristian Høgsberg07045392012-02-19 18:52:44 -05005059struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005060 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005061 struct weston_surface *current;
5062 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005063 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005064};
5065
5066static void
5067switcher_next(struct switcher *switcher)
5068{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005069 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005070 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005071 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005072 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005073
Jason Ekstranda7af7042013-10-12 22:38:11 -05005074 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
5075 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005076 case SHELL_SURFACE_TOPLEVEL:
5077 case SHELL_SURFACE_FULLSCREEN:
5078 case SHELL_SURFACE_MAXIMIZED:
5079 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005080 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005081 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005082 next = view->surface;
5083 prev = view->surface;
5084 view->alpha = 0.25;
5085 weston_view_geometry_dirty(view);
5086 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005087 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005088 case SHELL_SURFACE_TRANSIENT:
5089 case SHELL_SURFACE_POPUP:
5090 case SHELL_SURFACE_XWAYLAND:
5091 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005092 default:
5093 break;
5094 }
Alex Wu1659daa2012-04-01 20:13:09 +08005095
Jason Ekstranda7af7042013-10-12 22:38:11 -05005096 if (is_black_surface(view->surface, NULL)) {
5097 view->alpha = 0.25;
5098 weston_view_geometry_dirty(view);
5099 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005100 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005101 }
5102
5103 if (next == NULL)
5104 next = first;
5105
Alex Wu07b26062012-03-12 16:06:01 +08005106 if (next == NULL)
5107 return;
5108
Kristian Høgsberg07045392012-02-19 18:52:44 -05005109 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005110 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005111
5112 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005113 wl_list_for_each(view, &next->views, surface_link)
5114 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005115
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005116 shsurf = get_shell_surface(switcher->current);
5117 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005118 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005119}
5120
5121static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005122switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005123{
5124 struct switcher *switcher =
5125 container_of(listener, struct switcher, listener);
5126
5127 switcher_next(switcher);
5128}
5129
5130static void
Daniel Stone351eb612012-05-31 15:27:47 -04005131switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005132{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005133 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005134 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005135 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005136
Jason Ekstranda7af7042013-10-12 22:38:11 -05005137 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005138 if (is_focus_view(view))
5139 continue;
5140
Jason Ekstranda7af7042013-10-12 22:38:11 -05005141 view->alpha = 1.0;
5142 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005143 }
5144
Alex Wu07b26062012-03-12 16:06:01 +08005145 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005146 activate(switcher->shell, switcher->current,
5147 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005148 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005149 weston_keyboard_end_grab(keyboard);
5150 if (keyboard->input_method_resource)
5151 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005152 free(switcher);
5153}
5154
5155static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005156switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005157 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005158{
5159 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005160 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005161
Daniel Stonec9785ea2012-05-30 16:31:52 +01005162 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005163 switcher_next(switcher);
5164}
5165
5166static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005167switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005168 uint32_t mods_depressed, uint32_t mods_latched,
5169 uint32_t mods_locked, uint32_t group)
5170{
5171 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005172 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005173
Daniel Stone351eb612012-05-31 15:27:47 -04005174 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5175 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005176}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005177
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005178static void
5179switcher_cancel(struct weston_keyboard_grab *grab)
5180{
5181 struct switcher *switcher = container_of(grab, struct switcher, grab);
5182
5183 switcher_destroy(switcher);
5184}
5185
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005186static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005187 switcher_key,
5188 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005189 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005190};
5191
5192static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005193switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005194 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005195{
Tiago Vignattibe143262012-04-16 17:31:41 +03005196 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005197 struct switcher *switcher;
5198
5199 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005200 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005201 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005202 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005203 wl_list_init(&switcher->listener.link);
5204
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005205 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005206 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005207 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005208 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5209 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005210 switcher_next(switcher);
5211}
5212
Daniel Stonedf8133b2013-11-19 11:37:14 +01005213struct exposay_surface {
5214 struct desktop_shell *shell;
5215 struct weston_surface *surface;
5216 struct weston_view *view;
5217 struct wl_list link;
5218
5219 int x;
5220 int y;
5221 int width;
5222 int height;
5223 double scale;
5224
5225 int row;
5226 int column;
5227
5228 /* The animations only apply a transformation for their own lifetime,
5229 * and don't have an option to indefinitely maintain the
5230 * transformation in a steady state - so, we apply our own once the
5231 * animation has finished. */
5232 struct weston_transform transform;
5233};
5234
5235static void exposay_set_state(struct desktop_shell *shell,
5236 enum exposay_target_state state,
5237 struct weston_seat *seat);
5238static void exposay_check_state(struct desktop_shell *shell);
5239
5240static void
5241exposay_in_flight_inc(struct desktop_shell *shell)
5242{
5243 shell->exposay.in_flight++;
5244}
5245
5246static void
5247exposay_in_flight_dec(struct desktop_shell *shell)
5248{
5249 if (--shell->exposay.in_flight > 0)
5250 return;
5251
5252 exposay_check_state(shell);
5253}
5254
5255static void
5256exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5257{
5258 struct exposay_surface *esurface = data;
5259
5260 wl_list_insert(&esurface->view->geometry.transformation_list,
5261 &esurface->transform.link);
5262 weston_matrix_init(&esurface->transform.matrix);
5263 weston_matrix_scale(&esurface->transform.matrix,
5264 esurface->scale, esurface->scale, 1.0f);
5265 weston_matrix_translate(&esurface->transform.matrix,
5266 esurface->x - esurface->view->geometry.x,
5267 esurface->y - esurface->view->geometry.y,
5268 0);
5269
5270 weston_view_geometry_dirty(esurface->view);
5271 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5272
5273 exposay_in_flight_dec(esurface->shell);
5274}
5275
5276static void
5277exposay_animate_in(struct exposay_surface *esurface)
5278{
5279 exposay_in_flight_inc(esurface->shell);
5280
5281 weston_move_scale_run(esurface->view,
5282 esurface->x - esurface->view->geometry.x,
5283 esurface->y - esurface->view->geometry.y,
5284 1.0, esurface->scale, 0,
5285 exposay_animate_in_done, esurface);
5286}
5287
5288static void
5289exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5290{
5291 struct exposay_surface *esurface = data;
5292 struct desktop_shell *shell = esurface->shell;
5293
5294 wl_list_remove(&esurface->link);
5295 free(esurface);
5296
5297 exposay_in_flight_dec(shell);
5298}
5299
5300static void
5301exposay_animate_out(struct exposay_surface *esurface)
5302{
5303 exposay_in_flight_inc(esurface->shell);
5304
5305 /* Remove the static transformation set up by
5306 * exposay_transform_in_done(). */
5307 wl_list_remove(&esurface->transform.link);
5308 weston_view_geometry_dirty(esurface->view);
5309
5310 weston_move_scale_run(esurface->view,
5311 esurface->x - esurface->view->geometry.x,
5312 esurface->y - esurface->view->geometry.y,
5313 1.0, esurface->scale, 1,
5314 exposay_animate_out_done, esurface);
5315}
5316
5317static void
5318exposay_highlight_surface(struct desktop_shell *shell,
5319 struct exposay_surface *esurface)
5320{
5321 struct weston_view *view = NULL;
5322
5323 if (esurface) {
5324 shell->exposay.row_current = esurface->row;
5325 shell->exposay.column_current = esurface->column;
5326 view = esurface->view;
5327 }
5328
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005329 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005330 shell->exposay.focus_current = view;
5331}
5332
5333static int
5334exposay_is_animating(struct desktop_shell *shell)
5335{
5336 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5337 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5338 return 0;
5339
5340 return (shell->exposay.in_flight > 0);
5341}
5342
5343static void
5344exposay_pick(struct desktop_shell *shell, int x, int y)
5345{
5346 struct exposay_surface *esurface;
5347
5348 if (exposay_is_animating(shell))
5349 return;
5350
5351 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5352 if (x < esurface->x || x > esurface->x + esurface->width)
5353 continue;
5354 if (y < esurface->y || y > esurface->y + esurface->height)
5355 continue;
5356
5357 exposay_highlight_surface(shell, esurface);
5358 return;
5359 }
5360}
5361
5362/* Pretty lame layout for now; just tries to make a square. Should take
5363 * aspect ratio into account really. Also needs to be notified of surface
5364 * addition and removal and adjust layout/animate accordingly. */
5365static enum exposay_layout_state
5366exposay_layout(struct desktop_shell *shell)
5367{
5368 struct workspace *workspace = shell->exposay.workspace;
5369 struct weston_compositor *compositor = shell->compositor;
5370 struct weston_output *output = get_default_output(compositor);
5371 struct weston_view *view;
5372 struct exposay_surface *esurface;
5373 int w, h;
5374 int i;
5375 int last_row_removed = 0;
5376
5377 wl_list_init(&shell->exposay.surface_list);
5378
5379 shell->exposay.num_surfaces = 0;
5380 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5381 if (!get_shell_surface(view->surface))
5382 continue;
5383 shell->exposay.num_surfaces++;
5384 }
5385
5386 if (shell->exposay.num_surfaces == 0) {
5387 shell->exposay.grid_size = 0;
5388 shell->exposay.hpadding_outer = 0;
5389 shell->exposay.vpadding_outer = 0;
5390 shell->exposay.padding_inner = 0;
5391 shell->exposay.surface_size = 0;
5392 return EXPOSAY_LAYOUT_OVERVIEW;
5393 }
5394
5395 /* Lay the grid out as square as possible, losing surfaces from the
5396 * bottom row if required. Start with fixed padding of a 10% margin
5397 * around the outside and 80px internal padding between surfaces, and
5398 * maximise the area made available to surfaces after this, but only
5399 * to a maximum of 1/3rd the total output size.
5400 *
5401 * If we can't make a square grid, add one extra row at the bottom
5402 * which will have a smaller number of columns.
5403 *
5404 * XXX: Surely there has to be a better way to express this maths,
5405 * right?!
5406 */
5407 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5408 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5409 shell->exposay.grid_size++;
5410 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5411
5412 shell->exposay.hpadding_outer = (output->width / 10);
5413 shell->exposay.vpadding_outer = (output->height / 10);
5414 shell->exposay.padding_inner = 80;
5415
5416 w = output->width - (shell->exposay.hpadding_outer * 2);
5417 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5418 w /= shell->exposay.grid_size;
5419
5420 h = output->height - (shell->exposay.vpadding_outer * 2);
5421 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5422 h /= shell->exposay.grid_size;
5423
5424 shell->exposay.surface_size = (w < h) ? w : h;
5425 if (shell->exposay.surface_size > (output->width / 2))
5426 shell->exposay.surface_size = output->width / 2;
5427 if (shell->exposay.surface_size > (output->height / 2))
5428 shell->exposay.surface_size = output->height / 2;
5429
5430 i = 0;
5431 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5432 int pad;
5433
5434 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5435
5436 if (!get_shell_surface(view->surface))
5437 continue;
5438
5439 esurface = malloc(sizeof(*esurface));
5440 if (!esurface) {
5441 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5442 shell->exposay.seat);
5443 break;
5444 }
5445
5446 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5447 esurface->shell = shell;
5448 esurface->view = view;
5449
5450 esurface->row = i / shell->exposay.grid_size;
5451 esurface->column = i % shell->exposay.grid_size;
5452
5453 esurface->x = shell->exposay.hpadding_outer;
5454 esurface->x += pad * esurface->column;
5455 esurface->y = shell->exposay.vpadding_outer;
5456 esurface->y += pad * esurface->row;
5457
5458 if (esurface->row == shell->exposay.grid_size - 1)
5459 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5460
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005461 if (view->surface->width > view->surface->height)
5462 esurface->scale = shell->exposay.surface_size / (float) view->surface->width;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005463 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005464 esurface->scale = shell->exposay.surface_size / (float) view->surface->height;
5465 esurface->width = view->surface->width * esurface->scale;
5466 esurface->height = view->surface->height * esurface->scale;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005467
5468 if (shell->exposay.focus_current == esurface->view)
5469 exposay_highlight_surface(shell, esurface);
5470
5471 exposay_animate_in(esurface);
5472
5473 i++;
5474 }
5475
5476 weston_compositor_schedule_repaint(shell->compositor);
5477
5478 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5479}
5480
5481static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005482exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5483 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005484{
5485 struct desktop_shell *shell =
5486 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5487
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005488 weston_pointer_move(grab->pointer, x, y);
5489
Daniel Stonedf8133b2013-11-19 11:37:14 +01005490 exposay_pick(shell,
5491 wl_fixed_to_int(grab->pointer->x),
5492 wl_fixed_to_int(grab->pointer->y));
5493}
5494
5495static void
5496exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5497 uint32_t state_w)
5498{
5499 struct desktop_shell *shell =
5500 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5501 struct weston_seat *seat = grab->pointer->seat;
5502 enum wl_pointer_button_state state = state_w;
5503
5504 if (button != BTN_LEFT)
5505 return;
5506
5507 /* Store the surface we clicked on, and don't do anything if we end up
5508 * releasing on a different surface. */
5509 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5510 shell->exposay.clicked = shell->exposay.focus_current;
5511 return;
5512 }
5513
5514 if (shell->exposay.focus_current == shell->exposay.clicked)
5515 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5516 else
5517 shell->exposay.clicked = NULL;
5518}
5519
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005520static void
5521exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
5522{
5523 struct desktop_shell *shell =
5524 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5525
5526 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5527}
5528
Daniel Stonedf8133b2013-11-19 11:37:14 +01005529static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5530 noop_grab_focus,
5531 exposay_motion,
5532 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005533 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005534};
5535
5536static int
5537exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5538{
5539 struct exposay_surface *esurface;
5540
5541 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5542 if (esurface->row != row || esurface->column != column)
5543 continue;
5544
5545 exposay_highlight_surface(shell, esurface);
5546 return 1;
5547 }
5548
5549 return 0;
5550}
5551
5552static void
5553exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5554 uint32_t state_w)
5555{
5556 struct weston_seat *seat = grab->keyboard->seat;
5557 struct desktop_shell *shell =
5558 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5559 enum wl_keyboard_key_state state = state_w;
5560
5561 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5562 return;
5563
5564 switch (key) {
5565 case KEY_ESC:
5566 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5567 break;
5568 case KEY_ENTER:
5569 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5570 break;
5571 case KEY_UP:
5572 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5573 shell->exposay.column_current);
5574 break;
5575 case KEY_DOWN:
5576 /* Special case for trying to move to the bottom row when it
5577 * has fewer items than all the others. */
5578 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5579 shell->exposay.column_current) &&
5580 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5581 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5582 (shell->exposay.num_surfaces %
5583 shell->exposay.grid_size) - 1);
5584 }
5585 break;
5586 case KEY_LEFT:
5587 exposay_maybe_move(shell, shell->exposay.row_current,
5588 shell->exposay.column_current - 1);
5589 break;
5590 case KEY_RIGHT:
5591 exposay_maybe_move(shell, shell->exposay.row_current,
5592 shell->exposay.column_current + 1);
5593 break;
5594 case KEY_TAB:
5595 /* Try to move right, then down (and to the leftmost column),
5596 * then if all else fails, to the top left. */
5597 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5598 shell->exposay.column_current + 1) &&
5599 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5600 exposay_maybe_move(shell, 0, 0);
5601 break;
5602 default:
5603 break;
5604 }
5605}
5606
5607static void
5608exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5609 uint32_t mods_depressed, uint32_t mods_latched,
5610 uint32_t mods_locked, uint32_t group)
5611{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005612 struct desktop_shell *shell =
5613 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5614 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5615
5616 /* We want to know when mod has been pressed and released.
5617 * FIXME: There is a problem here: if mod is pressed, then a key
5618 * is pressed and released, then mod is released, we will treat that
5619 * as if only mod had been pressed and released. */
5620 if (seat->modifier_state) {
5621 if (seat->modifier_state == shell->binding_modifier) {
5622 shell->exposay.mod_pressed = true;
5623 } else {
5624 shell->exposay.mod_invalid = true;
5625 }
5626 } else {
5627 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
5628 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5629
5630 shell->exposay.mod_invalid = false;
5631 shell->exposay.mod_pressed = false;
5632 }
5633
5634 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005635}
5636
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005637static void
5638exposay_cancel(struct weston_keyboard_grab *grab)
5639{
5640 struct desktop_shell *shell =
5641 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5642
5643 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5644}
5645
Daniel Stonedf8133b2013-11-19 11:37:14 +01005646static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5647 exposay_key,
5648 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005649 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005650};
5651
5652/**
5653 * Called when the transition from overview -> inactive has completed.
5654 */
5655static enum exposay_layout_state
5656exposay_set_inactive(struct desktop_shell *shell)
5657{
5658 struct weston_seat *seat = shell->exposay.seat;
5659
5660 weston_keyboard_end_grab(seat->keyboard);
5661 weston_pointer_end_grab(seat->pointer);
5662 if (seat->keyboard->input_method_resource)
5663 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5664
5665 return EXPOSAY_LAYOUT_INACTIVE;
5666}
5667
5668/**
5669 * Begins the transition from overview to inactive. */
5670static enum exposay_layout_state
5671exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5672{
5673 struct exposay_surface *esurface;
5674
5675 /* Call activate() before we start the animations to avoid
5676 * animating back the old state and then immediately transitioning
5677 * to the new. */
5678 if (switch_focus && shell->exposay.focus_current)
5679 activate(shell, shell->exposay.focus_current->surface,
5680 shell->exposay.seat);
5681 else if (shell->exposay.focus_prev)
5682 activate(shell, shell->exposay.focus_prev->surface,
5683 shell->exposay.seat);
5684
5685 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5686 exposay_animate_out(esurface);
5687 weston_compositor_schedule_repaint(shell->compositor);
5688
5689 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5690}
5691
5692static enum exposay_layout_state
5693exposay_transition_active(struct desktop_shell *shell)
5694{
5695 struct weston_seat *seat = shell->exposay.seat;
5696
5697 shell->exposay.workspace = get_current_workspace(shell);
5698 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5699 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5700 shell->exposay.clicked = NULL;
5701 wl_list_init(&shell->exposay.surface_list);
5702
5703 lower_fullscreen_layer(shell);
5704 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5705 weston_keyboard_start_grab(seat->keyboard,
5706 &shell->exposay.grab_kbd);
5707 weston_keyboard_set_focus(seat->keyboard, NULL);
5708
5709 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5710 weston_pointer_start_grab(seat->pointer,
5711 &shell->exposay.grab_ptr);
5712 weston_pointer_set_focus(seat->pointer, NULL,
5713 seat->pointer->x, seat->pointer->y);
5714
5715 return exposay_layout(shell);
5716}
5717
5718static void
5719exposay_check_state(struct desktop_shell *shell)
5720{
5721 enum exposay_layout_state state_new = shell->exposay.state_cur;
5722 int do_switch = 0;
5723
5724 /* Don't do anything whilst animations are running, just store up
5725 * target state changes and only act on them when the animations have
5726 * completed. */
5727 if (exposay_is_animating(shell))
5728 return;
5729
5730 switch (shell->exposay.state_target) {
5731 case EXPOSAY_TARGET_OVERVIEW:
5732 switch (shell->exposay.state_cur) {
5733 case EXPOSAY_LAYOUT_OVERVIEW:
5734 goto out;
5735 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5736 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5737 break;
5738 default:
5739 state_new = exposay_transition_active(shell);
5740 break;
5741 }
5742 break;
5743
5744 case EXPOSAY_TARGET_SWITCH:
5745 do_switch = 1; /* fallthrough */
5746 case EXPOSAY_TARGET_CANCEL:
5747 switch (shell->exposay.state_cur) {
5748 case EXPOSAY_LAYOUT_INACTIVE:
5749 goto out;
5750 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5751 state_new = exposay_set_inactive(shell);
5752 break;
5753 default:
5754 state_new = exposay_transition_inactive(shell, do_switch);
5755 break;
5756 }
5757
5758 break;
5759 }
5760
5761out:
5762 shell->exposay.state_cur = state_new;
5763}
5764
5765static void
5766exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5767 struct weston_seat *seat)
5768{
5769 shell->exposay.state_target = state;
5770 shell->exposay.seat = seat;
5771 exposay_check_state(shell);
5772}
5773
5774static void
5775exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5776 void *data)
5777{
5778 struct desktop_shell *shell = data;
5779
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005780 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005781}
5782
Pekka Paalanen3c647232011-12-22 13:43:43 +02005783static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005784backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005785 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005786{
5787 struct weston_compositor *compositor = data;
5788 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005789 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005790
5791 /* TODO: we're limiting to simple use cases, where we assume just
5792 * control on the primary display. We'd have to extend later if we
5793 * ever get support for setting backlights on random desktop LCD
5794 * panels though */
5795 output = get_default_output(compositor);
5796 if (!output)
5797 return;
5798
5799 if (!output->set_backlight)
5800 return;
5801
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005802 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5803 backlight_new = output->backlight_current - 25;
5804 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5805 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005806
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005807 if (backlight_new < 5)
5808 backlight_new = 5;
5809 if (backlight_new > 255)
5810 backlight_new = 255;
5811
5812 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005813 output->set_backlight(output, output->backlight_current);
5814}
5815
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005816struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005817 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005818 struct weston_seat *seat;
5819 uint32_t key[2];
5820 int key_released[2];
5821};
5822
5823static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005824debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005825 uint32_t key, uint32_t state)
5826{
5827 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005828 struct weston_compositor *ec = db->seat->compositor;
5829 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005830 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005831 uint32_t serial;
5832 int send = 0, terminate = 0;
5833 int check_binding = 1;
5834 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005835 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005836
5837 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5838 /* Do not run bindings on key releases */
5839 check_binding = 0;
5840
5841 for (i = 0; i < 2; i++)
5842 if (key == db->key[i])
5843 db->key_released[i] = 1;
5844
5845 if (db->key_released[0] && db->key_released[1]) {
5846 /* All key releases been swalled so end the grab */
5847 terminate = 1;
5848 } else if (key != db->key[0] && key != db->key[1]) {
5849 /* Should not swallow release of other keys */
5850 send = 1;
5851 }
5852 } else if (key == db->key[0] && !db->key_released[0]) {
5853 /* Do not check bindings for the first press of the binding
5854 * key. This allows it to be used as a debug shortcut.
5855 * We still need to swallow this event. */
5856 check_binding = 0;
5857 } else if (db->key[1]) {
5858 /* If we already ran a binding don't process another one since
5859 * we can't keep track of all the binding keys that were
5860 * pressed in order to swallow the release events. */
5861 send = 1;
5862 check_binding = 0;
5863 }
5864
5865 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005866 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5867 key, state)) {
5868 /* We ran a binding so swallow the press and keep the
5869 * grab to swallow the released too. */
5870 send = 0;
5871 terminate = 0;
5872 db->key[1] = key;
5873 } else {
5874 /* Terminate the grab since the key pressed is not a
5875 * debug binding key. */
5876 send = 1;
5877 terminate = 1;
5878 }
5879 }
5880
5881 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005882 serial = wl_display_next_serial(display);
5883 resource_list = &grab->keyboard->focus_resource_list;
5884 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005885 wl_keyboard_send_key(resource, serial, time, key, state);
5886 }
5887 }
5888
5889 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005890 weston_keyboard_end_grab(grab->keyboard);
5891 if (grab->keyboard->input_method_resource)
5892 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005893 free(db);
5894 }
5895}
5896
5897static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005898debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005899 uint32_t mods_depressed, uint32_t mods_latched,
5900 uint32_t mods_locked, uint32_t group)
5901{
5902 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005903 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005904
Neil Roberts96d790e2013-09-19 17:32:00 +01005905 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005906
Neil Roberts96d790e2013-09-19 17:32:00 +01005907 wl_resource_for_each(resource, resource_list) {
5908 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5909 mods_latched, mods_locked, group);
5910 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005911}
5912
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005913static void
5914debug_binding_cancel(struct weston_keyboard_grab *grab)
5915{
5916 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5917
5918 weston_keyboard_end_grab(grab->keyboard);
5919 free(db);
5920}
5921
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005922struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005923 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005924 debug_binding_modifiers,
5925 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005926};
5927
5928static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005929debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005930{
5931 struct debug_binding_grab *grab;
5932
5933 grab = calloc(1, sizeof *grab);
5934 if (!grab)
5935 return;
5936
5937 grab->seat = (struct weston_seat *) seat;
5938 grab->key[0] = key;
5939 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005940 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005941}
5942
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005943static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005944force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005945 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005946{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005947 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005948 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005949 struct desktop_shell *shell = data;
5950 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005951 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005952
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005953 focus_surface = seat->keyboard->focus;
5954 if (!focus_surface)
5955 return;
5956
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005957 wl_signal_emit(&compositor->kill_signal, focus_surface);
5958
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005959 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005960 wl_client_get_credentials(client, &pid, NULL, NULL);
5961
5962 /* Skip clients that we launched ourselves (the credentials of
5963 * the socketpair is ours) */
5964 if (pid == getpid())
5965 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005966
Daniel Stone325fc2d2012-05-30 16:31:58 +01005967 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005968}
5969
5970static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005971workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005972 uint32_t key, void *data)
5973{
5974 struct desktop_shell *shell = data;
5975 unsigned int new_index = shell->workspaces.current;
5976
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005977 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005978 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005979 if (new_index != 0)
5980 new_index--;
5981
5982 change_workspace(shell, new_index);
5983}
5984
5985static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005986workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005987 uint32_t key, void *data)
5988{
5989 struct desktop_shell *shell = data;
5990 unsigned int new_index = shell->workspaces.current;
5991
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005992 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005993 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005994 if (new_index < shell->workspaces.num - 1)
5995 new_index++;
5996
5997 change_workspace(shell, new_index);
5998}
5999
6000static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006001workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006002 uint32_t key, void *data)
6003{
6004 struct desktop_shell *shell = data;
6005 unsigned int new_index;
6006
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006007 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006008 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006009 new_index = key - KEY_F1;
6010 if (new_index >= shell->workspaces.num)
6011 new_index = shell->workspaces.num - 1;
6012
6013 change_workspace(shell, new_index);
6014}
6015
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006016static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006017workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006018 uint32_t key, void *data)
6019{
6020 struct desktop_shell *shell = data;
6021 unsigned int new_index = shell->workspaces.current;
6022
6023 if (shell->locked)
6024 return;
6025
6026 if (new_index != 0)
6027 new_index--;
6028
6029 take_surface_to_workspace_by_seat(shell, seat, new_index);
6030}
6031
6032static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006033workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006034 uint32_t key, void *data)
6035{
6036 struct desktop_shell *shell = data;
6037 unsigned int new_index = shell->workspaces.current;
6038
6039 if (shell->locked)
6040 return;
6041
6042 if (new_index < shell->workspaces.num - 1)
6043 new_index++;
6044
6045 take_surface_to_workspace_by_seat(shell, seat, new_index);
6046}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006047
6048static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006049handle_output_destroy(struct wl_listener *listener, void *data)
6050{
6051 struct shell_output *output_listener =
6052 container_of(listener, struct shell_output, destroy_listener);
6053
6054 wl_list_remove(&output_listener->destroy_listener.link);
6055 wl_list_remove(&output_listener->link);
6056 free(output_listener);
6057}
6058
6059static void
6060create_shell_output(struct desktop_shell *shell,
6061 struct weston_output *output)
6062{
6063 struct shell_output *shell_output;
6064
6065 shell_output = zalloc(sizeof *shell_output);
6066 if (shell_output == NULL)
6067 return;
6068
6069 shell_output->output = output;
6070 shell_output->shell = shell;
6071 shell_output->destroy_listener.notify = handle_output_destroy;
6072 wl_signal_add(&output->destroy_signal,
6073 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006074 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006075}
6076
6077static void
6078handle_output_create(struct wl_listener *listener, void *data)
6079{
6080 struct desktop_shell *shell =
6081 container_of(listener, struct desktop_shell, output_create_listener);
6082 struct weston_output *output = (struct weston_output *)data;
6083
6084 create_shell_output(shell, output);
6085}
6086
6087static void
6088setup_output_destroy_handler(struct weston_compositor *ec,
6089 struct desktop_shell *shell)
6090{
6091 struct weston_output *output;
6092
6093 wl_list_init(&shell->output_list);
6094 wl_list_for_each(output, &ec->output_list, link)
6095 create_shell_output(shell, output);
6096
6097 shell->output_create_listener.notify = handle_output_create;
6098 wl_signal_add(&ec->output_created_signal,
6099 &shell->output_create_listener);
6100}
6101
6102static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006103shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006104{
Tiago Vignattibe143262012-04-16 17:31:41 +03006105 struct desktop_shell *shell =
6106 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006107 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006108 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006109
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006110 if (shell->child.client)
6111 wl_client_destroy(shell->child.client);
6112
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006113 wl_list_remove(&shell->idle_listener.link);
6114 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006115 wl_list_remove(&shell->show_input_panel_listener.link);
6116 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006117
Xiong Zhang6b481422013-10-23 13:58:32 +08006118 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6119 wl_list_remove(&shell_output->destroy_listener.link);
6120 wl_list_remove(&shell_output->link);
6121 free(shell_output);
6122 }
6123
6124 wl_list_remove(&shell->output_create_listener.link);
6125
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006126 wl_array_for_each(ws, &shell->workspaces.array)
6127 workspace_destroy(*ws);
6128 wl_array_release(&shell->workspaces.array);
6129
Pekka Paalanen3c647232011-12-22 13:43:43 +02006130 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006131 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006132 free(shell);
6133}
6134
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006135static void
6136shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6137{
6138 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006139 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006140
6141 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006142 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6143 MODIFIER_CTRL | MODIFIER_ALT,
6144 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01006145 weston_compositor_add_key_binding(ec, KEY_TAB,
6146 MODIFIER_ALT,
6147 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006148 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6149 click_to_activate_binding,
6150 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006151 weston_compositor_add_touch_binding(ec, 0,
6152 touch_to_activate_binding,
6153 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006154 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6155 MODIFIER_SUPER | MODIFIER_ALT,
6156 surface_opacity_binding, NULL);
6157 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6158 MODIFIER_SUPER, zoom_axis_binding,
6159 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006160
6161 /* configurable bindings */
6162 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006163 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6164 zoom_key_binding, NULL);
6165 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6166 zoom_key_binding, NULL);
6167 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6168 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006169 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006170 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6171 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006172
6173 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6174 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6175 rotate_binding, NULL);
6176
Daniel Stone325fc2d2012-05-30 16:31:58 +01006177 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6178 shell);
6179 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6180 ec);
6181 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6182 backlight_binding, ec);
6183 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6184 ec);
6185 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6186 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006187 weston_compositor_add_key_binding(ec, KEY_K, mod,
6188 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006189 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6190 workspace_up_binding, shell);
6191 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6192 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006193 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6194 workspace_move_surface_up_binding,
6195 shell);
6196 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6197 workspace_move_surface_down_binding,
6198 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006199
Daniel Stonedf8133b2013-11-19 11:37:14 +01006200 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6201
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006202 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6203 if (shell->workspaces.num > 1) {
6204 num_workspace_bindings = shell->workspaces.num;
6205 if (num_workspace_bindings > 6)
6206 num_workspace_bindings = 6;
6207 for (i = 0; i < num_workspace_bindings; i++)
6208 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6209 workspace_f_binding,
6210 shell);
6211 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006212
6213 /* Debug bindings */
6214 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6215 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006216}
6217
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006218WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006219module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006220 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006221{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006222 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006223 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006224 struct workspace **pws;
6225 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006226 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006227
Peter Huttererf3d62272013-08-08 11:57:05 +10006228 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006229 if (shell == NULL)
6230 return -1;
6231
Kristian Høgsberg75840622011-09-06 13:48:16 -04006232 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006233
6234 shell->destroy_listener.notify = shell_destroy;
6235 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006236 shell->idle_listener.notify = idle_handler;
6237 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6238 shell->wake_listener.notify = wake_handler;
6239 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006240 shell->show_input_panel_listener.notify = show_input_panels;
6241 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6242 shell->hide_input_panel_listener.notify = hide_input_panels;
6243 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006244 shell->update_input_panel_listener.notify = update_input_panels;
6245 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006246 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006247 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006248 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006249 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006250 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006251 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006252 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006253 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006254 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006255 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006256 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006257
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006258 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006259
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006260 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6261 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006262 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6263 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006264 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006265
6266 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006267 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006268
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006269 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006270
Daniel Stonedf8133b2013-11-19 11:37:14 +01006271 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6272 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6273
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006274 for (i = 0; i < shell->workspaces.num; i++) {
6275 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6276 if (pws == NULL)
6277 return -1;
6278
6279 *pws = workspace_create();
6280 if (*pws == NULL)
6281 return -1;
6282 }
6283 activate_workspace(shell, 0);
6284
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006285 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006286 wl_list_init(&shell->workspaces.animation.link);
6287 shell->workspaces.animation.frame = animate_workspace_change_frame;
6288
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006289 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006290 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006291 return -1;
6292
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006293 if (wl_global_create(ec->wl_display,
6294 &desktop_shell_interface, 2,
6295 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006296 return -1;
6297
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006298 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6299 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006300 return -1;
6301
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006302 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006303 shell, bind_input_panel) == NULL)
6304 return -1;
6305
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006306 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6307 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006308 return -1;
6309
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006310 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006311
Xiong Zhang6b481422013-10-23 13:58:32 +08006312 setup_output_destroy_handler(ec, shell);
6313
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006314 loop = wl_display_get_event_loop(ec->wl_display);
6315 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006316
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006317 shell->screensaver.timer =
6318 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6319
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006320 wl_list_for_each(seat, &ec->seat_list, link)
6321 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006322
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006323 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006324
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006325 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006326
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006327 return 0;
6328}