blob: 00e0f1cbc6aa8f2ba513e05c8b27f22bc4af322b [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020029#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050030#include <string.h>
31#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040032#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020034#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020035#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040036#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010051 ANIMATION_FADE,
52 ANIMATION_DIM_LAYER,
Juan Zhaoe10d2792012-04-25 19:09:52 +080053};
54
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020055enum fade_type {
56 FADE_IN,
57 FADE_OUT
58};
59
Daniel Stonedf8133b2013-11-19 11:37:14 +010060enum exposay_target_state {
61 EXPOSAY_TARGET_OVERVIEW, /* show all windows */
62 EXPOSAY_TARGET_CANCEL, /* return to normal, same focus */
63 EXPOSAY_TARGET_SWITCH, /* return to normal, switch focus */
64};
65
66enum exposay_layout_state {
67 EXPOSAY_LAYOUT_INACTIVE = 0, /* normal desktop */
68 EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE, /* in transition to normal */
69 EXPOSAY_LAYOUT_OVERVIEW, /* show all windows */
70 EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW, /* in transition to all windows */
71};
72
Jonas Ådahl04769742012-06-13 00:01:24 +020073struct focus_state {
74 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040075 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020076 struct weston_surface *keyboard_focus;
77 struct wl_list link;
78 struct wl_listener seat_destroy_listener;
79 struct wl_listener surface_destroy_listener;
80};
81
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010082struct focus_surface {
83 struct weston_surface *surface;
84 struct weston_view *view;
85 struct weston_transform workspace_transform;
86};
87
Jonas Ådahle3cddce2012-06-13 00:01:22 +020088struct workspace {
89 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020090
91 struct wl_list focus_list;
92 struct wl_listener seat_destroyed_listener;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010093
94 struct focus_surface *fsurf_front;
95 struct focus_surface *fsurf_back;
96 struct weston_view_animation *focus_animation;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020097};
98
Philipp Brüschweiler88013572012-08-06 13:44:42 +020099struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -0500100 struct wl_resource *resource;
101 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100102
103 struct desktop_shell *shell;
104
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200105 struct wl_list link;
106 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500107 struct weston_view *view;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100108 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200109
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +0200110 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200111 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200112};
113
Xiong Zhang6b481422013-10-23 13:58:32 +0800114struct shell_output {
115 struct desktop_shell *shell;
116 struct weston_output *output;
117 struct wl_listener destroy_listener;
118 struct wl_list link;
119};
120
Tiago Vignattibe143262012-04-16 17:31:41 +0300121struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400123
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200124 struct wl_listener idle_listener;
125 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400126 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200127 struct wl_listener show_input_panel_listener;
128 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200129 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200130
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500131 struct weston_layer fullscreen_layer;
132 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500133 struct weston_layer background_layer;
134 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200135 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500136
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400137 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300138 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400139
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200140 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500141 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200142 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200143 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200144
145 unsigned deathcount;
146 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200147 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200148
149 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200150 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200151 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200152
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200153 struct {
154 struct weston_surface *surface;
155 pixman_box32_t cursor_rectangle;
156 } text_input;
157
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400158 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500159 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160
Pekka Paalanen77346a62011-11-30 16:26:35 +0200161 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200162 struct wl_array array;
163 unsigned int current;
164 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200165
Jonas Ådahle9d22502012-08-29 22:13:01 +0200166 struct wl_list client_list;
167
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200168 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200169 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200170 int anim_dir;
171 uint32_t anim_timestamp;
172 double anim_current;
173 struct workspace *anim_from;
174 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200175 } workspaces;
176
177 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200178 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200179 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200180 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500181 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200182 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200183 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500184
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200185 struct {
186 struct wl_resource *binding;
187 struct wl_list surfaces;
188 } input_panel;
189
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200190 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500191 struct weston_view *view;
192 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200193 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300194 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200195 } fade;
196
Daniel Stonedf8133b2013-11-19 11:37:14 +0100197 struct exposay {
198 /* XXX: Make these exposay_surfaces. */
199 struct weston_view *focus_prev;
200 struct weston_view *focus_current;
201 struct weston_view *clicked;
202 struct workspace *workspace;
203 struct weston_seat *seat;
204 struct wl_list surface_list;
205
206 struct weston_keyboard_grab grab_kbd;
207 struct weston_pointer_grab grab_ptr;
208
209 enum exposay_target_state state_target;
210 enum exposay_layout_state state_cur;
211 int in_flight; /* number of animations still running */
212
213 int num_surfaces;
214 int grid_size;
215 int surface_size;
216
217 int hpadding_outer;
218 int vpadding_outer;
219 int padding_inner;
220
221 int row_current;
222 int column_current;
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +0100223
224 bool mod_pressed;
225 bool mod_invalid;
Daniel Stonedf8133b2013-11-19 11:37:14 +0100226 } exposay;
227
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300228 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800229 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700230 enum animation_type startup_animation_type;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100231 enum animation_type focus_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800232
233 struct wl_listener output_create_listener;
234 struct wl_list output_list;
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100235
236 char *client;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400237};
238
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500239enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200240 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500241 SHELL_SURFACE_TOPLEVEL,
242 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500243 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800244 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300245 SHELL_SURFACE_POPUP,
246 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200247};
248
Scott Moreauff1db4a2012-04-17 19:06:18 -0600249struct ping_timer {
250 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600251 uint32_t serial;
252};
253
Philip Withnall648a4dd2013-11-25 18:01:44 +0000254/*
255 * Surface stacking and ordering.
256 *
257 * This is handled using several linked lists of surfaces, organised into
258 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
259 * above all of the surfaces in the layer below. The set of layers is static and
260 * in the following order (top-most first):
261 * • Lock layer (only ever displayed on its own)
262 * • Cursor layer
263 * • Fullscreen layer
264 * • Panel layer
265 * • Input panel layer
266 * • Workspace layers
267 * • Background layer
268 *
269 * The list of layers may be manipulated to remove whole layers of surfaces from
270 * display. For example, when locking the screen, all layers except the lock
271 * layer are removed.
272 *
273 * A surface’s layer is modified on configuring the surface, in
274 * set_surface_type() (which is only called when the surface’s type change is
275 * _committed_). If a surface’s type changes (e.g. when making a window
276 * fullscreen) its layer changes too.
277 *
278 * In order to allow popup and transient surfaces to be correctly stacked above
279 * their parent surfaces, each surface tracks both its parent surface, and a
280 * linked list of its children. When a surface’s layer is updated, so are the
281 * layers of its children. Note that child surfaces are *not* the same as
282 * subsurfaces — child/parent surfaces are purely for maintaining stacking
283 * order.
284 *
285 * The children_link list of siblings of a surface (i.e. those surfaces which
286 * have the same parent) only contains weston_surfaces which have a
287 * shell_surface. Stacking is not implemented for non-shell_surface
288 * weston_surfaces. This means that the following implication does *not* hold:
289 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
290 */
291
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200292struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500293 struct wl_resource *resource;
294 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200295
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500296 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200298 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400299 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000300 struct wl_list children_list; /* child surfaces of this one */
301 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300302 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200303
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400304 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400305 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500306 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800307 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800308 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700309 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100310
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500311 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200312 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500313 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200314 } rotation;
315
316 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100317 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500318 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100319 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400320 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500321 } popup;
322
Alex Wu4539b082012-03-01 12:57:46 +0800323 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300324 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400325 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300326 } transient;
327
328 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800329 enum wl_shell_surface_fullscreen_method type;
330 struct weston_transform transform; /* matrix from x, y */
331 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800333 } fullscreen;
334
Scott Moreauff1db4a2012-04-17 19:06:18 -0600335 struct ping_timer *ping_timer;
336
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200337 struct weston_transform workspace_transform;
338
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500339 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500340 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100341 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400342
343 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200344};
345
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300346struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400347 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300348 struct shell_surface *shsurf;
349 struct wl_listener shsurf_destroy_listener;
350};
351
Rusty Lynch1084da52013-08-15 09:10:08 -0700352struct shell_touch_grab {
353 struct weston_touch_grab grab;
354 struct shell_surface *shsurf;
355 struct wl_listener shsurf_destroy_listener;
356 struct weston_touch *touch;
357};
358
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300359struct weston_move_grab {
360 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100361 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500362};
363
Rusty Lynch1084da52013-08-15 09:10:08 -0700364struct weston_touch_move_grab {
365 struct shell_touch_grab base;
366 wl_fixed_t dx, dy;
367};
368
Pekka Paalanen460099f2012-01-20 16:48:25 +0200369struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300370 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500371 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200372 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200373 float x;
374 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200375 } center;
376};
377
Giulio Camuffo5085a752013-03-25 21:42:45 +0100378struct shell_seat {
379 struct weston_seat *seat;
380 struct wl_listener seat_destroy_listener;
381
382 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400383 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100384 struct wl_list surfaces_list;
385 struct wl_client *client;
386 int32_t initial_up;
387 } popup_grab;
388};
389
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400390static void
391activate(struct desktop_shell *shell, struct weston_surface *es,
392 struct weston_seat *seat);
393
394static struct workspace *
395get_current_workspace(struct desktop_shell *shell);
396
Alex Wubd3354b2012-04-17 17:20:49 +0800397static struct shell_surface *
398get_shell_surface(struct weston_surface *surface);
399
400static struct desktop_shell *
401shell_surface_get_shell(struct shell_surface *shsurf);
402
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500403static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400404surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500405
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300406static void
407shell_fade_startup(struct desktop_shell *shell);
408
Philip Withnallbecb77e2013-11-25 18:01:30 +0000409static struct shell_seat *
410get_shell_seat(struct weston_seat *seat);
411
Philip Withnall648a4dd2013-11-25 18:01:44 +0000412static void
413shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
414
Alex Wubd3354b2012-04-17 17:20:49 +0800415static bool
416shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
417{
418 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500419 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800420
421 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100422
Jason Ekstranda7af7042013-10-12 22:38:11 -0500423 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800424 return false;
425
Jason Ekstranda7af7042013-10-12 22:38:11 -0500426 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
427 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800428 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500429 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800430}
431
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500432static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400433destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300434{
435 struct shell_grab *grab;
436
437 grab = container_of(listener, struct shell_grab,
438 shsurf_destroy_listener);
439
440 grab->shsurf = NULL;
441}
442
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443static struct weston_view *
444get_default_view(struct weston_surface *surface)
445{
446 struct shell_surface *shsurf;
447 struct weston_view *view;
448
449 if (!surface || wl_list_empty(&surface->views))
450 return NULL;
451
452 shsurf = get_shell_surface(surface);
453 if (shsurf)
454 return shsurf->view;
455
456 wl_list_for_each(view, &surface->views, surface_link)
457 if (weston_view_is_mapped(view))
458 return view;
459
460 return container_of(surface->views.next, struct weston_view, surface_link);
461}
462
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300463static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400464popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400465
466static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300467shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400468 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300469 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400470 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300471 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300472{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300473 struct desktop_shell *shell = shsurf->shell;
474
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400475 popup_grab_end(pointer);
476
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300477 grab->grab.interface = interface;
478 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400479 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500480 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400481 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300482
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700483 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400484 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400485 if (shell->child.desktop_shell) {
486 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
487 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500488 weston_pointer_set_focus(pointer,
489 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400490 wl_fixed_from_int(0),
491 wl_fixed_from_int(0));
492 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300493}
494
495static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300496shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300497{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700498 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400499 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700500 grab->shsurf->grabbed = 0;
501 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300502
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700503 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300504}
505
506static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700507shell_touch_grab_start(struct shell_touch_grab *grab,
508 const struct weston_touch_grab_interface *interface,
509 struct shell_surface *shsurf,
510 struct weston_touch *touch)
511{
512 struct desktop_shell *shell = shsurf->shell;
513
514 grab->grab.interface = interface;
515 grab->shsurf = shsurf;
516 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
517 wl_signal_add(&shsurf->destroy_signal,
518 &grab->shsurf_destroy_listener);
519
520 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700521 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700522
523 weston_touch_start_grab(touch, &grab->grab);
524 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500525 weston_touch_set_focus(touch->seat,
526 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700527}
528
529static void
530shell_touch_grab_end(struct shell_touch_grab *grab)
531{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700532 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700533 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700534 grab->shsurf->grabbed = 0;
535 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700536
537 weston_touch_end_grab(grab->touch);
538}
539
540static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500541center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800542 struct weston_output *output);
543
Daniel Stone496ca172012-05-30 16:31:42 +0100544static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300545get_modifier(char *modifier)
546{
547 if (!modifier)
548 return MODIFIER_SUPER;
549
550 if (!strcmp("ctrl", modifier))
551 return MODIFIER_CTRL;
552 else if (!strcmp("alt", modifier))
553 return MODIFIER_ALT;
554 else if (!strcmp("super", modifier))
555 return MODIFIER_SUPER;
556 else
557 return MODIFIER_SUPER;
558}
559
Juan Zhaoe10d2792012-04-25 19:09:52 +0800560static enum animation_type
561get_animation_type(char *animation)
562{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800563 if (!strcmp("zoom", animation))
564 return ANIMATION_ZOOM;
565 else if (!strcmp("fade", animation))
566 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100567 else if (!strcmp("dim-layer", animation))
568 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800569 else
570 return ANIMATION_NONE;
571}
572
Alex Wu4539b082012-03-01 12:57:46 +0800573static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400574shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200575{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400576 struct weston_config_section *section;
577 int duration;
578 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200579
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400580 section = weston_config_get_section(shell->compositor->config,
581 "screensaver", NULL, NULL);
582 weston_config_section_get_string(section,
583 "path", &shell->screensaver.path, NULL);
584 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200585 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400586
587 section = weston_config_get_section(shell->compositor->config,
588 "shell", NULL, NULL);
589 weston_config_section_get_string(section,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100590 "client", &s, LIBEXECDIR "/weston-desktop-shell");
591 shell->client = s;
592 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400593 "binding-modifier", &s, "super");
594 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200595 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400596 weston_config_section_get_string(section, "animation", &s, "none");
597 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200598 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700599 weston_config_section_get_string(section,
600 "startup-animation", &s, "fade");
601 shell->startup_animation_type = get_animation_type(s);
602 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700603 if (shell->startup_animation_type == ANIMATION_ZOOM)
604 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100605 weston_config_section_get_string(section, "focus-animation", &s, "none");
606 shell->focus_animation_type = get_animation_type(s);
607 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400608 weston_config_section_get_uint(section, "num-workspaces",
609 &shell->workspaces.num,
610 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200611}
612
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100613static struct weston_output *
614get_default_output(struct weston_compositor *compositor)
615{
616 return container_of(compositor->output_list.next,
617 struct weston_output, link);
618}
619
620
621/* no-op func for checking focus surface */
622static void
623focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy,
624 int32_t width, int32_t height)
625{
626}
627
628static struct focus_surface *
629get_focus_surface(struct weston_surface *surface)
630{
631 if (surface->configure == focus_surface_configure)
632 return surface->configure_private;
633 else
634 return NULL;
635}
636
637static bool
638is_focus_surface (struct weston_surface *es)
639{
640 return (es->configure == focus_surface_configure);
641}
642
643static bool
644is_focus_view (struct weston_view *view)
645{
646 return is_focus_surface (view->surface);
647}
648
649static struct focus_surface *
650create_focus_surface(struct weston_compositor *ec,
651 struct weston_output *output)
652{
653 struct focus_surface *fsurf = NULL;
654 struct weston_surface *surface = NULL;
655
656 fsurf = malloc(sizeof *fsurf);
657 if (!fsurf)
658 return NULL;
659
660 fsurf->surface = weston_surface_create(ec);
661 surface = fsurf->surface;
662 if (surface == NULL) {
663 free(fsurf);
664 return NULL;
665 }
666
667 surface->configure = focus_surface_configure;
668 surface->output = output;
669 surface->configure_private = fsurf;
670
671 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100672 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100673
674 weston_view_configure(fsurf->view, output->x, output->y,
675 output->width, output->height);
676 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
677 pixman_region32_fini(&surface->opaque);
678 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
679 output->width, output->height);
680 pixman_region32_fini(&surface->input);
681 pixman_region32_init(&surface->input);
682
683 wl_list_init(&fsurf->workspace_transform.link);
684
685 return fsurf;
686}
687
688static void
689focus_surface_destroy(struct focus_surface *fsurf)
690{
691 weston_surface_destroy(fsurf->surface);
692 free(fsurf);
693}
694
695static void
696focus_animation_done(struct weston_view_animation *animation, void *data)
697{
698 struct workspace *ws = data;
699
700 ws->focus_animation = NULL;
701}
702
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200703static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200704focus_state_destroy(struct focus_state *state)
705{
706 wl_list_remove(&state->seat_destroy_listener.link);
707 wl_list_remove(&state->surface_destroy_listener.link);
708 free(state);
709}
710
711static void
712focus_state_seat_destroy(struct wl_listener *listener, void *data)
713{
714 struct focus_state *state = container_of(listener,
715 struct focus_state,
716 seat_destroy_listener);
717
718 wl_list_remove(&state->link);
719 focus_state_destroy(state);
720}
721
722static void
723focus_state_surface_destroy(struct wl_listener *listener, void *data)
724{
725 struct focus_state *state = container_of(listener,
726 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400727 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400728 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500729 struct weston_surface *main_surface, *next;
730 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200731
Pekka Paalanen01388e22013-04-25 13:57:44 +0300732 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
733
Kristian Høgsberge3778222012-07-31 17:29:30 -0400734 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500735 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
736 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400737 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100738 if (is_focus_view(view))
739 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400740
Jason Ekstranda7af7042013-10-12 22:38:11 -0500741 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400742 break;
743 }
744
Pekka Paalanen01388e22013-04-25 13:57:44 +0300745 /* if the focus was a sub-surface, activate its main surface */
746 if (main_surface != state->keyboard_focus)
747 next = main_surface;
748
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100749 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400750 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100751 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400752 activate(shell, next, state->seat);
753 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100754 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
755 if (state->ws->focus_animation)
756 weston_view_animation_destroy(state->ws->focus_animation);
757
758 state->ws->focus_animation = weston_fade_run(
759 state->ws->fsurf_front->view,
760 state->ws->fsurf_front->view->alpha, 0.0, 300,
761 focus_animation_done, state->ws);
762 }
763
Kristian Høgsberge3778222012-07-31 17:29:30 -0400764 wl_list_remove(&state->link);
765 focus_state_destroy(state);
766 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200767}
768
769static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400770focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200771{
Jonas Ådahl04769742012-06-13 00:01:24 +0200772 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200773
774 state = malloc(sizeof *state);
775 if (state == NULL)
776 return NULL;
777
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100778 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400779 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200780 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400781 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200782
783 state->seat_destroy_listener.notify = focus_state_seat_destroy;
784 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400785 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200786 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400787 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200788
789 return state;
790}
791
Jonas Ådahl8538b222012-08-29 22:13:03 +0200792static struct focus_state *
793ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
794{
795 struct workspace *ws = get_current_workspace(shell);
796 struct focus_state *state;
797
798 wl_list_for_each(state, &ws->focus_list, link)
799 if (state->seat == seat)
800 break;
801
802 if (&state->link == &ws->focus_list)
803 state = focus_state_create(seat, ws);
804
805 return state;
806}
807
Jonas Ådahl04769742012-06-13 00:01:24 +0200808static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400809restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200810{
811 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400812 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200813
814 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400815 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200816
Kristian Høgsberge3148752013-05-06 23:19:49 -0400817 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200818 }
819}
820
821static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200822replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
823 struct weston_seat *seat)
824{
825 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400826 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200827
828 wl_list_for_each(state, &ws->focus_list, link) {
829 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400830 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500831 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200832 return;
833 }
834 }
835}
836
837static void
838drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
839 struct weston_surface *surface)
840{
841 struct focus_state *state;
842
843 wl_list_for_each(state, &ws->focus_list, link)
844 if (state->keyboard_focus == surface)
845 state->keyboard_focus = NULL;
846}
847
848static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100849animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
850 struct weston_view *from, struct weston_view *to)
851{
852 struct weston_output *output;
853 bool focus_surface_created = false;
854
855 /* FIXME: Only support dim animation using two layers */
856 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
857 return;
858
859 output = get_default_output(shell->compositor);
860 if (ws->fsurf_front == NULL && (from || to)) {
861 ws->fsurf_front = create_focus_surface(shell->compositor, output);
862 ws->fsurf_back = create_focus_surface(shell->compositor, output);
863 ws->fsurf_front->view->alpha = 0.0;
864 ws->fsurf_back->view->alpha = 0.0;
865 focus_surface_created = true;
866 } else {
867 wl_list_remove(&ws->fsurf_front->view->layer_link);
868 wl_list_remove(&ws->fsurf_back->view->layer_link);
869 }
870
871 if (ws->focus_animation) {
872 weston_view_animation_destroy(ws->focus_animation);
873 ws->focus_animation = NULL;
874 }
875
876 if (to)
877 wl_list_insert(&to->layer_link,
878 &ws->fsurf_front->view->layer_link);
879 else if (from)
880 wl_list_insert(&ws->layer.view_list,
881 &ws->fsurf_front->view->layer_link);
882
883 if (focus_surface_created) {
884 ws->focus_animation = weston_fade_run(
885 ws->fsurf_front->view,
886 ws->fsurf_front->view->alpha, 0.6, 300,
887 focus_animation_done, ws);
888 } else if (from) {
889 wl_list_insert(&from->layer_link,
890 &ws->fsurf_back->view->layer_link);
891 ws->focus_animation = weston_stable_fade_run(
892 ws->fsurf_front->view, 0.0,
893 ws->fsurf_back->view, 0.6,
894 focus_animation_done, ws);
895 } else if (to) {
896 wl_list_insert(&ws->layer.view_list,
897 &ws->fsurf_back->view->layer_link);
898 ws->focus_animation = weston_stable_fade_run(
899 ws->fsurf_front->view, 0.0,
900 ws->fsurf_back->view, 0.6,
901 focus_animation_done, ws);
902 }
903}
904
905static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200906workspace_destroy(struct workspace *ws)
907{
Jonas Ådahl04769742012-06-13 00:01:24 +0200908 struct focus_state *state, *next;
909
910 wl_list_for_each_safe(state, next, &ws->focus_list, link)
911 focus_state_destroy(state);
912
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100913 if (ws->fsurf_front)
914 focus_surface_destroy(ws->fsurf_front);
915 if (ws->fsurf_back)
916 focus_surface_destroy(ws->fsurf_back);
917
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200918 free(ws);
919}
920
Jonas Ådahl04769742012-06-13 00:01:24 +0200921static void
922seat_destroyed(struct wl_listener *listener, void *data)
923{
924 struct weston_seat *seat = data;
925 struct focus_state *state, *next;
926 struct workspace *ws = container_of(listener,
927 struct workspace,
928 seat_destroyed_listener);
929
930 wl_list_for_each_safe(state, next, &ws->focus_list, link)
931 if (state->seat == seat)
932 wl_list_remove(&state->link);
933}
934
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200935static struct workspace *
936workspace_create(void)
937{
938 struct workspace *ws = malloc(sizeof *ws);
939 if (ws == NULL)
940 return NULL;
941
942 weston_layer_init(&ws->layer, NULL);
943
Jonas Ådahl04769742012-06-13 00:01:24 +0200944 wl_list_init(&ws->focus_list);
945 wl_list_init(&ws->seat_destroyed_listener.link);
946 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100947 ws->fsurf_front = NULL;
948 ws->fsurf_back = NULL;
949 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200950
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200951 return ws;
952}
953
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200954static int
955workspace_is_empty(struct workspace *ws)
956{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200958}
959
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200960static struct workspace *
961get_workspace(struct desktop_shell *shell, unsigned int index)
962{
963 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200964 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200965 pws += index;
966 return *pws;
967}
968
969static struct workspace *
970get_current_workspace(struct desktop_shell *shell)
971{
972 return get_workspace(shell, shell->workspaces.current);
973}
974
975static void
976activate_workspace(struct desktop_shell *shell, unsigned int index)
977{
978 struct workspace *ws;
979
980 ws = get_workspace(shell, index);
981 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
982
983 shell->workspaces.current = index;
984}
985
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986static unsigned int
987get_output_height(struct weston_output *output)
988{
989 return abs(output->region.extents.y1 - output->region.extents.y2);
990}
991
992static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100993view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200995 struct weston_transform *transform;
996
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100997 if (is_focus_view(view)) {
998 struct focus_surface *fsurf = get_focus_surface(view->surface);
999 transform = &fsurf->workspace_transform;
1000 } else {
1001 struct shell_surface *shsurf = get_shell_surface(view->surface);
1002 transform = &shsurf->workspace_transform;
1003 }
1004
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001007 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001008
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001009 weston_matrix_init(&transform->matrix);
1010 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001011 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013}
1014
1015static void
1016workspace_translate_out(struct workspace *ws, double fraction)
1017{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019 unsigned int height;
1020 double d;
1021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1023 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001024 d = height * fraction;
1025
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001026 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001027 }
1028}
1029
1030static void
1031workspace_translate_in(struct workspace *ws, double fraction)
1032{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001034 unsigned int height;
1035 double d;
1036
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1038 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001039
1040 if (fraction > 0)
1041 d = -(height - height * fraction);
1042 else
1043 d = height + height * fraction;
1044
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001045 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001046 }
1047}
1048
1049static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001050broadcast_current_workspace_state(struct desktop_shell *shell)
1051{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001052 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001053
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001054 wl_resource_for_each(resource, &shell->workspaces.client_list)
1055 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001056 shell->workspaces.current,
1057 shell->workspaces.num);
1058}
1059
1060static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001061reverse_workspace_change_animation(struct desktop_shell *shell,
1062 unsigned int index,
1063 struct workspace *from,
1064 struct workspace *to)
1065{
1066 shell->workspaces.current = index;
1067
1068 shell->workspaces.anim_to = to;
1069 shell->workspaces.anim_from = from;
1070 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1071 shell->workspaces.anim_timestamp = 0;
1072
Scott Moreau4272e632012-08-13 09:58:41 -06001073 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001074}
1075
1076static void
1077workspace_deactivate_transforms(struct workspace *ws)
1078{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001080 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001081
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001083 if (is_focus_view(view)) {
1084 struct focus_surface *fsurf = get_focus_surface(view->surface);
1085 transform = &fsurf->workspace_transform;
1086 } else {
1087 struct shell_surface *shsurf = get_shell_surface(view->surface);
1088 transform = &shsurf->workspace_transform;
1089 }
1090
1091 if (!wl_list_empty(&transform->link)) {
1092 wl_list_remove(&transform->link);
1093 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001094 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001095 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001096 }
1097}
1098
1099static void
1100finish_workspace_change_animation(struct desktop_shell *shell,
1101 struct workspace *from,
1102 struct workspace *to)
1103{
Scott Moreau4272e632012-08-13 09:58:41 -06001104 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105
1106 wl_list_remove(&shell->workspaces.animation.link);
1107 workspace_deactivate_transforms(from);
1108 workspace_deactivate_transforms(to);
1109 shell->workspaces.anim_to = NULL;
1110
1111 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1112}
1113
1114static void
1115animate_workspace_change_frame(struct weston_animation *animation,
1116 struct weston_output *output, uint32_t msecs)
1117{
1118 struct desktop_shell *shell =
1119 container_of(animation, struct desktop_shell,
1120 workspaces.animation);
1121 struct workspace *from = shell->workspaces.anim_from;
1122 struct workspace *to = shell->workspaces.anim_to;
1123 uint32_t t;
1124 double x, y;
1125
1126 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1127 finish_workspace_change_animation(shell, from, to);
1128 return;
1129 }
1130
1131 if (shell->workspaces.anim_timestamp == 0) {
1132 if (shell->workspaces.anim_current == 0.0)
1133 shell->workspaces.anim_timestamp = msecs;
1134 else
1135 shell->workspaces.anim_timestamp =
1136 msecs -
1137 /* Invers of movement function 'y' below. */
1138 (asin(1.0 - shell->workspaces.anim_current) *
1139 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1140 M_2_PI);
1141 }
1142
1143 t = msecs - shell->workspaces.anim_timestamp;
1144
1145 /*
1146 * x = [0, π/2]
1147 * y(x) = sin(x)
1148 */
1149 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1150 y = sin(x);
1151
1152 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001153 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154
1155 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1156 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1157 shell->workspaces.anim_current = y;
1158
Scott Moreau4272e632012-08-13 09:58:41 -06001159 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001161 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001162 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163}
1164
1165static void
1166animate_workspace_change(struct desktop_shell *shell,
1167 unsigned int index,
1168 struct workspace *from,
1169 struct workspace *to)
1170{
1171 struct weston_output *output;
1172
1173 int dir;
1174
1175 if (index > shell->workspaces.current)
1176 dir = -1;
1177 else
1178 dir = 1;
1179
1180 shell->workspaces.current = index;
1181
1182 shell->workspaces.anim_dir = dir;
1183 shell->workspaces.anim_from = from;
1184 shell->workspaces.anim_to = to;
1185 shell->workspaces.anim_current = 0.0;
1186 shell->workspaces.anim_timestamp = 0;
1187
1188 output = container_of(shell->compositor->output_list.next,
1189 struct weston_output, link);
1190 wl_list_insert(&output->animation_list,
1191 &shell->workspaces.animation.link);
1192
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001193 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001194
1195 workspace_translate_in(to, 0);
1196
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001197 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001198
Scott Moreau4272e632012-08-13 09:58:41 -06001199 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001200}
1201
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001202static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001203update_workspace(struct desktop_shell *shell, unsigned int index,
1204 struct workspace *from, struct workspace *to)
1205{
1206 shell->workspaces.current = index;
1207 wl_list_insert(&from->layer.link, &to->layer.link);
1208 wl_list_remove(&from->layer.link);
1209}
1210
1211static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001212change_workspace(struct desktop_shell *shell, unsigned int index)
1213{
1214 struct workspace *from;
1215 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001216 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001217
1218 if (index == shell->workspaces.current)
1219 return;
1220
1221 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001223 return;
1224
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001225 from = get_current_workspace(shell);
1226 to = get_workspace(shell, index);
1227
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001228 if (shell->workspaces.anim_from == to &&
1229 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001230 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001231 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001232 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001233 return;
1234 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001235
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001236 if (shell->workspaces.anim_to != NULL)
1237 finish_workspace_change_animation(shell,
1238 shell->workspaces.anim_from,
1239 shell->workspaces.anim_to);
1240
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001241 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001242
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001243 if (shell->focus_animation_type != ANIMATION_NONE) {
1244 wl_list_for_each(state, &from->focus_list, link)
1245 if (state->keyboard_focus)
1246 animate_focus_change(shell, from,
1247 get_default_view(state->keyboard_focus), NULL);
1248
1249 wl_list_for_each(state, &to->focus_list, link)
1250 if (state->keyboard_focus)
1251 animate_focus_change(shell, to,
1252 NULL, get_default_view(state->keyboard_focus));
1253 }
1254
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001255 if (workspace_is_empty(to) && workspace_is_empty(from))
1256 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001257 else
1258 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001259
1260 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001261}
1262
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001263static bool
1264workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1265{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267 struct wl_list *e;
1268
1269 if (wl_list_empty(list))
1270 return false;
1271
1272 e = list->next;
1273
1274 if (e->next != list)
1275 return false;
1276
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001278}
1279
1280static void
Philip Withnall659163d2013-11-25 18:01:36 +00001281move_surface_to_workspace(struct desktop_shell *shell,
1282 struct shell_surface *shsurf,
1283 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284{
1285 struct workspace *from;
1286 struct workspace *to;
1287 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001288 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001289 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001290
1291 if (workspace == shell->workspaces.current)
1292 return;
1293
Philip Withnall659163d2013-11-25 18:01:36 +00001294 view = get_default_view(shsurf->surface);
1295 if (!view)
1296 return;
1297
1298 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1299
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001300 if (workspace >= shell->workspaces.num)
1301 workspace = shell->workspaces.num - 1;
1302
Jonas Ådahle9d22502012-08-29 22:13:01 +02001303 from = get_current_workspace(shell);
1304 to = get_workspace(shell, workspace);
1305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 wl_list_remove(&view->layer_link);
1307 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001308
Philip Withnall648a4dd2013-11-25 18:01:44 +00001309 shell_surface_update_child_surface_layers(shsurf);
1310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001312 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1313 if (!seat->keyboard)
1314 continue;
1315
1316 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001318 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001319 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001322}
1323
1324static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001325take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001326 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001327 unsigned int index)
1328{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001329 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001331 struct shell_surface *shsurf;
1332 struct workspace *from;
1333 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001334 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001335
Pekka Paalanen01388e22013-04-25 13:57:44 +03001336 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 view = get_default_view(surface);
1338 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001339 index == shell->workspaces.current ||
1340 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001341 return;
1342
1343 from = get_current_workspace(shell);
1344 to = get_workspace(shell, index);
1345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 wl_list_remove(&view->layer_link);
1347 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001348
Philip Withnall659163d2013-11-25 18:01:36 +00001349 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001350 if (shsurf != NULL)
1351 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001352
Jonas Ådahle9d22502012-08-29 22:13:01 +02001353 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001354 drop_focus_state(shell, from, surface);
1355
1356 if (shell->workspaces.anim_from == to &&
1357 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001358 wl_list_remove(&to->layer.link);
1359 wl_list_insert(from->layer.link.prev, &to->layer.link);
1360
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001361 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001362 broadcast_current_workspace_state(shell);
1363
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001364 return;
1365 }
1366
1367 if (shell->workspaces.anim_to != NULL)
1368 finish_workspace_change_animation(shell,
1369 shell->workspaces.anim_from,
1370 shell->workspaces.anim_to);
1371
1372 if (workspace_is_empty(from) &&
1373 workspace_has_only(to, surface))
1374 update_workspace(shell, index, from, to);
1375 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001376 if (shsurf != NULL &&
1377 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001378 wl_list_insert(&shell->workspaces.anim_sticky_list,
1379 &shsurf->workspace_transform.link);
1380
1381 animate_workspace_change(shell, index, from, to);
1382 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001383
1384 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001385
1386 state = ensure_focus_state(shell, seat);
1387 if (state != NULL)
1388 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001389}
1390
1391static void
1392workspace_manager_move_surface(struct wl_client *client,
1393 struct wl_resource *resource,
1394 struct wl_resource *surface_resource,
1395 uint32_t workspace)
1396{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001397 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001398 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001399 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001400 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001401 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001402
Pekka Paalanen01388e22013-04-25 13:57:44 +03001403 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001404 shell_surface = get_shell_surface(main_surface);
1405 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001407
1408 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001409}
1410
1411static const struct workspace_manager_interface workspace_manager_implementation = {
1412 workspace_manager_move_surface,
1413};
1414
1415static void
1416unbind_resource(struct wl_resource *resource)
1417{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001418 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001419}
1420
1421static void
1422bind_workspace_manager(struct wl_client *client,
1423 void *data, uint32_t version, uint32_t id)
1424{
1425 struct desktop_shell *shell = data;
1426 struct wl_resource *resource;
1427
Jason Ekstranda85118c2013-06-27 20:17:02 -05001428 resource = wl_resource_create(client,
1429 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001430
1431 if (resource == NULL) {
1432 weston_log("couldn't add workspace manager object");
1433 return;
1434 }
1435
Jason Ekstranda85118c2013-06-27 20:17:02 -05001436 wl_resource_set_implementation(resource,
1437 &workspace_manager_implementation,
1438 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001439 wl_list_insert(&shell->workspaces.client_list,
1440 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001441
1442 workspace_manager_send_state(resource,
1443 shell->workspaces.current,
1444 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001445}
1446
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001447static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001448touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1449 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1450{
1451}
1452
1453static void
1454touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1455{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001456 struct weston_touch_move_grab *move =
1457 (struct weston_touch_move_grab *) container_of(
1458 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001459
Jonas Ådahl9484b692013-12-02 22:05:03 +01001460 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001461 shell_touch_grab_end(&move->base);
1462 free(move);
1463 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001464}
1465
1466static void
1467touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1468 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1469{
1470 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1471 struct shell_surface *shsurf = move->base.shsurf;
1472 struct weston_surface *es;
1473 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1474 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1475
1476 if (!shsurf)
1477 return;
1478
1479 es = shsurf->surface;
1480
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 weston_view_configure(shsurf->view, dx, dy,
1482 shsurf->view->geometry.width,
1483 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001484
1485 weston_compositor_schedule_repaint(es->compositor);
1486}
1487
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001488static void
1489touch_move_grab_cancel(struct weston_touch_grab *grab)
1490{
1491 struct weston_touch_move_grab *move =
1492 (struct weston_touch_move_grab *) container_of(
1493 grab, struct shell_touch_grab, grab);
1494
1495 shell_touch_grab_end(&move->base);
1496 free(move);
1497}
1498
Rusty Lynch1084da52013-08-15 09:10:08 -07001499static const struct weston_touch_grab_interface touch_move_grab_interface = {
1500 touch_move_grab_down,
1501 touch_move_grab_up,
1502 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001503 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001504};
1505
1506static int
1507surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1508{
1509 struct weston_touch_move_grab *move;
1510
1511 if (!shsurf)
1512 return -1;
1513
1514 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1515 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001516 if (shsurf->grabbed)
1517 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001518
1519 move = malloc(sizeof *move);
1520 if (!move)
1521 return -1;
1522
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001524 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001526 seat->touch->grab_y;
1527
1528 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1529 seat->touch);
1530
1531 return 0;
1532}
1533
1534static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001535noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001536{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001537}
1538
1539static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001540move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1541 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001542{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001543 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001544 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001545 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001546 int dx, dy;
1547
1548 weston_pointer_move(pointer, x, y);
1549 dx = wl_fixed_to_int(pointer->x + move->dx);
1550 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001551
1552 if (!shsurf)
1553 return;
1554
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555 weston_view_configure(shsurf->view, dx, dy,
1556 shsurf->view->geometry.width,
1557 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001558
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001560}
1561
1562static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001563move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001564 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001565{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001566 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1567 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001568 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001569 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001570
Daniel Stone4dbadb12012-05-30 16:31:51 +01001571 if (pointer->button_count == 0 &&
1572 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001573 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001574 free(grab);
1575 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001576}
1577
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001578static void
1579move_grab_cancel(struct weston_pointer_grab *grab)
1580{
1581 struct shell_grab *shell_grab =
1582 container_of(grab, struct shell_grab, grab);
1583
1584 shell_grab_end(shell_grab);
1585 free(grab);
1586}
1587
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001588static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001589 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001590 move_grab_motion,
1591 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001592 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001593};
1594
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001596surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597{
1598 struct weston_move_grab *move;
1599
1600 if (!shsurf)
1601 return -1;
1602
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001603 if (shsurf->grabbed)
1604 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001605 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1606 return 0;
1607
1608 move = malloc(sizeof *move);
1609 if (!move)
1610 return -1;
1611
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001613 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001615 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001616
1617 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001618 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001619
1620 return 0;
1621}
1622
1623static void
1624shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1625 struct wl_resource *seat_resource, uint32_t serial)
1626{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001627 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001628 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001629 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001630
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001631 if (seat->pointer &&
1632 seat->pointer->button_count > 0 &&
1633 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001635 if ((surface == shsurf->surface) &&
1636 (surface_move(shsurf, seat) < 0))
1637 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001638 } else if (seat->touch &&
1639 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001641 if ((surface == shsurf->surface) &&
1642 (surface_touch_move(shsurf, seat) < 0))
1643 wl_resource_post_no_memory(resource);
1644 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645}
1646
1647struct weston_resize_grab {
1648 struct shell_grab base;
1649 uint32_t edges;
1650 int32_t width, height;
1651};
1652
1653static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001654resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1655 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001656{
1657 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001658 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001659 struct shell_surface *shsurf = resize->base.shsurf;
1660 int32_t width, height;
1661 wl_fixed_t from_x, from_y;
1662 wl_fixed_t to_x, to_y;
1663
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001664 weston_pointer_move(pointer, x, y);
1665
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001666 if (!shsurf)
1667 return;
1668
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 weston_view_from_global_fixed(shsurf->view,
1670 pointer->grab_x, pointer->grab_y,
1671 &from_x, &from_y);
1672 weston_view_from_global_fixed(shsurf->view,
1673 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001674
1675 width = resize->width;
1676 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1677 width += wl_fixed_to_int(from_x - to_x);
1678 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1679 width += wl_fixed_to_int(to_x - from_x);
1680 }
1681
1682 height = resize->height;
1683 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1684 height += wl_fixed_to_int(from_y - to_y);
1685 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1686 height += wl_fixed_to_int(to_y - from_y);
1687 }
1688
1689 shsurf->client->send_configure(shsurf->surface,
1690 resize->edges, width, height);
1691}
1692
1693static void
1694send_configure(struct weston_surface *surface,
1695 uint32_t edges, int32_t width, int32_t height)
1696{
1697 struct shell_surface *shsurf = get_shell_surface(surface);
1698
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001699 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001700 edges, width, height);
1701}
1702
1703static const struct weston_shell_client shell_client = {
1704 send_configure
1705};
1706
1707static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001708resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709 uint32_t time, uint32_t button, uint32_t state_w)
1710{
1711 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001712 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713 enum wl_pointer_button_state state = state_w;
1714
1715 if (pointer->button_count == 0 &&
1716 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1717 shell_grab_end(&resize->base);
1718 free(grab);
1719 }
1720}
1721
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001722static void
1723resize_grab_cancel(struct weston_pointer_grab *grab)
1724{
1725 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1726
1727 shell_grab_end(&resize->base);
1728 free(grab);
1729}
1730
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001731static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732 noop_grab_focus,
1733 resize_grab_motion,
1734 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001735 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736};
1737
Giulio Camuffob8366642013-04-25 13:57:46 +03001738/*
1739 * Returns the bounding box of a surface and all its sub-surfaces,
1740 * in the surface coordinates system. */
1741static void
1742surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1743 int32_t *y, int32_t *w, int32_t *h) {
1744 pixman_region32_t region;
1745 pixman_box32_t *box;
1746 struct weston_subsurface *subsurface;
1747
1748 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001749 surface->width,
1750 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001751
1752 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1753 pixman_region32_union_rect(&region, &region,
1754 subsurface->position.x,
1755 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001756 subsurface->surface->width,
1757 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001758 }
1759
1760 box = pixman_region32_extents(&region);
1761 if (x)
1762 *x = box->x1;
1763 if (y)
1764 *y = box->y1;
1765 if (w)
1766 *w = box->x2 - box->x1;
1767 if (h)
1768 *h = box->y2 - box->y1;
1769
1770 pixman_region32_fini(&region);
1771}
1772
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001773static int
1774surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001775 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001776{
1777 struct weston_resize_grab *resize;
1778
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001779 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1780 shsurf->type == SHELL_SURFACE_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
1810 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1811 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) {
2083 panel_height = view->geometry.height;
2084 break;
2085 }
2086 }
2087
2088 return panel_height;
2089}
2090
Philip Withnall07926d92013-11-25 18:01:40 +00002091/* The surface will be inserted into the list immediately after the link
2092 * returned by this function (i.e. will be stacked immediately above the
2093 * returned link). */
2094static struct wl_list *
2095shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2096{
2097 struct workspace *ws;
2098
2099 switch (shsurf->type) {
Philip Withnallda704d92013-11-25 18:01:46 +00002100 case SHELL_SURFACE_POPUP: {
2101 /* Popups should go at the front of the workspace of their
2102 * parent surface, rather than just in front of the parent. This
2103 * fixes the situation where there are two top-level windows:
2104 * - Above
2105 * - Below
2106 * and a pop-up menu is created for 'Below'. We want:
2107 * - Popup
2108 * - Above
2109 * - Below
2110 * not:
2111 * - Above
2112 * - Popup
2113 * - Below
2114 */
2115 struct shell_surface *parent_shsurf;
2116
2117 parent_shsurf = get_shell_surface(shsurf->parent);
2118 if (parent_shsurf != NULL)
2119 return shell_surface_calculate_layer_link(parent_shsurf);
2120
2121 break;
2122 }
2123
Philip Withnall07926d92013-11-25 18:01:40 +00002124 case SHELL_SURFACE_TRANSIENT: {
2125 /* Move the surface to its parent layer so that surfaces which
2126 * are transient for fullscreen surfaces don't get hidden by the
Philip Withnallda704d92013-11-25 18:01:46 +00002127 * fullscreen surfaces. However, unlike popups, transient
2128 * surfaces are stacked in front of their parent but not in
2129 * front of other surfaces of the same type. */
Philip Withnall07926d92013-11-25 18:01:40 +00002130 struct weston_view *parent;
2131
2132 /* TODO: Handle a parent with multiple views */
2133 parent = get_default_view(shsurf->parent);
2134 if (parent)
2135 return parent->layer_link.prev;
2136
2137 break;
2138 }
2139
2140 case SHELL_SURFACE_FULLSCREEN:
2141 return &shsurf->shell->fullscreen_layer.view_list;
2142
2143 case SHELL_SURFACE_XWAYLAND:
2144 case SHELL_SURFACE_TOPLEVEL:
2145 case SHELL_SURFACE_MAXIMIZED:
2146 case SHELL_SURFACE_NONE:
2147 default:
2148 /* Go to the fallback, below. */
2149 break;
2150 }
2151
2152 /* Move the surface to a normal workspace layer so that surfaces
2153 * which were previously fullscreen or transient are no longer
2154 * rendered on top. */
2155 ws = get_current_workspace(shsurf->shell);
2156 return &ws->layer.view_list;
2157}
2158
Philip Withnall648a4dd2013-11-25 18:01:44 +00002159static void
2160shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2161{
2162 struct shell_surface *child;
2163
2164 /* Move the child layers to the same workspace as shsurf. They will be
2165 * stacked above shsurf. */
2166 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2167 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2168 weston_view_geometry_dirty(child->view);
2169 wl_list_remove(&child->view->layer_link);
2170 wl_list_insert(shsurf->view->layer_link.prev,
2171 &child->view->layer_link);
2172 weston_view_geometry_dirty(child->view);
2173 weston_surface_damage(child->surface);
2174
2175 /* Recurse. We don’t expect this to recurse very far (if
2176 * at all) because that would imply we have transient
2177 * (or popup) children of transient surfaces, which
2178 * would be unusual. */
2179 shell_surface_update_child_surface_layers(child);
2180 }
2181 }
2182}
2183
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002184/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002185 * geometry to ensure the changes are redrawn.
2186 *
2187 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2188 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002189static void
2190shell_surface_update_layer(struct shell_surface *shsurf)
2191{
2192 struct wl_list *new_layer_link;
2193
2194 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2195
2196 if (new_layer_link == &shsurf->view->layer_link)
2197 return;
2198
2199 weston_view_geometry_dirty(shsurf->view);
2200 wl_list_remove(&shsurf->view->layer_link);
2201 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2202 weston_view_geometry_dirty(shsurf->view);
2203 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002204
2205 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002206}
2207
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002208static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002209shell_surface_set_parent(struct shell_surface *shsurf,
2210 struct weston_surface *parent)
2211{
2212 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002213
2214 wl_list_remove(&shsurf->children_link);
2215 wl_list_init(&shsurf->children_link);
2216
2217 /* Insert into the parent surface’s child list. */
2218 if (parent != NULL) {
2219 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2220 if (parent_shsurf != NULL)
2221 wl_list_insert(&parent_shsurf->children_list,
2222 &shsurf->children_link);
2223 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002224}
2225
2226static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002227shell_surface_set_output(struct shell_surface *shsurf,
2228 struct weston_output *output)
2229{
2230 struct weston_surface *es = shsurf->surface;
2231
2232 /* get the default output, if the client set it as NULL
2233 check whether the ouput is available */
2234 if (output)
2235 shsurf->output = output;
2236 else if (es->output)
2237 shsurf->output = es->output;
2238 else
2239 shsurf->output = get_default_output(es->compositor);
2240}
2241
2242static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002243set_toplevel(struct shell_surface *shsurf)
2244{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002245 shell_surface_set_parent(shsurf, NULL);
2246
Philip Withnallbecb77e2013-11-25 18:01:30 +00002247 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002248
2249 /* The layer_link is updated in set_surface_type(),
2250 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002251}
2252
2253static void
2254shell_surface_set_toplevel(struct wl_client *client,
2255 struct wl_resource *resource)
2256{
2257 struct shell_surface *surface = wl_resource_get_user_data(resource);
2258
2259 set_toplevel(surface);
2260}
2261
2262static void
2263set_transient(struct shell_surface *shsurf,
2264 struct weston_surface *parent, int x, int y, uint32_t flags)
2265{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002266 assert(parent != NULL);
2267
Philip Withnallbecb77e2013-11-25 18:01:30 +00002268 shsurf->transient.x = x;
2269 shsurf->transient.y = y;
2270 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002271
2272 shell_surface_set_parent(shsurf, parent);
2273
Philip Withnallbecb77e2013-11-25 18:01:30 +00002274 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002275
2276 /* The layer_link is updated in set_surface_type(),
2277 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002278}
2279
2280static void
2281shell_surface_set_transient(struct wl_client *client,
2282 struct wl_resource *resource,
2283 struct wl_resource *parent_resource,
2284 int x, int y, uint32_t flags)
2285{
2286 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2287 struct weston_surface *parent =
2288 wl_resource_get_user_data(parent_resource);
2289
2290 set_transient(shsurf, parent, x, y, flags);
2291}
2292
2293static void
2294set_fullscreen(struct shell_surface *shsurf,
2295 uint32_t method,
2296 uint32_t framerate,
2297 struct weston_output *output)
2298{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002299 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002300
2301 shsurf->fullscreen_output = shsurf->output;
2302 shsurf->fullscreen.type = method;
2303 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002304
2305 shell_surface_set_parent(shsurf, NULL);
2306
Philip Withnallbecb77e2013-11-25 18:01:30 +00002307 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
2308
2309 shsurf->client->send_configure(shsurf->surface, 0,
2310 shsurf->output->width,
2311 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002312
2313 /* The layer_link is updated in set_surface_type(),
2314 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002315}
2316
2317static void
2318unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002319{
Philip Withnallf85fe842013-11-25 18:01:37 +00002320 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002321 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2322 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002323 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002324 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002325 shsurf->fullscreen_output = NULL;
2326
Alex Wu4539b082012-03-01 12:57:46 +08002327 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2328 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002329
Alex Wu4539b082012-03-01 12:57:46 +08002330 wl_list_remove(&shsurf->fullscreen.transform.link);
2331 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002332
Jason Ekstranda7af7042013-10-12 22:38:11 -05002333 if (shsurf->fullscreen.black_view)
2334 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2335 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002336
Jason Ekstranda7af7042013-10-12 22:38:11 -05002337 weston_view_set_position(shsurf->view,
2338 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002339 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002340 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002341 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002342 shsurf->saved_rotation_valid = false;
2343 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002344
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002345 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002346}
2347
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002348static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002349shell_surface_set_fullscreen(struct wl_client *client,
2350 struct wl_resource *resource,
2351 uint32_t method,
2352 uint32_t framerate,
2353 struct wl_resource *output_resource)
2354{
2355 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2356 struct weston_output *output;
2357
2358 if (output_resource)
2359 output = wl_resource_get_user_data(output_resource);
2360 else
2361 output = NULL;
2362
2363 set_fullscreen(shsurf, method, framerate, output);
2364}
2365
2366static void
2367set_popup(struct shell_surface *shsurf,
2368 struct weston_surface *parent,
2369 struct weston_seat *seat,
2370 uint32_t serial,
2371 int32_t x,
2372 int32_t y)
2373{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002374 assert(parent != NULL);
2375
Philip Withnallbecb77e2013-11-25 18:01:30 +00002376 shsurf->popup.shseat = get_shell_seat(seat);
2377 shsurf->popup.serial = serial;
2378 shsurf->popup.x = x;
2379 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002380
2381 shell_surface_set_parent(shsurf, parent);
Philip Withnallb995e1d2013-11-25 18:01:39 +00002382
2383 shsurf->next_type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002384}
2385
2386static void
2387shell_surface_set_popup(struct wl_client *client,
2388 struct wl_resource *resource,
2389 struct wl_resource *seat_resource,
2390 uint32_t serial,
2391 struct wl_resource *parent_resource,
2392 int32_t x, int32_t y, uint32_t flags)
2393{
2394 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2395
2396 set_popup(shsurf,
2397 wl_resource_get_user_data(parent_resource),
2398 wl_resource_get_user_data(seat_resource),
2399 serial, x, y);
2400}
2401
2402static void
2403set_maximized(struct shell_surface *shsurf,
2404 struct weston_output *output)
2405{
2406 struct desktop_shell *shell;
2407 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002408
Philip Withnall352e7ed2013-11-25 18:01:35 +00002409 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002410
2411 shell = shell_surface_get_shell(shsurf);
2412 panel_height = get_output_panel_height(shell, shsurf->output);
2413 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2414
2415 shsurf->client->send_configure(shsurf->surface, edges,
2416 shsurf->output->width,
2417 shsurf->output->height - panel_height);
2418
Philip Withnalldc4332f2013-11-25 18:01:38 +00002419 shell_surface_set_parent(shsurf, NULL);
2420
Philip Withnallbecb77e2013-11-25 18:01:30 +00002421 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
2422}
2423
2424static void
2425unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002426{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002427 /* undo all maximized things here */
2428 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002429 weston_view_set_position(shsurf->view,
2430 shsurf->saved_x,
2431 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002432
2433 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434 wl_list_insert(&shsurf->view->geometry.transformation_list,
2435 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002436 shsurf->saved_rotation_valid = false;
2437 }
2438
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002439 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002440}
2441
Philip Withnallbecb77e2013-11-25 18:01:30 +00002442static void
2443shell_surface_set_maximized(struct wl_client *client,
2444 struct wl_resource *resource,
2445 struct wl_resource *output_resource)
2446{
2447 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2448 struct weston_output *output;
2449
2450 if (output_resource)
2451 output = wl_resource_get_user_data(output_resource);
2452 else
2453 output = NULL;
2454
2455 set_maximized(shsurf, output);
2456}
2457
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002458/* This is only ever called from set_surface_type(), so there’s no need to
2459 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002460static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002461reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002462{
2463 switch (surface->type) {
2464 case SHELL_SURFACE_FULLSCREEN:
Philip Withnallbecb77e2013-11-25 18:01:30 +00002465 unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002466 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002467 case SHELL_SURFACE_MAXIMIZED:
Philip Withnallbecb77e2013-11-25 18:01:30 +00002468 unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002469 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002470 case SHELL_SURFACE_NONE:
2471 case SHELL_SURFACE_TOPLEVEL:
2472 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002473 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002474 case SHELL_SURFACE_XWAYLAND:
Philip Withnall0f640e22013-11-25 18:01:31 +00002475 default:
Pekka Paalanen98262232011-12-01 10:42:22 +02002476 break;
2477 }
2478
2479 surface->type = SHELL_SURFACE_NONE;
2480 return 0;
2481}
2482
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002483static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002484set_surface_type(struct shell_surface *shsurf)
2485{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002486 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002488
Philip Withnallbecb77e2013-11-25 18:01:30 +00002489 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002490
2491 shsurf->type = shsurf->next_type;
2492 shsurf->next_type = SHELL_SURFACE_NONE;
2493
2494 switch (shsurf->type) {
2495 case SHELL_SURFACE_TOPLEVEL:
2496 break;
2497 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002498 if (pev)
2499 weston_view_set_position(shsurf->view,
2500 pev->geometry.x + shsurf->transient.x,
2501 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002502 break;
2503
2504 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002505 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002506 shsurf->saved_x = shsurf->view->geometry.x;
2507 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002508 shsurf->saved_position_valid = true;
2509
2510 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2511 wl_list_remove(&shsurf->rotation.transform.link);
2512 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002513 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002514 shsurf->saved_rotation_valid = true;
2515 }
2516 break;
2517
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002518 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002519 weston_view_set_position(shsurf->view, shsurf->transient.x,
2520 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002521 break;
2522
Philip Withnall0f640e22013-11-25 18:01:31 +00002523 case SHELL_SURFACE_POPUP:
2524 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002525 default:
2526 break;
2527 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002528
2529 /* Update the surface’s layer. */
2530 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002531}
2532
Tiago Vignattibe143262012-04-16 17:31:41 +03002533static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002534shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002535{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002536 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002537}
2538
Alex Wu21858432012-04-01 20:13:08 +08002539static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002540black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height);
Alex Wu21858432012-04-01 20:13:08 +08002541
Jason Ekstranda7af7042013-10-12 22:38:11 -05002542static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002543create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002544 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002545 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002546{
2547 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002548 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002549
2550 surface = weston_surface_create(ec);
2551 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002552 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002553 return NULL;
2554 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 view = weston_view_create(surface);
2556 if (surface == NULL) {
2557 weston_log("no memory\n");
2558 weston_surface_destroy(surface);
2559 return NULL;
2560 }
Alex Wu4539b082012-03-01 12:57:46 +08002561
Alex Wu21858432012-04-01 20:13:08 +08002562 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002563 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002564 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002565 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002566 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002567 pixman_region32_fini(&surface->input);
2568 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002569 surface->width = w;
2570 surface->height = h;
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002571
Jason Ekstranda7af7042013-10-12 22:38:11 -05002572 weston_view_configure(view, x, y, w, h);
2573
2574 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002575}
2576
Philip Withnalled8f1a92013-11-25 18:01:43 +00002577static void
2578shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2579{
2580 struct weston_output *output = shsurf->fullscreen_output;
2581
2582 assert(shsurf->type == SHELL_SURFACE_FULLSCREEN);
2583
2584 if (!shsurf->fullscreen.black_view)
2585 shsurf->fullscreen.black_view =
2586 create_black_surface(shsurf->surface->compositor,
2587 shsurf->surface,
2588 output->x, output->y,
2589 output->width,
2590 output->height);
2591
2592 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2593 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2594 wl_list_insert(&shsurf->view->layer_link,
2595 &shsurf->fullscreen.black_view->layer_link);
2596 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2597 weston_surface_damage(shsurf->surface);
2598}
2599
Alex Wu4539b082012-03-01 12:57:46 +08002600/* Create black surface and append it to the associated fullscreen surface.
2601 * Handle size dismatch and positioning according to the method. */
2602static void
2603shell_configure_fullscreen(struct shell_surface *shsurf)
2604{
2605 struct weston_output *output = shsurf->fullscreen_output;
2606 struct weston_surface *surface = shsurf->surface;
2607 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002608 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002609 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002610
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002611 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2612 restore_output_mode(output);
2613
Philip Withnalled8f1a92013-11-25 18:01:43 +00002614 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002615
Jason Ekstranda7af7042013-10-12 22:38:11 -05002616 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002617 &surf_width, &surf_height);
2618
Alex Wu4539b082012-03-01 12:57:46 +08002619 switch (shsurf->fullscreen.type) {
2620 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002621 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002622 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002623 break;
2624 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002625 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002626 if (output->width == surf_width &&
2627 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628 weston_view_set_position(shsurf->view,
2629 output->x - surf_x,
2630 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002631 break;
2632 }
2633
Alex Wu4539b082012-03-01 12:57:46 +08002634 matrix = &shsurf->fullscreen.transform.matrix;
2635 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002636
Scott Moreau1bad5db2012-08-18 01:04:05 -06002637 output_aspect = (float) output->width /
2638 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639 /* XXX: Use surf_width and surf_height here? */
2640 surface_aspect = (float) surface->width /
2641 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002642 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002643 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002644 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002645 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002646 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002647 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002648
Alex Wu4539b082012-03-01 12:57:46 +08002649 weston_matrix_scale(matrix, scale, scale, 1);
2650 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002651 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002652 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002653 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2654 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002655 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002656
Alex Wu4539b082012-03-01 12:57:46 +08002657 break;
2658 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002659 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002660 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002661 surf_width * surface->buffer_viewport.scale,
2662 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002663 shsurf->fullscreen.framerate};
2664
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002665 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002666 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 weston_view_set_position(shsurf->view,
2668 output->x - surf_x,
2669 output->y - surf_y);
2670 weston_view_configure(shsurf->fullscreen.black_view,
2671 output->x - surf_x,
2672 output->y - surf_y,
2673 output->width,
2674 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002675 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002676 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002677 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002678 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002679 }
Alex Wubd3354b2012-04-17 17:20:49 +08002680 }
Alex Wu4539b082012-03-01 12:57:46 +08002681 break;
2682 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002684 break;
2685 default:
2686 break;
2687 }
2688}
2689
Alex Wu4539b082012-03-01 12:57:46 +08002690static void
2691shell_map_fullscreen(struct shell_surface *shsurf)
2692{
Alex Wubd3354b2012-04-17 17:20:49 +08002693 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002694}
2695
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002696static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002697set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2698{
2699 /* XXX: using the same fields for transient type */
2700 shsurf->transient.x = x;
2701 shsurf->transient.y = y;
2702 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002703
2704 shell_surface_set_parent(shsurf, NULL);
2705
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002706 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2707}
2708
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002709static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002710
2711static void
2712destroy_shell_seat(struct wl_listener *listener, void *data)
2713{
2714 struct shell_seat *shseat =
2715 container_of(listener,
2716 struct shell_seat, seat_destroy_listener);
2717 struct shell_surface *shsurf, *prev = NULL;
2718
2719 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002720 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002721 shseat->popup_grab.client = NULL;
2722
2723 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2724 shsurf->popup.shseat = NULL;
2725 if (prev) {
2726 wl_list_init(&prev->popup.grab_link);
2727 }
2728 prev = shsurf;
2729 }
2730 wl_list_init(&prev->popup.grab_link);
2731 }
2732
2733 wl_list_remove(&shseat->seat_destroy_listener.link);
2734 free(shseat);
2735}
2736
2737static struct shell_seat *
2738create_shell_seat(struct weston_seat *seat)
2739{
2740 struct shell_seat *shseat;
2741
2742 shseat = calloc(1, sizeof *shseat);
2743 if (!shseat) {
2744 weston_log("no memory to allocate shell seat\n");
2745 return NULL;
2746 }
2747
2748 shseat->seat = seat;
2749 wl_list_init(&shseat->popup_grab.surfaces_list);
2750
2751 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2752 wl_signal_add(&seat->destroy_signal,
2753 &shseat->seat_destroy_listener);
2754
2755 return shseat;
2756}
2757
2758static struct shell_seat *
2759get_shell_seat(struct weston_seat *seat)
2760{
2761 struct wl_listener *listener;
2762
2763 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2764 if (listener == NULL)
2765 return create_shell_seat(seat);
2766
2767 return container_of(listener,
2768 struct shell_seat, seat_destroy_listener);
2769}
2770
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002771static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002772popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002773{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002774 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002776 struct shell_seat *shseat =
2777 container_of(grab, struct shell_seat, popup_grab.grab);
2778 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002779 wl_fixed_t sx, sy;
2780
Jason Ekstranda7af7042013-10-12 22:38:11 -05002781 view = weston_compositor_pick_view(pointer->seat->compositor,
2782 pointer->x, pointer->y,
2783 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002784
Jason Ekstranda7af7042013-10-12 22:38:11 -05002785 if (view && view->surface->resource &&
2786 wl_resource_get_client(view->surface->resource) == client) {
2787 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002788 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002789 weston_pointer_set_focus(pointer, NULL,
2790 wl_fixed_from_int(0),
2791 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002792 }
2793}
2794
2795static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002796popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2797 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002798{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002799 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002800 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002801 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002802
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002803 weston_pointer_move(pointer, x, y);
2804
Neil Roberts96d790e2013-09-19 17:32:00 +01002805 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002806 weston_view_from_global_fixed(pointer->focus,
2807 pointer->x, pointer->y,
2808 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002809 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002810 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002811}
2812
2813static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002814popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002815 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002816{
2817 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002818 struct shell_seat *shseat =
2819 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002820 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002821 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002822 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002823 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002824
Neil Roberts96d790e2013-09-19 17:32:00 +01002825 resource_list = &grab->pointer->focus_resource_list;
2826 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002827 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002828 wl_resource_for_each(resource, resource_list) {
2829 wl_pointer_send_button(resource, serial,
2830 time, button, state);
2831 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002832 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002833 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002834 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002835 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002836 }
2837
Daniel Stone4dbadb12012-05-30 16:31:51 +01002838 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002839 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002840}
2841
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002842static void
2843popup_grab_cancel(struct weston_pointer_grab *grab)
2844{
2845 popup_grab_end(grab->pointer);
2846}
2847
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002848static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002849 popup_grab_focus,
2850 popup_grab_motion,
2851 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002852 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002853};
2854
2855static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002856popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002857{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002858 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002859 struct shell_seat *shseat =
2860 container_of(grab, struct shell_seat, popup_grab.grab);
2861 struct shell_surface *shsurf;
2862 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002863
2864 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002865 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002866 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002867 shseat->popup_grab.grab.interface = NULL;
2868 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002869 /* Send the popup_done event to all the popups open */
2870 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002871 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002872 shsurf->popup.shseat = NULL;
2873 if (prev) {
2874 wl_list_init(&prev->popup.grab_link);
2875 }
2876 prev = shsurf;
2877 }
2878 wl_list_init(&prev->popup.grab_link);
2879 wl_list_init(&shseat->popup_grab.surfaces_list);
2880 }
2881}
2882
2883static void
2884add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2885{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002886 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002887
2888 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002889 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002890 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002891 /* We must make sure here that this popup was opened after
2892 * a mouse press, and not just by moving around with other
2893 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002894 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002895 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002896
Rob Bradforddfe31052013-06-26 19:49:11 +01002897 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002898 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002899 } else {
2900 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002901 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002902}
2903
2904static void
2905remove_popup_grab(struct shell_surface *shsurf)
2906{
2907 struct shell_seat *shseat = shsurf->popup.shseat;
2908
2909 wl_list_remove(&shsurf->popup.grab_link);
2910 wl_list_init(&shsurf->popup.grab_link);
2911 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002912 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002913 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002914 }
2915}
2916
2917static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002918shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002919{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002920 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002921 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002922
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 shsurf->surface->output = parent_view->output;
2924 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002925
Jason Ekstranda7af7042013-10-12 22:38:11 -05002926 weston_view_set_transform_parent(shsurf->view, parent_view);
2927 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2928 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002929
Kristian Høgsberge3148752013-05-06 23:19:49 -04002930 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002931 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002932 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002933 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002934 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002935 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002936}
2937
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002938static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002939 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002940 shell_surface_move,
2941 shell_surface_resize,
2942 shell_surface_set_toplevel,
2943 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002944 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002945 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002946 shell_surface_set_maximized,
2947 shell_surface_set_title,
2948 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002949};
2950
2951static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002952destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002953{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002954 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2955
Giulio Camuffo5085a752013-03-25 21:42:45 +01002956 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2957 remove_popup_grab(shsurf);
2958 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002959
Alex Wubd3354b2012-04-17 17:20:49 +08002960 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002961 shell_surface_is_top_fullscreen(shsurf))
2962 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002963
Jason Ekstranda7af7042013-10-12 22:38:11 -05002964 if (shsurf->fullscreen.black_view)
2965 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002966
Alex Wubd3354b2012-04-17 17:20:49 +08002967 /* As destroy_resource() use wl_list_for_each_safe(),
2968 * we can always remove the listener.
2969 */
2970 wl_list_remove(&shsurf->surface_destroy_listener.link);
2971 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002972 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002973 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002974
Jason Ekstranda7af7042013-10-12 22:38:11 -05002975 weston_view_destroy(shsurf->view);
2976
Philip Withnall648a4dd2013-11-25 18:01:44 +00002977 wl_list_remove(&shsurf->children_link);
2978
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002979 wl_list_remove(&shsurf->link);
2980 free(shsurf);
2981}
2982
2983static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002984shell_destroy_shell_surface(struct wl_resource *resource)
2985{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002986 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002987
2988 destroy_shell_surface(shsurf);
2989}
2990
2991static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002992shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002993{
2994 struct shell_surface *shsurf = container_of(listener,
2995 struct shell_surface,
2996 surface_destroy_listener);
2997
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002998 if (shsurf->resource)
2999 wl_resource_destroy(shsurf->resource);
3000 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003001 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003002}
3003
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003004static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003005shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003006
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003007static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003008get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003009{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003010 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003011 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003012 else
3013 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003014}
3015
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003016static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003017create_shell_surface(void *shell, struct weston_surface *surface,
3018 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003019{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003020 struct shell_surface *shsurf;
3021
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003022 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003023 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003024 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003025 }
3026
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003027 shsurf = calloc(1, sizeof *shsurf);
3028 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003029 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003030 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003031 }
3032
Jason Ekstranda7af7042013-10-12 22:38:11 -05003033 shsurf->view = weston_view_create(surface);
3034 if (!shsurf->view) {
3035 weston_log("no memory to allocate shell surface\n");
3036 free(shsurf);
3037 return NULL;
3038 }
3039
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003040 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003041 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003042
Tiago Vignattibc052c92012-04-19 16:18:18 +03003043 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003044 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003045 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003046 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003047 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003048 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3049 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003050 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003051 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003052 wl_list_init(&shsurf->fullscreen.transform.link);
3053
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003054 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003055 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003056 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003057 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003058
3059 /* init link so its safe to always remove it in destroy_shell_surface */
3060 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003061 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003062
Pekka Paalanen460099f2012-01-20 16:48:25 +02003063 /* empty when not in use */
3064 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003065 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003066
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003067 wl_list_init(&shsurf->workspace_transform.link);
3068
Philip Withnall648a4dd2013-11-25 18:01:44 +00003069 wl_list_init(&shsurf->children_link);
3070 wl_list_init(&shsurf->children_list);
3071 shsurf->parent = NULL;
3072
Pekka Paalanen98262232011-12-01 10:42:22 +02003073 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003074 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003075
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003076 shsurf->client = client;
3077
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003078 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003079}
3080
Jason Ekstranda7af7042013-10-12 22:38:11 -05003081static struct weston_view *
3082get_primary_view(void *shell, struct shell_surface *shsurf)
3083{
3084 return shsurf->view;
3085}
3086
Tiago Vignattibc052c92012-04-19 16:18:18 +03003087static void
3088shell_get_shell_surface(struct wl_client *client,
3089 struct wl_resource *resource,
3090 uint32_t id,
3091 struct wl_resource *surface_resource)
3092{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003093 struct weston_surface *surface =
3094 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003095 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003096 struct shell_surface *shsurf;
3097
3098 if (get_shell_surface(surface)) {
3099 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003100 WL_DISPLAY_ERROR_INVALID_OBJECT,
3101 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003102 return;
3103 }
3104
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003105 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003106 if (!shsurf) {
3107 wl_resource_post_error(surface_resource,
3108 WL_DISPLAY_ERROR_INVALID_OBJECT,
3109 "surface->configure already set");
3110 return;
3111 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003112
Jason Ekstranda85118c2013-06-27 20:17:02 -05003113 shsurf->resource =
3114 wl_resource_create(client,
3115 &wl_shell_surface_interface, 1, id);
3116 wl_resource_set_implementation(shsurf->resource,
3117 &shell_surface_implementation,
3118 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003119}
3120
3121static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003122 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003123};
3124
Kristian Høgsberg07937562011-04-12 17:25:42 -04003125static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003126shell_fade(struct desktop_shell *shell, enum fade_type type);
3127
3128static int
3129screensaver_timeout(void *data)
3130{
3131 struct desktop_shell *shell = data;
3132
3133 shell_fade(shell, FADE_OUT);
3134
3135 return 1;
3136}
3137
3138static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003139handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003140{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003141 struct desktop_shell *shell =
3142 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003143
Pekka Paalanen18027e52011-12-02 16:31:49 +02003144 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003145
3146 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003147 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003148}
3149
3150static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003151launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003152{
3153 if (shell->screensaver.binding)
3154 return;
3155
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003156 if (!shell->screensaver.path) {
3157 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003158 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003159 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003160
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003161 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003162 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003163 return;
3164 }
3165
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003166 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003167 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003168 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003169 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003170}
3171
3172static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003173terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003174{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003175 if (shell->screensaver.process.pid == 0)
3176 return;
3177
3178 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003179}
3180
3181static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003182configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003183{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003184 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003185
Giulio Camuffo184df502013-02-21 11:29:21 +01003186 if (width == 0)
3187 return;
3188
Jason Ekstranda7af7042013-10-12 22:38:11 -05003189 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3190 if (v->output == ev->output && v != ev) {
3191 weston_view_unmap(v);
3192 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003193 }
3194 }
3195
Jason Ekstranda7af7042013-10-12 22:38:11 -05003196 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003197
Jason Ekstranda7af7042013-10-12 22:38:11 -05003198 if (wl_list_empty(&ev->layer_link)) {
3199 wl_list_insert(&layer->view_list, &ev->layer_link);
3200 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003201 }
3202}
3203
3204static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003205background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003206{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003207 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003208 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003209
Jason Ekstranda7af7042013-10-12 22:38:11 -05003210 view = container_of(es->views.next, struct weston_view, surface_link);
3211
3212 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003213}
3214
3215static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003216desktop_shell_set_background(struct wl_client *client,
3217 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003218 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003219 struct wl_resource *surface_resource)
3220{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003221 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003222 struct weston_surface *surface =
3223 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003224 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003225
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003226 if (surface->configure) {
3227 wl_resource_post_error(surface_resource,
3228 WL_DISPLAY_ERROR_INVALID_OBJECT,
3229 "surface role already assigned");
3230 return;
3231 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003232
Jason Ekstranda7af7042013-10-12 22:38:11 -05003233 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3234 weston_view_destroy(view);
3235 view = weston_view_create(surface);
3236
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003237 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003238 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003239 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003240 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003241 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003242 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003243 surface->output->width,
3244 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003245}
3246
3247static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003248panel_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003249{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003250 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003251 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003252
Jason Ekstranda7af7042013-10-12 22:38:11 -05003253 view = container_of(es->views.next, struct weston_view, surface_link);
3254
3255 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003256}
3257
3258static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003259desktop_shell_set_panel(struct wl_client *client,
3260 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003261 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003262 struct wl_resource *surface_resource)
3263{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003264 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003265 struct weston_surface *surface =
3266 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003267 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003268
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003269 if (surface->configure) {
3270 wl_resource_post_error(surface_resource,
3271 WL_DISPLAY_ERROR_INVALID_OBJECT,
3272 "surface role already assigned");
3273 return;
3274 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003275
Jason Ekstranda7af7042013-10-12 22:38:11 -05003276 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3277 weston_view_destroy(view);
3278 view = weston_view_create(surface);
3279
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003280 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003281 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003282 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003283 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003284 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003285 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003286 surface->output->width,
3287 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003288}
3289
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003290static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003291lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003292{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003293 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003294 struct weston_view *view;
3295
3296 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003297
Giulio Camuffo184df502013-02-21 11:29:21 +01003298 if (width == 0)
3299 return;
3300
Jason Ekstranda7af7042013-10-12 22:38:11 -05003301 surface->width = width;
3302 surface->height = height;
3303 view->geometry.width = width;
3304 view->geometry.height = height;
3305 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003306
3307 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003308 wl_list_insert(&shell->lock_layer.view_list,
3309 &view->layer_link);
3310 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003311 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003312 }
3313}
3314
3315static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003316handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003317{
Tiago Vignattibe143262012-04-16 17:31:41 +03003318 struct desktop_shell *shell =
3319 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003320
Martin Minarik6d118362012-06-07 18:01:59 +02003321 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003322 shell->lock_surface = NULL;
3323}
3324
3325static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003326desktop_shell_set_lock_surface(struct wl_client *client,
3327 struct wl_resource *resource,
3328 struct wl_resource *surface_resource)
3329{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003330 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003331 struct weston_surface *surface =
3332 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003333
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003334 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003335
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003336 if (!shell->locked)
3337 return;
3338
Pekka Paalanen98262232011-12-01 10:42:22 +02003339 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003340
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003341 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003342 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003343 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003344
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003345 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003346 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003347 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003348}
3349
3350static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003351resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003352{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003353 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003354
Pekka Paalanen77346a62011-11-30 16:26:35 +02003355 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003356
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003357 wl_list_remove(&shell->lock_layer.link);
3358 wl_list_insert(&shell->compositor->cursor_layer.link,
3359 &shell->fullscreen_layer.link);
3360 wl_list_insert(&shell->fullscreen_layer.link,
3361 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003362 if (shell->showing_input_panels) {
3363 wl_list_insert(&shell->panel_layer.link,
3364 &shell->input_panel_layer.link);
3365 wl_list_insert(&shell->input_panel_layer.link,
3366 &ws->layer.link);
3367 } else {
3368 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3369 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003370
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003371 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003372
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003373 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003374 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003375 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003376}
3377
3378static void
3379desktop_shell_unlock(struct wl_client *client,
3380 struct wl_resource *resource)
3381{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003382 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003383
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003384 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003385
3386 if (shell->locked)
3387 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003388}
3389
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003390static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003391desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003392 struct wl_resource *resource,
3393 struct wl_resource *surface_resource)
3394{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003395 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003396
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003397 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003398 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003399}
3400
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003401static void
3402desktop_shell_desktop_ready(struct wl_client *client,
3403 struct wl_resource *resource)
3404{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003405 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003406
3407 shell_fade_startup(shell);
3408}
3409
Kristian Høgsberg75840622011-09-06 13:48:16 -04003410static const struct desktop_shell_interface desktop_shell_implementation = {
3411 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003412 desktop_shell_set_panel,
3413 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003414 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003415 desktop_shell_set_grab_surface,
3416 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003417};
3418
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003419static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003420get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003421{
3422 struct shell_surface *shsurf;
3423
3424 shsurf = get_shell_surface(surface);
3425 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003426 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003427 return shsurf->type;
3428}
3429
Kristian Høgsberg75840622011-09-06 13:48:16 -04003430static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003431move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003432{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003433 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003434 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003435 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003436
Pekka Paalanen01388e22013-04-25 13:57:44 +03003437 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003438 if (surface == NULL)
3439 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003440
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003441 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003442 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3443 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003444 return;
3445
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003446 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003447}
3448
3449static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003450touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3451{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003452 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003453 struct weston_surface *surface;
3454 struct shell_surface *shsurf;
3455
3456 surface = weston_surface_get_main_surface(focus);
3457 if (surface == NULL)
3458 return;
3459
3460 shsurf = get_shell_surface(surface);
3461 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3462 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3463 return;
3464
3465 surface_touch_move(shsurf, (struct weston_seat *) seat);
3466}
3467
3468static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003469resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003470{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003471 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003472 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003473 uint32_t edges = 0;
3474 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003475 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003476
Pekka Paalanen01388e22013-04-25 13:57:44 +03003477 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003478 if (surface == NULL)
3479 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003480
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003481 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003482 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3483 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003484 return;
3485
Jason Ekstranda7af7042013-10-12 22:38:11 -05003486 weston_view_from_global(shsurf->view,
3487 wl_fixed_to_int(seat->pointer->grab_x),
3488 wl_fixed_to_int(seat->pointer->grab_y),
3489 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003490
Jason Ekstranda7af7042013-10-12 22:38:11 -05003491 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003492 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003493 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003494 edges |= 0;
3495 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003496 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003497
Jason Ekstranda7af7042013-10-12 22:38:11 -05003498 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003499 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003500 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003501 edges |= 0;
3502 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003503 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003504
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003505 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003506}
3507
3508static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003509surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003510 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003511{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003512 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003513 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003514 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003515 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003516
Pekka Paalanen01388e22013-04-25 13:57:44 +03003517 /* XXX: broken for windows containing sub-surfaces */
3518 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003519 if (surface == NULL)
3520 return;
3521
3522 shsurf = get_shell_surface(surface);
3523 if (!shsurf)
3524 return;
3525
Jason Ekstranda7af7042013-10-12 22:38:11 -05003526 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003527
Jason Ekstranda7af7042013-10-12 22:38:11 -05003528 if (shsurf->view->alpha > 1.0)
3529 shsurf->view->alpha = 1.0;
3530 if (shsurf->view->alpha < step)
3531 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003532
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003534 weston_surface_damage(surface);
3535}
3536
3537static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003538do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003539 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003540{
Daniel Stone37816df2012-05-16 18:45:18 +01003541 struct weston_seat *ws = (struct weston_seat *) seat;
3542 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003543 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003544 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003545
3546 wl_list_for_each(output, &compositor->output_list, link) {
3547 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003548 wl_fixed_to_double(seat->pointer->x),
3549 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003550 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003551 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003552 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003553 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003554 increment = -output->zoom.increment;
3555 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003556 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003557 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003558 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003559 else
3560 increment = 0;
3561
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003562 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003563
Scott Moreaue6603982012-06-11 13:07:51 -06003564 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003565 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003566 else if (output->zoom.level > output->zoom.max_level)
3567 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003568 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003569 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003570 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003571
Scott Moreaue6603982012-06-11 13:07:51 -06003572 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003573
Jason Ekstranda7af7042013-10-12 22:38:11 -05003574 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003575 }
3576 }
3577}
3578
Scott Moreauccbf29d2012-02-22 14:21:41 -07003579static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003580zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003581 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003582{
3583 do_zoom(seat, time, 0, axis, value);
3584}
3585
3586static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003587zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003588 void *data)
3589{
3590 do_zoom(seat, time, key, 0, 0);
3591}
3592
3593static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003594terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003595 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003597 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003598
Daniel Stone325fc2d2012-05-30 16:31:58 +01003599 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003600}
3601
3602static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003603lower_fullscreen_layer(struct desktop_shell *shell);
3604
3605struct alt_tab {
3606 struct desktop_shell *shell;
3607 struct weston_keyboard_grab grab;
3608
3609 struct wl_list *current;
3610
3611 struct wl_list preview_list;
3612};
3613
3614struct alt_tab_preview {
3615 struct alt_tab *alt_tab;
3616
3617 struct weston_view *view;
3618 struct weston_transform transform;
3619
3620 struct wl_listener listener;
3621
3622 struct wl_list link;
3623};
3624
3625static void
3626alt_tab_next(struct alt_tab *alt_tab)
3627{
3628 alt_tab->current = alt_tab->current->next;
3629
3630 /* Make sure we're not pointing to the list header e.g. after
3631 * cycling through the whole list. */
3632 if (alt_tab->current->next == alt_tab->preview_list.next)
3633 alt_tab->current = alt_tab->current->next;
3634}
3635
3636static void
3637alt_tab_destroy(struct alt_tab *alt_tab)
3638{
3639 struct alt_tab_preview *preview, *next;
3640 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3641
3642 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3643 preview = wl_container_of(alt_tab->current, preview, link);
3644
3645 activate(alt_tab->shell, preview->view->surface,
3646 (struct weston_seat *) keyboard->seat);
3647 }
3648
3649 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3650 wl_list_remove(&preview->link);
3651 wl_list_remove(&preview->listener.link);
3652 weston_view_destroy(preview->view);
3653 free(preview);
3654 }
3655
3656 weston_keyboard_end_grab(keyboard);
3657 if (keyboard->input_method_resource)
3658 keyboard->grab = &keyboard->input_method_grab;
3659
3660 free(alt_tab);
3661}
3662
3663static void
3664alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3665{
3666 struct alt_tab_preview *preview =
3667 container_of(listener, struct alt_tab_preview, listener);
3668 struct alt_tab *alt_tab = preview->alt_tab;
3669 int advance = 0;
3670
3671 /* If the preview that we're removing is the currently selected one,
3672 * we want to move to the next one. So we move to ->prev and then
3673 * call _next() after removing the preview. */
3674 if (alt_tab->current == &preview->link) {
3675 alt_tab->current = alt_tab->current->prev;
3676 advance = 1;
3677 }
3678
3679 wl_list_remove(&preview->listener.link);
3680 wl_list_remove(&preview->link);
3681
3682 free(preview);
3683
3684 if (advance)
3685 alt_tab_next(alt_tab);
3686
3687 /* If the last preview goes away, stop the alt-tab */
3688 if (wl_list_empty(alt_tab->current))
3689 alt_tab_destroy(alt_tab);
3690}
3691
3692static void
3693alt_tab_key(struct weston_keyboard_grab *grab,
3694 uint32_t time, uint32_t key, uint32_t state_w)
3695{
3696 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3697 enum wl_keyboard_key_state state = state_w;
3698
3699 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3700 alt_tab_next(alt_tab);
3701}
3702
3703static void
3704alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3705 uint32_t mods_depressed, uint32_t mods_latched,
3706 uint32_t mods_locked, uint32_t group)
3707{
3708 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3709 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3710
3711 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3712 alt_tab_destroy(alt_tab);
3713}
3714
3715static void
3716alt_tab_cancel(struct weston_keyboard_grab *grab)
3717{
3718 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3719
3720 alt_tab_destroy(alt_tab);
3721}
3722
3723static const struct weston_keyboard_grab_interface alt_tab_grab = {
3724 alt_tab_key,
3725 alt_tab_modifier,
3726 alt_tab_cancel,
3727};
3728
3729static int
3730view_for_alt_tab(struct weston_view *view)
3731{
3732 if (!get_shell_surface(view->surface))
3733 return 0;
3734
3735 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3736 return 0;
3737
3738 if (view != get_default_view(view->surface))
3739 return 0;
3740
3741 return 1;
3742}
3743
3744static void
3745alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3746 void *data)
3747{
3748 struct alt_tab *alt_tab;
3749 struct desktop_shell *shell = data;
3750 struct weston_output *output = get_default_output(shell->compositor);
3751 struct workspace *ws;
3752 struct weston_view *view;
3753 int num_surfaces = 0;
3754 int x, y, side, margin;
3755
3756 wl_list_for_each(view, &shell->compositor->view_list, link) {
3757
3758 if (view_for_alt_tab(view))
3759 num_surfaces++;
3760 }
3761
3762 if (!num_surfaces)
3763 return;
3764
3765 alt_tab = malloc(sizeof *alt_tab);
3766 if (!alt_tab)
3767 return;
3768
3769 alt_tab->shell = shell;
3770 wl_list_init(&alt_tab->preview_list);
3771 alt_tab->current = &alt_tab->preview_list;
3772
3773 restore_all_output_modes(shell->compositor);
3774 lower_fullscreen_layer(alt_tab->shell);
3775
3776 alt_tab->grab.interface = &alt_tab_grab;
3777 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3778 weston_keyboard_set_focus(seat->keyboard, NULL);
3779
3780 ws = get_current_workspace(shell);
3781
3782 /* FIXME: add some visual candy e.g. prelight the selected view
3783 * and/or add a black surrounding background à la gnome-shell */
3784
3785 /* Determine the size for each preview */
3786 side = output->width / num_surfaces;
3787 if (side > 200)
3788 side = 200;
3789 margin = side / 4;
3790 side -= margin;
3791
3792 x = margin;
3793 y = (output->height - side) / 2;
3794
3795 /* Create a view for each surface */
3796 wl_list_for_each(view, &shell->compositor->view_list, link) {
3797 struct alt_tab_preview *preview;
3798 struct weston_view *v;
3799 float scale;
3800
3801 if (!view_for_alt_tab(view))
3802 continue;
3803
3804 preview = malloc(sizeof *preview);
3805 if (!preview) {
3806 alt_tab_destroy(alt_tab);
3807 return;
3808 }
3809
3810 preview->alt_tab = alt_tab;
3811
3812 preview->view = v = weston_view_create(view->surface);
3813 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003814
3815 wl_list_remove(&v->layer_link);
3816 wl_list_insert(&ws->layer.view_list, &v->layer_link);
3817 weston_view_damage_below(v);
3818 weston_surface_damage(v->surface);
3819
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003820 weston_view_configure(v, x, y, view->geometry.width, view->geometry.height);
3821
3822 preview->listener.notify = alt_tab_handle_surface_destroy;
3823 wl_signal_add(&v->destroy_signal, &preview->listener);
3824
3825 if (view->geometry.width > view->geometry.height)
3826 scale = side / (float) view->geometry.width;
3827 else
3828 scale = side / (float) view->geometry.height;
3829
3830 wl_list_insert(&v->geometry.transformation_list,
3831 &preview->transform.link);
3832 weston_matrix_init(&preview->transform.matrix);
3833 weston_matrix_scale(&preview->transform.matrix,
3834 scale, scale, 1.0f);
3835
3836 weston_view_geometry_dirty(v);
3837 weston_compositor_schedule_repaint(v->surface->compositor);
3838
3839 /* Insert at the end of the list */
3840 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3841
3842 x += side + margin;
3843 }
3844
3845 /* Start at the second preview so a simple <alt>tab changes window.
3846 * We set `current' to the first preview and not the second because
3847 * we're going to receive a key press callback for the initial
3848 * <alt>tab which will make `current' point to the second element. */
3849 alt_tab_next(alt_tab);
3850}
3851
3852static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003853rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3854 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003855{
3856 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003857 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003858 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003859 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003860 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003861
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003862 weston_pointer_move(pointer, x, y);
3863
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003864 if (!shsurf)
3865 return;
3866
Jason Ekstranda7af7042013-10-12 22:38:11 -05003867 cx = 0.5f * shsurf->view->geometry.width;
3868 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003869
Daniel Stone37816df2012-05-16 18:45:18 +01003870 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3871 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003872 r = sqrtf(dx * dx + dy * dy);
3873
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003874 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003875 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003876
3877 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003878 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003879 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003880
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003881 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003882 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003883
3884 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003885 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003886 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003887 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003888 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003889
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003890 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003891 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003892 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003893 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003894 wl_list_init(&shsurf->rotation.transform.link);
3895 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003896 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003897 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003898
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003899 /* We need to adjust the position of the surface
3900 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003901 cposx = shsurf->view->geometry.x + cx;
3902 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003903 dposx = rotate->center.x - cposx;
3904 dposy = rotate->center.y - cposy;
3905 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003906 weston_view_set_position(shsurf->view,
3907 shsurf->view->geometry.x + dposx,
3908 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003909 }
3910
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003911 /* Repaint implies weston_surface_update_transform(), which
3912 * lazily applies the damage due to rotation update.
3913 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003914 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003915}
3916
3917static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003918rotate_grab_button(struct weston_pointer_grab *grab,
3919 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003920{
3921 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003922 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003923 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003924 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003925 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003926
Daniel Stone4dbadb12012-05-30 16:31:51 +01003927 if (pointer->button_count == 0 &&
3928 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003929 if (shsurf)
3930 weston_matrix_multiply(&shsurf->rotation.rotation,
3931 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003932 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003933 free(rotate);
3934 }
3935}
3936
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003937static void
3938rotate_grab_cancel(struct weston_pointer_grab *grab)
3939{
3940 struct rotate_grab *rotate =
3941 container_of(grab, struct rotate_grab, base.grab);
3942
3943 shell_grab_end(&rotate->base);
3944 free(rotate);
3945}
3946
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003947static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003948 noop_grab_focus,
3949 rotate_grab_motion,
3950 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003951 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003952};
3953
3954static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003955surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003956{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003957 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003958 float dx, dy;
3959 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003960
Pekka Paalanen460099f2012-01-20 16:48:25 +02003961 rotate = malloc(sizeof *rotate);
3962 if (!rotate)
3963 return;
3964
Jason Ekstranda7af7042013-10-12 22:38:11 -05003965 weston_view_to_global_float(surface->view,
3966 surface->view->geometry.width * 0.5f,
3967 surface->view->geometry.height * 0.5f,
3968 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003969
Daniel Stone37816df2012-05-16 18:45:18 +01003970 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3971 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003972 r = sqrtf(dx * dx + dy * dy);
3973 if (r > 20.0f) {
3974 struct weston_matrix inverse;
3975
3976 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003977 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003978 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003979
3980 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003981 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003982 } else {
3983 weston_matrix_init(&surface->rotation.rotation);
3984 weston_matrix_init(&rotate->rotation);
3985 }
3986
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003987 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3988 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003989}
3990
3991static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003992rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003993 void *data)
3994{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003995 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003996 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003997 struct shell_surface *surface;
3998
Pekka Paalanen01388e22013-04-25 13:57:44 +03003999 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004000 if (base_surface == NULL)
4001 return;
4002
4003 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01004004 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
4005 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004006 return;
4007
4008 surface_rotate(surface, seat);
4009}
4010
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004011/* Move all fullscreen layers down to the current workspace in a non-reversible
4012 * manner. This should be used when implementing shell-wide overlays, such as
4013 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004014static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004015lower_fullscreen_layer(struct desktop_shell *shell)
4016{
4017 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004018 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004019
4020 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004021 wl_list_for_each_reverse_safe(view, prev,
4022 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004023 layer_link) {
4024 wl_list_remove(&view->layer_link);
4025 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4026 weston_view_damage_below(view);
4027 weston_surface_damage(view->surface);
4028 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004029}
4030
4031static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004032activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004033 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004034{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004035 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004036 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004037 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004038 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004039
Pekka Paalanen01388e22013-04-25 13:57:44 +03004040 main_surface = weston_surface_get_main_surface(es);
4041
Daniel Stone37816df2012-05-16 18:45:18 +01004042 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004043
Jonas Ådahl8538b222012-08-29 22:13:03 +02004044 state = ensure_focus_state(shell, seat);
4045 if (state == NULL)
4046 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004047
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004048 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004049 state->keyboard_focus = es;
4050 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004051 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004052
Pekka Paalanen01388e22013-04-25 13:57:44 +03004053 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08004054 case SHELL_SURFACE_FULLSCREEN:
4055 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03004056 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004057 return;
Philip Withnall0f640e22013-11-25 18:01:31 +00004058 case SHELL_SURFACE_TOPLEVEL:
4059 case SHELL_SURFACE_TRANSIENT:
4060 case SHELL_SURFACE_MAXIMIZED:
4061 case SHELL_SURFACE_POPUP:
4062 case SHELL_SURFACE_XWAYLAND:
4063 case SHELL_SURFACE_NONE:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004064 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004065 restore_all_output_modes(shell->compositor);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004066 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004067 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004068
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004069 if (shell->focus_animation_type != ANIMATION_NONE) {
4070 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004071 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004072 }
4073
4074 /* Update the surface’s layer. This brings it to the top of the stacking
4075 * order as appropriate. */
4076 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004077}
4078
Alex Wu21858432012-04-01 20:13:08 +08004079/* no-op func for checking black surface */
4080static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004081black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Alex Wu21858432012-04-01 20:13:08 +08004082{
4083}
4084
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004085static bool
Alex Wu21858432012-04-01 20:13:08 +08004086is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4087{
4088 if (es->configure == black_surface_configure) {
4089 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004090 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004091 return true;
4092 }
4093 return false;
4094}
4095
Kristian Høgsberg75840622011-09-06 13:48:16 -04004096static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004097activate_binding(struct weston_seat *seat,
4098 struct desktop_shell *shell,
4099 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004100{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004101 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004102
Alex Wu9c35e6b2012-03-05 14:13:13 +08004103 if (!focus)
4104 return;
4105
Pekka Paalanen01388e22013-04-25 13:57:44 +03004106 if (is_black_surface(focus, &main_surface))
4107 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004108
Pekka Paalanen01388e22013-04-25 13:57:44 +03004109 main_surface = weston_surface_get_main_surface(focus);
4110 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004111 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004112
Neil Robertsa28c6932013-10-03 16:43:04 +01004113 activate(shell, focus, seat);
4114}
4115
4116static void
4117click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4118 void *data)
4119{
4120 if (seat->pointer->grab != &seat->pointer->default_grab)
4121 return;
4122
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004123 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004124}
4125
4126static void
4127touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4128{
4129 if (seat->touch->grab != &seat->touch->default_grab)
4130 return;
4131
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004132 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004133}
4134
4135static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004136lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004137{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004138 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004139
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004140 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004141 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004142 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004143 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004144
4145 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004146
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004147 /* Hide all surfaces by removing the fullscreen, panel and
4148 * toplevel layers. This way nothing else can show or receive
4149 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004150
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004151 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004152 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004153 if (shell->showing_input_panels)
4154 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004155 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004156 wl_list_insert(&shell->compositor->cursor_layer.link,
4157 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004158
Pekka Paalanen77346a62011-11-30 16:26:35 +02004159 launch_screensaver(shell);
4160
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004161 /* TODO: disable bindings that should not work while locked. */
4162
4163 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004164}
4165
4166static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004167unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004168{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004169 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004170 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004171 return;
4172 }
4173
4174 /* If desktop-shell client has gone away, unlock immediately. */
4175 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004176 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004177 return;
4178 }
4179
4180 if (shell->prepare_event_sent)
4181 return;
4182
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004183 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004184 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004185}
4186
4187static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004188shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004189{
4190 struct desktop_shell *shell = data;
4191
4192 shell->fade.animation = NULL;
4193
4194 switch (shell->fade.type) {
4195 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004196 weston_surface_destroy(shell->fade.view->surface);
4197 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004198 break;
4199 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004200 lock(shell);
4201 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004202 default:
4203 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004204 }
4205}
4206
Jason Ekstranda7af7042013-10-12 22:38:11 -05004207static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004208shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004209{
4210 struct weston_compositor *compositor = shell->compositor;
4211 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004212 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004213
4214 surface = weston_surface_create(compositor);
4215 if (!surface)
4216 return NULL;
4217
Jason Ekstranda7af7042013-10-12 22:38:11 -05004218 view = weston_view_create(surface);
4219 if (!view) {
4220 weston_surface_destroy(surface);
4221 return NULL;
4222 }
4223
4224 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004225 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004226 wl_list_insert(&compositor->fade_layer.view_list,
4227 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004228 pixman_region32_init(&surface->input);
4229
Jason Ekstranda7af7042013-10-12 22:38:11 -05004230 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004231}
4232
4233static void
4234shell_fade(struct desktop_shell *shell, enum fade_type type)
4235{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004236 float tint;
4237
4238 switch (type) {
4239 case FADE_IN:
4240 tint = 0.0;
4241 break;
4242 case FADE_OUT:
4243 tint = 1.0;
4244 break;
4245 default:
4246 weston_log("shell: invalid fade type\n");
4247 return;
4248 }
4249
4250 shell->fade.type = type;
4251
Jason Ekstranda7af7042013-10-12 22:38:11 -05004252 if (shell->fade.view == NULL) {
4253 shell->fade.view = shell_fade_create_surface(shell);
4254 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004255 return;
4256
Jason Ekstranda7af7042013-10-12 22:38:11 -05004257 shell->fade.view->alpha = 1.0 - tint;
4258 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004259 }
4260
4261 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004262 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004263 else
4264 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004265 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004266 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004267 shell_fade_done, shell);
4268}
4269
4270static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004271do_shell_fade_startup(void *data)
4272{
4273 struct desktop_shell *shell = data;
4274
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004275 if (shell->startup_animation_type == ANIMATION_FADE)
4276 shell_fade(shell, FADE_IN);
4277 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004278 weston_surface_destroy(shell->fade.view->surface);
4279 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004280 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004281}
4282
4283static void
4284shell_fade_startup(struct desktop_shell *shell)
4285{
4286 struct wl_event_loop *loop;
4287
4288 if (!shell->fade.startup_timer)
4289 return;
4290
4291 wl_event_source_remove(shell->fade.startup_timer);
4292 shell->fade.startup_timer = NULL;
4293
4294 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4295 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4296}
4297
4298static int
4299fade_startup_timeout(void *data)
4300{
4301 struct desktop_shell *shell = data;
4302
4303 shell_fade_startup(shell);
4304 return 0;
4305}
4306
4307static void
4308shell_fade_init(struct desktop_shell *shell)
4309{
4310 /* Make compositor output all black, and wait for the desktop-shell
4311 * client to signal it is ready, then fade in. The timer triggers a
4312 * fade-in, in case the desktop-shell client takes too long.
4313 */
4314
4315 struct wl_event_loop *loop;
4316
Jason Ekstranda7af7042013-10-12 22:38:11 -05004317 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004318 weston_log("%s: warning: fade surface already exists\n",
4319 __func__);
4320 return;
4321 }
4322
Jason Ekstranda7af7042013-10-12 22:38:11 -05004323 shell->fade.view = shell_fade_create_surface(shell);
4324 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004325 return;
4326
Jason Ekstranda7af7042013-10-12 22:38:11 -05004327 weston_view_update_transform(shell->fade.view);
4328 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004329
4330 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4331 shell->fade.startup_timer =
4332 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4333 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4334}
4335
4336static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004337idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004338{
4339 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004340 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004341
4342 shell_fade(shell, FADE_OUT);
4343 /* lock() is called from shell_fade_done() */
4344}
4345
4346static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004347wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004348{
4349 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004350 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004351
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004352 unlock(shell);
4353}
4354
4355static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004356show_input_panels(struct wl_listener *listener, void *data)
4357{
4358 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004359 container_of(listener, struct desktop_shell,
4360 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004362
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004363 shell->text_input.surface = (struct weston_surface*)data;
4364
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004365 if (shell->showing_input_panels)
4366 return;
4367
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004368 shell->showing_input_panels = true;
4369
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004370 if (!shell->locked)
4371 wl_list_insert(&shell->panel_layer.link,
4372 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004373
Jason Ekstranda7af7042013-10-12 22:38:11 -05004374 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004375 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004377 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004378 wl_list_insert(&shell->input_panel_layer.view_list,
4379 &ipsurf->view->layer_link);
4380 weston_view_geometry_dirty(ipsurf->view);
4381 weston_view_update_transform(ipsurf->view);
4382 weston_surface_damage(ipsurf->surface);
4383 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
4384 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004385 }
4386}
4387
4388static void
4389hide_input_panels(struct wl_listener *listener, void *data)
4390{
4391 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004392 container_of(listener, struct desktop_shell,
4393 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004394 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004395
Jan Arne Petersen61381972013-01-31 15:52:21 +01004396 if (!shell->showing_input_panels)
4397 return;
4398
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004399 shell->showing_input_panels = false;
4400
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004401 if (!shell->locked)
4402 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004403
Jason Ekstranda7af7042013-10-12 22:38:11 -05004404 wl_list_for_each_safe(view, next,
4405 &shell->input_panel_layer.view_list, layer_link)
4406 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004407}
4408
4409static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004410update_input_panels(struct wl_listener *listener, void *data)
4411{
4412 struct desktop_shell *shell =
4413 container_of(listener, struct desktop_shell,
4414 update_input_panel_listener);
4415
4416 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4417}
4418
4419static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004420center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004421{
Giulio Camuffob8366642013-04-25 13:57:46 +03004422 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004423 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004424
Jason Ekstranda7af7042013-10-12 22:38:11 -05004425 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004426
4427 x = output->x + (output->width - width) / 2 - surf_x / 2;
4428 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004429
Jason Ekstranda7af7042013-10-12 22:38:11 -05004430 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004431}
4432
4433static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004434weston_view_set_initial_position(struct weston_view *view,
4435 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004436{
4437 struct weston_compositor *compositor = shell->compositor;
4438 int ix = 0, iy = 0;
4439 int range_x, range_y;
4440 int dx, dy, x, y, panel_height;
4441 struct weston_output *output, *target_output = NULL;
4442 struct weston_seat *seat;
4443
4444 /* As a heuristic place the new window on the same output as the
4445 * pointer. Falling back to the output containing 0, 0.
4446 *
4447 * TODO: Do something clever for touch too?
4448 */
4449 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004450 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004451 ix = wl_fixed_to_int(seat->pointer->x);
4452 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004453 break;
4454 }
4455 }
4456
4457 wl_list_for_each(output, &compositor->output_list, link) {
4458 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4459 target_output = output;
4460 break;
4461 }
4462 }
4463
4464 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004465 weston_view_set_position(view, 10 + random() % 400,
4466 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004467 return;
4468 }
4469
4470 /* Valid range within output where the surface will still be onscreen.
4471 * If this is negative it means that the surface is bigger than
4472 * output.
4473 */
4474 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004475 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004476 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05004477 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004478
Rob Bradford4cb88c72012-08-13 15:18:44 +01004479 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004480 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004481 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004482 dx = 0;
4483
4484 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004485 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004486 else
4487 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004488
4489 x = target_output->x + dx;
4490 y = target_output->y + dy;
4491
Jason Ekstranda7af7042013-10-12 22:38:11 -05004492 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004493}
4494
4495static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004496map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004497 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004498{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004499 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004500 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004501 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004502 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004503
Jason Ekstranda7af7042013-10-12 22:38:11 -05004504 shsurf->view->geometry.width = width;
4505 shsurf->view->geometry.height = height;
4506 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004507
4508 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004509 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004510 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004511 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004512 break;
Alex Wu4539b082012-03-01 12:57:46 +08004513 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004514 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004515 shell_map_fullscreen(shsurf);
4516 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004517 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004518 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004519 panel_height = get_output_panel_height(shell, shsurf->output);
4520 surface_subsurfaces_boundingbox(shsurf->surface,
4521 &surf_x, &surf_y, NULL, NULL);
4522 weston_view_set_position(shsurf->view,
4523 shsurf->output->x - surf_x,
4524 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004525 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004526 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004527 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004528 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004529 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004530 weston_view_set_position(shsurf->view,
4531 shsurf->view->geometry.x + sx,
4532 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004533 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004534 case SHELL_SURFACE_TRANSIENT:
4535 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004536 default:
4537 ;
4538 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004539
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004540 /* Surface stacking order, see also activate(). */
4541 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004542
Jason Ekstranda7af7042013-10-12 22:38:11 -05004543 if (shsurf->type != SHELL_SURFACE_NONE) {
4544 weston_view_update_transform(shsurf->view);
4545 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4546 shsurf->surface->output = shsurf->output;
4547 shsurf->view->output = shsurf->output;
4548 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004549 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004550
Jason Ekstranda7af7042013-10-12 22:38:11 -05004551 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004552 /* XXX: xwayland's using the same fields for transient type */
4553 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004554 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004555 if (shsurf->transient.flags ==
4556 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4557 break;
4558 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004559 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004560 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004561 if (!shell->locked) {
4562 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004563 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004564 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004565 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004566 case SHELL_SURFACE_POPUP:
4567 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004568 default:
4569 break;
4570 }
4571
Jason Ekstranda7af7042013-10-12 22:38:11 -05004572 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
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,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004590 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004591{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004592 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4593 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004594 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004595 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004596
Pekka Paalanen77346a62011-11-30 16:26:35 +02004597 shsurf = get_shell_surface(surface);
4598 if (shsurf)
4599 surface_type = shsurf->type;
4600
Jason Ekstranda7af7042013-10-12 22:38:11 -05004601 /* TODO:
4602 * This should probably be changed to be more shell_surface
4603 * dependent
4604 */
4605 wl_list_for_each(view, &surface->views, surface_link)
4606 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004607
4608 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004609 case SHELL_SURFACE_FULLSCREEN:
4610 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004611 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004612 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004613 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004614 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4615 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004616 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4617 shsurf->view->geometry.y = shsurf->output->y +
4618 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004619 break;
Alex Wu4539b082012-03-01 12:57:46 +08004620 case SHELL_SURFACE_TOPLEVEL:
Philip Withnall0f640e22013-11-25 18:01:31 +00004621 case SHELL_SURFACE_TRANSIENT:
4622 case SHELL_SURFACE_POPUP:
4623 case SHELL_SURFACE_XWAYLAND:
4624 case SHELL_SURFACE_NONE:
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004625 default:
4626 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004627 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004628
Alex Wu4539b082012-03-01 12:57:46 +08004629 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004630 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004631 wl_list_for_each(view, &surface->views, surface_link)
4632 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004633
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004634 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004635 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004636 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004637}
4638
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004639static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004640shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004641{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004642 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004643 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004644
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004645 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004646
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004647 if (!weston_surface_is_mapped(es) &&
4648 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004649 remove_popup_grab(shsurf);
4650 }
4651
Giulio Camuffo184df502013-02-21 11:29:21 +01004652 if (width == 0)
4653 return;
4654
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004655 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004656 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004657 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004658 type_changed = 1;
4659 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004660
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004661 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004662 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004663 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004664 shsurf->view->geometry.width != width ||
4665 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004666 float from_x, from_y;
4667 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004668
Jason Ekstranda7af7042013-10-12 22:38:11 -05004669 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4670 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004671 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004672 shsurf->view->geometry.x + to_x - from_x,
4673 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004674 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004675 }
4676}
4677
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004678static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004679
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004680static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004681desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004682{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004683 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004684 struct desktop_shell *shell =
4685 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004686
4687 shell->child.process.pid = 0;
4688 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004689
4690 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4691 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004692 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004693 shell->child.deathstamp = time;
4694 shell->child.deathcount = 0;
4695 }
4696
4697 shell->child.deathcount++;
4698 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004699 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004700 return;
4701 }
4702
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004703 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004704 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004705 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004706}
4707
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004708static void
4709launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004710{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004711 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004712
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004713 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004714 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004715 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004716 desktop_shell_sigchld);
4717
4718 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004719 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004720}
4721
4722static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004723bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4724{
Tiago Vignattibe143262012-04-16 17:31:41 +03004725 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004726 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004727
Jason Ekstranda85118c2013-06-27 20:17:02 -05004728 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4729 if (resource)
4730 wl_resource_set_implementation(resource, &shell_implementation,
4731 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004732}
4733
Kristian Høgsberg75840622011-09-06 13:48:16 -04004734static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004735unbind_desktop_shell(struct wl_resource *resource)
4736{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004737 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004738
4739 if (shell->locked)
4740 resume_desktop(shell);
4741
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004742 shell->child.desktop_shell = NULL;
4743 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004744}
4745
4746static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004747bind_desktop_shell(struct wl_client *client,
4748 void *data, uint32_t version, uint32_t id)
4749{
Tiago Vignattibe143262012-04-16 17:31:41 +03004750 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004751 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004752
Jason Ekstranda85118c2013-06-27 20:17:02 -05004753 resource = wl_resource_create(client, &desktop_shell_interface,
4754 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004755
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004756 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004757 wl_resource_set_implementation(resource,
4758 &desktop_shell_implementation,
4759 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004760 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004761
4762 if (version < 2)
4763 shell_fade_startup(shell);
4764
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004765 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004766 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004767
4768 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4769 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004770 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004771}
4772
Pekka Paalanen6e168112011-11-24 11:34:05 +02004773static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004774screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004775{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004776 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004777 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004778
Giulio Camuffo184df502013-02-21 11:29:21 +01004779 if (width == 0)
4780 return;
4781
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004782 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004783 if (!shell->locked)
4784 return;
4785
Jason Ekstranda7af7042013-10-12 22:38:11 -05004786 view = container_of(surface->views.next, struct weston_view, surface_link);
4787 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004788
Jason Ekstranda7af7042013-10-12 22:38:11 -05004789 if (wl_list_empty(&view->layer_link)) {
4790 wl_list_insert(shell->lock_layer.view_list.prev,
4791 &view->layer_link);
4792 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004793 wl_event_source_timer_update(shell->screensaver.timer,
4794 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004795 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004796 }
4797}
4798
4799static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004800screensaver_set_surface(struct wl_client *client,
4801 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004802 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004803 struct wl_resource *output_resource)
4804{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004805 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004806 struct weston_surface *surface =
4807 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004808 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004809 struct weston_view *view, *next;
4810
4811 /* Make sure we only have one view */
4812 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4813 weston_view_destroy(view);
4814 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004815
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004816 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004817 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004818 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004819}
4820
4821static const struct screensaver_interface screensaver_implementation = {
4822 screensaver_set_surface
4823};
4824
4825static void
4826unbind_screensaver(struct wl_resource *resource)
4827{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004828 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004829
Pekka Paalanen77346a62011-11-30 16:26:35 +02004830 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004831}
4832
4833static void
4834bind_screensaver(struct wl_client *client,
4835 void *data, uint32_t version, uint32_t id)
4836{
Tiago Vignattibe143262012-04-16 17:31:41 +03004837 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004838 struct wl_resource *resource;
4839
Jason Ekstranda85118c2013-06-27 20:17:02 -05004840 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004841
Pekka Paalanen77346a62011-11-30 16:26:35 +02004842 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004843 wl_resource_set_implementation(resource,
4844 &screensaver_implementation,
4845 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004846 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004847 return;
4848 }
4849
4850 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4851 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004852 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004853}
4854
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004855static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004856input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004857{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004858 struct input_panel_surface *ip_surface = surface->configure_private;
4859 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004860 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004861 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004862
Giulio Camuffo184df502013-02-21 11:29:21 +01004863 if (width == 0)
4864 return;
4865
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004866 if (!weston_surface_is_mapped(surface)) {
4867 if (!shell->showing_input_panels)
4868 return;
4869
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004870 show_surface = 1;
4871 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004872
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004873 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004874
4875 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004876 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4877 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004878 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004879 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4880 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004881 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004882
Jason Ekstranda7af7042013-10-12 22:38:11 -05004883 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004884
4885 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004886 wl_list_insert(&shell->input_panel_layer.view_list,
4887 &ip_surface->view->layer_link);
4888 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004889 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004890 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004891 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004892}
4893
4894static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004895destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004896{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004897 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4898
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004899 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004900 wl_list_remove(&input_panel_surface->link);
4901
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004902 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004903 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004904
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004905 free(input_panel_surface);
4906}
4907
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004908static struct input_panel_surface *
4909get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004910{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004911 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004912 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004913 } else {
4914 return NULL;
4915 }
4916}
4917
4918static void
4919input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4920{
4921 struct input_panel_surface *ipsurface = container_of(listener,
4922 struct input_panel_surface,
4923 surface_destroy_listener);
4924
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004925 if (ipsurface->resource) {
4926 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004927 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004928 destroy_input_panel_surface(ipsurface);
4929 }
4930}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004931
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004932static struct input_panel_surface *
4933create_input_panel_surface(struct desktop_shell *shell,
4934 struct weston_surface *surface)
4935{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004936 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004937
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004938 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4939 if (!input_panel_surface)
4940 return NULL;
4941
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004942 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004943 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004944
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004945 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004946
4947 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004948 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004949
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004950 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004951 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004952 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004953 &input_panel_surface->surface_destroy_listener);
4954
4955 wl_list_init(&input_panel_surface->link);
4956
4957 return input_panel_surface;
4958}
4959
4960static void
4961input_panel_surface_set_toplevel(struct wl_client *client,
4962 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004963 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004964 uint32_t position)
4965{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004966 struct input_panel_surface *input_panel_surface =
4967 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004968 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004969
4970 wl_list_insert(&shell->input_panel.surfaces,
4971 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004972
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004973 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004974 input_panel_surface->panel = 0;
4975}
4976
4977static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004978input_panel_surface_set_overlay_panel(struct wl_client *client,
4979 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004980{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004981 struct input_panel_surface *input_panel_surface =
4982 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004983 struct desktop_shell *shell = input_panel_surface->shell;
4984
4985 wl_list_insert(&shell->input_panel.surfaces,
4986 &input_panel_surface->link);
4987
4988 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004989}
4990
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004991static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004992 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004993 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004994};
4995
4996static void
4997destroy_input_panel_surface_resource(struct wl_resource *resource)
4998{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004999 struct input_panel_surface *ipsurf =
5000 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005001
5002 destroy_input_panel_surface(ipsurf);
5003}
5004
5005static void
5006input_panel_get_input_panel_surface(struct wl_client *client,
5007 struct wl_resource *resource,
5008 uint32_t id,
5009 struct wl_resource *surface_resource)
5010{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005011 struct weston_surface *surface =
5012 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005013 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005014 struct input_panel_surface *ipsurf;
5015
5016 if (get_input_panel_surface(surface)) {
5017 wl_resource_post_error(surface_resource,
5018 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005019 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005020 return;
5021 }
5022
5023 ipsurf = create_input_panel_surface(shell, surface);
5024 if (!ipsurf) {
5025 wl_resource_post_error(surface_resource,
5026 WL_DISPLAY_ERROR_INVALID_OBJECT,
5027 "surface->configure already set");
5028 return;
5029 }
5030
Jason Ekstranda85118c2013-06-27 20:17:02 -05005031 ipsurf->resource =
5032 wl_resource_create(client,
5033 &wl_input_panel_surface_interface, 1, id);
5034 wl_resource_set_implementation(ipsurf->resource,
5035 &input_panel_surface_implementation,
5036 ipsurf,
5037 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005038}
5039
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005040static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005041 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005042};
5043
5044static void
5045unbind_input_panel(struct wl_resource *resource)
5046{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005047 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005048
5049 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005050}
5051
5052static void
5053bind_input_panel(struct wl_client *client,
5054 void *data, uint32_t version, uint32_t id)
5055{
5056 struct desktop_shell *shell = data;
5057 struct wl_resource *resource;
5058
Jason Ekstranda85118c2013-06-27 20:17:02 -05005059 resource = wl_resource_create(client,
5060 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005061
5062 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005063 wl_resource_set_implementation(resource,
5064 &input_panel_implementation,
5065 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005066 shell->input_panel.binding = resource;
5067 return;
5068 }
5069
5070 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5071 "interface object already bound");
5072 wl_resource_destroy(resource);
5073}
5074
Kristian Høgsberg07045392012-02-19 18:52:44 -05005075struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005076 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005077 struct weston_surface *current;
5078 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005079 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005080};
5081
5082static void
5083switcher_next(struct switcher *switcher)
5084{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005085 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005086 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005087 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005088 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005089
Jason Ekstranda7af7042013-10-12 22:38:11 -05005090 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
5091 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005092 case SHELL_SURFACE_TOPLEVEL:
5093 case SHELL_SURFACE_FULLSCREEN:
5094 case SHELL_SURFACE_MAXIMIZED:
5095 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005096 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005097 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005098 next = view->surface;
5099 prev = view->surface;
5100 view->alpha = 0.25;
5101 weston_view_geometry_dirty(view);
5102 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005103 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005104 case SHELL_SURFACE_TRANSIENT:
5105 case SHELL_SURFACE_POPUP:
5106 case SHELL_SURFACE_XWAYLAND:
5107 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005108 default:
5109 break;
5110 }
Alex Wu1659daa2012-04-01 20:13:09 +08005111
Jason Ekstranda7af7042013-10-12 22:38:11 -05005112 if (is_black_surface(view->surface, NULL)) {
5113 view->alpha = 0.25;
5114 weston_view_geometry_dirty(view);
5115 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005116 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005117 }
5118
5119 if (next == NULL)
5120 next = first;
5121
Alex Wu07b26062012-03-12 16:06:01 +08005122 if (next == NULL)
5123 return;
5124
Kristian Høgsberg07045392012-02-19 18:52:44 -05005125 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005126 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005127
5128 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005129 wl_list_for_each(view, &next->views, surface_link)
5130 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005131
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005132 shsurf = get_shell_surface(switcher->current);
5133 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005134 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005135}
5136
5137static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005138switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005139{
5140 struct switcher *switcher =
5141 container_of(listener, struct switcher, listener);
5142
5143 switcher_next(switcher);
5144}
5145
5146static void
Daniel Stone351eb612012-05-31 15:27:47 -04005147switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005148{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005149 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005150 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005151 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005152
Jason Ekstranda7af7042013-10-12 22:38:11 -05005153 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005154 if (is_focus_view(view))
5155 continue;
5156
Jason Ekstranda7af7042013-10-12 22:38:11 -05005157 view->alpha = 1.0;
5158 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005159 }
5160
Alex Wu07b26062012-03-12 16:06:01 +08005161 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005162 activate(switcher->shell, switcher->current,
5163 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005164 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005165 weston_keyboard_end_grab(keyboard);
5166 if (keyboard->input_method_resource)
5167 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005168 free(switcher);
5169}
5170
5171static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005172switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005173 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005174{
5175 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005176 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005177
Daniel Stonec9785ea2012-05-30 16:31:52 +01005178 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005179 switcher_next(switcher);
5180}
5181
5182static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005183switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005184 uint32_t mods_depressed, uint32_t mods_latched,
5185 uint32_t mods_locked, uint32_t group)
5186{
5187 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005188 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005189
Daniel Stone351eb612012-05-31 15:27:47 -04005190 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5191 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005192}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005193
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005194static void
5195switcher_cancel(struct weston_keyboard_grab *grab)
5196{
5197 struct switcher *switcher = container_of(grab, struct switcher, grab);
5198
5199 switcher_destroy(switcher);
5200}
5201
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005202static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005203 switcher_key,
5204 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005205 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005206};
5207
5208static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005209switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005210 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005211{
Tiago Vignattibe143262012-04-16 17:31:41 +03005212 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005213 struct switcher *switcher;
5214
5215 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005216 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005217 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005218 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005219 wl_list_init(&switcher->listener.link);
5220
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005221 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005222 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005223 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005224 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5225 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005226 switcher_next(switcher);
5227}
5228
Daniel Stonedf8133b2013-11-19 11:37:14 +01005229struct exposay_surface {
5230 struct desktop_shell *shell;
5231 struct weston_surface *surface;
5232 struct weston_view *view;
5233 struct wl_list link;
5234
5235 int x;
5236 int y;
5237 int width;
5238 int height;
5239 double scale;
5240
5241 int row;
5242 int column;
5243
5244 /* The animations only apply a transformation for their own lifetime,
5245 * and don't have an option to indefinitely maintain the
5246 * transformation in a steady state - so, we apply our own once the
5247 * animation has finished. */
5248 struct weston_transform transform;
5249};
5250
5251static void exposay_set_state(struct desktop_shell *shell,
5252 enum exposay_target_state state,
5253 struct weston_seat *seat);
5254static void exposay_check_state(struct desktop_shell *shell);
5255
5256static void
5257exposay_in_flight_inc(struct desktop_shell *shell)
5258{
5259 shell->exposay.in_flight++;
5260}
5261
5262static void
5263exposay_in_flight_dec(struct desktop_shell *shell)
5264{
5265 if (--shell->exposay.in_flight > 0)
5266 return;
5267
5268 exposay_check_state(shell);
5269}
5270
5271static void
5272exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5273{
5274 struct exposay_surface *esurface = data;
5275
5276 wl_list_insert(&esurface->view->geometry.transformation_list,
5277 &esurface->transform.link);
5278 weston_matrix_init(&esurface->transform.matrix);
5279 weston_matrix_scale(&esurface->transform.matrix,
5280 esurface->scale, esurface->scale, 1.0f);
5281 weston_matrix_translate(&esurface->transform.matrix,
5282 esurface->x - esurface->view->geometry.x,
5283 esurface->y - esurface->view->geometry.y,
5284 0);
5285
5286 weston_view_geometry_dirty(esurface->view);
5287 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5288
5289 exposay_in_flight_dec(esurface->shell);
5290}
5291
5292static void
5293exposay_animate_in(struct exposay_surface *esurface)
5294{
5295 exposay_in_flight_inc(esurface->shell);
5296
5297 weston_move_scale_run(esurface->view,
5298 esurface->x - esurface->view->geometry.x,
5299 esurface->y - esurface->view->geometry.y,
5300 1.0, esurface->scale, 0,
5301 exposay_animate_in_done, esurface);
5302}
5303
5304static void
5305exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5306{
5307 struct exposay_surface *esurface = data;
5308 struct desktop_shell *shell = esurface->shell;
5309
5310 wl_list_remove(&esurface->link);
5311 free(esurface);
5312
5313 exposay_in_flight_dec(shell);
5314}
5315
5316static void
5317exposay_animate_out(struct exposay_surface *esurface)
5318{
5319 exposay_in_flight_inc(esurface->shell);
5320
5321 /* Remove the static transformation set up by
5322 * exposay_transform_in_done(). */
5323 wl_list_remove(&esurface->transform.link);
5324 weston_view_geometry_dirty(esurface->view);
5325
5326 weston_move_scale_run(esurface->view,
5327 esurface->x - esurface->view->geometry.x,
5328 esurface->y - esurface->view->geometry.y,
5329 1.0, esurface->scale, 1,
5330 exposay_animate_out_done, esurface);
5331}
5332
5333static void
5334exposay_highlight_surface(struct desktop_shell *shell,
5335 struct exposay_surface *esurface)
5336{
5337 struct weston_view *view = NULL;
5338
5339 if (esurface) {
5340 shell->exposay.row_current = esurface->row;
5341 shell->exposay.column_current = esurface->column;
5342 view = esurface->view;
5343 }
5344
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005345 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005346 shell->exposay.focus_current = view;
5347}
5348
5349static int
5350exposay_is_animating(struct desktop_shell *shell)
5351{
5352 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5353 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5354 return 0;
5355
5356 return (shell->exposay.in_flight > 0);
5357}
5358
5359static void
5360exposay_pick(struct desktop_shell *shell, int x, int y)
5361{
5362 struct exposay_surface *esurface;
5363
5364 if (exposay_is_animating(shell))
5365 return;
5366
5367 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5368 if (x < esurface->x || x > esurface->x + esurface->width)
5369 continue;
5370 if (y < esurface->y || y > esurface->y + esurface->height)
5371 continue;
5372
5373 exposay_highlight_surface(shell, esurface);
5374 return;
5375 }
5376}
5377
5378/* Pretty lame layout for now; just tries to make a square. Should take
5379 * aspect ratio into account really. Also needs to be notified of surface
5380 * addition and removal and adjust layout/animate accordingly. */
5381static enum exposay_layout_state
5382exposay_layout(struct desktop_shell *shell)
5383{
5384 struct workspace *workspace = shell->exposay.workspace;
5385 struct weston_compositor *compositor = shell->compositor;
5386 struct weston_output *output = get_default_output(compositor);
5387 struct weston_view *view;
5388 struct exposay_surface *esurface;
5389 int w, h;
5390 int i;
5391 int last_row_removed = 0;
5392
5393 wl_list_init(&shell->exposay.surface_list);
5394
5395 shell->exposay.num_surfaces = 0;
5396 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5397 if (!get_shell_surface(view->surface))
5398 continue;
5399 shell->exposay.num_surfaces++;
5400 }
5401
5402 if (shell->exposay.num_surfaces == 0) {
5403 shell->exposay.grid_size = 0;
5404 shell->exposay.hpadding_outer = 0;
5405 shell->exposay.vpadding_outer = 0;
5406 shell->exposay.padding_inner = 0;
5407 shell->exposay.surface_size = 0;
5408 return EXPOSAY_LAYOUT_OVERVIEW;
5409 }
5410
5411 /* Lay the grid out as square as possible, losing surfaces from the
5412 * bottom row if required. Start with fixed padding of a 10% margin
5413 * around the outside and 80px internal padding between surfaces, and
5414 * maximise the area made available to surfaces after this, but only
5415 * to a maximum of 1/3rd the total output size.
5416 *
5417 * If we can't make a square grid, add one extra row at the bottom
5418 * which will have a smaller number of columns.
5419 *
5420 * XXX: Surely there has to be a better way to express this maths,
5421 * right?!
5422 */
5423 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5424 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5425 shell->exposay.grid_size++;
5426 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5427
5428 shell->exposay.hpadding_outer = (output->width / 10);
5429 shell->exposay.vpadding_outer = (output->height / 10);
5430 shell->exposay.padding_inner = 80;
5431
5432 w = output->width - (shell->exposay.hpadding_outer * 2);
5433 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5434 w /= shell->exposay.grid_size;
5435
5436 h = output->height - (shell->exposay.vpadding_outer * 2);
5437 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5438 h /= shell->exposay.grid_size;
5439
5440 shell->exposay.surface_size = (w < h) ? w : h;
5441 if (shell->exposay.surface_size > (output->width / 2))
5442 shell->exposay.surface_size = output->width / 2;
5443 if (shell->exposay.surface_size > (output->height / 2))
5444 shell->exposay.surface_size = output->height / 2;
5445
5446 i = 0;
5447 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5448 int pad;
5449
5450 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5451
5452 if (!get_shell_surface(view->surface))
5453 continue;
5454
5455 esurface = malloc(sizeof(*esurface));
5456 if (!esurface) {
5457 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5458 shell->exposay.seat);
5459 break;
5460 }
5461
5462 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5463 esurface->shell = shell;
5464 esurface->view = view;
5465
5466 esurface->row = i / shell->exposay.grid_size;
5467 esurface->column = i % shell->exposay.grid_size;
5468
5469 esurface->x = shell->exposay.hpadding_outer;
5470 esurface->x += pad * esurface->column;
5471 esurface->y = shell->exposay.vpadding_outer;
5472 esurface->y += pad * esurface->row;
5473
5474 if (esurface->row == shell->exposay.grid_size - 1)
5475 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5476
5477 if (view->geometry.width > view->geometry.height)
5478 esurface->scale = shell->exposay.surface_size / (float) view->geometry.width;
5479 else
5480 esurface->scale = shell->exposay.surface_size / (float) view->geometry.height;
5481 esurface->width = view->geometry.width * esurface->scale;
5482 esurface->height = view->geometry.height * esurface->scale;
5483
5484 if (shell->exposay.focus_current == esurface->view)
5485 exposay_highlight_surface(shell, esurface);
5486
5487 exposay_animate_in(esurface);
5488
5489 i++;
5490 }
5491
5492 weston_compositor_schedule_repaint(shell->compositor);
5493
5494 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5495}
5496
5497static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005498exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5499 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005500{
5501 struct desktop_shell *shell =
5502 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5503
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005504 weston_pointer_move(grab->pointer, x, y);
5505
Daniel Stonedf8133b2013-11-19 11:37:14 +01005506 exposay_pick(shell,
5507 wl_fixed_to_int(grab->pointer->x),
5508 wl_fixed_to_int(grab->pointer->y));
5509}
5510
5511static void
5512exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5513 uint32_t state_w)
5514{
5515 struct desktop_shell *shell =
5516 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5517 struct weston_seat *seat = grab->pointer->seat;
5518 enum wl_pointer_button_state state = state_w;
5519
5520 if (button != BTN_LEFT)
5521 return;
5522
5523 /* Store the surface we clicked on, and don't do anything if we end up
5524 * releasing on a different surface. */
5525 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5526 shell->exposay.clicked = shell->exposay.focus_current;
5527 return;
5528 }
5529
5530 if (shell->exposay.focus_current == shell->exposay.clicked)
5531 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5532 else
5533 shell->exposay.clicked = NULL;
5534}
5535
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005536static void
5537exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
5538{
5539 struct desktop_shell *shell =
5540 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5541
5542 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5543}
5544
Daniel Stonedf8133b2013-11-19 11:37:14 +01005545static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5546 noop_grab_focus,
5547 exposay_motion,
5548 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005549 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005550};
5551
5552static int
5553exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5554{
5555 struct exposay_surface *esurface;
5556
5557 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5558 if (esurface->row != row || esurface->column != column)
5559 continue;
5560
5561 exposay_highlight_surface(shell, esurface);
5562 return 1;
5563 }
5564
5565 return 0;
5566}
5567
5568static void
5569exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5570 uint32_t state_w)
5571{
5572 struct weston_seat *seat = grab->keyboard->seat;
5573 struct desktop_shell *shell =
5574 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5575 enum wl_keyboard_key_state state = state_w;
5576
5577 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5578 return;
5579
5580 switch (key) {
5581 case KEY_ESC:
5582 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5583 break;
5584 case KEY_ENTER:
5585 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5586 break;
5587 case KEY_UP:
5588 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5589 shell->exposay.column_current);
5590 break;
5591 case KEY_DOWN:
5592 /* Special case for trying to move to the bottom row when it
5593 * has fewer items than all the others. */
5594 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5595 shell->exposay.column_current) &&
5596 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5597 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5598 (shell->exposay.num_surfaces %
5599 shell->exposay.grid_size) - 1);
5600 }
5601 break;
5602 case KEY_LEFT:
5603 exposay_maybe_move(shell, shell->exposay.row_current,
5604 shell->exposay.column_current - 1);
5605 break;
5606 case KEY_RIGHT:
5607 exposay_maybe_move(shell, shell->exposay.row_current,
5608 shell->exposay.column_current + 1);
5609 break;
5610 case KEY_TAB:
5611 /* Try to move right, then down (and to the leftmost column),
5612 * then if all else fails, to the top left. */
5613 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5614 shell->exposay.column_current + 1) &&
5615 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5616 exposay_maybe_move(shell, 0, 0);
5617 break;
5618 default:
5619 break;
5620 }
5621}
5622
5623static void
5624exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5625 uint32_t mods_depressed, uint32_t mods_latched,
5626 uint32_t mods_locked, uint32_t group)
5627{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005628 struct desktop_shell *shell =
5629 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5630 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5631
5632 /* We want to know when mod has been pressed and released.
5633 * FIXME: There is a problem here: if mod is pressed, then a key
5634 * is pressed and released, then mod is released, we will treat that
5635 * as if only mod had been pressed and released. */
5636 if (seat->modifier_state) {
5637 if (seat->modifier_state == shell->binding_modifier) {
5638 shell->exposay.mod_pressed = true;
5639 } else {
5640 shell->exposay.mod_invalid = true;
5641 }
5642 } else {
5643 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
5644 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5645
5646 shell->exposay.mod_invalid = false;
5647 shell->exposay.mod_pressed = false;
5648 }
5649
5650 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005651}
5652
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005653static void
5654exposay_cancel(struct weston_keyboard_grab *grab)
5655{
5656 struct desktop_shell *shell =
5657 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5658
5659 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5660}
5661
Daniel Stonedf8133b2013-11-19 11:37:14 +01005662static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5663 exposay_key,
5664 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005665 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005666};
5667
5668/**
5669 * Called when the transition from overview -> inactive has completed.
5670 */
5671static enum exposay_layout_state
5672exposay_set_inactive(struct desktop_shell *shell)
5673{
5674 struct weston_seat *seat = shell->exposay.seat;
5675
5676 weston_keyboard_end_grab(seat->keyboard);
5677 weston_pointer_end_grab(seat->pointer);
5678 if (seat->keyboard->input_method_resource)
5679 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5680
5681 return EXPOSAY_LAYOUT_INACTIVE;
5682}
5683
5684/**
5685 * Begins the transition from overview to inactive. */
5686static enum exposay_layout_state
5687exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5688{
5689 struct exposay_surface *esurface;
5690
5691 /* Call activate() before we start the animations to avoid
5692 * animating back the old state and then immediately transitioning
5693 * to the new. */
5694 if (switch_focus && shell->exposay.focus_current)
5695 activate(shell, shell->exposay.focus_current->surface,
5696 shell->exposay.seat);
5697 else if (shell->exposay.focus_prev)
5698 activate(shell, shell->exposay.focus_prev->surface,
5699 shell->exposay.seat);
5700
5701 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5702 exposay_animate_out(esurface);
5703 weston_compositor_schedule_repaint(shell->compositor);
5704
5705 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5706}
5707
5708static enum exposay_layout_state
5709exposay_transition_active(struct desktop_shell *shell)
5710{
5711 struct weston_seat *seat = shell->exposay.seat;
5712
5713 shell->exposay.workspace = get_current_workspace(shell);
5714 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5715 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5716 shell->exposay.clicked = NULL;
5717 wl_list_init(&shell->exposay.surface_list);
5718
5719 lower_fullscreen_layer(shell);
5720 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5721 weston_keyboard_start_grab(seat->keyboard,
5722 &shell->exposay.grab_kbd);
5723 weston_keyboard_set_focus(seat->keyboard, NULL);
5724
5725 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5726 weston_pointer_start_grab(seat->pointer,
5727 &shell->exposay.grab_ptr);
5728 weston_pointer_set_focus(seat->pointer, NULL,
5729 seat->pointer->x, seat->pointer->y);
5730
5731 return exposay_layout(shell);
5732}
5733
5734static void
5735exposay_check_state(struct desktop_shell *shell)
5736{
5737 enum exposay_layout_state state_new = shell->exposay.state_cur;
5738 int do_switch = 0;
5739
5740 /* Don't do anything whilst animations are running, just store up
5741 * target state changes and only act on them when the animations have
5742 * completed. */
5743 if (exposay_is_animating(shell))
5744 return;
5745
5746 switch (shell->exposay.state_target) {
5747 case EXPOSAY_TARGET_OVERVIEW:
5748 switch (shell->exposay.state_cur) {
5749 case EXPOSAY_LAYOUT_OVERVIEW:
5750 goto out;
5751 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5752 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5753 break;
5754 default:
5755 state_new = exposay_transition_active(shell);
5756 break;
5757 }
5758 break;
5759
5760 case EXPOSAY_TARGET_SWITCH:
5761 do_switch = 1; /* fallthrough */
5762 case EXPOSAY_TARGET_CANCEL:
5763 switch (shell->exposay.state_cur) {
5764 case EXPOSAY_LAYOUT_INACTIVE:
5765 goto out;
5766 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5767 state_new = exposay_set_inactive(shell);
5768 break;
5769 default:
5770 state_new = exposay_transition_inactive(shell, do_switch);
5771 break;
5772 }
5773
5774 break;
5775 }
5776
5777out:
5778 shell->exposay.state_cur = state_new;
5779}
5780
5781static void
5782exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5783 struct weston_seat *seat)
5784{
5785 shell->exposay.state_target = state;
5786 shell->exposay.seat = seat;
5787 exposay_check_state(shell);
5788}
5789
5790static void
5791exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5792 void *data)
5793{
5794 struct desktop_shell *shell = data;
5795
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005796 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005797}
5798
Pekka Paalanen3c647232011-12-22 13:43:43 +02005799static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005800backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005801 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005802{
5803 struct weston_compositor *compositor = data;
5804 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005805 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005806
5807 /* TODO: we're limiting to simple use cases, where we assume just
5808 * control on the primary display. We'd have to extend later if we
5809 * ever get support for setting backlights on random desktop LCD
5810 * panels though */
5811 output = get_default_output(compositor);
5812 if (!output)
5813 return;
5814
5815 if (!output->set_backlight)
5816 return;
5817
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005818 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5819 backlight_new = output->backlight_current - 25;
5820 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5821 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005822
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005823 if (backlight_new < 5)
5824 backlight_new = 5;
5825 if (backlight_new > 255)
5826 backlight_new = 255;
5827
5828 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005829 output->set_backlight(output, output->backlight_current);
5830}
5831
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005832struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005833 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005834 struct weston_seat *seat;
5835 uint32_t key[2];
5836 int key_released[2];
5837};
5838
5839static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005840debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005841 uint32_t key, uint32_t state)
5842{
5843 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005844 struct weston_compositor *ec = db->seat->compositor;
5845 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005846 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005847 uint32_t serial;
5848 int send = 0, terminate = 0;
5849 int check_binding = 1;
5850 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005851 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005852
5853 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5854 /* Do not run bindings on key releases */
5855 check_binding = 0;
5856
5857 for (i = 0; i < 2; i++)
5858 if (key == db->key[i])
5859 db->key_released[i] = 1;
5860
5861 if (db->key_released[0] && db->key_released[1]) {
5862 /* All key releases been swalled so end the grab */
5863 terminate = 1;
5864 } else if (key != db->key[0] && key != db->key[1]) {
5865 /* Should not swallow release of other keys */
5866 send = 1;
5867 }
5868 } else if (key == db->key[0] && !db->key_released[0]) {
5869 /* Do not check bindings for the first press of the binding
5870 * key. This allows it to be used as a debug shortcut.
5871 * We still need to swallow this event. */
5872 check_binding = 0;
5873 } else if (db->key[1]) {
5874 /* If we already ran a binding don't process another one since
5875 * we can't keep track of all the binding keys that were
5876 * pressed in order to swallow the release events. */
5877 send = 1;
5878 check_binding = 0;
5879 }
5880
5881 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005882 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5883 key, state)) {
5884 /* We ran a binding so swallow the press and keep the
5885 * grab to swallow the released too. */
5886 send = 0;
5887 terminate = 0;
5888 db->key[1] = key;
5889 } else {
5890 /* Terminate the grab since the key pressed is not a
5891 * debug binding key. */
5892 send = 1;
5893 terminate = 1;
5894 }
5895 }
5896
5897 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005898 serial = wl_display_next_serial(display);
5899 resource_list = &grab->keyboard->focus_resource_list;
5900 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005901 wl_keyboard_send_key(resource, serial, time, key, state);
5902 }
5903 }
5904
5905 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005906 weston_keyboard_end_grab(grab->keyboard);
5907 if (grab->keyboard->input_method_resource)
5908 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005909 free(db);
5910 }
5911}
5912
5913static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005914debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005915 uint32_t mods_depressed, uint32_t mods_latched,
5916 uint32_t mods_locked, uint32_t group)
5917{
5918 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005919 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005920
Neil Roberts96d790e2013-09-19 17:32:00 +01005921 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005922
Neil Roberts96d790e2013-09-19 17:32:00 +01005923 wl_resource_for_each(resource, resource_list) {
5924 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5925 mods_latched, mods_locked, group);
5926 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005927}
5928
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005929static void
5930debug_binding_cancel(struct weston_keyboard_grab *grab)
5931{
5932 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5933
5934 weston_keyboard_end_grab(grab->keyboard);
5935 free(db);
5936}
5937
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005938struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005939 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005940 debug_binding_modifiers,
5941 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005942};
5943
5944static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005945debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005946{
5947 struct debug_binding_grab *grab;
5948
5949 grab = calloc(1, sizeof *grab);
5950 if (!grab)
5951 return;
5952
5953 grab->seat = (struct weston_seat *) seat;
5954 grab->key[0] = key;
5955 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005956 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005957}
5958
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005959static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005960force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005961 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005962{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005963 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005964 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005965 struct desktop_shell *shell = data;
5966 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005967 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005968
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005969 focus_surface = seat->keyboard->focus;
5970 if (!focus_surface)
5971 return;
5972
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005973 wl_signal_emit(&compositor->kill_signal, focus_surface);
5974
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005975 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005976 wl_client_get_credentials(client, &pid, NULL, NULL);
5977
5978 /* Skip clients that we launched ourselves (the credentials of
5979 * the socketpair is ours) */
5980 if (pid == getpid())
5981 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005982
Daniel Stone325fc2d2012-05-30 16:31:58 +01005983 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005984}
5985
5986static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005987workspace_up_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 != 0)
5996 new_index--;
5997
5998 change_workspace(shell, new_index);
5999}
6000
6001static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006002workspace_down_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 = shell->workspaces.current;
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 if (new_index < shell->workspaces.num - 1)
6011 new_index++;
6012
6013 change_workspace(shell, new_index);
6014}
6015
6016static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006017workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006018 uint32_t key, void *data)
6019{
6020 struct desktop_shell *shell = data;
6021 unsigned int new_index;
6022
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006023 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006024 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006025 new_index = key - KEY_F1;
6026 if (new_index >= shell->workspaces.num)
6027 new_index = shell->workspaces.num - 1;
6028
6029 change_workspace(shell, new_index);
6030}
6031
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006032static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006033workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006034 uint32_t key, void *data)
6035{
6036 struct desktop_shell *shell = data;
6037 unsigned int new_index = shell->workspaces.current;
6038
6039 if (shell->locked)
6040 return;
6041
6042 if (new_index != 0)
6043 new_index--;
6044
6045 take_surface_to_workspace_by_seat(shell, seat, new_index);
6046}
6047
6048static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006049workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006050 uint32_t key, void *data)
6051{
6052 struct desktop_shell *shell = data;
6053 unsigned int new_index = shell->workspaces.current;
6054
6055 if (shell->locked)
6056 return;
6057
6058 if (new_index < shell->workspaces.num - 1)
6059 new_index++;
6060
6061 take_surface_to_workspace_by_seat(shell, seat, new_index);
6062}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006063
6064static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006065handle_output_destroy(struct wl_listener *listener, void *data)
6066{
6067 struct shell_output *output_listener =
6068 container_of(listener, struct shell_output, destroy_listener);
6069
6070 wl_list_remove(&output_listener->destroy_listener.link);
6071 wl_list_remove(&output_listener->link);
6072 free(output_listener);
6073}
6074
6075static void
6076create_shell_output(struct desktop_shell *shell,
6077 struct weston_output *output)
6078{
6079 struct shell_output *shell_output;
6080
6081 shell_output = zalloc(sizeof *shell_output);
6082 if (shell_output == NULL)
6083 return;
6084
6085 shell_output->output = output;
6086 shell_output->shell = shell;
6087 shell_output->destroy_listener.notify = handle_output_destroy;
6088 wl_signal_add(&output->destroy_signal,
6089 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006090 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006091}
6092
6093static void
6094handle_output_create(struct wl_listener *listener, void *data)
6095{
6096 struct desktop_shell *shell =
6097 container_of(listener, struct desktop_shell, output_create_listener);
6098 struct weston_output *output = (struct weston_output *)data;
6099
6100 create_shell_output(shell, output);
6101}
6102
6103static void
6104setup_output_destroy_handler(struct weston_compositor *ec,
6105 struct desktop_shell *shell)
6106{
6107 struct weston_output *output;
6108
6109 wl_list_init(&shell->output_list);
6110 wl_list_for_each(output, &ec->output_list, link)
6111 create_shell_output(shell, output);
6112
6113 shell->output_create_listener.notify = handle_output_create;
6114 wl_signal_add(&ec->output_created_signal,
6115 &shell->output_create_listener);
6116}
6117
6118static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006119shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006120{
Tiago Vignattibe143262012-04-16 17:31:41 +03006121 struct desktop_shell *shell =
6122 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006123 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006124 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006125
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006126 if (shell->child.client)
6127 wl_client_destroy(shell->child.client);
6128
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006129 wl_list_remove(&shell->idle_listener.link);
6130 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006131 wl_list_remove(&shell->show_input_panel_listener.link);
6132 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006133
Xiong Zhang6b481422013-10-23 13:58:32 +08006134 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6135 wl_list_remove(&shell_output->destroy_listener.link);
6136 wl_list_remove(&shell_output->link);
6137 free(shell_output);
6138 }
6139
6140 wl_list_remove(&shell->output_create_listener.link);
6141
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006142 wl_array_for_each(ws, &shell->workspaces.array)
6143 workspace_destroy(*ws);
6144 wl_array_release(&shell->workspaces.array);
6145
Pekka Paalanen3c647232011-12-22 13:43:43 +02006146 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006147 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006148 free(shell);
6149}
6150
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006151static void
6152shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6153{
6154 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006155 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006156
6157 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006158 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6159 MODIFIER_CTRL | MODIFIER_ALT,
6160 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01006161 weston_compositor_add_key_binding(ec, KEY_TAB,
6162 MODIFIER_ALT,
6163 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006164 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6165 click_to_activate_binding,
6166 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006167 weston_compositor_add_touch_binding(ec, 0,
6168 touch_to_activate_binding,
6169 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006170 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6171 MODIFIER_SUPER | MODIFIER_ALT,
6172 surface_opacity_binding, NULL);
6173 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6174 MODIFIER_SUPER, zoom_axis_binding,
6175 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006176
6177 /* configurable bindings */
6178 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006179 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6180 zoom_key_binding, NULL);
6181 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6182 zoom_key_binding, NULL);
6183 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6184 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006185 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006186 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6187 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006188
6189 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6190 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6191 rotate_binding, NULL);
6192
Daniel Stone325fc2d2012-05-30 16:31:58 +01006193 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6194 shell);
6195 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6196 ec);
6197 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6198 backlight_binding, ec);
6199 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6200 ec);
6201 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6202 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006203 weston_compositor_add_key_binding(ec, KEY_K, mod,
6204 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006205 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6206 workspace_up_binding, shell);
6207 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6208 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006209 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6210 workspace_move_surface_up_binding,
6211 shell);
6212 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6213 workspace_move_surface_down_binding,
6214 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006215
Daniel Stonedf8133b2013-11-19 11:37:14 +01006216 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6217
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006218 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6219 if (shell->workspaces.num > 1) {
6220 num_workspace_bindings = shell->workspaces.num;
6221 if (num_workspace_bindings > 6)
6222 num_workspace_bindings = 6;
6223 for (i = 0; i < num_workspace_bindings; i++)
6224 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6225 workspace_f_binding,
6226 shell);
6227 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006228
6229 /* Debug bindings */
6230 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6231 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006232}
6233
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006234WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006235module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006236 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006237{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006238 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006239 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006240 struct workspace **pws;
6241 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006242 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006243
Peter Huttererf3d62272013-08-08 11:57:05 +10006244 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006245 if (shell == NULL)
6246 return -1;
6247
Kristian Høgsberg75840622011-09-06 13:48:16 -04006248 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006249
6250 shell->destroy_listener.notify = shell_destroy;
6251 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006252 shell->idle_listener.notify = idle_handler;
6253 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6254 shell->wake_listener.notify = wake_handler;
6255 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006256 shell->show_input_panel_listener.notify = show_input_panels;
6257 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6258 shell->hide_input_panel_listener.notify = hide_input_panels;
6259 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006260 shell->update_input_panel_listener.notify = update_input_panels;
6261 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006262 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006263 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006264 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006265 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006266 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006267 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006268 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006269 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006270 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006271 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006272 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006273
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006274 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006275
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006276 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6277 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006278 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6279 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006280 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006281
6282 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006283 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006284
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006285 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006286
Daniel Stonedf8133b2013-11-19 11:37:14 +01006287 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6288 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6289
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006290 for (i = 0; i < shell->workspaces.num; i++) {
6291 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6292 if (pws == NULL)
6293 return -1;
6294
6295 *pws = workspace_create();
6296 if (*pws == NULL)
6297 return -1;
6298 }
6299 activate_workspace(shell, 0);
6300
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006301 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006302 wl_list_init(&shell->workspaces.animation.link);
6303 shell->workspaces.animation.frame = animate_workspace_change_frame;
6304
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006305 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006306 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006307 return -1;
6308
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006309 if (wl_global_create(ec->wl_display,
6310 &desktop_shell_interface, 2,
6311 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006312 return -1;
6313
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006314 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6315 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006316 return -1;
6317
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006318 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006319 shell, bind_input_panel) == NULL)
6320 return -1;
6321
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006322 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6323 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006324 return -1;
6325
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006326 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006327
Xiong Zhang6b481422013-10-23 13:58:32 +08006328 setup_output_destroy_handler(ec, shell);
6329
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006330 loop = wl_display_get_event_loop(ec->wl_display);
6331 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006332
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006333 shell->screensaver.timer =
6334 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6335
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006336 wl_list_for_each(seat, &ec->seat_list, link)
6337 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006338
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006339 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006340
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006341 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006342
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006343 return 0;
6344}