blob: f8b6e138cdbb6cafe496b23b000e67719e1761bc [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"
Rafael Antognollie2a34552013-12-03 15:35:45 -020043#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050044
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020046#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020047
Juan Zhaoe10d2792012-04-25 19:09:52 +080048enum animation_type {
49 ANIMATION_NONE,
50
51 ANIMATION_ZOOM,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010052 ANIMATION_FADE,
53 ANIMATION_DIM_LAYER,
Juan Zhaoe10d2792012-04-25 19:09:52 +080054};
55
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020056enum fade_type {
57 FADE_IN,
58 FADE_OUT
59};
60
Daniel Stonedf8133b2013-11-19 11:37:14 +010061enum exposay_target_state {
62 EXPOSAY_TARGET_OVERVIEW, /* show all windows */
63 EXPOSAY_TARGET_CANCEL, /* return to normal, same focus */
64 EXPOSAY_TARGET_SWITCH, /* return to normal, switch focus */
65};
66
67enum exposay_layout_state {
68 EXPOSAY_LAYOUT_INACTIVE = 0, /* normal desktop */
69 EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE, /* in transition to normal */
70 EXPOSAY_LAYOUT_OVERVIEW, /* show all windows */
71 EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW, /* in transition to all windows */
72};
73
Jonas Ådahl04769742012-06-13 00:01:24 +020074struct focus_state {
75 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040076 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020077 struct weston_surface *keyboard_focus;
78 struct wl_list link;
79 struct wl_listener seat_destroy_listener;
80 struct wl_listener surface_destroy_listener;
81};
82
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010083struct focus_surface {
84 struct weston_surface *surface;
85 struct weston_view *view;
86 struct weston_transform workspace_transform;
87};
88
Jonas Ådahle3cddce2012-06-13 00:01:22 +020089struct workspace {
90 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020091
92 struct wl_list focus_list;
93 struct wl_listener seat_destroyed_listener;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010094
95 struct focus_surface *fsurf_front;
96 struct focus_surface *fsurf_back;
97 struct weston_view_animation *focus_animation;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020098};
99
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200100struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -0500101 struct wl_resource *resource;
102 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100103
104 struct desktop_shell *shell;
105
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200106 struct wl_list link;
107 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500108 struct weston_view *view;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100109 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200110
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +0200111 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200112 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200113};
114
Xiong Zhang6b481422013-10-23 13:58:32 +0800115struct shell_output {
116 struct desktop_shell *shell;
117 struct weston_output *output;
118 struct wl_listener destroy_listener;
119 struct wl_list link;
120};
121
Tiago Vignattibe143262012-04-16 17:31:41 +0300122struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500123 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400124
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200125 struct wl_listener idle_listener;
126 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400127 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200128 struct wl_listener show_input_panel_listener;
129 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200130 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200131
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500132 struct weston_layer fullscreen_layer;
133 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500134 struct weston_layer background_layer;
135 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200136 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500137
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400138 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300139 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400140
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200141 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500142 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200143 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200144 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200145
146 unsigned deathcount;
147 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200148 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200149
150 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200151 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200152 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200153
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200154 struct {
155 struct weston_surface *surface;
156 pixman_box32_t cursor_rectangle;
157 } text_input;
158
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400159 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500160 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161
Pekka Paalanen77346a62011-11-30 16:26:35 +0200162 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200163 struct wl_array array;
164 unsigned int current;
165 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200166
Jonas Ådahle9d22502012-08-29 22:13:01 +0200167 struct wl_list client_list;
168
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200169 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200170 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200171 int anim_dir;
172 uint32_t anim_timestamp;
173 double anim_current;
174 struct workspace *anim_from;
175 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200176 } workspaces;
177
178 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200179 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200180 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200181 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500182 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200183 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200184 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500185
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200186 struct {
187 struct wl_resource *binding;
188 struct wl_list surfaces;
189 } input_panel;
190
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200191 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500192 struct weston_view *view;
193 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200194 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300195 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200196 } fade;
197
Daniel Stonedf8133b2013-11-19 11:37:14 +0100198 struct exposay {
199 /* XXX: Make these exposay_surfaces. */
200 struct weston_view *focus_prev;
201 struct weston_view *focus_current;
202 struct weston_view *clicked;
203 struct workspace *workspace;
204 struct weston_seat *seat;
205 struct wl_list surface_list;
206
207 struct weston_keyboard_grab grab_kbd;
208 struct weston_pointer_grab grab_ptr;
209
210 enum exposay_target_state state_target;
211 enum exposay_layout_state state_cur;
212 int in_flight; /* number of animations still running */
213
214 int num_surfaces;
215 int grid_size;
216 int surface_size;
217
218 int hpadding_outer;
219 int vpadding_outer;
220 int padding_inner;
221
222 int row_current;
223 int column_current;
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +0100224
225 bool mod_pressed;
226 bool mod_invalid;
Daniel Stonedf8133b2013-11-19 11:37:14 +0100227 } exposay;
228
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300229 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800230 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700231 enum animation_type startup_animation_type;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100232 enum animation_type focus_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800233
234 struct wl_listener output_create_listener;
235 struct wl_list output_list;
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100236
237 char *client;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400238};
239
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500240enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200241 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500242 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300243 SHELL_SURFACE_POPUP,
244 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200245};
246
Scott Moreauff1db4a2012-04-17 19:06:18 -0600247struct ping_timer {
248 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600249 uint32_t serial;
250};
251
Philip Withnall648a4dd2013-11-25 18:01:44 +0000252/*
253 * Surface stacking and ordering.
254 *
255 * This is handled using several linked lists of surfaces, organised into
256 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
257 * above all of the surfaces in the layer below. The set of layers is static and
258 * in the following order (top-most first):
259 * • Lock layer (only ever displayed on its own)
260 * • Cursor layer
261 * • Fullscreen layer
262 * • Panel layer
263 * • Input panel layer
264 * • Workspace layers
265 * • Background layer
266 *
267 * The list of layers may be manipulated to remove whole layers of surfaces from
268 * display. For example, when locking the screen, all layers except the lock
269 * layer are removed.
270 *
271 * A surface’s layer is modified on configuring the surface, in
272 * set_surface_type() (which is only called when the surface’s type change is
273 * _committed_). If a surface’s type changes (e.g. when making a window
274 * fullscreen) its layer changes too.
275 *
276 * In order to allow popup and transient surfaces to be correctly stacked above
277 * their parent surfaces, each surface tracks both its parent surface, and a
278 * linked list of its children. When a surface’s layer is updated, so are the
279 * layers of its children. Note that child surfaces are *not* the same as
280 * subsurfaces — child/parent surfaces are purely for maintaining stacking
281 * order.
282 *
283 * The children_link list of siblings of a surface (i.e. those surfaces which
284 * have the same parent) only contains weston_surfaces which have a
285 * shell_surface. Stacking is not implemented for non-shell_surface
286 * weston_surfaces. This means that the following implication does *not* hold:
287 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
288 */
289
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200290struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500291 struct wl_resource *resource;
292 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200293
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500294 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500295 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600296 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200297 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400298 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000299 struct wl_list children_list; /* child surfaces of this one */
300 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300301 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200302
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400303 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400304 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500305 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800306 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800307 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700308 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100309
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500310 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200311 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500312 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200313 } rotation;
314
315 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100316 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500317 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100318 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400319 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500320 } popup;
321
Alex Wu4539b082012-03-01 12:57:46 +0800322 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300323 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400324 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300325 } transient;
326
327 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800328 enum wl_shell_surface_fullscreen_method type;
329 struct weston_transform transform; /* matrix from x, y */
330 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500331 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800332 } fullscreen;
333
Scott Moreauff1db4a2012-04-17 19:06:18 -0600334 struct ping_timer *ping_timer;
335
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200336 struct weston_transform workspace_transform;
337
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500338 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500339 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100340 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400341
342 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200343
344 struct {
345 bool maximized;
346 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200347 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200348 } state, next_state; /* surface states */
349 bool state_changed;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200350};
351
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300352struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400353 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300354 struct shell_surface *shsurf;
355 struct wl_listener shsurf_destroy_listener;
356};
357
Rusty Lynch1084da52013-08-15 09:10:08 -0700358struct shell_touch_grab {
359 struct weston_touch_grab grab;
360 struct shell_surface *shsurf;
361 struct wl_listener shsurf_destroy_listener;
362 struct weston_touch *touch;
363};
364
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300365struct weston_move_grab {
366 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100367 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500368};
369
Rusty Lynch1084da52013-08-15 09:10:08 -0700370struct weston_touch_move_grab {
371 struct shell_touch_grab base;
372 wl_fixed_t dx, dy;
373};
374
Pekka Paalanen460099f2012-01-20 16:48:25 +0200375struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300376 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500377 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200378 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200379 float x;
380 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200381 } center;
382};
383
Giulio Camuffo5085a752013-03-25 21:42:45 +0100384struct shell_seat {
385 struct weston_seat *seat;
386 struct wl_listener seat_destroy_listener;
387
388 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400389 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100390 struct wl_list surfaces_list;
391 struct wl_client *client;
392 int32_t initial_up;
393 } popup_grab;
394};
395
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400396static void
397activate(struct desktop_shell *shell, struct weston_surface *es,
398 struct weston_seat *seat);
399
400static struct workspace *
401get_current_workspace(struct desktop_shell *shell);
402
Alex Wubd3354b2012-04-17 17:20:49 +0800403static struct shell_surface *
404get_shell_surface(struct weston_surface *surface);
405
406static struct desktop_shell *
407shell_surface_get_shell(struct shell_surface *shsurf);
408
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500409static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400410surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500411
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300412static void
413shell_fade_startup(struct desktop_shell *shell);
414
Philip Withnallbecb77e2013-11-25 18:01:30 +0000415static struct shell_seat *
416get_shell_seat(struct weston_seat *seat);
417
Philip Withnall648a4dd2013-11-25 18:01:44 +0000418static void
419shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
420
Alex Wubd3354b2012-04-17 17:20:49 +0800421static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200422shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
423
424static bool
425shell_surface_is_xdg_surface(struct shell_surface *shsurf);
426
427static bool
428shell_surface_is_xdg_popup(struct shell_surface *shsurf);
429
430static void
431shell_surface_set_parent(struct shell_surface *shsurf,
432 struct weston_surface *parent);
433
434static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800435shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
436{
437 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500438 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800439
440 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100441
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800443 return false;
444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
446 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800447 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500448 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800449}
450
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500451static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400452destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300453{
454 struct shell_grab *grab;
455
456 grab = container_of(listener, struct shell_grab,
457 shsurf_destroy_listener);
458
459 grab->shsurf = NULL;
460}
461
Jason Ekstranda7af7042013-10-12 22:38:11 -0500462static struct weston_view *
463get_default_view(struct weston_surface *surface)
464{
465 struct shell_surface *shsurf;
466 struct weston_view *view;
467
468 if (!surface || wl_list_empty(&surface->views))
469 return NULL;
470
471 shsurf = get_shell_surface(surface);
472 if (shsurf)
473 return shsurf->view;
474
475 wl_list_for_each(view, &surface->views, surface_link)
476 if (weston_view_is_mapped(view))
477 return view;
478
479 return container_of(surface->views.next, struct weston_view, surface_link);
480}
481
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300482static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400483popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400484
485static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300486shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400487 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300488 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400489 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300490 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300491{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300492 struct desktop_shell *shell = shsurf->shell;
493
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400494 popup_grab_end(pointer);
495
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300496 grab->grab.interface = interface;
497 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400498 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500499 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400500 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300501
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700502 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400503 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400504 if (shell->child.desktop_shell) {
505 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
506 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500507 weston_pointer_set_focus(pointer,
508 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400509 wl_fixed_from_int(0),
510 wl_fixed_from_int(0));
511 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300512}
513
514static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300515shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300516{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700517 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400518 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700519 grab->shsurf->grabbed = 0;
520 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300521
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700522 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300523}
524
525static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700526shell_touch_grab_start(struct shell_touch_grab *grab,
527 const struct weston_touch_grab_interface *interface,
528 struct shell_surface *shsurf,
529 struct weston_touch *touch)
530{
531 struct desktop_shell *shell = shsurf->shell;
532
533 grab->grab.interface = interface;
534 grab->shsurf = shsurf;
535 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
536 wl_signal_add(&shsurf->destroy_signal,
537 &grab->shsurf_destroy_listener);
538
539 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700540 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700541
542 weston_touch_start_grab(touch, &grab->grab);
543 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500544 weston_touch_set_focus(touch->seat,
545 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700546}
547
548static void
549shell_touch_grab_end(struct shell_touch_grab *grab)
550{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700551 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700552 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700553 grab->shsurf->grabbed = 0;
554 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700555
556 weston_touch_end_grab(grab->touch);
557}
558
559static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500560center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800561 struct weston_output *output);
562
Daniel Stone496ca172012-05-30 16:31:42 +0100563static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300564get_modifier(char *modifier)
565{
566 if (!modifier)
567 return MODIFIER_SUPER;
568
569 if (!strcmp("ctrl", modifier))
570 return MODIFIER_CTRL;
571 else if (!strcmp("alt", modifier))
572 return MODIFIER_ALT;
573 else if (!strcmp("super", modifier))
574 return MODIFIER_SUPER;
575 else
576 return MODIFIER_SUPER;
577}
578
Juan Zhaoe10d2792012-04-25 19:09:52 +0800579static enum animation_type
580get_animation_type(char *animation)
581{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800582 if (!strcmp("zoom", animation))
583 return ANIMATION_ZOOM;
584 else if (!strcmp("fade", animation))
585 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100586 else if (!strcmp("dim-layer", animation))
587 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800588 else
589 return ANIMATION_NONE;
590}
591
Alex Wu4539b082012-03-01 12:57:46 +0800592static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400593shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200594{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400595 struct weston_config_section *section;
596 int duration;
597 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200598
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400599 section = weston_config_get_section(shell->compositor->config,
600 "screensaver", NULL, NULL);
601 weston_config_section_get_string(section,
602 "path", &shell->screensaver.path, NULL);
603 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200604 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400605
606 section = weston_config_get_section(shell->compositor->config,
607 "shell", NULL, NULL);
608 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100609 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100610 shell->client = s;
611 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400612 "binding-modifier", &s, "super");
613 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200614 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400615 weston_config_section_get_string(section, "animation", &s, "none");
616 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200617 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700618 weston_config_section_get_string(section,
619 "startup-animation", &s, "fade");
620 shell->startup_animation_type = get_animation_type(s);
621 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700622 if (shell->startup_animation_type == ANIMATION_ZOOM)
623 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100624 weston_config_section_get_string(section, "focus-animation", &s, "none");
625 shell->focus_animation_type = get_animation_type(s);
626 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400627 weston_config_section_get_uint(section, "num-workspaces",
628 &shell->workspaces.num,
629 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200630}
631
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100632static struct weston_output *
633get_default_output(struct weston_compositor *compositor)
634{
635 return container_of(compositor->output_list.next,
636 struct weston_output, link);
637}
638
639
640/* no-op func for checking focus surface */
641static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600642focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100643{
644}
645
646static struct focus_surface *
647get_focus_surface(struct weston_surface *surface)
648{
649 if (surface->configure == focus_surface_configure)
650 return surface->configure_private;
651 else
652 return NULL;
653}
654
655static bool
656is_focus_surface (struct weston_surface *es)
657{
658 return (es->configure == focus_surface_configure);
659}
660
661static bool
662is_focus_view (struct weston_view *view)
663{
664 return is_focus_surface (view->surface);
665}
666
667static struct focus_surface *
668create_focus_surface(struct weston_compositor *ec,
669 struct weston_output *output)
670{
671 struct focus_surface *fsurf = NULL;
672 struct weston_surface *surface = NULL;
673
674 fsurf = malloc(sizeof *fsurf);
675 if (!fsurf)
676 return NULL;
677
678 fsurf->surface = weston_surface_create(ec);
679 surface = fsurf->surface;
680 if (surface == NULL) {
681 free(fsurf);
682 return NULL;
683 }
684
685 surface->configure = focus_surface_configure;
686 surface->output = output;
687 surface->configure_private = fsurf;
688
689 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100690 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100691
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600692 surface->width = output->width;
693 surface->height = output->height;
694 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100695 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
696 pixman_region32_fini(&surface->opaque);
697 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
698 output->width, output->height);
699 pixman_region32_fini(&surface->input);
700 pixman_region32_init(&surface->input);
701
702 wl_list_init(&fsurf->workspace_transform.link);
703
704 return fsurf;
705}
706
707static void
708focus_surface_destroy(struct focus_surface *fsurf)
709{
710 weston_surface_destroy(fsurf->surface);
711 free(fsurf);
712}
713
714static void
715focus_animation_done(struct weston_view_animation *animation, void *data)
716{
717 struct workspace *ws = data;
718
719 ws->focus_animation = NULL;
720}
721
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200722static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200723focus_state_destroy(struct focus_state *state)
724{
725 wl_list_remove(&state->seat_destroy_listener.link);
726 wl_list_remove(&state->surface_destroy_listener.link);
727 free(state);
728}
729
730static void
731focus_state_seat_destroy(struct wl_listener *listener, void *data)
732{
733 struct focus_state *state = container_of(listener,
734 struct focus_state,
735 seat_destroy_listener);
736
737 wl_list_remove(&state->link);
738 focus_state_destroy(state);
739}
740
741static void
742focus_state_surface_destroy(struct wl_listener *listener, void *data)
743{
744 struct focus_state *state = container_of(listener,
745 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400746 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400747 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500748 struct weston_surface *main_surface, *next;
749 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200750
Pekka Paalanen01388e22013-04-25 13:57:44 +0300751 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
752
Kristian Høgsberge3778222012-07-31 17:29:30 -0400753 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500754 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
755 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400756 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100757 if (is_focus_view(view))
758 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400759
Jason Ekstranda7af7042013-10-12 22:38:11 -0500760 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400761 break;
762 }
763
Pekka Paalanen01388e22013-04-25 13:57:44 +0300764 /* if the focus was a sub-surface, activate its main surface */
765 if (main_surface != state->keyboard_focus)
766 next = main_surface;
767
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100768 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400769 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100770 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400771 activate(shell, next, state->seat);
772 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100773 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
774 if (state->ws->focus_animation)
775 weston_view_animation_destroy(state->ws->focus_animation);
776
777 state->ws->focus_animation = weston_fade_run(
778 state->ws->fsurf_front->view,
779 state->ws->fsurf_front->view->alpha, 0.0, 300,
780 focus_animation_done, state->ws);
781 }
782
Kristian Høgsberge3778222012-07-31 17:29:30 -0400783 wl_list_remove(&state->link);
784 focus_state_destroy(state);
785 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200786}
787
788static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400789focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200790{
Jonas Ådahl04769742012-06-13 00:01:24 +0200791 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200792
793 state = malloc(sizeof *state);
794 if (state == NULL)
795 return NULL;
796
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100797 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400798 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200799 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400800 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200801
802 state->seat_destroy_listener.notify = focus_state_seat_destroy;
803 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400804 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200805 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400806 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200807
808 return state;
809}
810
Jonas Ådahl8538b222012-08-29 22:13:03 +0200811static struct focus_state *
812ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
813{
814 struct workspace *ws = get_current_workspace(shell);
815 struct focus_state *state;
816
817 wl_list_for_each(state, &ws->focus_list, link)
818 if (state->seat == seat)
819 break;
820
821 if (&state->link == &ws->focus_list)
822 state = focus_state_create(seat, ws);
823
824 return state;
825}
826
Jonas Ådahl04769742012-06-13 00:01:24 +0200827static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400828restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200829{
830 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400831 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200832
833 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400834 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200835
Kristian Høgsberge3148752013-05-06 23:19:49 -0400836 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200837 }
838}
839
840static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200841replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
842 struct weston_seat *seat)
843{
844 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400845 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200846
847 wl_list_for_each(state, &ws->focus_list, link) {
848 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400849 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500850 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200851 return;
852 }
853 }
854}
855
856static void
857drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
858 struct weston_surface *surface)
859{
860 struct focus_state *state;
861
862 wl_list_for_each(state, &ws->focus_list, link)
863 if (state->keyboard_focus == surface)
864 state->keyboard_focus = NULL;
865}
866
867static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
869 struct weston_view *from, struct weston_view *to)
870{
871 struct weston_output *output;
872 bool focus_surface_created = false;
873
874 /* FIXME: Only support dim animation using two layers */
875 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
876 return;
877
878 output = get_default_output(shell->compositor);
879 if (ws->fsurf_front == NULL && (from || to)) {
880 ws->fsurf_front = create_focus_surface(shell->compositor, output);
881 ws->fsurf_back = create_focus_surface(shell->compositor, output);
882 ws->fsurf_front->view->alpha = 0.0;
883 ws->fsurf_back->view->alpha = 0.0;
884 focus_surface_created = true;
885 } else {
886 wl_list_remove(&ws->fsurf_front->view->layer_link);
887 wl_list_remove(&ws->fsurf_back->view->layer_link);
888 }
889
890 if (ws->focus_animation) {
891 weston_view_animation_destroy(ws->focus_animation);
892 ws->focus_animation = NULL;
893 }
894
895 if (to)
896 wl_list_insert(&to->layer_link,
897 &ws->fsurf_front->view->layer_link);
898 else if (from)
899 wl_list_insert(&ws->layer.view_list,
900 &ws->fsurf_front->view->layer_link);
901
902 if (focus_surface_created) {
903 ws->focus_animation = weston_fade_run(
904 ws->fsurf_front->view,
905 ws->fsurf_front->view->alpha, 0.6, 300,
906 focus_animation_done, ws);
907 } else if (from) {
908 wl_list_insert(&from->layer_link,
909 &ws->fsurf_back->view->layer_link);
910 ws->focus_animation = weston_stable_fade_run(
911 ws->fsurf_front->view, 0.0,
912 ws->fsurf_back->view, 0.6,
913 focus_animation_done, ws);
914 } else if (to) {
915 wl_list_insert(&ws->layer.view_list,
916 &ws->fsurf_back->view->layer_link);
917 ws->focus_animation = weston_stable_fade_run(
918 ws->fsurf_front->view, 0.0,
919 ws->fsurf_back->view, 0.6,
920 focus_animation_done, ws);
921 }
922}
923
924static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200925workspace_destroy(struct workspace *ws)
926{
Jonas Ådahl04769742012-06-13 00:01:24 +0200927 struct focus_state *state, *next;
928
929 wl_list_for_each_safe(state, next, &ws->focus_list, link)
930 focus_state_destroy(state);
931
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100932 if (ws->fsurf_front)
933 focus_surface_destroy(ws->fsurf_front);
934 if (ws->fsurf_back)
935 focus_surface_destroy(ws->fsurf_back);
936
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200937 free(ws);
938}
939
Jonas Ådahl04769742012-06-13 00:01:24 +0200940static void
941seat_destroyed(struct wl_listener *listener, void *data)
942{
943 struct weston_seat *seat = data;
944 struct focus_state *state, *next;
945 struct workspace *ws = container_of(listener,
946 struct workspace,
947 seat_destroyed_listener);
948
949 wl_list_for_each_safe(state, next, &ws->focus_list, link)
950 if (state->seat == seat)
951 wl_list_remove(&state->link);
952}
953
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200954static struct workspace *
955workspace_create(void)
956{
957 struct workspace *ws = malloc(sizeof *ws);
958 if (ws == NULL)
959 return NULL;
960
961 weston_layer_init(&ws->layer, NULL);
962
Jonas Ådahl04769742012-06-13 00:01:24 +0200963 wl_list_init(&ws->focus_list);
964 wl_list_init(&ws->seat_destroyed_listener.link);
965 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 ws->fsurf_front = NULL;
967 ws->fsurf_back = NULL;
968 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200969
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200970 return ws;
971}
972
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200973static int
974workspace_is_empty(struct workspace *ws)
975{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200977}
978
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200979static struct workspace *
980get_workspace(struct desktop_shell *shell, unsigned int index)
981{
982 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200983 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200984 pws += index;
985 return *pws;
986}
987
988static struct workspace *
989get_current_workspace(struct desktop_shell *shell)
990{
991 return get_workspace(shell, shell->workspaces.current);
992}
993
994static void
995activate_workspace(struct desktop_shell *shell, unsigned int index)
996{
997 struct workspace *ws;
998
999 ws = get_workspace(shell, index);
1000 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1001
1002 shell->workspaces.current = index;
1003}
1004
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005static unsigned int
1006get_output_height(struct weston_output *output)
1007{
1008 return abs(output->region.extents.y1 - output->region.extents.y2);
1009}
1010
1011static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001012view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001014 struct weston_transform *transform;
1015
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001016 if (is_focus_view(view)) {
1017 struct focus_surface *fsurf = get_focus_surface(view->surface);
1018 transform = &fsurf->workspace_transform;
1019 } else {
1020 struct shell_surface *shsurf = get_shell_surface(view->surface);
1021 transform = &shsurf->workspace_transform;
1022 }
1023
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001024 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001026 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001027
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028 weston_matrix_init(&transform->matrix);
1029 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001030 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001032}
1033
1034static void
1035workspace_translate_out(struct workspace *ws, double fraction)
1036{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001038 unsigned int height;
1039 double d;
1040
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1042 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001043 d = 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
1050workspace_translate_in(struct workspace *ws, double fraction)
1051{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001053 unsigned int height;
1054 double d;
1055
Jason Ekstranda7af7042013-10-12 22:38:11 -05001056 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1057 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001058
1059 if (fraction > 0)
1060 d = -(height - height * fraction);
1061 else
1062 d = height + height * fraction;
1063
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001064 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001065 }
1066}
1067
1068static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001069broadcast_current_workspace_state(struct desktop_shell *shell)
1070{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001071 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001072
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001073 wl_resource_for_each(resource, &shell->workspaces.client_list)
1074 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001075 shell->workspaces.current,
1076 shell->workspaces.num);
1077}
1078
1079static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080reverse_workspace_change_animation(struct desktop_shell *shell,
1081 unsigned int index,
1082 struct workspace *from,
1083 struct workspace *to)
1084{
1085 shell->workspaces.current = index;
1086
1087 shell->workspaces.anim_to = to;
1088 shell->workspaces.anim_from = from;
1089 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1090 shell->workspaces.anim_timestamp = 0;
1091
Scott Moreau4272e632012-08-13 09:58:41 -06001092 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093}
1094
1095static void
1096workspace_deactivate_transforms(struct workspace *ws)
1097{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001099 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001102 if (is_focus_view(view)) {
1103 struct focus_surface *fsurf = get_focus_surface(view->surface);
1104 transform = &fsurf->workspace_transform;
1105 } else {
1106 struct shell_surface *shsurf = get_shell_surface(view->surface);
1107 transform = &shsurf->workspace_transform;
1108 }
1109
1110 if (!wl_list_empty(&transform->link)) {
1111 wl_list_remove(&transform->link);
1112 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001113 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115 }
1116}
1117
1118static void
1119finish_workspace_change_animation(struct desktop_shell *shell,
1120 struct workspace *from,
1121 struct workspace *to)
1122{
Scott Moreau4272e632012-08-13 09:58:41 -06001123 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124
1125 wl_list_remove(&shell->workspaces.animation.link);
1126 workspace_deactivate_transforms(from);
1127 workspace_deactivate_transforms(to);
1128 shell->workspaces.anim_to = NULL;
1129
1130 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1131}
1132
1133static void
1134animate_workspace_change_frame(struct weston_animation *animation,
1135 struct weston_output *output, uint32_t msecs)
1136{
1137 struct desktop_shell *shell =
1138 container_of(animation, struct desktop_shell,
1139 workspaces.animation);
1140 struct workspace *from = shell->workspaces.anim_from;
1141 struct workspace *to = shell->workspaces.anim_to;
1142 uint32_t t;
1143 double x, y;
1144
1145 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1146 finish_workspace_change_animation(shell, from, to);
1147 return;
1148 }
1149
1150 if (shell->workspaces.anim_timestamp == 0) {
1151 if (shell->workspaces.anim_current == 0.0)
1152 shell->workspaces.anim_timestamp = msecs;
1153 else
1154 shell->workspaces.anim_timestamp =
1155 msecs -
1156 /* Invers of movement function 'y' below. */
1157 (asin(1.0 - shell->workspaces.anim_current) *
1158 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1159 M_2_PI);
1160 }
1161
1162 t = msecs - shell->workspaces.anim_timestamp;
1163
1164 /*
1165 * x = [0, π/2]
1166 * y(x) = sin(x)
1167 */
1168 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1169 y = sin(x);
1170
1171 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001172 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173
1174 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1175 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1176 shell->workspaces.anim_current = y;
1177
Scott Moreau4272e632012-08-13 09:58:41 -06001178 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001179 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001180 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001181 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182}
1183
1184static void
1185animate_workspace_change(struct desktop_shell *shell,
1186 unsigned int index,
1187 struct workspace *from,
1188 struct workspace *to)
1189{
1190 struct weston_output *output;
1191
1192 int dir;
1193
1194 if (index > shell->workspaces.current)
1195 dir = -1;
1196 else
1197 dir = 1;
1198
1199 shell->workspaces.current = index;
1200
1201 shell->workspaces.anim_dir = dir;
1202 shell->workspaces.anim_from = from;
1203 shell->workspaces.anim_to = to;
1204 shell->workspaces.anim_current = 0.0;
1205 shell->workspaces.anim_timestamp = 0;
1206
1207 output = container_of(shell->compositor->output_list.next,
1208 struct weston_output, link);
1209 wl_list_insert(&output->animation_list,
1210 &shell->workspaces.animation.link);
1211
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001212 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001213
1214 workspace_translate_in(to, 0);
1215
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001216 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001217
Scott Moreau4272e632012-08-13 09:58:41 -06001218 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219}
1220
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001221static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001222update_workspace(struct desktop_shell *shell, unsigned int index,
1223 struct workspace *from, struct workspace *to)
1224{
1225 shell->workspaces.current = index;
1226 wl_list_insert(&from->layer.link, &to->layer.link);
1227 wl_list_remove(&from->layer.link);
1228}
1229
1230static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001231change_workspace(struct desktop_shell *shell, unsigned int index)
1232{
1233 struct workspace *from;
1234 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001235 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001236
1237 if (index == shell->workspaces.current)
1238 return;
1239
1240 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001242 return;
1243
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001244 from = get_current_workspace(shell);
1245 to = get_workspace(shell, index);
1246
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001247 if (shell->workspaces.anim_from == to &&
1248 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001249 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001250 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001251 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001252 return;
1253 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001254
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001255 if (shell->workspaces.anim_to != NULL)
1256 finish_workspace_change_animation(shell,
1257 shell->workspaces.anim_from,
1258 shell->workspaces.anim_to);
1259
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001260 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001261
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001262 if (shell->focus_animation_type != ANIMATION_NONE) {
1263 wl_list_for_each(state, &from->focus_list, link)
1264 if (state->keyboard_focus)
1265 animate_focus_change(shell, from,
1266 get_default_view(state->keyboard_focus), NULL);
1267
1268 wl_list_for_each(state, &to->focus_list, link)
1269 if (state->keyboard_focus)
1270 animate_focus_change(shell, to,
1271 NULL, get_default_view(state->keyboard_focus));
1272 }
1273
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274 if (workspace_is_empty(to) && workspace_is_empty(from))
1275 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001276 else
1277 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001278
1279 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001280}
1281
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282static bool
1283workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1284{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001286 struct wl_list *e;
1287
1288 if (wl_list_empty(list))
1289 return false;
1290
1291 e = list->next;
1292
1293 if (e->next != list)
1294 return false;
1295
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001297}
1298
1299static void
Philip Withnall659163d2013-11-25 18:01:36 +00001300move_surface_to_workspace(struct desktop_shell *shell,
1301 struct shell_surface *shsurf,
1302 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001303{
1304 struct workspace *from;
1305 struct workspace *to;
1306 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001307 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001308 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001309
1310 if (workspace == shell->workspaces.current)
1311 return;
1312
Philip Withnall659163d2013-11-25 18:01:36 +00001313 view = get_default_view(shsurf->surface);
1314 if (!view)
1315 return;
1316
1317 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1318
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001319 if (workspace >= shell->workspaces.num)
1320 workspace = shell->workspaces.num - 1;
1321
Jonas Ådahle9d22502012-08-29 22:13:01 +02001322 from = get_current_workspace(shell);
1323 to = get_workspace(shell, workspace);
1324
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 wl_list_remove(&view->layer_link);
1326 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327
Philip Withnall648a4dd2013-11-25 18:01:44 +00001328 shell_surface_update_child_surface_layers(shsurf);
1329
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001331 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1332 if (!seat->keyboard)
1333 continue;
1334
1335 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001337 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001338 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001341}
1342
1343static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001344take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001345 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001346 unsigned int index)
1347{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001348 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001350 struct shell_surface *shsurf;
1351 struct workspace *from;
1352 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001353 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001354
Pekka Paalanen01388e22013-04-25 13:57:44 +03001355 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 view = get_default_view(surface);
1357 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001358 index == shell->workspaces.current ||
1359 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001360 return;
1361
1362 from = get_current_workspace(shell);
1363 to = get_workspace(shell, index);
1364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 wl_list_remove(&view->layer_link);
1366 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001367
Philip Withnall659163d2013-11-25 18:01:36 +00001368 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001369 if (shsurf != NULL)
1370 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001371
Jonas Ådahle9d22502012-08-29 22:13:01 +02001372 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001373 drop_focus_state(shell, from, surface);
1374
1375 if (shell->workspaces.anim_from == to &&
1376 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001377 wl_list_remove(&to->layer.link);
1378 wl_list_insert(from->layer.link.prev, &to->layer.link);
1379
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001380 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001381 broadcast_current_workspace_state(shell);
1382
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001383 return;
1384 }
1385
1386 if (shell->workspaces.anim_to != NULL)
1387 finish_workspace_change_animation(shell,
1388 shell->workspaces.anim_from,
1389 shell->workspaces.anim_to);
1390
1391 if (workspace_is_empty(from) &&
1392 workspace_has_only(to, surface))
1393 update_workspace(shell, index, from, to);
1394 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001395 if (shsurf != NULL &&
1396 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001397 wl_list_insert(&shell->workspaces.anim_sticky_list,
1398 &shsurf->workspace_transform.link);
1399
1400 animate_workspace_change(shell, index, from, to);
1401 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001402
1403 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001404
1405 state = ensure_focus_state(shell, seat);
1406 if (state != NULL)
1407 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001408}
1409
1410static void
1411workspace_manager_move_surface(struct wl_client *client,
1412 struct wl_resource *resource,
1413 struct wl_resource *surface_resource,
1414 uint32_t workspace)
1415{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001416 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001417 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001418 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001419 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001420 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001421
Pekka Paalanen01388e22013-04-25 13:57:44 +03001422 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001423 shell_surface = get_shell_surface(main_surface);
1424 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001426
1427 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001428}
1429
1430static const struct workspace_manager_interface workspace_manager_implementation = {
1431 workspace_manager_move_surface,
1432};
1433
1434static void
1435unbind_resource(struct wl_resource *resource)
1436{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001437 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001438}
1439
1440static void
1441bind_workspace_manager(struct wl_client *client,
1442 void *data, uint32_t version, uint32_t id)
1443{
1444 struct desktop_shell *shell = data;
1445 struct wl_resource *resource;
1446
Jason Ekstranda85118c2013-06-27 20:17:02 -05001447 resource = wl_resource_create(client,
1448 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001449
1450 if (resource == NULL) {
1451 weston_log("couldn't add workspace manager object");
1452 return;
1453 }
1454
Jason Ekstranda85118c2013-06-27 20:17:02 -05001455 wl_resource_set_implementation(resource,
1456 &workspace_manager_implementation,
1457 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001458 wl_list_insert(&shell->workspaces.client_list,
1459 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001460
1461 workspace_manager_send_state(resource,
1462 shell->workspaces.current,
1463 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001464}
1465
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001466static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001467touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1468 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1469{
1470}
1471
1472static void
1473touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1474{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001475 struct weston_touch_move_grab *move =
1476 (struct weston_touch_move_grab *) container_of(
1477 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001478
Jonas Ådahl9484b692013-12-02 22:05:03 +01001479 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001480 shell_touch_grab_end(&move->base);
1481 free(move);
1482 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001483}
1484
1485static void
1486touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1487 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1488{
1489 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1490 struct shell_surface *shsurf = move->base.shsurf;
1491 struct weston_surface *es;
1492 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1493 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1494
1495 if (!shsurf)
1496 return;
1497
1498 es = shsurf->surface;
1499
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001500 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001501
1502 weston_compositor_schedule_repaint(es->compositor);
1503}
1504
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001505static void
1506touch_move_grab_cancel(struct weston_touch_grab *grab)
1507{
1508 struct weston_touch_move_grab *move =
1509 (struct weston_touch_move_grab *) container_of(
1510 grab, struct shell_touch_grab, grab);
1511
1512 shell_touch_grab_end(&move->base);
1513 free(move);
1514}
1515
Rusty Lynch1084da52013-08-15 09:10:08 -07001516static const struct weston_touch_grab_interface touch_move_grab_interface = {
1517 touch_move_grab_down,
1518 touch_move_grab_up,
1519 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001520 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001521};
1522
1523static int
1524surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1525{
1526 struct weston_touch_move_grab *move;
1527
1528 if (!shsurf)
1529 return -1;
1530
Rafael Antognolli03b16592013-12-03 15:35:42 -02001531 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001532 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001533 if (shsurf->grabbed)
1534 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001535
1536 move = malloc(sizeof *move);
1537 if (!move)
1538 return -1;
1539
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001541 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001543 seat->touch->grab_y;
1544
1545 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1546 seat->touch);
1547
1548 return 0;
1549}
1550
1551static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001552noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001553{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001554}
1555
1556static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001557move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1558 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001559{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001560 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001561 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001562 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001563 int dx, dy;
1564
1565 weston_pointer_move(pointer, x, y);
1566 dx = wl_fixed_to_int(pointer->x + move->dx);
1567 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001568
1569 if (!shsurf)
1570 return;
1571
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001572 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001573
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001575}
1576
1577static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001578move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001579 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001580{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001581 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1582 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001583 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001584 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001585
Daniel Stone4dbadb12012-05-30 16:31:51 +01001586 if (pointer->button_count == 0 &&
1587 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001588 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001589 free(grab);
1590 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001591}
1592
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001593static void
1594move_grab_cancel(struct weston_pointer_grab *grab)
1595{
1596 struct shell_grab *shell_grab =
1597 container_of(grab, struct shell_grab, grab);
1598
1599 shell_grab_end(shell_grab);
1600 free(grab);
1601}
1602
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001603static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001604 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001605 move_grab_motion,
1606 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001607 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001608};
1609
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001611surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001612{
1613 struct weston_move_grab *move;
1614
1615 if (!shsurf)
1616 return -1;
1617
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001618 if (shsurf->grabbed)
1619 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001620 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621 return 0;
1622
1623 move = malloc(sizeof *move);
1624 if (!move)
1625 return -1;
1626
Jason Ekstranda7af7042013-10-12 22:38:11 -05001627 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001628 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001630 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001631
1632 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001633 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001634
1635 return 0;
1636}
1637
1638static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001639common_surface_move(struct wl_resource *resource,
1640 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001641{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001642 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001643 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001644 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001646 if (seat->pointer &&
1647 seat->pointer->button_count > 0 &&
1648 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001650 if ((surface == shsurf->surface) &&
1651 (surface_move(shsurf, seat) < 0))
1652 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001653 } else if (seat->touch &&
1654 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001656 if ((surface == shsurf->surface) &&
1657 (surface_touch_move(shsurf, seat) < 0))
1658 wl_resource_post_no_memory(resource);
1659 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001660}
1661
Rafael Antognollie2a34552013-12-03 15:35:45 -02001662static void
1663shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1664 struct wl_resource *seat_resource, uint32_t serial)
1665{
1666 common_surface_move(resource, seat_resource, serial);
1667}
1668
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001669struct weston_resize_grab {
1670 struct shell_grab base;
1671 uint32_t edges;
1672 int32_t width, height;
1673};
1674
1675static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001676resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1677 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678{
1679 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001680 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001681 struct shell_surface *shsurf = resize->base.shsurf;
1682 int32_t width, height;
1683 wl_fixed_t from_x, from_y;
1684 wl_fixed_t to_x, to_y;
1685
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001686 weston_pointer_move(pointer, x, y);
1687
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688 if (!shsurf)
1689 return;
1690
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691 weston_view_from_global_fixed(shsurf->view,
1692 pointer->grab_x, pointer->grab_y,
1693 &from_x, &from_y);
1694 weston_view_from_global_fixed(shsurf->view,
1695 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001696
1697 width = resize->width;
1698 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1699 width += wl_fixed_to_int(from_x - to_x);
1700 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1701 width += wl_fixed_to_int(to_x - from_x);
1702 }
1703
1704 height = resize->height;
1705 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1706 height += wl_fixed_to_int(from_y - to_y);
1707 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1708 height += wl_fixed_to_int(to_y - from_y);
1709 }
1710
1711 shsurf->client->send_configure(shsurf->surface,
1712 resize->edges, width, height);
1713}
1714
1715static void
1716send_configure(struct weston_surface *surface,
1717 uint32_t edges, int32_t width, int32_t height)
1718{
1719 struct shell_surface *shsurf = get_shell_surface(surface);
1720
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001721 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001722 edges, width, height);
1723}
1724
1725static const struct weston_shell_client shell_client = {
1726 send_configure
1727};
1728
1729static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001730resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731 uint32_t time, uint32_t button, uint32_t state_w)
1732{
1733 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001734 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001735 enum wl_pointer_button_state state = state_w;
1736
1737 if (pointer->button_count == 0 &&
1738 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1739 shell_grab_end(&resize->base);
1740 free(grab);
1741 }
1742}
1743
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001744static void
1745resize_grab_cancel(struct weston_pointer_grab *grab)
1746{
1747 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1748
1749 shell_grab_end(&resize->base);
1750 free(grab);
1751}
1752
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001753static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001754 noop_grab_focus,
1755 resize_grab_motion,
1756 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001757 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001758};
1759
Giulio Camuffob8366642013-04-25 13:57:46 +03001760/*
1761 * Returns the bounding box of a surface and all its sub-surfaces,
1762 * in the surface coordinates system. */
1763static void
1764surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1765 int32_t *y, int32_t *w, int32_t *h) {
1766 pixman_region32_t region;
1767 pixman_box32_t *box;
1768 struct weston_subsurface *subsurface;
1769
1770 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 surface->width,
1772 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001773
1774 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1775 pixman_region32_union_rect(&region, &region,
1776 subsurface->position.x,
1777 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778 subsurface->surface->width,
1779 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001780 }
1781
1782 box = pixman_region32_extents(&region);
1783 if (x)
1784 *x = box->x1;
1785 if (y)
1786 *y = box->y1;
1787 if (w)
1788 *w = box->x2 - box->x1;
1789 if (h)
1790 *h = box->y2 - box->y1;
1791
1792 pixman_region32_fini(&region);
1793}
1794
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001795static int
1796surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001797 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001798{
1799 struct weston_resize_grab *resize;
1800
Rafael Antognolli03b16592013-12-03 15:35:42 -02001801 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001802 return 0;
1803
1804 if (edges == 0 || edges > 15 ||
1805 (edges & 3) == 3 || (edges & 12) == 12)
1806 return 0;
1807
1808 resize = malloc(sizeof *resize);
1809 if (!resize)
1810 return -1;
1811
1812 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001813 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1814 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001815
1816 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001817 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001818
1819 return 0;
1820}
1821
1822static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001823common_surface_resize(struct wl_resource *resource,
1824 struct wl_resource *seat_resource, uint32_t serial,
1825 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001826{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001827 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001828 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001829 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001830
Rafael Antognolli03b16592013-12-03 15:35:42 -02001831 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001832 return;
1833
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001835 if (seat->pointer->button_count == 0 ||
1836 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001837 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001838 return;
1839
Kristian Høgsberge3148752013-05-06 23:19:49 -04001840 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001841 wl_resource_post_no_memory(resource);
1842}
1843
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001844static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001845shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1846 struct wl_resource *seat_resource, uint32_t serial,
1847 uint32_t edges)
1848{
1849 common_surface_resize(resource, seat_resource, serial, edges);
1850}
1851
1852static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001853end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1854
1855static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001856busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001857{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001858 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001859 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001861 wl_fixed_t sx, sy;
1862
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863 view = weston_compositor_pick_view(pointer->seat->compositor,
1864 pointer->x, pointer->y,
1865 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001866
Jason Ekstranda7af7042013-10-12 22:38:11 -05001867 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001868 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001869}
1870
1871static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001872busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1873 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001874{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001875 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001876}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001877
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001878static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001879busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001880 uint32_t time, uint32_t button, uint32_t state)
1881{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001882 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001883 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001884 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001885
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001886 if (shsurf && button == BTN_LEFT && state) {
1887 activate(shsurf->shell, shsurf->surface, seat);
1888 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001889 } else if (shsurf && button == BTN_RIGHT && state) {
1890 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001891 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001892 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001893}
1894
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001895static void
1896busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1897{
1898 struct shell_grab *grab = (struct shell_grab *) base;
1899
1900 shell_grab_end(grab);
1901 free(grab);
1902}
1903
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001904static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001905 busy_cursor_grab_focus,
1906 busy_cursor_grab_motion,
1907 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001908 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001909};
1910
1911static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001912set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001913{
1914 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001915
1916 grab = malloc(sizeof *grab);
1917 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001918 return;
1919
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001920 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1921 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001922}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001923
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001924static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001925end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001926{
1927 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1928
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001929 if (grab->grab.interface == &busy_cursor_grab_interface &&
1930 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001931 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001932 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001933 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001934}
1935
Scott Moreau9521d5e2012-04-19 13:06:17 -06001936static void
1937ping_timer_destroy(struct shell_surface *shsurf)
1938{
1939 if (!shsurf || !shsurf->ping_timer)
1940 return;
1941
1942 if (shsurf->ping_timer->source)
1943 wl_event_source_remove(shsurf->ping_timer->source);
1944
1945 free(shsurf->ping_timer);
1946 shsurf->ping_timer = NULL;
1947}
1948
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001949static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001950ping_timeout_handler(void *data)
1951{
1952 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001953 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001954
Scott Moreau9521d5e2012-04-19 13:06:17 -06001955 /* Client is not responding */
1956 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001957
1958 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001959 if (seat->pointer->focus &&
1960 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001961 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001962
1963 return 1;
1964}
1965
1966static void
1967ping_handler(struct weston_surface *surface, uint32_t serial)
1968{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001969 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001970 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001971 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001972
1973 if (!shsurf)
1974 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001975 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001976 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001977
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001978 if (shsurf->surface == shsurf->shell->grab_surface)
1979 return;
1980
Scott Moreauff1db4a2012-04-17 19:06:18 -06001981 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001982 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001983 if (!shsurf->ping_timer)
1984 return;
1985
1986 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001987 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1988 shsurf->ping_timer->source =
1989 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1990 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1991
Rafael Antognollie2a34552013-12-03 15:35:45 -02001992 if (shell_surface_is_wl_shell_surface(shsurf))
1993 wl_shell_surface_send_ping(shsurf->resource, serial);
1994 else if (shell_surface_is_xdg_surface(shsurf))
1995 xdg_surface_send_ping(shsurf->resource, serial);
1996 else if (shell_surface_is_xdg_popup(shsurf))
1997 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001998 }
1999}
2000
2001static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002002handle_pointer_focus(struct wl_listener *listener, void *data)
2003{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002004 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002005 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002006 struct weston_compositor *compositor;
2007 struct shell_surface *shsurf;
2008 uint32_t serial;
2009
Jason Ekstranda7af7042013-10-12 22:38:11 -05002010 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002011 return;
2012
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 compositor = view->surface->compositor;
2014 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002015
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03002016 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002017 set_busy_cursor(shsurf, pointer);
2018 } else {
2019 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002021 }
2022}
2023
2024static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002025create_pointer_focus_listener(struct weston_seat *seat)
2026{
2027 struct wl_listener *listener;
2028
Kristian Høgsberge3148752013-05-06 23:19:49 -04002029 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002030 return;
2031
2032 listener = malloc(sizeof *listener);
2033 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002034 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002035}
2036
2037static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002038xdg_surface_set_transient_for(struct wl_client *client,
2039 struct wl_resource *resource,
2040 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06002041{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002042 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002043 struct weston_surface *parent =
2044 wl_resource_get_user_data(parent_resource);
2045
2046 shell_surface_set_parent(shsurf, parent);
2047}
2048
2049static void
2050surface_pong(struct shell_surface *shsurf, uint32_t serial)
2051{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002052 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002053 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002054
Kristian Høgsberg92374e12012-08-11 22:39:12 -04002055 if (shsurf->ping_timer == NULL)
2056 /* Just ignore unsolicited pong. */
2057 return;
2058
Scott Moreauff1db4a2012-04-17 19:06:18 -06002059 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002060 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02002061 wl_list_for_each(seat, &ec->seat_list, link) {
2062 if(seat->pointer)
2063 end_busy_cursor(shsurf, seat->pointer);
2064 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06002065 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002066 }
2067}
2068
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002069static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002070shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
2071 uint32_t serial)
2072{
2073 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2074
2075 surface_pong(shsurf, serial);
2076
2077}
2078
2079static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002080set_title(struct shell_surface *shsurf, const char *title)
2081{
2082 free(shsurf->title);
2083 shsurf->title = strdup(title);
2084}
2085
2086static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002087shell_surface_set_title(struct wl_client *client,
2088 struct wl_resource *resource, const char *title)
2089{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002090 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002091
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002092 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002093}
2094
2095static void
2096shell_surface_set_class(struct wl_client *client,
2097 struct wl_resource *resource, const char *class)
2098{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002099 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002100
2101 free(shsurf->class);
2102 shsurf->class = strdup(class);
2103}
2104
Alex Wu4539b082012-03-01 12:57:46 +08002105static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002106restore_output_mode(struct weston_output *output)
2107{
Hardening57388e42013-09-18 23:56:36 +02002108 if (output->current_mode != output->original_mode ||
2109 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002110 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002111 output->original_mode,
2112 output->original_scale,
2113 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002114}
2115
2116static void
2117restore_all_output_modes(struct weston_compositor *compositor)
2118{
2119 struct weston_output *output;
2120
2121 wl_list_for_each(output, &compositor->output_list, link)
2122 restore_output_mode(output);
2123}
2124
Philip Withnallbecb77e2013-11-25 18:01:30 +00002125static int
2126get_output_panel_height(struct desktop_shell *shell,
2127 struct weston_output *output)
2128{
2129 struct weston_view *view;
2130 int panel_height = 0;
2131
2132 if (!output)
2133 return 0;
2134
2135 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2136 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002137 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002138 break;
2139 }
2140 }
2141
2142 return panel_height;
2143}
2144
Philip Withnall07926d92013-11-25 18:01:40 +00002145/* The surface will be inserted into the list immediately after the link
2146 * returned by this function (i.e. will be stacked immediately above the
2147 * returned link). */
2148static struct wl_list *
2149shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2150{
2151 struct workspace *ws;
2152
2153 switch (shsurf->type) {
Philip Withnallda704d92013-11-25 18:01:46 +00002154 case SHELL_SURFACE_POPUP: {
2155 /* Popups should go at the front of the workspace of their
2156 * parent surface, rather than just in front of the parent. This
2157 * fixes the situation where there are two top-level windows:
2158 * - Above
2159 * - Below
2160 * and a pop-up menu is created for 'Below'. We want:
2161 * - Popup
2162 * - Above
2163 * - Below
2164 * not:
2165 * - Above
2166 * - Popup
2167 * - Below
2168 */
2169 struct shell_surface *parent_shsurf;
2170
2171 parent_shsurf = get_shell_surface(shsurf->parent);
2172 if (parent_shsurf != NULL)
2173 return shell_surface_calculate_layer_link(parent_shsurf);
2174
2175 break;
2176 }
2177
Rafael Antognolli03b16592013-12-03 15:35:42 -02002178 case SHELL_SURFACE_TOPLEVEL: {
Rafael Antognollied207b42013-12-03 15:35:43 -02002179 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002180 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002181 } else if (shsurf->parent) {
2182 /* Move the surface to its parent layer so that
2183 * surfaces which are transient for fullscreen surfaces
2184 * don't get hidden by the fullscreen surfaces.
2185 * However, unlike popups, transient surfaces are
2186 * stacked in front of their parent but not in front of
2187 * other surfaces of the same type. */
2188 struct weston_view *parent;
2189
2190 /* TODO: Handle a parent with multiple views */
2191 parent = get_default_view(shsurf->parent);
2192 if (parent)
2193 return parent->layer_link.prev;
2194 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002195 break;
2196 }
Philip Withnall07926d92013-11-25 18:01:40 +00002197
2198 case SHELL_SURFACE_XWAYLAND:
Philip Withnall07926d92013-11-25 18:01:40 +00002199 case SHELL_SURFACE_NONE:
2200 default:
2201 /* Go to the fallback, below. */
2202 break;
2203 }
2204
2205 /* Move the surface to a normal workspace layer so that surfaces
2206 * which were previously fullscreen or transient are no longer
2207 * rendered on top. */
2208 ws = get_current_workspace(shsurf->shell);
2209 return &ws->layer.view_list;
2210}
2211
Philip Withnall648a4dd2013-11-25 18:01:44 +00002212static void
2213shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2214{
2215 struct shell_surface *child;
2216
2217 /* Move the child layers to the same workspace as shsurf. They will be
2218 * stacked above shsurf. */
2219 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2220 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2221 weston_view_geometry_dirty(child->view);
2222 wl_list_remove(&child->view->layer_link);
2223 wl_list_insert(shsurf->view->layer_link.prev,
2224 &child->view->layer_link);
2225 weston_view_geometry_dirty(child->view);
2226 weston_surface_damage(child->surface);
2227
2228 /* Recurse. We don’t expect this to recurse very far (if
2229 * at all) because that would imply we have transient
2230 * (or popup) children of transient surfaces, which
2231 * would be unusual. */
2232 shell_surface_update_child_surface_layers(child);
2233 }
2234 }
2235}
2236
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002237/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002238 * geometry to ensure the changes are redrawn.
2239 *
2240 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2241 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002242static void
2243shell_surface_update_layer(struct shell_surface *shsurf)
2244{
2245 struct wl_list *new_layer_link;
2246
2247 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2248
2249 if (new_layer_link == &shsurf->view->layer_link)
2250 return;
2251
2252 weston_view_geometry_dirty(shsurf->view);
2253 wl_list_remove(&shsurf->view->layer_link);
2254 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2255 weston_view_geometry_dirty(shsurf->view);
2256 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002257
2258 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002259}
2260
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002261static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002262shell_surface_set_parent(struct shell_surface *shsurf,
2263 struct weston_surface *parent)
2264{
2265 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002266
2267 wl_list_remove(&shsurf->children_link);
2268 wl_list_init(&shsurf->children_link);
2269
2270 /* Insert into the parent surface’s child list. */
2271 if (parent != NULL) {
2272 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2273 if (parent_shsurf != NULL)
2274 wl_list_insert(&parent_shsurf->children_list,
2275 &shsurf->children_link);
2276 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002277}
2278
2279static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002280shell_surface_set_output(struct shell_surface *shsurf,
2281 struct weston_output *output)
2282{
2283 struct weston_surface *es = shsurf->surface;
2284
2285 /* get the default output, if the client set it as NULL
2286 check whether the ouput is available */
2287 if (output)
2288 shsurf->output = output;
2289 else if (es->output)
2290 shsurf->output = es->output;
2291 else
2292 shsurf->output = get_default_output(es->compositor);
2293}
2294
2295static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002296surface_clear_next_states(struct shell_surface *shsurf)
2297{
2298 shsurf->next_state.maximized = false;
2299 shsurf->next_state.fullscreen = false;
2300
2301 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2302 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2303 shsurf->state_changed = true;
2304}
2305
2306static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002307set_toplevel(struct shell_surface *shsurf)
2308{
2309 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002310
2311 /* The layer_link is updated in set_surface_type(),
2312 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002313}
2314
2315static void
2316shell_surface_set_toplevel(struct wl_client *client,
2317 struct wl_resource *resource)
2318{
2319 struct shell_surface *surface = wl_resource_get_user_data(resource);
2320
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002321 shell_surface_set_parent(surface, NULL);
2322
Rafael Antognolli03b16592013-12-03 15:35:42 -02002323 surface_clear_next_states(surface);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002324 set_toplevel(surface);
2325}
2326
2327static void
2328set_transient(struct shell_surface *shsurf,
2329 struct weston_surface *parent, int x, int y, uint32_t flags)
2330{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002331 assert(parent != NULL);
2332
Philip Withnallbecb77e2013-11-25 18:01:30 +00002333 shsurf->transient.x = x;
2334 shsurf->transient.y = y;
2335 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002336
Rafael Antognollied207b42013-12-03 15:35:43 -02002337 shsurf->next_state.relative = true;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002338
2339 /* The layer_link is updated in set_surface_type(),
2340 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002341}
2342
2343static void
2344shell_surface_set_transient(struct wl_client *client,
2345 struct wl_resource *resource,
2346 struct wl_resource *parent_resource,
2347 int x, int y, uint32_t flags)
2348{
2349 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2350 struct weston_surface *parent =
2351 wl_resource_get_user_data(parent_resource);
2352
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002353 shell_surface_set_parent(shsurf, parent);
2354
Rafael Antognolli03b16592013-12-03 15:35:42 -02002355 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002356 set_transient(shsurf, parent, x, y, flags);
2357}
2358
2359static void
2360set_fullscreen(struct shell_surface *shsurf,
2361 uint32_t method,
2362 uint32_t framerate,
2363 struct weston_output *output)
2364{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002365 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002366
2367 shsurf->fullscreen_output = shsurf->output;
2368 shsurf->fullscreen.type = method;
2369 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002370
Rafael Antognolli03b16592013-12-03 15:35:42 -02002371 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002372
2373 shsurf->client->send_configure(shsurf->surface, 0,
2374 shsurf->output->width,
2375 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002376
2377 /* The layer_link is updated in set_surface_type(),
2378 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002379}
2380
2381static void
2382unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002383{
Philip Withnallf85fe842013-11-25 18:01:37 +00002384 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002385 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2386 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002387 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002388 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002389 shsurf->fullscreen_output = NULL;
2390
Alex Wu4539b082012-03-01 12:57:46 +08002391 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2392 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002393
Alex Wu4539b082012-03-01 12:57:46 +08002394 wl_list_remove(&shsurf->fullscreen.transform.link);
2395 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002396
Jason Ekstranda7af7042013-10-12 22:38:11 -05002397 if (shsurf->fullscreen.black_view)
2398 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2399 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002400
Jason Ekstranda7af7042013-10-12 22:38:11 -05002401 weston_view_set_position(shsurf->view,
2402 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002403 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002404 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002405 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002406 shsurf->saved_rotation_valid = false;
2407 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002408
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002409 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002410}
2411
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002412static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002413shell_surface_set_fullscreen(struct wl_client *client,
2414 struct wl_resource *resource,
2415 uint32_t method,
2416 uint32_t framerate,
2417 struct wl_resource *output_resource)
2418{
2419 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2420 struct weston_output *output;
2421
2422 if (output_resource)
2423 output = wl_resource_get_user_data(output_resource);
2424 else
2425 output = NULL;
2426
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002427 shell_surface_set_parent(shsurf, NULL);
2428
Rafael Antognolli03b16592013-12-03 15:35:42 -02002429 surface_clear_next_states(shsurf);
2430 shsurf->next_state.fullscreen = true;
2431 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002432 set_fullscreen(shsurf, method, framerate, output);
2433}
2434
2435static void
2436set_popup(struct shell_surface *shsurf,
2437 struct weston_surface *parent,
2438 struct weston_seat *seat,
2439 uint32_t serial,
2440 int32_t x,
2441 int32_t y)
2442{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002443 assert(parent != NULL);
2444
Philip Withnallbecb77e2013-11-25 18:01:30 +00002445 shsurf->popup.shseat = get_shell_seat(seat);
2446 shsurf->popup.serial = serial;
2447 shsurf->popup.x = x;
2448 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002449
Philip Withnallb995e1d2013-11-25 18:01:39 +00002450 shsurf->next_type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002451}
2452
2453static void
2454shell_surface_set_popup(struct wl_client *client,
2455 struct wl_resource *resource,
2456 struct wl_resource *seat_resource,
2457 uint32_t serial,
2458 struct wl_resource *parent_resource,
2459 int32_t x, int32_t y, uint32_t flags)
2460{
2461 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002462 struct weston_surface *parent =
2463 wl_resource_get_user_data(parent_resource);
2464
2465 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002466
Rafael Antognolli03b16592013-12-03 15:35:42 -02002467 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002468 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002469 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002470 wl_resource_get_user_data(seat_resource),
2471 serial, x, y);
2472}
2473
2474static void
2475set_maximized(struct shell_surface *shsurf,
2476 struct weston_output *output)
2477{
2478 struct desktop_shell *shell;
2479 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002480
Philip Withnall352e7ed2013-11-25 18:01:35 +00002481 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002482
2483 shell = shell_surface_get_shell(shsurf);
2484 panel_height = get_output_panel_height(shell, shsurf->output);
2485 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2486
2487 shsurf->client->send_configure(shsurf->surface, edges,
2488 shsurf->output->width,
2489 shsurf->output->height - panel_height);
2490
Rafael Antognolli03b16592013-12-03 15:35:42 -02002491 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002492}
2493
2494static void
2495unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002496{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002497 /* undo all maximized things here */
2498 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002499 weston_view_set_position(shsurf->view,
2500 shsurf->saved_x,
2501 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002502
2503 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002504 wl_list_insert(&shsurf->view->geometry.transformation_list,
2505 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002506 shsurf->saved_rotation_valid = false;
2507 }
2508
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002509 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002510}
2511
Philip Withnallbecb77e2013-11-25 18:01:30 +00002512static void
2513shell_surface_set_maximized(struct wl_client *client,
2514 struct wl_resource *resource,
2515 struct wl_resource *output_resource)
2516{
2517 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2518 struct weston_output *output;
2519
2520 if (output_resource)
2521 output = wl_resource_get_user_data(output_resource);
2522 else
2523 output = NULL;
2524
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002525 shell_surface_set_parent(shsurf, NULL);
2526
Rafael Antognolli03b16592013-12-03 15:35:42 -02002527 surface_clear_next_states(shsurf);
2528 shsurf->next_state.maximized = true;
2529 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002530 set_maximized(shsurf, output);
2531}
2532
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002533/* This is only ever called from set_surface_type(), so there’s no need to
2534 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002535static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002536reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002537{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002538 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002539 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002540 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002541 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002542
2543 surface->type = SHELL_SURFACE_NONE;
2544 return 0;
2545}
2546
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002547static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002548set_full_output(struct shell_surface *shsurf)
2549{
2550 shsurf->saved_x = shsurf->view->geometry.x;
2551 shsurf->saved_y = shsurf->view->geometry.y;
2552 shsurf->saved_position_valid = true;
2553
2554 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2555 wl_list_remove(&shsurf->rotation.transform.link);
2556 wl_list_init(&shsurf->rotation.transform.link);
2557 weston_view_geometry_dirty(shsurf->view);
2558 shsurf->saved_rotation_valid = true;
2559 }
2560}
2561
2562static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002563set_surface_type(struct shell_surface *shsurf)
2564{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002565 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002567
Philip Withnallbecb77e2013-11-25 18:01:30 +00002568 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002569
2570 shsurf->type = shsurf->next_type;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002571 shsurf->state = shsurf->next_state;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002572 shsurf->next_type = SHELL_SURFACE_NONE;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002573 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002574
2575 switch (shsurf->type) {
2576 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002577 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002578 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002579 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002580 weston_view_set_position(shsurf->view,
2581 pev->geometry.x + shsurf->transient.x,
2582 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002583 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002584
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002585 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002586 weston_view_set_position(shsurf->view, shsurf->transient.x,
2587 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002588 break;
2589
Philip Withnall0f640e22013-11-25 18:01:31 +00002590 case SHELL_SURFACE_POPUP:
2591 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002592 default:
2593 break;
2594 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002595
2596 /* Update the surface’s layer. */
2597 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002598}
2599
Tiago Vignattibe143262012-04-16 17:31:41 +03002600static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002601shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002602{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002603 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002604}
2605
Alex Wu21858432012-04-01 20:13:08 +08002606static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002607black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002608
Jason Ekstranda7af7042013-10-12 22:38:11 -05002609static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002610create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002611 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002612 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002613{
2614 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002615 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002616
2617 surface = weston_surface_create(ec);
2618 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002619 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002620 return NULL;
2621 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002622 view = weston_view_create(surface);
2623 if (surface == NULL) {
2624 weston_log("no memory\n");
2625 weston_surface_destroy(surface);
2626 return NULL;
2627 }
Alex Wu4539b082012-03-01 12:57:46 +08002628
Alex Wu21858432012-04-01 20:13:08 +08002629 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002630 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002631 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002632 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002633 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002634 pixman_region32_fini(&surface->input);
2635 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002636
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002637 surface->width = w;
2638 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002639 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002640
2641 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002642}
2643
Philip Withnalled8f1a92013-11-25 18:01:43 +00002644static void
2645shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2646{
2647 struct weston_output *output = shsurf->fullscreen_output;
2648
Rafael Antognolli03b16592013-12-03 15:35:42 -02002649 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002650
2651 if (!shsurf->fullscreen.black_view)
2652 shsurf->fullscreen.black_view =
2653 create_black_surface(shsurf->surface->compositor,
2654 shsurf->surface,
2655 output->x, output->y,
2656 output->width,
2657 output->height);
2658
2659 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2660 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2661 wl_list_insert(&shsurf->view->layer_link,
2662 &shsurf->fullscreen.black_view->layer_link);
2663 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2664 weston_surface_damage(shsurf->surface);
2665}
2666
Alex Wu4539b082012-03-01 12:57:46 +08002667/* Create black surface and append it to the associated fullscreen surface.
2668 * Handle size dismatch and positioning according to the method. */
2669static void
2670shell_configure_fullscreen(struct shell_surface *shsurf)
2671{
2672 struct weston_output *output = shsurf->fullscreen_output;
2673 struct weston_surface *surface = shsurf->surface;
2674 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002675 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002676 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002677
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002678 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2679 restore_output_mode(output);
2680
Philip Withnalled8f1a92013-11-25 18:01:43 +00002681 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002682
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002684 &surf_width, &surf_height);
2685
Alex Wu4539b082012-03-01 12:57:46 +08002686 switch (shsurf->fullscreen.type) {
2687 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002688 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002690 break;
2691 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002692 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002693 if (output->width == surf_width &&
2694 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 weston_view_set_position(shsurf->view,
2696 output->x - surf_x,
2697 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002698 break;
2699 }
2700
Alex Wu4539b082012-03-01 12:57:46 +08002701 matrix = &shsurf->fullscreen.transform.matrix;
2702 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002703
Scott Moreau1bad5db2012-08-18 01:04:05 -06002704 output_aspect = (float) output->width /
2705 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002706 /* XXX: Use surf_width and surf_height here? */
2707 surface_aspect = (float) surface->width /
2708 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002709 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002710 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002711 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002712 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002713 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002714 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002715
Alex Wu4539b082012-03-01 12:57:46 +08002716 weston_matrix_scale(matrix, scale, scale, 1);
2717 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002718 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002719 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002720 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2721 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002722 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002723
Alex Wu4539b082012-03-01 12:57:46 +08002724 break;
2725 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002726 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002727 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002728 surf_width * surface->buffer_viewport.scale,
2729 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002730 shsurf->fullscreen.framerate};
2731
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002732 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002733 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002734 weston_view_set_position(shsurf->view,
2735 output->x - surf_x,
2736 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002737 shsurf->fullscreen.black_view->surface->width = output->width;
2738 shsurf->fullscreen.black_view->surface->height = output->height;
2739 weston_view_set_position(shsurf->fullscreen.black_view,
2740 output->x - surf_x,
2741 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002742 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002743 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002744 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002745 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002746 }
Alex Wubd3354b2012-04-17 17:20:49 +08002747 }
Alex Wu4539b082012-03-01 12:57:46 +08002748 break;
2749 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002750 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002751 break;
2752 default:
2753 break;
2754 }
2755}
2756
Alex Wu4539b082012-03-01 12:57:46 +08002757static void
2758shell_map_fullscreen(struct shell_surface *shsurf)
2759{
Alex Wubd3354b2012-04-17 17:20:49 +08002760 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002761}
2762
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002763static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002764set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2765{
2766 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002767 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002768 shsurf->transient.x = x;
2769 shsurf->transient.y = y;
2770 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002771
2772 shell_surface_set_parent(shsurf, NULL);
2773
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002774 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2775}
2776
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002777static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002778
2779static void
2780destroy_shell_seat(struct wl_listener *listener, void *data)
2781{
2782 struct shell_seat *shseat =
2783 container_of(listener,
2784 struct shell_seat, seat_destroy_listener);
2785 struct shell_surface *shsurf, *prev = NULL;
2786
2787 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002788 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002789 shseat->popup_grab.client = NULL;
2790
2791 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2792 shsurf->popup.shseat = NULL;
2793 if (prev) {
2794 wl_list_init(&prev->popup.grab_link);
2795 }
2796 prev = shsurf;
2797 }
2798 wl_list_init(&prev->popup.grab_link);
2799 }
2800
2801 wl_list_remove(&shseat->seat_destroy_listener.link);
2802 free(shseat);
2803}
2804
2805static struct shell_seat *
2806create_shell_seat(struct weston_seat *seat)
2807{
2808 struct shell_seat *shseat;
2809
2810 shseat = calloc(1, sizeof *shseat);
2811 if (!shseat) {
2812 weston_log("no memory to allocate shell seat\n");
2813 return NULL;
2814 }
2815
2816 shseat->seat = seat;
2817 wl_list_init(&shseat->popup_grab.surfaces_list);
2818
2819 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2820 wl_signal_add(&seat->destroy_signal,
2821 &shseat->seat_destroy_listener);
2822
2823 return shseat;
2824}
2825
2826static struct shell_seat *
2827get_shell_seat(struct weston_seat *seat)
2828{
2829 struct wl_listener *listener;
2830
2831 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2832 if (listener == NULL)
2833 return create_shell_seat(seat);
2834
2835 return container_of(listener,
2836 struct shell_seat, seat_destroy_listener);
2837}
2838
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002839static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002840popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002841{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002842 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002843 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002844 struct shell_seat *shseat =
2845 container_of(grab, struct shell_seat, popup_grab.grab);
2846 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002847 wl_fixed_t sx, sy;
2848
Jason Ekstranda7af7042013-10-12 22:38:11 -05002849 view = weston_compositor_pick_view(pointer->seat->compositor,
2850 pointer->x, pointer->y,
2851 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002852
Jason Ekstranda7af7042013-10-12 22:38:11 -05002853 if (view && view->surface->resource &&
2854 wl_resource_get_client(view->surface->resource) == client) {
2855 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002856 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002857 weston_pointer_set_focus(pointer, NULL,
2858 wl_fixed_from_int(0),
2859 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002860 }
2861}
2862
2863static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002864popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2865 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002866{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002867 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002868 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002869 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002870
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002871 weston_pointer_move(pointer, x, y);
2872
Neil Roberts96d790e2013-09-19 17:32:00 +01002873 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002874 weston_view_from_global_fixed(pointer->focus,
2875 pointer->x, pointer->y,
2876 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002877 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002878 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002879}
2880
2881static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002882popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002883 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002884{
2885 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002886 struct shell_seat *shseat =
2887 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002888 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002889 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002890 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002891 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002892
Neil Roberts96d790e2013-09-19 17:32:00 +01002893 resource_list = &grab->pointer->focus_resource_list;
2894 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002895 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002896 wl_resource_for_each(resource, resource_list) {
2897 wl_pointer_send_button(resource, serial,
2898 time, button, state);
2899 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002900 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002901 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002902 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002903 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002904 }
2905
Daniel Stone4dbadb12012-05-30 16:31:51 +01002906 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002907 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002908}
2909
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002910static void
2911popup_grab_cancel(struct weston_pointer_grab *grab)
2912{
2913 popup_grab_end(grab->pointer);
2914}
2915
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002916static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002917 popup_grab_focus,
2918 popup_grab_motion,
2919 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002920 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002921};
2922
2923static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002924shell_surface_send_popup_done(struct shell_surface *shsurf)
2925{
2926 if (shell_surface_is_wl_shell_surface(shsurf))
2927 wl_shell_surface_send_popup_done(shsurf->resource);
2928 else if (shell_surface_is_xdg_popup(shsurf))
2929 xdg_popup_send_popup_done(shsurf->resource,
2930 shsurf->popup.serial);
2931}
2932
2933static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002934popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002935{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002936 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002937 struct shell_seat *shseat =
2938 container_of(grab, struct shell_seat, popup_grab.grab);
2939 struct shell_surface *shsurf;
2940 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002941
2942 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002943 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002944 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002945 shseat->popup_grab.grab.interface = NULL;
2946 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002947 /* Send the popup_done event to all the popups open */
2948 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002949 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002950 shsurf->popup.shseat = NULL;
2951 if (prev) {
2952 wl_list_init(&prev->popup.grab_link);
2953 }
2954 prev = shsurf;
2955 }
2956 wl_list_init(&prev->popup.grab_link);
2957 wl_list_init(&shseat->popup_grab.surfaces_list);
2958 }
2959}
2960
2961static void
2962add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2963{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002964 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002965
2966 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002967 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002968 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002969 /* We must make sure here that this popup was opened after
2970 * a mouse press, and not just by moving around with other
2971 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002972 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002973 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002974
Rob Bradforddfe31052013-06-26 19:49:11 +01002975 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002976 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002977 } else {
2978 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002979 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002980}
2981
2982static void
2983remove_popup_grab(struct shell_surface *shsurf)
2984{
2985 struct shell_seat *shseat = shsurf->popup.shseat;
2986
2987 wl_list_remove(&shsurf->popup.grab_link);
2988 wl_list_init(&shsurf->popup.grab_link);
2989 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002990 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002991 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002992 }
2993}
2994
2995static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002996shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002997{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002998 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002999 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003000
Jason Ekstranda7af7042013-10-12 22:38:11 -05003001 shsurf->surface->output = parent_view->output;
3002 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003003
Jason Ekstranda7af7042013-10-12 22:38:11 -05003004 weston_view_set_transform_parent(shsurf->view, parent_view);
3005 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3006 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003007
Kristian Høgsberge3148752013-05-06 23:19:49 -04003008 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003009 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003010 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003011 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003012 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003013 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003014}
3015
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003016static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003017 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003018 shell_surface_move,
3019 shell_surface_resize,
3020 shell_surface_set_toplevel,
3021 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003022 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003023 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003024 shell_surface_set_maximized,
3025 shell_surface_set_title,
3026 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003027};
3028
3029static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003030destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003031{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003032 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3033
Giulio Camuffo5085a752013-03-25 21:42:45 +01003034 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3035 remove_popup_grab(shsurf);
3036 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003037
Alex Wubd3354b2012-04-17 17:20:49 +08003038 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003039 shell_surface_is_top_fullscreen(shsurf))
3040 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003041
Jason Ekstranda7af7042013-10-12 22:38:11 -05003042 if (shsurf->fullscreen.black_view)
3043 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003044
Alex Wubd3354b2012-04-17 17:20:49 +08003045 /* As destroy_resource() use wl_list_for_each_safe(),
3046 * we can always remove the listener.
3047 */
3048 wl_list_remove(&shsurf->surface_destroy_listener.link);
3049 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06003050 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07003051 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003052
Jason Ekstranda7af7042013-10-12 22:38:11 -05003053 weston_view_destroy(shsurf->view);
3054
Philip Withnall648a4dd2013-11-25 18:01:44 +00003055 wl_list_remove(&shsurf->children_link);
3056
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003057 wl_list_remove(&shsurf->link);
3058 free(shsurf);
3059}
3060
3061static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003062shell_destroy_shell_surface(struct wl_resource *resource)
3063{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003064 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003065
3066 destroy_shell_surface(shsurf);
3067}
3068
3069static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003070shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003071{
3072 struct shell_surface *shsurf = container_of(listener,
3073 struct shell_surface,
3074 surface_destroy_listener);
3075
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003076 if (shsurf->resource)
3077 wl_resource_destroy(shsurf->resource);
3078 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003079 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003080}
3081
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003082static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003083shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003084
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003085static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003086get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003087{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003088 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003089 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003090 else
3091 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003092}
3093
Rafael Antognollie2a34552013-12-03 15:35:45 -02003094static struct shell_surface *
3095create_common_surface(void *shell, struct weston_surface *surface,
3096 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003097{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003098 struct shell_surface *shsurf;
3099
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003100 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003101 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003102 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003103 }
3104
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003105 shsurf = calloc(1, sizeof *shsurf);
3106 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003107 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003108 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003109 }
3110
Jason Ekstranda7af7042013-10-12 22:38:11 -05003111 shsurf->view = weston_view_create(surface);
3112 if (!shsurf->view) {
3113 weston_log("no memory to allocate shell surface\n");
3114 free(shsurf);
3115 return NULL;
3116 }
3117
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003118 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003119 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003120
Tiago Vignattibc052c92012-04-19 16:18:18 +03003121 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003122 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003123 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003124 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003125 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003126 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3127 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003128 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003129 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003130 wl_list_init(&shsurf->fullscreen.transform.link);
3131
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003132 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003133 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003134 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003135 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003136
3137 /* init link so its safe to always remove it in destroy_shell_surface */
3138 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003139 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003140
Pekka Paalanen460099f2012-01-20 16:48:25 +02003141 /* empty when not in use */
3142 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003143 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003144
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003145 wl_list_init(&shsurf->workspace_transform.link);
3146
Philip Withnall648a4dd2013-11-25 18:01:44 +00003147 wl_list_init(&shsurf->children_link);
3148 wl_list_init(&shsurf->children_list);
3149 shsurf->parent = NULL;
3150
Pekka Paalanen98262232011-12-01 10:42:22 +02003151 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003152 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003153
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003154 shsurf->client = client;
3155
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003156 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003157}
3158
Rafael Antognollie2a34552013-12-03 15:35:45 -02003159static struct shell_surface *
3160create_shell_surface(void *shell, struct weston_surface *surface,
3161 const struct weston_shell_client *client)
3162{
3163 struct shell_surface *shsurf;
3164 shsurf = create_common_surface(shell, surface, client);
3165
3166 shsurf->type = SHELL_SURFACE_NONE;
3167 shsurf->next_type = SHELL_SURFACE_NONE;
3168
3169 return shsurf;
3170}
3171
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172static struct weston_view *
3173get_primary_view(void *shell, struct shell_surface *shsurf)
3174{
3175 return shsurf->view;
3176}
3177
Tiago Vignattibc052c92012-04-19 16:18:18 +03003178static void
3179shell_get_shell_surface(struct wl_client *client,
3180 struct wl_resource *resource,
3181 uint32_t id,
3182 struct wl_resource *surface_resource)
3183{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003184 struct weston_surface *surface =
3185 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003186 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003187 struct shell_surface *shsurf;
3188
3189 if (get_shell_surface(surface)) {
3190 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003191 WL_DISPLAY_ERROR_INVALID_OBJECT,
3192 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003193 return;
3194 }
3195
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003196 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003197 if (!shsurf) {
3198 wl_resource_post_error(surface_resource,
3199 WL_DISPLAY_ERROR_INVALID_OBJECT,
3200 "surface->configure already set");
3201 return;
3202 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003203
Jason Ekstranda85118c2013-06-27 20:17:02 -05003204 shsurf->resource =
3205 wl_resource_create(client,
3206 &wl_shell_surface_interface, 1, id);
3207 wl_resource_set_implementation(shsurf->resource,
3208 &shell_surface_implementation,
3209 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003210}
3211
Rafael Antognollie2a34552013-12-03 15:35:45 -02003212static bool
3213shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3214{
3215 return wl_resource_instance_of(shsurf->resource,
3216 &wl_shell_surface_interface,
3217 &shell_surface_implementation);
3218}
3219
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003220static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003221 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003222};
3223
Rafael Antognollie2a34552013-12-03 15:35:45 -02003224/****************************
3225 * xdg-shell implementation */
3226
3227static void
3228xdg_surface_destroy(struct wl_client *client,
3229 struct wl_resource *resource)
3230{
3231 wl_resource_destroy(resource);
3232}
3233
3234static void
3235xdg_surface_pong(struct wl_client *client,
3236 struct wl_resource *resource,
3237 uint32_t serial)
3238{
3239 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3240
3241 surface_pong(shsurf, serial);
3242}
3243
3244static void
3245xdg_surface_set_app_id(struct wl_client *client,
3246 struct wl_resource *resource,
3247 const char *app_id)
3248{
3249 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3250
3251 free(shsurf->class);
3252 shsurf->class = strdup(app_id);
3253}
3254
3255static void
3256xdg_surface_set_title(struct wl_client *client,
3257 struct wl_resource *resource, const char *title)
3258{
3259 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3260
3261 set_title(shsurf, title);
3262}
3263
3264static void
3265xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3266 struct wl_resource *seat_resource, uint32_t serial)
3267{
3268 common_surface_move(resource, seat_resource, serial);
3269}
3270
3271static void
3272xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3273 struct wl_resource *seat_resource, uint32_t serial,
3274 uint32_t edges)
3275{
3276 common_surface_resize(resource, seat_resource, serial, edges);
3277}
3278
3279static void
3280xdg_surface_set_output(struct wl_client *client,
3281 struct wl_resource *resource,
3282 struct wl_resource *output_resource)
3283{
3284 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3285 struct weston_output *output;
3286
3287 if (output_resource)
3288 output = wl_resource_get_user_data(output_resource);
3289 else
3290 output = NULL;
3291
3292 if (!weston_surface_is_mapped(shsurf->surface))
3293 shsurf->output = output;
3294}
3295
3296static void
3297xdg_surface_set_fullscreen(struct wl_client *client,
3298 struct wl_resource *resource)
3299{
3300 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3301
3302 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3303 return;
3304
3305 if (!shsurf->next_state.fullscreen) {
3306 shsurf->next_state.fullscreen = true;
3307 shsurf->state_changed = true;
3308 set_fullscreen(shsurf,
3309 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3310 0, shsurf->output);
3311 }
3312}
3313
3314static void
3315xdg_surface_unset_fullscreen(struct wl_client *client,
3316 struct wl_resource *resource)
3317{
3318 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3319
3320 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3321 return;
3322
3323 shsurf->next_state.fullscreen = false;
3324 shsurf->state_changed = true;
3325}
3326
3327static void
3328xdg_surface_set_maximized(struct wl_client *client,
3329 struct wl_resource *resource)
3330{
3331 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3332
3333 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3334 return;
3335
3336 if (!shsurf->next_state.maximized) {
3337 shsurf->next_state.maximized = true;
3338 shsurf->state_changed = true;
3339 set_maximized(shsurf, shsurf->output);
3340 }
3341}
3342
3343static void
3344xdg_surface_unset_maximized(struct wl_client *client,
3345 struct wl_resource *resource)
3346{
3347 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3348
3349 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3350 return;
3351
3352 shsurf->next_state.maximized = false;
3353 shsurf->state_changed = true;
3354}
3355
3356static const struct xdg_surface_interface xdg_surface_implementation = {
3357 xdg_surface_destroy,
3358 xdg_surface_set_transient_for,
3359 xdg_surface_set_title,
3360 xdg_surface_set_app_id,
3361 xdg_surface_pong,
3362 xdg_surface_move,
3363 xdg_surface_resize,
3364 xdg_surface_set_output,
3365 xdg_surface_set_fullscreen,
3366 xdg_surface_unset_fullscreen,
3367 xdg_surface_set_maximized,
3368 xdg_surface_unset_maximized,
3369 NULL /* set_minimized */
3370};
3371
3372static void
3373xdg_send_configure(struct weston_surface *surface,
3374 uint32_t edges, int32_t width, int32_t height)
3375{
3376 struct shell_surface *shsurf = get_shell_surface(surface);
3377
3378 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3379}
3380
3381static const struct weston_shell_client xdg_client = {
3382 xdg_send_configure
3383};
3384
3385static void
3386xdg_use_unstable_version(struct wl_client *client,
3387 struct wl_resource *resource,
3388 int32_t version)
3389{
3390 if (version > 1) {
3391 wl_resource_post_error(resource,
3392 1,
3393 "xdg-shell:: version not implemented yet.");
3394 return;
3395 }
3396}
3397
3398static struct shell_surface *
3399create_xdg_surface(void *shell, struct weston_surface *surface,
3400 const struct weston_shell_client *client)
3401{
3402 struct shell_surface *shsurf;
3403 shsurf = create_common_surface(shell, surface, client);
3404
3405 shsurf->type = SHELL_SURFACE_NONE;
3406 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
3407
3408 return shsurf;
3409}
3410
3411static void
3412xdg_get_xdg_surface(struct wl_client *client,
3413 struct wl_resource *resource,
3414 uint32_t id,
3415 struct wl_resource *surface_resource)
3416{
3417 struct weston_surface *surface =
3418 wl_resource_get_user_data(surface_resource);
3419 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3420 struct shell_surface *shsurf;
3421
3422 if (get_shell_surface(surface)) {
3423 wl_resource_post_error(surface_resource,
3424 WL_DISPLAY_ERROR_INVALID_OBJECT,
3425 "desktop_shell::get_shell_surface already requested");
3426 return;
3427 }
3428
3429 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3430 if (!shsurf) {
3431 wl_resource_post_error(surface_resource,
3432 WL_DISPLAY_ERROR_INVALID_OBJECT,
3433 "surface->configure already set");
3434 return;
3435 }
3436
3437 shsurf->resource =
3438 wl_resource_create(client,
3439 &xdg_surface_interface, 1, id);
3440 wl_resource_set_implementation(shsurf->resource,
3441 &xdg_surface_implementation,
3442 shsurf, shell_destroy_shell_surface);
3443}
3444
3445static bool
3446shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3447{
3448 return wl_resource_instance_of(shsurf->resource,
3449 &xdg_surface_interface,
3450 &xdg_surface_implementation);
3451}
3452
3453/* xdg-popup implementation */
3454
3455static void
3456xdg_popup_destroy(struct wl_client *client,
3457 struct wl_resource *resource)
3458{
3459 wl_resource_destroy(resource);
3460}
3461
3462static void
3463xdg_popup_pong(struct wl_client *client,
3464 struct wl_resource *resource,
3465 uint32_t serial)
3466{
3467 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3468
3469 surface_pong(shsurf, serial);
3470}
3471
3472static const struct xdg_popup_interface xdg_popup_implementation = {
3473 xdg_popup_destroy,
3474 xdg_popup_pong
3475};
3476
3477static void
3478xdg_popup_send_configure(struct weston_surface *surface,
3479 uint32_t edges, int32_t width, int32_t height)
3480{
3481}
3482
3483static const struct weston_shell_client xdg_popup_client = {
3484 xdg_popup_send_configure
3485};
3486
3487static struct shell_surface *
3488create_xdg_popup(void *shell, struct weston_surface *surface,
3489 const struct weston_shell_client *client,
3490 struct weston_surface *parent,
3491 struct shell_seat *seat,
3492 uint32_t serial,
3493 int32_t x, int32_t y)
3494{
3495 struct shell_surface *shsurf;
3496 shsurf = create_common_surface(shell, surface, client);
3497
3498 shsurf->type = SHELL_SURFACE_NONE;
3499 shsurf->next_type = SHELL_SURFACE_POPUP;
3500 shsurf->popup.shseat = seat;
3501 shsurf->popup.serial = serial;
3502 shsurf->popup.x = x;
3503 shsurf->popup.y = y;
3504 shell_surface_set_parent(shsurf, parent);
3505
3506 return shsurf;
3507}
3508
3509static void
3510xdg_get_xdg_popup(struct wl_client *client,
3511 struct wl_resource *resource,
3512 uint32_t id,
3513 struct wl_resource *surface_resource,
3514 struct wl_resource *parent_resource,
3515 struct wl_resource *seat_resource,
3516 uint32_t serial,
3517 int32_t x, int32_t y, uint32_t flags)
3518{
3519 struct weston_surface *surface =
3520 wl_resource_get_user_data(surface_resource);
3521 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3522 struct shell_surface *shsurf;
3523 struct weston_surface *parent;
3524 struct shell_seat *seat;
3525
3526 if (get_shell_surface(surface)) {
3527 wl_resource_post_error(surface_resource,
3528 WL_DISPLAY_ERROR_INVALID_OBJECT,
3529 "desktop_shell::get_shell_surface already requested");
3530 return;
3531 }
3532
3533 if (!parent_resource) {
3534 wl_resource_post_error(surface_resource,
3535 WL_DISPLAY_ERROR_INVALID_OBJECT,
3536 "xdg_shell::get_xdg_popup requires a parent shell surface");
3537 }
3538
3539 parent = wl_resource_get_user_data(parent_resource);
3540 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3541
3542 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3543 parent, seat, serial, x, y);
3544 if (!shsurf) {
3545 wl_resource_post_error(surface_resource,
3546 WL_DISPLAY_ERROR_INVALID_OBJECT,
3547 "surface->configure already set");
3548 return;
3549 }
3550
3551 shsurf->resource =
3552 wl_resource_create(client,
3553 &xdg_popup_interface, 1, id);
3554 wl_resource_set_implementation(shsurf->resource,
3555 &xdg_popup_implementation,
3556 shsurf, shell_destroy_shell_surface);
3557}
3558
3559static bool
3560shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3561{
3562 return wl_resource_instance_of(shsurf->resource,
3563 &xdg_popup_interface,
3564 &xdg_popup_implementation);
3565}
3566
3567static const struct xdg_shell_interface xdg_implementation = {
3568 xdg_use_unstable_version,
3569 xdg_get_xdg_surface,
3570 xdg_get_xdg_popup
3571};
3572
3573static int
3574xdg_shell_unversioned_dispatch(const void *implementation,
3575 void *_target, uint32_t opcode,
3576 const struct wl_message *message,
3577 union wl_argument *args)
3578{
3579 struct wl_resource *resource = _target;
3580 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3581
3582 if (opcode != 0) {
3583 wl_resource_post_error(resource,
3584 WL_DISPLAY_ERROR_INVALID_OBJECT,
3585 "must call use_unstable_version first");
3586 return 0;
3587 }
3588
3589#define XDG_SERVER_VERSION 1
3590
3591 if (args[0].i != XDG_SERVER_VERSION) {
3592 wl_resource_post_error(resource,
3593 WL_DISPLAY_ERROR_INVALID_OBJECT,
3594 "incompatible version, server is %d "
3595 "client wants %d",
3596 XDG_SERVER_VERSION, args[0].i);
3597 return 0;
3598 }
3599
3600 wl_resource_set_implementation(resource, &xdg_implementation,
3601 shell, NULL);
3602
3603 return 1;
3604}
3605
3606/* end of xdg-shell implementation */
3607/***********************************/
3608
Kristian Høgsberg07937562011-04-12 17:25:42 -04003609static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003610shell_fade(struct desktop_shell *shell, enum fade_type type);
3611
3612static int
3613screensaver_timeout(void *data)
3614{
3615 struct desktop_shell *shell = data;
3616
3617 shell_fade(shell, FADE_OUT);
3618
3619 return 1;
3620}
3621
3622static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003623handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003624{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003625 struct desktop_shell *shell =
3626 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003627
Pekka Paalanen18027e52011-12-02 16:31:49 +02003628 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003629
3630 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003631 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003632}
3633
3634static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003635launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003636{
3637 if (shell->screensaver.binding)
3638 return;
3639
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003640 if (!shell->screensaver.path) {
3641 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003642 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003643 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003644
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003645 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003646 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003647 return;
3648 }
3649
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003650 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003651 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003652 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003653 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003654}
3655
3656static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003657terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003658{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003659 if (shell->screensaver.process.pid == 0)
3660 return;
3661
3662 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003663}
3664
3665static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003666configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003667{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003668 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003669
Jason Ekstranda7af7042013-10-12 22:38:11 -05003670 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3671 if (v->output == ev->output && v != ev) {
3672 weston_view_unmap(v);
3673 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003674 }
3675 }
3676
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003677 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003678
Jason Ekstranda7af7042013-10-12 22:38:11 -05003679 if (wl_list_empty(&ev->layer_link)) {
3680 wl_list_insert(&layer->view_list, &ev->layer_link);
3681 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003682 }
3683}
3684
3685static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003686background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003687{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003688 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003689 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003690
Jason Ekstranda7af7042013-10-12 22:38:11 -05003691 view = container_of(es->views.next, struct weston_view, surface_link);
3692
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003693 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003694}
3695
3696static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003697desktop_shell_set_background(struct wl_client *client,
3698 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003699 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003700 struct wl_resource *surface_resource)
3701{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003702 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003703 struct weston_surface *surface =
3704 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003705 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003706
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003707 if (surface->configure) {
3708 wl_resource_post_error(surface_resource,
3709 WL_DISPLAY_ERROR_INVALID_OBJECT,
3710 "surface role already assigned");
3711 return;
3712 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003713
Jason Ekstranda7af7042013-10-12 22:38:11 -05003714 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3715 weston_view_destroy(view);
3716 view = weston_view_create(surface);
3717
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003718 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003719 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003720 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003721 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003722 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003723 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003724 surface->output->width,
3725 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003726}
3727
3728static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003729panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003730{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003731 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003732 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003733
Jason Ekstranda7af7042013-10-12 22:38:11 -05003734 view = container_of(es->views.next, struct weston_view, surface_link);
3735
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003736 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003737}
3738
3739static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003740desktop_shell_set_panel(struct wl_client *client,
3741 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003742 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003743 struct wl_resource *surface_resource)
3744{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003745 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003746 struct weston_surface *surface =
3747 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003748 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003749
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003750 if (surface->configure) {
3751 wl_resource_post_error(surface_resource,
3752 WL_DISPLAY_ERROR_INVALID_OBJECT,
3753 "surface role already assigned");
3754 return;
3755 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003756
Jason Ekstranda7af7042013-10-12 22:38:11 -05003757 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3758 weston_view_destroy(view);
3759 view = weston_view_create(surface);
3760
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003761 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003762 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003763 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003764 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003765 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003766 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003767 surface->output->width,
3768 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003769}
3770
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003771static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003772lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003773{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003774 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003775 struct weston_view *view;
3776
3777 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003778
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003779 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003780 return;
3781
Jason Ekstranda7af7042013-10-12 22:38:11 -05003782 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003783
3784 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003785 wl_list_insert(&shell->lock_layer.view_list,
3786 &view->layer_link);
3787 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003788 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003789 }
3790}
3791
3792static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003793handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003794{
Tiago Vignattibe143262012-04-16 17:31:41 +03003795 struct desktop_shell *shell =
3796 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003797
Martin Minarik6d118362012-06-07 18:01:59 +02003798 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003799 shell->lock_surface = NULL;
3800}
3801
3802static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003803desktop_shell_set_lock_surface(struct wl_client *client,
3804 struct wl_resource *resource,
3805 struct wl_resource *surface_resource)
3806{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003807 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003808 struct weston_surface *surface =
3809 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003810
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003811 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003812
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003813 if (!shell->locked)
3814 return;
3815
Pekka Paalanen98262232011-12-01 10:42:22 +02003816 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003817
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003818 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003819 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003820 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003821
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003822 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003823 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003824 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003825}
3826
3827static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003828resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003829{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003830 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003831
Pekka Paalanen77346a62011-11-30 16:26:35 +02003832 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003833
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003834 wl_list_remove(&shell->lock_layer.link);
3835 wl_list_insert(&shell->compositor->cursor_layer.link,
3836 &shell->fullscreen_layer.link);
3837 wl_list_insert(&shell->fullscreen_layer.link,
3838 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003839 if (shell->showing_input_panels) {
3840 wl_list_insert(&shell->panel_layer.link,
3841 &shell->input_panel_layer.link);
3842 wl_list_insert(&shell->input_panel_layer.link,
3843 &ws->layer.link);
3844 } else {
3845 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3846 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003847
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003848 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003849
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003850 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003851 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003852 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003853}
3854
3855static void
3856desktop_shell_unlock(struct wl_client *client,
3857 struct wl_resource *resource)
3858{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003859 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003860
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003861 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003862
3863 if (shell->locked)
3864 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003865}
3866
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003867static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003868desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003869 struct wl_resource *resource,
3870 struct wl_resource *surface_resource)
3871{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003872 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003873
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003874 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003875 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003876}
3877
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003878static void
3879desktop_shell_desktop_ready(struct wl_client *client,
3880 struct wl_resource *resource)
3881{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003882 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003883
3884 shell_fade_startup(shell);
3885}
3886
Kristian Høgsberg75840622011-09-06 13:48:16 -04003887static const struct desktop_shell_interface desktop_shell_implementation = {
3888 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003889 desktop_shell_set_panel,
3890 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003891 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003892 desktop_shell_set_grab_surface,
3893 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003894};
3895
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003896static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003897get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003898{
3899 struct shell_surface *shsurf;
3900
3901 shsurf = get_shell_surface(surface);
3902 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003903 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003904 return shsurf->type;
3905}
3906
Kristian Høgsberg75840622011-09-06 13:48:16 -04003907static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003908move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003909{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003910 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003911 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003912 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003913
Pekka Paalanen01388e22013-04-25 13:57:44 +03003914 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003915 if (surface == NULL)
3916 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003917
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003918 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003919 if (shsurf == NULL || shsurf->state.fullscreen ||
3920 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003921 return;
3922
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003923 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003924}
3925
3926static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003927touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3928{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003929 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003930 struct weston_surface *surface;
3931 struct shell_surface *shsurf;
3932
3933 surface = weston_surface_get_main_surface(focus);
3934 if (surface == NULL)
3935 return;
3936
3937 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003938 if (shsurf == NULL || shsurf->state.fullscreen ||
3939 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003940 return;
3941
3942 surface_touch_move(shsurf, (struct weston_seat *) seat);
3943}
3944
3945static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003946resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003947{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003948 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003949 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003950 uint32_t edges = 0;
3951 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003952 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003953
Pekka Paalanen01388e22013-04-25 13:57:44 +03003954 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003955 if (surface == NULL)
3956 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003957
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003958 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003959 if (shsurf == NULL || shsurf->state.fullscreen ||
3960 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003961 return;
3962
Jason Ekstranda7af7042013-10-12 22:38:11 -05003963 weston_view_from_global(shsurf->view,
3964 wl_fixed_to_int(seat->pointer->grab_x),
3965 wl_fixed_to_int(seat->pointer->grab_y),
3966 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003967
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003968 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003969 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003970 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003971 edges |= 0;
3972 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003973 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003974
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003975 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003976 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003977 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003978 edges |= 0;
3979 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003980 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003981
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003982 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003983}
3984
3985static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003986surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003987 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003988{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003989 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003990 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003991 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003992 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003993
Pekka Paalanen01388e22013-04-25 13:57:44 +03003994 /* XXX: broken for windows containing sub-surfaces */
3995 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003996 if (surface == NULL)
3997 return;
3998
3999 shsurf = get_shell_surface(surface);
4000 if (!shsurf)
4001 return;
4002
Jason Ekstranda7af7042013-10-12 22:38:11 -05004003 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004004
Jason Ekstranda7af7042013-10-12 22:38:11 -05004005 if (shsurf->view->alpha > 1.0)
4006 shsurf->view->alpha = 1.0;
4007 if (shsurf->view->alpha < step)
4008 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004009
Jason Ekstranda7af7042013-10-12 22:38:11 -05004010 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004011 weston_surface_damage(surface);
4012}
4013
4014static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004015do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004016 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004017{
Daniel Stone37816df2012-05-16 18:45:18 +01004018 struct weston_seat *ws = (struct weston_seat *) seat;
4019 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004020 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004021 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004022
4023 wl_list_for_each(output, &compositor->output_list, link) {
4024 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004025 wl_fixed_to_double(seat->pointer->x),
4026 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004027 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004028 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004029 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004030 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004031 increment = -output->zoom.increment;
4032 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004033 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004034 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004035 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004036 else
4037 increment = 0;
4038
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004039 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004040
Scott Moreaue6603982012-06-11 13:07:51 -06004041 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004042 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004043 else if (output->zoom.level > output->zoom.max_level)
4044 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004045 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004046 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004047 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004048
Scott Moreaue6603982012-06-11 13:07:51 -06004049 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004050
Jason Ekstranda7af7042013-10-12 22:38:11 -05004051 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004052 }
4053 }
4054}
4055
Scott Moreauccbf29d2012-02-22 14:21:41 -07004056static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004057zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004058 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004059{
4060 do_zoom(seat, time, 0, axis, value);
4061}
4062
4063static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004064zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004065 void *data)
4066{
4067 do_zoom(seat, time, key, 0, 0);
4068}
4069
4070static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004071terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004072 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004073{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004074 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004075
Daniel Stone325fc2d2012-05-30 16:31:58 +01004076 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004077}
4078
4079static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004080lower_fullscreen_layer(struct desktop_shell *shell);
4081
4082struct alt_tab {
4083 struct desktop_shell *shell;
4084 struct weston_keyboard_grab grab;
4085
4086 struct wl_list *current;
4087
4088 struct wl_list preview_list;
4089};
4090
4091struct alt_tab_preview {
4092 struct alt_tab *alt_tab;
4093
4094 struct weston_view *view;
4095 struct weston_transform transform;
4096
4097 struct wl_listener listener;
4098
4099 struct wl_list link;
4100};
4101
4102static void
4103alt_tab_next(struct alt_tab *alt_tab)
4104{
4105 alt_tab->current = alt_tab->current->next;
4106
4107 /* Make sure we're not pointing to the list header e.g. after
4108 * cycling through the whole list. */
4109 if (alt_tab->current->next == alt_tab->preview_list.next)
4110 alt_tab->current = alt_tab->current->next;
4111}
4112
4113static void
4114alt_tab_destroy(struct alt_tab *alt_tab)
4115{
4116 struct alt_tab_preview *preview, *next;
4117 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
4118
4119 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
4120 preview = wl_container_of(alt_tab->current, preview, link);
4121
4122 activate(alt_tab->shell, preview->view->surface,
4123 (struct weston_seat *) keyboard->seat);
4124 }
4125
4126 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
4127 wl_list_remove(&preview->link);
4128 wl_list_remove(&preview->listener.link);
4129 weston_view_destroy(preview->view);
4130 free(preview);
4131 }
4132
4133 weston_keyboard_end_grab(keyboard);
4134 if (keyboard->input_method_resource)
4135 keyboard->grab = &keyboard->input_method_grab;
4136
4137 free(alt_tab);
4138}
4139
4140static void
4141alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
4142{
4143 struct alt_tab_preview *preview =
4144 container_of(listener, struct alt_tab_preview, listener);
4145 struct alt_tab *alt_tab = preview->alt_tab;
4146 int advance = 0;
4147
4148 /* If the preview that we're removing is the currently selected one,
4149 * we want to move to the next one. So we move to ->prev and then
4150 * call _next() after removing the preview. */
4151 if (alt_tab->current == &preview->link) {
4152 alt_tab->current = alt_tab->current->prev;
4153 advance = 1;
4154 }
4155
4156 wl_list_remove(&preview->listener.link);
4157 wl_list_remove(&preview->link);
4158
4159 free(preview);
4160
4161 if (advance)
4162 alt_tab_next(alt_tab);
4163
4164 /* If the last preview goes away, stop the alt-tab */
4165 if (wl_list_empty(alt_tab->current))
4166 alt_tab_destroy(alt_tab);
4167}
4168
4169static void
4170alt_tab_key(struct weston_keyboard_grab *grab,
4171 uint32_t time, uint32_t key, uint32_t state_w)
4172{
4173 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4174 enum wl_keyboard_key_state state = state_w;
4175
4176 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4177 alt_tab_next(alt_tab);
4178}
4179
4180static void
4181alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4182 uint32_t mods_depressed, uint32_t mods_latched,
4183 uint32_t mods_locked, uint32_t group)
4184{
4185 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4186 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
4187
4188 if ((seat->modifier_state & MODIFIER_ALT) == 0)
4189 alt_tab_destroy(alt_tab);
4190}
4191
4192static void
4193alt_tab_cancel(struct weston_keyboard_grab *grab)
4194{
4195 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4196
4197 alt_tab_destroy(alt_tab);
4198}
4199
4200static const struct weston_keyboard_grab_interface alt_tab_grab = {
4201 alt_tab_key,
4202 alt_tab_modifier,
4203 alt_tab_cancel,
4204};
4205
4206static int
4207view_for_alt_tab(struct weston_view *view)
4208{
Rafael Antognollied207b42013-12-03 15:35:43 -02004209 struct shell_surface *shsurf = get_shell_surface(view->surface);
4210 if (!shsurf)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004211 return 0;
4212
Rafael Antognollied207b42013-12-03 15:35:43 -02004213 if (shsurf->parent)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004214 return 0;
4215
4216 if (view != get_default_view(view->surface))
4217 return 0;
4218
4219 return 1;
4220}
4221
4222static void
4223alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4224 void *data)
4225{
4226 struct alt_tab *alt_tab;
4227 struct desktop_shell *shell = data;
4228 struct weston_output *output = get_default_output(shell->compositor);
4229 struct workspace *ws;
4230 struct weston_view *view;
4231 int num_surfaces = 0;
4232 int x, y, side, margin;
4233
4234 wl_list_for_each(view, &shell->compositor->view_list, link) {
4235
4236 if (view_for_alt_tab(view))
4237 num_surfaces++;
4238 }
4239
4240 if (!num_surfaces)
4241 return;
4242
4243 alt_tab = malloc(sizeof *alt_tab);
4244 if (!alt_tab)
4245 return;
4246
4247 alt_tab->shell = shell;
4248 wl_list_init(&alt_tab->preview_list);
4249 alt_tab->current = &alt_tab->preview_list;
4250
4251 restore_all_output_modes(shell->compositor);
4252 lower_fullscreen_layer(alt_tab->shell);
4253
4254 alt_tab->grab.interface = &alt_tab_grab;
4255 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
4256 weston_keyboard_set_focus(seat->keyboard, NULL);
4257
4258 ws = get_current_workspace(shell);
4259
4260 /* FIXME: add some visual candy e.g. prelight the selected view
4261 * and/or add a black surrounding background à la gnome-shell */
4262
4263 /* Determine the size for each preview */
4264 side = output->width / num_surfaces;
4265 if (side > 200)
4266 side = 200;
4267 margin = side / 4;
4268 side -= margin;
4269
4270 x = margin;
4271 y = (output->height - side) / 2;
4272
4273 /* Create a view for each surface */
4274 wl_list_for_each(view, &shell->compositor->view_list, link) {
4275 struct alt_tab_preview *preview;
4276 struct weston_view *v;
4277 float scale;
4278
4279 if (!view_for_alt_tab(view))
4280 continue;
4281
4282 preview = malloc(sizeof *preview);
4283 if (!preview) {
4284 alt_tab_destroy(alt_tab);
4285 return;
4286 }
4287
4288 preview->alt_tab = alt_tab;
4289
4290 preview->view = v = weston_view_create(view->surface);
4291 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004292
4293 wl_list_remove(&v->layer_link);
4294 wl_list_insert(&ws->layer.view_list, &v->layer_link);
4295 weston_view_damage_below(v);
4296 weston_surface_damage(v->surface);
4297
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004298 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004299
4300 preview->listener.notify = alt_tab_handle_surface_destroy;
4301 wl_signal_add(&v->destroy_signal, &preview->listener);
4302
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004303 if (view->surface->width > view->surface->height)
4304 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004305 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004306 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004307
4308 wl_list_insert(&v->geometry.transformation_list,
4309 &preview->transform.link);
4310 weston_matrix_init(&preview->transform.matrix);
4311 weston_matrix_scale(&preview->transform.matrix,
4312 scale, scale, 1.0f);
4313
4314 weston_view_geometry_dirty(v);
4315 weston_compositor_schedule_repaint(v->surface->compositor);
4316
4317 /* Insert at the end of the list */
4318 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
4319
4320 x += side + margin;
4321 }
4322
4323 /* Start at the second preview so a simple <alt>tab changes window.
4324 * We set `current' to the first preview and not the second because
4325 * we're going to receive a key press callback for the initial
4326 * <alt>tab which will make `current' point to the second element. */
4327 alt_tab_next(alt_tab);
4328}
4329
4330static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004331rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4332 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004333{
4334 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004335 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004336 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004337 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004338 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004339
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004340 weston_pointer_move(pointer, x, y);
4341
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004342 if (!shsurf)
4343 return;
4344
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004345 cx = 0.5f * shsurf->surface->width;
4346 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004347
Daniel Stone37816df2012-05-16 18:45:18 +01004348 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4349 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004350 r = sqrtf(dx * dx + dy * dy);
4351
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004352 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004353 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004354
4355 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004356 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004357 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004358
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004359 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004360 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004361
4362 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004363 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004364 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004365 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004366 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004367
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004368 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004369 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004370 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004371 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004372 wl_list_init(&shsurf->rotation.transform.link);
4373 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004374 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004375 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004376
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004377 /* We need to adjust the position of the surface
4378 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004379 cposx = shsurf->view->geometry.x + cx;
4380 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004381 dposx = rotate->center.x - cposx;
4382 dposy = rotate->center.y - cposy;
4383 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004384 weston_view_set_position(shsurf->view,
4385 shsurf->view->geometry.x + dposx,
4386 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004387 }
4388
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004389 /* Repaint implies weston_surface_update_transform(), which
4390 * lazily applies the damage due to rotation update.
4391 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004392 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004393}
4394
4395static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004396rotate_grab_button(struct weston_pointer_grab *grab,
4397 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004398{
4399 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004400 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004401 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004402 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004403 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004404
Daniel Stone4dbadb12012-05-30 16:31:51 +01004405 if (pointer->button_count == 0 &&
4406 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004407 if (shsurf)
4408 weston_matrix_multiply(&shsurf->rotation.rotation,
4409 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004410 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004411 free(rotate);
4412 }
4413}
4414
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004415static void
4416rotate_grab_cancel(struct weston_pointer_grab *grab)
4417{
4418 struct rotate_grab *rotate =
4419 container_of(grab, struct rotate_grab, base.grab);
4420
4421 shell_grab_end(&rotate->base);
4422 free(rotate);
4423}
4424
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004425static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004426 noop_grab_focus,
4427 rotate_grab_motion,
4428 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004429 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004430};
4431
4432static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004433surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004434{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004435 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004436 float dx, dy;
4437 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004438
Pekka Paalanen460099f2012-01-20 16:48:25 +02004439 rotate = malloc(sizeof *rotate);
4440 if (!rotate)
4441 return;
4442
Jason Ekstranda7af7042013-10-12 22:38:11 -05004443 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004444 surface->surface->width * 0.5f,
4445 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004446 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004447
Daniel Stone37816df2012-05-16 18:45:18 +01004448 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4449 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004450 r = sqrtf(dx * dx + dy * dy);
4451 if (r > 20.0f) {
4452 struct weston_matrix inverse;
4453
4454 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004455 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004456 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004457
4458 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004459 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004460 } else {
4461 weston_matrix_init(&surface->rotation.rotation);
4462 weston_matrix_init(&rotate->rotation);
4463 }
4464
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004465 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4466 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004467}
4468
4469static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004470rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004471 void *data)
4472{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004473 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004474 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004475 struct shell_surface *surface;
4476
Pekka Paalanen01388e22013-04-25 13:57:44 +03004477 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004478 if (base_surface == NULL)
4479 return;
4480
4481 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004482 if (surface == NULL || surface->state.fullscreen ||
4483 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004484 return;
4485
4486 surface_rotate(surface, seat);
4487}
4488
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004489/* Move all fullscreen layers down to the current workspace in a non-reversible
4490 * manner. This should be used when implementing shell-wide overlays, such as
4491 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004492static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004493lower_fullscreen_layer(struct desktop_shell *shell)
4494{
4495 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004496 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004497
4498 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004499 wl_list_for_each_reverse_safe(view, prev,
4500 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004501 layer_link) {
4502 wl_list_remove(&view->layer_link);
4503 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4504 weston_view_damage_below(view);
4505 weston_surface_damage(view->surface);
4506 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004507}
4508
4509static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004510activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004511 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004512{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004513 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004514 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004515 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004516 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004517 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004518
Pekka Paalanen01388e22013-04-25 13:57:44 +03004519 main_surface = weston_surface_get_main_surface(es);
4520
Daniel Stone37816df2012-05-16 18:45:18 +01004521 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004522
Jonas Ådahl8538b222012-08-29 22:13:03 +02004523 state = ensure_focus_state(shell, seat);
4524 if (state == NULL)
4525 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004526
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004527 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004528 state->keyboard_focus = es;
4529 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004530 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004531
Rafael Antognolli03b16592013-12-03 15:35:42 -02004532 shsurf = get_shell_surface(main_surface);
4533 if (shsurf->state.fullscreen)
4534 shell_configure_fullscreen(shsurf);
4535 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004536 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004537
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004538 if (shell->focus_animation_type != ANIMATION_NONE) {
4539 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004540 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004541 }
4542
4543 /* Update the surface’s layer. This brings it to the top of the stacking
4544 * order as appropriate. */
4545 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004546}
4547
Alex Wu21858432012-04-01 20:13:08 +08004548/* no-op func for checking black surface */
4549static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004550black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004551{
4552}
4553
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004554static bool
Alex Wu21858432012-04-01 20:13:08 +08004555is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4556{
4557 if (es->configure == black_surface_configure) {
4558 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004559 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004560 return true;
4561 }
4562 return false;
4563}
4564
Kristian Høgsberg75840622011-09-06 13:48:16 -04004565static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004566activate_binding(struct weston_seat *seat,
4567 struct desktop_shell *shell,
4568 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004569{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004570 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004571
Alex Wu9c35e6b2012-03-05 14:13:13 +08004572 if (!focus)
4573 return;
4574
Pekka Paalanen01388e22013-04-25 13:57:44 +03004575 if (is_black_surface(focus, &main_surface))
4576 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004577
Pekka Paalanen01388e22013-04-25 13:57:44 +03004578 main_surface = weston_surface_get_main_surface(focus);
4579 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004580 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004581
Neil Robertsa28c6932013-10-03 16:43:04 +01004582 activate(shell, focus, seat);
4583}
4584
4585static void
4586click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4587 void *data)
4588{
4589 if (seat->pointer->grab != &seat->pointer->default_grab)
4590 return;
4591
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004592 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004593}
4594
4595static void
4596touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4597{
4598 if (seat->touch->grab != &seat->touch->default_grab)
4599 return;
4600
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004601 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004602}
4603
4604static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004605lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004606{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004607 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004608
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004609 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004610 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004611 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004612 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004613
4614 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004615
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004616 /* Hide all surfaces by removing the fullscreen, panel and
4617 * toplevel layers. This way nothing else can show or receive
4618 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004619
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004620 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004621 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004622 if (shell->showing_input_panels)
4623 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004624 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004625 wl_list_insert(&shell->compositor->cursor_layer.link,
4626 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004627
Pekka Paalanen77346a62011-11-30 16:26:35 +02004628 launch_screensaver(shell);
4629
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004630 /* TODO: disable bindings that should not work while locked. */
4631
4632 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004633}
4634
4635static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004636unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004637{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004638 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004639 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004640 return;
4641 }
4642
4643 /* If desktop-shell client has gone away, unlock immediately. */
4644 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004645 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004646 return;
4647 }
4648
4649 if (shell->prepare_event_sent)
4650 return;
4651
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004652 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004653 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004654}
4655
4656static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004657shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004658{
4659 struct desktop_shell *shell = data;
4660
4661 shell->fade.animation = NULL;
4662
4663 switch (shell->fade.type) {
4664 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004665 weston_surface_destroy(shell->fade.view->surface);
4666 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004667 break;
4668 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004669 lock(shell);
4670 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004671 default:
4672 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004673 }
4674}
4675
Jason Ekstranda7af7042013-10-12 22:38:11 -05004676static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004677shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004678{
4679 struct weston_compositor *compositor = shell->compositor;
4680 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004681 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004682
4683 surface = weston_surface_create(compositor);
4684 if (!surface)
4685 return NULL;
4686
Jason Ekstranda7af7042013-10-12 22:38:11 -05004687 view = weston_view_create(surface);
4688 if (!view) {
4689 weston_surface_destroy(surface);
4690 return NULL;
4691 }
4692
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004693 surface->width = 8192;
4694 surface->height = 8192;
4695 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004696 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004697 wl_list_insert(&compositor->fade_layer.view_list,
4698 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004699 pixman_region32_init(&surface->input);
4700
Jason Ekstranda7af7042013-10-12 22:38:11 -05004701 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004702}
4703
4704static void
4705shell_fade(struct desktop_shell *shell, enum fade_type type)
4706{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004707 float tint;
4708
4709 switch (type) {
4710 case FADE_IN:
4711 tint = 0.0;
4712 break;
4713 case FADE_OUT:
4714 tint = 1.0;
4715 break;
4716 default:
4717 weston_log("shell: invalid fade type\n");
4718 return;
4719 }
4720
4721 shell->fade.type = type;
4722
Jason Ekstranda7af7042013-10-12 22:38:11 -05004723 if (shell->fade.view == NULL) {
4724 shell->fade.view = shell_fade_create_surface(shell);
4725 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004726 return;
4727
Jason Ekstranda7af7042013-10-12 22:38:11 -05004728 shell->fade.view->alpha = 1.0 - tint;
4729 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004730 }
4731
4732 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004733 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004734 else
4735 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004736 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004737 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004738 shell_fade_done, shell);
4739}
4740
4741static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004742do_shell_fade_startup(void *data)
4743{
4744 struct desktop_shell *shell = data;
4745
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004746 if (shell->startup_animation_type == ANIMATION_FADE)
4747 shell_fade(shell, FADE_IN);
4748 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004749 weston_surface_destroy(shell->fade.view->surface);
4750 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004751 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004752}
4753
4754static void
4755shell_fade_startup(struct desktop_shell *shell)
4756{
4757 struct wl_event_loop *loop;
4758
4759 if (!shell->fade.startup_timer)
4760 return;
4761
4762 wl_event_source_remove(shell->fade.startup_timer);
4763 shell->fade.startup_timer = NULL;
4764
4765 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4766 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4767}
4768
4769static int
4770fade_startup_timeout(void *data)
4771{
4772 struct desktop_shell *shell = data;
4773
4774 shell_fade_startup(shell);
4775 return 0;
4776}
4777
4778static void
4779shell_fade_init(struct desktop_shell *shell)
4780{
4781 /* Make compositor output all black, and wait for the desktop-shell
4782 * client to signal it is ready, then fade in. The timer triggers a
4783 * fade-in, in case the desktop-shell client takes too long.
4784 */
4785
4786 struct wl_event_loop *loop;
4787
Jason Ekstranda7af7042013-10-12 22:38:11 -05004788 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004789 weston_log("%s: warning: fade surface already exists\n",
4790 __func__);
4791 return;
4792 }
4793
Jason Ekstranda7af7042013-10-12 22:38:11 -05004794 shell->fade.view = shell_fade_create_surface(shell);
4795 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004796 return;
4797
Jason Ekstranda7af7042013-10-12 22:38:11 -05004798 weston_view_update_transform(shell->fade.view);
4799 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004800
4801 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4802 shell->fade.startup_timer =
4803 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4804 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4805}
4806
4807static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004808idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004809{
4810 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004811 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004812
4813 shell_fade(shell, FADE_OUT);
4814 /* lock() is called from shell_fade_done() */
4815}
4816
4817static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004818wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004819{
4820 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004821 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004822
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004823 unlock(shell);
4824}
4825
4826static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004827show_input_panels(struct wl_listener *listener, void *data)
4828{
4829 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004830 container_of(listener, struct desktop_shell,
4831 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004832 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004833
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004834 shell->text_input.surface = (struct weston_surface*)data;
4835
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004836 if (shell->showing_input_panels)
4837 return;
4838
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004839 shell->showing_input_panels = true;
4840
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004841 if (!shell->locked)
4842 wl_list_insert(&shell->panel_layer.link,
4843 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004844
Jason Ekstranda7af7042013-10-12 22:38:11 -05004845 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004846 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004847 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004848 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004849 wl_list_insert(&shell->input_panel_layer.view_list,
4850 &ipsurf->view->layer_link);
4851 weston_view_geometry_dirty(ipsurf->view);
4852 weston_view_update_transform(ipsurf->view);
4853 weston_surface_damage(ipsurf->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004854 weston_slide_run(ipsurf->view, ipsurf->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004855 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004856 }
4857}
4858
4859static void
4860hide_input_panels(struct wl_listener *listener, void *data)
4861{
4862 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004863 container_of(listener, struct desktop_shell,
4864 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004865 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004866
Jan Arne Petersen61381972013-01-31 15:52:21 +01004867 if (!shell->showing_input_panels)
4868 return;
4869
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004870 shell->showing_input_panels = false;
4871
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004872 if (!shell->locked)
4873 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004874
Jason Ekstranda7af7042013-10-12 22:38:11 -05004875 wl_list_for_each_safe(view, next,
4876 &shell->input_panel_layer.view_list, layer_link)
4877 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004878}
4879
4880static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004881update_input_panels(struct wl_listener *listener, void *data)
4882{
4883 struct desktop_shell *shell =
4884 container_of(listener, struct desktop_shell,
4885 update_input_panel_listener);
4886
4887 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4888}
4889
4890static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004891center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004892{
Giulio Camuffob8366642013-04-25 13:57:46 +03004893 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004894 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004895
Jason Ekstranda7af7042013-10-12 22:38:11 -05004896 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004897
4898 x = output->x + (output->width - width) / 2 - surf_x / 2;
4899 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004900
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004901 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004902}
4903
4904static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004905weston_view_set_initial_position(struct weston_view *view,
4906 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004907{
4908 struct weston_compositor *compositor = shell->compositor;
4909 int ix = 0, iy = 0;
4910 int range_x, range_y;
4911 int dx, dy, x, y, panel_height;
4912 struct weston_output *output, *target_output = NULL;
4913 struct weston_seat *seat;
4914
4915 /* As a heuristic place the new window on the same output as the
4916 * pointer. Falling back to the output containing 0, 0.
4917 *
4918 * TODO: Do something clever for touch too?
4919 */
4920 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004921 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004922 ix = wl_fixed_to_int(seat->pointer->x);
4923 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004924 break;
4925 }
4926 }
4927
4928 wl_list_for_each(output, &compositor->output_list, link) {
4929 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4930 target_output = output;
4931 break;
4932 }
4933 }
4934
4935 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004936 weston_view_set_position(view, 10 + random() % 400,
4937 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004938 return;
4939 }
4940
4941 /* Valid range within output where the surface will still be onscreen.
4942 * If this is negative it means that the surface is bigger than
4943 * output.
4944 */
4945 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004946 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004947 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004948 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004949
Rob Bradford4cb88c72012-08-13 15:18:44 +01004950 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004951 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004952 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004953 dx = 0;
4954
4955 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004956 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004957 else
4958 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004959
4960 x = target_output->x + dx;
4961 y = target_output->y + dy;
4962
Jason Ekstranda7af7042013-10-12 22:38:11 -05004963 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004964}
4965
4966static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004967map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004968 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004969{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004970 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004971 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004972 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004973 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004974
Pekka Paalanen77346a62011-11-30 16:26:35 +02004975 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004976 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004977 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004978 if (shsurf->state.fullscreen) {
4979 center_on_output(shsurf->view, shsurf->fullscreen_output);
4980 shell_map_fullscreen(shsurf);
4981 } else if (shsurf->state.maximized) {
4982 /* use surface configure to set the geometry */
4983 panel_height = get_output_panel_height(shell, shsurf->output);
4984 surface_subsurfaces_boundingbox(shsurf->surface,
4985 &surf_x, &surf_y, NULL, NULL);
4986 weston_view_set_position(shsurf->view,
4987 shsurf->output->x - surf_x,
4988 shsurf->output->y +
4989 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004990 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004991 weston_view_set_initial_position(shsurf->view, shell);
4992 }
Juan Zhao96879df2012-02-07 08:45:41 +08004993 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004994 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004995 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004996 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004997 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004998 weston_view_set_position(shsurf->view,
4999 shsurf->view->geometry.x + sx,
5000 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005001 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005002 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005003 default:
5004 ;
5005 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005006
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005007 /* Surface stacking order, see also activate(). */
5008 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005009
Jason Ekstranda7af7042013-10-12 22:38:11 -05005010 if (shsurf->type != SHELL_SURFACE_NONE) {
5011 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005012 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005013 shsurf->surface->output = shsurf->output;
5014 shsurf->view->output = shsurf->output;
5015 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005016 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005017
Rafael Antognollied207b42013-12-03 15:35:43 -02005018 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
5019 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
5020 }
5021
Jason Ekstranda7af7042013-10-12 22:38:11 -05005022 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005023 /* XXX: xwayland's using the same fields for transient type */
5024 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005025 if (shsurf->transient.flags ==
5026 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5027 break;
5028 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005029 if (shsurf->state.relative &&
5030 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5031 break;
5032 if (!shell->locked)
5033 break;
5034 wl_list_for_each(seat, &compositor->seat_list, link)
5035 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005036 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005037 case SHELL_SURFACE_POPUP:
5038 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005039 default:
5040 break;
5041 }
5042
Rafael Antognolli03b16592013-12-03 15:35:42 -02005043 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5044 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005045 {
5046 switch (shell->win_animation_type) {
5047 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005048 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005049 break;
5050 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005051 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005052 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005053 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005054 default:
5055 break;
5056 }
5057 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005058}
5059
5060static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005061configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005062 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005063{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005064 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005065 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005066 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005067
Pekka Paalanen77346a62011-11-30 16:26:35 +02005068 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005069
Rafael Antognolli03b16592013-12-03 15:35:42 -02005070 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005071 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005072 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005073 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005074 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5075 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005076 mx = shsurf->output->x - surf_x;
5077 my = shsurf->output->y +
5078 get_output_panel_height(shell,shsurf->output) - surf_y;
5079 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005080 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005081 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005082 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005083
Alex Wu4539b082012-03-01 12:57:46 +08005084 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005085 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005086 wl_list_for_each(view, &surface->views, surface_link)
5087 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005088
Rafael Antognolli03b16592013-12-03 15:35:42 -02005089 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005090 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005091 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005092}
5093
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005094static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005095shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005096{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005097 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03005098 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01005099
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005100 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005101
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005102 if (!weston_surface_is_mapped(es) &&
5103 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005104 remove_popup_grab(shsurf);
5105 }
5106
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005107 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005108 return;
5109
Rafael Antognolli03b16592013-12-03 15:35:42 -02005110 if ((shsurf->next_type != SHELL_SURFACE_NONE &&
5111 shsurf->type != shsurf->next_type) ||
5112 shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005113 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005114 type_changed = 1;
5115 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005116
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005117 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005118 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005119 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005120 shsurf->last_width != es->width ||
5121 shsurf->last_height != es->height) {
5122 shsurf->last_width = es->width;
5123 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005124 float from_x, from_y;
5125 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005126
Jason Ekstranda7af7042013-10-12 22:38:11 -05005127 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5128 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005129 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005130 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005131 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005132 }
5133}
5134
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005135static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005136
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005137static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005138desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005139{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005140 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005141 struct desktop_shell *shell =
5142 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005143
5144 shell->child.process.pid = 0;
5145 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005146
5147 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5148 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005149 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005150 shell->child.deathstamp = time;
5151 shell->child.deathcount = 0;
5152 }
5153
5154 shell->child.deathcount++;
5155 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005156 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005157 return;
5158 }
5159
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005160 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005161 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005162 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005163}
5164
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005165static void
5166launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005167{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005168 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005169
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005170 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005171 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005172 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005173 desktop_shell_sigchld);
5174
5175 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005176 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005177}
5178
5179static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005180bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5181{
Tiago Vignattibe143262012-04-16 17:31:41 +03005182 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005183 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005184
Jason Ekstranda85118c2013-06-27 20:17:02 -05005185 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
5186 if (resource)
5187 wl_resource_set_implementation(resource, &shell_implementation,
5188 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005189}
5190
Kristian Høgsberg75840622011-09-06 13:48:16 -04005191static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02005192bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5193{
5194 struct desktop_shell *shell = data;
5195 struct wl_resource *resource;
5196
5197 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
5198 if (resource)
5199 wl_resource_set_dispatcher(resource,
5200 xdg_shell_unversioned_dispatch,
5201 NULL, shell, NULL);
5202}
5203
5204static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005205unbind_desktop_shell(struct wl_resource *resource)
5206{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005207 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005208
5209 if (shell->locked)
5210 resume_desktop(shell);
5211
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005212 shell->child.desktop_shell = NULL;
5213 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005214}
5215
5216static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005217bind_desktop_shell(struct wl_client *client,
5218 void *data, uint32_t version, uint32_t id)
5219{
Tiago Vignattibe143262012-04-16 17:31:41 +03005220 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005221 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005222
Jason Ekstranda85118c2013-06-27 20:17:02 -05005223 resource = wl_resource_create(client, &desktop_shell_interface,
5224 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005225
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005226 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005227 wl_resource_set_implementation(resource,
5228 &desktop_shell_implementation,
5229 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005230 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005231
5232 if (version < 2)
5233 shell_fade_startup(shell);
5234
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005235 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005236 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005237
5238 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5239 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005240 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005241}
5242
Pekka Paalanen6e168112011-11-24 11:34:05 +02005243static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005244screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005245{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005246 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005247 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005248
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005249 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005250 return;
5251
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005252 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005253 if (!shell->locked)
5254 return;
5255
Jason Ekstranda7af7042013-10-12 22:38:11 -05005256 view = container_of(surface->views.next, struct weston_view, surface_link);
5257 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005258
Jason Ekstranda7af7042013-10-12 22:38:11 -05005259 if (wl_list_empty(&view->layer_link)) {
5260 wl_list_insert(shell->lock_layer.view_list.prev,
5261 &view->layer_link);
5262 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005263 wl_event_source_timer_update(shell->screensaver.timer,
5264 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005265 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005266 }
5267}
5268
5269static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005270screensaver_set_surface(struct wl_client *client,
5271 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005272 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005273 struct wl_resource *output_resource)
5274{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005275 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005276 struct weston_surface *surface =
5277 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005278 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005279 struct weston_view *view, *next;
5280
5281 /* Make sure we only have one view */
5282 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5283 weston_view_destroy(view);
5284 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005285
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005286 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005287 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005288 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005289}
5290
5291static const struct screensaver_interface screensaver_implementation = {
5292 screensaver_set_surface
5293};
5294
5295static void
5296unbind_screensaver(struct wl_resource *resource)
5297{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005298 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005299
Pekka Paalanen77346a62011-11-30 16:26:35 +02005300 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005301}
5302
5303static void
5304bind_screensaver(struct wl_client *client,
5305 void *data, uint32_t version, uint32_t id)
5306{
Tiago Vignattibe143262012-04-16 17:31:41 +03005307 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005308 struct wl_resource *resource;
5309
Jason Ekstranda85118c2013-06-27 20:17:02 -05005310 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005311
Pekka Paalanen77346a62011-11-30 16:26:35 +02005312 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005313 wl_resource_set_implementation(resource,
5314 &screensaver_implementation,
5315 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005316 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005317 return;
5318 }
5319
5320 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5321 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005322 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005323}
5324
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005325static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005326input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005327{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005328 struct input_panel_surface *ip_surface = surface->configure_private;
5329 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005330 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005331 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005332
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005333 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005334 return;
5335
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005336 if (!weston_surface_is_mapped(surface)) {
5337 if (!shell->showing_input_panels)
5338 return;
5339
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005340 show_surface = 1;
5341 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005342
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005343 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005344
5345 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005346 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
5347 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005348 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005349 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
5350 y = ip_surface->output->y + ip_surface->output->height - surface->height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005351 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005352
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005353 weston_view_set_position(ip_surface->view, x, y);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005354
5355 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005356 wl_list_insert(&shell->input_panel_layer.view_list,
5357 &ip_surface->view->layer_link);
5358 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005359 weston_surface_damage(surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005360 weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005361 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005362}
5363
5364static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005365destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005366{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005367 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
5368
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005369 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005370 wl_list_remove(&input_panel_surface->link);
5371
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005372 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005373 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005374
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005375 free(input_panel_surface);
5376}
5377
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005378static struct input_panel_surface *
5379get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005380{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005381 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005382 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005383 } else {
5384 return NULL;
5385 }
5386}
5387
5388static void
5389input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
5390{
5391 struct input_panel_surface *ipsurface = container_of(listener,
5392 struct input_panel_surface,
5393 surface_destroy_listener);
5394
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005395 if (ipsurface->resource) {
5396 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005397 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005398 destroy_input_panel_surface(ipsurface);
5399 }
5400}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005401
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005402static struct input_panel_surface *
5403create_input_panel_surface(struct desktop_shell *shell,
5404 struct weston_surface *surface)
5405{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005406 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005407
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005408 input_panel_surface = calloc(1, sizeof *input_panel_surface);
5409 if (!input_panel_surface)
5410 return NULL;
5411
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005412 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005413 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005414
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005415 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005416
5417 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005418 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005419
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005420 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005421 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005422 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005423 &input_panel_surface->surface_destroy_listener);
5424
5425 wl_list_init(&input_panel_surface->link);
5426
5427 return input_panel_surface;
5428}
5429
5430static void
5431input_panel_surface_set_toplevel(struct wl_client *client,
5432 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005433 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005434 uint32_t position)
5435{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005436 struct input_panel_surface *input_panel_surface =
5437 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005438 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005439
5440 wl_list_insert(&shell->input_panel.surfaces,
5441 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005442
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005443 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005444 input_panel_surface->panel = 0;
5445}
5446
5447static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02005448input_panel_surface_set_overlay_panel(struct wl_client *client,
5449 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005450{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005451 struct input_panel_surface *input_panel_surface =
5452 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005453 struct desktop_shell *shell = input_panel_surface->shell;
5454
5455 wl_list_insert(&shell->input_panel.surfaces,
5456 &input_panel_surface->link);
5457
5458 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005459}
5460
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005461static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005462 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02005463 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005464};
5465
5466static void
5467destroy_input_panel_surface_resource(struct wl_resource *resource)
5468{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005469 struct input_panel_surface *ipsurf =
5470 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005471
5472 destroy_input_panel_surface(ipsurf);
5473}
5474
5475static void
5476input_panel_get_input_panel_surface(struct wl_client *client,
5477 struct wl_resource *resource,
5478 uint32_t id,
5479 struct wl_resource *surface_resource)
5480{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005481 struct weston_surface *surface =
5482 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005483 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005484 struct input_panel_surface *ipsurf;
5485
5486 if (get_input_panel_surface(surface)) {
5487 wl_resource_post_error(surface_resource,
5488 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005489 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005490 return;
5491 }
5492
5493 ipsurf = create_input_panel_surface(shell, surface);
5494 if (!ipsurf) {
5495 wl_resource_post_error(surface_resource,
5496 WL_DISPLAY_ERROR_INVALID_OBJECT,
5497 "surface->configure already set");
5498 return;
5499 }
5500
Jason Ekstranda85118c2013-06-27 20:17:02 -05005501 ipsurf->resource =
5502 wl_resource_create(client,
5503 &wl_input_panel_surface_interface, 1, id);
5504 wl_resource_set_implementation(ipsurf->resource,
5505 &input_panel_surface_implementation,
5506 ipsurf,
5507 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005508}
5509
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005510static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005511 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005512};
5513
5514static void
5515unbind_input_panel(struct wl_resource *resource)
5516{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005517 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005518
5519 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005520}
5521
5522static void
5523bind_input_panel(struct wl_client *client,
5524 void *data, uint32_t version, uint32_t id)
5525{
5526 struct desktop_shell *shell = data;
5527 struct wl_resource *resource;
5528
Jason Ekstranda85118c2013-06-27 20:17:02 -05005529 resource = wl_resource_create(client,
5530 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005531
5532 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005533 wl_resource_set_implementation(resource,
5534 &input_panel_implementation,
5535 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005536 shell->input_panel.binding = resource;
5537 return;
5538 }
5539
5540 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5541 "interface object already bound");
5542 wl_resource_destroy(resource);
5543}
5544
Kristian Høgsberg07045392012-02-19 18:52:44 -05005545struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005546 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005547 struct weston_surface *current;
5548 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005549 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005550};
5551
5552static void
5553switcher_next(struct switcher *switcher)
5554{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005555 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005556 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005557 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005558 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005559
Jason Ekstranda7af7042013-10-12 22:38:11 -05005560 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005561 shsurf = get_shell_surface(view->surface);
5562 switch (shsurf->type) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005563 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005564 if (shsurf->parent)
5565 break;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005566 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005567 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005568 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005569 next = view->surface;
5570 prev = view->surface;
5571 view->alpha = 0.25;
5572 weston_view_geometry_dirty(view);
5573 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005574 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005575 case SHELL_SURFACE_POPUP:
5576 case SHELL_SURFACE_XWAYLAND:
5577 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005578 default:
5579 break;
5580 }
Alex Wu1659daa2012-04-01 20:13:09 +08005581
Jason Ekstranda7af7042013-10-12 22:38:11 -05005582 if (is_black_surface(view->surface, NULL)) {
5583 view->alpha = 0.25;
5584 weston_view_geometry_dirty(view);
5585 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005586 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005587 }
5588
5589 if (next == NULL)
5590 next = first;
5591
Alex Wu07b26062012-03-12 16:06:01 +08005592 if (next == NULL)
5593 return;
5594
Kristian Høgsberg07045392012-02-19 18:52:44 -05005595 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005596 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005597
5598 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005599 wl_list_for_each(view, &next->views, surface_link)
5600 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005601
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005602 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005603 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005604 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005605}
5606
5607static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005608switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005609{
5610 struct switcher *switcher =
5611 container_of(listener, struct switcher, listener);
5612
5613 switcher_next(switcher);
5614}
5615
5616static void
Daniel Stone351eb612012-05-31 15:27:47 -04005617switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005618{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005619 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005620 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005621 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005622
Jason Ekstranda7af7042013-10-12 22:38:11 -05005623 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005624 if (is_focus_view(view))
5625 continue;
5626
Jason Ekstranda7af7042013-10-12 22:38:11 -05005627 view->alpha = 1.0;
5628 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005629 }
5630
Alex Wu07b26062012-03-12 16:06:01 +08005631 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005632 activate(switcher->shell, switcher->current,
5633 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005634 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005635 weston_keyboard_end_grab(keyboard);
5636 if (keyboard->input_method_resource)
5637 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005638 free(switcher);
5639}
5640
5641static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005642switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005643 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005644{
5645 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005646 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005647
Daniel Stonec9785ea2012-05-30 16:31:52 +01005648 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005649 switcher_next(switcher);
5650}
5651
5652static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005653switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005654 uint32_t mods_depressed, uint32_t mods_latched,
5655 uint32_t mods_locked, uint32_t group)
5656{
5657 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005658 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005659
Daniel Stone351eb612012-05-31 15:27:47 -04005660 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5661 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005662}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005663
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005664static void
5665switcher_cancel(struct weston_keyboard_grab *grab)
5666{
5667 struct switcher *switcher = container_of(grab, struct switcher, grab);
5668
5669 switcher_destroy(switcher);
5670}
5671
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005672static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005673 switcher_key,
5674 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005675 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005676};
5677
5678static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005679switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005680 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005681{
Tiago Vignattibe143262012-04-16 17:31:41 +03005682 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005683 struct switcher *switcher;
5684
5685 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005686 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005687 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005688 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005689 wl_list_init(&switcher->listener.link);
5690
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005691 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005692 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005693 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005694 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5695 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005696 switcher_next(switcher);
5697}
5698
Daniel Stonedf8133b2013-11-19 11:37:14 +01005699struct exposay_surface {
5700 struct desktop_shell *shell;
5701 struct weston_surface *surface;
5702 struct weston_view *view;
5703 struct wl_list link;
5704
5705 int x;
5706 int y;
5707 int width;
5708 int height;
5709 double scale;
5710
5711 int row;
5712 int column;
5713
5714 /* The animations only apply a transformation for their own lifetime,
5715 * and don't have an option to indefinitely maintain the
5716 * transformation in a steady state - so, we apply our own once the
5717 * animation has finished. */
5718 struct weston_transform transform;
5719};
5720
5721static void exposay_set_state(struct desktop_shell *shell,
5722 enum exposay_target_state state,
5723 struct weston_seat *seat);
5724static void exposay_check_state(struct desktop_shell *shell);
5725
5726static void
5727exposay_in_flight_inc(struct desktop_shell *shell)
5728{
5729 shell->exposay.in_flight++;
5730}
5731
5732static void
5733exposay_in_flight_dec(struct desktop_shell *shell)
5734{
5735 if (--shell->exposay.in_flight > 0)
5736 return;
5737
5738 exposay_check_state(shell);
5739}
5740
5741static void
5742exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5743{
5744 struct exposay_surface *esurface = data;
5745
5746 wl_list_insert(&esurface->view->geometry.transformation_list,
5747 &esurface->transform.link);
5748 weston_matrix_init(&esurface->transform.matrix);
5749 weston_matrix_scale(&esurface->transform.matrix,
5750 esurface->scale, esurface->scale, 1.0f);
5751 weston_matrix_translate(&esurface->transform.matrix,
5752 esurface->x - esurface->view->geometry.x,
5753 esurface->y - esurface->view->geometry.y,
5754 0);
5755
5756 weston_view_geometry_dirty(esurface->view);
5757 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5758
5759 exposay_in_flight_dec(esurface->shell);
5760}
5761
5762static void
5763exposay_animate_in(struct exposay_surface *esurface)
5764{
5765 exposay_in_flight_inc(esurface->shell);
5766
5767 weston_move_scale_run(esurface->view,
5768 esurface->x - esurface->view->geometry.x,
5769 esurface->y - esurface->view->geometry.y,
5770 1.0, esurface->scale, 0,
5771 exposay_animate_in_done, esurface);
5772}
5773
5774static void
5775exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5776{
5777 struct exposay_surface *esurface = data;
5778 struct desktop_shell *shell = esurface->shell;
5779
5780 wl_list_remove(&esurface->link);
5781 free(esurface);
5782
5783 exposay_in_flight_dec(shell);
5784}
5785
5786static void
5787exposay_animate_out(struct exposay_surface *esurface)
5788{
5789 exposay_in_flight_inc(esurface->shell);
5790
5791 /* Remove the static transformation set up by
5792 * exposay_transform_in_done(). */
5793 wl_list_remove(&esurface->transform.link);
5794 weston_view_geometry_dirty(esurface->view);
5795
5796 weston_move_scale_run(esurface->view,
5797 esurface->x - esurface->view->geometry.x,
5798 esurface->y - esurface->view->geometry.y,
5799 1.0, esurface->scale, 1,
5800 exposay_animate_out_done, esurface);
5801}
5802
5803static void
5804exposay_highlight_surface(struct desktop_shell *shell,
5805 struct exposay_surface *esurface)
5806{
5807 struct weston_view *view = NULL;
5808
5809 if (esurface) {
5810 shell->exposay.row_current = esurface->row;
5811 shell->exposay.column_current = esurface->column;
5812 view = esurface->view;
5813 }
5814
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005815 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005816 shell->exposay.focus_current = view;
5817}
5818
5819static int
5820exposay_is_animating(struct desktop_shell *shell)
5821{
5822 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5823 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5824 return 0;
5825
5826 return (shell->exposay.in_flight > 0);
5827}
5828
5829static void
5830exposay_pick(struct desktop_shell *shell, int x, int y)
5831{
5832 struct exposay_surface *esurface;
5833
5834 if (exposay_is_animating(shell))
5835 return;
5836
5837 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5838 if (x < esurface->x || x > esurface->x + esurface->width)
5839 continue;
5840 if (y < esurface->y || y > esurface->y + esurface->height)
5841 continue;
5842
5843 exposay_highlight_surface(shell, esurface);
5844 return;
5845 }
5846}
5847
5848/* Pretty lame layout for now; just tries to make a square. Should take
5849 * aspect ratio into account really. Also needs to be notified of surface
5850 * addition and removal and adjust layout/animate accordingly. */
5851static enum exposay_layout_state
5852exposay_layout(struct desktop_shell *shell)
5853{
5854 struct workspace *workspace = shell->exposay.workspace;
5855 struct weston_compositor *compositor = shell->compositor;
5856 struct weston_output *output = get_default_output(compositor);
5857 struct weston_view *view;
5858 struct exposay_surface *esurface;
5859 int w, h;
5860 int i;
5861 int last_row_removed = 0;
5862
5863 wl_list_init(&shell->exposay.surface_list);
5864
5865 shell->exposay.num_surfaces = 0;
5866 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5867 if (!get_shell_surface(view->surface))
5868 continue;
5869 shell->exposay.num_surfaces++;
5870 }
5871
5872 if (shell->exposay.num_surfaces == 0) {
5873 shell->exposay.grid_size = 0;
5874 shell->exposay.hpadding_outer = 0;
5875 shell->exposay.vpadding_outer = 0;
5876 shell->exposay.padding_inner = 0;
5877 shell->exposay.surface_size = 0;
5878 return EXPOSAY_LAYOUT_OVERVIEW;
5879 }
5880
5881 /* Lay the grid out as square as possible, losing surfaces from the
5882 * bottom row if required. Start with fixed padding of a 10% margin
5883 * around the outside and 80px internal padding between surfaces, and
5884 * maximise the area made available to surfaces after this, but only
5885 * to a maximum of 1/3rd the total output size.
5886 *
5887 * If we can't make a square grid, add one extra row at the bottom
5888 * which will have a smaller number of columns.
5889 *
5890 * XXX: Surely there has to be a better way to express this maths,
5891 * right?!
5892 */
5893 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5894 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5895 shell->exposay.grid_size++;
5896 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5897
5898 shell->exposay.hpadding_outer = (output->width / 10);
5899 shell->exposay.vpadding_outer = (output->height / 10);
5900 shell->exposay.padding_inner = 80;
5901
5902 w = output->width - (shell->exposay.hpadding_outer * 2);
5903 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5904 w /= shell->exposay.grid_size;
5905
5906 h = output->height - (shell->exposay.vpadding_outer * 2);
5907 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5908 h /= shell->exposay.grid_size;
5909
5910 shell->exposay.surface_size = (w < h) ? w : h;
5911 if (shell->exposay.surface_size > (output->width / 2))
5912 shell->exposay.surface_size = output->width / 2;
5913 if (shell->exposay.surface_size > (output->height / 2))
5914 shell->exposay.surface_size = output->height / 2;
5915
5916 i = 0;
5917 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5918 int pad;
5919
5920 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5921
5922 if (!get_shell_surface(view->surface))
5923 continue;
5924
5925 esurface = malloc(sizeof(*esurface));
5926 if (!esurface) {
5927 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5928 shell->exposay.seat);
5929 break;
5930 }
5931
5932 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5933 esurface->shell = shell;
5934 esurface->view = view;
5935
5936 esurface->row = i / shell->exposay.grid_size;
5937 esurface->column = i % shell->exposay.grid_size;
5938
5939 esurface->x = shell->exposay.hpadding_outer;
5940 esurface->x += pad * esurface->column;
5941 esurface->y = shell->exposay.vpadding_outer;
5942 esurface->y += pad * esurface->row;
5943
5944 if (esurface->row == shell->exposay.grid_size - 1)
5945 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5946
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005947 if (view->surface->width > view->surface->height)
5948 esurface->scale = shell->exposay.surface_size / (float) view->surface->width;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005949 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005950 esurface->scale = shell->exposay.surface_size / (float) view->surface->height;
5951 esurface->width = view->surface->width * esurface->scale;
5952 esurface->height = view->surface->height * esurface->scale;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005953
5954 if (shell->exposay.focus_current == esurface->view)
5955 exposay_highlight_surface(shell, esurface);
5956
5957 exposay_animate_in(esurface);
5958
5959 i++;
5960 }
5961
5962 weston_compositor_schedule_repaint(shell->compositor);
5963
5964 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5965}
5966
5967static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005968exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5969 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005970{
5971 struct desktop_shell *shell =
5972 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5973
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005974 weston_pointer_move(grab->pointer, x, y);
5975
Daniel Stonedf8133b2013-11-19 11:37:14 +01005976 exposay_pick(shell,
5977 wl_fixed_to_int(grab->pointer->x),
5978 wl_fixed_to_int(grab->pointer->y));
5979}
5980
5981static void
5982exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5983 uint32_t state_w)
5984{
5985 struct desktop_shell *shell =
5986 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5987 struct weston_seat *seat = grab->pointer->seat;
5988 enum wl_pointer_button_state state = state_w;
5989
5990 if (button != BTN_LEFT)
5991 return;
5992
5993 /* Store the surface we clicked on, and don't do anything if we end up
5994 * releasing on a different surface. */
5995 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5996 shell->exposay.clicked = shell->exposay.focus_current;
5997 return;
5998 }
5999
6000 if (shell->exposay.focus_current == shell->exposay.clicked)
6001 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
6002 else
6003 shell->exposay.clicked = NULL;
6004}
6005
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01006006static void
6007exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
6008{
6009 struct desktop_shell *shell =
6010 container_of(grab, struct desktop_shell, exposay.grab_ptr);
6011
6012 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
6013}
6014
Daniel Stonedf8133b2013-11-19 11:37:14 +01006015static const struct weston_pointer_grab_interface exposay_ptr_grab = {
6016 noop_grab_focus,
6017 exposay_motion,
6018 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01006019 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01006020};
6021
6022static int
6023exposay_maybe_move(struct desktop_shell *shell, int row, int column)
6024{
6025 struct exposay_surface *esurface;
6026
6027 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
6028 if (esurface->row != row || esurface->column != column)
6029 continue;
6030
6031 exposay_highlight_surface(shell, esurface);
6032 return 1;
6033 }
6034
6035 return 0;
6036}
6037
6038static void
6039exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
6040 uint32_t state_w)
6041{
6042 struct weston_seat *seat = grab->keyboard->seat;
6043 struct desktop_shell *shell =
6044 container_of(grab, struct desktop_shell, exposay.grab_kbd);
6045 enum wl_keyboard_key_state state = state_w;
6046
6047 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
6048 return;
6049
6050 switch (key) {
6051 case KEY_ESC:
6052 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
6053 break;
6054 case KEY_ENTER:
6055 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
6056 break;
6057 case KEY_UP:
6058 exposay_maybe_move(shell, shell->exposay.row_current - 1,
6059 shell->exposay.column_current);
6060 break;
6061 case KEY_DOWN:
6062 /* Special case for trying to move to the bottom row when it
6063 * has fewer items than all the others. */
6064 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
6065 shell->exposay.column_current) &&
6066 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
6067 exposay_maybe_move(shell, shell->exposay.row_current + 1,
6068 (shell->exposay.num_surfaces %
6069 shell->exposay.grid_size) - 1);
6070 }
6071 break;
6072 case KEY_LEFT:
6073 exposay_maybe_move(shell, shell->exposay.row_current,
6074 shell->exposay.column_current - 1);
6075 break;
6076 case KEY_RIGHT:
6077 exposay_maybe_move(shell, shell->exposay.row_current,
6078 shell->exposay.column_current + 1);
6079 break;
6080 case KEY_TAB:
6081 /* Try to move right, then down (and to the leftmost column),
6082 * then if all else fails, to the top left. */
6083 if (!exposay_maybe_move(shell, shell->exposay.row_current,
6084 shell->exposay.column_current + 1) &&
6085 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
6086 exposay_maybe_move(shell, 0, 0);
6087 break;
6088 default:
6089 break;
6090 }
6091}
6092
6093static void
6094exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
6095 uint32_t mods_depressed, uint32_t mods_latched,
6096 uint32_t mods_locked, uint32_t group)
6097{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01006098 struct desktop_shell *shell =
6099 container_of(grab, struct desktop_shell, exposay.grab_kbd);
6100 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
6101
6102 /* We want to know when mod has been pressed and released.
6103 * FIXME: There is a problem here: if mod is pressed, then a key
6104 * is pressed and released, then mod is released, we will treat that
6105 * as if only mod had been pressed and released. */
6106 if (seat->modifier_state) {
6107 if (seat->modifier_state == shell->binding_modifier) {
6108 shell->exposay.mod_pressed = true;
6109 } else {
6110 shell->exposay.mod_invalid = true;
6111 }
6112 } else {
6113 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
6114 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
6115
6116 shell->exposay.mod_invalid = false;
6117 shell->exposay.mod_pressed = false;
6118 }
6119
6120 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01006121}
6122
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01006123static void
6124exposay_cancel(struct weston_keyboard_grab *grab)
6125{
6126 struct desktop_shell *shell =
6127 container_of(grab, struct desktop_shell, exposay.grab_kbd);
6128
6129 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
6130}
6131
Daniel Stonedf8133b2013-11-19 11:37:14 +01006132static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
6133 exposay_key,
6134 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01006135 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01006136};
6137
6138/**
6139 * Called when the transition from overview -> inactive has completed.
6140 */
6141static enum exposay_layout_state
6142exposay_set_inactive(struct desktop_shell *shell)
6143{
6144 struct weston_seat *seat = shell->exposay.seat;
6145
6146 weston_keyboard_end_grab(seat->keyboard);
6147 weston_pointer_end_grab(seat->pointer);
6148 if (seat->keyboard->input_method_resource)
6149 seat->keyboard->grab = &seat->keyboard->input_method_grab;
6150
6151 return EXPOSAY_LAYOUT_INACTIVE;
6152}
6153
6154/**
6155 * Begins the transition from overview to inactive. */
6156static enum exposay_layout_state
6157exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
6158{
6159 struct exposay_surface *esurface;
6160
6161 /* Call activate() before we start the animations to avoid
6162 * animating back the old state and then immediately transitioning
6163 * to the new. */
6164 if (switch_focus && shell->exposay.focus_current)
6165 activate(shell, shell->exposay.focus_current->surface,
6166 shell->exposay.seat);
6167 else if (shell->exposay.focus_prev)
6168 activate(shell, shell->exposay.focus_prev->surface,
6169 shell->exposay.seat);
6170
6171 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
6172 exposay_animate_out(esurface);
6173 weston_compositor_schedule_repaint(shell->compositor);
6174
6175 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
6176}
6177
6178static enum exposay_layout_state
6179exposay_transition_active(struct desktop_shell *shell)
6180{
6181 struct weston_seat *seat = shell->exposay.seat;
6182
6183 shell->exposay.workspace = get_current_workspace(shell);
6184 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
6185 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
6186 shell->exposay.clicked = NULL;
6187 wl_list_init(&shell->exposay.surface_list);
6188
6189 lower_fullscreen_layer(shell);
6190 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
6191 weston_keyboard_start_grab(seat->keyboard,
6192 &shell->exposay.grab_kbd);
6193 weston_keyboard_set_focus(seat->keyboard, NULL);
6194
6195 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
6196 weston_pointer_start_grab(seat->pointer,
6197 &shell->exposay.grab_ptr);
6198 weston_pointer_set_focus(seat->pointer, NULL,
6199 seat->pointer->x, seat->pointer->y);
6200
6201 return exposay_layout(shell);
6202}
6203
6204static void
6205exposay_check_state(struct desktop_shell *shell)
6206{
6207 enum exposay_layout_state state_new = shell->exposay.state_cur;
6208 int do_switch = 0;
6209
6210 /* Don't do anything whilst animations are running, just store up
6211 * target state changes and only act on them when the animations have
6212 * completed. */
6213 if (exposay_is_animating(shell))
6214 return;
6215
6216 switch (shell->exposay.state_target) {
6217 case EXPOSAY_TARGET_OVERVIEW:
6218 switch (shell->exposay.state_cur) {
6219 case EXPOSAY_LAYOUT_OVERVIEW:
6220 goto out;
6221 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
6222 state_new = EXPOSAY_LAYOUT_OVERVIEW;
6223 break;
6224 default:
6225 state_new = exposay_transition_active(shell);
6226 break;
6227 }
6228 break;
6229
6230 case EXPOSAY_TARGET_SWITCH:
6231 do_switch = 1; /* fallthrough */
6232 case EXPOSAY_TARGET_CANCEL:
6233 switch (shell->exposay.state_cur) {
6234 case EXPOSAY_LAYOUT_INACTIVE:
6235 goto out;
6236 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
6237 state_new = exposay_set_inactive(shell);
6238 break;
6239 default:
6240 state_new = exposay_transition_inactive(shell, do_switch);
6241 break;
6242 }
6243
6244 break;
6245 }
6246
6247out:
6248 shell->exposay.state_cur = state_new;
6249}
6250
6251static void
6252exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
6253 struct weston_seat *seat)
6254{
6255 shell->exposay.state_target = state;
6256 shell->exposay.seat = seat;
6257 exposay_check_state(shell);
6258}
6259
6260static void
6261exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
6262 void *data)
6263{
6264 struct desktop_shell *shell = data;
6265
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01006266 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006267}
6268
Pekka Paalanen3c647232011-12-22 13:43:43 +02006269static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006270backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006271 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006272{
6273 struct weston_compositor *compositor = data;
6274 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006275 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006276
6277 /* TODO: we're limiting to simple use cases, where we assume just
6278 * control on the primary display. We'd have to extend later if we
6279 * ever get support for setting backlights on random desktop LCD
6280 * panels though */
6281 output = get_default_output(compositor);
6282 if (!output)
6283 return;
6284
6285 if (!output->set_backlight)
6286 return;
6287
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006288 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6289 backlight_new = output->backlight_current - 25;
6290 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6291 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006292
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006293 if (backlight_new < 5)
6294 backlight_new = 5;
6295 if (backlight_new > 255)
6296 backlight_new = 255;
6297
6298 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006299 output->set_backlight(output, output->backlight_current);
6300}
6301
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006302struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006303 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006304 struct weston_seat *seat;
6305 uint32_t key[2];
6306 int key_released[2];
6307};
6308
6309static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006310debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006311 uint32_t key, uint32_t state)
6312{
6313 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01006314 struct weston_compositor *ec = db->seat->compositor;
6315 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006316 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006317 uint32_t serial;
6318 int send = 0, terminate = 0;
6319 int check_binding = 1;
6320 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01006321 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006322
6323 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
6324 /* Do not run bindings on key releases */
6325 check_binding = 0;
6326
6327 for (i = 0; i < 2; i++)
6328 if (key == db->key[i])
6329 db->key_released[i] = 1;
6330
6331 if (db->key_released[0] && db->key_released[1]) {
6332 /* All key releases been swalled so end the grab */
6333 terminate = 1;
6334 } else if (key != db->key[0] && key != db->key[1]) {
6335 /* Should not swallow release of other keys */
6336 send = 1;
6337 }
6338 } else if (key == db->key[0] && !db->key_released[0]) {
6339 /* Do not check bindings for the first press of the binding
6340 * key. This allows it to be used as a debug shortcut.
6341 * We still need to swallow this event. */
6342 check_binding = 0;
6343 } else if (db->key[1]) {
6344 /* If we already ran a binding don't process another one since
6345 * we can't keep track of all the binding keys that were
6346 * pressed in order to swallow the release events. */
6347 send = 1;
6348 check_binding = 0;
6349 }
6350
6351 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006352 if (weston_compositor_run_debug_binding(ec, db->seat, time,
6353 key, state)) {
6354 /* We ran a binding so swallow the press and keep the
6355 * grab to swallow the released too. */
6356 send = 0;
6357 terminate = 0;
6358 db->key[1] = key;
6359 } else {
6360 /* Terminate the grab since the key pressed is not a
6361 * debug binding key. */
6362 send = 1;
6363 terminate = 1;
6364 }
6365 }
6366
6367 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01006368 serial = wl_display_next_serial(display);
6369 resource_list = &grab->keyboard->focus_resource_list;
6370 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006371 wl_keyboard_send_key(resource, serial, time, key, state);
6372 }
6373 }
6374
6375 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006376 weston_keyboard_end_grab(grab->keyboard);
6377 if (grab->keyboard->input_method_resource)
6378 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006379 free(db);
6380 }
6381}
6382
6383static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006384debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006385 uint32_t mods_depressed, uint32_t mods_latched,
6386 uint32_t mods_locked, uint32_t group)
6387{
6388 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01006389 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006390
Neil Roberts96d790e2013-09-19 17:32:00 +01006391 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006392
Neil Roberts96d790e2013-09-19 17:32:00 +01006393 wl_resource_for_each(resource, resource_list) {
6394 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
6395 mods_latched, mods_locked, group);
6396 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006397}
6398
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006399static void
6400debug_binding_cancel(struct weston_keyboard_grab *grab)
6401{
6402 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
6403
6404 weston_keyboard_end_grab(grab->keyboard);
6405 free(db);
6406}
6407
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006408struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006409 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006410 debug_binding_modifiers,
6411 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006412};
6413
6414static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006415debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006416{
6417 struct debug_binding_grab *grab;
6418
6419 grab = calloc(1, sizeof *grab);
6420 if (!grab)
6421 return;
6422
6423 grab->seat = (struct weston_seat *) seat;
6424 grab->key[0] = key;
6425 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006426 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006427}
6428
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006429static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006430force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006431 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006432{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006433 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006434 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006435 struct desktop_shell *shell = data;
6436 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006437 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006438
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006439 focus_surface = seat->keyboard->focus;
6440 if (!focus_surface)
6441 return;
6442
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006443 wl_signal_emit(&compositor->kill_signal, focus_surface);
6444
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006445 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006446 wl_client_get_credentials(client, &pid, NULL, NULL);
6447
6448 /* Skip clients that we launched ourselves (the credentials of
6449 * the socketpair is ours) */
6450 if (pid == getpid())
6451 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006452
Daniel Stone325fc2d2012-05-30 16:31:58 +01006453 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006454}
6455
6456static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006457workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006458 uint32_t key, void *data)
6459{
6460 struct desktop_shell *shell = data;
6461 unsigned int new_index = shell->workspaces.current;
6462
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006463 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006464 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006465 if (new_index != 0)
6466 new_index--;
6467
6468 change_workspace(shell, new_index);
6469}
6470
6471static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006472workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006473 uint32_t key, void *data)
6474{
6475 struct desktop_shell *shell = data;
6476 unsigned int new_index = shell->workspaces.current;
6477
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006478 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006479 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006480 if (new_index < shell->workspaces.num - 1)
6481 new_index++;
6482
6483 change_workspace(shell, new_index);
6484}
6485
6486static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006487workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006488 uint32_t key, void *data)
6489{
6490 struct desktop_shell *shell = data;
6491 unsigned int new_index;
6492
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006493 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006494 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006495 new_index = key - KEY_F1;
6496 if (new_index >= shell->workspaces.num)
6497 new_index = shell->workspaces.num - 1;
6498
6499 change_workspace(shell, new_index);
6500}
6501
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006502static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006503workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006504 uint32_t key, void *data)
6505{
6506 struct desktop_shell *shell = data;
6507 unsigned int new_index = shell->workspaces.current;
6508
6509 if (shell->locked)
6510 return;
6511
6512 if (new_index != 0)
6513 new_index--;
6514
6515 take_surface_to_workspace_by_seat(shell, seat, new_index);
6516}
6517
6518static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006519workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006520 uint32_t key, void *data)
6521{
6522 struct desktop_shell *shell = data;
6523 unsigned int new_index = shell->workspaces.current;
6524
6525 if (shell->locked)
6526 return;
6527
6528 if (new_index < shell->workspaces.num - 1)
6529 new_index++;
6530
6531 take_surface_to_workspace_by_seat(shell, seat, new_index);
6532}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006533
6534static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006535handle_output_destroy(struct wl_listener *listener, void *data)
6536{
6537 struct shell_output *output_listener =
6538 container_of(listener, struct shell_output, destroy_listener);
6539
6540 wl_list_remove(&output_listener->destroy_listener.link);
6541 wl_list_remove(&output_listener->link);
6542 free(output_listener);
6543}
6544
6545static void
6546create_shell_output(struct desktop_shell *shell,
6547 struct weston_output *output)
6548{
6549 struct shell_output *shell_output;
6550
6551 shell_output = zalloc(sizeof *shell_output);
6552 if (shell_output == NULL)
6553 return;
6554
6555 shell_output->output = output;
6556 shell_output->shell = shell;
6557 shell_output->destroy_listener.notify = handle_output_destroy;
6558 wl_signal_add(&output->destroy_signal,
6559 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006560 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006561}
6562
6563static void
6564handle_output_create(struct wl_listener *listener, void *data)
6565{
6566 struct desktop_shell *shell =
6567 container_of(listener, struct desktop_shell, output_create_listener);
6568 struct weston_output *output = (struct weston_output *)data;
6569
6570 create_shell_output(shell, output);
6571}
6572
6573static void
6574setup_output_destroy_handler(struct weston_compositor *ec,
6575 struct desktop_shell *shell)
6576{
6577 struct weston_output *output;
6578
6579 wl_list_init(&shell->output_list);
6580 wl_list_for_each(output, &ec->output_list, link)
6581 create_shell_output(shell, output);
6582
6583 shell->output_create_listener.notify = handle_output_create;
6584 wl_signal_add(&ec->output_created_signal,
6585 &shell->output_create_listener);
6586}
6587
6588static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006589shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006590{
Tiago Vignattibe143262012-04-16 17:31:41 +03006591 struct desktop_shell *shell =
6592 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006593 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006594 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006595
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006596 if (shell->child.client)
6597 wl_client_destroy(shell->child.client);
6598
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006599 wl_list_remove(&shell->idle_listener.link);
6600 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006601 wl_list_remove(&shell->show_input_panel_listener.link);
6602 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006603
Xiong Zhang6b481422013-10-23 13:58:32 +08006604 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6605 wl_list_remove(&shell_output->destroy_listener.link);
6606 wl_list_remove(&shell_output->link);
6607 free(shell_output);
6608 }
6609
6610 wl_list_remove(&shell->output_create_listener.link);
6611
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006612 wl_array_for_each(ws, &shell->workspaces.array)
6613 workspace_destroy(*ws);
6614 wl_array_release(&shell->workspaces.array);
6615
Pekka Paalanen3c647232011-12-22 13:43:43 +02006616 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006617 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006618 free(shell);
6619}
6620
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006621static void
6622shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6623{
6624 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006625 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006626
6627 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006628 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6629 MODIFIER_CTRL | MODIFIER_ALT,
6630 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01006631 weston_compositor_add_key_binding(ec, KEY_TAB,
6632 MODIFIER_ALT,
6633 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006634 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6635 click_to_activate_binding,
6636 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006637 weston_compositor_add_touch_binding(ec, 0,
6638 touch_to_activate_binding,
6639 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006640 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6641 MODIFIER_SUPER | MODIFIER_ALT,
6642 surface_opacity_binding, NULL);
6643 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6644 MODIFIER_SUPER, zoom_axis_binding,
6645 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006646
6647 /* configurable bindings */
6648 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006649 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6650 zoom_key_binding, NULL);
6651 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6652 zoom_key_binding, NULL);
6653 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6654 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006655 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006656 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6657 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006658
6659 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6660 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6661 rotate_binding, NULL);
6662
Daniel Stone325fc2d2012-05-30 16:31:58 +01006663 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6664 shell);
6665 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6666 ec);
6667 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6668 backlight_binding, ec);
6669 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6670 ec);
6671 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6672 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006673 weston_compositor_add_key_binding(ec, KEY_K, mod,
6674 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006675 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6676 workspace_up_binding, shell);
6677 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6678 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006679 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6680 workspace_move_surface_up_binding,
6681 shell);
6682 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6683 workspace_move_surface_down_binding,
6684 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006685
Daniel Stonedf8133b2013-11-19 11:37:14 +01006686 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6687
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006688 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6689 if (shell->workspaces.num > 1) {
6690 num_workspace_bindings = shell->workspaces.num;
6691 if (num_workspace_bindings > 6)
6692 num_workspace_bindings = 6;
6693 for (i = 0; i < num_workspace_bindings; i++)
6694 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6695 workspace_f_binding,
6696 shell);
6697 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006698
6699 /* Debug bindings */
6700 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6701 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006702}
6703
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006704WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006705module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006706 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006707{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006708 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006709 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006710 struct workspace **pws;
6711 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006712 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006713
Peter Huttererf3d62272013-08-08 11:57:05 +10006714 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006715 if (shell == NULL)
6716 return -1;
6717
Kristian Høgsberg75840622011-09-06 13:48:16 -04006718 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006719
6720 shell->destroy_listener.notify = shell_destroy;
6721 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006722 shell->idle_listener.notify = idle_handler;
6723 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6724 shell->wake_listener.notify = wake_handler;
6725 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006726 shell->show_input_panel_listener.notify = show_input_panels;
6727 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6728 shell->hide_input_panel_listener.notify = hide_input_panels;
6729 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006730 shell->update_input_panel_listener.notify = update_input_panels;
6731 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006732 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006733 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006734 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006735 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006736 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006737 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006738 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006739 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006740 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006741 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006742 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006743
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006744 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006745
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006746 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6747 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006748 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6749 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006750 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006751
6752 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006753 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006754
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006755 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006756
Daniel Stonedf8133b2013-11-19 11:37:14 +01006757 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6758 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6759
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006760 for (i = 0; i < shell->workspaces.num; i++) {
6761 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6762 if (pws == NULL)
6763 return -1;
6764
6765 *pws = workspace_create();
6766 if (*pws == NULL)
6767 return -1;
6768 }
6769 activate_workspace(shell, 0);
6770
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006771 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006772 wl_list_init(&shell->workspaces.animation.link);
6773 shell->workspaces.animation.frame = animate_workspace_change_frame;
6774
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006775 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006776 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006777 return -1;
6778
Rafael Antognollie2a34552013-12-03 15:35:45 -02006779 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6780 shell, bind_xdg_shell) == NULL)
6781 return -1;
6782
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006783 if (wl_global_create(ec->wl_display,
6784 &desktop_shell_interface, 2,
6785 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006786 return -1;
6787
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006788 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6789 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006790 return -1;
6791
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006792 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006793 shell, bind_input_panel) == NULL)
6794 return -1;
6795
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006796 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6797 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006798 return -1;
6799
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006800 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006801
Xiong Zhang6b481422013-10-23 13:58:32 +08006802 setup_output_destroy_handler(ec, shell);
6803
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006804 loop = wl_display_get_event_loop(ec->wl_display);
6805 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006806
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006807 shell->screensaver.timer =
6808 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6809
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006810 wl_list_for_each(seat, &ec->seat_list, link)
6811 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006812
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006813 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006814
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006815 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006816
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006817 return 0;
6818}