blob: d346b4c5eba31f70ffa66fde395164f2d9f3d134 [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{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002255 shell_surface_set_parent(shsurf, NULL);
2256
Philip Withnallbecb77e2013-11-25 18:01:30 +00002257 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002258
2259 /* The layer_link is updated in set_surface_type(),
2260 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002261}
2262
2263static void
2264shell_surface_set_toplevel(struct wl_client *client,
2265 struct wl_resource *resource)
2266{
2267 struct shell_surface *surface = wl_resource_get_user_data(resource);
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
2283 shell_surface_set_parent(shsurf, parent);
2284
Rafael Antognollied207b42013-12-03 15:35:43 -02002285 shsurf->next_state.relative = true;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002286
2287 /* The layer_link is updated in set_surface_type(),
2288 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002289}
2290
2291static void
2292shell_surface_set_transient(struct wl_client *client,
2293 struct wl_resource *resource,
2294 struct wl_resource *parent_resource,
2295 int x, int y, uint32_t flags)
2296{
2297 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2298 struct weston_surface *parent =
2299 wl_resource_get_user_data(parent_resource);
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
2317 shell_surface_set_parent(shsurf, NULL);
2318
Rafael Antognolli03b16592013-12-03 15:35:42 -02002319 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002320
2321 shsurf->client->send_configure(shsurf->surface, 0,
2322 shsurf->output->width,
2323 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002324
2325 /* The layer_link is updated in set_surface_type(),
2326 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002327}
2328
2329static void
2330unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002331{
Philip Withnallf85fe842013-11-25 18:01:37 +00002332 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002333 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2334 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002335 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002336 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002337 shsurf->fullscreen_output = NULL;
2338
Alex Wu4539b082012-03-01 12:57:46 +08002339 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2340 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002341
Alex Wu4539b082012-03-01 12:57:46 +08002342 wl_list_remove(&shsurf->fullscreen.transform.link);
2343 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002344
Jason Ekstranda7af7042013-10-12 22:38:11 -05002345 if (shsurf->fullscreen.black_view)
2346 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2347 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002348
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 weston_view_set_position(shsurf->view,
2350 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002351 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002352 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002354 shsurf->saved_rotation_valid = false;
2355 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002356
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002357 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002358}
2359
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002360static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002361shell_surface_set_fullscreen(struct wl_client *client,
2362 struct wl_resource *resource,
2363 uint32_t method,
2364 uint32_t framerate,
2365 struct wl_resource *output_resource)
2366{
2367 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2368 struct weston_output *output;
2369
2370 if (output_resource)
2371 output = wl_resource_get_user_data(output_resource);
2372 else
2373 output = 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
2396 shell_surface_set_parent(shsurf, parent);
Philip Withnallb995e1d2013-11-25 18:01:39 +00002397
2398 shsurf->next_type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002399}
2400
2401static void
2402shell_surface_set_popup(struct wl_client *client,
2403 struct wl_resource *resource,
2404 struct wl_resource *seat_resource,
2405 uint32_t serial,
2406 struct wl_resource *parent_resource,
2407 int32_t x, int32_t y, uint32_t flags)
2408{
2409 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2410
Rafael Antognolli03b16592013-12-03 15:35:42 -02002411 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002412 set_popup(shsurf,
2413 wl_resource_get_user_data(parent_resource),
2414 wl_resource_get_user_data(seat_resource),
2415 serial, x, y);
2416}
2417
2418static void
2419set_maximized(struct shell_surface *shsurf,
2420 struct weston_output *output)
2421{
2422 struct desktop_shell *shell;
2423 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002424
Philip Withnall352e7ed2013-11-25 18:01:35 +00002425 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002426
2427 shell = shell_surface_get_shell(shsurf);
2428 panel_height = get_output_panel_height(shell, shsurf->output);
2429 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2430
2431 shsurf->client->send_configure(shsurf->surface, edges,
2432 shsurf->output->width,
2433 shsurf->output->height - panel_height);
2434
Philip Withnalldc4332f2013-11-25 18:01:38 +00002435 shell_surface_set_parent(shsurf, NULL);
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 Antognolli03b16592013-12-03 15:35:42 -02002471 surface_clear_next_states(shsurf);
2472 shsurf->next_state.maximized = true;
2473 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002474 set_maximized(shsurf, output);
2475}
2476
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002477/* This is only ever called from set_surface_type(), so there’s no need to
2478 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002479static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002480reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002481{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002482 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002483 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002484 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002485 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002486
2487 surface->type = SHELL_SURFACE_NONE;
2488 return 0;
2489}
2490
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002491static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002492set_full_output(struct shell_surface *shsurf)
2493{
2494 shsurf->saved_x = shsurf->view->geometry.x;
2495 shsurf->saved_y = shsurf->view->geometry.y;
2496 shsurf->saved_position_valid = true;
2497
2498 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2499 wl_list_remove(&shsurf->rotation.transform.link);
2500 wl_list_init(&shsurf->rotation.transform.link);
2501 weston_view_geometry_dirty(shsurf->view);
2502 shsurf->saved_rotation_valid = true;
2503 }
2504}
2505
2506static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002507set_surface_type(struct shell_surface *shsurf)
2508{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002509 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002510 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002511
Philip Withnallbecb77e2013-11-25 18:01:30 +00002512 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002513
2514 shsurf->type = shsurf->next_type;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002515 shsurf->state = shsurf->next_state;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002516 shsurf->next_type = SHELL_SURFACE_NONE;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002517 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002518
2519 switch (shsurf->type) {
2520 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002521 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002522 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002523 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002524 weston_view_set_position(shsurf->view,
2525 pev->geometry.x + shsurf->transient.x,
2526 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002527 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002528
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002529 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002530 weston_view_set_position(shsurf->view, shsurf->transient.x,
2531 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002532 break;
2533
Philip Withnall0f640e22013-11-25 18:01:31 +00002534 case SHELL_SURFACE_POPUP:
2535 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002536 default:
2537 break;
2538 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002539
2540 /* Update the surface’s layer. */
2541 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002542}
2543
Tiago Vignattibe143262012-04-16 17:31:41 +03002544static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002545shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002546{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002547 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002548}
2549
Alex Wu21858432012-04-01 20:13:08 +08002550static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002551black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002552
Jason Ekstranda7af7042013-10-12 22:38:11 -05002553static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002554create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002555 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002556 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002557{
2558 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002559 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002560
2561 surface = weston_surface_create(ec);
2562 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002563 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002564 return NULL;
2565 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566 view = weston_view_create(surface);
2567 if (surface == NULL) {
2568 weston_log("no memory\n");
2569 weston_surface_destroy(surface);
2570 return NULL;
2571 }
Alex Wu4539b082012-03-01 12:57:46 +08002572
Alex Wu21858432012-04-01 20:13:08 +08002573 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002574 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002575 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002576 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002577 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002578 pixman_region32_fini(&surface->input);
2579 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002580
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002581 surface->width = w;
2582 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002583 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002584
2585 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002586}
2587
Philip Withnalled8f1a92013-11-25 18:01:43 +00002588static void
2589shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2590{
2591 struct weston_output *output = shsurf->fullscreen_output;
2592
Rafael Antognolli03b16592013-12-03 15:35:42 -02002593 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002594
2595 if (!shsurf->fullscreen.black_view)
2596 shsurf->fullscreen.black_view =
2597 create_black_surface(shsurf->surface->compositor,
2598 shsurf->surface,
2599 output->x, output->y,
2600 output->width,
2601 output->height);
2602
2603 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2604 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2605 wl_list_insert(&shsurf->view->layer_link,
2606 &shsurf->fullscreen.black_view->layer_link);
2607 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2608 weston_surface_damage(shsurf->surface);
2609}
2610
Alex Wu4539b082012-03-01 12:57:46 +08002611/* Create black surface and append it to the associated fullscreen surface.
2612 * Handle size dismatch and positioning according to the method. */
2613static void
2614shell_configure_fullscreen(struct shell_surface *shsurf)
2615{
2616 struct weston_output *output = shsurf->fullscreen_output;
2617 struct weston_surface *surface = shsurf->surface;
2618 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002619 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002620 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002621
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002622 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2623 restore_output_mode(output);
2624
Philip Withnalled8f1a92013-11-25 18:01:43 +00002625 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002626
Jason Ekstranda7af7042013-10-12 22:38:11 -05002627 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002628 &surf_width, &surf_height);
2629
Alex Wu4539b082012-03-01 12:57:46 +08002630 switch (shsurf->fullscreen.type) {
2631 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002632 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002633 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002634 break;
2635 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002636 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002637 if (output->width == surf_width &&
2638 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639 weston_view_set_position(shsurf->view,
2640 output->x - surf_x,
2641 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002642 break;
2643 }
2644
Alex Wu4539b082012-03-01 12:57:46 +08002645 matrix = &shsurf->fullscreen.transform.matrix;
2646 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002647
Scott Moreau1bad5db2012-08-18 01:04:05 -06002648 output_aspect = (float) output->width /
2649 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002650 /* XXX: Use surf_width and surf_height here? */
2651 surface_aspect = (float) surface->width /
2652 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002653 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002654 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002655 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002656 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002657 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002658 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002659
Alex Wu4539b082012-03-01 12:57:46 +08002660 weston_matrix_scale(matrix, scale, scale, 1);
2661 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002662 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002663 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002664 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2665 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002666 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002667
Alex Wu4539b082012-03-01 12:57:46 +08002668 break;
2669 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002670 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002671 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002672 surf_width * surface->buffer_viewport.scale,
2673 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002674 shsurf->fullscreen.framerate};
2675
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002676 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002677 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002678 weston_view_set_position(shsurf->view,
2679 output->x - surf_x,
2680 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002681 shsurf->fullscreen.black_view->surface->width = output->width;
2682 shsurf->fullscreen.black_view->surface->height = output->height;
2683 weston_view_set_position(shsurf->fullscreen.black_view,
2684 output->x - surf_x,
2685 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002686 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002687 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002688 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002690 }
Alex Wubd3354b2012-04-17 17:20:49 +08002691 }
Alex Wu4539b082012-03-01 12:57:46 +08002692 break;
2693 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002694 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002695 break;
2696 default:
2697 break;
2698 }
2699}
2700
Alex Wu4539b082012-03-01 12:57:46 +08002701static void
2702shell_map_fullscreen(struct shell_surface *shsurf)
2703{
Alex Wubd3354b2012-04-17 17:20:49 +08002704 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002705}
2706
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002707static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002708set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2709{
2710 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002711 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002712 shsurf->transient.x = x;
2713 shsurf->transient.y = y;
2714 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002715
2716 shell_surface_set_parent(shsurf, NULL);
2717
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002718 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2719}
2720
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002721static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002722
2723static void
2724destroy_shell_seat(struct wl_listener *listener, void *data)
2725{
2726 struct shell_seat *shseat =
2727 container_of(listener,
2728 struct shell_seat, seat_destroy_listener);
2729 struct shell_surface *shsurf, *prev = NULL;
2730
2731 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002732 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002733 shseat->popup_grab.client = NULL;
2734
2735 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2736 shsurf->popup.shseat = NULL;
2737 if (prev) {
2738 wl_list_init(&prev->popup.grab_link);
2739 }
2740 prev = shsurf;
2741 }
2742 wl_list_init(&prev->popup.grab_link);
2743 }
2744
2745 wl_list_remove(&shseat->seat_destroy_listener.link);
2746 free(shseat);
2747}
2748
2749static struct shell_seat *
2750create_shell_seat(struct weston_seat *seat)
2751{
2752 struct shell_seat *shseat;
2753
2754 shseat = calloc(1, sizeof *shseat);
2755 if (!shseat) {
2756 weston_log("no memory to allocate shell seat\n");
2757 return NULL;
2758 }
2759
2760 shseat->seat = seat;
2761 wl_list_init(&shseat->popup_grab.surfaces_list);
2762
2763 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2764 wl_signal_add(&seat->destroy_signal,
2765 &shseat->seat_destroy_listener);
2766
2767 return shseat;
2768}
2769
2770static struct shell_seat *
2771get_shell_seat(struct weston_seat *seat)
2772{
2773 struct wl_listener *listener;
2774
2775 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2776 if (listener == NULL)
2777 return create_shell_seat(seat);
2778
2779 return container_of(listener,
2780 struct shell_seat, seat_destroy_listener);
2781}
2782
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002783static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002784popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002785{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002786 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002787 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002788 struct shell_seat *shseat =
2789 container_of(grab, struct shell_seat, popup_grab.grab);
2790 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002791 wl_fixed_t sx, sy;
2792
Jason Ekstranda7af7042013-10-12 22:38:11 -05002793 view = weston_compositor_pick_view(pointer->seat->compositor,
2794 pointer->x, pointer->y,
2795 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002796
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797 if (view && view->surface->resource &&
2798 wl_resource_get_client(view->surface->resource) == client) {
2799 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002800 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002801 weston_pointer_set_focus(pointer, NULL,
2802 wl_fixed_from_int(0),
2803 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002804 }
2805}
2806
2807static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002808popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2809 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002810{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002811 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002812 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002813 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002814
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002815 weston_pointer_move(pointer, x, y);
2816
Neil Roberts96d790e2013-09-19 17:32:00 +01002817 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002818 weston_view_from_global_fixed(pointer->focus,
2819 pointer->x, pointer->y,
2820 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002821 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002822 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002823}
2824
2825static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002826popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002827 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002828{
2829 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002830 struct shell_seat *shseat =
2831 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002832 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002833 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002834 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002835 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002836
Neil Roberts96d790e2013-09-19 17:32:00 +01002837 resource_list = &grab->pointer->focus_resource_list;
2838 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002839 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002840 wl_resource_for_each(resource, resource_list) {
2841 wl_pointer_send_button(resource, serial,
2842 time, button, state);
2843 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002844 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002845 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002846 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002847 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002848 }
2849
Daniel Stone4dbadb12012-05-30 16:31:51 +01002850 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002851 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002852}
2853
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002854static void
2855popup_grab_cancel(struct weston_pointer_grab *grab)
2856{
2857 popup_grab_end(grab->pointer);
2858}
2859
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002860static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002861 popup_grab_focus,
2862 popup_grab_motion,
2863 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002864 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002865};
2866
2867static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002868popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002869{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002870 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002871 struct shell_seat *shseat =
2872 container_of(grab, struct shell_seat, popup_grab.grab);
2873 struct shell_surface *shsurf;
2874 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002875
2876 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002877 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002878 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002879 shseat->popup_grab.grab.interface = NULL;
2880 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002881 /* Send the popup_done event to all the popups open */
2882 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002883 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002884 shsurf->popup.shseat = NULL;
2885 if (prev) {
2886 wl_list_init(&prev->popup.grab_link);
2887 }
2888 prev = shsurf;
2889 }
2890 wl_list_init(&prev->popup.grab_link);
2891 wl_list_init(&shseat->popup_grab.surfaces_list);
2892 }
2893}
2894
2895static void
2896add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2897{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002898 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002899
2900 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002901 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002902 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002903 /* We must make sure here that this popup was opened after
2904 * a mouse press, and not just by moving around with other
2905 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002906 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002907 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002908
Rob Bradforddfe31052013-06-26 19:49:11 +01002909 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002910 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002911 } else {
2912 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002913 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002914}
2915
2916static void
2917remove_popup_grab(struct shell_surface *shsurf)
2918{
2919 struct shell_seat *shseat = shsurf->popup.shseat;
2920
2921 wl_list_remove(&shsurf->popup.grab_link);
2922 wl_list_init(&shsurf->popup.grab_link);
2923 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002924 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002925 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002926 }
2927}
2928
2929static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002930shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002931{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002932 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002933 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002934
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 shsurf->surface->output = parent_view->output;
2936 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002937
Jason Ekstranda7af7042013-10-12 22:38:11 -05002938 weston_view_set_transform_parent(shsurf->view, parent_view);
2939 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2940 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002941
Kristian Høgsberge3148752013-05-06 23:19:49 -04002942 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002943 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002944 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002945 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002946 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002947 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002948}
2949
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002950static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002951 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002952 shell_surface_move,
2953 shell_surface_resize,
2954 shell_surface_set_toplevel,
2955 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002956 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002957 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002958 shell_surface_set_maximized,
2959 shell_surface_set_title,
2960 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002961};
2962
2963static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002964destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002965{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002966 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2967
Giulio Camuffo5085a752013-03-25 21:42:45 +01002968 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2969 remove_popup_grab(shsurf);
2970 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002971
Alex Wubd3354b2012-04-17 17:20:49 +08002972 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002973 shell_surface_is_top_fullscreen(shsurf))
2974 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002975
Jason Ekstranda7af7042013-10-12 22:38:11 -05002976 if (shsurf->fullscreen.black_view)
2977 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002978
Alex Wubd3354b2012-04-17 17:20:49 +08002979 /* As destroy_resource() use wl_list_for_each_safe(),
2980 * we can always remove the listener.
2981 */
2982 wl_list_remove(&shsurf->surface_destroy_listener.link);
2983 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002984 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002985 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002986
Jason Ekstranda7af7042013-10-12 22:38:11 -05002987 weston_view_destroy(shsurf->view);
2988
Philip Withnall648a4dd2013-11-25 18:01:44 +00002989 wl_list_remove(&shsurf->children_link);
2990
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002991 wl_list_remove(&shsurf->link);
2992 free(shsurf);
2993}
2994
2995static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002996shell_destroy_shell_surface(struct wl_resource *resource)
2997{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002998 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002999
3000 destroy_shell_surface(shsurf);
3001}
3002
3003static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003004shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003005{
3006 struct shell_surface *shsurf = container_of(listener,
3007 struct shell_surface,
3008 surface_destroy_listener);
3009
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003010 if (shsurf->resource)
3011 wl_resource_destroy(shsurf->resource);
3012 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003013 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003014}
3015
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003016static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003017shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003018
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003019static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003020get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003021{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003022 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003023 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003024 else
3025 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003026}
3027
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003028static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003029create_shell_surface(void *shell, struct weston_surface *surface,
3030 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003031{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003032 struct shell_surface *shsurf;
3033
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003034 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003035 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003036 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003037 }
3038
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003039 shsurf = calloc(1, sizeof *shsurf);
3040 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003041 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003042 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003043 }
3044
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045 shsurf->view = weston_view_create(surface);
3046 if (!shsurf->view) {
3047 weston_log("no memory to allocate shell surface\n");
3048 free(shsurf);
3049 return NULL;
3050 }
3051
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003052 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003053 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003054
Tiago Vignattibc052c92012-04-19 16:18:18 +03003055 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003056 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003057 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003058 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003059 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003060 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3061 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003062 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003063 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003064 wl_list_init(&shsurf->fullscreen.transform.link);
3065
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003066 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003067 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003068 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003069 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003070
3071 /* init link so its safe to always remove it in destroy_shell_surface */
3072 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003073 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003074
Pekka Paalanen460099f2012-01-20 16:48:25 +02003075 /* empty when not in use */
3076 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003077 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003078
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003079 wl_list_init(&shsurf->workspace_transform.link);
3080
Philip Withnall648a4dd2013-11-25 18:01:44 +00003081 wl_list_init(&shsurf->children_link);
3082 wl_list_init(&shsurf->children_list);
3083 shsurf->parent = NULL;
3084
Pekka Paalanen98262232011-12-01 10:42:22 +02003085 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003086 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003087
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003088 shsurf->client = client;
3089
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003090 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003091}
3092
Jason Ekstranda7af7042013-10-12 22:38:11 -05003093static struct weston_view *
3094get_primary_view(void *shell, struct shell_surface *shsurf)
3095{
3096 return shsurf->view;
3097}
3098
Tiago Vignattibc052c92012-04-19 16:18:18 +03003099static void
3100shell_get_shell_surface(struct wl_client *client,
3101 struct wl_resource *resource,
3102 uint32_t id,
3103 struct wl_resource *surface_resource)
3104{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003105 struct weston_surface *surface =
3106 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003107 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003108 struct shell_surface *shsurf;
3109
3110 if (get_shell_surface(surface)) {
3111 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003112 WL_DISPLAY_ERROR_INVALID_OBJECT,
3113 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003114 return;
3115 }
3116
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003117 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003118 if (!shsurf) {
3119 wl_resource_post_error(surface_resource,
3120 WL_DISPLAY_ERROR_INVALID_OBJECT,
3121 "surface->configure already set");
3122 return;
3123 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003124
Jason Ekstranda85118c2013-06-27 20:17:02 -05003125 shsurf->resource =
3126 wl_resource_create(client,
3127 &wl_shell_surface_interface, 1, id);
3128 wl_resource_set_implementation(shsurf->resource,
3129 &shell_surface_implementation,
3130 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003131}
3132
3133static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003134 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003135};
3136
Kristian Høgsberg07937562011-04-12 17:25:42 -04003137static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003138shell_fade(struct desktop_shell *shell, enum fade_type type);
3139
3140static int
3141screensaver_timeout(void *data)
3142{
3143 struct desktop_shell *shell = data;
3144
3145 shell_fade(shell, FADE_OUT);
3146
3147 return 1;
3148}
3149
3150static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003151handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003152{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003153 struct desktop_shell *shell =
3154 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003155
Pekka Paalanen18027e52011-12-02 16:31:49 +02003156 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003157
3158 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003159 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003160}
3161
3162static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003163launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003164{
3165 if (shell->screensaver.binding)
3166 return;
3167
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003168 if (!shell->screensaver.path) {
3169 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003170 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003171 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003172
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003173 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003174 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003175 return;
3176 }
3177
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003178 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003179 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003180 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003181 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003182}
3183
3184static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003185terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003186{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003187 if (shell->screensaver.process.pid == 0)
3188 return;
3189
3190 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003191}
3192
3193static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003194configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003195{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003196 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003197
Jason Ekstranda7af7042013-10-12 22:38:11 -05003198 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3199 if (v->output == ev->output && v != ev) {
3200 weston_view_unmap(v);
3201 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003202 }
3203 }
3204
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003205 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003206
Jason Ekstranda7af7042013-10-12 22:38:11 -05003207 if (wl_list_empty(&ev->layer_link)) {
3208 wl_list_insert(&layer->view_list, &ev->layer_link);
3209 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003210 }
3211}
3212
3213static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003214background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003215{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003216 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003217 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003218
Jason Ekstranda7af7042013-10-12 22:38:11 -05003219 view = container_of(es->views.next, struct weston_view, surface_link);
3220
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003221 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003222}
3223
3224static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003225desktop_shell_set_background(struct wl_client *client,
3226 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003227 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003228 struct wl_resource *surface_resource)
3229{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003230 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003231 struct weston_surface *surface =
3232 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003233 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003234
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003235 if (surface->configure) {
3236 wl_resource_post_error(surface_resource,
3237 WL_DISPLAY_ERROR_INVALID_OBJECT,
3238 "surface role already assigned");
3239 return;
3240 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003241
Jason Ekstranda7af7042013-10-12 22:38:11 -05003242 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3243 weston_view_destroy(view);
3244 view = weston_view_create(surface);
3245
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003246 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003247 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003248 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003249 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003250 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003251 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003252 surface->output->width,
3253 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003254}
3255
3256static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003257panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003258{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003259 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003260 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003261
Jason Ekstranda7af7042013-10-12 22:38:11 -05003262 view = container_of(es->views.next, struct weston_view, surface_link);
3263
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003264 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003265}
3266
3267static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003268desktop_shell_set_panel(struct wl_client *client,
3269 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003270 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003271 struct wl_resource *surface_resource)
3272{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003273 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003274 struct weston_surface *surface =
3275 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003276 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003277
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003278 if (surface->configure) {
3279 wl_resource_post_error(surface_resource,
3280 WL_DISPLAY_ERROR_INVALID_OBJECT,
3281 "surface role already assigned");
3282 return;
3283 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003284
Jason Ekstranda7af7042013-10-12 22:38:11 -05003285 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3286 weston_view_destroy(view);
3287 view = weston_view_create(surface);
3288
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003289 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003290 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003291 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003292 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003293 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003294 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003295 surface->output->width,
3296 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003297}
3298
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003299static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003300lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003301{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003302 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003303 struct weston_view *view;
3304
3305 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003306
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003307 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003308 return;
3309
Jason Ekstranda7af7042013-10-12 22:38:11 -05003310 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003311
3312 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003313 wl_list_insert(&shell->lock_layer.view_list,
3314 &view->layer_link);
3315 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003316 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003317 }
3318}
3319
3320static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003321handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003322{
Tiago Vignattibe143262012-04-16 17:31:41 +03003323 struct desktop_shell *shell =
3324 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003325
Martin Minarik6d118362012-06-07 18:01:59 +02003326 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003327 shell->lock_surface = NULL;
3328}
3329
3330static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003331desktop_shell_set_lock_surface(struct wl_client *client,
3332 struct wl_resource *resource,
3333 struct wl_resource *surface_resource)
3334{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003335 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003336 struct weston_surface *surface =
3337 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003338
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003339 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003340
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003341 if (!shell->locked)
3342 return;
3343
Pekka Paalanen98262232011-12-01 10:42:22 +02003344 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003345
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003346 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003347 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003348 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003349
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003350 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003351 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003352 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003353}
3354
3355static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003356resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003357{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003358 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003359
Pekka Paalanen77346a62011-11-30 16:26:35 +02003360 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003361
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003362 wl_list_remove(&shell->lock_layer.link);
3363 wl_list_insert(&shell->compositor->cursor_layer.link,
3364 &shell->fullscreen_layer.link);
3365 wl_list_insert(&shell->fullscreen_layer.link,
3366 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003367 if (shell->showing_input_panels) {
3368 wl_list_insert(&shell->panel_layer.link,
3369 &shell->input_panel_layer.link);
3370 wl_list_insert(&shell->input_panel_layer.link,
3371 &ws->layer.link);
3372 } else {
3373 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3374 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003375
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003376 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003377
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003378 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003379 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003380 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003381}
3382
3383static void
3384desktop_shell_unlock(struct wl_client *client,
3385 struct wl_resource *resource)
3386{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003387 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003388
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003389 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003390
3391 if (shell->locked)
3392 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003393}
3394
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003395static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003396desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003397 struct wl_resource *resource,
3398 struct wl_resource *surface_resource)
3399{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003400 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003401
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003402 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003403 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003404}
3405
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003406static void
3407desktop_shell_desktop_ready(struct wl_client *client,
3408 struct wl_resource *resource)
3409{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003410 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003411
3412 shell_fade_startup(shell);
3413}
3414
Kristian Høgsberg75840622011-09-06 13:48:16 -04003415static const struct desktop_shell_interface desktop_shell_implementation = {
3416 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003417 desktop_shell_set_panel,
3418 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003419 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003420 desktop_shell_set_grab_surface,
3421 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003422};
3423
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003424static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003425get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003426{
3427 struct shell_surface *shsurf;
3428
3429 shsurf = get_shell_surface(surface);
3430 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003431 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003432 return shsurf->type;
3433}
3434
Kristian Høgsberg75840622011-09-06 13:48:16 -04003435static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003436move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003437{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003438 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003439 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003440 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003441
Pekka Paalanen01388e22013-04-25 13:57:44 +03003442 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003443 if (surface == NULL)
3444 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003445
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003446 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003447 if (shsurf == NULL || shsurf->state.fullscreen ||
3448 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003449 return;
3450
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003451 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003452}
3453
3454static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003455touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3456{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003457 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003458 struct weston_surface *surface;
3459 struct shell_surface *shsurf;
3460
3461 surface = weston_surface_get_main_surface(focus);
3462 if (surface == NULL)
3463 return;
3464
3465 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003466 if (shsurf == NULL || shsurf->state.fullscreen ||
3467 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003468 return;
3469
3470 surface_touch_move(shsurf, (struct weston_seat *) seat);
3471}
3472
3473static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003474resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003475{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003476 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003477 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003478 uint32_t edges = 0;
3479 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003480 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003481
Pekka Paalanen01388e22013-04-25 13:57:44 +03003482 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003483 if (surface == NULL)
3484 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003485
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003486 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003487 if (shsurf == NULL || shsurf->state.fullscreen ||
3488 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003489 return;
3490
Jason Ekstranda7af7042013-10-12 22:38:11 -05003491 weston_view_from_global(shsurf->view,
3492 wl_fixed_to_int(seat->pointer->grab_x),
3493 wl_fixed_to_int(seat->pointer->grab_y),
3494 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003495
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003496 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003497 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003498 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003499 edges |= 0;
3500 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003501 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003502
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003503 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003504 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003505 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003506 edges |= 0;
3507 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003508 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003509
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003510 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003511}
3512
3513static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003514surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003515 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003516{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003517 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003518 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003519 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003520 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003521
Pekka Paalanen01388e22013-04-25 13:57:44 +03003522 /* XXX: broken for windows containing sub-surfaces */
3523 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003524 if (surface == NULL)
3525 return;
3526
3527 shsurf = get_shell_surface(surface);
3528 if (!shsurf)
3529 return;
3530
Jason Ekstranda7af7042013-10-12 22:38:11 -05003531 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003532
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 if (shsurf->view->alpha > 1.0)
3534 shsurf->view->alpha = 1.0;
3535 if (shsurf->view->alpha < step)
3536 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003537
Jason Ekstranda7af7042013-10-12 22:38:11 -05003538 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003539 weston_surface_damage(surface);
3540}
3541
3542static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003543do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003544 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003545{
Daniel Stone37816df2012-05-16 18:45:18 +01003546 struct weston_seat *ws = (struct weston_seat *) seat;
3547 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003548 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003549 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003550
3551 wl_list_for_each(output, &compositor->output_list, link) {
3552 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003553 wl_fixed_to_double(seat->pointer->x),
3554 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003555 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003556 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003557 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003558 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003559 increment = -output->zoom.increment;
3560 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003561 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003562 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003563 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003564 else
3565 increment = 0;
3566
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003567 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003568
Scott Moreaue6603982012-06-11 13:07:51 -06003569 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003570 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003571 else if (output->zoom.level > output->zoom.max_level)
3572 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003573 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003574 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003575 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003576
Scott Moreaue6603982012-06-11 13:07:51 -06003577 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003578
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003580 }
3581 }
3582}
3583
Scott Moreauccbf29d2012-02-22 14:21:41 -07003584static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003585zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003586 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003587{
3588 do_zoom(seat, time, 0, axis, value);
3589}
3590
3591static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003592zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003593 void *data)
3594{
3595 do_zoom(seat, time, key, 0, 0);
3596}
3597
3598static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003599terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003600 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003601{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003602 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003603
Daniel Stone325fc2d2012-05-30 16:31:58 +01003604 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003605}
3606
3607static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003608lower_fullscreen_layer(struct desktop_shell *shell);
3609
3610struct alt_tab {
3611 struct desktop_shell *shell;
3612 struct weston_keyboard_grab grab;
3613
3614 struct wl_list *current;
3615
3616 struct wl_list preview_list;
3617};
3618
3619struct alt_tab_preview {
3620 struct alt_tab *alt_tab;
3621
3622 struct weston_view *view;
3623 struct weston_transform transform;
3624
3625 struct wl_listener listener;
3626
3627 struct wl_list link;
3628};
3629
3630static void
3631alt_tab_next(struct alt_tab *alt_tab)
3632{
3633 alt_tab->current = alt_tab->current->next;
3634
3635 /* Make sure we're not pointing to the list header e.g. after
3636 * cycling through the whole list. */
3637 if (alt_tab->current->next == alt_tab->preview_list.next)
3638 alt_tab->current = alt_tab->current->next;
3639}
3640
3641static void
3642alt_tab_destroy(struct alt_tab *alt_tab)
3643{
3644 struct alt_tab_preview *preview, *next;
3645 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3646
3647 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3648 preview = wl_container_of(alt_tab->current, preview, link);
3649
3650 activate(alt_tab->shell, preview->view->surface,
3651 (struct weston_seat *) keyboard->seat);
3652 }
3653
3654 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3655 wl_list_remove(&preview->link);
3656 wl_list_remove(&preview->listener.link);
3657 weston_view_destroy(preview->view);
3658 free(preview);
3659 }
3660
3661 weston_keyboard_end_grab(keyboard);
3662 if (keyboard->input_method_resource)
3663 keyboard->grab = &keyboard->input_method_grab;
3664
3665 free(alt_tab);
3666}
3667
3668static void
3669alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3670{
3671 struct alt_tab_preview *preview =
3672 container_of(listener, struct alt_tab_preview, listener);
3673 struct alt_tab *alt_tab = preview->alt_tab;
3674 int advance = 0;
3675
3676 /* If the preview that we're removing is the currently selected one,
3677 * we want to move to the next one. So we move to ->prev and then
3678 * call _next() after removing the preview. */
3679 if (alt_tab->current == &preview->link) {
3680 alt_tab->current = alt_tab->current->prev;
3681 advance = 1;
3682 }
3683
3684 wl_list_remove(&preview->listener.link);
3685 wl_list_remove(&preview->link);
3686
3687 free(preview);
3688
3689 if (advance)
3690 alt_tab_next(alt_tab);
3691
3692 /* If the last preview goes away, stop the alt-tab */
3693 if (wl_list_empty(alt_tab->current))
3694 alt_tab_destroy(alt_tab);
3695}
3696
3697static void
3698alt_tab_key(struct weston_keyboard_grab *grab,
3699 uint32_t time, uint32_t key, uint32_t state_w)
3700{
3701 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3702 enum wl_keyboard_key_state state = state_w;
3703
3704 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3705 alt_tab_next(alt_tab);
3706}
3707
3708static void
3709alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3710 uint32_t mods_depressed, uint32_t mods_latched,
3711 uint32_t mods_locked, uint32_t group)
3712{
3713 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3714 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3715
3716 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3717 alt_tab_destroy(alt_tab);
3718}
3719
3720static void
3721alt_tab_cancel(struct weston_keyboard_grab *grab)
3722{
3723 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3724
3725 alt_tab_destroy(alt_tab);
3726}
3727
3728static const struct weston_keyboard_grab_interface alt_tab_grab = {
3729 alt_tab_key,
3730 alt_tab_modifier,
3731 alt_tab_cancel,
3732};
3733
3734static int
3735view_for_alt_tab(struct weston_view *view)
3736{
Rafael Antognollied207b42013-12-03 15:35:43 -02003737 struct shell_surface *shsurf = get_shell_surface(view->surface);
3738 if (!shsurf)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003739 return 0;
3740
Rafael Antognollied207b42013-12-03 15:35:43 -02003741 if (shsurf->parent)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003742 return 0;
3743
3744 if (view != get_default_view(view->surface))
3745 return 0;
3746
3747 return 1;
3748}
3749
3750static void
3751alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3752 void *data)
3753{
3754 struct alt_tab *alt_tab;
3755 struct desktop_shell *shell = data;
3756 struct weston_output *output = get_default_output(shell->compositor);
3757 struct workspace *ws;
3758 struct weston_view *view;
3759 int num_surfaces = 0;
3760 int x, y, side, margin;
3761
3762 wl_list_for_each(view, &shell->compositor->view_list, link) {
3763
3764 if (view_for_alt_tab(view))
3765 num_surfaces++;
3766 }
3767
3768 if (!num_surfaces)
3769 return;
3770
3771 alt_tab = malloc(sizeof *alt_tab);
3772 if (!alt_tab)
3773 return;
3774
3775 alt_tab->shell = shell;
3776 wl_list_init(&alt_tab->preview_list);
3777 alt_tab->current = &alt_tab->preview_list;
3778
3779 restore_all_output_modes(shell->compositor);
3780 lower_fullscreen_layer(alt_tab->shell);
3781
3782 alt_tab->grab.interface = &alt_tab_grab;
3783 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3784 weston_keyboard_set_focus(seat->keyboard, NULL);
3785
3786 ws = get_current_workspace(shell);
3787
3788 /* FIXME: add some visual candy e.g. prelight the selected view
3789 * and/or add a black surrounding background à la gnome-shell */
3790
3791 /* Determine the size for each preview */
3792 side = output->width / num_surfaces;
3793 if (side > 200)
3794 side = 200;
3795 margin = side / 4;
3796 side -= margin;
3797
3798 x = margin;
3799 y = (output->height - side) / 2;
3800
3801 /* Create a view for each surface */
3802 wl_list_for_each(view, &shell->compositor->view_list, link) {
3803 struct alt_tab_preview *preview;
3804 struct weston_view *v;
3805 float scale;
3806
3807 if (!view_for_alt_tab(view))
3808 continue;
3809
3810 preview = malloc(sizeof *preview);
3811 if (!preview) {
3812 alt_tab_destroy(alt_tab);
3813 return;
3814 }
3815
3816 preview->alt_tab = alt_tab;
3817
3818 preview->view = v = weston_view_create(view->surface);
3819 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003820
3821 wl_list_remove(&v->layer_link);
3822 wl_list_insert(&ws->layer.view_list, &v->layer_link);
3823 weston_view_damage_below(v);
3824 weston_surface_damage(v->surface);
3825
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003826 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003827
3828 preview->listener.notify = alt_tab_handle_surface_destroy;
3829 wl_signal_add(&v->destroy_signal, &preview->listener);
3830
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003831 if (view->surface->width > view->surface->height)
3832 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003833 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003834 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003835
3836 wl_list_insert(&v->geometry.transformation_list,
3837 &preview->transform.link);
3838 weston_matrix_init(&preview->transform.matrix);
3839 weston_matrix_scale(&preview->transform.matrix,
3840 scale, scale, 1.0f);
3841
3842 weston_view_geometry_dirty(v);
3843 weston_compositor_schedule_repaint(v->surface->compositor);
3844
3845 /* Insert at the end of the list */
3846 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3847
3848 x += side + margin;
3849 }
3850
3851 /* Start at the second preview so a simple <alt>tab changes window.
3852 * We set `current' to the first preview and not the second because
3853 * we're going to receive a key press callback for the initial
3854 * <alt>tab which will make `current' point to the second element. */
3855 alt_tab_next(alt_tab);
3856}
3857
3858static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003859rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3860 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003861{
3862 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003863 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003864 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003865 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003866 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003867
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003868 weston_pointer_move(pointer, x, y);
3869
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003870 if (!shsurf)
3871 return;
3872
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003873 cx = 0.5f * shsurf->surface->width;
3874 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003875
Daniel Stone37816df2012-05-16 18:45:18 +01003876 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3877 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003878 r = sqrtf(dx * dx + dy * dy);
3879
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003880 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003881 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003882
3883 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003884 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003885 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003886
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003887 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003888 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003889
3890 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003891 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003892 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003893 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003894 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003895
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003896 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003897 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003898 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003899 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003900 wl_list_init(&shsurf->rotation.transform.link);
3901 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003902 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003903 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003904
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003905 /* We need to adjust the position of the surface
3906 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003907 cposx = shsurf->view->geometry.x + cx;
3908 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003909 dposx = rotate->center.x - cposx;
3910 dposy = rotate->center.y - cposy;
3911 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003912 weston_view_set_position(shsurf->view,
3913 shsurf->view->geometry.x + dposx,
3914 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003915 }
3916
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003917 /* Repaint implies weston_surface_update_transform(), which
3918 * lazily applies the damage due to rotation update.
3919 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003920 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003921}
3922
3923static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003924rotate_grab_button(struct weston_pointer_grab *grab,
3925 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003926{
3927 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003928 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003929 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003930 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003931 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003932
Daniel Stone4dbadb12012-05-30 16:31:51 +01003933 if (pointer->button_count == 0 &&
3934 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003935 if (shsurf)
3936 weston_matrix_multiply(&shsurf->rotation.rotation,
3937 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003938 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003939 free(rotate);
3940 }
3941}
3942
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003943static void
3944rotate_grab_cancel(struct weston_pointer_grab *grab)
3945{
3946 struct rotate_grab *rotate =
3947 container_of(grab, struct rotate_grab, base.grab);
3948
3949 shell_grab_end(&rotate->base);
3950 free(rotate);
3951}
3952
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003953static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003954 noop_grab_focus,
3955 rotate_grab_motion,
3956 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003957 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003958};
3959
3960static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003961surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003962{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003963 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003964 float dx, dy;
3965 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003966
Pekka Paalanen460099f2012-01-20 16:48:25 +02003967 rotate = malloc(sizeof *rotate);
3968 if (!rotate)
3969 return;
3970
Jason Ekstranda7af7042013-10-12 22:38:11 -05003971 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003972 surface->surface->width * 0.5f,
3973 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003974 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003975
Daniel Stone37816df2012-05-16 18:45:18 +01003976 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3977 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003978 r = sqrtf(dx * dx + dy * dy);
3979 if (r > 20.0f) {
3980 struct weston_matrix inverse;
3981
3982 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003983 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003984 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003985
3986 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003987 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003988 } else {
3989 weston_matrix_init(&surface->rotation.rotation);
3990 weston_matrix_init(&rotate->rotation);
3991 }
3992
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003993 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3994 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003995}
3996
3997static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003998rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003999 void *data)
4000{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004001 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004002 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004003 struct shell_surface *surface;
4004
Pekka Paalanen01388e22013-04-25 13:57:44 +03004005 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004006 if (base_surface == NULL)
4007 return;
4008
4009 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004010 if (surface == NULL || surface->state.fullscreen ||
4011 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004012 return;
4013
4014 surface_rotate(surface, seat);
4015}
4016
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004017/* Move all fullscreen layers down to the current workspace in a non-reversible
4018 * manner. This should be used when implementing shell-wide overlays, such as
4019 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004020static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004021lower_fullscreen_layer(struct desktop_shell *shell)
4022{
4023 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004024 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004025
4026 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004027 wl_list_for_each_reverse_safe(view, prev,
4028 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004029 layer_link) {
4030 wl_list_remove(&view->layer_link);
4031 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4032 weston_view_damage_below(view);
4033 weston_surface_damage(view->surface);
4034 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004035}
4036
4037static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004038activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004039 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004040{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004041 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004042 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004043 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004044 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004045 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004046
Pekka Paalanen01388e22013-04-25 13:57:44 +03004047 main_surface = weston_surface_get_main_surface(es);
4048
Daniel Stone37816df2012-05-16 18:45:18 +01004049 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004050
Jonas Ådahl8538b222012-08-29 22:13:03 +02004051 state = ensure_focus_state(shell, seat);
4052 if (state == NULL)
4053 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004054
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004055 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004056 state->keyboard_focus = es;
4057 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004058 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004059
Rafael Antognolli03b16592013-12-03 15:35:42 -02004060 shsurf = get_shell_surface(main_surface);
4061 if (shsurf->state.fullscreen)
4062 shell_configure_fullscreen(shsurf);
4063 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004064 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004065
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004066 if (shell->focus_animation_type != ANIMATION_NONE) {
4067 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004068 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004069 }
4070
4071 /* Update the surface’s layer. This brings it to the top of the stacking
4072 * order as appropriate. */
4073 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004074}
4075
Alex Wu21858432012-04-01 20:13:08 +08004076/* no-op func for checking black surface */
4077static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004078black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004079{
4080}
4081
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004082static bool
Alex Wu21858432012-04-01 20:13:08 +08004083is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4084{
4085 if (es->configure == black_surface_configure) {
4086 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004087 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004088 return true;
4089 }
4090 return false;
4091}
4092
Kristian Høgsberg75840622011-09-06 13:48:16 -04004093static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004094activate_binding(struct weston_seat *seat,
4095 struct desktop_shell *shell,
4096 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004097{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004098 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004099
Alex Wu9c35e6b2012-03-05 14:13:13 +08004100 if (!focus)
4101 return;
4102
Pekka Paalanen01388e22013-04-25 13:57:44 +03004103 if (is_black_surface(focus, &main_surface))
4104 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004105
Pekka Paalanen01388e22013-04-25 13:57:44 +03004106 main_surface = weston_surface_get_main_surface(focus);
4107 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004108 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004109
Neil Robertsa28c6932013-10-03 16:43:04 +01004110 activate(shell, focus, seat);
4111}
4112
4113static void
4114click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4115 void *data)
4116{
4117 if (seat->pointer->grab != &seat->pointer->default_grab)
4118 return;
4119
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004120 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004121}
4122
4123static void
4124touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4125{
4126 if (seat->touch->grab != &seat->touch->default_grab)
4127 return;
4128
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004129 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004130}
4131
4132static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004133lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004134{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004135 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004136
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004137 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004138 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004139 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004140 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004141
4142 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004143
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004144 /* Hide all surfaces by removing the fullscreen, panel and
4145 * toplevel layers. This way nothing else can show or receive
4146 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004147
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004148 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004149 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004150 if (shell->showing_input_panels)
4151 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004152 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004153 wl_list_insert(&shell->compositor->cursor_layer.link,
4154 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004155
Pekka Paalanen77346a62011-11-30 16:26:35 +02004156 launch_screensaver(shell);
4157
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004158 /* TODO: disable bindings that should not work while locked. */
4159
4160 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004161}
4162
4163static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004164unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004165{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004166 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004167 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004168 return;
4169 }
4170
4171 /* If desktop-shell client has gone away, unlock immediately. */
4172 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004173 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004174 return;
4175 }
4176
4177 if (shell->prepare_event_sent)
4178 return;
4179
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004180 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004181 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004182}
4183
4184static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004185shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004186{
4187 struct desktop_shell *shell = data;
4188
4189 shell->fade.animation = NULL;
4190
4191 switch (shell->fade.type) {
4192 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004193 weston_surface_destroy(shell->fade.view->surface);
4194 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004195 break;
4196 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004197 lock(shell);
4198 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004199 default:
4200 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004201 }
4202}
4203
Jason Ekstranda7af7042013-10-12 22:38:11 -05004204static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004205shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004206{
4207 struct weston_compositor *compositor = shell->compositor;
4208 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004209 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004210
4211 surface = weston_surface_create(compositor);
4212 if (!surface)
4213 return NULL;
4214
Jason Ekstranda7af7042013-10-12 22:38:11 -05004215 view = weston_view_create(surface);
4216 if (!view) {
4217 weston_surface_destroy(surface);
4218 return NULL;
4219 }
4220
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004221 surface->width = 8192;
4222 surface->height = 8192;
4223 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004224 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004225 wl_list_insert(&compositor->fade_layer.view_list,
4226 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004227 pixman_region32_init(&surface->input);
4228
Jason Ekstranda7af7042013-10-12 22:38:11 -05004229 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004230}
4231
4232static void
4233shell_fade(struct desktop_shell *shell, enum fade_type type)
4234{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004235 float tint;
4236
4237 switch (type) {
4238 case FADE_IN:
4239 tint = 0.0;
4240 break;
4241 case FADE_OUT:
4242 tint = 1.0;
4243 break;
4244 default:
4245 weston_log("shell: invalid fade type\n");
4246 return;
4247 }
4248
4249 shell->fade.type = type;
4250
Jason Ekstranda7af7042013-10-12 22:38:11 -05004251 if (shell->fade.view == NULL) {
4252 shell->fade.view = shell_fade_create_surface(shell);
4253 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004254 return;
4255
Jason Ekstranda7af7042013-10-12 22:38:11 -05004256 shell->fade.view->alpha = 1.0 - tint;
4257 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004258 }
4259
4260 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004261 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004262 else
4263 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004264 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004265 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004266 shell_fade_done, shell);
4267}
4268
4269static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004270do_shell_fade_startup(void *data)
4271{
4272 struct desktop_shell *shell = data;
4273
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004274 if (shell->startup_animation_type == ANIMATION_FADE)
4275 shell_fade(shell, FADE_IN);
4276 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004277 weston_surface_destroy(shell->fade.view->surface);
4278 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004279 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004280}
4281
4282static void
4283shell_fade_startup(struct desktop_shell *shell)
4284{
4285 struct wl_event_loop *loop;
4286
4287 if (!shell->fade.startup_timer)
4288 return;
4289
4290 wl_event_source_remove(shell->fade.startup_timer);
4291 shell->fade.startup_timer = NULL;
4292
4293 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4294 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4295}
4296
4297static int
4298fade_startup_timeout(void *data)
4299{
4300 struct desktop_shell *shell = data;
4301
4302 shell_fade_startup(shell);
4303 return 0;
4304}
4305
4306static void
4307shell_fade_init(struct desktop_shell *shell)
4308{
4309 /* Make compositor output all black, and wait for the desktop-shell
4310 * client to signal it is ready, then fade in. The timer triggers a
4311 * fade-in, in case the desktop-shell client takes too long.
4312 */
4313
4314 struct wl_event_loop *loop;
4315
Jason Ekstranda7af7042013-10-12 22:38:11 -05004316 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004317 weston_log("%s: warning: fade surface already exists\n",
4318 __func__);
4319 return;
4320 }
4321
Jason Ekstranda7af7042013-10-12 22:38:11 -05004322 shell->fade.view = shell_fade_create_surface(shell);
4323 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004324 return;
4325
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 weston_view_update_transform(shell->fade.view);
4327 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004328
4329 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4330 shell->fade.startup_timer =
4331 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4332 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4333}
4334
4335static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004336idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004337{
4338 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004339 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004340
4341 shell_fade(shell, FADE_OUT);
4342 /* lock() is called from shell_fade_done() */
4343}
4344
4345static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004346wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004347{
4348 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004349 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004350
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004351 unlock(shell);
4352}
4353
4354static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004355show_input_panels(struct wl_listener *listener, void *data)
4356{
4357 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004358 container_of(listener, struct desktop_shell,
4359 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004361
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004362 shell->text_input.surface = (struct weston_surface*)data;
4363
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004364 if (shell->showing_input_panels)
4365 return;
4366
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004367 shell->showing_input_panels = true;
4368
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004369 if (!shell->locked)
4370 wl_list_insert(&shell->panel_layer.link,
4371 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004372
Jason Ekstranda7af7042013-10-12 22:38:11 -05004373 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004374 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004376 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004377 wl_list_insert(&shell->input_panel_layer.view_list,
4378 &ipsurf->view->layer_link);
4379 weston_view_geometry_dirty(ipsurf->view);
4380 weston_view_update_transform(ipsurf->view);
4381 weston_surface_damage(ipsurf->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004382 weston_slide_run(ipsurf->view, ipsurf->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004383 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004384 }
4385}
4386
4387static void
4388hide_input_panels(struct wl_listener *listener, void *data)
4389{
4390 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004391 container_of(listener, struct desktop_shell,
4392 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004394
Jan Arne Petersen61381972013-01-31 15:52:21 +01004395 if (!shell->showing_input_panels)
4396 return;
4397
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004398 shell->showing_input_panels = false;
4399
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004400 if (!shell->locked)
4401 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004402
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 wl_list_for_each_safe(view, next,
4404 &shell->input_panel_layer.view_list, layer_link)
4405 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004406}
4407
4408static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004409update_input_panels(struct wl_listener *listener, void *data)
4410{
4411 struct desktop_shell *shell =
4412 container_of(listener, struct desktop_shell,
4413 update_input_panel_listener);
4414
4415 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4416}
4417
4418static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004419center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004420{
Giulio Camuffob8366642013-04-25 13:57:46 +03004421 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004422 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004423
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004425
4426 x = output->x + (output->width - width) / 2 - surf_x / 2;
4427 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004428
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004429 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004430}
4431
4432static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004433weston_view_set_initial_position(struct weston_view *view,
4434 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004435{
4436 struct weston_compositor *compositor = shell->compositor;
4437 int ix = 0, iy = 0;
4438 int range_x, range_y;
4439 int dx, dy, x, y, panel_height;
4440 struct weston_output *output, *target_output = NULL;
4441 struct weston_seat *seat;
4442
4443 /* As a heuristic place the new window on the same output as the
4444 * pointer. Falling back to the output containing 0, 0.
4445 *
4446 * TODO: Do something clever for touch too?
4447 */
4448 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004449 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004450 ix = wl_fixed_to_int(seat->pointer->x);
4451 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004452 break;
4453 }
4454 }
4455
4456 wl_list_for_each(output, &compositor->output_list, link) {
4457 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4458 target_output = output;
4459 break;
4460 }
4461 }
4462
4463 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004464 weston_view_set_position(view, 10 + random() % 400,
4465 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004466 return;
4467 }
4468
4469 /* Valid range within output where the surface will still be onscreen.
4470 * If this is negative it means that the surface is bigger than
4471 * output.
4472 */
4473 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004474 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004475 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004476 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004477
Rob Bradford4cb88c72012-08-13 15:18:44 +01004478 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004479 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004480 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004481 dx = 0;
4482
4483 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004484 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004485 else
4486 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004487
4488 x = target_output->x + dx;
4489 y = target_output->y + dy;
4490
Jason Ekstranda7af7042013-10-12 22:38:11 -05004491 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004492}
4493
4494static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004495map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004496 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004497{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004498 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004499 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004500 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004501 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004502
Pekka Paalanen77346a62011-11-30 16:26:35 +02004503 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004504 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004505 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004506 if (shsurf->state.fullscreen) {
4507 center_on_output(shsurf->view, shsurf->fullscreen_output);
4508 shell_map_fullscreen(shsurf);
4509 } else if (shsurf->state.maximized) {
4510 /* use surface configure to set the geometry */
4511 panel_height = get_output_panel_height(shell, shsurf->output);
4512 surface_subsurfaces_boundingbox(shsurf->surface,
4513 &surf_x, &surf_y, NULL, NULL);
4514 weston_view_set_position(shsurf->view,
4515 shsurf->output->x - surf_x,
4516 shsurf->output->y +
4517 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004518 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004519 weston_view_set_initial_position(shsurf->view, shell);
4520 }
Juan Zhao96879df2012-02-07 08:45:41 +08004521 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004522 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004523 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004524 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004525 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004526 weston_view_set_position(shsurf->view,
4527 shsurf->view->geometry.x + sx,
4528 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004529 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004530 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004531 default:
4532 ;
4533 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004534
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004535 /* Surface stacking order, see also activate(). */
4536 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004537
Jason Ekstranda7af7042013-10-12 22:38:11 -05004538 if (shsurf->type != SHELL_SURFACE_NONE) {
4539 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004540 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004541 shsurf->surface->output = shsurf->output;
4542 shsurf->view->output = shsurf->output;
4543 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004544 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004545
Rafael Antognollied207b42013-12-03 15:35:43 -02004546 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4547 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4548 }
4549
Jason Ekstranda7af7042013-10-12 22:38:11 -05004550 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004551 /* XXX: xwayland's using the same fields for transient type */
4552 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004553 if (shsurf->transient.flags ==
4554 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4555 break;
4556 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004557 if (shsurf->state.relative &&
4558 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4559 break;
4560 if (!shell->locked)
4561 break;
4562 wl_list_for_each(seat, &compositor->seat_list, link)
4563 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004564 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004565 case SHELL_SURFACE_POPUP:
4566 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004567 default:
4568 break;
4569 }
4570
Rafael Antognolli03b16592013-12-03 15:35:42 -02004571 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4572 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004573 {
4574 switch (shell->win_animation_type) {
4575 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004576 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004577 break;
4578 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004579 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004580 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004581 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004582 default:
4583 break;
4584 }
4585 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004586}
4587
4588static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004589configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004590 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004591{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004592 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004593 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004594 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004595
Pekka Paalanen77346a62011-11-30 16:26:35 +02004596 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004597
Rafael Antognolli03b16592013-12-03 15:35:42 -02004598 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004599 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004600 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004601 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004602 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4603 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004604 mx = shsurf->output->x - surf_x;
4605 my = shsurf->output->y +
4606 get_output_panel_height(shell,shsurf->output) - surf_y;
4607 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004608 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004609 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004610 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004611
Alex Wu4539b082012-03-01 12:57:46 +08004612 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004613 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004614 wl_list_for_each(view, &surface->views, surface_link)
4615 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004616
Rafael Antognolli03b16592013-12-03 15:35:42 -02004617 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004618 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004619 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004620}
4621
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004622static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004623shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004624{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004625 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004626 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004627
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004628 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004629
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004630 if (!weston_surface_is_mapped(es) &&
4631 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004632 remove_popup_grab(shsurf);
4633 }
4634
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004635 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004636 return;
4637
Rafael Antognolli03b16592013-12-03 15:35:42 -02004638 if ((shsurf->next_type != SHELL_SURFACE_NONE &&
4639 shsurf->type != shsurf->next_type) ||
4640 shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004641 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004642 type_changed = 1;
4643 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004644
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004645 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004646 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004647 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004648 shsurf->last_width != es->width ||
4649 shsurf->last_height != es->height) {
4650 shsurf->last_width = es->width;
4651 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004652 float from_x, from_y;
4653 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004654
Jason Ekstranda7af7042013-10-12 22:38:11 -05004655 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4656 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004657 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004658 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004659 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004660 }
4661}
4662
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004663static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004664
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004665static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004666desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004667{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004668 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004669 struct desktop_shell *shell =
4670 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004671
4672 shell->child.process.pid = 0;
4673 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004674
4675 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4676 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004677 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004678 shell->child.deathstamp = time;
4679 shell->child.deathcount = 0;
4680 }
4681
4682 shell->child.deathcount++;
4683 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004684 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004685 return;
4686 }
4687
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004688 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004689 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004690 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004691}
4692
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004693static void
4694launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004695{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004696 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004697
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004698 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004699 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004700 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004701 desktop_shell_sigchld);
4702
4703 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004704 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004705}
4706
4707static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004708bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4709{
Tiago Vignattibe143262012-04-16 17:31:41 +03004710 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004711 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004712
Jason Ekstranda85118c2013-06-27 20:17:02 -05004713 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4714 if (resource)
4715 wl_resource_set_implementation(resource, &shell_implementation,
4716 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004717}
4718
Kristian Høgsberg75840622011-09-06 13:48:16 -04004719static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004720unbind_desktop_shell(struct wl_resource *resource)
4721{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004722 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004723
4724 if (shell->locked)
4725 resume_desktop(shell);
4726
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004727 shell->child.desktop_shell = NULL;
4728 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004729}
4730
4731static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004732bind_desktop_shell(struct wl_client *client,
4733 void *data, uint32_t version, uint32_t id)
4734{
Tiago Vignattibe143262012-04-16 17:31:41 +03004735 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004736 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004737
Jason Ekstranda85118c2013-06-27 20:17:02 -05004738 resource = wl_resource_create(client, &desktop_shell_interface,
4739 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004740
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004741 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004742 wl_resource_set_implementation(resource,
4743 &desktop_shell_implementation,
4744 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004745 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004746
4747 if (version < 2)
4748 shell_fade_startup(shell);
4749
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004750 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004751 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004752
4753 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4754 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004755 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004756}
4757
Pekka Paalanen6e168112011-11-24 11:34:05 +02004758static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004759screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004760{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004761 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004762 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004763
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004764 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004765 return;
4766
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004767 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004768 if (!shell->locked)
4769 return;
4770
Jason Ekstranda7af7042013-10-12 22:38:11 -05004771 view = container_of(surface->views.next, struct weston_view, surface_link);
4772 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004773
Jason Ekstranda7af7042013-10-12 22:38:11 -05004774 if (wl_list_empty(&view->layer_link)) {
4775 wl_list_insert(shell->lock_layer.view_list.prev,
4776 &view->layer_link);
4777 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004778 wl_event_source_timer_update(shell->screensaver.timer,
4779 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004780 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004781 }
4782}
4783
4784static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004785screensaver_set_surface(struct wl_client *client,
4786 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004787 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004788 struct wl_resource *output_resource)
4789{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004790 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004791 struct weston_surface *surface =
4792 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004793 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004794 struct weston_view *view, *next;
4795
4796 /* Make sure we only have one view */
4797 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4798 weston_view_destroy(view);
4799 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004800
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004801 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004802 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004803 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004804}
4805
4806static const struct screensaver_interface screensaver_implementation = {
4807 screensaver_set_surface
4808};
4809
4810static void
4811unbind_screensaver(struct wl_resource *resource)
4812{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004813 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004814
Pekka Paalanen77346a62011-11-30 16:26:35 +02004815 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004816}
4817
4818static void
4819bind_screensaver(struct wl_client *client,
4820 void *data, uint32_t version, uint32_t id)
4821{
Tiago Vignattibe143262012-04-16 17:31:41 +03004822 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004823 struct wl_resource *resource;
4824
Jason Ekstranda85118c2013-06-27 20:17:02 -05004825 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004826
Pekka Paalanen77346a62011-11-30 16:26:35 +02004827 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004828 wl_resource_set_implementation(resource,
4829 &screensaver_implementation,
4830 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004831 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004832 return;
4833 }
4834
4835 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4836 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004837 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004838}
4839
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004840static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004841input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004842{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004843 struct input_panel_surface *ip_surface = surface->configure_private;
4844 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004845 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004846 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004847
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004848 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004849 return;
4850
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004851 if (!weston_surface_is_mapped(surface)) {
4852 if (!shell->showing_input_panels)
4853 return;
4854
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004855 show_surface = 1;
4856 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004857
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004858 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004859
4860 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004861 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4862 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004863 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004864 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
4865 y = ip_surface->output->y + ip_surface->output->height - surface->height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004866 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004867
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004868 weston_view_set_position(ip_surface->view, x, y);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004869
4870 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004871 wl_list_insert(&shell->input_panel_layer.view_list,
4872 &ip_surface->view->layer_link);
4873 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004874 weston_surface_damage(surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004875 weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004876 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004877}
4878
4879static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004880destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004881{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004882 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4883
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004884 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004885 wl_list_remove(&input_panel_surface->link);
4886
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004887 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004888 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004889
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004890 free(input_panel_surface);
4891}
4892
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004893static struct input_panel_surface *
4894get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004895{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004896 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004897 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004898 } else {
4899 return NULL;
4900 }
4901}
4902
4903static void
4904input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4905{
4906 struct input_panel_surface *ipsurface = container_of(listener,
4907 struct input_panel_surface,
4908 surface_destroy_listener);
4909
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004910 if (ipsurface->resource) {
4911 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004912 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004913 destroy_input_panel_surface(ipsurface);
4914 }
4915}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004916
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004917static struct input_panel_surface *
4918create_input_panel_surface(struct desktop_shell *shell,
4919 struct weston_surface *surface)
4920{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004921 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004922
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004923 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4924 if (!input_panel_surface)
4925 return NULL;
4926
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004927 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004928 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004929
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004930 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004931
4932 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004933 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004934
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004935 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004936 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004937 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004938 &input_panel_surface->surface_destroy_listener);
4939
4940 wl_list_init(&input_panel_surface->link);
4941
4942 return input_panel_surface;
4943}
4944
4945static void
4946input_panel_surface_set_toplevel(struct wl_client *client,
4947 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004948 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004949 uint32_t position)
4950{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004951 struct input_panel_surface *input_panel_surface =
4952 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004953 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004954
4955 wl_list_insert(&shell->input_panel.surfaces,
4956 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004957
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004958 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004959 input_panel_surface->panel = 0;
4960}
4961
4962static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004963input_panel_surface_set_overlay_panel(struct wl_client *client,
4964 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004965{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004966 struct input_panel_surface *input_panel_surface =
4967 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004968 struct desktop_shell *shell = input_panel_surface->shell;
4969
4970 wl_list_insert(&shell->input_panel.surfaces,
4971 &input_panel_surface->link);
4972
4973 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004974}
4975
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004976static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004977 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004978 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004979};
4980
4981static void
4982destroy_input_panel_surface_resource(struct wl_resource *resource)
4983{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004984 struct input_panel_surface *ipsurf =
4985 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004986
4987 destroy_input_panel_surface(ipsurf);
4988}
4989
4990static void
4991input_panel_get_input_panel_surface(struct wl_client *client,
4992 struct wl_resource *resource,
4993 uint32_t id,
4994 struct wl_resource *surface_resource)
4995{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004996 struct weston_surface *surface =
4997 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004998 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004999 struct input_panel_surface *ipsurf;
5000
5001 if (get_input_panel_surface(surface)) {
5002 wl_resource_post_error(surface_resource,
5003 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005004 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005005 return;
5006 }
5007
5008 ipsurf = create_input_panel_surface(shell, surface);
5009 if (!ipsurf) {
5010 wl_resource_post_error(surface_resource,
5011 WL_DISPLAY_ERROR_INVALID_OBJECT,
5012 "surface->configure already set");
5013 return;
5014 }
5015
Jason Ekstranda85118c2013-06-27 20:17:02 -05005016 ipsurf->resource =
5017 wl_resource_create(client,
5018 &wl_input_panel_surface_interface, 1, id);
5019 wl_resource_set_implementation(ipsurf->resource,
5020 &input_panel_surface_implementation,
5021 ipsurf,
5022 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005023}
5024
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005025static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005026 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005027};
5028
5029static void
5030unbind_input_panel(struct wl_resource *resource)
5031{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005032 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005033
5034 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005035}
5036
5037static void
5038bind_input_panel(struct wl_client *client,
5039 void *data, uint32_t version, uint32_t id)
5040{
5041 struct desktop_shell *shell = data;
5042 struct wl_resource *resource;
5043
Jason Ekstranda85118c2013-06-27 20:17:02 -05005044 resource = wl_resource_create(client,
5045 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005046
5047 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005048 wl_resource_set_implementation(resource,
5049 &input_panel_implementation,
5050 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005051 shell->input_panel.binding = resource;
5052 return;
5053 }
5054
5055 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5056 "interface object already bound");
5057 wl_resource_destroy(resource);
5058}
5059
Kristian Høgsberg07045392012-02-19 18:52:44 -05005060struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005061 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005062 struct weston_surface *current;
5063 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005064 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005065};
5066
5067static void
5068switcher_next(struct switcher *switcher)
5069{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005070 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005071 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005072 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005073 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005074
Jason Ekstranda7af7042013-10-12 22:38:11 -05005075 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005076 shsurf = get_shell_surface(view->surface);
5077 switch (shsurf->type) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005078 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005079 if (shsurf->parent)
5080 break;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005081 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005082 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005083 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005084 next = view->surface;
5085 prev = view->surface;
5086 view->alpha = 0.25;
5087 weston_view_geometry_dirty(view);
5088 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005089 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005090 case SHELL_SURFACE_POPUP:
5091 case SHELL_SURFACE_XWAYLAND:
5092 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005093 default:
5094 break;
5095 }
Alex Wu1659daa2012-04-01 20:13:09 +08005096
Jason Ekstranda7af7042013-10-12 22:38:11 -05005097 if (is_black_surface(view->surface, NULL)) {
5098 view->alpha = 0.25;
5099 weston_view_geometry_dirty(view);
5100 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005101 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005102 }
5103
5104 if (next == NULL)
5105 next = first;
5106
Alex Wu07b26062012-03-12 16:06:01 +08005107 if (next == NULL)
5108 return;
5109
Kristian Høgsberg07045392012-02-19 18:52:44 -05005110 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005111 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005112
5113 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005114 wl_list_for_each(view, &next->views, surface_link)
5115 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005116
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005117 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005118 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005119 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005120}
5121
5122static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005123switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005124{
5125 struct switcher *switcher =
5126 container_of(listener, struct switcher, listener);
5127
5128 switcher_next(switcher);
5129}
5130
5131static void
Daniel Stone351eb612012-05-31 15:27:47 -04005132switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005133{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005134 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005135 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005136 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005137
Jason Ekstranda7af7042013-10-12 22:38:11 -05005138 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005139 if (is_focus_view(view))
5140 continue;
5141
Jason Ekstranda7af7042013-10-12 22:38:11 -05005142 view->alpha = 1.0;
5143 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005144 }
5145
Alex Wu07b26062012-03-12 16:06:01 +08005146 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005147 activate(switcher->shell, switcher->current,
5148 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005149 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005150 weston_keyboard_end_grab(keyboard);
5151 if (keyboard->input_method_resource)
5152 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005153 free(switcher);
5154}
5155
5156static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005157switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005158 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005159{
5160 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005161 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005162
Daniel Stonec9785ea2012-05-30 16:31:52 +01005163 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005164 switcher_next(switcher);
5165}
5166
5167static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005168switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005169 uint32_t mods_depressed, uint32_t mods_latched,
5170 uint32_t mods_locked, uint32_t group)
5171{
5172 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005173 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005174
Daniel Stone351eb612012-05-31 15:27:47 -04005175 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5176 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005177}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005178
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005179static void
5180switcher_cancel(struct weston_keyboard_grab *grab)
5181{
5182 struct switcher *switcher = container_of(grab, struct switcher, grab);
5183
5184 switcher_destroy(switcher);
5185}
5186
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005187static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005188 switcher_key,
5189 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005190 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005191};
5192
5193static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005194switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005195 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005196{
Tiago Vignattibe143262012-04-16 17:31:41 +03005197 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005198 struct switcher *switcher;
5199
5200 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005201 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005202 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005203 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005204 wl_list_init(&switcher->listener.link);
5205
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005206 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005207 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005208 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005209 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5210 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005211 switcher_next(switcher);
5212}
5213
Daniel Stonedf8133b2013-11-19 11:37:14 +01005214struct exposay_surface {
5215 struct desktop_shell *shell;
5216 struct weston_surface *surface;
5217 struct weston_view *view;
5218 struct wl_list link;
5219
5220 int x;
5221 int y;
5222 int width;
5223 int height;
5224 double scale;
5225
5226 int row;
5227 int column;
5228
5229 /* The animations only apply a transformation for their own lifetime,
5230 * and don't have an option to indefinitely maintain the
5231 * transformation in a steady state - so, we apply our own once the
5232 * animation has finished. */
5233 struct weston_transform transform;
5234};
5235
5236static void exposay_set_state(struct desktop_shell *shell,
5237 enum exposay_target_state state,
5238 struct weston_seat *seat);
5239static void exposay_check_state(struct desktop_shell *shell);
5240
5241static void
5242exposay_in_flight_inc(struct desktop_shell *shell)
5243{
5244 shell->exposay.in_flight++;
5245}
5246
5247static void
5248exposay_in_flight_dec(struct desktop_shell *shell)
5249{
5250 if (--shell->exposay.in_flight > 0)
5251 return;
5252
5253 exposay_check_state(shell);
5254}
5255
5256static void
5257exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5258{
5259 struct exposay_surface *esurface = data;
5260
5261 wl_list_insert(&esurface->view->geometry.transformation_list,
5262 &esurface->transform.link);
5263 weston_matrix_init(&esurface->transform.matrix);
5264 weston_matrix_scale(&esurface->transform.matrix,
5265 esurface->scale, esurface->scale, 1.0f);
5266 weston_matrix_translate(&esurface->transform.matrix,
5267 esurface->x - esurface->view->geometry.x,
5268 esurface->y - esurface->view->geometry.y,
5269 0);
5270
5271 weston_view_geometry_dirty(esurface->view);
5272 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5273
5274 exposay_in_flight_dec(esurface->shell);
5275}
5276
5277static void
5278exposay_animate_in(struct exposay_surface *esurface)
5279{
5280 exposay_in_flight_inc(esurface->shell);
5281
5282 weston_move_scale_run(esurface->view,
5283 esurface->x - esurface->view->geometry.x,
5284 esurface->y - esurface->view->geometry.y,
5285 1.0, esurface->scale, 0,
5286 exposay_animate_in_done, esurface);
5287}
5288
5289static void
5290exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5291{
5292 struct exposay_surface *esurface = data;
5293 struct desktop_shell *shell = esurface->shell;
5294
5295 wl_list_remove(&esurface->link);
5296 free(esurface);
5297
5298 exposay_in_flight_dec(shell);
5299}
5300
5301static void
5302exposay_animate_out(struct exposay_surface *esurface)
5303{
5304 exposay_in_flight_inc(esurface->shell);
5305
5306 /* Remove the static transformation set up by
5307 * exposay_transform_in_done(). */
5308 wl_list_remove(&esurface->transform.link);
5309 weston_view_geometry_dirty(esurface->view);
5310
5311 weston_move_scale_run(esurface->view,
5312 esurface->x - esurface->view->geometry.x,
5313 esurface->y - esurface->view->geometry.y,
5314 1.0, esurface->scale, 1,
5315 exposay_animate_out_done, esurface);
5316}
5317
5318static void
5319exposay_highlight_surface(struct desktop_shell *shell,
5320 struct exposay_surface *esurface)
5321{
5322 struct weston_view *view = NULL;
5323
5324 if (esurface) {
5325 shell->exposay.row_current = esurface->row;
5326 shell->exposay.column_current = esurface->column;
5327 view = esurface->view;
5328 }
5329
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005330 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005331 shell->exposay.focus_current = view;
5332}
5333
5334static int
5335exposay_is_animating(struct desktop_shell *shell)
5336{
5337 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5338 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5339 return 0;
5340
5341 return (shell->exposay.in_flight > 0);
5342}
5343
5344static void
5345exposay_pick(struct desktop_shell *shell, int x, int y)
5346{
5347 struct exposay_surface *esurface;
5348
5349 if (exposay_is_animating(shell))
5350 return;
5351
5352 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5353 if (x < esurface->x || x > esurface->x + esurface->width)
5354 continue;
5355 if (y < esurface->y || y > esurface->y + esurface->height)
5356 continue;
5357
5358 exposay_highlight_surface(shell, esurface);
5359 return;
5360 }
5361}
5362
5363/* Pretty lame layout for now; just tries to make a square. Should take
5364 * aspect ratio into account really. Also needs to be notified of surface
5365 * addition and removal and adjust layout/animate accordingly. */
5366static enum exposay_layout_state
5367exposay_layout(struct desktop_shell *shell)
5368{
5369 struct workspace *workspace = shell->exposay.workspace;
5370 struct weston_compositor *compositor = shell->compositor;
5371 struct weston_output *output = get_default_output(compositor);
5372 struct weston_view *view;
5373 struct exposay_surface *esurface;
5374 int w, h;
5375 int i;
5376 int last_row_removed = 0;
5377
5378 wl_list_init(&shell->exposay.surface_list);
5379
5380 shell->exposay.num_surfaces = 0;
5381 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5382 if (!get_shell_surface(view->surface))
5383 continue;
5384 shell->exposay.num_surfaces++;
5385 }
5386
5387 if (shell->exposay.num_surfaces == 0) {
5388 shell->exposay.grid_size = 0;
5389 shell->exposay.hpadding_outer = 0;
5390 shell->exposay.vpadding_outer = 0;
5391 shell->exposay.padding_inner = 0;
5392 shell->exposay.surface_size = 0;
5393 return EXPOSAY_LAYOUT_OVERVIEW;
5394 }
5395
5396 /* Lay the grid out as square as possible, losing surfaces from the
5397 * bottom row if required. Start with fixed padding of a 10% margin
5398 * around the outside and 80px internal padding between surfaces, and
5399 * maximise the area made available to surfaces after this, but only
5400 * to a maximum of 1/3rd the total output size.
5401 *
5402 * If we can't make a square grid, add one extra row at the bottom
5403 * which will have a smaller number of columns.
5404 *
5405 * XXX: Surely there has to be a better way to express this maths,
5406 * right?!
5407 */
5408 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5409 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5410 shell->exposay.grid_size++;
5411 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5412
5413 shell->exposay.hpadding_outer = (output->width / 10);
5414 shell->exposay.vpadding_outer = (output->height / 10);
5415 shell->exposay.padding_inner = 80;
5416
5417 w = output->width - (shell->exposay.hpadding_outer * 2);
5418 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5419 w /= shell->exposay.grid_size;
5420
5421 h = output->height - (shell->exposay.vpadding_outer * 2);
5422 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5423 h /= shell->exposay.grid_size;
5424
5425 shell->exposay.surface_size = (w < h) ? w : h;
5426 if (shell->exposay.surface_size > (output->width / 2))
5427 shell->exposay.surface_size = output->width / 2;
5428 if (shell->exposay.surface_size > (output->height / 2))
5429 shell->exposay.surface_size = output->height / 2;
5430
5431 i = 0;
5432 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5433 int pad;
5434
5435 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5436
5437 if (!get_shell_surface(view->surface))
5438 continue;
5439
5440 esurface = malloc(sizeof(*esurface));
5441 if (!esurface) {
5442 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5443 shell->exposay.seat);
5444 break;
5445 }
5446
5447 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5448 esurface->shell = shell;
5449 esurface->view = view;
5450
5451 esurface->row = i / shell->exposay.grid_size;
5452 esurface->column = i % shell->exposay.grid_size;
5453
5454 esurface->x = shell->exposay.hpadding_outer;
5455 esurface->x += pad * esurface->column;
5456 esurface->y = shell->exposay.vpadding_outer;
5457 esurface->y += pad * esurface->row;
5458
5459 if (esurface->row == shell->exposay.grid_size - 1)
5460 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5461
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005462 if (view->surface->width > view->surface->height)
5463 esurface->scale = shell->exposay.surface_size / (float) view->surface->width;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005464 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005465 esurface->scale = shell->exposay.surface_size / (float) view->surface->height;
5466 esurface->width = view->surface->width * esurface->scale;
5467 esurface->height = view->surface->height * esurface->scale;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005468
5469 if (shell->exposay.focus_current == esurface->view)
5470 exposay_highlight_surface(shell, esurface);
5471
5472 exposay_animate_in(esurface);
5473
5474 i++;
5475 }
5476
5477 weston_compositor_schedule_repaint(shell->compositor);
5478
5479 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5480}
5481
5482static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005483exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5484 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005485{
5486 struct desktop_shell *shell =
5487 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5488
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005489 weston_pointer_move(grab->pointer, x, y);
5490
Daniel Stonedf8133b2013-11-19 11:37:14 +01005491 exposay_pick(shell,
5492 wl_fixed_to_int(grab->pointer->x),
5493 wl_fixed_to_int(grab->pointer->y));
5494}
5495
5496static void
5497exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5498 uint32_t state_w)
5499{
5500 struct desktop_shell *shell =
5501 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5502 struct weston_seat *seat = grab->pointer->seat;
5503 enum wl_pointer_button_state state = state_w;
5504
5505 if (button != BTN_LEFT)
5506 return;
5507
5508 /* Store the surface we clicked on, and don't do anything if we end up
5509 * releasing on a different surface. */
5510 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5511 shell->exposay.clicked = shell->exposay.focus_current;
5512 return;
5513 }
5514
5515 if (shell->exposay.focus_current == shell->exposay.clicked)
5516 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5517 else
5518 shell->exposay.clicked = NULL;
5519}
5520
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005521static void
5522exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
5523{
5524 struct desktop_shell *shell =
5525 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5526
5527 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5528}
5529
Daniel Stonedf8133b2013-11-19 11:37:14 +01005530static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5531 noop_grab_focus,
5532 exposay_motion,
5533 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005534 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005535};
5536
5537static int
5538exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5539{
5540 struct exposay_surface *esurface;
5541
5542 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5543 if (esurface->row != row || esurface->column != column)
5544 continue;
5545
5546 exposay_highlight_surface(shell, esurface);
5547 return 1;
5548 }
5549
5550 return 0;
5551}
5552
5553static void
5554exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5555 uint32_t state_w)
5556{
5557 struct weston_seat *seat = grab->keyboard->seat;
5558 struct desktop_shell *shell =
5559 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5560 enum wl_keyboard_key_state state = state_w;
5561
5562 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5563 return;
5564
5565 switch (key) {
5566 case KEY_ESC:
5567 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5568 break;
5569 case KEY_ENTER:
5570 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5571 break;
5572 case KEY_UP:
5573 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5574 shell->exposay.column_current);
5575 break;
5576 case KEY_DOWN:
5577 /* Special case for trying to move to the bottom row when it
5578 * has fewer items than all the others. */
5579 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5580 shell->exposay.column_current) &&
5581 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5582 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5583 (shell->exposay.num_surfaces %
5584 shell->exposay.grid_size) - 1);
5585 }
5586 break;
5587 case KEY_LEFT:
5588 exposay_maybe_move(shell, shell->exposay.row_current,
5589 shell->exposay.column_current - 1);
5590 break;
5591 case KEY_RIGHT:
5592 exposay_maybe_move(shell, shell->exposay.row_current,
5593 shell->exposay.column_current + 1);
5594 break;
5595 case KEY_TAB:
5596 /* Try to move right, then down (and to the leftmost column),
5597 * then if all else fails, to the top left. */
5598 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5599 shell->exposay.column_current + 1) &&
5600 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5601 exposay_maybe_move(shell, 0, 0);
5602 break;
5603 default:
5604 break;
5605 }
5606}
5607
5608static void
5609exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5610 uint32_t mods_depressed, uint32_t mods_latched,
5611 uint32_t mods_locked, uint32_t group)
5612{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005613 struct desktop_shell *shell =
5614 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5615 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5616
5617 /* We want to know when mod has been pressed and released.
5618 * FIXME: There is a problem here: if mod is pressed, then a key
5619 * is pressed and released, then mod is released, we will treat that
5620 * as if only mod had been pressed and released. */
5621 if (seat->modifier_state) {
5622 if (seat->modifier_state == shell->binding_modifier) {
5623 shell->exposay.mod_pressed = true;
5624 } else {
5625 shell->exposay.mod_invalid = true;
5626 }
5627 } else {
5628 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
5629 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5630
5631 shell->exposay.mod_invalid = false;
5632 shell->exposay.mod_pressed = false;
5633 }
5634
5635 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005636}
5637
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005638static void
5639exposay_cancel(struct weston_keyboard_grab *grab)
5640{
5641 struct desktop_shell *shell =
5642 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5643
5644 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5645}
5646
Daniel Stonedf8133b2013-11-19 11:37:14 +01005647static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5648 exposay_key,
5649 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005650 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005651};
5652
5653/**
5654 * Called when the transition from overview -> inactive has completed.
5655 */
5656static enum exposay_layout_state
5657exposay_set_inactive(struct desktop_shell *shell)
5658{
5659 struct weston_seat *seat = shell->exposay.seat;
5660
5661 weston_keyboard_end_grab(seat->keyboard);
5662 weston_pointer_end_grab(seat->pointer);
5663 if (seat->keyboard->input_method_resource)
5664 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5665
5666 return EXPOSAY_LAYOUT_INACTIVE;
5667}
5668
5669/**
5670 * Begins the transition from overview to inactive. */
5671static enum exposay_layout_state
5672exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5673{
5674 struct exposay_surface *esurface;
5675
5676 /* Call activate() before we start the animations to avoid
5677 * animating back the old state and then immediately transitioning
5678 * to the new. */
5679 if (switch_focus && shell->exposay.focus_current)
5680 activate(shell, shell->exposay.focus_current->surface,
5681 shell->exposay.seat);
5682 else if (shell->exposay.focus_prev)
5683 activate(shell, shell->exposay.focus_prev->surface,
5684 shell->exposay.seat);
5685
5686 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5687 exposay_animate_out(esurface);
5688 weston_compositor_schedule_repaint(shell->compositor);
5689
5690 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5691}
5692
5693static enum exposay_layout_state
5694exposay_transition_active(struct desktop_shell *shell)
5695{
5696 struct weston_seat *seat = shell->exposay.seat;
5697
5698 shell->exposay.workspace = get_current_workspace(shell);
5699 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5700 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5701 shell->exposay.clicked = NULL;
5702 wl_list_init(&shell->exposay.surface_list);
5703
5704 lower_fullscreen_layer(shell);
5705 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5706 weston_keyboard_start_grab(seat->keyboard,
5707 &shell->exposay.grab_kbd);
5708 weston_keyboard_set_focus(seat->keyboard, NULL);
5709
5710 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5711 weston_pointer_start_grab(seat->pointer,
5712 &shell->exposay.grab_ptr);
5713 weston_pointer_set_focus(seat->pointer, NULL,
5714 seat->pointer->x, seat->pointer->y);
5715
5716 return exposay_layout(shell);
5717}
5718
5719static void
5720exposay_check_state(struct desktop_shell *shell)
5721{
5722 enum exposay_layout_state state_new = shell->exposay.state_cur;
5723 int do_switch = 0;
5724
5725 /* Don't do anything whilst animations are running, just store up
5726 * target state changes and only act on them when the animations have
5727 * completed. */
5728 if (exposay_is_animating(shell))
5729 return;
5730
5731 switch (shell->exposay.state_target) {
5732 case EXPOSAY_TARGET_OVERVIEW:
5733 switch (shell->exposay.state_cur) {
5734 case EXPOSAY_LAYOUT_OVERVIEW:
5735 goto out;
5736 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5737 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5738 break;
5739 default:
5740 state_new = exposay_transition_active(shell);
5741 break;
5742 }
5743 break;
5744
5745 case EXPOSAY_TARGET_SWITCH:
5746 do_switch = 1; /* fallthrough */
5747 case EXPOSAY_TARGET_CANCEL:
5748 switch (shell->exposay.state_cur) {
5749 case EXPOSAY_LAYOUT_INACTIVE:
5750 goto out;
5751 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5752 state_new = exposay_set_inactive(shell);
5753 break;
5754 default:
5755 state_new = exposay_transition_inactive(shell, do_switch);
5756 break;
5757 }
5758
5759 break;
5760 }
5761
5762out:
5763 shell->exposay.state_cur = state_new;
5764}
5765
5766static void
5767exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5768 struct weston_seat *seat)
5769{
5770 shell->exposay.state_target = state;
5771 shell->exposay.seat = seat;
5772 exposay_check_state(shell);
5773}
5774
5775static void
5776exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5777 void *data)
5778{
5779 struct desktop_shell *shell = data;
5780
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005781 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005782}
5783
Pekka Paalanen3c647232011-12-22 13:43:43 +02005784static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005785backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005786 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005787{
5788 struct weston_compositor *compositor = data;
5789 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005790 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005791
5792 /* TODO: we're limiting to simple use cases, where we assume just
5793 * control on the primary display. We'd have to extend later if we
5794 * ever get support for setting backlights on random desktop LCD
5795 * panels though */
5796 output = get_default_output(compositor);
5797 if (!output)
5798 return;
5799
5800 if (!output->set_backlight)
5801 return;
5802
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005803 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5804 backlight_new = output->backlight_current - 25;
5805 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5806 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005807
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005808 if (backlight_new < 5)
5809 backlight_new = 5;
5810 if (backlight_new > 255)
5811 backlight_new = 255;
5812
5813 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005814 output->set_backlight(output, output->backlight_current);
5815}
5816
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005817struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005818 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005819 struct weston_seat *seat;
5820 uint32_t key[2];
5821 int key_released[2];
5822};
5823
5824static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005825debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005826 uint32_t key, uint32_t state)
5827{
5828 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005829 struct weston_compositor *ec = db->seat->compositor;
5830 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005831 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005832 uint32_t serial;
5833 int send = 0, terminate = 0;
5834 int check_binding = 1;
5835 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005836 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005837
5838 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5839 /* Do not run bindings on key releases */
5840 check_binding = 0;
5841
5842 for (i = 0; i < 2; i++)
5843 if (key == db->key[i])
5844 db->key_released[i] = 1;
5845
5846 if (db->key_released[0] && db->key_released[1]) {
5847 /* All key releases been swalled so end the grab */
5848 terminate = 1;
5849 } else if (key != db->key[0] && key != db->key[1]) {
5850 /* Should not swallow release of other keys */
5851 send = 1;
5852 }
5853 } else if (key == db->key[0] && !db->key_released[0]) {
5854 /* Do not check bindings for the first press of the binding
5855 * key. This allows it to be used as a debug shortcut.
5856 * We still need to swallow this event. */
5857 check_binding = 0;
5858 } else if (db->key[1]) {
5859 /* If we already ran a binding don't process another one since
5860 * we can't keep track of all the binding keys that were
5861 * pressed in order to swallow the release events. */
5862 send = 1;
5863 check_binding = 0;
5864 }
5865
5866 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005867 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5868 key, state)) {
5869 /* We ran a binding so swallow the press and keep the
5870 * grab to swallow the released too. */
5871 send = 0;
5872 terminate = 0;
5873 db->key[1] = key;
5874 } else {
5875 /* Terminate the grab since the key pressed is not a
5876 * debug binding key. */
5877 send = 1;
5878 terminate = 1;
5879 }
5880 }
5881
5882 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005883 serial = wl_display_next_serial(display);
5884 resource_list = &grab->keyboard->focus_resource_list;
5885 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005886 wl_keyboard_send_key(resource, serial, time, key, state);
5887 }
5888 }
5889
5890 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005891 weston_keyboard_end_grab(grab->keyboard);
5892 if (grab->keyboard->input_method_resource)
5893 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005894 free(db);
5895 }
5896}
5897
5898static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005899debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005900 uint32_t mods_depressed, uint32_t mods_latched,
5901 uint32_t mods_locked, uint32_t group)
5902{
5903 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005904 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005905
Neil Roberts96d790e2013-09-19 17:32:00 +01005906 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005907
Neil Roberts96d790e2013-09-19 17:32:00 +01005908 wl_resource_for_each(resource, resource_list) {
5909 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5910 mods_latched, mods_locked, group);
5911 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005912}
5913
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005914static void
5915debug_binding_cancel(struct weston_keyboard_grab *grab)
5916{
5917 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5918
5919 weston_keyboard_end_grab(grab->keyboard);
5920 free(db);
5921}
5922
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005923struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005924 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005925 debug_binding_modifiers,
5926 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005927};
5928
5929static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005930debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005931{
5932 struct debug_binding_grab *grab;
5933
5934 grab = calloc(1, sizeof *grab);
5935 if (!grab)
5936 return;
5937
5938 grab->seat = (struct weston_seat *) seat;
5939 grab->key[0] = key;
5940 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005941 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005942}
5943
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005944static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005945force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005946 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005947{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005948 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005949 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005950 struct desktop_shell *shell = data;
5951 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005952 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005953
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005954 focus_surface = seat->keyboard->focus;
5955 if (!focus_surface)
5956 return;
5957
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005958 wl_signal_emit(&compositor->kill_signal, focus_surface);
5959
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005960 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005961 wl_client_get_credentials(client, &pid, NULL, NULL);
5962
5963 /* Skip clients that we launched ourselves (the credentials of
5964 * the socketpair is ours) */
5965 if (pid == getpid())
5966 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005967
Daniel Stone325fc2d2012-05-30 16:31:58 +01005968 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005969}
5970
5971static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005972workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005973 uint32_t key, void *data)
5974{
5975 struct desktop_shell *shell = data;
5976 unsigned int new_index = shell->workspaces.current;
5977
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005978 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005979 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005980 if (new_index != 0)
5981 new_index--;
5982
5983 change_workspace(shell, new_index);
5984}
5985
5986static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005987workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005988 uint32_t key, void *data)
5989{
5990 struct desktop_shell *shell = data;
5991 unsigned int new_index = shell->workspaces.current;
5992
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005993 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005994 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005995 if (new_index < shell->workspaces.num - 1)
5996 new_index++;
5997
5998 change_workspace(shell, new_index);
5999}
6000
6001static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006002workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006003 uint32_t key, void *data)
6004{
6005 struct desktop_shell *shell = data;
6006 unsigned int new_index;
6007
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006008 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006009 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006010 new_index = key - KEY_F1;
6011 if (new_index >= shell->workspaces.num)
6012 new_index = shell->workspaces.num - 1;
6013
6014 change_workspace(shell, new_index);
6015}
6016
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006017static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006018workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006019 uint32_t key, void *data)
6020{
6021 struct desktop_shell *shell = data;
6022 unsigned int new_index = shell->workspaces.current;
6023
6024 if (shell->locked)
6025 return;
6026
6027 if (new_index != 0)
6028 new_index--;
6029
6030 take_surface_to_workspace_by_seat(shell, seat, new_index);
6031}
6032
6033static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006034workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006035 uint32_t key, void *data)
6036{
6037 struct desktop_shell *shell = data;
6038 unsigned int new_index = shell->workspaces.current;
6039
6040 if (shell->locked)
6041 return;
6042
6043 if (new_index < shell->workspaces.num - 1)
6044 new_index++;
6045
6046 take_surface_to_workspace_by_seat(shell, seat, new_index);
6047}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006048
6049static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006050handle_output_destroy(struct wl_listener *listener, void *data)
6051{
6052 struct shell_output *output_listener =
6053 container_of(listener, struct shell_output, destroy_listener);
6054
6055 wl_list_remove(&output_listener->destroy_listener.link);
6056 wl_list_remove(&output_listener->link);
6057 free(output_listener);
6058}
6059
6060static void
6061create_shell_output(struct desktop_shell *shell,
6062 struct weston_output *output)
6063{
6064 struct shell_output *shell_output;
6065
6066 shell_output = zalloc(sizeof *shell_output);
6067 if (shell_output == NULL)
6068 return;
6069
6070 shell_output->output = output;
6071 shell_output->shell = shell;
6072 shell_output->destroy_listener.notify = handle_output_destroy;
6073 wl_signal_add(&output->destroy_signal,
6074 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006075 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006076}
6077
6078static void
6079handle_output_create(struct wl_listener *listener, void *data)
6080{
6081 struct desktop_shell *shell =
6082 container_of(listener, struct desktop_shell, output_create_listener);
6083 struct weston_output *output = (struct weston_output *)data;
6084
6085 create_shell_output(shell, output);
6086}
6087
6088static void
6089setup_output_destroy_handler(struct weston_compositor *ec,
6090 struct desktop_shell *shell)
6091{
6092 struct weston_output *output;
6093
6094 wl_list_init(&shell->output_list);
6095 wl_list_for_each(output, &ec->output_list, link)
6096 create_shell_output(shell, output);
6097
6098 shell->output_create_listener.notify = handle_output_create;
6099 wl_signal_add(&ec->output_created_signal,
6100 &shell->output_create_listener);
6101}
6102
6103static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006104shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006105{
Tiago Vignattibe143262012-04-16 17:31:41 +03006106 struct desktop_shell *shell =
6107 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006108 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006109 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006110
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006111 if (shell->child.client)
6112 wl_client_destroy(shell->child.client);
6113
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006114 wl_list_remove(&shell->idle_listener.link);
6115 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006116 wl_list_remove(&shell->show_input_panel_listener.link);
6117 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006118
Xiong Zhang6b481422013-10-23 13:58:32 +08006119 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6120 wl_list_remove(&shell_output->destroy_listener.link);
6121 wl_list_remove(&shell_output->link);
6122 free(shell_output);
6123 }
6124
6125 wl_list_remove(&shell->output_create_listener.link);
6126
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006127 wl_array_for_each(ws, &shell->workspaces.array)
6128 workspace_destroy(*ws);
6129 wl_array_release(&shell->workspaces.array);
6130
Pekka Paalanen3c647232011-12-22 13:43:43 +02006131 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006132 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006133 free(shell);
6134}
6135
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006136static void
6137shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6138{
6139 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006140 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006141
6142 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006143 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6144 MODIFIER_CTRL | MODIFIER_ALT,
6145 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01006146 weston_compositor_add_key_binding(ec, KEY_TAB,
6147 MODIFIER_ALT,
6148 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006149 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6150 click_to_activate_binding,
6151 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006152 weston_compositor_add_touch_binding(ec, 0,
6153 touch_to_activate_binding,
6154 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006155 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6156 MODIFIER_SUPER | MODIFIER_ALT,
6157 surface_opacity_binding, NULL);
6158 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6159 MODIFIER_SUPER, zoom_axis_binding,
6160 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006161
6162 /* configurable bindings */
6163 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006164 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6165 zoom_key_binding, NULL);
6166 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6167 zoom_key_binding, NULL);
6168 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6169 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006170 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006171 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6172 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006173
6174 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6175 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6176 rotate_binding, NULL);
6177
Daniel Stone325fc2d2012-05-30 16:31:58 +01006178 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6179 shell);
6180 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6181 ec);
6182 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6183 backlight_binding, ec);
6184 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6185 ec);
6186 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6187 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006188 weston_compositor_add_key_binding(ec, KEY_K, mod,
6189 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006190 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6191 workspace_up_binding, shell);
6192 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6193 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006194 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6195 workspace_move_surface_up_binding,
6196 shell);
6197 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6198 workspace_move_surface_down_binding,
6199 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006200
Daniel Stonedf8133b2013-11-19 11:37:14 +01006201 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6202
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006203 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6204 if (shell->workspaces.num > 1) {
6205 num_workspace_bindings = shell->workspaces.num;
6206 if (num_workspace_bindings > 6)
6207 num_workspace_bindings = 6;
6208 for (i = 0; i < num_workspace_bindings; i++)
6209 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6210 workspace_f_binding,
6211 shell);
6212 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006213
6214 /* Debug bindings */
6215 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6216 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006217}
6218
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006219WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006220module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006221 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006222{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006223 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006224 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006225 struct workspace **pws;
6226 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006227 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006228
Peter Huttererf3d62272013-08-08 11:57:05 +10006229 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006230 if (shell == NULL)
6231 return -1;
6232
Kristian Høgsberg75840622011-09-06 13:48:16 -04006233 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006234
6235 shell->destroy_listener.notify = shell_destroy;
6236 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006237 shell->idle_listener.notify = idle_handler;
6238 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6239 shell->wake_listener.notify = wake_handler;
6240 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006241 shell->show_input_panel_listener.notify = show_input_panels;
6242 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6243 shell->hide_input_panel_listener.notify = hide_input_panels;
6244 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006245 shell->update_input_panel_listener.notify = update_input_panels;
6246 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006247 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006248 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006249 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006250 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006251 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006252 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006253 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006254 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006255 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006256 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006257 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006258
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006259 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006260
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006261 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6262 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006263 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6264 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006265 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006266
6267 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006268 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006269
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006270 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006271
Daniel Stonedf8133b2013-11-19 11:37:14 +01006272 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6273 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6274
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006275 for (i = 0; i < shell->workspaces.num; i++) {
6276 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6277 if (pws == NULL)
6278 return -1;
6279
6280 *pws = workspace_create();
6281 if (*pws == NULL)
6282 return -1;
6283 }
6284 activate_workspace(shell, 0);
6285
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006286 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006287 wl_list_init(&shell->workspaces.animation.link);
6288 shell->workspaces.animation.frame = animate_workspace_change_frame;
6289
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006290 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006291 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006292 return -1;
6293
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006294 if (wl_global_create(ec->wl_display,
6295 &desktop_shell_interface, 2,
6296 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006297 return -1;
6298
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006299 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6300 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006301 return -1;
6302
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006303 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006304 shell, bind_input_panel) == NULL)
6305 return -1;
6306
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006307 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6308 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006309 return -1;
6310
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006311 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006312
Xiong Zhang6b481422013-10-23 13:58:32 +08006313 setup_output_destroy_handler(ec, shell);
6314
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006315 loop = wl_display_get_event_loop(ec->wl_display);
6316 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006317
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006318 shell->screensaver.timer =
6319 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6320
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006321 wl_list_for_each(seat, &ec->seat_list, link)
6322 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006323
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006324 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006325
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006326 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006327
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006328 return 0;
6329}