blob: 9071727b1f0c8ae23ac952026f5ac09fd30cf22f [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,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300243 SHELL_SURFACE_POPUP,
244 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200245};
246
Scott Moreauff1db4a2012-04-17 19:06:18 -0600247struct ping_timer {
248 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600249 uint32_t serial;
250};
251
Philip Withnall648a4dd2013-11-25 18:01:44 +0000252/*
253 * Surface stacking and ordering.
254 *
255 * This is handled using several linked lists of surfaces, organised into
256 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
257 * above all of the surfaces in the layer below. The set of layers is static and
258 * in the following order (top-most first):
259 * • Lock layer (only ever displayed on its own)
260 * • Cursor layer
261 * • Fullscreen layer
262 * • Panel layer
263 * • Input panel layer
264 * • Workspace layers
265 * • Background layer
266 *
267 * The list of layers may be manipulated to remove whole layers of surfaces from
268 * display. For example, when locking the screen, all layers except the lock
269 * layer are removed.
270 *
271 * A surface’s layer is modified on configuring the surface, in
272 * set_surface_type() (which is only called when the surface’s type change is
273 * _committed_). If a surface’s type changes (e.g. when making a window
274 * fullscreen) its layer changes too.
275 *
276 * In order to allow popup and transient surfaces to be correctly stacked above
277 * their parent surfaces, each surface tracks both its parent surface, and a
278 * linked list of its children. When a surface’s layer is updated, so are the
279 * layers of its children. Note that child surfaces are *not* the same as
280 * subsurfaces — child/parent surfaces are purely for maintaining stacking
281 * order.
282 *
283 * The children_link list of siblings of a surface (i.e. those surfaces which
284 * have the same parent) only contains weston_surfaces which have a
285 * shell_surface. Stacking is not implemented for non-shell_surface
286 * weston_surfaces. This means that the following implication does *not* hold:
287 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
288 */
289
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200290struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500291 struct wl_resource *resource;
292 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200293
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500294 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500295 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600296 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200297 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400298 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000299 struct wl_list children_list; /* child surfaces of this one */
300 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300301 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200302
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400303 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400304 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500305 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800306 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800307 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700308 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100309
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500310 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200311 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500312 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200313 } rotation;
314
315 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100316 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500317 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100318 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400319 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500320 } popup;
321
Alex Wu4539b082012-03-01 12:57:46 +0800322 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300323 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400324 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300325 } transient;
326
327 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800328 enum wl_shell_surface_fullscreen_method type;
329 struct weston_transform transform; /* matrix from x, y */
330 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500331 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800332 } fullscreen;
333
Scott Moreauff1db4a2012-04-17 19:06:18 -0600334 struct ping_timer *ping_timer;
335
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200336 struct weston_transform workspace_transform;
337
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500338 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500339 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100340 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400341
342 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200343
344 struct {
345 bool maximized;
346 bool fullscreen;
347 } state, next_state; /* surface states */
348 bool state_changed;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200349};
350
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300351struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400352 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300353 struct shell_surface *shsurf;
354 struct wl_listener shsurf_destroy_listener;
355};
356
Rusty Lynch1084da52013-08-15 09:10:08 -0700357struct shell_touch_grab {
358 struct weston_touch_grab grab;
359 struct shell_surface *shsurf;
360 struct wl_listener shsurf_destroy_listener;
361 struct weston_touch *touch;
362};
363
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300364struct weston_move_grab {
365 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100366 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500367};
368
Rusty Lynch1084da52013-08-15 09:10:08 -0700369struct weston_touch_move_grab {
370 struct shell_touch_grab base;
371 wl_fixed_t dx, dy;
372};
373
Pekka Paalanen460099f2012-01-20 16:48:25 +0200374struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300375 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500376 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200377 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200378 float x;
379 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200380 } center;
381};
382
Giulio Camuffo5085a752013-03-25 21:42:45 +0100383struct shell_seat {
384 struct weston_seat *seat;
385 struct wl_listener seat_destroy_listener;
386
387 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400388 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100389 struct wl_list surfaces_list;
390 struct wl_client *client;
391 int32_t initial_up;
392 } popup_grab;
393};
394
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400395static void
396activate(struct desktop_shell *shell, struct weston_surface *es,
397 struct weston_seat *seat);
398
399static struct workspace *
400get_current_workspace(struct desktop_shell *shell);
401
Alex Wubd3354b2012-04-17 17:20:49 +0800402static struct shell_surface *
403get_shell_surface(struct weston_surface *surface);
404
405static struct desktop_shell *
406shell_surface_get_shell(struct shell_surface *shsurf);
407
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500408static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400409surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500410
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300411static void
412shell_fade_startup(struct desktop_shell *shell);
413
Philip Withnallbecb77e2013-11-25 18:01:30 +0000414static struct shell_seat *
415get_shell_seat(struct weston_seat *seat);
416
Philip Withnall648a4dd2013-11-25 18:01:44 +0000417static void
418shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
419
Alex Wubd3354b2012-04-17 17:20:49 +0800420static bool
421shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
422{
423 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800425
426 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100427
Jason Ekstranda7af7042013-10-12 22:38:11 -0500428 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800429 return false;
430
Jason Ekstranda7af7042013-10-12 22:38:11 -0500431 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
432 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800433 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500434 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800435}
436
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500437static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400438destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300439{
440 struct shell_grab *grab;
441
442 grab = container_of(listener, struct shell_grab,
443 shsurf_destroy_listener);
444
445 grab->shsurf = NULL;
446}
447
Jason Ekstranda7af7042013-10-12 22:38:11 -0500448static struct weston_view *
449get_default_view(struct weston_surface *surface)
450{
451 struct shell_surface *shsurf;
452 struct weston_view *view;
453
454 if (!surface || wl_list_empty(&surface->views))
455 return NULL;
456
457 shsurf = get_shell_surface(surface);
458 if (shsurf)
459 return shsurf->view;
460
461 wl_list_for_each(view, &surface->views, surface_link)
462 if (weston_view_is_mapped(view))
463 return view;
464
465 return container_of(surface->views.next, struct weston_view, surface_link);
466}
467
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300468static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400469popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400470
471static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300472shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400473 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300474 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400475 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300476 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300477{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300478 struct desktop_shell *shell = shsurf->shell;
479
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400480 popup_grab_end(pointer);
481
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300482 grab->grab.interface = interface;
483 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400484 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500485 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400486 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300487
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700488 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400489 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400490 if (shell->child.desktop_shell) {
491 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
492 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500493 weston_pointer_set_focus(pointer,
494 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400495 wl_fixed_from_int(0),
496 wl_fixed_from_int(0));
497 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300498}
499
500static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300501shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300502{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700503 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400504 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700505 grab->shsurf->grabbed = 0;
506 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300507
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700508 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300509}
510
511static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700512shell_touch_grab_start(struct shell_touch_grab *grab,
513 const struct weston_touch_grab_interface *interface,
514 struct shell_surface *shsurf,
515 struct weston_touch *touch)
516{
517 struct desktop_shell *shell = shsurf->shell;
518
519 grab->grab.interface = interface;
520 grab->shsurf = shsurf;
521 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
522 wl_signal_add(&shsurf->destroy_signal,
523 &grab->shsurf_destroy_listener);
524
525 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700526 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700527
528 weston_touch_start_grab(touch, &grab->grab);
529 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500530 weston_touch_set_focus(touch->seat,
531 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700532}
533
534static void
535shell_touch_grab_end(struct shell_touch_grab *grab)
536{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700537 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700538 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700539 grab->shsurf->grabbed = 0;
540 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700541
542 weston_touch_end_grab(grab->touch);
543}
544
545static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500546center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800547 struct weston_output *output);
548
Daniel Stone496ca172012-05-30 16:31:42 +0100549static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300550get_modifier(char *modifier)
551{
552 if (!modifier)
553 return MODIFIER_SUPER;
554
555 if (!strcmp("ctrl", modifier))
556 return MODIFIER_CTRL;
557 else if (!strcmp("alt", modifier))
558 return MODIFIER_ALT;
559 else if (!strcmp("super", modifier))
560 return MODIFIER_SUPER;
561 else
562 return MODIFIER_SUPER;
563}
564
Juan Zhaoe10d2792012-04-25 19:09:52 +0800565static enum animation_type
566get_animation_type(char *animation)
567{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800568 if (!strcmp("zoom", animation))
569 return ANIMATION_ZOOM;
570 else if (!strcmp("fade", animation))
571 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100572 else if (!strcmp("dim-layer", animation))
573 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800574 else
575 return ANIMATION_NONE;
576}
577
Alex Wu4539b082012-03-01 12:57:46 +0800578static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400579shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200580{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400581 struct weston_config_section *section;
582 int duration;
583 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200584
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400585 section = weston_config_get_section(shell->compositor->config,
586 "screensaver", NULL, NULL);
587 weston_config_section_get_string(section,
588 "path", &shell->screensaver.path, NULL);
589 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200590 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400591
592 section = weston_config_get_section(shell->compositor->config,
593 "shell", NULL, NULL);
594 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100595 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100596 shell->client = s;
597 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400598 "binding-modifier", &s, "super");
599 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200600 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400601 weston_config_section_get_string(section, "animation", &s, "none");
602 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200603 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700604 weston_config_section_get_string(section,
605 "startup-animation", &s, "fade");
606 shell->startup_animation_type = get_animation_type(s);
607 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700608 if (shell->startup_animation_type == ANIMATION_ZOOM)
609 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100610 weston_config_section_get_string(section, "focus-animation", &s, "none");
611 shell->focus_animation_type = get_animation_type(s);
612 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400613 weston_config_section_get_uint(section, "num-workspaces",
614 &shell->workspaces.num,
615 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200616}
617
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100618static struct weston_output *
619get_default_output(struct weston_compositor *compositor)
620{
621 return container_of(compositor->output_list.next,
622 struct weston_output, link);
623}
624
625
626/* no-op func for checking focus surface */
627static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600628focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100629{
630}
631
632static struct focus_surface *
633get_focus_surface(struct weston_surface *surface)
634{
635 if (surface->configure == focus_surface_configure)
636 return surface->configure_private;
637 else
638 return NULL;
639}
640
641static bool
642is_focus_surface (struct weston_surface *es)
643{
644 return (es->configure == focus_surface_configure);
645}
646
647static bool
648is_focus_view (struct weston_view *view)
649{
650 return is_focus_surface (view->surface);
651}
652
653static struct focus_surface *
654create_focus_surface(struct weston_compositor *ec,
655 struct weston_output *output)
656{
657 struct focus_surface *fsurf = NULL;
658 struct weston_surface *surface = NULL;
659
660 fsurf = malloc(sizeof *fsurf);
661 if (!fsurf)
662 return NULL;
663
664 fsurf->surface = weston_surface_create(ec);
665 surface = fsurf->surface;
666 if (surface == NULL) {
667 free(fsurf);
668 return NULL;
669 }
670
671 surface->configure = focus_surface_configure;
672 surface->output = output;
673 surface->configure_private = fsurf;
674
675 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100676 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100677
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600678 surface->width = output->width;
679 surface->height = output->height;
680 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100681 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
682 pixman_region32_fini(&surface->opaque);
683 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
684 output->width, output->height);
685 pixman_region32_fini(&surface->input);
686 pixman_region32_init(&surface->input);
687
688 wl_list_init(&fsurf->workspace_transform.link);
689
690 return fsurf;
691}
692
693static void
694focus_surface_destroy(struct focus_surface *fsurf)
695{
696 weston_surface_destroy(fsurf->surface);
697 free(fsurf);
698}
699
700static void
701focus_animation_done(struct weston_view_animation *animation, void *data)
702{
703 struct workspace *ws = data;
704
705 ws->focus_animation = NULL;
706}
707
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200708static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200709focus_state_destroy(struct focus_state *state)
710{
711 wl_list_remove(&state->seat_destroy_listener.link);
712 wl_list_remove(&state->surface_destroy_listener.link);
713 free(state);
714}
715
716static void
717focus_state_seat_destroy(struct wl_listener *listener, void *data)
718{
719 struct focus_state *state = container_of(listener,
720 struct focus_state,
721 seat_destroy_listener);
722
723 wl_list_remove(&state->link);
724 focus_state_destroy(state);
725}
726
727static void
728focus_state_surface_destroy(struct wl_listener *listener, void *data)
729{
730 struct focus_state *state = container_of(listener,
731 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400732 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400733 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500734 struct weston_surface *main_surface, *next;
735 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200736
Pekka Paalanen01388e22013-04-25 13:57:44 +0300737 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
738
Kristian Høgsberge3778222012-07-31 17:29:30 -0400739 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500740 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
741 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400742 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100743 if (is_focus_view(view))
744 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400745
Jason Ekstranda7af7042013-10-12 22:38:11 -0500746 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400747 break;
748 }
749
Pekka Paalanen01388e22013-04-25 13:57:44 +0300750 /* if the focus was a sub-surface, activate its main surface */
751 if (main_surface != state->keyboard_focus)
752 next = main_surface;
753
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100754 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400755 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100756 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400757 activate(shell, next, state->seat);
758 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100759 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
760 if (state->ws->focus_animation)
761 weston_view_animation_destroy(state->ws->focus_animation);
762
763 state->ws->focus_animation = weston_fade_run(
764 state->ws->fsurf_front->view,
765 state->ws->fsurf_front->view->alpha, 0.0, 300,
766 focus_animation_done, state->ws);
767 }
768
Kristian Høgsberge3778222012-07-31 17:29:30 -0400769 wl_list_remove(&state->link);
770 focus_state_destroy(state);
771 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200772}
773
774static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400775focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200776{
Jonas Ådahl04769742012-06-13 00:01:24 +0200777 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200778
779 state = malloc(sizeof *state);
780 if (state == NULL)
781 return NULL;
782
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100783 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400784 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200785 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400786 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200787
788 state->seat_destroy_listener.notify = focus_state_seat_destroy;
789 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400790 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200791 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400792 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200793
794 return state;
795}
796
Jonas Ådahl8538b222012-08-29 22:13:03 +0200797static struct focus_state *
798ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
799{
800 struct workspace *ws = get_current_workspace(shell);
801 struct focus_state *state;
802
803 wl_list_for_each(state, &ws->focus_list, link)
804 if (state->seat == seat)
805 break;
806
807 if (&state->link == &ws->focus_list)
808 state = focus_state_create(seat, ws);
809
810 return state;
811}
812
Jonas Ådahl04769742012-06-13 00:01:24 +0200813static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400814restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200815{
816 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400817 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200818
819 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400820 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200821
Kristian Høgsberge3148752013-05-06 23:19:49 -0400822 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200823 }
824}
825
826static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200827replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
828 struct weston_seat *seat)
829{
830 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400831 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200832
833 wl_list_for_each(state, &ws->focus_list, link) {
834 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400835 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500836 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200837 return;
838 }
839 }
840}
841
842static void
843drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
844 struct weston_surface *surface)
845{
846 struct focus_state *state;
847
848 wl_list_for_each(state, &ws->focus_list, link)
849 if (state->keyboard_focus == surface)
850 state->keyboard_focus = NULL;
851}
852
853static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100854animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
855 struct weston_view *from, struct weston_view *to)
856{
857 struct weston_output *output;
858 bool focus_surface_created = false;
859
860 /* FIXME: Only support dim animation using two layers */
861 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
862 return;
863
864 output = get_default_output(shell->compositor);
865 if (ws->fsurf_front == NULL && (from || to)) {
866 ws->fsurf_front = create_focus_surface(shell->compositor, output);
867 ws->fsurf_back = create_focus_surface(shell->compositor, output);
868 ws->fsurf_front->view->alpha = 0.0;
869 ws->fsurf_back->view->alpha = 0.0;
870 focus_surface_created = true;
871 } else {
872 wl_list_remove(&ws->fsurf_front->view->layer_link);
873 wl_list_remove(&ws->fsurf_back->view->layer_link);
874 }
875
876 if (ws->focus_animation) {
877 weston_view_animation_destroy(ws->focus_animation);
878 ws->focus_animation = NULL;
879 }
880
881 if (to)
882 wl_list_insert(&to->layer_link,
883 &ws->fsurf_front->view->layer_link);
884 else if (from)
885 wl_list_insert(&ws->layer.view_list,
886 &ws->fsurf_front->view->layer_link);
887
888 if (focus_surface_created) {
889 ws->focus_animation = weston_fade_run(
890 ws->fsurf_front->view,
891 ws->fsurf_front->view->alpha, 0.6, 300,
892 focus_animation_done, ws);
893 } else if (from) {
894 wl_list_insert(&from->layer_link,
895 &ws->fsurf_back->view->layer_link);
896 ws->focus_animation = weston_stable_fade_run(
897 ws->fsurf_front->view, 0.0,
898 ws->fsurf_back->view, 0.6,
899 focus_animation_done, ws);
900 } else if (to) {
901 wl_list_insert(&ws->layer.view_list,
902 &ws->fsurf_back->view->layer_link);
903 ws->focus_animation = weston_stable_fade_run(
904 ws->fsurf_front->view, 0.0,
905 ws->fsurf_back->view, 0.6,
906 focus_animation_done, ws);
907 }
908}
909
910static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200911workspace_destroy(struct workspace *ws)
912{
Jonas Ådahl04769742012-06-13 00:01:24 +0200913 struct focus_state *state, *next;
914
915 wl_list_for_each_safe(state, next, &ws->focus_list, link)
916 focus_state_destroy(state);
917
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100918 if (ws->fsurf_front)
919 focus_surface_destroy(ws->fsurf_front);
920 if (ws->fsurf_back)
921 focus_surface_destroy(ws->fsurf_back);
922
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200923 free(ws);
924}
925
Jonas Ådahl04769742012-06-13 00:01:24 +0200926static void
927seat_destroyed(struct wl_listener *listener, void *data)
928{
929 struct weston_seat *seat = data;
930 struct focus_state *state, *next;
931 struct workspace *ws = container_of(listener,
932 struct workspace,
933 seat_destroyed_listener);
934
935 wl_list_for_each_safe(state, next, &ws->focus_list, link)
936 if (state->seat == seat)
937 wl_list_remove(&state->link);
938}
939
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200940static struct workspace *
941workspace_create(void)
942{
943 struct workspace *ws = malloc(sizeof *ws);
944 if (ws == NULL)
945 return NULL;
946
947 weston_layer_init(&ws->layer, NULL);
948
Jonas Ådahl04769742012-06-13 00:01:24 +0200949 wl_list_init(&ws->focus_list);
950 wl_list_init(&ws->seat_destroyed_listener.link);
951 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100952 ws->fsurf_front = NULL;
953 ws->fsurf_back = NULL;
954 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200955
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200956 return ws;
957}
958
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200959static int
960workspace_is_empty(struct workspace *ws)
961{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200963}
964
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200965static struct workspace *
966get_workspace(struct desktop_shell *shell, unsigned int index)
967{
968 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200969 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200970 pws += index;
971 return *pws;
972}
973
974static struct workspace *
975get_current_workspace(struct desktop_shell *shell)
976{
977 return get_workspace(shell, shell->workspaces.current);
978}
979
980static void
981activate_workspace(struct desktop_shell *shell, unsigned int index)
982{
983 struct workspace *ws;
984
985 ws = get_workspace(shell, index);
986 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
987
988 shell->workspaces.current = index;
989}
990
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991static unsigned int
992get_output_height(struct weston_output *output)
993{
994 return abs(output->region.extents.y1 - output->region.extents.y2);
995}
996
997static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100998view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200999{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000 struct weston_transform *transform;
1001
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001002 if (is_focus_view(view)) {
1003 struct focus_surface *fsurf = get_focus_surface(view->surface);
1004 transform = &fsurf->workspace_transform;
1005 } else {
1006 struct shell_surface *shsurf = get_shell_surface(view->surface);
1007 transform = &shsurf->workspace_transform;
1008 }
1009
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001010 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001012 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001014 weston_matrix_init(&transform->matrix);
1015 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001016 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001018}
1019
1020static void
1021workspace_translate_out(struct workspace *ws, double fraction)
1022{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001024 unsigned int height;
1025 double d;
1026
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1028 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001029 d = height * fraction;
1030
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001032 }
1033}
1034
1035static void
1036workspace_translate_in(struct workspace *ws, double fraction)
1037{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001039 unsigned int height;
1040 double d;
1041
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1043 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044
1045 if (fraction > 0)
1046 d = -(height - height * fraction);
1047 else
1048 d = height + height * fraction;
1049
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001050 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051 }
1052}
1053
1054static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001055broadcast_current_workspace_state(struct desktop_shell *shell)
1056{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001057 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001058
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001059 wl_resource_for_each(resource, &shell->workspaces.client_list)
1060 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001061 shell->workspaces.current,
1062 shell->workspaces.num);
1063}
1064
1065static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001066reverse_workspace_change_animation(struct desktop_shell *shell,
1067 unsigned int index,
1068 struct workspace *from,
1069 struct workspace *to)
1070{
1071 shell->workspaces.current = index;
1072
1073 shell->workspaces.anim_to = to;
1074 shell->workspaces.anim_from = from;
1075 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1076 shell->workspaces.anim_timestamp = 0;
1077
Scott Moreau4272e632012-08-13 09:58:41 -06001078 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001079}
1080
1081static void
1082workspace_deactivate_transforms(struct workspace *ws)
1083{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001085 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001088 if (is_focus_view(view)) {
1089 struct focus_surface *fsurf = get_focus_surface(view->surface);
1090 transform = &fsurf->workspace_transform;
1091 } else {
1092 struct shell_surface *shsurf = get_shell_surface(view->surface);
1093 transform = &shsurf->workspace_transform;
1094 }
1095
1096 if (!wl_list_empty(&transform->link)) {
1097 wl_list_remove(&transform->link);
1098 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001099 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101 }
1102}
1103
1104static void
1105finish_workspace_change_animation(struct desktop_shell *shell,
1106 struct workspace *from,
1107 struct workspace *to)
1108{
Scott Moreau4272e632012-08-13 09:58:41 -06001109 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001110
1111 wl_list_remove(&shell->workspaces.animation.link);
1112 workspace_deactivate_transforms(from);
1113 workspace_deactivate_transforms(to);
1114 shell->workspaces.anim_to = NULL;
1115
1116 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1117}
1118
1119static void
1120animate_workspace_change_frame(struct weston_animation *animation,
1121 struct weston_output *output, uint32_t msecs)
1122{
1123 struct desktop_shell *shell =
1124 container_of(animation, struct desktop_shell,
1125 workspaces.animation);
1126 struct workspace *from = shell->workspaces.anim_from;
1127 struct workspace *to = shell->workspaces.anim_to;
1128 uint32_t t;
1129 double x, y;
1130
1131 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1132 finish_workspace_change_animation(shell, from, to);
1133 return;
1134 }
1135
1136 if (shell->workspaces.anim_timestamp == 0) {
1137 if (shell->workspaces.anim_current == 0.0)
1138 shell->workspaces.anim_timestamp = msecs;
1139 else
1140 shell->workspaces.anim_timestamp =
1141 msecs -
1142 /* Invers of movement function 'y' below. */
1143 (asin(1.0 - shell->workspaces.anim_current) *
1144 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1145 M_2_PI);
1146 }
1147
1148 t = msecs - shell->workspaces.anim_timestamp;
1149
1150 /*
1151 * x = [0, π/2]
1152 * y(x) = sin(x)
1153 */
1154 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1155 y = sin(x);
1156
1157 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001158 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001159
1160 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1161 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1162 shell->workspaces.anim_current = y;
1163
Scott Moreau4272e632012-08-13 09:58:41 -06001164 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001165 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001166 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001167 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168}
1169
1170static void
1171animate_workspace_change(struct desktop_shell *shell,
1172 unsigned int index,
1173 struct workspace *from,
1174 struct workspace *to)
1175{
1176 struct weston_output *output;
1177
1178 int dir;
1179
1180 if (index > shell->workspaces.current)
1181 dir = -1;
1182 else
1183 dir = 1;
1184
1185 shell->workspaces.current = index;
1186
1187 shell->workspaces.anim_dir = dir;
1188 shell->workspaces.anim_from = from;
1189 shell->workspaces.anim_to = to;
1190 shell->workspaces.anim_current = 0.0;
1191 shell->workspaces.anim_timestamp = 0;
1192
1193 output = container_of(shell->compositor->output_list.next,
1194 struct weston_output, link);
1195 wl_list_insert(&output->animation_list,
1196 &shell->workspaces.animation.link);
1197
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001199
1200 workspace_translate_in(to, 0);
1201
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001202 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001203
Scott Moreau4272e632012-08-13 09:58:41 -06001204 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001205}
1206
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001207static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208update_workspace(struct desktop_shell *shell, unsigned int index,
1209 struct workspace *from, struct workspace *to)
1210{
1211 shell->workspaces.current = index;
1212 wl_list_insert(&from->layer.link, &to->layer.link);
1213 wl_list_remove(&from->layer.link);
1214}
1215
1216static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001217change_workspace(struct desktop_shell *shell, unsigned int index)
1218{
1219 struct workspace *from;
1220 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001221 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001222
1223 if (index == shell->workspaces.current)
1224 return;
1225
1226 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001228 return;
1229
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001230 from = get_current_workspace(shell);
1231 to = get_workspace(shell, index);
1232
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001233 if (shell->workspaces.anim_from == to &&
1234 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001235 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001236 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001237 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001238 return;
1239 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001240
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001241 if (shell->workspaces.anim_to != NULL)
1242 finish_workspace_change_animation(shell,
1243 shell->workspaces.anim_from,
1244 shell->workspaces.anim_to);
1245
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001246 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001247
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001248 if (shell->focus_animation_type != ANIMATION_NONE) {
1249 wl_list_for_each(state, &from->focus_list, link)
1250 if (state->keyboard_focus)
1251 animate_focus_change(shell, from,
1252 get_default_view(state->keyboard_focus), NULL);
1253
1254 wl_list_for_each(state, &to->focus_list, link)
1255 if (state->keyboard_focus)
1256 animate_focus_change(shell, to,
1257 NULL, get_default_view(state->keyboard_focus));
1258 }
1259
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001260 if (workspace_is_empty(to) && workspace_is_empty(from))
1261 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001262 else
1263 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001264
1265 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001266}
1267
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001268static bool
1269workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1270{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272 struct wl_list *e;
1273
1274 if (wl_list_empty(list))
1275 return false;
1276
1277 e = list->next;
1278
1279 if (e->next != list)
1280 return false;
1281
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283}
1284
1285static void
Philip Withnall659163d2013-11-25 18:01:36 +00001286move_surface_to_workspace(struct desktop_shell *shell,
1287 struct shell_surface *shsurf,
1288 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001289{
1290 struct workspace *from;
1291 struct workspace *to;
1292 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001293 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001294 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001295
1296 if (workspace == shell->workspaces.current)
1297 return;
1298
Philip Withnall659163d2013-11-25 18:01:36 +00001299 view = get_default_view(shsurf->surface);
1300 if (!view)
1301 return;
1302
1303 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1304
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001305 if (workspace >= shell->workspaces.num)
1306 workspace = shell->workspaces.num - 1;
1307
Jonas Ådahle9d22502012-08-29 22:13:01 +02001308 from = get_current_workspace(shell);
1309 to = get_workspace(shell, workspace);
1310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 wl_list_remove(&view->layer_link);
1312 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001313
Philip Withnall648a4dd2013-11-25 18:01:44 +00001314 shell_surface_update_child_surface_layers(shsurf);
1315
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001317 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1318 if (!seat->keyboard)
1319 continue;
1320
1321 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001323 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001324 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001325
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327}
1328
1329static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001330take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001331 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001332 unsigned int index)
1333{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001334 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001336 struct shell_surface *shsurf;
1337 struct workspace *from;
1338 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001339 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001340
Pekka Paalanen01388e22013-04-25 13:57:44 +03001341 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 view = get_default_view(surface);
1343 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001344 index == shell->workspaces.current ||
1345 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001346 return;
1347
1348 from = get_current_workspace(shell);
1349 to = get_workspace(shell, index);
1350
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351 wl_list_remove(&view->layer_link);
1352 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001353
Philip Withnall659163d2013-11-25 18:01:36 +00001354 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001355 if (shsurf != NULL)
1356 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001357
Jonas Ådahle9d22502012-08-29 22:13:01 +02001358 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001359 drop_focus_state(shell, from, surface);
1360
1361 if (shell->workspaces.anim_from == to &&
1362 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001363 wl_list_remove(&to->layer.link);
1364 wl_list_insert(from->layer.link.prev, &to->layer.link);
1365
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001366 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001367 broadcast_current_workspace_state(shell);
1368
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001369 return;
1370 }
1371
1372 if (shell->workspaces.anim_to != NULL)
1373 finish_workspace_change_animation(shell,
1374 shell->workspaces.anim_from,
1375 shell->workspaces.anim_to);
1376
1377 if (workspace_is_empty(from) &&
1378 workspace_has_only(to, surface))
1379 update_workspace(shell, index, from, to);
1380 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001381 if (shsurf != NULL &&
1382 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001383 wl_list_insert(&shell->workspaces.anim_sticky_list,
1384 &shsurf->workspace_transform.link);
1385
1386 animate_workspace_change(shell, index, from, to);
1387 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001388
1389 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001390
1391 state = ensure_focus_state(shell, seat);
1392 if (state != NULL)
1393 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001394}
1395
1396static void
1397workspace_manager_move_surface(struct wl_client *client,
1398 struct wl_resource *resource,
1399 struct wl_resource *surface_resource,
1400 uint32_t workspace)
1401{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001402 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001403 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001404 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001405 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001406 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001407
Pekka Paalanen01388e22013-04-25 13:57:44 +03001408 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001409 shell_surface = get_shell_surface(main_surface);
1410 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001412
1413 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001414}
1415
1416static const struct workspace_manager_interface workspace_manager_implementation = {
1417 workspace_manager_move_surface,
1418};
1419
1420static void
1421unbind_resource(struct wl_resource *resource)
1422{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001423 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001424}
1425
1426static void
1427bind_workspace_manager(struct wl_client *client,
1428 void *data, uint32_t version, uint32_t id)
1429{
1430 struct desktop_shell *shell = data;
1431 struct wl_resource *resource;
1432
Jason Ekstranda85118c2013-06-27 20:17:02 -05001433 resource = wl_resource_create(client,
1434 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001435
1436 if (resource == NULL) {
1437 weston_log("couldn't add workspace manager object");
1438 return;
1439 }
1440
Jason Ekstranda85118c2013-06-27 20:17:02 -05001441 wl_resource_set_implementation(resource,
1442 &workspace_manager_implementation,
1443 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001444 wl_list_insert(&shell->workspaces.client_list,
1445 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001446
1447 workspace_manager_send_state(resource,
1448 shell->workspaces.current,
1449 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001450}
1451
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001452static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001453touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1454 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1455{
1456}
1457
1458static void
1459touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1460{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001461 struct weston_touch_move_grab *move =
1462 (struct weston_touch_move_grab *) container_of(
1463 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001464
Jonas Ådahl9484b692013-12-02 22:05:03 +01001465 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001466 shell_touch_grab_end(&move->base);
1467 free(move);
1468 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001469}
1470
1471static void
1472touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1473 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1474{
1475 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1476 struct shell_surface *shsurf = move->base.shsurf;
1477 struct weston_surface *es;
1478 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1479 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1480
1481 if (!shsurf)
1482 return;
1483
1484 es = shsurf->surface;
1485
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001486 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001487
1488 weston_compositor_schedule_repaint(es->compositor);
1489}
1490
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001491static void
1492touch_move_grab_cancel(struct weston_touch_grab *grab)
1493{
1494 struct weston_touch_move_grab *move =
1495 (struct weston_touch_move_grab *) container_of(
1496 grab, struct shell_touch_grab, grab);
1497
1498 shell_touch_grab_end(&move->base);
1499 free(move);
1500}
1501
Rusty Lynch1084da52013-08-15 09:10:08 -07001502static const struct weston_touch_grab_interface touch_move_grab_interface = {
1503 touch_move_grab_down,
1504 touch_move_grab_up,
1505 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001506 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001507};
1508
1509static int
1510surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1511{
1512 struct weston_touch_move_grab *move;
1513
1514 if (!shsurf)
1515 return -1;
1516
Rafael Antognolli03b16592013-12-03 15:35:42 -02001517 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001518 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001519 if (shsurf->grabbed)
1520 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001521
1522 move = malloc(sizeof *move);
1523 if (!move)
1524 return -1;
1525
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001527 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001529 seat->touch->grab_y;
1530
1531 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1532 seat->touch);
1533
1534 return 0;
1535}
1536
1537static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001538noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001539{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001540}
1541
1542static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001543move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1544 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001545{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001546 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001547 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001548 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001549 int dx, dy;
1550
1551 weston_pointer_move(pointer, x, y);
1552 dx = wl_fixed_to_int(pointer->x + move->dx);
1553 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001554
1555 if (!shsurf)
1556 return;
1557
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001558 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001559
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001561}
1562
1563static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001564move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001565 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001566{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001567 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1568 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001569 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001570 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001571
Daniel Stone4dbadb12012-05-30 16:31:51 +01001572 if (pointer->button_count == 0 &&
1573 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001574 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001575 free(grab);
1576 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001577}
1578
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001579static void
1580move_grab_cancel(struct weston_pointer_grab *grab)
1581{
1582 struct shell_grab *shell_grab =
1583 container_of(grab, struct shell_grab, grab);
1584
1585 shell_grab_end(shell_grab);
1586 free(grab);
1587}
1588
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001589static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001590 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001591 move_grab_motion,
1592 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001593 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001594};
1595
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001596static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001597surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598{
1599 struct weston_move_grab *move;
1600
1601 if (!shsurf)
1602 return -1;
1603
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001604 if (shsurf->grabbed)
1605 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001606 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001607 return 0;
1608
1609 move = malloc(sizeof *move);
1610 if (!move)
1611 return -1;
1612
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001614 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001616 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617
1618 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001619 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001620
1621 return 0;
1622}
1623
1624static void
1625shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1626 struct wl_resource *seat_resource, uint32_t serial)
1627{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001628 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001629 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001630 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001631
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001632 if (seat->pointer &&
1633 seat->pointer->button_count > 0 &&
1634 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001636 if ((surface == shsurf->surface) &&
1637 (surface_move(shsurf, seat) < 0))
1638 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001639 } else if (seat->touch &&
1640 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001642 if ((surface == shsurf->surface) &&
1643 (surface_touch_move(shsurf, seat) < 0))
1644 wl_resource_post_no_memory(resource);
1645 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001646}
1647
1648struct weston_resize_grab {
1649 struct shell_grab base;
1650 uint32_t edges;
1651 int32_t width, height;
1652};
1653
1654static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001655resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1656 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657{
1658 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001659 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001660 struct shell_surface *shsurf = resize->base.shsurf;
1661 int32_t width, height;
1662 wl_fixed_t from_x, from_y;
1663 wl_fixed_t to_x, to_y;
1664
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001665 weston_pointer_move(pointer, x, y);
1666
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001667 if (!shsurf)
1668 return;
1669
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 weston_view_from_global_fixed(shsurf->view,
1671 pointer->grab_x, pointer->grab_y,
1672 &from_x, &from_y);
1673 weston_view_from_global_fixed(shsurf->view,
1674 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001675
1676 width = resize->width;
1677 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1678 width += wl_fixed_to_int(from_x - to_x);
1679 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1680 width += wl_fixed_to_int(to_x - from_x);
1681 }
1682
1683 height = resize->height;
1684 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1685 height += wl_fixed_to_int(from_y - to_y);
1686 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1687 height += wl_fixed_to_int(to_y - from_y);
1688 }
1689
1690 shsurf->client->send_configure(shsurf->surface,
1691 resize->edges, width, height);
1692}
1693
1694static void
1695send_configure(struct weston_surface *surface,
1696 uint32_t edges, int32_t width, int32_t height)
1697{
1698 struct shell_surface *shsurf = get_shell_surface(surface);
1699
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001700 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701 edges, width, height);
1702}
1703
1704static const struct weston_shell_client shell_client = {
1705 send_configure
1706};
1707
1708static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001709resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710 uint32_t time, uint32_t button, uint32_t state_w)
1711{
1712 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001713 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001714 enum wl_pointer_button_state state = state_w;
1715
1716 if (pointer->button_count == 0 &&
1717 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1718 shell_grab_end(&resize->base);
1719 free(grab);
1720 }
1721}
1722
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001723static void
1724resize_grab_cancel(struct weston_pointer_grab *grab)
1725{
1726 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1727
1728 shell_grab_end(&resize->base);
1729 free(grab);
1730}
1731
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001732static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001733 noop_grab_focus,
1734 resize_grab_motion,
1735 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001736 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001737};
1738
Giulio Camuffob8366642013-04-25 13:57:46 +03001739/*
1740 * Returns the bounding box of a surface and all its sub-surfaces,
1741 * in the surface coordinates system. */
1742static void
1743surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1744 int32_t *y, int32_t *w, int32_t *h) {
1745 pixman_region32_t region;
1746 pixman_box32_t *box;
1747 struct weston_subsurface *subsurface;
1748
1749 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 surface->width,
1751 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001752
1753 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1754 pixman_region32_union_rect(&region, &region,
1755 subsurface->position.x,
1756 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 subsurface->surface->width,
1758 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001759 }
1760
1761 box = pixman_region32_extents(&region);
1762 if (x)
1763 *x = box->x1;
1764 if (y)
1765 *y = box->y1;
1766 if (w)
1767 *w = box->x2 - box->x1;
1768 if (h)
1769 *h = box->y2 - box->y1;
1770
1771 pixman_region32_fini(&region);
1772}
1773
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774static int
1775surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001776 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001777{
1778 struct weston_resize_grab *resize;
1779
Rafael Antognolli03b16592013-12-03 15:35:42 -02001780 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001781 return 0;
1782
1783 if (edges == 0 || edges > 15 ||
1784 (edges & 3) == 3 || (edges & 12) == 12)
1785 return 0;
1786
1787 resize = malloc(sizeof *resize);
1788 if (!resize)
1789 return -1;
1790
1791 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001792 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1793 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001794
1795 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001796 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001797
1798 return 0;
1799}
1800
1801static void
1802shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1803 struct wl_resource *seat_resource, uint32_t serial,
1804 uint32_t edges)
1805{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001806 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001807 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001808 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001809
Rafael Antognolli03b16592013-12-03 15:35:42 -02001810 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001811 return;
1812
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001814 if (seat->pointer->button_count == 0 ||
1815 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001816 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001817 return;
1818
Kristian Høgsberge3148752013-05-06 23:19:49 -04001819 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001820 wl_resource_post_no_memory(resource);
1821}
1822
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001823static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001824end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1825
1826static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001827busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001828{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001829 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001830 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001832 wl_fixed_t sx, sy;
1833
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 view = weston_compositor_pick_view(pointer->seat->compositor,
1835 pointer->x, pointer->y,
1836 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001837
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001839 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001840}
1841
1842static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001843busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1844 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001845{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001846 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001847}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001848
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001849static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001850busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001851 uint32_t time, uint32_t button, uint32_t state)
1852{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001853 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001854 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001855 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001856
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001857 if (shsurf && button == BTN_LEFT && state) {
1858 activate(shsurf->shell, shsurf->surface, seat);
1859 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001860 } else if (shsurf && button == BTN_RIGHT && state) {
1861 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001862 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001863 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001864}
1865
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001866static void
1867busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1868{
1869 struct shell_grab *grab = (struct shell_grab *) base;
1870
1871 shell_grab_end(grab);
1872 free(grab);
1873}
1874
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001875static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001876 busy_cursor_grab_focus,
1877 busy_cursor_grab_motion,
1878 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001879 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001880};
1881
1882static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001883set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001884{
1885 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001886
1887 grab = malloc(sizeof *grab);
1888 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001889 return;
1890
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001891 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1892 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001893}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001894
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001895static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001896end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001897{
1898 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1899
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001900 if (grab->grab.interface == &busy_cursor_grab_interface &&
1901 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001902 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001903 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001904 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001905}
1906
Scott Moreau9521d5e2012-04-19 13:06:17 -06001907static void
1908ping_timer_destroy(struct shell_surface *shsurf)
1909{
1910 if (!shsurf || !shsurf->ping_timer)
1911 return;
1912
1913 if (shsurf->ping_timer->source)
1914 wl_event_source_remove(shsurf->ping_timer->source);
1915
1916 free(shsurf->ping_timer);
1917 shsurf->ping_timer = NULL;
1918}
1919
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001920static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001921ping_timeout_handler(void *data)
1922{
1923 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001924 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001925
Scott Moreau9521d5e2012-04-19 13:06:17 -06001926 /* Client is not responding */
1927 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001928
1929 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001930 if (seat->pointer->focus &&
1931 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001932 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001933
1934 return 1;
1935}
1936
1937static void
1938ping_handler(struct weston_surface *surface, uint32_t serial)
1939{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001940 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001941 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001942 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001943
1944 if (!shsurf)
1945 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001946 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001947 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001948
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001949 if (shsurf->surface == shsurf->shell->grab_surface)
1950 return;
1951
Scott Moreauff1db4a2012-04-17 19:06:18 -06001952 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001953 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001954 if (!shsurf->ping_timer)
1955 return;
1956
1957 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001958 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1959 shsurf->ping_timer->source =
1960 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1961 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1962
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001963 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001964 }
1965}
1966
1967static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001968handle_pointer_focus(struct wl_listener *listener, void *data)
1969{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001970 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001971 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001972 struct weston_compositor *compositor;
1973 struct shell_surface *shsurf;
1974 uint32_t serial;
1975
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001977 return;
1978
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979 compositor = view->surface->compositor;
1980 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001981
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001982 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001983 set_busy_cursor(shsurf, pointer);
1984 } else {
1985 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001986 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001987 }
1988}
1989
1990static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001991create_pointer_focus_listener(struct weston_seat *seat)
1992{
1993 struct wl_listener *listener;
1994
Kristian Høgsberge3148752013-05-06 23:19:49 -04001995 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001996 return;
1997
1998 listener = malloc(sizeof *listener);
1999 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002000 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002001}
2002
2003static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002004shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
2005 uint32_t serial)
2006{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002007 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002008 struct weston_seat *seat;
2009 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002010
Kristian Høgsberg92374e12012-08-11 22:39:12 -04002011 if (shsurf->ping_timer == NULL)
2012 /* Just ignore unsolicited pong. */
2013 return;
2014
Scott Moreauff1db4a2012-04-17 19:06:18 -06002015 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002016 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02002017 wl_list_for_each(seat, &ec->seat_list, link) {
2018 if(seat->pointer)
2019 end_busy_cursor(shsurf, seat->pointer);
2020 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06002021 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002022 }
2023}
2024
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002025static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002026set_title(struct shell_surface *shsurf, const char *title)
2027{
2028 free(shsurf->title);
2029 shsurf->title = strdup(title);
2030}
2031
2032static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002033shell_surface_set_title(struct wl_client *client,
2034 struct wl_resource *resource, const char *title)
2035{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002036 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002037
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002038 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002039}
2040
2041static void
2042shell_surface_set_class(struct wl_client *client,
2043 struct wl_resource *resource, const char *class)
2044{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002045 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002046
2047 free(shsurf->class);
2048 shsurf->class = strdup(class);
2049}
2050
Alex Wu4539b082012-03-01 12:57:46 +08002051static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002052restore_output_mode(struct weston_output *output)
2053{
Hardening57388e42013-09-18 23:56:36 +02002054 if (output->current_mode != output->original_mode ||
2055 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002056 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002057 output->original_mode,
2058 output->original_scale,
2059 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002060}
2061
2062static void
2063restore_all_output_modes(struct weston_compositor *compositor)
2064{
2065 struct weston_output *output;
2066
2067 wl_list_for_each(output, &compositor->output_list, link)
2068 restore_output_mode(output);
2069}
2070
Philip Withnallbecb77e2013-11-25 18:01:30 +00002071static int
2072get_output_panel_height(struct desktop_shell *shell,
2073 struct weston_output *output)
2074{
2075 struct weston_view *view;
2076 int panel_height = 0;
2077
2078 if (!output)
2079 return 0;
2080
2081 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2082 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002083 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002084 break;
2085 }
2086 }
2087
2088 return panel_height;
2089}
2090
Philip Withnall07926d92013-11-25 18:01:40 +00002091/* The surface will be inserted into the list immediately after the link
2092 * returned by this function (i.e. will be stacked immediately above the
2093 * returned link). */
2094static struct wl_list *
2095shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2096{
2097 struct workspace *ws;
2098
2099 switch (shsurf->type) {
Philip Withnallda704d92013-11-25 18:01:46 +00002100 case SHELL_SURFACE_POPUP: {
2101 /* Popups should go at the front of the workspace of their
2102 * parent surface, rather than just in front of the parent. This
2103 * fixes the situation where there are two top-level windows:
2104 * - Above
2105 * - Below
2106 * and a pop-up menu is created for 'Below'. We want:
2107 * - Popup
2108 * - Above
2109 * - Below
2110 * not:
2111 * - Above
2112 * - Popup
2113 * - Below
2114 */
2115 struct shell_surface *parent_shsurf;
2116
2117 parent_shsurf = get_shell_surface(shsurf->parent);
2118 if (parent_shsurf != NULL)
2119 return shell_surface_calculate_layer_link(parent_shsurf);
2120
2121 break;
2122 }
2123
Philip Withnall07926d92013-11-25 18:01:40 +00002124 case SHELL_SURFACE_TRANSIENT: {
2125 /* Move the surface to its parent layer so that surfaces which
2126 * are transient for fullscreen surfaces don't get hidden by the
Philip Withnallda704d92013-11-25 18:01:46 +00002127 * fullscreen surfaces. However, unlike popups, transient
2128 * surfaces are stacked in front of their parent but not in
2129 * front of other surfaces of the same type. */
Philip Withnall07926d92013-11-25 18:01:40 +00002130 struct weston_view *parent;
2131
2132 /* TODO: Handle a parent with multiple views */
2133 parent = get_default_view(shsurf->parent);
2134 if (parent)
2135 return parent->layer_link.prev;
2136
2137 break;
2138 }
2139
Rafael Antognolli03b16592013-12-03 15:35:42 -02002140 case SHELL_SURFACE_TOPLEVEL: {
2141 if (shsurf->state.fullscreen)
2142 return &shsurf->shell->fullscreen_layer.view_list;
2143 break;
2144 }
Philip Withnall07926d92013-11-25 18:01:40 +00002145
2146 case SHELL_SURFACE_XWAYLAND:
Philip Withnall07926d92013-11-25 18:01:40 +00002147 case SHELL_SURFACE_NONE:
2148 default:
2149 /* Go to the fallback, below. */
2150 break;
2151 }
2152
2153 /* Move the surface to a normal workspace layer so that surfaces
2154 * which were previously fullscreen or transient are no longer
2155 * rendered on top. */
2156 ws = get_current_workspace(shsurf->shell);
2157 return &ws->layer.view_list;
2158}
2159
Philip Withnall648a4dd2013-11-25 18:01:44 +00002160static void
2161shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2162{
2163 struct shell_surface *child;
2164
2165 /* Move the child layers to the same workspace as shsurf. They will be
2166 * stacked above shsurf. */
2167 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2168 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2169 weston_view_geometry_dirty(child->view);
2170 wl_list_remove(&child->view->layer_link);
2171 wl_list_insert(shsurf->view->layer_link.prev,
2172 &child->view->layer_link);
2173 weston_view_geometry_dirty(child->view);
2174 weston_surface_damage(child->surface);
2175
2176 /* Recurse. We don’t expect this to recurse very far (if
2177 * at all) because that would imply we have transient
2178 * (or popup) children of transient surfaces, which
2179 * would be unusual. */
2180 shell_surface_update_child_surface_layers(child);
2181 }
2182 }
2183}
2184
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002185/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002186 * geometry to ensure the changes are redrawn.
2187 *
2188 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2189 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002190static void
2191shell_surface_update_layer(struct shell_surface *shsurf)
2192{
2193 struct wl_list *new_layer_link;
2194
2195 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2196
2197 if (new_layer_link == &shsurf->view->layer_link)
2198 return;
2199
2200 weston_view_geometry_dirty(shsurf->view);
2201 wl_list_remove(&shsurf->view->layer_link);
2202 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2203 weston_view_geometry_dirty(shsurf->view);
2204 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002205
2206 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002207}
2208
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002209static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002210shell_surface_set_parent(struct shell_surface *shsurf,
2211 struct weston_surface *parent)
2212{
2213 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002214
2215 wl_list_remove(&shsurf->children_link);
2216 wl_list_init(&shsurf->children_link);
2217
2218 /* Insert into the parent surface’s child list. */
2219 if (parent != NULL) {
2220 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2221 if (parent_shsurf != NULL)
2222 wl_list_insert(&parent_shsurf->children_list,
2223 &shsurf->children_link);
2224 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002225}
2226
2227static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002228shell_surface_set_output(struct shell_surface *shsurf,
2229 struct weston_output *output)
2230{
2231 struct weston_surface *es = shsurf->surface;
2232
2233 /* get the default output, if the client set it as NULL
2234 check whether the ouput is available */
2235 if (output)
2236 shsurf->output = output;
2237 else if (es->output)
2238 shsurf->output = es->output;
2239 else
2240 shsurf->output = get_default_output(es->compositor);
2241}
2242
2243static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002244surface_clear_next_states(struct shell_surface *shsurf)
2245{
2246 shsurf->next_state.maximized = false;
2247 shsurf->next_state.fullscreen = false;
2248
2249 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2250 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2251 shsurf->state_changed = true;
2252}
2253
2254static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002255set_toplevel(struct shell_surface *shsurf)
2256{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002257 shell_surface_set_parent(shsurf, NULL);
2258
Philip Withnallbecb77e2013-11-25 18:01:30 +00002259 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002260
2261 /* The layer_link is updated in set_surface_type(),
2262 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002263}
2264
2265static void
2266shell_surface_set_toplevel(struct wl_client *client,
2267 struct wl_resource *resource)
2268{
2269 struct shell_surface *surface = wl_resource_get_user_data(resource);
2270
Rafael Antognolli03b16592013-12-03 15:35:42 -02002271 surface_clear_next_states(surface);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002272 set_toplevel(surface);
2273}
2274
2275static void
2276set_transient(struct shell_surface *shsurf,
2277 struct weston_surface *parent, int x, int y, uint32_t flags)
2278{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002279 assert(parent != NULL);
2280
Philip Withnallbecb77e2013-11-25 18:01:30 +00002281 shsurf->transient.x = x;
2282 shsurf->transient.y = y;
2283 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002284
2285 shell_surface_set_parent(shsurf, parent);
2286
Philip Withnallbecb77e2013-11-25 18:01:30 +00002287 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002288
2289 /* The layer_link is updated in set_surface_type(),
2290 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002291}
2292
2293static void
2294shell_surface_set_transient(struct wl_client *client,
2295 struct wl_resource *resource,
2296 struct wl_resource *parent_resource,
2297 int x, int y, uint32_t flags)
2298{
2299 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2300 struct weston_surface *parent =
2301 wl_resource_get_user_data(parent_resource);
2302
Rafael Antognolli03b16592013-12-03 15:35:42 -02002303 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002304 set_transient(shsurf, parent, x, y, flags);
2305}
2306
2307static void
2308set_fullscreen(struct shell_surface *shsurf,
2309 uint32_t method,
2310 uint32_t framerate,
2311 struct weston_output *output)
2312{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002313 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002314
2315 shsurf->fullscreen_output = shsurf->output;
2316 shsurf->fullscreen.type = method;
2317 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002318
2319 shell_surface_set_parent(shsurf, NULL);
2320
Rafael Antognolli03b16592013-12-03 15:35:42 -02002321 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002322
2323 shsurf->client->send_configure(shsurf->surface, 0,
2324 shsurf->output->width,
2325 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002326
2327 /* The layer_link is updated in set_surface_type(),
2328 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002329}
2330
2331static void
2332unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002333{
Philip Withnallf85fe842013-11-25 18:01:37 +00002334 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002335 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2336 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002337 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002338 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002339 shsurf->fullscreen_output = NULL;
2340
Alex Wu4539b082012-03-01 12:57:46 +08002341 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2342 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002343
Alex Wu4539b082012-03-01 12:57:46 +08002344 wl_list_remove(&shsurf->fullscreen.transform.link);
2345 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002346
Jason Ekstranda7af7042013-10-12 22:38:11 -05002347 if (shsurf->fullscreen.black_view)
2348 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2349 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002350
Jason Ekstranda7af7042013-10-12 22:38:11 -05002351 weston_view_set_position(shsurf->view,
2352 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002353 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002354 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002355 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002356 shsurf->saved_rotation_valid = false;
2357 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002358
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002359 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002360}
2361
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002362static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002363shell_surface_set_fullscreen(struct wl_client *client,
2364 struct wl_resource *resource,
2365 uint32_t method,
2366 uint32_t framerate,
2367 struct wl_resource *output_resource)
2368{
2369 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2370 struct weston_output *output;
2371
2372 if (output_resource)
2373 output = wl_resource_get_user_data(output_resource);
2374 else
2375 output = NULL;
2376
Rafael Antognolli03b16592013-12-03 15:35:42 -02002377 surface_clear_next_states(shsurf);
2378 shsurf->next_state.fullscreen = true;
2379 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002380 set_fullscreen(shsurf, method, framerate, output);
2381}
2382
2383static void
2384set_popup(struct shell_surface *shsurf,
2385 struct weston_surface *parent,
2386 struct weston_seat *seat,
2387 uint32_t serial,
2388 int32_t x,
2389 int32_t y)
2390{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002391 assert(parent != NULL);
2392
Philip Withnallbecb77e2013-11-25 18:01:30 +00002393 shsurf->popup.shseat = get_shell_seat(seat);
2394 shsurf->popup.serial = serial;
2395 shsurf->popup.x = x;
2396 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002397
2398 shell_surface_set_parent(shsurf, parent);
Philip Withnallb995e1d2013-11-25 18:01:39 +00002399
2400 shsurf->next_type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002401}
2402
2403static void
2404shell_surface_set_popup(struct wl_client *client,
2405 struct wl_resource *resource,
2406 struct wl_resource *seat_resource,
2407 uint32_t serial,
2408 struct wl_resource *parent_resource,
2409 int32_t x, int32_t y, uint32_t flags)
2410{
2411 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2412
Rafael Antognolli03b16592013-12-03 15:35:42 -02002413 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002414 set_popup(shsurf,
2415 wl_resource_get_user_data(parent_resource),
2416 wl_resource_get_user_data(seat_resource),
2417 serial, x, y);
2418}
2419
2420static void
2421set_maximized(struct shell_surface *shsurf,
2422 struct weston_output *output)
2423{
2424 struct desktop_shell *shell;
2425 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002426
Philip Withnall352e7ed2013-11-25 18:01:35 +00002427 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002428
2429 shell = shell_surface_get_shell(shsurf);
2430 panel_height = get_output_panel_height(shell, shsurf->output);
2431 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2432
2433 shsurf->client->send_configure(shsurf->surface, edges,
2434 shsurf->output->width,
2435 shsurf->output->height - panel_height);
2436
Philip Withnalldc4332f2013-11-25 18:01:38 +00002437 shell_surface_set_parent(shsurf, NULL);
2438
Rafael Antognolli03b16592013-12-03 15:35:42 -02002439 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002440}
2441
2442static void
2443unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002444{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002445 /* undo all maximized things here */
2446 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002447 weston_view_set_position(shsurf->view,
2448 shsurf->saved_x,
2449 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002450
2451 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002452 wl_list_insert(&shsurf->view->geometry.transformation_list,
2453 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002454 shsurf->saved_rotation_valid = false;
2455 }
2456
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002457 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002458}
2459
Philip Withnallbecb77e2013-11-25 18:01:30 +00002460static void
2461shell_surface_set_maximized(struct wl_client *client,
2462 struct wl_resource *resource,
2463 struct wl_resource *output_resource)
2464{
2465 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2466 struct weston_output *output;
2467
2468 if (output_resource)
2469 output = wl_resource_get_user_data(output_resource);
2470 else
2471 output = NULL;
2472
Rafael Antognolli03b16592013-12-03 15:35:42 -02002473 surface_clear_next_states(shsurf);
2474 shsurf->next_state.maximized = true;
2475 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002476 set_maximized(shsurf, output);
2477}
2478
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002479/* This is only ever called from set_surface_type(), so there’s no need to
2480 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002481static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002482reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002483{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002484 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002485 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002486 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002487 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002488
2489 surface->type = SHELL_SURFACE_NONE;
2490 return 0;
2491}
2492
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002493static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002494set_full_output(struct shell_surface *shsurf)
2495{
2496 shsurf->saved_x = shsurf->view->geometry.x;
2497 shsurf->saved_y = shsurf->view->geometry.y;
2498 shsurf->saved_position_valid = true;
2499
2500 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2501 wl_list_remove(&shsurf->rotation.transform.link);
2502 wl_list_init(&shsurf->rotation.transform.link);
2503 weston_view_geometry_dirty(shsurf->view);
2504 shsurf->saved_rotation_valid = true;
2505 }
2506}
2507
2508static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002509set_surface_type(struct shell_surface *shsurf)
2510{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002511 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002512 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002513
Philip Withnallbecb77e2013-11-25 18:01:30 +00002514 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002515
2516 shsurf->type = shsurf->next_type;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002517 shsurf->state = shsurf->next_state;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002518 shsurf->next_type = SHELL_SURFACE_NONE;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002519 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002520
2521 switch (shsurf->type) {
2522 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02002523 if (shsurf->state.maximized || shsurf->state.fullscreen)
2524 set_full_output(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002525 break;
2526 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002527 if (pev)
2528 weston_view_set_position(shsurf->view,
2529 pev->geometry.x + shsurf->transient.x,
2530 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002531 break;
2532
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002533 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002534 weston_view_set_position(shsurf->view, shsurf->transient.x,
2535 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002536 break;
2537
Philip Withnall0f640e22013-11-25 18:01:31 +00002538 case SHELL_SURFACE_POPUP:
2539 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002540 default:
2541 break;
2542 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002543
2544 /* Update the surface’s layer. */
2545 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002546}
2547
Tiago Vignattibe143262012-04-16 17:31:41 +03002548static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002549shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002550{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002551 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002552}
2553
Alex Wu21858432012-04-01 20:13:08 +08002554static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002555black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002556
Jason Ekstranda7af7042013-10-12 22:38:11 -05002557static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002558create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002559 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002560 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002561{
2562 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002564
2565 surface = weston_surface_create(ec);
2566 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002567 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002568 return NULL;
2569 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002570 view = weston_view_create(surface);
2571 if (surface == NULL) {
2572 weston_log("no memory\n");
2573 weston_surface_destroy(surface);
2574 return NULL;
2575 }
Alex Wu4539b082012-03-01 12:57:46 +08002576
Alex Wu21858432012-04-01 20:13:08 +08002577 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002578 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002579 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002580 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002581 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002582 pixman_region32_fini(&surface->input);
2583 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002584
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002585 surface->width = w;
2586 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002587 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002588
2589 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002590}
2591
Philip Withnalled8f1a92013-11-25 18:01:43 +00002592static void
2593shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2594{
2595 struct weston_output *output = shsurf->fullscreen_output;
2596
Rafael Antognolli03b16592013-12-03 15:35:42 -02002597 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002598
2599 if (!shsurf->fullscreen.black_view)
2600 shsurf->fullscreen.black_view =
2601 create_black_surface(shsurf->surface->compositor,
2602 shsurf->surface,
2603 output->x, output->y,
2604 output->width,
2605 output->height);
2606
2607 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2608 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2609 wl_list_insert(&shsurf->view->layer_link,
2610 &shsurf->fullscreen.black_view->layer_link);
2611 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2612 weston_surface_damage(shsurf->surface);
2613}
2614
Alex Wu4539b082012-03-01 12:57:46 +08002615/* Create black surface and append it to the associated fullscreen surface.
2616 * Handle size dismatch and positioning according to the method. */
2617static void
2618shell_configure_fullscreen(struct shell_surface *shsurf)
2619{
2620 struct weston_output *output = shsurf->fullscreen_output;
2621 struct weston_surface *surface = shsurf->surface;
2622 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002623 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002624 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002625
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002626 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2627 restore_output_mode(output);
2628
Philip Withnalled8f1a92013-11-25 18:01:43 +00002629 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002630
Jason Ekstranda7af7042013-10-12 22:38:11 -05002631 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002632 &surf_width, &surf_height);
2633
Alex Wu4539b082012-03-01 12:57:46 +08002634 switch (shsurf->fullscreen.type) {
2635 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002636 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002637 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002638 break;
2639 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002640 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002641 if (output->width == surf_width &&
2642 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002643 weston_view_set_position(shsurf->view,
2644 output->x - surf_x,
2645 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002646 break;
2647 }
2648
Alex Wu4539b082012-03-01 12:57:46 +08002649 matrix = &shsurf->fullscreen.transform.matrix;
2650 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002651
Scott Moreau1bad5db2012-08-18 01:04:05 -06002652 output_aspect = (float) output->width /
2653 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 /* XXX: Use surf_width and surf_height here? */
2655 surface_aspect = (float) surface->width /
2656 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002657 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002658 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002659 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002660 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002661 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002662 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002663
Alex Wu4539b082012-03-01 12:57:46 +08002664 weston_matrix_scale(matrix, scale, scale, 1);
2665 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002666 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002667 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002668 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2669 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002670 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002671
Alex Wu4539b082012-03-01 12:57:46 +08002672 break;
2673 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002674 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002675 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002676 surf_width * surface->buffer_viewport.scale,
2677 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002678 shsurf->fullscreen.framerate};
2679
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002680 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002681 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002682 weston_view_set_position(shsurf->view,
2683 output->x - surf_x,
2684 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002685 shsurf->fullscreen.black_view->surface->width = output->width;
2686 shsurf->fullscreen.black_view->surface->height = output->height;
2687 weston_view_set_position(shsurf->fullscreen.black_view,
2688 output->x - surf_x,
2689 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002690 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002691 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002692 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002693 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002694 }
Alex Wubd3354b2012-04-17 17:20:49 +08002695 }
Alex Wu4539b082012-03-01 12:57:46 +08002696 break;
2697 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002698 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002699 break;
2700 default:
2701 break;
2702 }
2703}
2704
Alex Wu4539b082012-03-01 12:57:46 +08002705static void
2706shell_map_fullscreen(struct shell_surface *shsurf)
2707{
Alex Wubd3354b2012-04-17 17:20:49 +08002708 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002709}
2710
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002711static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002712set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2713{
2714 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002715 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002716 shsurf->transient.x = x;
2717 shsurf->transient.y = y;
2718 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002719
2720 shell_surface_set_parent(shsurf, NULL);
2721
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002722 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2723}
2724
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002725static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002726
2727static void
2728destroy_shell_seat(struct wl_listener *listener, void *data)
2729{
2730 struct shell_seat *shseat =
2731 container_of(listener,
2732 struct shell_seat, seat_destroy_listener);
2733 struct shell_surface *shsurf, *prev = NULL;
2734
2735 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002736 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002737 shseat->popup_grab.client = NULL;
2738
2739 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2740 shsurf->popup.shseat = NULL;
2741 if (prev) {
2742 wl_list_init(&prev->popup.grab_link);
2743 }
2744 prev = shsurf;
2745 }
2746 wl_list_init(&prev->popup.grab_link);
2747 }
2748
2749 wl_list_remove(&shseat->seat_destroy_listener.link);
2750 free(shseat);
2751}
2752
2753static struct shell_seat *
2754create_shell_seat(struct weston_seat *seat)
2755{
2756 struct shell_seat *shseat;
2757
2758 shseat = calloc(1, sizeof *shseat);
2759 if (!shseat) {
2760 weston_log("no memory to allocate shell seat\n");
2761 return NULL;
2762 }
2763
2764 shseat->seat = seat;
2765 wl_list_init(&shseat->popup_grab.surfaces_list);
2766
2767 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2768 wl_signal_add(&seat->destroy_signal,
2769 &shseat->seat_destroy_listener);
2770
2771 return shseat;
2772}
2773
2774static struct shell_seat *
2775get_shell_seat(struct weston_seat *seat)
2776{
2777 struct wl_listener *listener;
2778
2779 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2780 if (listener == NULL)
2781 return create_shell_seat(seat);
2782
2783 return container_of(listener,
2784 struct shell_seat, seat_destroy_listener);
2785}
2786
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002787static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002788popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002789{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002790 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002792 struct shell_seat *shseat =
2793 container_of(grab, struct shell_seat, popup_grab.grab);
2794 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002795 wl_fixed_t sx, sy;
2796
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797 view = weston_compositor_pick_view(pointer->seat->compositor,
2798 pointer->x, pointer->y,
2799 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002800
Jason Ekstranda7af7042013-10-12 22:38:11 -05002801 if (view && view->surface->resource &&
2802 wl_resource_get_client(view->surface->resource) == client) {
2803 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002804 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002805 weston_pointer_set_focus(pointer, NULL,
2806 wl_fixed_from_int(0),
2807 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002808 }
2809}
2810
2811static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002812popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2813 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002814{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002815 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002816 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002817 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002818
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002819 weston_pointer_move(pointer, x, y);
2820
Neil Roberts96d790e2013-09-19 17:32:00 +01002821 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002822 weston_view_from_global_fixed(pointer->focus,
2823 pointer->x, pointer->y,
2824 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002825 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002826 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002827}
2828
2829static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002830popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002831 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002832{
2833 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002834 struct shell_seat *shseat =
2835 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002836 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002837 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002838 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002839 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002840
Neil Roberts96d790e2013-09-19 17:32:00 +01002841 resource_list = &grab->pointer->focus_resource_list;
2842 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002843 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002844 wl_resource_for_each(resource, resource_list) {
2845 wl_pointer_send_button(resource, serial,
2846 time, button, state);
2847 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002848 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002849 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002850 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002851 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002852 }
2853
Daniel Stone4dbadb12012-05-30 16:31:51 +01002854 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002855 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002856}
2857
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002858static void
2859popup_grab_cancel(struct weston_pointer_grab *grab)
2860{
2861 popup_grab_end(grab->pointer);
2862}
2863
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002864static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002865 popup_grab_focus,
2866 popup_grab_motion,
2867 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002868 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002869};
2870
2871static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002872popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002873{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002874 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002875 struct shell_seat *shseat =
2876 container_of(grab, struct shell_seat, popup_grab.grab);
2877 struct shell_surface *shsurf;
2878 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002879
2880 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002881 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002882 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002883 shseat->popup_grab.grab.interface = NULL;
2884 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002885 /* Send the popup_done event to all the popups open */
2886 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002887 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002888 shsurf->popup.shseat = NULL;
2889 if (prev) {
2890 wl_list_init(&prev->popup.grab_link);
2891 }
2892 prev = shsurf;
2893 }
2894 wl_list_init(&prev->popup.grab_link);
2895 wl_list_init(&shseat->popup_grab.surfaces_list);
2896 }
2897}
2898
2899static void
2900add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2901{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002902 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002903
2904 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002905 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002906 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002907 /* We must make sure here that this popup was opened after
2908 * a mouse press, and not just by moving around with other
2909 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002910 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002911 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002912
Rob Bradforddfe31052013-06-26 19:49:11 +01002913 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002914 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002915 } else {
2916 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002917 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002918}
2919
2920static void
2921remove_popup_grab(struct shell_surface *shsurf)
2922{
2923 struct shell_seat *shseat = shsurf->popup.shseat;
2924
2925 wl_list_remove(&shsurf->popup.grab_link);
2926 wl_list_init(&shsurf->popup.grab_link);
2927 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002928 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002929 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002930 }
2931}
2932
2933static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002934shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002935{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002936 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002937 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002938
Jason Ekstranda7af7042013-10-12 22:38:11 -05002939 shsurf->surface->output = parent_view->output;
2940 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002941
Jason Ekstranda7af7042013-10-12 22:38:11 -05002942 weston_view_set_transform_parent(shsurf->view, parent_view);
2943 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2944 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002945
Kristian Høgsberge3148752013-05-06 23:19:49 -04002946 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002947 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002948 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002949 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002950 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002951 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002952}
2953
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002954static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002955 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002956 shell_surface_move,
2957 shell_surface_resize,
2958 shell_surface_set_toplevel,
2959 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002960 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002961 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002962 shell_surface_set_maximized,
2963 shell_surface_set_title,
2964 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002965};
2966
2967static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002968destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002969{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002970 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2971
Giulio Camuffo5085a752013-03-25 21:42:45 +01002972 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2973 remove_popup_grab(shsurf);
2974 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002975
Alex Wubd3354b2012-04-17 17:20:49 +08002976 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002977 shell_surface_is_top_fullscreen(shsurf))
2978 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002979
Jason Ekstranda7af7042013-10-12 22:38:11 -05002980 if (shsurf->fullscreen.black_view)
2981 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002982
Alex Wubd3354b2012-04-17 17:20:49 +08002983 /* As destroy_resource() use wl_list_for_each_safe(),
2984 * we can always remove the listener.
2985 */
2986 wl_list_remove(&shsurf->surface_destroy_listener.link);
2987 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002988 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002989 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002990
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 weston_view_destroy(shsurf->view);
2992
Philip Withnall648a4dd2013-11-25 18:01:44 +00002993 wl_list_remove(&shsurf->children_link);
2994
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002995 wl_list_remove(&shsurf->link);
2996 free(shsurf);
2997}
2998
2999static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003000shell_destroy_shell_surface(struct wl_resource *resource)
3001{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003002 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003003
3004 destroy_shell_surface(shsurf);
3005}
3006
3007static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003008shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003009{
3010 struct shell_surface *shsurf = container_of(listener,
3011 struct shell_surface,
3012 surface_destroy_listener);
3013
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003014 if (shsurf->resource)
3015 wl_resource_destroy(shsurf->resource);
3016 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003017 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003018}
3019
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003020static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003021shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003022
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003023static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003024get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003025{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003026 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003027 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003028 else
3029 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003030}
3031
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003032static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003033create_shell_surface(void *shell, struct weston_surface *surface,
3034 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003035{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003036 struct shell_surface *shsurf;
3037
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003038 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003039 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003040 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003041 }
3042
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003043 shsurf = calloc(1, sizeof *shsurf);
3044 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003045 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003046 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003047 }
3048
Jason Ekstranda7af7042013-10-12 22:38:11 -05003049 shsurf->view = weston_view_create(surface);
3050 if (!shsurf->view) {
3051 weston_log("no memory to allocate shell surface\n");
3052 free(shsurf);
3053 return NULL;
3054 }
3055
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003056 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003057 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003058
Tiago Vignattibc052c92012-04-19 16:18:18 +03003059 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003060 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003061 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003062 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003063 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003064 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3065 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003066 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003067 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003068 wl_list_init(&shsurf->fullscreen.transform.link);
3069
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003070 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003071 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003072 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003073 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003074
3075 /* init link so its safe to always remove it in destroy_shell_surface */
3076 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003077 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003078
Pekka Paalanen460099f2012-01-20 16:48:25 +02003079 /* empty when not in use */
3080 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003081 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003082
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003083 wl_list_init(&shsurf->workspace_transform.link);
3084
Philip Withnall648a4dd2013-11-25 18:01:44 +00003085 wl_list_init(&shsurf->children_link);
3086 wl_list_init(&shsurf->children_list);
3087 shsurf->parent = NULL;
3088
Pekka Paalanen98262232011-12-01 10:42:22 +02003089 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003090 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003091
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003092 shsurf->client = client;
3093
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003094 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003095}
3096
Jason Ekstranda7af7042013-10-12 22:38:11 -05003097static struct weston_view *
3098get_primary_view(void *shell, struct shell_surface *shsurf)
3099{
3100 return shsurf->view;
3101}
3102
Tiago Vignattibc052c92012-04-19 16:18:18 +03003103static void
3104shell_get_shell_surface(struct wl_client *client,
3105 struct wl_resource *resource,
3106 uint32_t id,
3107 struct wl_resource *surface_resource)
3108{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003109 struct weston_surface *surface =
3110 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003111 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003112 struct shell_surface *shsurf;
3113
3114 if (get_shell_surface(surface)) {
3115 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003116 WL_DISPLAY_ERROR_INVALID_OBJECT,
3117 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003118 return;
3119 }
3120
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003121 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003122 if (!shsurf) {
3123 wl_resource_post_error(surface_resource,
3124 WL_DISPLAY_ERROR_INVALID_OBJECT,
3125 "surface->configure already set");
3126 return;
3127 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003128
Jason Ekstranda85118c2013-06-27 20:17:02 -05003129 shsurf->resource =
3130 wl_resource_create(client,
3131 &wl_shell_surface_interface, 1, id);
3132 wl_resource_set_implementation(shsurf->resource,
3133 &shell_surface_implementation,
3134 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003135}
3136
3137static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003138 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003139};
3140
Kristian Høgsberg07937562011-04-12 17:25:42 -04003141static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003142shell_fade(struct desktop_shell *shell, enum fade_type type);
3143
3144static int
3145screensaver_timeout(void *data)
3146{
3147 struct desktop_shell *shell = data;
3148
3149 shell_fade(shell, FADE_OUT);
3150
3151 return 1;
3152}
3153
3154static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003155handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003156{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003157 struct desktop_shell *shell =
3158 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003159
Pekka Paalanen18027e52011-12-02 16:31:49 +02003160 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003161
3162 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003163 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003164}
3165
3166static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003167launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003168{
3169 if (shell->screensaver.binding)
3170 return;
3171
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003172 if (!shell->screensaver.path) {
3173 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003174 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003175 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003176
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003177 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003178 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003179 return;
3180 }
3181
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003182 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003183 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003184 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003185 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003186}
3187
3188static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003189terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003190{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003191 if (shell->screensaver.process.pid == 0)
3192 return;
3193
3194 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003195}
3196
3197static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003198configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003199{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003200 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003201
Jason Ekstranda7af7042013-10-12 22:38:11 -05003202 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3203 if (v->output == ev->output && v != ev) {
3204 weston_view_unmap(v);
3205 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003206 }
3207 }
3208
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003209 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003210
Jason Ekstranda7af7042013-10-12 22:38:11 -05003211 if (wl_list_empty(&ev->layer_link)) {
3212 wl_list_insert(&layer->view_list, &ev->layer_link);
3213 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003214 }
3215}
3216
3217static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003218background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003219{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003220 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003221 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003222
Jason Ekstranda7af7042013-10-12 22:38:11 -05003223 view = container_of(es->views.next, struct weston_view, surface_link);
3224
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003225 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003226}
3227
3228static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003229desktop_shell_set_background(struct wl_client *client,
3230 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003231 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003232 struct wl_resource *surface_resource)
3233{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003234 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003235 struct weston_surface *surface =
3236 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003237 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003238
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003239 if (surface->configure) {
3240 wl_resource_post_error(surface_resource,
3241 WL_DISPLAY_ERROR_INVALID_OBJECT,
3242 "surface role already assigned");
3243 return;
3244 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003245
Jason Ekstranda7af7042013-10-12 22:38:11 -05003246 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3247 weston_view_destroy(view);
3248 view = weston_view_create(surface);
3249
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003250 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003251 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003252 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003253 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003254 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003255 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003256 surface->output->width,
3257 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003258}
3259
3260static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003261panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003262{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003263 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003264 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003265
Jason Ekstranda7af7042013-10-12 22:38:11 -05003266 view = container_of(es->views.next, struct weston_view, surface_link);
3267
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003268 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003269}
3270
3271static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003272desktop_shell_set_panel(struct wl_client *client,
3273 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003274 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003275 struct wl_resource *surface_resource)
3276{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003277 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003278 struct weston_surface *surface =
3279 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003280 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003281
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003282 if (surface->configure) {
3283 wl_resource_post_error(surface_resource,
3284 WL_DISPLAY_ERROR_INVALID_OBJECT,
3285 "surface role already assigned");
3286 return;
3287 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003288
Jason Ekstranda7af7042013-10-12 22:38:11 -05003289 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3290 weston_view_destroy(view);
3291 view = weston_view_create(surface);
3292
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003293 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003294 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003295 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003296 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003297 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003298 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003299 surface->output->width,
3300 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003301}
3302
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003303static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003304lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003305{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003306 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003307 struct weston_view *view;
3308
3309 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003310
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003311 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003312 return;
3313
Jason Ekstranda7af7042013-10-12 22:38:11 -05003314 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003315
3316 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003317 wl_list_insert(&shell->lock_layer.view_list,
3318 &view->layer_link);
3319 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003320 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003321 }
3322}
3323
3324static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003325handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003326{
Tiago Vignattibe143262012-04-16 17:31:41 +03003327 struct desktop_shell *shell =
3328 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003329
Martin Minarik6d118362012-06-07 18:01:59 +02003330 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003331 shell->lock_surface = NULL;
3332}
3333
3334static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003335desktop_shell_set_lock_surface(struct wl_client *client,
3336 struct wl_resource *resource,
3337 struct wl_resource *surface_resource)
3338{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003339 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003340 struct weston_surface *surface =
3341 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003342
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003343 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003344
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003345 if (!shell->locked)
3346 return;
3347
Pekka Paalanen98262232011-12-01 10:42:22 +02003348 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003349
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003350 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003351 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003352 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003353
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003354 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003355 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003356 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003357}
3358
3359static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003360resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003361{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003362 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003363
Pekka Paalanen77346a62011-11-30 16:26:35 +02003364 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003365
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003366 wl_list_remove(&shell->lock_layer.link);
3367 wl_list_insert(&shell->compositor->cursor_layer.link,
3368 &shell->fullscreen_layer.link);
3369 wl_list_insert(&shell->fullscreen_layer.link,
3370 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003371 if (shell->showing_input_panels) {
3372 wl_list_insert(&shell->panel_layer.link,
3373 &shell->input_panel_layer.link);
3374 wl_list_insert(&shell->input_panel_layer.link,
3375 &ws->layer.link);
3376 } else {
3377 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3378 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003379
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003380 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003381
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003382 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003383 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003384 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003385}
3386
3387static void
3388desktop_shell_unlock(struct wl_client *client,
3389 struct wl_resource *resource)
3390{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003391 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003392
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003393 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003394
3395 if (shell->locked)
3396 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003397}
3398
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003399static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003400desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003401 struct wl_resource *resource,
3402 struct wl_resource *surface_resource)
3403{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003404 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003405
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003406 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003407 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003408}
3409
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003410static void
3411desktop_shell_desktop_ready(struct wl_client *client,
3412 struct wl_resource *resource)
3413{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003414 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003415
3416 shell_fade_startup(shell);
3417}
3418
Kristian Høgsberg75840622011-09-06 13:48:16 -04003419static const struct desktop_shell_interface desktop_shell_implementation = {
3420 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003421 desktop_shell_set_panel,
3422 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003423 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003424 desktop_shell_set_grab_surface,
3425 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003426};
3427
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003428static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003429get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003430{
3431 struct shell_surface *shsurf;
3432
3433 shsurf = get_shell_surface(surface);
3434 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003435 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003436 return shsurf->type;
3437}
3438
Kristian Høgsberg75840622011-09-06 13:48:16 -04003439static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003440move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003441{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003442 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003443 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003444 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003445
Pekka Paalanen01388e22013-04-25 13:57:44 +03003446 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003447 if (surface == NULL)
3448 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003449
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003450 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003451 if (shsurf == NULL || shsurf->state.fullscreen ||
3452 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003453 return;
3454
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003455 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003456}
3457
3458static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003459touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3460{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003461 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003462 struct weston_surface *surface;
3463 struct shell_surface *shsurf;
3464
3465 surface = weston_surface_get_main_surface(focus);
3466 if (surface == NULL)
3467 return;
3468
3469 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003470 if (shsurf == NULL || shsurf->state.fullscreen ||
3471 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003472 return;
3473
3474 surface_touch_move(shsurf, (struct weston_seat *) seat);
3475}
3476
3477static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003478resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003479{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003480 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003481 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003482 uint32_t edges = 0;
3483 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003484 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003485
Pekka Paalanen01388e22013-04-25 13:57:44 +03003486 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003487 if (surface == NULL)
3488 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003489
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003490 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003491 if (shsurf == NULL || shsurf->state.fullscreen ||
3492 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003493 return;
3494
Jason Ekstranda7af7042013-10-12 22:38:11 -05003495 weston_view_from_global(shsurf->view,
3496 wl_fixed_to_int(seat->pointer->grab_x),
3497 wl_fixed_to_int(seat->pointer->grab_y),
3498 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003499
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003500 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003501 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003502 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003503 edges |= 0;
3504 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003505 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003506
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003507 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003508 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003509 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003510 edges |= 0;
3511 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003512 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003513
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003514 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003515}
3516
3517static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003518surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003519 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003520{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003521 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003522 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003523 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003524 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003525
Pekka Paalanen01388e22013-04-25 13:57:44 +03003526 /* XXX: broken for windows containing sub-surfaces */
3527 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003528 if (surface == NULL)
3529 return;
3530
3531 shsurf = get_shell_surface(surface);
3532 if (!shsurf)
3533 return;
3534
Jason Ekstranda7af7042013-10-12 22:38:11 -05003535 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003536
Jason Ekstranda7af7042013-10-12 22:38:11 -05003537 if (shsurf->view->alpha > 1.0)
3538 shsurf->view->alpha = 1.0;
3539 if (shsurf->view->alpha < step)
3540 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003541
Jason Ekstranda7af7042013-10-12 22:38:11 -05003542 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003543 weston_surface_damage(surface);
3544}
3545
3546static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003547do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003548 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003549{
Daniel Stone37816df2012-05-16 18:45:18 +01003550 struct weston_seat *ws = (struct weston_seat *) seat;
3551 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003552 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003553 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003554
3555 wl_list_for_each(output, &compositor->output_list, link) {
3556 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003557 wl_fixed_to_double(seat->pointer->x),
3558 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003559 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003560 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003561 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003562 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003563 increment = -output->zoom.increment;
3564 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003565 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003566 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003567 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003568 else
3569 increment = 0;
3570
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003571 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003572
Scott Moreaue6603982012-06-11 13:07:51 -06003573 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003574 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003575 else if (output->zoom.level > output->zoom.max_level)
3576 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003577 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003578 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003579 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003580
Scott Moreaue6603982012-06-11 13:07:51 -06003581 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003582
Jason Ekstranda7af7042013-10-12 22:38:11 -05003583 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003584 }
3585 }
3586}
3587
Scott Moreauccbf29d2012-02-22 14:21:41 -07003588static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003589zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003590 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003591{
3592 do_zoom(seat, time, 0, axis, value);
3593}
3594
3595static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003596zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003597 void *data)
3598{
3599 do_zoom(seat, time, key, 0, 0);
3600}
3601
3602static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003603terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003604 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003605{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003606 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003607
Daniel Stone325fc2d2012-05-30 16:31:58 +01003608 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003609}
3610
3611static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003612lower_fullscreen_layer(struct desktop_shell *shell);
3613
3614struct alt_tab {
3615 struct desktop_shell *shell;
3616 struct weston_keyboard_grab grab;
3617
3618 struct wl_list *current;
3619
3620 struct wl_list preview_list;
3621};
3622
3623struct alt_tab_preview {
3624 struct alt_tab *alt_tab;
3625
3626 struct weston_view *view;
3627 struct weston_transform transform;
3628
3629 struct wl_listener listener;
3630
3631 struct wl_list link;
3632};
3633
3634static void
3635alt_tab_next(struct alt_tab *alt_tab)
3636{
3637 alt_tab->current = alt_tab->current->next;
3638
3639 /* Make sure we're not pointing to the list header e.g. after
3640 * cycling through the whole list. */
3641 if (alt_tab->current->next == alt_tab->preview_list.next)
3642 alt_tab->current = alt_tab->current->next;
3643}
3644
3645static void
3646alt_tab_destroy(struct alt_tab *alt_tab)
3647{
3648 struct alt_tab_preview *preview, *next;
3649 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3650
3651 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3652 preview = wl_container_of(alt_tab->current, preview, link);
3653
3654 activate(alt_tab->shell, preview->view->surface,
3655 (struct weston_seat *) keyboard->seat);
3656 }
3657
3658 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3659 wl_list_remove(&preview->link);
3660 wl_list_remove(&preview->listener.link);
3661 weston_view_destroy(preview->view);
3662 free(preview);
3663 }
3664
3665 weston_keyboard_end_grab(keyboard);
3666 if (keyboard->input_method_resource)
3667 keyboard->grab = &keyboard->input_method_grab;
3668
3669 free(alt_tab);
3670}
3671
3672static void
3673alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3674{
3675 struct alt_tab_preview *preview =
3676 container_of(listener, struct alt_tab_preview, listener);
3677 struct alt_tab *alt_tab = preview->alt_tab;
3678 int advance = 0;
3679
3680 /* If the preview that we're removing is the currently selected one,
3681 * we want to move to the next one. So we move to ->prev and then
3682 * call _next() after removing the preview. */
3683 if (alt_tab->current == &preview->link) {
3684 alt_tab->current = alt_tab->current->prev;
3685 advance = 1;
3686 }
3687
3688 wl_list_remove(&preview->listener.link);
3689 wl_list_remove(&preview->link);
3690
3691 free(preview);
3692
3693 if (advance)
3694 alt_tab_next(alt_tab);
3695
3696 /* If the last preview goes away, stop the alt-tab */
3697 if (wl_list_empty(alt_tab->current))
3698 alt_tab_destroy(alt_tab);
3699}
3700
3701static void
3702alt_tab_key(struct weston_keyboard_grab *grab,
3703 uint32_t time, uint32_t key, uint32_t state_w)
3704{
3705 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3706 enum wl_keyboard_key_state state = state_w;
3707
3708 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3709 alt_tab_next(alt_tab);
3710}
3711
3712static void
3713alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3714 uint32_t mods_depressed, uint32_t mods_latched,
3715 uint32_t mods_locked, uint32_t group)
3716{
3717 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3718 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3719
3720 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3721 alt_tab_destroy(alt_tab);
3722}
3723
3724static void
3725alt_tab_cancel(struct weston_keyboard_grab *grab)
3726{
3727 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3728
3729 alt_tab_destroy(alt_tab);
3730}
3731
3732static const struct weston_keyboard_grab_interface alt_tab_grab = {
3733 alt_tab_key,
3734 alt_tab_modifier,
3735 alt_tab_cancel,
3736};
3737
3738static int
3739view_for_alt_tab(struct weston_view *view)
3740{
3741 if (!get_shell_surface(view->surface))
3742 return 0;
3743
3744 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3745 return 0;
3746
3747 if (view != get_default_view(view->surface))
3748 return 0;
3749
3750 return 1;
3751}
3752
3753static void
3754alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3755 void *data)
3756{
3757 struct alt_tab *alt_tab;
3758 struct desktop_shell *shell = data;
3759 struct weston_output *output = get_default_output(shell->compositor);
3760 struct workspace *ws;
3761 struct weston_view *view;
3762 int num_surfaces = 0;
3763 int x, y, side, margin;
3764
3765 wl_list_for_each(view, &shell->compositor->view_list, link) {
3766
3767 if (view_for_alt_tab(view))
3768 num_surfaces++;
3769 }
3770
3771 if (!num_surfaces)
3772 return;
3773
3774 alt_tab = malloc(sizeof *alt_tab);
3775 if (!alt_tab)
3776 return;
3777
3778 alt_tab->shell = shell;
3779 wl_list_init(&alt_tab->preview_list);
3780 alt_tab->current = &alt_tab->preview_list;
3781
3782 restore_all_output_modes(shell->compositor);
3783 lower_fullscreen_layer(alt_tab->shell);
3784
3785 alt_tab->grab.interface = &alt_tab_grab;
3786 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3787 weston_keyboard_set_focus(seat->keyboard, NULL);
3788
3789 ws = get_current_workspace(shell);
3790
3791 /* FIXME: add some visual candy e.g. prelight the selected view
3792 * and/or add a black surrounding background à la gnome-shell */
3793
3794 /* Determine the size for each preview */
3795 side = output->width / num_surfaces;
3796 if (side > 200)
3797 side = 200;
3798 margin = side / 4;
3799 side -= margin;
3800
3801 x = margin;
3802 y = (output->height - side) / 2;
3803
3804 /* Create a view for each surface */
3805 wl_list_for_each(view, &shell->compositor->view_list, link) {
3806 struct alt_tab_preview *preview;
3807 struct weston_view *v;
3808 float scale;
3809
3810 if (!view_for_alt_tab(view))
3811 continue;
3812
3813 preview = malloc(sizeof *preview);
3814 if (!preview) {
3815 alt_tab_destroy(alt_tab);
3816 return;
3817 }
3818
3819 preview->alt_tab = alt_tab;
3820
3821 preview->view = v = weston_view_create(view->surface);
3822 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003823
3824 wl_list_remove(&v->layer_link);
3825 wl_list_insert(&ws->layer.view_list, &v->layer_link);
3826 weston_view_damage_below(v);
3827 weston_surface_damage(v->surface);
3828
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003829 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003830
3831 preview->listener.notify = alt_tab_handle_surface_destroy;
3832 wl_signal_add(&v->destroy_signal, &preview->listener);
3833
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003834 if (view->surface->width > view->surface->height)
3835 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003836 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003837 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003838
3839 wl_list_insert(&v->geometry.transformation_list,
3840 &preview->transform.link);
3841 weston_matrix_init(&preview->transform.matrix);
3842 weston_matrix_scale(&preview->transform.matrix,
3843 scale, scale, 1.0f);
3844
3845 weston_view_geometry_dirty(v);
3846 weston_compositor_schedule_repaint(v->surface->compositor);
3847
3848 /* Insert at the end of the list */
3849 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3850
3851 x += side + margin;
3852 }
3853
3854 /* Start at the second preview so a simple <alt>tab changes window.
3855 * We set `current' to the first preview and not the second because
3856 * we're going to receive a key press callback for the initial
3857 * <alt>tab which will make `current' point to the second element. */
3858 alt_tab_next(alt_tab);
3859}
3860
3861static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003862rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3863 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003864{
3865 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003866 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003867 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003868 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003869 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003870
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003871 weston_pointer_move(pointer, x, y);
3872
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003873 if (!shsurf)
3874 return;
3875
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003876 cx = 0.5f * shsurf->surface->width;
3877 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003878
Daniel Stone37816df2012-05-16 18:45:18 +01003879 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3880 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003881 r = sqrtf(dx * dx + dy * dy);
3882
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003883 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003884 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003885
3886 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003887 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003888 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003889
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003890 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003891 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003892
3893 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003894 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003895 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003896 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003897 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003898
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003899 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003900 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003901 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003902 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003903 wl_list_init(&shsurf->rotation.transform.link);
3904 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003905 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003906 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003907
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003908 /* We need to adjust the position of the surface
3909 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003910 cposx = shsurf->view->geometry.x + cx;
3911 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003912 dposx = rotate->center.x - cposx;
3913 dposy = rotate->center.y - cposy;
3914 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003915 weston_view_set_position(shsurf->view,
3916 shsurf->view->geometry.x + dposx,
3917 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003918 }
3919
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003920 /* Repaint implies weston_surface_update_transform(), which
3921 * lazily applies the damage due to rotation update.
3922 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003923 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003924}
3925
3926static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003927rotate_grab_button(struct weston_pointer_grab *grab,
3928 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003929{
3930 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003931 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003932 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003933 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003934 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003935
Daniel Stone4dbadb12012-05-30 16:31:51 +01003936 if (pointer->button_count == 0 &&
3937 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003938 if (shsurf)
3939 weston_matrix_multiply(&shsurf->rotation.rotation,
3940 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003941 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003942 free(rotate);
3943 }
3944}
3945
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003946static void
3947rotate_grab_cancel(struct weston_pointer_grab *grab)
3948{
3949 struct rotate_grab *rotate =
3950 container_of(grab, struct rotate_grab, base.grab);
3951
3952 shell_grab_end(&rotate->base);
3953 free(rotate);
3954}
3955
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003956static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003957 noop_grab_focus,
3958 rotate_grab_motion,
3959 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003960 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003961};
3962
3963static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003964surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003965{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003966 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003967 float dx, dy;
3968 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003969
Pekka Paalanen460099f2012-01-20 16:48:25 +02003970 rotate = malloc(sizeof *rotate);
3971 if (!rotate)
3972 return;
3973
Jason Ekstranda7af7042013-10-12 22:38:11 -05003974 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003975 surface->surface->width * 0.5f,
3976 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003977 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003978
Daniel Stone37816df2012-05-16 18:45:18 +01003979 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3980 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003981 r = sqrtf(dx * dx + dy * dy);
3982 if (r > 20.0f) {
3983 struct weston_matrix inverse;
3984
3985 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003986 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003987 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003988
3989 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003990 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003991 } else {
3992 weston_matrix_init(&surface->rotation.rotation);
3993 weston_matrix_init(&rotate->rotation);
3994 }
3995
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003996 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3997 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003998}
3999
4000static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004001rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004002 void *data)
4003{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004004 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004005 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004006 struct shell_surface *surface;
4007
Pekka Paalanen01388e22013-04-25 13:57:44 +03004008 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004009 if (base_surface == NULL)
4010 return;
4011
4012 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004013 if (surface == NULL || surface->state.fullscreen ||
4014 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004015 return;
4016
4017 surface_rotate(surface, seat);
4018}
4019
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004020/* Move all fullscreen layers down to the current workspace in a non-reversible
4021 * manner. This should be used when implementing shell-wide overlays, such as
4022 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004023static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004024lower_fullscreen_layer(struct desktop_shell *shell)
4025{
4026 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004027 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004028
4029 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004030 wl_list_for_each_reverse_safe(view, prev,
4031 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004032 layer_link) {
4033 wl_list_remove(&view->layer_link);
4034 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4035 weston_view_damage_below(view);
4036 weston_surface_damage(view->surface);
4037 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004038}
4039
4040static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004041activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004042 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004043{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004044 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004045 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004046 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004047 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004048 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004049
Pekka Paalanen01388e22013-04-25 13:57:44 +03004050 main_surface = weston_surface_get_main_surface(es);
4051
Daniel Stone37816df2012-05-16 18:45:18 +01004052 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004053
Jonas Ådahl8538b222012-08-29 22:13:03 +02004054 state = ensure_focus_state(shell, seat);
4055 if (state == NULL)
4056 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004057
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004058 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004059 state->keyboard_focus = es;
4060 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004061 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004062
Rafael Antognolli03b16592013-12-03 15:35:42 -02004063 shsurf = get_shell_surface(main_surface);
4064 if (shsurf->state.fullscreen)
4065 shell_configure_fullscreen(shsurf);
4066 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004067 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004068
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004069 if (shell->focus_animation_type != ANIMATION_NONE) {
4070 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004071 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004072 }
4073
4074 /* Update the surface’s layer. This brings it to the top of the stacking
4075 * order as appropriate. */
4076 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004077}
4078
Alex Wu21858432012-04-01 20:13:08 +08004079/* no-op func for checking black surface */
4080static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004081black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004082{
4083}
4084
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004085static bool
Alex Wu21858432012-04-01 20:13:08 +08004086is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4087{
4088 if (es->configure == black_surface_configure) {
4089 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004090 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004091 return true;
4092 }
4093 return false;
4094}
4095
Kristian Høgsberg75840622011-09-06 13:48:16 -04004096static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004097activate_binding(struct weston_seat *seat,
4098 struct desktop_shell *shell,
4099 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004100{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004101 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004102
Alex Wu9c35e6b2012-03-05 14:13:13 +08004103 if (!focus)
4104 return;
4105
Pekka Paalanen01388e22013-04-25 13:57:44 +03004106 if (is_black_surface(focus, &main_surface))
4107 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004108
Pekka Paalanen01388e22013-04-25 13:57:44 +03004109 main_surface = weston_surface_get_main_surface(focus);
4110 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004111 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004112
Neil Robertsa28c6932013-10-03 16:43:04 +01004113 activate(shell, focus, seat);
4114}
4115
4116static void
4117click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4118 void *data)
4119{
4120 if (seat->pointer->grab != &seat->pointer->default_grab)
4121 return;
4122
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004123 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004124}
4125
4126static void
4127touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4128{
4129 if (seat->touch->grab != &seat->touch->default_grab)
4130 return;
4131
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004132 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004133}
4134
4135static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004136lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004137{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004138 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004139
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004140 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004141 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004142 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004143 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004144
4145 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004146
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004147 /* Hide all surfaces by removing the fullscreen, panel and
4148 * toplevel layers. This way nothing else can show or receive
4149 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004150
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004151 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004152 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004153 if (shell->showing_input_panels)
4154 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004155 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004156 wl_list_insert(&shell->compositor->cursor_layer.link,
4157 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004158
Pekka Paalanen77346a62011-11-30 16:26:35 +02004159 launch_screensaver(shell);
4160
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004161 /* TODO: disable bindings that should not work while locked. */
4162
4163 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004164}
4165
4166static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004167unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004168{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004169 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004170 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004171 return;
4172 }
4173
4174 /* If desktop-shell client has gone away, unlock immediately. */
4175 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004176 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004177 return;
4178 }
4179
4180 if (shell->prepare_event_sent)
4181 return;
4182
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004183 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004184 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004185}
4186
4187static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004188shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004189{
4190 struct desktop_shell *shell = data;
4191
4192 shell->fade.animation = NULL;
4193
4194 switch (shell->fade.type) {
4195 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004196 weston_surface_destroy(shell->fade.view->surface);
4197 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004198 break;
4199 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004200 lock(shell);
4201 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004202 default:
4203 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004204 }
4205}
4206
Jason Ekstranda7af7042013-10-12 22:38:11 -05004207static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004208shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004209{
4210 struct weston_compositor *compositor = shell->compositor;
4211 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004212 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004213
4214 surface = weston_surface_create(compositor);
4215 if (!surface)
4216 return NULL;
4217
Jason Ekstranda7af7042013-10-12 22:38:11 -05004218 view = weston_view_create(surface);
4219 if (!view) {
4220 weston_surface_destroy(surface);
4221 return NULL;
4222 }
4223
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004224 surface->width = 8192;
4225 surface->height = 8192;
4226 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004227 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004228 wl_list_insert(&compositor->fade_layer.view_list,
4229 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004230 pixman_region32_init(&surface->input);
4231
Jason Ekstranda7af7042013-10-12 22:38:11 -05004232 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004233}
4234
4235static void
4236shell_fade(struct desktop_shell *shell, enum fade_type type)
4237{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004238 float tint;
4239
4240 switch (type) {
4241 case FADE_IN:
4242 tint = 0.0;
4243 break;
4244 case FADE_OUT:
4245 tint = 1.0;
4246 break;
4247 default:
4248 weston_log("shell: invalid fade type\n");
4249 return;
4250 }
4251
4252 shell->fade.type = type;
4253
Jason Ekstranda7af7042013-10-12 22:38:11 -05004254 if (shell->fade.view == NULL) {
4255 shell->fade.view = shell_fade_create_surface(shell);
4256 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004257 return;
4258
Jason Ekstranda7af7042013-10-12 22:38:11 -05004259 shell->fade.view->alpha = 1.0 - tint;
4260 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004261 }
4262
4263 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004264 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004265 else
4266 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004267 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004268 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004269 shell_fade_done, shell);
4270}
4271
4272static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004273do_shell_fade_startup(void *data)
4274{
4275 struct desktop_shell *shell = data;
4276
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004277 if (shell->startup_animation_type == ANIMATION_FADE)
4278 shell_fade(shell, FADE_IN);
4279 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 weston_surface_destroy(shell->fade.view->surface);
4281 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004282 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004283}
4284
4285static void
4286shell_fade_startup(struct desktop_shell *shell)
4287{
4288 struct wl_event_loop *loop;
4289
4290 if (!shell->fade.startup_timer)
4291 return;
4292
4293 wl_event_source_remove(shell->fade.startup_timer);
4294 shell->fade.startup_timer = NULL;
4295
4296 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4297 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4298}
4299
4300static int
4301fade_startup_timeout(void *data)
4302{
4303 struct desktop_shell *shell = data;
4304
4305 shell_fade_startup(shell);
4306 return 0;
4307}
4308
4309static void
4310shell_fade_init(struct desktop_shell *shell)
4311{
4312 /* Make compositor output all black, and wait for the desktop-shell
4313 * client to signal it is ready, then fade in. The timer triggers a
4314 * fade-in, in case the desktop-shell client takes too long.
4315 */
4316
4317 struct wl_event_loop *loop;
4318
Jason Ekstranda7af7042013-10-12 22:38:11 -05004319 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004320 weston_log("%s: warning: fade surface already exists\n",
4321 __func__);
4322 return;
4323 }
4324
Jason Ekstranda7af7042013-10-12 22:38:11 -05004325 shell->fade.view = shell_fade_create_surface(shell);
4326 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004327 return;
4328
Jason Ekstranda7af7042013-10-12 22:38:11 -05004329 weston_view_update_transform(shell->fade.view);
4330 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004331
4332 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4333 shell->fade.startup_timer =
4334 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4335 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4336}
4337
4338static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004339idle_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, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004343
4344 shell_fade(shell, FADE_OUT);
4345 /* lock() is called from shell_fade_done() */
4346}
4347
4348static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004349wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004350{
4351 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004352 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004353
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004354 unlock(shell);
4355}
4356
4357static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004358show_input_panels(struct wl_listener *listener, void *data)
4359{
4360 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004361 container_of(listener, struct desktop_shell,
4362 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004364
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004365 shell->text_input.surface = (struct weston_surface*)data;
4366
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004367 if (shell->showing_input_panels)
4368 return;
4369
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004370 shell->showing_input_panels = true;
4371
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004372 if (!shell->locked)
4373 wl_list_insert(&shell->panel_layer.link,
4374 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004375
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004377 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004378 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004379 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004380 wl_list_insert(&shell->input_panel_layer.view_list,
4381 &ipsurf->view->layer_link);
4382 weston_view_geometry_dirty(ipsurf->view);
4383 weston_view_update_transform(ipsurf->view);
4384 weston_surface_damage(ipsurf->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004385 weston_slide_run(ipsurf->view, ipsurf->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004386 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004387 }
4388}
4389
4390static void
4391hide_input_panels(struct wl_listener *listener, void *data)
4392{
4393 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004394 container_of(listener, struct desktop_shell,
4395 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004397
Jan Arne Petersen61381972013-01-31 15:52:21 +01004398 if (!shell->showing_input_panels)
4399 return;
4400
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004401 shell->showing_input_panels = false;
4402
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004403 if (!shell->locked)
4404 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004405
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 wl_list_for_each_safe(view, next,
4407 &shell->input_panel_layer.view_list, layer_link)
4408 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004409}
4410
4411static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004412update_input_panels(struct wl_listener *listener, void *data)
4413{
4414 struct desktop_shell *shell =
4415 container_of(listener, struct desktop_shell,
4416 update_input_panel_listener);
4417
4418 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4419}
4420
4421static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004422center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004423{
Giulio Camuffob8366642013-04-25 13:57:46 +03004424 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004425 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004426
Jason Ekstranda7af7042013-10-12 22:38:11 -05004427 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004428
4429 x = output->x + (output->width - width) / 2 - surf_x / 2;
4430 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004431
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004432 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004433}
4434
4435static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004436weston_view_set_initial_position(struct weston_view *view,
4437 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004438{
4439 struct weston_compositor *compositor = shell->compositor;
4440 int ix = 0, iy = 0;
4441 int range_x, range_y;
4442 int dx, dy, x, y, panel_height;
4443 struct weston_output *output, *target_output = NULL;
4444 struct weston_seat *seat;
4445
4446 /* As a heuristic place the new window on the same output as the
4447 * pointer. Falling back to the output containing 0, 0.
4448 *
4449 * TODO: Do something clever for touch too?
4450 */
4451 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004452 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004453 ix = wl_fixed_to_int(seat->pointer->x);
4454 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004455 break;
4456 }
4457 }
4458
4459 wl_list_for_each(output, &compositor->output_list, link) {
4460 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4461 target_output = output;
4462 break;
4463 }
4464 }
4465
4466 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004467 weston_view_set_position(view, 10 + random() % 400,
4468 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004469 return;
4470 }
4471
4472 /* Valid range within output where the surface will still be onscreen.
4473 * If this is negative it means that the surface is bigger than
4474 * output.
4475 */
4476 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004477 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004478 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004479 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004480
Rob Bradford4cb88c72012-08-13 15:18:44 +01004481 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004482 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004483 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004484 dx = 0;
4485
4486 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004487 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004488 else
4489 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004490
4491 x = target_output->x + dx;
4492 y = target_output->y + dy;
4493
Jason Ekstranda7af7042013-10-12 22:38:11 -05004494 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004495}
4496
4497static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004498map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004499 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004500{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004501 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004502 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004503 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004504 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004505
Pekka Paalanen77346a62011-11-30 16:26:35 +02004506 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004507 switch (shsurf->type) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004508 if (shsurf->state.fullscreen) {
4509 center_on_output(shsurf->view, shsurf->fullscreen_output);
4510 shell_map_fullscreen(shsurf);
4511 } else if (shsurf->state.maximized) {
4512 /* use surface configure to set the geometry */
4513 panel_height = get_output_panel_height(shell, shsurf->output);
4514 surface_subsurfaces_boundingbox(shsurf->surface,
4515 &surf_x, &surf_y, NULL, NULL);
4516 weston_view_set_position(shsurf->view,
4517 shsurf->output->x - surf_x,
4518 shsurf->output->y +
4519 panel_height - surf_y);
4520 } else {
4521 weston_view_set_initial_position(shsurf->view, shell);
4522 }
Juan Zhao96879df2012-02-07 08:45:41 +08004523 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004524 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004525 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004526 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004527 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004528 weston_view_set_position(shsurf->view,
4529 shsurf->view->geometry.x + sx,
4530 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004531 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004532 case SHELL_SURFACE_TRANSIENT:
4533 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004534 default:
4535 ;
4536 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004537
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004538 /* Surface stacking order, see also activate(). */
4539 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004540
Jason Ekstranda7af7042013-10-12 22:38:11 -05004541 if (shsurf->type != SHELL_SURFACE_NONE) {
4542 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004543 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004544 shsurf->surface->output = shsurf->output;
4545 shsurf->view->output = shsurf->output;
4546 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004547 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004548
Jason Ekstranda7af7042013-10-12 22:38:11 -05004549 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004550 /* XXX: xwayland's using the same fields for transient type */
4551 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004552 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004553 if (shsurf->transient.flags ==
4554 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4555 break;
4556 case SHELL_SURFACE_TOPLEVEL:
Daniel Stoneb2104682012-05-30 16:31:56 +01004557 if (!shell->locked) {
4558 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004559 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004560 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004561 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004562 case SHELL_SURFACE_POPUP:
4563 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004564 default:
4565 break;
4566 }
4567
Rafael Antognolli03b16592013-12-03 15:35:42 -02004568 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4569 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004570 {
4571 switch (shell->win_animation_type) {
4572 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004573 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004574 break;
4575 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004576 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004577 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004578 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004579 default:
4580 break;
4581 }
4582 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004583}
4584
4585static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004586configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004587 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004588{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004589 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004590 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004591 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004592
Pekka Paalanen77346a62011-11-30 16:26:35 +02004593 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004594
Rafael Antognolli03b16592013-12-03 15:35:42 -02004595 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004596 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004597 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004598 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004599 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4600 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004601 mx = shsurf->output->x - surf_x;
4602 my = shsurf->output->y +
4603 get_output_panel_height(shell,shsurf->output) - surf_y;
4604 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004605 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004606 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004607 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004608
Alex Wu4539b082012-03-01 12:57:46 +08004609 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004610 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004611 wl_list_for_each(view, &surface->views, surface_link)
4612 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004613
Rafael Antognolli03b16592013-12-03 15:35:42 -02004614 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004615 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004616 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004617}
4618
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004619static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004620shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004621{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004622 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004623 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004624
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004625 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004626
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004627 if (!weston_surface_is_mapped(es) &&
4628 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004629 remove_popup_grab(shsurf);
4630 }
4631
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004632 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004633 return;
4634
Rafael Antognolli03b16592013-12-03 15:35:42 -02004635 if ((shsurf->next_type != SHELL_SURFACE_NONE &&
4636 shsurf->type != shsurf->next_type) ||
4637 shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004638 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004639 type_changed = 1;
4640 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004641
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004642 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004643 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004644 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004645 shsurf->last_width != es->width ||
4646 shsurf->last_height != es->height) {
4647 shsurf->last_width = es->width;
4648 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004649 float from_x, from_y;
4650 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004651
Jason Ekstranda7af7042013-10-12 22:38:11 -05004652 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4653 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004654 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004655 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004656 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004657 }
4658}
4659
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004660static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004661
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004662static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004663desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004664{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004665 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004666 struct desktop_shell *shell =
4667 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004668
4669 shell->child.process.pid = 0;
4670 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004671
4672 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4673 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004674 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004675 shell->child.deathstamp = time;
4676 shell->child.deathcount = 0;
4677 }
4678
4679 shell->child.deathcount++;
4680 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004681 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004682 return;
4683 }
4684
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004685 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004686 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004687 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004688}
4689
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004690static void
4691launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004692{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004693 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004694
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004695 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004696 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004697 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004698 desktop_shell_sigchld);
4699
4700 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004701 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004702}
4703
4704static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004705bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4706{
Tiago Vignattibe143262012-04-16 17:31:41 +03004707 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004708 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004709
Jason Ekstranda85118c2013-06-27 20:17:02 -05004710 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4711 if (resource)
4712 wl_resource_set_implementation(resource, &shell_implementation,
4713 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004714}
4715
Kristian Høgsberg75840622011-09-06 13:48:16 -04004716static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004717unbind_desktop_shell(struct wl_resource *resource)
4718{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004719 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004720
4721 if (shell->locked)
4722 resume_desktop(shell);
4723
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004724 shell->child.desktop_shell = NULL;
4725 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004726}
4727
4728static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004729bind_desktop_shell(struct wl_client *client,
4730 void *data, uint32_t version, uint32_t id)
4731{
Tiago Vignattibe143262012-04-16 17:31:41 +03004732 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004733 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004734
Jason Ekstranda85118c2013-06-27 20:17:02 -05004735 resource = wl_resource_create(client, &desktop_shell_interface,
4736 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004737
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004738 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004739 wl_resource_set_implementation(resource,
4740 &desktop_shell_implementation,
4741 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004742 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004743
4744 if (version < 2)
4745 shell_fade_startup(shell);
4746
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004747 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004748 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004749
4750 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4751 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004752 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004753}
4754
Pekka Paalanen6e168112011-11-24 11:34:05 +02004755static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004756screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004757{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004758 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004759 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004760
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004761 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004762 return;
4763
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004764 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004765 if (!shell->locked)
4766 return;
4767
Jason Ekstranda7af7042013-10-12 22:38:11 -05004768 view = container_of(surface->views.next, struct weston_view, surface_link);
4769 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004770
Jason Ekstranda7af7042013-10-12 22:38:11 -05004771 if (wl_list_empty(&view->layer_link)) {
4772 wl_list_insert(shell->lock_layer.view_list.prev,
4773 &view->layer_link);
4774 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004775 wl_event_source_timer_update(shell->screensaver.timer,
4776 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004777 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004778 }
4779}
4780
4781static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004782screensaver_set_surface(struct wl_client *client,
4783 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004784 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004785 struct wl_resource *output_resource)
4786{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004787 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004788 struct weston_surface *surface =
4789 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004790 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004791 struct weston_view *view, *next;
4792
4793 /* Make sure we only have one view */
4794 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4795 weston_view_destroy(view);
4796 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004797
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004798 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004799 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004800 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004801}
4802
4803static const struct screensaver_interface screensaver_implementation = {
4804 screensaver_set_surface
4805};
4806
4807static void
4808unbind_screensaver(struct wl_resource *resource)
4809{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004810 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004811
Pekka Paalanen77346a62011-11-30 16:26:35 +02004812 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004813}
4814
4815static void
4816bind_screensaver(struct wl_client *client,
4817 void *data, uint32_t version, uint32_t id)
4818{
Tiago Vignattibe143262012-04-16 17:31:41 +03004819 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004820 struct wl_resource *resource;
4821
Jason Ekstranda85118c2013-06-27 20:17:02 -05004822 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004823
Pekka Paalanen77346a62011-11-30 16:26:35 +02004824 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004825 wl_resource_set_implementation(resource,
4826 &screensaver_implementation,
4827 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004828 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004829 return;
4830 }
4831
4832 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4833 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004834 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004835}
4836
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004837static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004838input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004839{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004840 struct input_panel_surface *ip_surface = surface->configure_private;
4841 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004842 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004843 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004844
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004845 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004846 return;
4847
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004848 if (!weston_surface_is_mapped(surface)) {
4849 if (!shell->showing_input_panels)
4850 return;
4851
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004852 show_surface = 1;
4853 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004854
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004855 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004856
4857 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004858 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4859 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004860 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004861 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
4862 y = ip_surface->output->y + ip_surface->output->height - surface->height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004863 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004864
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004865 weston_view_set_position(ip_surface->view, x, y);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004866
4867 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004868 wl_list_insert(&shell->input_panel_layer.view_list,
4869 &ip_surface->view->layer_link);
4870 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004871 weston_surface_damage(surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004872 weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004873 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004874}
4875
4876static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004877destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004878{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004879 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4880
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004881 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004882 wl_list_remove(&input_panel_surface->link);
4883
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004884 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004885 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004886
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004887 free(input_panel_surface);
4888}
4889
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004890static struct input_panel_surface *
4891get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004892{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004893 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004894 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004895 } else {
4896 return NULL;
4897 }
4898}
4899
4900static void
4901input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4902{
4903 struct input_panel_surface *ipsurface = container_of(listener,
4904 struct input_panel_surface,
4905 surface_destroy_listener);
4906
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004907 if (ipsurface->resource) {
4908 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004909 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004910 destroy_input_panel_surface(ipsurface);
4911 }
4912}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004913
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004914static struct input_panel_surface *
4915create_input_panel_surface(struct desktop_shell *shell,
4916 struct weston_surface *surface)
4917{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004918 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004919
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004920 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4921 if (!input_panel_surface)
4922 return NULL;
4923
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004924 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004925 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004926
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004927 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004928
4929 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004930 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004931
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004932 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004933 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004934 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004935 &input_panel_surface->surface_destroy_listener);
4936
4937 wl_list_init(&input_panel_surface->link);
4938
4939 return input_panel_surface;
4940}
4941
4942static void
4943input_panel_surface_set_toplevel(struct wl_client *client,
4944 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004945 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004946 uint32_t position)
4947{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004948 struct input_panel_surface *input_panel_surface =
4949 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004950 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004951
4952 wl_list_insert(&shell->input_panel.surfaces,
4953 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004954
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004955 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004956 input_panel_surface->panel = 0;
4957}
4958
4959static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004960input_panel_surface_set_overlay_panel(struct wl_client *client,
4961 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004962{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004963 struct input_panel_surface *input_panel_surface =
4964 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004965 struct desktop_shell *shell = input_panel_surface->shell;
4966
4967 wl_list_insert(&shell->input_panel.surfaces,
4968 &input_panel_surface->link);
4969
4970 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004971}
4972
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004973static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004974 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004975 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004976};
4977
4978static void
4979destroy_input_panel_surface_resource(struct wl_resource *resource)
4980{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004981 struct input_panel_surface *ipsurf =
4982 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004983
4984 destroy_input_panel_surface(ipsurf);
4985}
4986
4987static void
4988input_panel_get_input_panel_surface(struct wl_client *client,
4989 struct wl_resource *resource,
4990 uint32_t id,
4991 struct wl_resource *surface_resource)
4992{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004993 struct weston_surface *surface =
4994 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004995 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004996 struct input_panel_surface *ipsurf;
4997
4998 if (get_input_panel_surface(surface)) {
4999 wl_resource_post_error(surface_resource,
5000 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005001 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005002 return;
5003 }
5004
5005 ipsurf = create_input_panel_surface(shell, surface);
5006 if (!ipsurf) {
5007 wl_resource_post_error(surface_resource,
5008 WL_DISPLAY_ERROR_INVALID_OBJECT,
5009 "surface->configure already set");
5010 return;
5011 }
5012
Jason Ekstranda85118c2013-06-27 20:17:02 -05005013 ipsurf->resource =
5014 wl_resource_create(client,
5015 &wl_input_panel_surface_interface, 1, id);
5016 wl_resource_set_implementation(ipsurf->resource,
5017 &input_panel_surface_implementation,
5018 ipsurf,
5019 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005020}
5021
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005022static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005023 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005024};
5025
5026static void
5027unbind_input_panel(struct wl_resource *resource)
5028{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005029 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005030
5031 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005032}
5033
5034static void
5035bind_input_panel(struct wl_client *client,
5036 void *data, uint32_t version, uint32_t id)
5037{
5038 struct desktop_shell *shell = data;
5039 struct wl_resource *resource;
5040
Jason Ekstranda85118c2013-06-27 20:17:02 -05005041 resource = wl_resource_create(client,
5042 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005043
5044 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005045 wl_resource_set_implementation(resource,
5046 &input_panel_implementation,
5047 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005048 shell->input_panel.binding = resource;
5049 return;
5050 }
5051
5052 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5053 "interface object already bound");
5054 wl_resource_destroy(resource);
5055}
5056
Kristian Høgsberg07045392012-02-19 18:52:44 -05005057struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005058 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005059 struct weston_surface *current;
5060 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005061 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005062};
5063
5064static void
5065switcher_next(struct switcher *switcher)
5066{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005067 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005068 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005069 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005070 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005071
Jason Ekstranda7af7042013-10-12 22:38:11 -05005072 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
5073 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005074 case SHELL_SURFACE_TOPLEVEL:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005075 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005076 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005077 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005078 next = view->surface;
5079 prev = view->surface;
5080 view->alpha = 0.25;
5081 weston_view_geometry_dirty(view);
5082 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005083 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005084 case SHELL_SURFACE_TRANSIENT:
5085 case SHELL_SURFACE_POPUP:
5086 case SHELL_SURFACE_XWAYLAND:
5087 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005088 default:
5089 break;
5090 }
Alex Wu1659daa2012-04-01 20:13:09 +08005091
Jason Ekstranda7af7042013-10-12 22:38:11 -05005092 if (is_black_surface(view->surface, NULL)) {
5093 view->alpha = 0.25;
5094 weston_view_geometry_dirty(view);
5095 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005096 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005097 }
5098
5099 if (next == NULL)
5100 next = first;
5101
Alex Wu07b26062012-03-12 16:06:01 +08005102 if (next == NULL)
5103 return;
5104
Kristian Høgsberg07045392012-02-19 18:52:44 -05005105 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005106 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005107
5108 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005109 wl_list_for_each(view, &next->views, surface_link)
5110 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005111
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005112 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005113 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005114 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005115}
5116
5117static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005118switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005119{
5120 struct switcher *switcher =
5121 container_of(listener, struct switcher, listener);
5122
5123 switcher_next(switcher);
5124}
5125
5126static void
Daniel Stone351eb612012-05-31 15:27:47 -04005127switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005128{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005129 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005130 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005131 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005132
Jason Ekstranda7af7042013-10-12 22:38:11 -05005133 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005134 if (is_focus_view(view))
5135 continue;
5136
Jason Ekstranda7af7042013-10-12 22:38:11 -05005137 view->alpha = 1.0;
5138 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005139 }
5140
Alex Wu07b26062012-03-12 16:06:01 +08005141 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005142 activate(switcher->shell, switcher->current,
5143 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005144 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005145 weston_keyboard_end_grab(keyboard);
5146 if (keyboard->input_method_resource)
5147 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005148 free(switcher);
5149}
5150
5151static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005152switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005153 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005154{
5155 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005156 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005157
Daniel Stonec9785ea2012-05-30 16:31:52 +01005158 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005159 switcher_next(switcher);
5160}
5161
5162static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005163switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005164 uint32_t mods_depressed, uint32_t mods_latched,
5165 uint32_t mods_locked, uint32_t group)
5166{
5167 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005168 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005169
Daniel Stone351eb612012-05-31 15:27:47 -04005170 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5171 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005172}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005173
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005174static void
5175switcher_cancel(struct weston_keyboard_grab *grab)
5176{
5177 struct switcher *switcher = container_of(grab, struct switcher, grab);
5178
5179 switcher_destroy(switcher);
5180}
5181
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005182static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005183 switcher_key,
5184 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005185 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005186};
5187
5188static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005189switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005190 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005191{
Tiago Vignattibe143262012-04-16 17:31:41 +03005192 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005193 struct switcher *switcher;
5194
5195 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005196 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005197 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005198 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005199 wl_list_init(&switcher->listener.link);
5200
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005201 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005202 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005203 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005204 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5205 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005206 switcher_next(switcher);
5207}
5208
Daniel Stonedf8133b2013-11-19 11:37:14 +01005209struct exposay_surface {
5210 struct desktop_shell *shell;
5211 struct weston_surface *surface;
5212 struct weston_view *view;
5213 struct wl_list link;
5214
5215 int x;
5216 int y;
5217 int width;
5218 int height;
5219 double scale;
5220
5221 int row;
5222 int column;
5223
5224 /* The animations only apply a transformation for their own lifetime,
5225 * and don't have an option to indefinitely maintain the
5226 * transformation in a steady state - so, we apply our own once the
5227 * animation has finished. */
5228 struct weston_transform transform;
5229};
5230
5231static void exposay_set_state(struct desktop_shell *shell,
5232 enum exposay_target_state state,
5233 struct weston_seat *seat);
5234static void exposay_check_state(struct desktop_shell *shell);
5235
5236static void
5237exposay_in_flight_inc(struct desktop_shell *shell)
5238{
5239 shell->exposay.in_flight++;
5240}
5241
5242static void
5243exposay_in_flight_dec(struct desktop_shell *shell)
5244{
5245 if (--shell->exposay.in_flight > 0)
5246 return;
5247
5248 exposay_check_state(shell);
5249}
5250
5251static void
5252exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5253{
5254 struct exposay_surface *esurface = data;
5255
5256 wl_list_insert(&esurface->view->geometry.transformation_list,
5257 &esurface->transform.link);
5258 weston_matrix_init(&esurface->transform.matrix);
5259 weston_matrix_scale(&esurface->transform.matrix,
5260 esurface->scale, esurface->scale, 1.0f);
5261 weston_matrix_translate(&esurface->transform.matrix,
5262 esurface->x - esurface->view->geometry.x,
5263 esurface->y - esurface->view->geometry.y,
5264 0);
5265
5266 weston_view_geometry_dirty(esurface->view);
5267 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5268
5269 exposay_in_flight_dec(esurface->shell);
5270}
5271
5272static void
5273exposay_animate_in(struct exposay_surface *esurface)
5274{
5275 exposay_in_flight_inc(esurface->shell);
5276
5277 weston_move_scale_run(esurface->view,
5278 esurface->x - esurface->view->geometry.x,
5279 esurface->y - esurface->view->geometry.y,
5280 1.0, esurface->scale, 0,
5281 exposay_animate_in_done, esurface);
5282}
5283
5284static void
5285exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5286{
5287 struct exposay_surface *esurface = data;
5288 struct desktop_shell *shell = esurface->shell;
5289
5290 wl_list_remove(&esurface->link);
5291 free(esurface);
5292
5293 exposay_in_flight_dec(shell);
5294}
5295
5296static void
5297exposay_animate_out(struct exposay_surface *esurface)
5298{
5299 exposay_in_flight_inc(esurface->shell);
5300
5301 /* Remove the static transformation set up by
5302 * exposay_transform_in_done(). */
5303 wl_list_remove(&esurface->transform.link);
5304 weston_view_geometry_dirty(esurface->view);
5305
5306 weston_move_scale_run(esurface->view,
5307 esurface->x - esurface->view->geometry.x,
5308 esurface->y - esurface->view->geometry.y,
5309 1.0, esurface->scale, 1,
5310 exposay_animate_out_done, esurface);
5311}
5312
5313static void
5314exposay_highlight_surface(struct desktop_shell *shell,
5315 struct exposay_surface *esurface)
5316{
5317 struct weston_view *view = NULL;
5318
5319 if (esurface) {
5320 shell->exposay.row_current = esurface->row;
5321 shell->exposay.column_current = esurface->column;
5322 view = esurface->view;
5323 }
5324
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005325 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005326 shell->exposay.focus_current = view;
5327}
5328
5329static int
5330exposay_is_animating(struct desktop_shell *shell)
5331{
5332 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5333 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5334 return 0;
5335
5336 return (shell->exposay.in_flight > 0);
5337}
5338
5339static void
5340exposay_pick(struct desktop_shell *shell, int x, int y)
5341{
5342 struct exposay_surface *esurface;
5343
5344 if (exposay_is_animating(shell))
5345 return;
5346
5347 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5348 if (x < esurface->x || x > esurface->x + esurface->width)
5349 continue;
5350 if (y < esurface->y || y > esurface->y + esurface->height)
5351 continue;
5352
5353 exposay_highlight_surface(shell, esurface);
5354 return;
5355 }
5356}
5357
5358/* Pretty lame layout for now; just tries to make a square. Should take
5359 * aspect ratio into account really. Also needs to be notified of surface
5360 * addition and removal and adjust layout/animate accordingly. */
5361static enum exposay_layout_state
5362exposay_layout(struct desktop_shell *shell)
5363{
5364 struct workspace *workspace = shell->exposay.workspace;
5365 struct weston_compositor *compositor = shell->compositor;
5366 struct weston_output *output = get_default_output(compositor);
5367 struct weston_view *view;
5368 struct exposay_surface *esurface;
5369 int w, h;
5370 int i;
5371 int last_row_removed = 0;
5372
5373 wl_list_init(&shell->exposay.surface_list);
5374
5375 shell->exposay.num_surfaces = 0;
5376 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5377 if (!get_shell_surface(view->surface))
5378 continue;
5379 shell->exposay.num_surfaces++;
5380 }
5381
5382 if (shell->exposay.num_surfaces == 0) {
5383 shell->exposay.grid_size = 0;
5384 shell->exposay.hpadding_outer = 0;
5385 shell->exposay.vpadding_outer = 0;
5386 shell->exposay.padding_inner = 0;
5387 shell->exposay.surface_size = 0;
5388 return EXPOSAY_LAYOUT_OVERVIEW;
5389 }
5390
5391 /* Lay the grid out as square as possible, losing surfaces from the
5392 * bottom row if required. Start with fixed padding of a 10% margin
5393 * around the outside and 80px internal padding between surfaces, and
5394 * maximise the area made available to surfaces after this, but only
5395 * to a maximum of 1/3rd the total output size.
5396 *
5397 * If we can't make a square grid, add one extra row at the bottom
5398 * which will have a smaller number of columns.
5399 *
5400 * XXX: Surely there has to be a better way to express this maths,
5401 * right?!
5402 */
5403 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5404 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5405 shell->exposay.grid_size++;
5406 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5407
5408 shell->exposay.hpadding_outer = (output->width / 10);
5409 shell->exposay.vpadding_outer = (output->height / 10);
5410 shell->exposay.padding_inner = 80;
5411
5412 w = output->width - (shell->exposay.hpadding_outer * 2);
5413 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5414 w /= shell->exposay.grid_size;
5415
5416 h = output->height - (shell->exposay.vpadding_outer * 2);
5417 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5418 h /= shell->exposay.grid_size;
5419
5420 shell->exposay.surface_size = (w < h) ? w : h;
5421 if (shell->exposay.surface_size > (output->width / 2))
5422 shell->exposay.surface_size = output->width / 2;
5423 if (shell->exposay.surface_size > (output->height / 2))
5424 shell->exposay.surface_size = output->height / 2;
5425
5426 i = 0;
5427 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5428 int pad;
5429
5430 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5431
5432 if (!get_shell_surface(view->surface))
5433 continue;
5434
5435 esurface = malloc(sizeof(*esurface));
5436 if (!esurface) {
5437 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5438 shell->exposay.seat);
5439 break;
5440 }
5441
5442 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5443 esurface->shell = shell;
5444 esurface->view = view;
5445
5446 esurface->row = i / shell->exposay.grid_size;
5447 esurface->column = i % shell->exposay.grid_size;
5448
5449 esurface->x = shell->exposay.hpadding_outer;
5450 esurface->x += pad * esurface->column;
5451 esurface->y = shell->exposay.vpadding_outer;
5452 esurface->y += pad * esurface->row;
5453
5454 if (esurface->row == shell->exposay.grid_size - 1)
5455 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5456
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005457 if (view->surface->width > view->surface->height)
5458 esurface->scale = shell->exposay.surface_size / (float) view->surface->width;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005459 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005460 esurface->scale = shell->exposay.surface_size / (float) view->surface->height;
5461 esurface->width = view->surface->width * esurface->scale;
5462 esurface->height = view->surface->height * esurface->scale;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005463
5464 if (shell->exposay.focus_current == esurface->view)
5465 exposay_highlight_surface(shell, esurface);
5466
5467 exposay_animate_in(esurface);
5468
5469 i++;
5470 }
5471
5472 weston_compositor_schedule_repaint(shell->compositor);
5473
5474 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5475}
5476
5477static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005478exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5479 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005480{
5481 struct desktop_shell *shell =
5482 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5483
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005484 weston_pointer_move(grab->pointer, x, y);
5485
Daniel Stonedf8133b2013-11-19 11:37:14 +01005486 exposay_pick(shell,
5487 wl_fixed_to_int(grab->pointer->x),
5488 wl_fixed_to_int(grab->pointer->y));
5489}
5490
5491static void
5492exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5493 uint32_t state_w)
5494{
5495 struct desktop_shell *shell =
5496 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5497 struct weston_seat *seat = grab->pointer->seat;
5498 enum wl_pointer_button_state state = state_w;
5499
5500 if (button != BTN_LEFT)
5501 return;
5502
5503 /* Store the surface we clicked on, and don't do anything if we end up
5504 * releasing on a different surface. */
5505 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5506 shell->exposay.clicked = shell->exposay.focus_current;
5507 return;
5508 }
5509
5510 if (shell->exposay.focus_current == shell->exposay.clicked)
5511 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5512 else
5513 shell->exposay.clicked = NULL;
5514}
5515
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005516static void
5517exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
5518{
5519 struct desktop_shell *shell =
5520 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5521
5522 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5523}
5524
Daniel Stonedf8133b2013-11-19 11:37:14 +01005525static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5526 noop_grab_focus,
5527 exposay_motion,
5528 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005529 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005530};
5531
5532static int
5533exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5534{
5535 struct exposay_surface *esurface;
5536
5537 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5538 if (esurface->row != row || esurface->column != column)
5539 continue;
5540
5541 exposay_highlight_surface(shell, esurface);
5542 return 1;
5543 }
5544
5545 return 0;
5546}
5547
5548static void
5549exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5550 uint32_t state_w)
5551{
5552 struct weston_seat *seat = grab->keyboard->seat;
5553 struct desktop_shell *shell =
5554 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5555 enum wl_keyboard_key_state state = state_w;
5556
5557 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5558 return;
5559
5560 switch (key) {
5561 case KEY_ESC:
5562 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5563 break;
5564 case KEY_ENTER:
5565 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5566 break;
5567 case KEY_UP:
5568 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5569 shell->exposay.column_current);
5570 break;
5571 case KEY_DOWN:
5572 /* Special case for trying to move to the bottom row when it
5573 * has fewer items than all the others. */
5574 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5575 shell->exposay.column_current) &&
5576 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5577 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5578 (shell->exposay.num_surfaces %
5579 shell->exposay.grid_size) - 1);
5580 }
5581 break;
5582 case KEY_LEFT:
5583 exposay_maybe_move(shell, shell->exposay.row_current,
5584 shell->exposay.column_current - 1);
5585 break;
5586 case KEY_RIGHT:
5587 exposay_maybe_move(shell, shell->exposay.row_current,
5588 shell->exposay.column_current + 1);
5589 break;
5590 case KEY_TAB:
5591 /* Try to move right, then down (and to the leftmost column),
5592 * then if all else fails, to the top left. */
5593 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5594 shell->exposay.column_current + 1) &&
5595 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5596 exposay_maybe_move(shell, 0, 0);
5597 break;
5598 default:
5599 break;
5600 }
5601}
5602
5603static void
5604exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5605 uint32_t mods_depressed, uint32_t mods_latched,
5606 uint32_t mods_locked, uint32_t group)
5607{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005608 struct desktop_shell *shell =
5609 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5610 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5611
5612 /* We want to know when mod has been pressed and released.
5613 * FIXME: There is a problem here: if mod is pressed, then a key
5614 * is pressed and released, then mod is released, we will treat that
5615 * as if only mod had been pressed and released. */
5616 if (seat->modifier_state) {
5617 if (seat->modifier_state == shell->binding_modifier) {
5618 shell->exposay.mod_pressed = true;
5619 } else {
5620 shell->exposay.mod_invalid = true;
5621 }
5622 } else {
5623 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
5624 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5625
5626 shell->exposay.mod_invalid = false;
5627 shell->exposay.mod_pressed = false;
5628 }
5629
5630 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005631}
5632
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005633static void
5634exposay_cancel(struct weston_keyboard_grab *grab)
5635{
5636 struct desktop_shell *shell =
5637 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5638
5639 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5640}
5641
Daniel Stonedf8133b2013-11-19 11:37:14 +01005642static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5643 exposay_key,
5644 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005645 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005646};
5647
5648/**
5649 * Called when the transition from overview -> inactive has completed.
5650 */
5651static enum exposay_layout_state
5652exposay_set_inactive(struct desktop_shell *shell)
5653{
5654 struct weston_seat *seat = shell->exposay.seat;
5655
5656 weston_keyboard_end_grab(seat->keyboard);
5657 weston_pointer_end_grab(seat->pointer);
5658 if (seat->keyboard->input_method_resource)
5659 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5660
5661 return EXPOSAY_LAYOUT_INACTIVE;
5662}
5663
5664/**
5665 * Begins the transition from overview to inactive. */
5666static enum exposay_layout_state
5667exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5668{
5669 struct exposay_surface *esurface;
5670
5671 /* Call activate() before we start the animations to avoid
5672 * animating back the old state and then immediately transitioning
5673 * to the new. */
5674 if (switch_focus && shell->exposay.focus_current)
5675 activate(shell, shell->exposay.focus_current->surface,
5676 shell->exposay.seat);
5677 else if (shell->exposay.focus_prev)
5678 activate(shell, shell->exposay.focus_prev->surface,
5679 shell->exposay.seat);
5680
5681 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5682 exposay_animate_out(esurface);
5683 weston_compositor_schedule_repaint(shell->compositor);
5684
5685 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5686}
5687
5688static enum exposay_layout_state
5689exposay_transition_active(struct desktop_shell *shell)
5690{
5691 struct weston_seat *seat = shell->exposay.seat;
5692
5693 shell->exposay.workspace = get_current_workspace(shell);
5694 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5695 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5696 shell->exposay.clicked = NULL;
5697 wl_list_init(&shell->exposay.surface_list);
5698
5699 lower_fullscreen_layer(shell);
5700 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5701 weston_keyboard_start_grab(seat->keyboard,
5702 &shell->exposay.grab_kbd);
5703 weston_keyboard_set_focus(seat->keyboard, NULL);
5704
5705 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5706 weston_pointer_start_grab(seat->pointer,
5707 &shell->exposay.grab_ptr);
5708 weston_pointer_set_focus(seat->pointer, NULL,
5709 seat->pointer->x, seat->pointer->y);
5710
5711 return exposay_layout(shell);
5712}
5713
5714static void
5715exposay_check_state(struct desktop_shell *shell)
5716{
5717 enum exposay_layout_state state_new = shell->exposay.state_cur;
5718 int do_switch = 0;
5719
5720 /* Don't do anything whilst animations are running, just store up
5721 * target state changes and only act on them when the animations have
5722 * completed. */
5723 if (exposay_is_animating(shell))
5724 return;
5725
5726 switch (shell->exposay.state_target) {
5727 case EXPOSAY_TARGET_OVERVIEW:
5728 switch (shell->exposay.state_cur) {
5729 case EXPOSAY_LAYOUT_OVERVIEW:
5730 goto out;
5731 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5732 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5733 break;
5734 default:
5735 state_new = exposay_transition_active(shell);
5736 break;
5737 }
5738 break;
5739
5740 case EXPOSAY_TARGET_SWITCH:
5741 do_switch = 1; /* fallthrough */
5742 case EXPOSAY_TARGET_CANCEL:
5743 switch (shell->exposay.state_cur) {
5744 case EXPOSAY_LAYOUT_INACTIVE:
5745 goto out;
5746 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5747 state_new = exposay_set_inactive(shell);
5748 break;
5749 default:
5750 state_new = exposay_transition_inactive(shell, do_switch);
5751 break;
5752 }
5753
5754 break;
5755 }
5756
5757out:
5758 shell->exposay.state_cur = state_new;
5759}
5760
5761static void
5762exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5763 struct weston_seat *seat)
5764{
5765 shell->exposay.state_target = state;
5766 shell->exposay.seat = seat;
5767 exposay_check_state(shell);
5768}
5769
5770static void
5771exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5772 void *data)
5773{
5774 struct desktop_shell *shell = data;
5775
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005776 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005777}
5778
Pekka Paalanen3c647232011-12-22 13:43:43 +02005779static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005780backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005781 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005782{
5783 struct weston_compositor *compositor = data;
5784 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005785 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005786
5787 /* TODO: we're limiting to simple use cases, where we assume just
5788 * control on the primary display. We'd have to extend later if we
5789 * ever get support for setting backlights on random desktop LCD
5790 * panels though */
5791 output = get_default_output(compositor);
5792 if (!output)
5793 return;
5794
5795 if (!output->set_backlight)
5796 return;
5797
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005798 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5799 backlight_new = output->backlight_current - 25;
5800 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5801 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005802
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005803 if (backlight_new < 5)
5804 backlight_new = 5;
5805 if (backlight_new > 255)
5806 backlight_new = 255;
5807
5808 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005809 output->set_backlight(output, output->backlight_current);
5810}
5811
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005812struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005813 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005814 struct weston_seat *seat;
5815 uint32_t key[2];
5816 int key_released[2];
5817};
5818
5819static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005820debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005821 uint32_t key, uint32_t state)
5822{
5823 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005824 struct weston_compositor *ec = db->seat->compositor;
5825 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005826 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005827 uint32_t serial;
5828 int send = 0, terminate = 0;
5829 int check_binding = 1;
5830 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005831 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005832
5833 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5834 /* Do not run bindings on key releases */
5835 check_binding = 0;
5836
5837 for (i = 0; i < 2; i++)
5838 if (key == db->key[i])
5839 db->key_released[i] = 1;
5840
5841 if (db->key_released[0] && db->key_released[1]) {
5842 /* All key releases been swalled so end the grab */
5843 terminate = 1;
5844 } else if (key != db->key[0] && key != db->key[1]) {
5845 /* Should not swallow release of other keys */
5846 send = 1;
5847 }
5848 } else if (key == db->key[0] && !db->key_released[0]) {
5849 /* Do not check bindings for the first press of the binding
5850 * key. This allows it to be used as a debug shortcut.
5851 * We still need to swallow this event. */
5852 check_binding = 0;
5853 } else if (db->key[1]) {
5854 /* If we already ran a binding don't process another one since
5855 * we can't keep track of all the binding keys that were
5856 * pressed in order to swallow the release events. */
5857 send = 1;
5858 check_binding = 0;
5859 }
5860
5861 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005862 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5863 key, state)) {
5864 /* We ran a binding so swallow the press and keep the
5865 * grab to swallow the released too. */
5866 send = 0;
5867 terminate = 0;
5868 db->key[1] = key;
5869 } else {
5870 /* Terminate the grab since the key pressed is not a
5871 * debug binding key. */
5872 send = 1;
5873 terminate = 1;
5874 }
5875 }
5876
5877 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005878 serial = wl_display_next_serial(display);
5879 resource_list = &grab->keyboard->focus_resource_list;
5880 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005881 wl_keyboard_send_key(resource, serial, time, key, state);
5882 }
5883 }
5884
5885 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005886 weston_keyboard_end_grab(grab->keyboard);
5887 if (grab->keyboard->input_method_resource)
5888 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005889 free(db);
5890 }
5891}
5892
5893static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005894debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005895 uint32_t mods_depressed, uint32_t mods_latched,
5896 uint32_t mods_locked, uint32_t group)
5897{
5898 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005899 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005900
Neil Roberts96d790e2013-09-19 17:32:00 +01005901 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005902
Neil Roberts96d790e2013-09-19 17:32:00 +01005903 wl_resource_for_each(resource, resource_list) {
5904 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5905 mods_latched, mods_locked, group);
5906 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005907}
5908
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005909static void
5910debug_binding_cancel(struct weston_keyboard_grab *grab)
5911{
5912 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5913
5914 weston_keyboard_end_grab(grab->keyboard);
5915 free(db);
5916}
5917
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005918struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005919 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005920 debug_binding_modifiers,
5921 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005922};
5923
5924static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005925debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005926{
5927 struct debug_binding_grab *grab;
5928
5929 grab = calloc(1, sizeof *grab);
5930 if (!grab)
5931 return;
5932
5933 grab->seat = (struct weston_seat *) seat;
5934 grab->key[0] = key;
5935 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005936 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005937}
5938
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005939static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005940force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005941 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005942{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005943 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005944 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005945 struct desktop_shell *shell = data;
5946 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005947 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005948
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005949 focus_surface = seat->keyboard->focus;
5950 if (!focus_surface)
5951 return;
5952
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005953 wl_signal_emit(&compositor->kill_signal, focus_surface);
5954
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005955 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005956 wl_client_get_credentials(client, &pid, NULL, NULL);
5957
5958 /* Skip clients that we launched ourselves (the credentials of
5959 * the socketpair is ours) */
5960 if (pid == getpid())
5961 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005962
Daniel Stone325fc2d2012-05-30 16:31:58 +01005963 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005964}
5965
5966static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005967workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005968 uint32_t key, void *data)
5969{
5970 struct desktop_shell *shell = data;
5971 unsigned int new_index = shell->workspaces.current;
5972
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005973 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005974 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005975 if (new_index != 0)
5976 new_index--;
5977
5978 change_workspace(shell, new_index);
5979}
5980
5981static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005982workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005983 uint32_t key, void *data)
5984{
5985 struct desktop_shell *shell = data;
5986 unsigned int new_index = shell->workspaces.current;
5987
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005988 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005989 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005990 if (new_index < shell->workspaces.num - 1)
5991 new_index++;
5992
5993 change_workspace(shell, new_index);
5994}
5995
5996static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005997workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005998 uint32_t key, void *data)
5999{
6000 struct desktop_shell *shell = data;
6001 unsigned int new_index;
6002
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006003 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006004 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006005 new_index = key - KEY_F1;
6006 if (new_index >= shell->workspaces.num)
6007 new_index = shell->workspaces.num - 1;
6008
6009 change_workspace(shell, new_index);
6010}
6011
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006012static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006013workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006014 uint32_t key, void *data)
6015{
6016 struct desktop_shell *shell = data;
6017 unsigned int new_index = shell->workspaces.current;
6018
6019 if (shell->locked)
6020 return;
6021
6022 if (new_index != 0)
6023 new_index--;
6024
6025 take_surface_to_workspace_by_seat(shell, seat, new_index);
6026}
6027
6028static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006029workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006030 uint32_t key, void *data)
6031{
6032 struct desktop_shell *shell = data;
6033 unsigned int new_index = shell->workspaces.current;
6034
6035 if (shell->locked)
6036 return;
6037
6038 if (new_index < shell->workspaces.num - 1)
6039 new_index++;
6040
6041 take_surface_to_workspace_by_seat(shell, seat, new_index);
6042}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006043
6044static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006045handle_output_destroy(struct wl_listener *listener, void *data)
6046{
6047 struct shell_output *output_listener =
6048 container_of(listener, struct shell_output, destroy_listener);
6049
6050 wl_list_remove(&output_listener->destroy_listener.link);
6051 wl_list_remove(&output_listener->link);
6052 free(output_listener);
6053}
6054
6055static void
6056create_shell_output(struct desktop_shell *shell,
6057 struct weston_output *output)
6058{
6059 struct shell_output *shell_output;
6060
6061 shell_output = zalloc(sizeof *shell_output);
6062 if (shell_output == NULL)
6063 return;
6064
6065 shell_output->output = output;
6066 shell_output->shell = shell;
6067 shell_output->destroy_listener.notify = handle_output_destroy;
6068 wl_signal_add(&output->destroy_signal,
6069 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006070 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006071}
6072
6073static void
6074handle_output_create(struct wl_listener *listener, void *data)
6075{
6076 struct desktop_shell *shell =
6077 container_of(listener, struct desktop_shell, output_create_listener);
6078 struct weston_output *output = (struct weston_output *)data;
6079
6080 create_shell_output(shell, output);
6081}
6082
6083static void
6084setup_output_destroy_handler(struct weston_compositor *ec,
6085 struct desktop_shell *shell)
6086{
6087 struct weston_output *output;
6088
6089 wl_list_init(&shell->output_list);
6090 wl_list_for_each(output, &ec->output_list, link)
6091 create_shell_output(shell, output);
6092
6093 shell->output_create_listener.notify = handle_output_create;
6094 wl_signal_add(&ec->output_created_signal,
6095 &shell->output_create_listener);
6096}
6097
6098static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006099shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006100{
Tiago Vignattibe143262012-04-16 17:31:41 +03006101 struct desktop_shell *shell =
6102 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006103 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006104 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006105
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006106 if (shell->child.client)
6107 wl_client_destroy(shell->child.client);
6108
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006109 wl_list_remove(&shell->idle_listener.link);
6110 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006111 wl_list_remove(&shell->show_input_panel_listener.link);
6112 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006113
Xiong Zhang6b481422013-10-23 13:58:32 +08006114 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6115 wl_list_remove(&shell_output->destroy_listener.link);
6116 wl_list_remove(&shell_output->link);
6117 free(shell_output);
6118 }
6119
6120 wl_list_remove(&shell->output_create_listener.link);
6121
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006122 wl_array_for_each(ws, &shell->workspaces.array)
6123 workspace_destroy(*ws);
6124 wl_array_release(&shell->workspaces.array);
6125
Pekka Paalanen3c647232011-12-22 13:43:43 +02006126 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006127 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006128 free(shell);
6129}
6130
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006131static void
6132shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6133{
6134 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006135 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006136
6137 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006138 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6139 MODIFIER_CTRL | MODIFIER_ALT,
6140 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01006141 weston_compositor_add_key_binding(ec, KEY_TAB,
6142 MODIFIER_ALT,
6143 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006144 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6145 click_to_activate_binding,
6146 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006147 weston_compositor_add_touch_binding(ec, 0,
6148 touch_to_activate_binding,
6149 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006150 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6151 MODIFIER_SUPER | MODIFIER_ALT,
6152 surface_opacity_binding, NULL);
6153 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6154 MODIFIER_SUPER, zoom_axis_binding,
6155 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006156
6157 /* configurable bindings */
6158 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006159 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6160 zoom_key_binding, NULL);
6161 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6162 zoom_key_binding, NULL);
6163 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6164 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006165 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006166 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6167 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006168
6169 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6170 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6171 rotate_binding, NULL);
6172
Daniel Stone325fc2d2012-05-30 16:31:58 +01006173 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6174 shell);
6175 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6176 ec);
6177 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6178 backlight_binding, ec);
6179 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6180 ec);
6181 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6182 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006183 weston_compositor_add_key_binding(ec, KEY_K, mod,
6184 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006185 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6186 workspace_up_binding, shell);
6187 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6188 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006189 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6190 workspace_move_surface_up_binding,
6191 shell);
6192 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6193 workspace_move_surface_down_binding,
6194 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006195
Daniel Stonedf8133b2013-11-19 11:37:14 +01006196 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6197
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006198 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6199 if (shell->workspaces.num > 1) {
6200 num_workspace_bindings = shell->workspaces.num;
6201 if (num_workspace_bindings > 6)
6202 num_workspace_bindings = 6;
6203 for (i = 0; i < num_workspace_bindings; i++)
6204 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6205 workspace_f_binding,
6206 shell);
6207 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006208
6209 /* Debug bindings */
6210 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6211 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006212}
6213
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006214WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006215module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006216 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006217{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006218 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006219 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006220 struct workspace **pws;
6221 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006222 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006223
Peter Huttererf3d62272013-08-08 11:57:05 +10006224 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006225 if (shell == NULL)
6226 return -1;
6227
Kristian Høgsberg75840622011-09-06 13:48:16 -04006228 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006229
6230 shell->destroy_listener.notify = shell_destroy;
6231 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006232 shell->idle_listener.notify = idle_handler;
6233 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6234 shell->wake_listener.notify = wake_handler;
6235 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006236 shell->show_input_panel_listener.notify = show_input_panels;
6237 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6238 shell->hide_input_panel_listener.notify = hide_input_panels;
6239 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006240 shell->update_input_panel_listener.notify = update_input_panels;
6241 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006242 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006243 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006244 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006245 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006246 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006247 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006248 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006249 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006250 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006251 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006252 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006253
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006254 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006255
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006256 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6257 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006258 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6259 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006260 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006261
6262 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006263 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006264
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006265 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006266
Daniel Stonedf8133b2013-11-19 11:37:14 +01006267 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6268 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6269
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006270 for (i = 0; i < shell->workspaces.num; i++) {
6271 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6272 if (pws == NULL)
6273 return -1;
6274
6275 *pws = workspace_create();
6276 if (*pws == NULL)
6277 return -1;
6278 }
6279 activate_workspace(shell, 0);
6280
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006281 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006282 wl_list_init(&shell->workspaces.animation.link);
6283 shell->workspaces.animation.frame = animate_workspace_change_frame;
6284
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006285 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006286 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006287 return -1;
6288
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006289 if (wl_global_create(ec->wl_display,
6290 &desktop_shell_interface, 2,
6291 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006292 return -1;
6293
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006294 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6295 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006296 return -1;
6297
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006298 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006299 shell, bind_input_panel) == NULL)
6300 return -1;
6301
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006302 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6303 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006304 return -1;
6305
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006306 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006307
Xiong Zhang6b481422013-10-23 13:58:32 +08006308 setup_output_destroy_handler(ec, shell);
6309
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006310 loop = wl_display_get_event_loop(ec->wl_display);
6311 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006312
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006313 shell->screensaver.timer =
6314 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6315
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006316 wl_list_for_each(seat, &ec->seat_list, link)
6317 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006318
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006319 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006320
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006321 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006322
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006323 return 0;
6324}