blob: 6483e69ee086655d083756e07d65e8bb8534825d [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,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300242 SHELL_SURFACE_POPUP,
243 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200244};
245
Scott Moreauff1db4a2012-04-17 19:06:18 -0600246struct ping_timer {
247 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600248 uint32_t serial;
249};
250
Philip Withnall648a4dd2013-11-25 18:01:44 +0000251/*
252 * Surface stacking and ordering.
253 *
254 * This is handled using several linked lists of surfaces, organised into
255 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
256 * above all of the surfaces in the layer below. The set of layers is static and
257 * in the following order (top-most first):
258 * • Lock layer (only ever displayed on its own)
259 * • Cursor layer
260 * • Fullscreen layer
261 * • Panel layer
262 * • Input panel layer
263 * • Workspace layers
264 * • Background layer
265 *
266 * The list of layers may be manipulated to remove whole layers of surfaces from
267 * display. For example, when locking the screen, all layers except the lock
268 * layer are removed.
269 *
270 * A surface’s layer is modified on configuring the surface, in
271 * set_surface_type() (which is only called when the surface’s type change is
272 * _committed_). If a surface’s type changes (e.g. when making a window
273 * fullscreen) its layer changes too.
274 *
275 * In order to allow popup and transient surfaces to be correctly stacked above
276 * their parent surfaces, each surface tracks both its parent surface, and a
277 * linked list of its children. When a surface’s layer is updated, so are the
278 * layers of its children. Note that child surfaces are *not* the same as
279 * subsurfaces — child/parent surfaces are purely for maintaining stacking
280 * order.
281 *
282 * The children_link list of siblings of a surface (i.e. those surfaces which
283 * have the same parent) only contains weston_surfaces which have a
284 * shell_surface. Stacking is not implemented for non-shell_surface
285 * weston_surfaces. This means that the following implication does *not* hold:
286 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
287 */
288
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200289struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500290 struct wl_resource *resource;
291 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200292
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500293 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500294 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600295 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200296 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400297 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000298 struct wl_list children_list; /* child surfaces of this one */
299 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300300 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200301
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400302 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400303 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500304 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800305 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800306 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700307 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100308
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500309 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200310 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500311 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200312 } rotation;
313
314 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100315 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500316 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100317 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400318 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500319 } popup;
320
Alex Wu4539b082012-03-01 12:57:46 +0800321 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300322 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400323 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300324 } transient;
325
326 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800327 enum wl_shell_surface_fullscreen_method type;
328 struct weston_transform transform; /* matrix from x, y */
329 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500330 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800331 } fullscreen;
332
Scott Moreauff1db4a2012-04-17 19:06:18 -0600333 struct ping_timer *ping_timer;
334
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200335 struct weston_transform workspace_transform;
336
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500337 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500338 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100339 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400340
341 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200342
343 struct {
344 bool maximized;
345 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200346 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200347 } 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
Rafael Antognolli03b16592013-12-03 15:35:42 -02002124 case SHELL_SURFACE_TOPLEVEL: {
Rafael Antognollied207b42013-12-03 15:35:43 -02002125 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002126 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002127 } else if (shsurf->parent) {
2128 /* Move the surface to its parent layer so that
2129 * surfaces which are transient for fullscreen surfaces
2130 * don't get hidden by the fullscreen surfaces.
2131 * However, unlike popups, transient surfaces are
2132 * stacked in front of their parent but not in front of
2133 * other surfaces of the same type. */
2134 struct weston_view *parent;
2135
2136 /* TODO: Handle a parent with multiple views */
2137 parent = get_default_view(shsurf->parent);
2138 if (parent)
2139 return parent->layer_link.prev;
2140 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002141 break;
2142 }
Philip Withnall07926d92013-11-25 18:01:40 +00002143
2144 case SHELL_SURFACE_XWAYLAND:
Philip Withnall07926d92013-11-25 18:01:40 +00002145 case SHELL_SURFACE_NONE:
2146 default:
2147 /* Go to the fallback, below. */
2148 break;
2149 }
2150
2151 /* Move the surface to a normal workspace layer so that surfaces
2152 * which were previously fullscreen or transient are no longer
2153 * rendered on top. */
2154 ws = get_current_workspace(shsurf->shell);
2155 return &ws->layer.view_list;
2156}
2157
Philip Withnall648a4dd2013-11-25 18:01:44 +00002158static void
2159shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2160{
2161 struct shell_surface *child;
2162
2163 /* Move the child layers to the same workspace as shsurf. They will be
2164 * stacked above shsurf. */
2165 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2166 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2167 weston_view_geometry_dirty(child->view);
2168 wl_list_remove(&child->view->layer_link);
2169 wl_list_insert(shsurf->view->layer_link.prev,
2170 &child->view->layer_link);
2171 weston_view_geometry_dirty(child->view);
2172 weston_surface_damage(child->surface);
2173
2174 /* Recurse. We don’t expect this to recurse very far (if
2175 * at all) because that would imply we have transient
2176 * (or popup) children of transient surfaces, which
2177 * would be unusual. */
2178 shell_surface_update_child_surface_layers(child);
2179 }
2180 }
2181}
2182
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002183/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002184 * geometry to ensure the changes are redrawn.
2185 *
2186 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2187 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002188static void
2189shell_surface_update_layer(struct shell_surface *shsurf)
2190{
2191 struct wl_list *new_layer_link;
2192
2193 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2194
2195 if (new_layer_link == &shsurf->view->layer_link)
2196 return;
2197
2198 weston_view_geometry_dirty(shsurf->view);
2199 wl_list_remove(&shsurf->view->layer_link);
2200 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2201 weston_view_geometry_dirty(shsurf->view);
2202 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002203
2204 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002205}
2206
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002207static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002208shell_surface_set_parent(struct shell_surface *shsurf,
2209 struct weston_surface *parent)
2210{
2211 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002212
2213 wl_list_remove(&shsurf->children_link);
2214 wl_list_init(&shsurf->children_link);
2215
2216 /* Insert into the parent surface’s child list. */
2217 if (parent != NULL) {
2218 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2219 if (parent_shsurf != NULL)
2220 wl_list_insert(&parent_shsurf->children_list,
2221 &shsurf->children_link);
2222 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002223}
2224
2225static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002226shell_surface_set_output(struct shell_surface *shsurf,
2227 struct weston_output *output)
2228{
2229 struct weston_surface *es = shsurf->surface;
2230
2231 /* get the default output, if the client set it as NULL
2232 check whether the ouput is available */
2233 if (output)
2234 shsurf->output = output;
2235 else if (es->output)
2236 shsurf->output = es->output;
2237 else
2238 shsurf->output = get_default_output(es->compositor);
2239}
2240
2241static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002242surface_clear_next_states(struct shell_surface *shsurf)
2243{
2244 shsurf->next_state.maximized = false;
2245 shsurf->next_state.fullscreen = false;
2246
2247 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2248 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2249 shsurf->state_changed = true;
2250}
2251
2252static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002253set_toplevel(struct shell_surface *shsurf)
2254{
2255 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002256
2257 /* The layer_link is updated in set_surface_type(),
2258 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002259}
2260
2261static void
2262shell_surface_set_toplevel(struct wl_client *client,
2263 struct wl_resource *resource)
2264{
2265 struct shell_surface *surface = wl_resource_get_user_data(resource);
2266
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002267 shell_surface_set_parent(surface, NULL);
2268
Rafael Antognolli03b16592013-12-03 15:35:42 -02002269 surface_clear_next_states(surface);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002270 set_toplevel(surface);
2271}
2272
2273static void
2274set_transient(struct shell_surface *shsurf,
2275 struct weston_surface *parent, int x, int y, uint32_t flags)
2276{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002277 assert(parent != NULL);
2278
Philip Withnallbecb77e2013-11-25 18:01:30 +00002279 shsurf->transient.x = x;
2280 shsurf->transient.y = y;
2281 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002282
Rafael Antognollied207b42013-12-03 15:35:43 -02002283 shsurf->next_state.relative = true;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002284
2285 /* The layer_link is updated in set_surface_type(),
2286 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002287}
2288
2289static void
2290shell_surface_set_transient(struct wl_client *client,
2291 struct wl_resource *resource,
2292 struct wl_resource *parent_resource,
2293 int x, int y, uint32_t flags)
2294{
2295 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2296 struct weston_surface *parent =
2297 wl_resource_get_user_data(parent_resource);
2298
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002299 shell_surface_set_parent(shsurf, parent);
2300
Rafael Antognolli03b16592013-12-03 15:35:42 -02002301 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002302 set_transient(shsurf, parent, x, y, flags);
2303}
2304
2305static void
2306set_fullscreen(struct shell_surface *shsurf,
2307 uint32_t method,
2308 uint32_t framerate,
2309 struct weston_output *output)
2310{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002311 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002312
2313 shsurf->fullscreen_output = shsurf->output;
2314 shsurf->fullscreen.type = method;
2315 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002316
Rafael Antognolli03b16592013-12-03 15:35:42 -02002317 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002318
2319 shsurf->client->send_configure(shsurf->surface, 0,
2320 shsurf->output->width,
2321 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002322
2323 /* The layer_link is updated in set_surface_type(),
2324 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002325}
2326
2327static void
2328unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002329{
Philip Withnallf85fe842013-11-25 18:01:37 +00002330 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002331 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2332 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002333 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002334 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002335 shsurf->fullscreen_output = NULL;
2336
Alex Wu4539b082012-03-01 12:57:46 +08002337 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2338 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002339
Alex Wu4539b082012-03-01 12:57:46 +08002340 wl_list_remove(&shsurf->fullscreen.transform.link);
2341 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002342
Jason Ekstranda7af7042013-10-12 22:38:11 -05002343 if (shsurf->fullscreen.black_view)
2344 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2345 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002346
Jason Ekstranda7af7042013-10-12 22:38:11 -05002347 weston_view_set_position(shsurf->view,
2348 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002349 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002350 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002351 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002352 shsurf->saved_rotation_valid = false;
2353 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002354
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002355 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002356}
2357
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002358static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002359shell_surface_set_fullscreen(struct wl_client *client,
2360 struct wl_resource *resource,
2361 uint32_t method,
2362 uint32_t framerate,
2363 struct wl_resource *output_resource)
2364{
2365 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2366 struct weston_output *output;
2367
2368 if (output_resource)
2369 output = wl_resource_get_user_data(output_resource);
2370 else
2371 output = NULL;
2372
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002373 shell_surface_set_parent(shsurf, NULL);
2374
Rafael Antognolli03b16592013-12-03 15:35:42 -02002375 surface_clear_next_states(shsurf);
2376 shsurf->next_state.fullscreen = true;
2377 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002378 set_fullscreen(shsurf, method, framerate, output);
2379}
2380
2381static void
2382set_popup(struct shell_surface *shsurf,
2383 struct weston_surface *parent,
2384 struct weston_seat *seat,
2385 uint32_t serial,
2386 int32_t x,
2387 int32_t y)
2388{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002389 assert(parent != NULL);
2390
Philip Withnallbecb77e2013-11-25 18:01:30 +00002391 shsurf->popup.shseat = get_shell_seat(seat);
2392 shsurf->popup.serial = serial;
2393 shsurf->popup.x = x;
2394 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002395
Philip Withnallb995e1d2013-11-25 18:01:39 +00002396 shsurf->next_type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002397}
2398
2399static void
2400shell_surface_set_popup(struct wl_client *client,
2401 struct wl_resource *resource,
2402 struct wl_resource *seat_resource,
2403 uint32_t serial,
2404 struct wl_resource *parent_resource,
2405 int32_t x, int32_t y, uint32_t flags)
2406{
2407 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002408 struct weston_surface *parent =
2409 wl_resource_get_user_data(parent_resource);
2410
2411 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002412
Rafael Antognolli03b16592013-12-03 15:35:42 -02002413 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002414 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002415 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002416 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
Rafael Antognolli03b16592013-12-03 15:35:42 -02002437 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002438}
2439
2440static void
2441unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002442{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002443 /* undo all maximized things here */
2444 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002445 weston_view_set_position(shsurf->view,
2446 shsurf->saved_x,
2447 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002448
2449 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002450 wl_list_insert(&shsurf->view->geometry.transformation_list,
2451 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002452 shsurf->saved_rotation_valid = false;
2453 }
2454
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002455 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002456}
2457
Philip Withnallbecb77e2013-11-25 18:01:30 +00002458static void
2459shell_surface_set_maximized(struct wl_client *client,
2460 struct wl_resource *resource,
2461 struct wl_resource *output_resource)
2462{
2463 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2464 struct weston_output *output;
2465
2466 if (output_resource)
2467 output = wl_resource_get_user_data(output_resource);
2468 else
2469 output = NULL;
2470
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002471 shell_surface_set_parent(shsurf, 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 Antognollied207b42013-12-03 15:35:43 -02002523 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002524 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002525 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002526 weston_view_set_position(shsurf->view,
2527 pev->geometry.x + shsurf->transient.x,
2528 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002529 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002530
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002531 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002532 weston_view_set_position(shsurf->view, shsurf->transient.x,
2533 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002534 break;
2535
Philip Withnall0f640e22013-11-25 18:01:31 +00002536 case SHELL_SURFACE_POPUP:
2537 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002538 default:
2539 break;
2540 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002541
2542 /* Update the surface’s layer. */
2543 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002544}
2545
Tiago Vignattibe143262012-04-16 17:31:41 +03002546static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002547shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002548{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002549 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002550}
2551
Alex Wu21858432012-04-01 20:13:08 +08002552static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002553black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002554
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002556create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002557 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002558 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002559{
2560 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002561 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002562
2563 surface = weston_surface_create(ec);
2564 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002565 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002566 return NULL;
2567 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 view = weston_view_create(surface);
2569 if (surface == NULL) {
2570 weston_log("no memory\n");
2571 weston_surface_destroy(surface);
2572 return NULL;
2573 }
Alex Wu4539b082012-03-01 12:57:46 +08002574
Alex Wu21858432012-04-01 20:13:08 +08002575 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002576 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002577 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002578 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002579 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002580 pixman_region32_fini(&surface->input);
2581 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002582
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002583 surface->width = w;
2584 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002585 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002586
2587 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002588}
2589
Philip Withnalled8f1a92013-11-25 18:01:43 +00002590static void
2591shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2592{
2593 struct weston_output *output = shsurf->fullscreen_output;
2594
Rafael Antognolli03b16592013-12-03 15:35:42 -02002595 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002596
2597 if (!shsurf->fullscreen.black_view)
2598 shsurf->fullscreen.black_view =
2599 create_black_surface(shsurf->surface->compositor,
2600 shsurf->surface,
2601 output->x, output->y,
2602 output->width,
2603 output->height);
2604
2605 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2606 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2607 wl_list_insert(&shsurf->view->layer_link,
2608 &shsurf->fullscreen.black_view->layer_link);
2609 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2610 weston_surface_damage(shsurf->surface);
2611}
2612
Alex Wu4539b082012-03-01 12:57:46 +08002613/* Create black surface and append it to the associated fullscreen surface.
2614 * Handle size dismatch and positioning according to the method. */
2615static void
2616shell_configure_fullscreen(struct shell_surface *shsurf)
2617{
2618 struct weston_output *output = shsurf->fullscreen_output;
2619 struct weston_surface *surface = shsurf->surface;
2620 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002621 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002622 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002623
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002624 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2625 restore_output_mode(output);
2626
Philip Withnalled8f1a92013-11-25 18:01:43 +00002627 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002628
Jason Ekstranda7af7042013-10-12 22:38:11 -05002629 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002630 &surf_width, &surf_height);
2631
Alex Wu4539b082012-03-01 12:57:46 +08002632 switch (shsurf->fullscreen.type) {
2633 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002634 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002635 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002636 break;
2637 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002638 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002639 if (output->width == surf_width &&
2640 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641 weston_view_set_position(shsurf->view,
2642 output->x - surf_x,
2643 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002644 break;
2645 }
2646
Alex Wu4539b082012-03-01 12:57:46 +08002647 matrix = &shsurf->fullscreen.transform.matrix;
2648 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002649
Scott Moreau1bad5db2012-08-18 01:04:05 -06002650 output_aspect = (float) output->width /
2651 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002652 /* XXX: Use surf_width and surf_height here? */
2653 surface_aspect = (float) surface->width /
2654 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002655 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002656 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002657 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002658 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002659 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002660 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002661
Alex Wu4539b082012-03-01 12:57:46 +08002662 weston_matrix_scale(matrix, scale, scale, 1);
2663 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002665 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002666 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2667 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002668 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002669
Alex Wu4539b082012-03-01 12:57:46 +08002670 break;
2671 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002672 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002673 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002674 surf_width * surface->buffer_viewport.scale,
2675 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002676 shsurf->fullscreen.framerate};
2677
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002678 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002679 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002680 weston_view_set_position(shsurf->view,
2681 output->x - surf_x,
2682 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002683 shsurf->fullscreen.black_view->surface->width = output->width;
2684 shsurf->fullscreen.black_view->surface->height = output->height;
2685 weston_view_set_position(shsurf->fullscreen.black_view,
2686 output->x - surf_x,
2687 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002688 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002689 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002690 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002691 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002692 }
Alex Wubd3354b2012-04-17 17:20:49 +08002693 }
Alex Wu4539b082012-03-01 12:57:46 +08002694 break;
2695 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002696 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002697 break;
2698 default:
2699 break;
2700 }
2701}
2702
Alex Wu4539b082012-03-01 12:57:46 +08002703static void
2704shell_map_fullscreen(struct shell_surface *shsurf)
2705{
Alex Wubd3354b2012-04-17 17:20:49 +08002706 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002707}
2708
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002709static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002710set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2711{
2712 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002713 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002714 shsurf->transient.x = x;
2715 shsurf->transient.y = y;
2716 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002717
2718 shell_surface_set_parent(shsurf, NULL);
2719
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002720 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2721}
2722
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002723static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002724
2725static void
2726destroy_shell_seat(struct wl_listener *listener, void *data)
2727{
2728 struct shell_seat *shseat =
2729 container_of(listener,
2730 struct shell_seat, seat_destroy_listener);
2731 struct shell_surface *shsurf, *prev = NULL;
2732
2733 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002734 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002735 shseat->popup_grab.client = NULL;
2736
2737 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2738 shsurf->popup.shseat = NULL;
2739 if (prev) {
2740 wl_list_init(&prev->popup.grab_link);
2741 }
2742 prev = shsurf;
2743 }
2744 wl_list_init(&prev->popup.grab_link);
2745 }
2746
2747 wl_list_remove(&shseat->seat_destroy_listener.link);
2748 free(shseat);
2749}
2750
2751static struct shell_seat *
2752create_shell_seat(struct weston_seat *seat)
2753{
2754 struct shell_seat *shseat;
2755
2756 shseat = calloc(1, sizeof *shseat);
2757 if (!shseat) {
2758 weston_log("no memory to allocate shell seat\n");
2759 return NULL;
2760 }
2761
2762 shseat->seat = seat;
2763 wl_list_init(&shseat->popup_grab.surfaces_list);
2764
2765 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2766 wl_signal_add(&seat->destroy_signal,
2767 &shseat->seat_destroy_listener);
2768
2769 return shseat;
2770}
2771
2772static struct shell_seat *
2773get_shell_seat(struct weston_seat *seat)
2774{
2775 struct wl_listener *listener;
2776
2777 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2778 if (listener == NULL)
2779 return create_shell_seat(seat);
2780
2781 return container_of(listener,
2782 struct shell_seat, seat_destroy_listener);
2783}
2784
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002785static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002786popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002787{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002788 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002789 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002790 struct shell_seat *shseat =
2791 container_of(grab, struct shell_seat, popup_grab.grab);
2792 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002793 wl_fixed_t sx, sy;
2794
Jason Ekstranda7af7042013-10-12 22:38:11 -05002795 view = weston_compositor_pick_view(pointer->seat->compositor,
2796 pointer->x, pointer->y,
2797 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002798
Jason Ekstranda7af7042013-10-12 22:38:11 -05002799 if (view && view->surface->resource &&
2800 wl_resource_get_client(view->surface->resource) == client) {
2801 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002802 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002803 weston_pointer_set_focus(pointer, NULL,
2804 wl_fixed_from_int(0),
2805 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002806 }
2807}
2808
2809static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002810popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2811 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002812{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002813 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002814 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002815 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002816
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002817 weston_pointer_move(pointer, x, y);
2818
Neil Roberts96d790e2013-09-19 17:32:00 +01002819 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 weston_view_from_global_fixed(pointer->focus,
2821 pointer->x, pointer->y,
2822 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002823 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002824 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002825}
2826
2827static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002828popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002829 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002830{
2831 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002832 struct shell_seat *shseat =
2833 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002834 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002835 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002836 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002837 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002838
Neil Roberts96d790e2013-09-19 17:32:00 +01002839 resource_list = &grab->pointer->focus_resource_list;
2840 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002841 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002842 wl_resource_for_each(resource, resource_list) {
2843 wl_pointer_send_button(resource, serial,
2844 time, button, state);
2845 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002846 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002847 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002848 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002849 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002850 }
2851
Daniel Stone4dbadb12012-05-30 16:31:51 +01002852 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002853 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002854}
2855
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002856static void
2857popup_grab_cancel(struct weston_pointer_grab *grab)
2858{
2859 popup_grab_end(grab->pointer);
2860}
2861
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002862static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002863 popup_grab_focus,
2864 popup_grab_motion,
2865 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002866 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002867};
2868
2869static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002870popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002871{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002872 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002873 struct shell_seat *shseat =
2874 container_of(grab, struct shell_seat, popup_grab.grab);
2875 struct shell_surface *shsurf;
2876 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002877
2878 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002879 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002880 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002881 shseat->popup_grab.grab.interface = NULL;
2882 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002883 /* Send the popup_done event to all the popups open */
2884 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002885 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002886 shsurf->popup.shseat = NULL;
2887 if (prev) {
2888 wl_list_init(&prev->popup.grab_link);
2889 }
2890 prev = shsurf;
2891 }
2892 wl_list_init(&prev->popup.grab_link);
2893 wl_list_init(&shseat->popup_grab.surfaces_list);
2894 }
2895}
2896
2897static void
2898add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2899{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002900 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002901
2902 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002903 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002904 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002905 /* We must make sure here that this popup was opened after
2906 * a mouse press, and not just by moving around with other
2907 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002908 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002909 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002910
Rob Bradforddfe31052013-06-26 19:49:11 +01002911 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002912 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002913 } else {
2914 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002915 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002916}
2917
2918static void
2919remove_popup_grab(struct shell_surface *shsurf)
2920{
2921 struct shell_seat *shseat = shsurf->popup.shseat;
2922
2923 wl_list_remove(&shsurf->popup.grab_link);
2924 wl_list_init(&shsurf->popup.grab_link);
2925 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002926 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002927 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002928 }
2929}
2930
2931static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002932shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002933{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002934 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002936
Jason Ekstranda7af7042013-10-12 22:38:11 -05002937 shsurf->surface->output = parent_view->output;
2938 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002939
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 weston_view_set_transform_parent(shsurf->view, parent_view);
2941 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2942 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002943
Kristian Høgsberge3148752013-05-06 23:19:49 -04002944 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002945 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002946 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002947 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002948 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002949 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002950}
2951
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002952static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002953 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002954 shell_surface_move,
2955 shell_surface_resize,
2956 shell_surface_set_toplevel,
2957 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002958 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002959 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002960 shell_surface_set_maximized,
2961 shell_surface_set_title,
2962 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002963};
2964
2965static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002966destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002967{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002968 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2969
Giulio Camuffo5085a752013-03-25 21:42:45 +01002970 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2971 remove_popup_grab(shsurf);
2972 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002973
Alex Wubd3354b2012-04-17 17:20:49 +08002974 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002975 shell_surface_is_top_fullscreen(shsurf))
2976 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002977
Jason Ekstranda7af7042013-10-12 22:38:11 -05002978 if (shsurf->fullscreen.black_view)
2979 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002980
Alex Wubd3354b2012-04-17 17:20:49 +08002981 /* As destroy_resource() use wl_list_for_each_safe(),
2982 * we can always remove the listener.
2983 */
2984 wl_list_remove(&shsurf->surface_destroy_listener.link);
2985 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002986 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002987 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002988
Jason Ekstranda7af7042013-10-12 22:38:11 -05002989 weston_view_destroy(shsurf->view);
2990
Philip Withnall648a4dd2013-11-25 18:01:44 +00002991 wl_list_remove(&shsurf->children_link);
2992
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002993 wl_list_remove(&shsurf->link);
2994 free(shsurf);
2995}
2996
2997static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002998shell_destroy_shell_surface(struct wl_resource *resource)
2999{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003000 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003001
3002 destroy_shell_surface(shsurf);
3003}
3004
3005static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003006shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003007{
3008 struct shell_surface *shsurf = container_of(listener,
3009 struct shell_surface,
3010 surface_destroy_listener);
3011
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003012 if (shsurf->resource)
3013 wl_resource_destroy(shsurf->resource);
3014 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003015 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003016}
3017
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003018static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003019shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003020
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003021static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003022get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003023{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003024 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003025 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003026 else
3027 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003028}
3029
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003030static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003031create_shell_surface(void *shell, struct weston_surface *surface,
3032 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003033{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003034 struct shell_surface *shsurf;
3035
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003036 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003037 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003038 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003039 }
3040
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003041 shsurf = calloc(1, sizeof *shsurf);
3042 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003043 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003044 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003045 }
3046
Jason Ekstranda7af7042013-10-12 22:38:11 -05003047 shsurf->view = weston_view_create(surface);
3048 if (!shsurf->view) {
3049 weston_log("no memory to allocate shell surface\n");
3050 free(shsurf);
3051 return NULL;
3052 }
3053
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003054 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003055 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003056
Tiago Vignattibc052c92012-04-19 16:18:18 +03003057 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003058 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003059 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003060 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003061 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003062 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3063 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003064 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003065 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003066 wl_list_init(&shsurf->fullscreen.transform.link);
3067
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003068 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003069 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003070 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003071 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003072
3073 /* init link so its safe to always remove it in destroy_shell_surface */
3074 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003075 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003076
Pekka Paalanen460099f2012-01-20 16:48:25 +02003077 /* empty when not in use */
3078 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003079 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003080
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003081 wl_list_init(&shsurf->workspace_transform.link);
3082
Philip Withnall648a4dd2013-11-25 18:01:44 +00003083 wl_list_init(&shsurf->children_link);
3084 wl_list_init(&shsurf->children_list);
3085 shsurf->parent = NULL;
3086
Pekka Paalanen98262232011-12-01 10:42:22 +02003087 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003088 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003089
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003090 shsurf->client = client;
3091
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003092 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003093}
3094
Jason Ekstranda7af7042013-10-12 22:38:11 -05003095static struct weston_view *
3096get_primary_view(void *shell, struct shell_surface *shsurf)
3097{
3098 return shsurf->view;
3099}
3100
Tiago Vignattibc052c92012-04-19 16:18:18 +03003101static void
3102shell_get_shell_surface(struct wl_client *client,
3103 struct wl_resource *resource,
3104 uint32_t id,
3105 struct wl_resource *surface_resource)
3106{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003107 struct weston_surface *surface =
3108 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003109 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003110 struct shell_surface *shsurf;
3111
3112 if (get_shell_surface(surface)) {
3113 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003114 WL_DISPLAY_ERROR_INVALID_OBJECT,
3115 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003116 return;
3117 }
3118
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003119 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003120 if (!shsurf) {
3121 wl_resource_post_error(surface_resource,
3122 WL_DISPLAY_ERROR_INVALID_OBJECT,
3123 "surface->configure already set");
3124 return;
3125 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003126
Jason Ekstranda85118c2013-06-27 20:17:02 -05003127 shsurf->resource =
3128 wl_resource_create(client,
3129 &wl_shell_surface_interface, 1, id);
3130 wl_resource_set_implementation(shsurf->resource,
3131 &shell_surface_implementation,
3132 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003133}
3134
3135static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003136 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003137};
3138
Kristian Høgsberg07937562011-04-12 17:25:42 -04003139static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003140shell_fade(struct desktop_shell *shell, enum fade_type type);
3141
3142static int
3143screensaver_timeout(void *data)
3144{
3145 struct desktop_shell *shell = data;
3146
3147 shell_fade(shell, FADE_OUT);
3148
3149 return 1;
3150}
3151
3152static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003153handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003154{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003155 struct desktop_shell *shell =
3156 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003157
Pekka Paalanen18027e52011-12-02 16:31:49 +02003158 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003159
3160 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003161 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003162}
3163
3164static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003165launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003166{
3167 if (shell->screensaver.binding)
3168 return;
3169
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003170 if (!shell->screensaver.path) {
3171 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003172 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003173 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003174
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003175 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003176 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003177 return;
3178 }
3179
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003180 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003181 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003182 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003183 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003184}
3185
3186static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003187terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003188{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003189 if (shell->screensaver.process.pid == 0)
3190 return;
3191
3192 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003193}
3194
3195static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003196configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003197{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003198 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003199
Jason Ekstranda7af7042013-10-12 22:38:11 -05003200 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3201 if (v->output == ev->output && v != ev) {
3202 weston_view_unmap(v);
3203 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003204 }
3205 }
3206
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003207 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003208
Jason Ekstranda7af7042013-10-12 22:38:11 -05003209 if (wl_list_empty(&ev->layer_link)) {
3210 wl_list_insert(&layer->view_list, &ev->layer_link);
3211 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003212 }
3213}
3214
3215static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003216background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003217{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003218 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003219 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003220
Jason Ekstranda7af7042013-10-12 22:38:11 -05003221 view = container_of(es->views.next, struct weston_view, surface_link);
3222
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003223 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003224}
3225
3226static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003227desktop_shell_set_background(struct wl_client *client,
3228 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003229 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003230 struct wl_resource *surface_resource)
3231{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003232 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003233 struct weston_surface *surface =
3234 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003235 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003236
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003237 if (surface->configure) {
3238 wl_resource_post_error(surface_resource,
3239 WL_DISPLAY_ERROR_INVALID_OBJECT,
3240 "surface role already assigned");
3241 return;
3242 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003243
Jason Ekstranda7af7042013-10-12 22:38:11 -05003244 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3245 weston_view_destroy(view);
3246 view = weston_view_create(surface);
3247
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003248 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003249 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003250 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003251 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003252 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003253 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003254 surface->output->width,
3255 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003256}
3257
3258static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003259panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003260{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003261 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003262 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003263
Jason Ekstranda7af7042013-10-12 22:38:11 -05003264 view = container_of(es->views.next, struct weston_view, surface_link);
3265
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003266 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003267}
3268
3269static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003270desktop_shell_set_panel(struct wl_client *client,
3271 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003272 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003273 struct wl_resource *surface_resource)
3274{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003275 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003276 struct weston_surface *surface =
3277 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003278 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003279
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003280 if (surface->configure) {
3281 wl_resource_post_error(surface_resource,
3282 WL_DISPLAY_ERROR_INVALID_OBJECT,
3283 "surface role already assigned");
3284 return;
3285 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003286
Jason Ekstranda7af7042013-10-12 22:38:11 -05003287 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3288 weston_view_destroy(view);
3289 view = weston_view_create(surface);
3290
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003291 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003292 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003293 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003294 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003295 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003296 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003297 surface->output->width,
3298 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003299}
3300
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003301static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003302lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003303{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003304 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003305 struct weston_view *view;
3306
3307 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003308
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003309 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003310 return;
3311
Jason Ekstranda7af7042013-10-12 22:38:11 -05003312 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003313
3314 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003315 wl_list_insert(&shell->lock_layer.view_list,
3316 &view->layer_link);
3317 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003318 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003319 }
3320}
3321
3322static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003323handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003324{
Tiago Vignattibe143262012-04-16 17:31:41 +03003325 struct desktop_shell *shell =
3326 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003327
Martin Minarik6d118362012-06-07 18:01:59 +02003328 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003329 shell->lock_surface = NULL;
3330}
3331
3332static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003333desktop_shell_set_lock_surface(struct wl_client *client,
3334 struct wl_resource *resource,
3335 struct wl_resource *surface_resource)
3336{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003337 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003338 struct weston_surface *surface =
3339 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003340
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003341 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003342
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003343 if (!shell->locked)
3344 return;
3345
Pekka Paalanen98262232011-12-01 10:42:22 +02003346 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003347
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003348 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003349 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003350 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003351
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003352 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003353 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003354 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003355}
3356
3357static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003358resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003359{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003360 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003361
Pekka Paalanen77346a62011-11-30 16:26:35 +02003362 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003363
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003364 wl_list_remove(&shell->lock_layer.link);
3365 wl_list_insert(&shell->compositor->cursor_layer.link,
3366 &shell->fullscreen_layer.link);
3367 wl_list_insert(&shell->fullscreen_layer.link,
3368 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003369 if (shell->showing_input_panels) {
3370 wl_list_insert(&shell->panel_layer.link,
3371 &shell->input_panel_layer.link);
3372 wl_list_insert(&shell->input_panel_layer.link,
3373 &ws->layer.link);
3374 } else {
3375 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3376 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003377
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003378 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003379
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003380 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003381 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003382 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003383}
3384
3385static void
3386desktop_shell_unlock(struct wl_client *client,
3387 struct wl_resource *resource)
3388{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003389 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003390
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003391 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003392
3393 if (shell->locked)
3394 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003395}
3396
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003397static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003398desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003399 struct wl_resource *resource,
3400 struct wl_resource *surface_resource)
3401{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003402 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003403
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003404 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003405 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003406}
3407
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003408static void
3409desktop_shell_desktop_ready(struct wl_client *client,
3410 struct wl_resource *resource)
3411{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003412 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003413
3414 shell_fade_startup(shell);
3415}
3416
Kristian Høgsberg75840622011-09-06 13:48:16 -04003417static const struct desktop_shell_interface desktop_shell_implementation = {
3418 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003419 desktop_shell_set_panel,
3420 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003421 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003422 desktop_shell_set_grab_surface,
3423 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003424};
3425
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003426static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003427get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003428{
3429 struct shell_surface *shsurf;
3430
3431 shsurf = get_shell_surface(surface);
3432 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003433 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003434 return shsurf->type;
3435}
3436
Kristian Høgsberg75840622011-09-06 13:48:16 -04003437static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003438move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003439{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003440 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003441 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003442 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003443
Pekka Paalanen01388e22013-04-25 13:57:44 +03003444 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003445 if (surface == NULL)
3446 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003447
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003448 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003449 if (shsurf == NULL || shsurf->state.fullscreen ||
3450 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003451 return;
3452
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003453 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003454}
3455
3456static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003457touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3458{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003459 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003460 struct weston_surface *surface;
3461 struct shell_surface *shsurf;
3462
3463 surface = weston_surface_get_main_surface(focus);
3464 if (surface == NULL)
3465 return;
3466
3467 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003468 if (shsurf == NULL || shsurf->state.fullscreen ||
3469 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003470 return;
3471
3472 surface_touch_move(shsurf, (struct weston_seat *) seat);
3473}
3474
3475static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003476resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003477{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003478 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003479 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003480 uint32_t edges = 0;
3481 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003482 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003483
Pekka Paalanen01388e22013-04-25 13:57:44 +03003484 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003485 if (surface == NULL)
3486 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003487
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003488 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003489 if (shsurf == NULL || shsurf->state.fullscreen ||
3490 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003491 return;
3492
Jason Ekstranda7af7042013-10-12 22:38:11 -05003493 weston_view_from_global(shsurf->view,
3494 wl_fixed_to_int(seat->pointer->grab_x),
3495 wl_fixed_to_int(seat->pointer->grab_y),
3496 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003497
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003498 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003499 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003500 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003501 edges |= 0;
3502 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003503 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003504
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003505 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003506 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003507 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003508 edges |= 0;
3509 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003510 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003511
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003512 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003513}
3514
3515static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003516surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003517 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003518{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003519 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003520 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003521 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003522 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003523
Pekka Paalanen01388e22013-04-25 13:57:44 +03003524 /* XXX: broken for windows containing sub-surfaces */
3525 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003526 if (surface == NULL)
3527 return;
3528
3529 shsurf = get_shell_surface(surface);
3530 if (!shsurf)
3531 return;
3532
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003534
Jason Ekstranda7af7042013-10-12 22:38:11 -05003535 if (shsurf->view->alpha > 1.0)
3536 shsurf->view->alpha = 1.0;
3537 if (shsurf->view->alpha < step)
3538 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003539
Jason Ekstranda7af7042013-10-12 22:38:11 -05003540 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003541 weston_surface_damage(surface);
3542}
3543
3544static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003545do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003546 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003547{
Daniel Stone37816df2012-05-16 18:45:18 +01003548 struct weston_seat *ws = (struct weston_seat *) seat;
3549 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003550 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003551 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003552
3553 wl_list_for_each(output, &compositor->output_list, link) {
3554 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003555 wl_fixed_to_double(seat->pointer->x),
3556 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003557 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003558 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003559 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003560 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003561 increment = -output->zoom.increment;
3562 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003563 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003564 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003565 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003566 else
3567 increment = 0;
3568
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003569 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003570
Scott Moreaue6603982012-06-11 13:07:51 -06003571 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003572 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003573 else if (output->zoom.level > output->zoom.max_level)
3574 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003575 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003576 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003577 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003578
Scott Moreaue6603982012-06-11 13:07:51 -06003579 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003580
Jason Ekstranda7af7042013-10-12 22:38:11 -05003581 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003582 }
3583 }
3584}
3585
Scott Moreauccbf29d2012-02-22 14:21:41 -07003586static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003587zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003588 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003589{
3590 do_zoom(seat, time, 0, axis, value);
3591}
3592
3593static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003594zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003595 void *data)
3596{
3597 do_zoom(seat, time, key, 0, 0);
3598}
3599
3600static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003601terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003602 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003603{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003604 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003605
Daniel Stone325fc2d2012-05-30 16:31:58 +01003606 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003607}
3608
3609static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003610lower_fullscreen_layer(struct desktop_shell *shell);
3611
3612struct alt_tab {
3613 struct desktop_shell *shell;
3614 struct weston_keyboard_grab grab;
3615
3616 struct wl_list *current;
3617
3618 struct wl_list preview_list;
3619};
3620
3621struct alt_tab_preview {
3622 struct alt_tab *alt_tab;
3623
3624 struct weston_view *view;
3625 struct weston_transform transform;
3626
3627 struct wl_listener listener;
3628
3629 struct wl_list link;
3630};
3631
3632static void
3633alt_tab_next(struct alt_tab *alt_tab)
3634{
3635 alt_tab->current = alt_tab->current->next;
3636
3637 /* Make sure we're not pointing to the list header e.g. after
3638 * cycling through the whole list. */
3639 if (alt_tab->current->next == alt_tab->preview_list.next)
3640 alt_tab->current = alt_tab->current->next;
3641}
3642
3643static void
3644alt_tab_destroy(struct alt_tab *alt_tab)
3645{
3646 struct alt_tab_preview *preview, *next;
3647 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3648
3649 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3650 preview = wl_container_of(alt_tab->current, preview, link);
3651
3652 activate(alt_tab->shell, preview->view->surface,
3653 (struct weston_seat *) keyboard->seat);
3654 }
3655
3656 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3657 wl_list_remove(&preview->link);
3658 wl_list_remove(&preview->listener.link);
3659 weston_view_destroy(preview->view);
3660 free(preview);
3661 }
3662
3663 weston_keyboard_end_grab(keyboard);
3664 if (keyboard->input_method_resource)
3665 keyboard->grab = &keyboard->input_method_grab;
3666
3667 free(alt_tab);
3668}
3669
3670static void
3671alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3672{
3673 struct alt_tab_preview *preview =
3674 container_of(listener, struct alt_tab_preview, listener);
3675 struct alt_tab *alt_tab = preview->alt_tab;
3676 int advance = 0;
3677
3678 /* If the preview that we're removing is the currently selected one,
3679 * we want to move to the next one. So we move to ->prev and then
3680 * call _next() after removing the preview. */
3681 if (alt_tab->current == &preview->link) {
3682 alt_tab->current = alt_tab->current->prev;
3683 advance = 1;
3684 }
3685
3686 wl_list_remove(&preview->listener.link);
3687 wl_list_remove(&preview->link);
3688
3689 free(preview);
3690
3691 if (advance)
3692 alt_tab_next(alt_tab);
3693
3694 /* If the last preview goes away, stop the alt-tab */
3695 if (wl_list_empty(alt_tab->current))
3696 alt_tab_destroy(alt_tab);
3697}
3698
3699static void
3700alt_tab_key(struct weston_keyboard_grab *grab,
3701 uint32_t time, uint32_t key, uint32_t state_w)
3702{
3703 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3704 enum wl_keyboard_key_state state = state_w;
3705
3706 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3707 alt_tab_next(alt_tab);
3708}
3709
3710static void
3711alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3712 uint32_t mods_depressed, uint32_t mods_latched,
3713 uint32_t mods_locked, uint32_t group)
3714{
3715 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3716 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3717
3718 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3719 alt_tab_destroy(alt_tab);
3720}
3721
3722static void
3723alt_tab_cancel(struct weston_keyboard_grab *grab)
3724{
3725 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3726
3727 alt_tab_destroy(alt_tab);
3728}
3729
3730static const struct weston_keyboard_grab_interface alt_tab_grab = {
3731 alt_tab_key,
3732 alt_tab_modifier,
3733 alt_tab_cancel,
3734};
3735
3736static int
3737view_for_alt_tab(struct weston_view *view)
3738{
Rafael Antognollied207b42013-12-03 15:35:43 -02003739 struct shell_surface *shsurf = get_shell_surface(view->surface);
3740 if (!shsurf)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003741 return 0;
3742
Rafael Antognollied207b42013-12-03 15:35:43 -02003743 if (shsurf->parent)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003744 return 0;
3745
3746 if (view != get_default_view(view->surface))
3747 return 0;
3748
3749 return 1;
3750}
3751
3752static void
3753alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3754 void *data)
3755{
3756 struct alt_tab *alt_tab;
3757 struct desktop_shell *shell = data;
3758 struct weston_output *output = get_default_output(shell->compositor);
3759 struct workspace *ws;
3760 struct weston_view *view;
3761 int num_surfaces = 0;
3762 int x, y, side, margin;
3763
3764 wl_list_for_each(view, &shell->compositor->view_list, link) {
3765
3766 if (view_for_alt_tab(view))
3767 num_surfaces++;
3768 }
3769
3770 if (!num_surfaces)
3771 return;
3772
3773 alt_tab = malloc(sizeof *alt_tab);
3774 if (!alt_tab)
3775 return;
3776
3777 alt_tab->shell = shell;
3778 wl_list_init(&alt_tab->preview_list);
3779 alt_tab->current = &alt_tab->preview_list;
3780
3781 restore_all_output_modes(shell->compositor);
3782 lower_fullscreen_layer(alt_tab->shell);
3783
3784 alt_tab->grab.interface = &alt_tab_grab;
3785 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3786 weston_keyboard_set_focus(seat->keyboard, NULL);
3787
3788 ws = get_current_workspace(shell);
3789
3790 /* FIXME: add some visual candy e.g. prelight the selected view
3791 * and/or add a black surrounding background à la gnome-shell */
3792
3793 /* Determine the size for each preview */
3794 side = output->width / num_surfaces;
3795 if (side > 200)
3796 side = 200;
3797 margin = side / 4;
3798 side -= margin;
3799
3800 x = margin;
3801 y = (output->height - side) / 2;
3802
3803 /* Create a view for each surface */
3804 wl_list_for_each(view, &shell->compositor->view_list, link) {
3805 struct alt_tab_preview *preview;
3806 struct weston_view *v;
3807 float scale;
3808
3809 if (!view_for_alt_tab(view))
3810 continue;
3811
3812 preview = malloc(sizeof *preview);
3813 if (!preview) {
3814 alt_tab_destroy(alt_tab);
3815 return;
3816 }
3817
3818 preview->alt_tab = alt_tab;
3819
3820 preview->view = v = weston_view_create(view->surface);
3821 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003822
3823 wl_list_remove(&v->layer_link);
3824 wl_list_insert(&ws->layer.view_list, &v->layer_link);
3825 weston_view_damage_below(v);
3826 weston_surface_damage(v->surface);
3827
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003828 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003829
3830 preview->listener.notify = alt_tab_handle_surface_destroy;
3831 wl_signal_add(&v->destroy_signal, &preview->listener);
3832
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003833 if (view->surface->width > view->surface->height)
3834 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003835 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003836 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003837
3838 wl_list_insert(&v->geometry.transformation_list,
3839 &preview->transform.link);
3840 weston_matrix_init(&preview->transform.matrix);
3841 weston_matrix_scale(&preview->transform.matrix,
3842 scale, scale, 1.0f);
3843
3844 weston_view_geometry_dirty(v);
3845 weston_compositor_schedule_repaint(v->surface->compositor);
3846
3847 /* Insert at the end of the list */
3848 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3849
3850 x += side + margin;
3851 }
3852
3853 /* Start at the second preview so a simple <alt>tab changes window.
3854 * We set `current' to the first preview and not the second because
3855 * we're going to receive a key press callback for the initial
3856 * <alt>tab which will make `current' point to the second element. */
3857 alt_tab_next(alt_tab);
3858}
3859
3860static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003861rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3862 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003863{
3864 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003865 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003866 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003867 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003868 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003869
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003870 weston_pointer_move(pointer, x, y);
3871
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003872 if (!shsurf)
3873 return;
3874
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003875 cx = 0.5f * shsurf->surface->width;
3876 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003877
Daniel Stone37816df2012-05-16 18:45:18 +01003878 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3879 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003880 r = sqrtf(dx * dx + dy * dy);
3881
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003882 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003883 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003884
3885 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003886 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003887 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003888
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003889 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003890 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003891
3892 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003893 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003894 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003895 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003896 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003897
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003898 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003899 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003900 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003901 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003902 wl_list_init(&shsurf->rotation.transform.link);
3903 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003904 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003905 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003906
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003907 /* We need to adjust the position of the surface
3908 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003909 cposx = shsurf->view->geometry.x + cx;
3910 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003911 dposx = rotate->center.x - cposx;
3912 dposy = rotate->center.y - cposy;
3913 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003914 weston_view_set_position(shsurf->view,
3915 shsurf->view->geometry.x + dposx,
3916 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003917 }
3918
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003919 /* Repaint implies weston_surface_update_transform(), which
3920 * lazily applies the damage due to rotation update.
3921 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003922 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003923}
3924
3925static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003926rotate_grab_button(struct weston_pointer_grab *grab,
3927 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003928{
3929 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003930 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003931 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003932 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003933 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003934
Daniel Stone4dbadb12012-05-30 16:31:51 +01003935 if (pointer->button_count == 0 &&
3936 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003937 if (shsurf)
3938 weston_matrix_multiply(&shsurf->rotation.rotation,
3939 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003940 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003941 free(rotate);
3942 }
3943}
3944
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003945static void
3946rotate_grab_cancel(struct weston_pointer_grab *grab)
3947{
3948 struct rotate_grab *rotate =
3949 container_of(grab, struct rotate_grab, base.grab);
3950
3951 shell_grab_end(&rotate->base);
3952 free(rotate);
3953}
3954
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003955static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003956 noop_grab_focus,
3957 rotate_grab_motion,
3958 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003959 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003960};
3961
3962static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003963surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003964{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003965 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003966 float dx, dy;
3967 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003968
Pekka Paalanen460099f2012-01-20 16:48:25 +02003969 rotate = malloc(sizeof *rotate);
3970 if (!rotate)
3971 return;
3972
Jason Ekstranda7af7042013-10-12 22:38:11 -05003973 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003974 surface->surface->width * 0.5f,
3975 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003976 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003977
Daniel Stone37816df2012-05-16 18:45:18 +01003978 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3979 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003980 r = sqrtf(dx * dx + dy * dy);
3981 if (r > 20.0f) {
3982 struct weston_matrix inverse;
3983
3984 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003985 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003986 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003987
3988 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003989 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003990 } else {
3991 weston_matrix_init(&surface->rotation.rotation);
3992 weston_matrix_init(&rotate->rotation);
3993 }
3994
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003995 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3996 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003997}
3998
3999static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004000rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004001 void *data)
4002{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004003 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004004 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004005 struct shell_surface *surface;
4006
Pekka Paalanen01388e22013-04-25 13:57:44 +03004007 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004008 if (base_surface == NULL)
4009 return;
4010
4011 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004012 if (surface == NULL || surface->state.fullscreen ||
4013 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004014 return;
4015
4016 surface_rotate(surface, seat);
4017}
4018
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004019/* Move all fullscreen layers down to the current workspace in a non-reversible
4020 * manner. This should be used when implementing shell-wide overlays, such as
4021 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004022static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004023lower_fullscreen_layer(struct desktop_shell *shell)
4024{
4025 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004026 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004027
4028 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004029 wl_list_for_each_reverse_safe(view, prev,
4030 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004031 layer_link) {
4032 wl_list_remove(&view->layer_link);
4033 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4034 weston_view_damage_below(view);
4035 weston_surface_damage(view->surface);
4036 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004037}
4038
4039static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004040activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004041 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004042{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004043 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004044 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004045 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004046 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004047 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004048
Pekka Paalanen01388e22013-04-25 13:57:44 +03004049 main_surface = weston_surface_get_main_surface(es);
4050
Daniel Stone37816df2012-05-16 18:45:18 +01004051 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004052
Jonas Ådahl8538b222012-08-29 22:13:03 +02004053 state = ensure_focus_state(shell, seat);
4054 if (state == NULL)
4055 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004056
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004057 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004058 state->keyboard_focus = es;
4059 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004060 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004061
Rafael Antognolli03b16592013-12-03 15:35:42 -02004062 shsurf = get_shell_surface(main_surface);
4063 if (shsurf->state.fullscreen)
4064 shell_configure_fullscreen(shsurf);
4065 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004066 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004067
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004068 if (shell->focus_animation_type != ANIMATION_NONE) {
4069 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004070 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004071 }
4072
4073 /* Update the surface’s layer. This brings it to the top of the stacking
4074 * order as appropriate. */
4075 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004076}
4077
Alex Wu21858432012-04-01 20:13:08 +08004078/* no-op func for checking black surface */
4079static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004080black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004081{
4082}
4083
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004084static bool
Alex Wu21858432012-04-01 20:13:08 +08004085is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4086{
4087 if (es->configure == black_surface_configure) {
4088 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004089 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004090 return true;
4091 }
4092 return false;
4093}
4094
Kristian Høgsberg75840622011-09-06 13:48:16 -04004095static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004096activate_binding(struct weston_seat *seat,
4097 struct desktop_shell *shell,
4098 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004099{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004100 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004101
Alex Wu9c35e6b2012-03-05 14:13:13 +08004102 if (!focus)
4103 return;
4104
Pekka Paalanen01388e22013-04-25 13:57:44 +03004105 if (is_black_surface(focus, &main_surface))
4106 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004107
Pekka Paalanen01388e22013-04-25 13:57:44 +03004108 main_surface = weston_surface_get_main_surface(focus);
4109 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004110 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004111
Neil Robertsa28c6932013-10-03 16:43:04 +01004112 activate(shell, focus, seat);
4113}
4114
4115static void
4116click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4117 void *data)
4118{
4119 if (seat->pointer->grab != &seat->pointer->default_grab)
4120 return;
4121
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004122 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004123}
4124
4125static void
4126touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4127{
4128 if (seat->touch->grab != &seat->touch->default_grab)
4129 return;
4130
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004131 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004132}
4133
4134static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004135lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004136{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004137 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004138
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004139 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004140 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004141 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004142 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004143
4144 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004145
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004146 /* Hide all surfaces by removing the fullscreen, panel and
4147 * toplevel layers. This way nothing else can show or receive
4148 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004149
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004150 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004151 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004152 if (shell->showing_input_panels)
4153 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004154 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004155 wl_list_insert(&shell->compositor->cursor_layer.link,
4156 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004157
Pekka Paalanen77346a62011-11-30 16:26:35 +02004158 launch_screensaver(shell);
4159
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004160 /* TODO: disable bindings that should not work while locked. */
4161
4162 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004163}
4164
4165static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004166unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004167{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004168 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004169 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004170 return;
4171 }
4172
4173 /* If desktop-shell client has gone away, unlock immediately. */
4174 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004175 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004176 return;
4177 }
4178
4179 if (shell->prepare_event_sent)
4180 return;
4181
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004182 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004183 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004184}
4185
4186static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004187shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004188{
4189 struct desktop_shell *shell = data;
4190
4191 shell->fade.animation = NULL;
4192
4193 switch (shell->fade.type) {
4194 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004195 weston_surface_destroy(shell->fade.view->surface);
4196 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004197 break;
4198 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004199 lock(shell);
4200 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004201 default:
4202 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004203 }
4204}
4205
Jason Ekstranda7af7042013-10-12 22:38:11 -05004206static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004207shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004208{
4209 struct weston_compositor *compositor = shell->compositor;
4210 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004211 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004212
4213 surface = weston_surface_create(compositor);
4214 if (!surface)
4215 return NULL;
4216
Jason Ekstranda7af7042013-10-12 22:38:11 -05004217 view = weston_view_create(surface);
4218 if (!view) {
4219 weston_surface_destroy(surface);
4220 return NULL;
4221 }
4222
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004223 surface->width = 8192;
4224 surface->height = 8192;
4225 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004226 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004227 wl_list_insert(&compositor->fade_layer.view_list,
4228 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004229 pixman_region32_init(&surface->input);
4230
Jason Ekstranda7af7042013-10-12 22:38:11 -05004231 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004232}
4233
4234static void
4235shell_fade(struct desktop_shell *shell, enum fade_type type)
4236{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004237 float tint;
4238
4239 switch (type) {
4240 case FADE_IN:
4241 tint = 0.0;
4242 break;
4243 case FADE_OUT:
4244 tint = 1.0;
4245 break;
4246 default:
4247 weston_log("shell: invalid fade type\n");
4248 return;
4249 }
4250
4251 shell->fade.type = type;
4252
Jason Ekstranda7af7042013-10-12 22:38:11 -05004253 if (shell->fade.view == NULL) {
4254 shell->fade.view = shell_fade_create_surface(shell);
4255 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004256 return;
4257
Jason Ekstranda7af7042013-10-12 22:38:11 -05004258 shell->fade.view->alpha = 1.0 - tint;
4259 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004260 }
4261
4262 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004263 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004264 else
4265 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004266 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004267 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004268 shell_fade_done, shell);
4269}
4270
4271static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004272do_shell_fade_startup(void *data)
4273{
4274 struct desktop_shell *shell = data;
4275
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004276 if (shell->startup_animation_type == ANIMATION_FADE)
4277 shell_fade(shell, FADE_IN);
4278 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004279 weston_surface_destroy(shell->fade.view->surface);
4280 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004281 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004282}
4283
4284static void
4285shell_fade_startup(struct desktop_shell *shell)
4286{
4287 struct wl_event_loop *loop;
4288
4289 if (!shell->fade.startup_timer)
4290 return;
4291
4292 wl_event_source_remove(shell->fade.startup_timer);
4293 shell->fade.startup_timer = NULL;
4294
4295 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4296 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4297}
4298
4299static int
4300fade_startup_timeout(void *data)
4301{
4302 struct desktop_shell *shell = data;
4303
4304 shell_fade_startup(shell);
4305 return 0;
4306}
4307
4308static void
4309shell_fade_init(struct desktop_shell *shell)
4310{
4311 /* Make compositor output all black, and wait for the desktop-shell
4312 * client to signal it is ready, then fade in. The timer triggers a
4313 * fade-in, in case the desktop-shell client takes too long.
4314 */
4315
4316 struct wl_event_loop *loop;
4317
Jason Ekstranda7af7042013-10-12 22:38:11 -05004318 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004319 weston_log("%s: warning: fade surface already exists\n",
4320 __func__);
4321 return;
4322 }
4323
Jason Ekstranda7af7042013-10-12 22:38:11 -05004324 shell->fade.view = shell_fade_create_surface(shell);
4325 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004326 return;
4327
Jason Ekstranda7af7042013-10-12 22:38:11 -05004328 weston_view_update_transform(shell->fade.view);
4329 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004330
4331 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4332 shell->fade.startup_timer =
4333 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4334 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4335}
4336
4337static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004338idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004339{
4340 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004341 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004342
4343 shell_fade(shell, FADE_OUT);
4344 /* lock() is called from shell_fade_done() */
4345}
4346
4347static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004348wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004349{
4350 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004351 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004352
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004353 unlock(shell);
4354}
4355
4356static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004357show_input_panels(struct wl_listener *listener, void *data)
4358{
4359 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004360 container_of(listener, struct desktop_shell,
4361 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004362 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004363
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004364 shell->text_input.surface = (struct weston_surface*)data;
4365
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004366 if (shell->showing_input_panels)
4367 return;
4368
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004369 shell->showing_input_panels = true;
4370
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004371 if (!shell->locked)
4372 wl_list_insert(&shell->panel_layer.link,
4373 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004374
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004376 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004377 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004378 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004379 wl_list_insert(&shell->input_panel_layer.view_list,
4380 &ipsurf->view->layer_link);
4381 weston_view_geometry_dirty(ipsurf->view);
4382 weston_view_update_transform(ipsurf->view);
4383 weston_surface_damage(ipsurf->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004384 weston_slide_run(ipsurf->view, ipsurf->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004385 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004386 }
4387}
4388
4389static void
4390hide_input_panels(struct wl_listener *listener, void *data)
4391{
4392 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004393 container_of(listener, struct desktop_shell,
4394 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004395 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004396
Jan Arne Petersen61381972013-01-31 15:52:21 +01004397 if (!shell->showing_input_panels)
4398 return;
4399
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004400 shell->showing_input_panels = false;
4401
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004402 if (!shell->locked)
4403 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004404
Jason Ekstranda7af7042013-10-12 22:38:11 -05004405 wl_list_for_each_safe(view, next,
4406 &shell->input_panel_layer.view_list, layer_link)
4407 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004408}
4409
4410static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004411update_input_panels(struct wl_listener *listener, void *data)
4412{
4413 struct desktop_shell *shell =
4414 container_of(listener, struct desktop_shell,
4415 update_input_panel_listener);
4416
4417 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4418}
4419
4420static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004421center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004422{
Giulio Camuffob8366642013-04-25 13:57:46 +03004423 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004424 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004425
Jason Ekstranda7af7042013-10-12 22:38:11 -05004426 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004427
4428 x = output->x + (output->width - width) / 2 - surf_x / 2;
4429 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004430
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004431 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004432}
4433
4434static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004435weston_view_set_initial_position(struct weston_view *view,
4436 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004437{
4438 struct weston_compositor *compositor = shell->compositor;
4439 int ix = 0, iy = 0;
4440 int range_x, range_y;
4441 int dx, dy, x, y, panel_height;
4442 struct weston_output *output, *target_output = NULL;
4443 struct weston_seat *seat;
4444
4445 /* As a heuristic place the new window on the same output as the
4446 * pointer. Falling back to the output containing 0, 0.
4447 *
4448 * TODO: Do something clever for touch too?
4449 */
4450 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004451 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004452 ix = wl_fixed_to_int(seat->pointer->x);
4453 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004454 break;
4455 }
4456 }
4457
4458 wl_list_for_each(output, &compositor->output_list, link) {
4459 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4460 target_output = output;
4461 break;
4462 }
4463 }
4464
4465 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004466 weston_view_set_position(view, 10 + random() % 400,
4467 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004468 return;
4469 }
4470
4471 /* Valid range within output where the surface will still be onscreen.
4472 * If this is negative it means that the surface is bigger than
4473 * output.
4474 */
4475 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004476 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004477 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004478 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004479
Rob Bradford4cb88c72012-08-13 15:18:44 +01004480 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004481 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004482 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004483 dx = 0;
4484
4485 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004486 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004487 else
4488 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004489
4490 x = target_output->x + dx;
4491 y = target_output->y + dy;
4492
Jason Ekstranda7af7042013-10-12 22:38:11 -05004493 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004494}
4495
4496static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004497map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004498 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004499{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004500 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004501 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004502 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004503 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004504
Pekka Paalanen77346a62011-11-30 16:26:35 +02004505 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004506 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004507 case SHELL_SURFACE_TOPLEVEL:
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);
Rafael Antognollied207b42013-12-03 15:35:43 -02004520 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004521 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_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004533 default:
4534 ;
4535 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004536
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004537 /* Surface stacking order, see also activate(). */
4538 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004539
Jason Ekstranda7af7042013-10-12 22:38:11 -05004540 if (shsurf->type != SHELL_SURFACE_NONE) {
4541 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004542 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004543 shsurf->surface->output = shsurf->output;
4544 shsurf->view->output = shsurf->output;
4545 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004546 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004547
Rafael Antognollied207b42013-12-03 15:35:43 -02004548 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4549 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4550 }
4551
Jason Ekstranda7af7042013-10-12 22:38:11 -05004552 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004553 /* XXX: xwayland's using the same fields for transient type */
4554 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004555 if (shsurf->transient.flags ==
4556 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4557 break;
4558 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004559 if (shsurf->state.relative &&
4560 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4561 break;
4562 if (!shell->locked)
4563 break;
4564 wl_list_for_each(seat, &compositor->seat_list, link)
4565 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004566 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004567 case SHELL_SURFACE_POPUP:
4568 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004569 default:
4570 break;
4571 }
4572
Rafael Antognolli03b16592013-12-03 15:35:42 -02004573 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4574 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004575 {
4576 switch (shell->win_animation_type) {
4577 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004578 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004579 break;
4580 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004581 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004582 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004583 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004584 default:
4585 break;
4586 }
4587 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004588}
4589
4590static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004591configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004592 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004593{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004594 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004595 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004596 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004597
Pekka Paalanen77346a62011-11-30 16:26:35 +02004598 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004599
Rafael Antognolli03b16592013-12-03 15:35:42 -02004600 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004601 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004602 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004603 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004604 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4605 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004606 mx = shsurf->output->x - surf_x;
4607 my = shsurf->output->y +
4608 get_output_panel_height(shell,shsurf->output) - surf_y;
4609 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004610 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004611 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004612 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004613
Alex Wu4539b082012-03-01 12:57:46 +08004614 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004615 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004616 wl_list_for_each(view, &surface->views, surface_link)
4617 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004618
Rafael Antognolli03b16592013-12-03 15:35:42 -02004619 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004620 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004621 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004622}
4623
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004624static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004625shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004626{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004627 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004628 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004629
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004630 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004631
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004632 if (!weston_surface_is_mapped(es) &&
4633 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004634 remove_popup_grab(shsurf);
4635 }
4636
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004637 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004638 return;
4639
Rafael Antognolli03b16592013-12-03 15:35:42 -02004640 if ((shsurf->next_type != SHELL_SURFACE_NONE &&
4641 shsurf->type != shsurf->next_type) ||
4642 shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004643 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004644 type_changed = 1;
4645 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004646
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004647 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004648 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004649 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004650 shsurf->last_width != es->width ||
4651 shsurf->last_height != es->height) {
4652 shsurf->last_width = es->width;
4653 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004654 float from_x, from_y;
4655 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004656
Jason Ekstranda7af7042013-10-12 22:38:11 -05004657 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4658 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004659 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004660 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004661 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004662 }
4663}
4664
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004665static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004666
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004667static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004668desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004669{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004670 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004671 struct desktop_shell *shell =
4672 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004673
4674 shell->child.process.pid = 0;
4675 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004676
4677 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4678 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004679 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004680 shell->child.deathstamp = time;
4681 shell->child.deathcount = 0;
4682 }
4683
4684 shell->child.deathcount++;
4685 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004686 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004687 return;
4688 }
4689
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004690 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004691 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004692 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004693}
4694
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004695static void
4696launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004697{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004698 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004699
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004700 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004701 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004702 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004703 desktop_shell_sigchld);
4704
4705 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004706 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004707}
4708
4709static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004710bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4711{
Tiago Vignattibe143262012-04-16 17:31:41 +03004712 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004713 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004714
Jason Ekstranda85118c2013-06-27 20:17:02 -05004715 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4716 if (resource)
4717 wl_resource_set_implementation(resource, &shell_implementation,
4718 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004719}
4720
Kristian Høgsberg75840622011-09-06 13:48:16 -04004721static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004722unbind_desktop_shell(struct wl_resource *resource)
4723{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004724 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004725
4726 if (shell->locked)
4727 resume_desktop(shell);
4728
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004729 shell->child.desktop_shell = NULL;
4730 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004731}
4732
4733static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004734bind_desktop_shell(struct wl_client *client,
4735 void *data, uint32_t version, uint32_t id)
4736{
Tiago Vignattibe143262012-04-16 17:31:41 +03004737 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004738 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004739
Jason Ekstranda85118c2013-06-27 20:17:02 -05004740 resource = wl_resource_create(client, &desktop_shell_interface,
4741 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004742
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004743 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004744 wl_resource_set_implementation(resource,
4745 &desktop_shell_implementation,
4746 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004747 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004748
4749 if (version < 2)
4750 shell_fade_startup(shell);
4751
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004752 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004753 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004754
4755 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4756 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004757 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004758}
4759
Pekka Paalanen6e168112011-11-24 11:34:05 +02004760static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004761screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004762{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004763 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004764 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004765
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004766 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004767 return;
4768
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004769 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004770 if (!shell->locked)
4771 return;
4772
Jason Ekstranda7af7042013-10-12 22:38:11 -05004773 view = container_of(surface->views.next, struct weston_view, surface_link);
4774 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004775
Jason Ekstranda7af7042013-10-12 22:38:11 -05004776 if (wl_list_empty(&view->layer_link)) {
4777 wl_list_insert(shell->lock_layer.view_list.prev,
4778 &view->layer_link);
4779 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004780 wl_event_source_timer_update(shell->screensaver.timer,
4781 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004782 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004783 }
4784}
4785
4786static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004787screensaver_set_surface(struct wl_client *client,
4788 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004789 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004790 struct wl_resource *output_resource)
4791{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004792 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004793 struct weston_surface *surface =
4794 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004795 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004796 struct weston_view *view, *next;
4797
4798 /* Make sure we only have one view */
4799 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4800 weston_view_destroy(view);
4801 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004802
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004803 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004804 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004805 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004806}
4807
4808static const struct screensaver_interface screensaver_implementation = {
4809 screensaver_set_surface
4810};
4811
4812static void
4813unbind_screensaver(struct wl_resource *resource)
4814{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004815 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004816
Pekka Paalanen77346a62011-11-30 16:26:35 +02004817 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004818}
4819
4820static void
4821bind_screensaver(struct wl_client *client,
4822 void *data, uint32_t version, uint32_t id)
4823{
Tiago Vignattibe143262012-04-16 17:31:41 +03004824 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004825 struct wl_resource *resource;
4826
Jason Ekstranda85118c2013-06-27 20:17:02 -05004827 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004828
Pekka Paalanen77346a62011-11-30 16:26:35 +02004829 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004830 wl_resource_set_implementation(resource,
4831 &screensaver_implementation,
4832 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004833 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004834 return;
4835 }
4836
4837 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4838 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004839 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004840}
4841
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004842static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004843input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004844{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004845 struct input_panel_surface *ip_surface = surface->configure_private;
4846 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004847 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004848 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004849
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004850 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004851 return;
4852
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004853 if (!weston_surface_is_mapped(surface)) {
4854 if (!shell->showing_input_panels)
4855 return;
4856
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004857 show_surface = 1;
4858 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004859
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004860 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004861
4862 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004863 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4864 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004865 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004866 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
4867 y = ip_surface->output->y + ip_surface->output->height - surface->height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004868 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004869
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004870 weston_view_set_position(ip_surface->view, x, y);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004871
4872 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004873 wl_list_insert(&shell->input_panel_layer.view_list,
4874 &ip_surface->view->layer_link);
4875 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004876 weston_surface_damage(surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004877 weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004878 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004879}
4880
4881static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004882destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004883{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004884 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4885
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004886 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004887 wl_list_remove(&input_panel_surface->link);
4888
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004889 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004890 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004891
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004892 free(input_panel_surface);
4893}
4894
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004895static struct input_panel_surface *
4896get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004897{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004898 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004899 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004900 } else {
4901 return NULL;
4902 }
4903}
4904
4905static void
4906input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4907{
4908 struct input_panel_surface *ipsurface = container_of(listener,
4909 struct input_panel_surface,
4910 surface_destroy_listener);
4911
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004912 if (ipsurface->resource) {
4913 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004914 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004915 destroy_input_panel_surface(ipsurface);
4916 }
4917}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004918
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004919static struct input_panel_surface *
4920create_input_panel_surface(struct desktop_shell *shell,
4921 struct weston_surface *surface)
4922{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004923 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004924
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004925 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4926 if (!input_panel_surface)
4927 return NULL;
4928
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004929 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004930 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004931
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004932 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004933
4934 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004935 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004936
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004937 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004938 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004939 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004940 &input_panel_surface->surface_destroy_listener);
4941
4942 wl_list_init(&input_panel_surface->link);
4943
4944 return input_panel_surface;
4945}
4946
4947static void
4948input_panel_surface_set_toplevel(struct wl_client *client,
4949 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004950 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004951 uint32_t position)
4952{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004953 struct input_panel_surface *input_panel_surface =
4954 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004955 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004956
4957 wl_list_insert(&shell->input_panel.surfaces,
4958 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004959
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004960 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004961 input_panel_surface->panel = 0;
4962}
4963
4964static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004965input_panel_surface_set_overlay_panel(struct wl_client *client,
4966 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004967{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004968 struct input_panel_surface *input_panel_surface =
4969 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004970 struct desktop_shell *shell = input_panel_surface->shell;
4971
4972 wl_list_insert(&shell->input_panel.surfaces,
4973 &input_panel_surface->link);
4974
4975 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004976}
4977
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004978static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004979 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004980 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004981};
4982
4983static void
4984destroy_input_panel_surface_resource(struct wl_resource *resource)
4985{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004986 struct input_panel_surface *ipsurf =
4987 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004988
4989 destroy_input_panel_surface(ipsurf);
4990}
4991
4992static void
4993input_panel_get_input_panel_surface(struct wl_client *client,
4994 struct wl_resource *resource,
4995 uint32_t id,
4996 struct wl_resource *surface_resource)
4997{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004998 struct weston_surface *surface =
4999 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005000 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005001 struct input_panel_surface *ipsurf;
5002
5003 if (get_input_panel_surface(surface)) {
5004 wl_resource_post_error(surface_resource,
5005 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005006 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005007 return;
5008 }
5009
5010 ipsurf = create_input_panel_surface(shell, surface);
5011 if (!ipsurf) {
5012 wl_resource_post_error(surface_resource,
5013 WL_DISPLAY_ERROR_INVALID_OBJECT,
5014 "surface->configure already set");
5015 return;
5016 }
5017
Jason Ekstranda85118c2013-06-27 20:17:02 -05005018 ipsurf->resource =
5019 wl_resource_create(client,
5020 &wl_input_panel_surface_interface, 1, id);
5021 wl_resource_set_implementation(ipsurf->resource,
5022 &input_panel_surface_implementation,
5023 ipsurf,
5024 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005025}
5026
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005027static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005028 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005029};
5030
5031static void
5032unbind_input_panel(struct wl_resource *resource)
5033{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005034 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005035
5036 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005037}
5038
5039static void
5040bind_input_panel(struct wl_client *client,
5041 void *data, uint32_t version, uint32_t id)
5042{
5043 struct desktop_shell *shell = data;
5044 struct wl_resource *resource;
5045
Jason Ekstranda85118c2013-06-27 20:17:02 -05005046 resource = wl_resource_create(client,
5047 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005048
5049 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005050 wl_resource_set_implementation(resource,
5051 &input_panel_implementation,
5052 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005053 shell->input_panel.binding = resource;
5054 return;
5055 }
5056
5057 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5058 "interface object already bound");
5059 wl_resource_destroy(resource);
5060}
5061
Kristian Høgsberg07045392012-02-19 18:52:44 -05005062struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005063 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005064 struct weston_surface *current;
5065 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005066 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005067};
5068
5069static void
5070switcher_next(struct switcher *switcher)
5071{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005072 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005073 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005074 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005075 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005076
Jason Ekstranda7af7042013-10-12 22:38:11 -05005077 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005078 shsurf = get_shell_surface(view->surface);
5079 switch (shsurf->type) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005080 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005081 if (shsurf->parent)
5082 break;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005083 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005084 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005085 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005086 next = view->surface;
5087 prev = view->surface;
5088 view->alpha = 0.25;
5089 weston_view_geometry_dirty(view);
5090 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005091 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005092 case SHELL_SURFACE_POPUP:
5093 case SHELL_SURFACE_XWAYLAND:
5094 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005095 default:
5096 break;
5097 }
Alex Wu1659daa2012-04-01 20:13:09 +08005098
Jason Ekstranda7af7042013-10-12 22:38:11 -05005099 if (is_black_surface(view->surface, NULL)) {
5100 view->alpha = 0.25;
5101 weston_view_geometry_dirty(view);
5102 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005103 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005104 }
5105
5106 if (next == NULL)
5107 next = first;
5108
Alex Wu07b26062012-03-12 16:06:01 +08005109 if (next == NULL)
5110 return;
5111
Kristian Høgsberg07045392012-02-19 18:52:44 -05005112 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005113 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005114
5115 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005116 wl_list_for_each(view, &next->views, surface_link)
5117 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005118
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005119 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005120 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005121 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005122}
5123
5124static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005125switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005126{
5127 struct switcher *switcher =
5128 container_of(listener, struct switcher, listener);
5129
5130 switcher_next(switcher);
5131}
5132
5133static void
Daniel Stone351eb612012-05-31 15:27:47 -04005134switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005135{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005136 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005137 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005138 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005139
Jason Ekstranda7af7042013-10-12 22:38:11 -05005140 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005141 if (is_focus_view(view))
5142 continue;
5143
Jason Ekstranda7af7042013-10-12 22:38:11 -05005144 view->alpha = 1.0;
5145 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005146 }
5147
Alex Wu07b26062012-03-12 16:06:01 +08005148 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005149 activate(switcher->shell, switcher->current,
5150 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005151 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005152 weston_keyboard_end_grab(keyboard);
5153 if (keyboard->input_method_resource)
5154 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005155 free(switcher);
5156}
5157
5158static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005159switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005160 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005161{
5162 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005163 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005164
Daniel Stonec9785ea2012-05-30 16:31:52 +01005165 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005166 switcher_next(switcher);
5167}
5168
5169static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005170switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005171 uint32_t mods_depressed, uint32_t mods_latched,
5172 uint32_t mods_locked, uint32_t group)
5173{
5174 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005175 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005176
Daniel Stone351eb612012-05-31 15:27:47 -04005177 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5178 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005179}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005180
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005181static void
5182switcher_cancel(struct weston_keyboard_grab *grab)
5183{
5184 struct switcher *switcher = container_of(grab, struct switcher, grab);
5185
5186 switcher_destroy(switcher);
5187}
5188
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005189static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005190 switcher_key,
5191 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005192 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005193};
5194
5195static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005196switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005197 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005198{
Tiago Vignattibe143262012-04-16 17:31:41 +03005199 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005200 struct switcher *switcher;
5201
5202 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005203 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005204 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005205 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005206 wl_list_init(&switcher->listener.link);
5207
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005208 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005209 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005210 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005211 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5212 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005213 switcher_next(switcher);
5214}
5215
Daniel Stonedf8133b2013-11-19 11:37:14 +01005216struct exposay_surface {
5217 struct desktop_shell *shell;
5218 struct weston_surface *surface;
5219 struct weston_view *view;
5220 struct wl_list link;
5221
5222 int x;
5223 int y;
5224 int width;
5225 int height;
5226 double scale;
5227
5228 int row;
5229 int column;
5230
5231 /* The animations only apply a transformation for their own lifetime,
5232 * and don't have an option to indefinitely maintain the
5233 * transformation in a steady state - so, we apply our own once the
5234 * animation has finished. */
5235 struct weston_transform transform;
5236};
5237
5238static void exposay_set_state(struct desktop_shell *shell,
5239 enum exposay_target_state state,
5240 struct weston_seat *seat);
5241static void exposay_check_state(struct desktop_shell *shell);
5242
5243static void
5244exposay_in_flight_inc(struct desktop_shell *shell)
5245{
5246 shell->exposay.in_flight++;
5247}
5248
5249static void
5250exposay_in_flight_dec(struct desktop_shell *shell)
5251{
5252 if (--shell->exposay.in_flight > 0)
5253 return;
5254
5255 exposay_check_state(shell);
5256}
5257
5258static void
5259exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5260{
5261 struct exposay_surface *esurface = data;
5262
5263 wl_list_insert(&esurface->view->geometry.transformation_list,
5264 &esurface->transform.link);
5265 weston_matrix_init(&esurface->transform.matrix);
5266 weston_matrix_scale(&esurface->transform.matrix,
5267 esurface->scale, esurface->scale, 1.0f);
5268 weston_matrix_translate(&esurface->transform.matrix,
5269 esurface->x - esurface->view->geometry.x,
5270 esurface->y - esurface->view->geometry.y,
5271 0);
5272
5273 weston_view_geometry_dirty(esurface->view);
5274 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5275
5276 exposay_in_flight_dec(esurface->shell);
5277}
5278
5279static void
5280exposay_animate_in(struct exposay_surface *esurface)
5281{
5282 exposay_in_flight_inc(esurface->shell);
5283
5284 weston_move_scale_run(esurface->view,
5285 esurface->x - esurface->view->geometry.x,
5286 esurface->y - esurface->view->geometry.y,
5287 1.0, esurface->scale, 0,
5288 exposay_animate_in_done, esurface);
5289}
5290
5291static void
5292exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5293{
5294 struct exposay_surface *esurface = data;
5295 struct desktop_shell *shell = esurface->shell;
5296
5297 wl_list_remove(&esurface->link);
5298 free(esurface);
5299
5300 exposay_in_flight_dec(shell);
5301}
5302
5303static void
5304exposay_animate_out(struct exposay_surface *esurface)
5305{
5306 exposay_in_flight_inc(esurface->shell);
5307
5308 /* Remove the static transformation set up by
5309 * exposay_transform_in_done(). */
5310 wl_list_remove(&esurface->transform.link);
5311 weston_view_geometry_dirty(esurface->view);
5312
5313 weston_move_scale_run(esurface->view,
5314 esurface->x - esurface->view->geometry.x,
5315 esurface->y - esurface->view->geometry.y,
5316 1.0, esurface->scale, 1,
5317 exposay_animate_out_done, esurface);
5318}
5319
5320static void
5321exposay_highlight_surface(struct desktop_shell *shell,
5322 struct exposay_surface *esurface)
5323{
5324 struct weston_view *view = NULL;
5325
5326 if (esurface) {
5327 shell->exposay.row_current = esurface->row;
5328 shell->exposay.column_current = esurface->column;
5329 view = esurface->view;
5330 }
5331
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005332 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005333 shell->exposay.focus_current = view;
5334}
5335
5336static int
5337exposay_is_animating(struct desktop_shell *shell)
5338{
5339 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5340 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5341 return 0;
5342
5343 return (shell->exposay.in_flight > 0);
5344}
5345
5346static void
5347exposay_pick(struct desktop_shell *shell, int x, int y)
5348{
5349 struct exposay_surface *esurface;
5350
5351 if (exposay_is_animating(shell))
5352 return;
5353
5354 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5355 if (x < esurface->x || x > esurface->x + esurface->width)
5356 continue;
5357 if (y < esurface->y || y > esurface->y + esurface->height)
5358 continue;
5359
5360 exposay_highlight_surface(shell, esurface);
5361 return;
5362 }
5363}
5364
5365/* Pretty lame layout for now; just tries to make a square. Should take
5366 * aspect ratio into account really. Also needs to be notified of surface
5367 * addition and removal and adjust layout/animate accordingly. */
5368static enum exposay_layout_state
5369exposay_layout(struct desktop_shell *shell)
5370{
5371 struct workspace *workspace = shell->exposay.workspace;
5372 struct weston_compositor *compositor = shell->compositor;
5373 struct weston_output *output = get_default_output(compositor);
5374 struct weston_view *view;
5375 struct exposay_surface *esurface;
5376 int w, h;
5377 int i;
5378 int last_row_removed = 0;
5379
5380 wl_list_init(&shell->exposay.surface_list);
5381
5382 shell->exposay.num_surfaces = 0;
5383 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5384 if (!get_shell_surface(view->surface))
5385 continue;
5386 shell->exposay.num_surfaces++;
5387 }
5388
5389 if (shell->exposay.num_surfaces == 0) {
5390 shell->exposay.grid_size = 0;
5391 shell->exposay.hpadding_outer = 0;
5392 shell->exposay.vpadding_outer = 0;
5393 shell->exposay.padding_inner = 0;
5394 shell->exposay.surface_size = 0;
5395 return EXPOSAY_LAYOUT_OVERVIEW;
5396 }
5397
5398 /* Lay the grid out as square as possible, losing surfaces from the
5399 * bottom row if required. Start with fixed padding of a 10% margin
5400 * around the outside and 80px internal padding between surfaces, and
5401 * maximise the area made available to surfaces after this, but only
5402 * to a maximum of 1/3rd the total output size.
5403 *
5404 * If we can't make a square grid, add one extra row at the bottom
5405 * which will have a smaller number of columns.
5406 *
5407 * XXX: Surely there has to be a better way to express this maths,
5408 * right?!
5409 */
5410 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5411 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5412 shell->exposay.grid_size++;
5413 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5414
5415 shell->exposay.hpadding_outer = (output->width / 10);
5416 shell->exposay.vpadding_outer = (output->height / 10);
5417 shell->exposay.padding_inner = 80;
5418
5419 w = output->width - (shell->exposay.hpadding_outer * 2);
5420 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5421 w /= shell->exposay.grid_size;
5422
5423 h = output->height - (shell->exposay.vpadding_outer * 2);
5424 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5425 h /= shell->exposay.grid_size;
5426
5427 shell->exposay.surface_size = (w < h) ? w : h;
5428 if (shell->exposay.surface_size > (output->width / 2))
5429 shell->exposay.surface_size = output->width / 2;
5430 if (shell->exposay.surface_size > (output->height / 2))
5431 shell->exposay.surface_size = output->height / 2;
5432
5433 i = 0;
5434 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5435 int pad;
5436
5437 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5438
5439 if (!get_shell_surface(view->surface))
5440 continue;
5441
5442 esurface = malloc(sizeof(*esurface));
5443 if (!esurface) {
5444 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5445 shell->exposay.seat);
5446 break;
5447 }
5448
5449 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5450 esurface->shell = shell;
5451 esurface->view = view;
5452
5453 esurface->row = i / shell->exposay.grid_size;
5454 esurface->column = i % shell->exposay.grid_size;
5455
5456 esurface->x = shell->exposay.hpadding_outer;
5457 esurface->x += pad * esurface->column;
5458 esurface->y = shell->exposay.vpadding_outer;
5459 esurface->y += pad * esurface->row;
5460
5461 if (esurface->row == shell->exposay.grid_size - 1)
5462 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5463
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005464 if (view->surface->width > view->surface->height)
5465 esurface->scale = shell->exposay.surface_size / (float) view->surface->width;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005466 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005467 esurface->scale = shell->exposay.surface_size / (float) view->surface->height;
5468 esurface->width = view->surface->width * esurface->scale;
5469 esurface->height = view->surface->height * esurface->scale;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005470
5471 if (shell->exposay.focus_current == esurface->view)
5472 exposay_highlight_surface(shell, esurface);
5473
5474 exposay_animate_in(esurface);
5475
5476 i++;
5477 }
5478
5479 weston_compositor_schedule_repaint(shell->compositor);
5480
5481 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5482}
5483
5484static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005485exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5486 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005487{
5488 struct desktop_shell *shell =
5489 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5490
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005491 weston_pointer_move(grab->pointer, x, y);
5492
Daniel Stonedf8133b2013-11-19 11:37:14 +01005493 exposay_pick(shell,
5494 wl_fixed_to_int(grab->pointer->x),
5495 wl_fixed_to_int(grab->pointer->y));
5496}
5497
5498static void
5499exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5500 uint32_t state_w)
5501{
5502 struct desktop_shell *shell =
5503 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5504 struct weston_seat *seat = grab->pointer->seat;
5505 enum wl_pointer_button_state state = state_w;
5506
5507 if (button != BTN_LEFT)
5508 return;
5509
5510 /* Store the surface we clicked on, and don't do anything if we end up
5511 * releasing on a different surface. */
5512 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5513 shell->exposay.clicked = shell->exposay.focus_current;
5514 return;
5515 }
5516
5517 if (shell->exposay.focus_current == shell->exposay.clicked)
5518 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5519 else
5520 shell->exposay.clicked = NULL;
5521}
5522
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005523static void
5524exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
5525{
5526 struct desktop_shell *shell =
5527 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5528
5529 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5530}
5531
Daniel Stonedf8133b2013-11-19 11:37:14 +01005532static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5533 noop_grab_focus,
5534 exposay_motion,
5535 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005536 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005537};
5538
5539static int
5540exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5541{
5542 struct exposay_surface *esurface;
5543
5544 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5545 if (esurface->row != row || esurface->column != column)
5546 continue;
5547
5548 exposay_highlight_surface(shell, esurface);
5549 return 1;
5550 }
5551
5552 return 0;
5553}
5554
5555static void
5556exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5557 uint32_t state_w)
5558{
5559 struct weston_seat *seat = grab->keyboard->seat;
5560 struct desktop_shell *shell =
5561 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5562 enum wl_keyboard_key_state state = state_w;
5563
5564 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5565 return;
5566
5567 switch (key) {
5568 case KEY_ESC:
5569 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5570 break;
5571 case KEY_ENTER:
5572 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5573 break;
5574 case KEY_UP:
5575 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5576 shell->exposay.column_current);
5577 break;
5578 case KEY_DOWN:
5579 /* Special case for trying to move to the bottom row when it
5580 * has fewer items than all the others. */
5581 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5582 shell->exposay.column_current) &&
5583 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5584 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5585 (shell->exposay.num_surfaces %
5586 shell->exposay.grid_size) - 1);
5587 }
5588 break;
5589 case KEY_LEFT:
5590 exposay_maybe_move(shell, shell->exposay.row_current,
5591 shell->exposay.column_current - 1);
5592 break;
5593 case KEY_RIGHT:
5594 exposay_maybe_move(shell, shell->exposay.row_current,
5595 shell->exposay.column_current + 1);
5596 break;
5597 case KEY_TAB:
5598 /* Try to move right, then down (and to the leftmost column),
5599 * then if all else fails, to the top left. */
5600 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5601 shell->exposay.column_current + 1) &&
5602 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5603 exposay_maybe_move(shell, 0, 0);
5604 break;
5605 default:
5606 break;
5607 }
5608}
5609
5610static void
5611exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5612 uint32_t mods_depressed, uint32_t mods_latched,
5613 uint32_t mods_locked, uint32_t group)
5614{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005615 struct desktop_shell *shell =
5616 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5617 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5618
5619 /* We want to know when mod has been pressed and released.
5620 * FIXME: There is a problem here: if mod is pressed, then a key
5621 * is pressed and released, then mod is released, we will treat that
5622 * as if only mod had been pressed and released. */
5623 if (seat->modifier_state) {
5624 if (seat->modifier_state == shell->binding_modifier) {
5625 shell->exposay.mod_pressed = true;
5626 } else {
5627 shell->exposay.mod_invalid = true;
5628 }
5629 } else {
5630 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
5631 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5632
5633 shell->exposay.mod_invalid = false;
5634 shell->exposay.mod_pressed = false;
5635 }
5636
5637 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005638}
5639
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005640static void
5641exposay_cancel(struct weston_keyboard_grab *grab)
5642{
5643 struct desktop_shell *shell =
5644 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5645
5646 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5647}
5648
Daniel Stonedf8133b2013-11-19 11:37:14 +01005649static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5650 exposay_key,
5651 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005652 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005653};
5654
5655/**
5656 * Called when the transition from overview -> inactive has completed.
5657 */
5658static enum exposay_layout_state
5659exposay_set_inactive(struct desktop_shell *shell)
5660{
5661 struct weston_seat *seat = shell->exposay.seat;
5662
5663 weston_keyboard_end_grab(seat->keyboard);
5664 weston_pointer_end_grab(seat->pointer);
5665 if (seat->keyboard->input_method_resource)
5666 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5667
5668 return EXPOSAY_LAYOUT_INACTIVE;
5669}
5670
5671/**
5672 * Begins the transition from overview to inactive. */
5673static enum exposay_layout_state
5674exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5675{
5676 struct exposay_surface *esurface;
5677
5678 /* Call activate() before we start the animations to avoid
5679 * animating back the old state and then immediately transitioning
5680 * to the new. */
5681 if (switch_focus && shell->exposay.focus_current)
5682 activate(shell, shell->exposay.focus_current->surface,
5683 shell->exposay.seat);
5684 else if (shell->exposay.focus_prev)
5685 activate(shell, shell->exposay.focus_prev->surface,
5686 shell->exposay.seat);
5687
5688 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5689 exposay_animate_out(esurface);
5690 weston_compositor_schedule_repaint(shell->compositor);
5691
5692 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5693}
5694
5695static enum exposay_layout_state
5696exposay_transition_active(struct desktop_shell *shell)
5697{
5698 struct weston_seat *seat = shell->exposay.seat;
5699
5700 shell->exposay.workspace = get_current_workspace(shell);
5701 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5702 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5703 shell->exposay.clicked = NULL;
5704 wl_list_init(&shell->exposay.surface_list);
5705
5706 lower_fullscreen_layer(shell);
5707 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5708 weston_keyboard_start_grab(seat->keyboard,
5709 &shell->exposay.grab_kbd);
5710 weston_keyboard_set_focus(seat->keyboard, NULL);
5711
5712 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5713 weston_pointer_start_grab(seat->pointer,
5714 &shell->exposay.grab_ptr);
5715 weston_pointer_set_focus(seat->pointer, NULL,
5716 seat->pointer->x, seat->pointer->y);
5717
5718 return exposay_layout(shell);
5719}
5720
5721static void
5722exposay_check_state(struct desktop_shell *shell)
5723{
5724 enum exposay_layout_state state_new = shell->exposay.state_cur;
5725 int do_switch = 0;
5726
5727 /* Don't do anything whilst animations are running, just store up
5728 * target state changes and only act on them when the animations have
5729 * completed. */
5730 if (exposay_is_animating(shell))
5731 return;
5732
5733 switch (shell->exposay.state_target) {
5734 case EXPOSAY_TARGET_OVERVIEW:
5735 switch (shell->exposay.state_cur) {
5736 case EXPOSAY_LAYOUT_OVERVIEW:
5737 goto out;
5738 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5739 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5740 break;
5741 default:
5742 state_new = exposay_transition_active(shell);
5743 break;
5744 }
5745 break;
5746
5747 case EXPOSAY_TARGET_SWITCH:
5748 do_switch = 1; /* fallthrough */
5749 case EXPOSAY_TARGET_CANCEL:
5750 switch (shell->exposay.state_cur) {
5751 case EXPOSAY_LAYOUT_INACTIVE:
5752 goto out;
5753 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5754 state_new = exposay_set_inactive(shell);
5755 break;
5756 default:
5757 state_new = exposay_transition_inactive(shell, do_switch);
5758 break;
5759 }
5760
5761 break;
5762 }
5763
5764out:
5765 shell->exposay.state_cur = state_new;
5766}
5767
5768static void
5769exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5770 struct weston_seat *seat)
5771{
5772 shell->exposay.state_target = state;
5773 shell->exposay.seat = seat;
5774 exposay_check_state(shell);
5775}
5776
5777static void
5778exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5779 void *data)
5780{
5781 struct desktop_shell *shell = data;
5782
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005783 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005784}
5785
Pekka Paalanen3c647232011-12-22 13:43:43 +02005786static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005787backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005788 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005789{
5790 struct weston_compositor *compositor = data;
5791 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005792 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005793
5794 /* TODO: we're limiting to simple use cases, where we assume just
5795 * control on the primary display. We'd have to extend later if we
5796 * ever get support for setting backlights on random desktop LCD
5797 * panels though */
5798 output = get_default_output(compositor);
5799 if (!output)
5800 return;
5801
5802 if (!output->set_backlight)
5803 return;
5804
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005805 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5806 backlight_new = output->backlight_current - 25;
5807 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5808 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005809
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005810 if (backlight_new < 5)
5811 backlight_new = 5;
5812 if (backlight_new > 255)
5813 backlight_new = 255;
5814
5815 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005816 output->set_backlight(output, output->backlight_current);
5817}
5818
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005819struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005820 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005821 struct weston_seat *seat;
5822 uint32_t key[2];
5823 int key_released[2];
5824};
5825
5826static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005827debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005828 uint32_t key, uint32_t state)
5829{
5830 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005831 struct weston_compositor *ec = db->seat->compositor;
5832 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005833 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005834 uint32_t serial;
5835 int send = 0, terminate = 0;
5836 int check_binding = 1;
5837 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005838 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005839
5840 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5841 /* Do not run bindings on key releases */
5842 check_binding = 0;
5843
5844 for (i = 0; i < 2; i++)
5845 if (key == db->key[i])
5846 db->key_released[i] = 1;
5847
5848 if (db->key_released[0] && db->key_released[1]) {
5849 /* All key releases been swalled so end the grab */
5850 terminate = 1;
5851 } else if (key != db->key[0] && key != db->key[1]) {
5852 /* Should not swallow release of other keys */
5853 send = 1;
5854 }
5855 } else if (key == db->key[0] && !db->key_released[0]) {
5856 /* Do not check bindings for the first press of the binding
5857 * key. This allows it to be used as a debug shortcut.
5858 * We still need to swallow this event. */
5859 check_binding = 0;
5860 } else if (db->key[1]) {
5861 /* If we already ran a binding don't process another one since
5862 * we can't keep track of all the binding keys that were
5863 * pressed in order to swallow the release events. */
5864 send = 1;
5865 check_binding = 0;
5866 }
5867
5868 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005869 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5870 key, state)) {
5871 /* We ran a binding so swallow the press and keep the
5872 * grab to swallow the released too. */
5873 send = 0;
5874 terminate = 0;
5875 db->key[1] = key;
5876 } else {
5877 /* Terminate the grab since the key pressed is not a
5878 * debug binding key. */
5879 send = 1;
5880 terminate = 1;
5881 }
5882 }
5883
5884 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005885 serial = wl_display_next_serial(display);
5886 resource_list = &grab->keyboard->focus_resource_list;
5887 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005888 wl_keyboard_send_key(resource, serial, time, key, state);
5889 }
5890 }
5891
5892 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005893 weston_keyboard_end_grab(grab->keyboard);
5894 if (grab->keyboard->input_method_resource)
5895 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005896 free(db);
5897 }
5898}
5899
5900static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005901debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005902 uint32_t mods_depressed, uint32_t mods_latched,
5903 uint32_t mods_locked, uint32_t group)
5904{
5905 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005906 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005907
Neil Roberts96d790e2013-09-19 17:32:00 +01005908 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005909
Neil Roberts96d790e2013-09-19 17:32:00 +01005910 wl_resource_for_each(resource, resource_list) {
5911 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5912 mods_latched, mods_locked, group);
5913 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005914}
5915
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005916static void
5917debug_binding_cancel(struct weston_keyboard_grab *grab)
5918{
5919 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5920
5921 weston_keyboard_end_grab(grab->keyboard);
5922 free(db);
5923}
5924
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005925struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005926 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005927 debug_binding_modifiers,
5928 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005929};
5930
5931static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005932debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005933{
5934 struct debug_binding_grab *grab;
5935
5936 grab = calloc(1, sizeof *grab);
5937 if (!grab)
5938 return;
5939
5940 grab->seat = (struct weston_seat *) seat;
5941 grab->key[0] = key;
5942 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005943 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005944}
5945
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005946static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005947force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005948 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005949{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005950 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005951 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005952 struct desktop_shell *shell = data;
5953 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005954 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005955
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005956 focus_surface = seat->keyboard->focus;
5957 if (!focus_surface)
5958 return;
5959
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005960 wl_signal_emit(&compositor->kill_signal, focus_surface);
5961
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005962 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005963 wl_client_get_credentials(client, &pid, NULL, NULL);
5964
5965 /* Skip clients that we launched ourselves (the credentials of
5966 * the socketpair is ours) */
5967 if (pid == getpid())
5968 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005969
Daniel Stone325fc2d2012-05-30 16:31:58 +01005970 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005971}
5972
5973static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005974workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005975 uint32_t key, void *data)
5976{
5977 struct desktop_shell *shell = data;
5978 unsigned int new_index = shell->workspaces.current;
5979
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005980 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005981 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005982 if (new_index != 0)
5983 new_index--;
5984
5985 change_workspace(shell, new_index);
5986}
5987
5988static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005989workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005990 uint32_t key, void *data)
5991{
5992 struct desktop_shell *shell = data;
5993 unsigned int new_index = shell->workspaces.current;
5994
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005995 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005996 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005997 if (new_index < shell->workspaces.num - 1)
5998 new_index++;
5999
6000 change_workspace(shell, new_index);
6001}
6002
6003static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006004workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006005 uint32_t key, void *data)
6006{
6007 struct desktop_shell *shell = data;
6008 unsigned int new_index;
6009
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006010 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006011 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006012 new_index = key - KEY_F1;
6013 if (new_index >= shell->workspaces.num)
6014 new_index = shell->workspaces.num - 1;
6015
6016 change_workspace(shell, new_index);
6017}
6018
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006019static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006020workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006021 uint32_t key, void *data)
6022{
6023 struct desktop_shell *shell = data;
6024 unsigned int new_index = shell->workspaces.current;
6025
6026 if (shell->locked)
6027 return;
6028
6029 if (new_index != 0)
6030 new_index--;
6031
6032 take_surface_to_workspace_by_seat(shell, seat, new_index);
6033}
6034
6035static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006036workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006037 uint32_t key, void *data)
6038{
6039 struct desktop_shell *shell = data;
6040 unsigned int new_index = shell->workspaces.current;
6041
6042 if (shell->locked)
6043 return;
6044
6045 if (new_index < shell->workspaces.num - 1)
6046 new_index++;
6047
6048 take_surface_to_workspace_by_seat(shell, seat, new_index);
6049}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006050
6051static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006052handle_output_destroy(struct wl_listener *listener, void *data)
6053{
6054 struct shell_output *output_listener =
6055 container_of(listener, struct shell_output, destroy_listener);
6056
6057 wl_list_remove(&output_listener->destroy_listener.link);
6058 wl_list_remove(&output_listener->link);
6059 free(output_listener);
6060}
6061
6062static void
6063create_shell_output(struct desktop_shell *shell,
6064 struct weston_output *output)
6065{
6066 struct shell_output *shell_output;
6067
6068 shell_output = zalloc(sizeof *shell_output);
6069 if (shell_output == NULL)
6070 return;
6071
6072 shell_output->output = output;
6073 shell_output->shell = shell;
6074 shell_output->destroy_listener.notify = handle_output_destroy;
6075 wl_signal_add(&output->destroy_signal,
6076 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006077 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006078}
6079
6080static void
6081handle_output_create(struct wl_listener *listener, void *data)
6082{
6083 struct desktop_shell *shell =
6084 container_of(listener, struct desktop_shell, output_create_listener);
6085 struct weston_output *output = (struct weston_output *)data;
6086
6087 create_shell_output(shell, output);
6088}
6089
6090static void
6091setup_output_destroy_handler(struct weston_compositor *ec,
6092 struct desktop_shell *shell)
6093{
6094 struct weston_output *output;
6095
6096 wl_list_init(&shell->output_list);
6097 wl_list_for_each(output, &ec->output_list, link)
6098 create_shell_output(shell, output);
6099
6100 shell->output_create_listener.notify = handle_output_create;
6101 wl_signal_add(&ec->output_created_signal,
6102 &shell->output_create_listener);
6103}
6104
6105static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006106shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006107{
Tiago Vignattibe143262012-04-16 17:31:41 +03006108 struct desktop_shell *shell =
6109 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006110 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006111 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006112
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006113 if (shell->child.client)
6114 wl_client_destroy(shell->child.client);
6115
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006116 wl_list_remove(&shell->idle_listener.link);
6117 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006118 wl_list_remove(&shell->show_input_panel_listener.link);
6119 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006120
Xiong Zhang6b481422013-10-23 13:58:32 +08006121 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6122 wl_list_remove(&shell_output->destroy_listener.link);
6123 wl_list_remove(&shell_output->link);
6124 free(shell_output);
6125 }
6126
6127 wl_list_remove(&shell->output_create_listener.link);
6128
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006129 wl_array_for_each(ws, &shell->workspaces.array)
6130 workspace_destroy(*ws);
6131 wl_array_release(&shell->workspaces.array);
6132
Pekka Paalanen3c647232011-12-22 13:43:43 +02006133 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006134 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006135 free(shell);
6136}
6137
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006138static void
6139shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6140{
6141 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006142 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006143
6144 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006145 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6146 MODIFIER_CTRL | MODIFIER_ALT,
6147 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01006148 weston_compositor_add_key_binding(ec, KEY_TAB,
6149 MODIFIER_ALT,
6150 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006151 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6152 click_to_activate_binding,
6153 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006154 weston_compositor_add_touch_binding(ec, 0,
6155 touch_to_activate_binding,
6156 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006157 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6158 MODIFIER_SUPER | MODIFIER_ALT,
6159 surface_opacity_binding, NULL);
6160 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6161 MODIFIER_SUPER, zoom_axis_binding,
6162 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006163
6164 /* configurable bindings */
6165 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006166 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6167 zoom_key_binding, NULL);
6168 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6169 zoom_key_binding, NULL);
6170 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6171 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006172 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006173 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6174 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006175
6176 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6177 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6178 rotate_binding, NULL);
6179
Daniel Stone325fc2d2012-05-30 16:31:58 +01006180 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6181 shell);
6182 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6183 ec);
6184 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6185 backlight_binding, ec);
6186 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6187 ec);
6188 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6189 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006190 weston_compositor_add_key_binding(ec, KEY_K, mod,
6191 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006192 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6193 workspace_up_binding, shell);
6194 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6195 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006196 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6197 workspace_move_surface_up_binding,
6198 shell);
6199 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6200 workspace_move_surface_down_binding,
6201 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006202
Daniel Stonedf8133b2013-11-19 11:37:14 +01006203 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6204
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006205 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6206 if (shell->workspaces.num > 1) {
6207 num_workspace_bindings = shell->workspaces.num;
6208 if (num_workspace_bindings > 6)
6209 num_workspace_bindings = 6;
6210 for (i = 0; i < num_workspace_bindings; i++)
6211 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6212 workspace_f_binding,
6213 shell);
6214 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006215
6216 /* Debug bindings */
6217 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6218 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006219}
6220
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006221WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006222module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006223 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006224{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006225 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006226 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006227 struct workspace **pws;
6228 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006229 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006230
Peter Huttererf3d62272013-08-08 11:57:05 +10006231 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006232 if (shell == NULL)
6233 return -1;
6234
Kristian Høgsberg75840622011-09-06 13:48:16 -04006235 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006236
6237 shell->destroy_listener.notify = shell_destroy;
6238 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006239 shell->idle_listener.notify = idle_handler;
6240 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6241 shell->wake_listener.notify = wake_handler;
6242 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006243 shell->show_input_panel_listener.notify = show_input_panels;
6244 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6245 shell->hide_input_panel_listener.notify = hide_input_panels;
6246 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006247 shell->update_input_panel_listener.notify = update_input_panels;
6248 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006249 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006250 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006251 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006252 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006253 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006254 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006255 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006256 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006257 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006258 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006259 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006260
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006261 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006262
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006263 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6264 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006265 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6266 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006267 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006268
6269 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006270 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006271
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006272 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006273
Daniel Stonedf8133b2013-11-19 11:37:14 +01006274 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6275 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6276
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006277 for (i = 0; i < shell->workspaces.num; i++) {
6278 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6279 if (pws == NULL)
6280 return -1;
6281
6282 *pws = workspace_create();
6283 if (*pws == NULL)
6284 return -1;
6285 }
6286 activate_workspace(shell, 0);
6287
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006288 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006289 wl_list_init(&shell->workspaces.animation.link);
6290 shell->workspaces.animation.frame = animate_workspace_change_frame;
6291
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006292 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006293 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006294 return -1;
6295
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006296 if (wl_global_create(ec->wl_display,
6297 &desktop_shell_interface, 2,
6298 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006299 return -1;
6300
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006301 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6302 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006303 return -1;
6304
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006305 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006306 shell, bind_input_panel) == NULL)
6307 return -1;
6308
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006309 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6310 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006311 return -1;
6312
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006313 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006314
Xiong Zhang6b481422013-10-23 13:58:32 +08006315 setup_output_destroy_handler(ec, shell);
6316
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006317 loop = wl_display_get_event_loop(ec->wl_display);
6318 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006319
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006320 shell->screensaver.timer =
6321 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6322
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006323 wl_list_for_each(seat, &ec->seat_list, link)
6324 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006325
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006326 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006327
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006328 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006329
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006330 return 0;
6331}