blob: a2fcf3dc709bd36934c0afe9e9a80aeafcbd2e94 [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;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200306 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800307 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200308 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800309 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700310 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100311
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500312 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200313 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500314 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200315 } rotation;
316
317 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100318 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500319 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100320 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400321 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500322 } popup;
323
Alex Wu4539b082012-03-01 12:57:46 +0800324 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300325 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400326 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300327 } transient;
328
329 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800330 enum wl_shell_surface_fullscreen_method type;
331 struct weston_transform transform; /* matrix from x, y */
332 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800334 } fullscreen;
335
Scott Moreauff1db4a2012-04-17 19:06:18 -0600336 struct ping_timer *ping_timer;
337
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200338 struct weston_transform workspace_transform;
339
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500340 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500341 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200342 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100343 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400344
345 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200346
347 struct {
348 bool maximized;
349 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200350 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200351 } state, next_state; /* surface states */
352 bool state_changed;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200353};
354
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300355struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400356 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357 struct shell_surface *shsurf;
358 struct wl_listener shsurf_destroy_listener;
359};
360
Rusty Lynch1084da52013-08-15 09:10:08 -0700361struct shell_touch_grab {
362 struct weston_touch_grab grab;
363 struct shell_surface *shsurf;
364 struct wl_listener shsurf_destroy_listener;
365 struct weston_touch *touch;
366};
367
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368struct weston_move_grab {
369 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100370 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500371};
372
Rusty Lynch1084da52013-08-15 09:10:08 -0700373struct weston_touch_move_grab {
374 struct shell_touch_grab base;
375 wl_fixed_t dx, dy;
376};
377
Pekka Paalanen460099f2012-01-20 16:48:25 +0200378struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500380 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200381 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200382 float x;
383 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200384 } center;
385};
386
Giulio Camuffo5085a752013-03-25 21:42:45 +0100387struct shell_seat {
388 struct weston_seat *seat;
389 struct wl_listener seat_destroy_listener;
390
391 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400392 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100393 struct wl_list surfaces_list;
394 struct wl_client *client;
395 int32_t initial_up;
396 } popup_grab;
397};
398
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400399static void
400activate(struct desktop_shell *shell, struct weston_surface *es,
401 struct weston_seat *seat);
402
403static struct workspace *
404get_current_workspace(struct desktop_shell *shell);
405
Alex Wubd3354b2012-04-17 17:20:49 +0800406static struct shell_surface *
407get_shell_surface(struct weston_surface *surface);
408
409static struct desktop_shell *
410shell_surface_get_shell(struct shell_surface *shsurf);
411
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500412static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400413surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500414
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300415static void
416shell_fade_startup(struct desktop_shell *shell);
417
Philip Withnallbecb77e2013-11-25 18:01:30 +0000418static struct shell_seat *
419get_shell_seat(struct weston_seat *seat);
420
Philip Withnall648a4dd2013-11-25 18:01:44 +0000421static void
422shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
423
Alex Wubd3354b2012-04-17 17:20:49 +0800424static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200425shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
426
427static bool
428shell_surface_is_xdg_surface(struct shell_surface *shsurf);
429
430static bool
431shell_surface_is_xdg_popup(struct shell_surface *shsurf);
432
433static void
434shell_surface_set_parent(struct shell_surface *shsurf,
435 struct weston_surface *parent);
436
437static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800438shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
439{
440 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500441 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800442
443 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800446 return false;
447
Jason Ekstranda7af7042013-10-12 22:38:11 -0500448 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
449 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800450 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500451 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800452}
453
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500454static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400455destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300456{
457 struct shell_grab *grab;
458
459 grab = container_of(listener, struct shell_grab,
460 shsurf_destroy_listener);
461
462 grab->shsurf = NULL;
463}
464
Jason Ekstranda7af7042013-10-12 22:38:11 -0500465static struct weston_view *
466get_default_view(struct weston_surface *surface)
467{
468 struct shell_surface *shsurf;
469 struct weston_view *view;
470
471 if (!surface || wl_list_empty(&surface->views))
472 return NULL;
473
474 shsurf = get_shell_surface(surface);
475 if (shsurf)
476 return shsurf->view;
477
478 wl_list_for_each(view, &surface->views, surface_link)
479 if (weston_view_is_mapped(view))
480 return view;
481
482 return container_of(surface->views.next, struct weston_view, surface_link);
483}
484
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300485static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400486popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400487
488static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300489shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400490 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300491 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400492 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300493 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300494{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300495 struct desktop_shell *shell = shsurf->shell;
496
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400497 popup_grab_end(pointer);
498
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300499 grab->grab.interface = interface;
500 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400501 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500502 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400503 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300504
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700505 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400506 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400507 if (shell->child.desktop_shell) {
508 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
509 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510 weston_pointer_set_focus(pointer,
511 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400512 wl_fixed_from_int(0),
513 wl_fixed_from_int(0));
514 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300515}
516
517static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300518shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300519{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700520 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400521 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700522 grab->shsurf->grabbed = 0;
523 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300524
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700525 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300526}
527
528static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700529shell_touch_grab_start(struct shell_touch_grab *grab,
530 const struct weston_touch_grab_interface *interface,
531 struct shell_surface *shsurf,
532 struct weston_touch *touch)
533{
534 struct desktop_shell *shell = shsurf->shell;
535
536 grab->grab.interface = interface;
537 grab->shsurf = shsurf;
538 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
539 wl_signal_add(&shsurf->destroy_signal,
540 &grab->shsurf_destroy_listener);
541
542 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700543 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700544
545 weston_touch_start_grab(touch, &grab->grab);
546 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500547 weston_touch_set_focus(touch->seat,
548 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700549}
550
551static void
552shell_touch_grab_end(struct shell_touch_grab *grab)
553{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700554 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700555 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700556 grab->shsurf->grabbed = 0;
557 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700558
559 weston_touch_end_grab(grab->touch);
560}
561
562static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500563center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800564 struct weston_output *output);
565
Daniel Stone496ca172012-05-30 16:31:42 +0100566static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300567get_modifier(char *modifier)
568{
569 if (!modifier)
570 return MODIFIER_SUPER;
571
572 if (!strcmp("ctrl", modifier))
573 return MODIFIER_CTRL;
574 else if (!strcmp("alt", modifier))
575 return MODIFIER_ALT;
576 else if (!strcmp("super", modifier))
577 return MODIFIER_SUPER;
578 else
579 return MODIFIER_SUPER;
580}
581
Juan Zhaoe10d2792012-04-25 19:09:52 +0800582static enum animation_type
583get_animation_type(char *animation)
584{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800585 if (!strcmp("zoom", animation))
586 return ANIMATION_ZOOM;
587 else if (!strcmp("fade", animation))
588 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100589 else if (!strcmp("dim-layer", animation))
590 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800591 else
592 return ANIMATION_NONE;
593}
594
Alex Wu4539b082012-03-01 12:57:46 +0800595static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400596shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200597{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400598 struct weston_config_section *section;
599 int duration;
600 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200601
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400602 section = weston_config_get_section(shell->compositor->config,
603 "screensaver", NULL, NULL);
604 weston_config_section_get_string(section,
605 "path", &shell->screensaver.path, NULL);
606 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200607 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400608
609 section = weston_config_get_section(shell->compositor->config,
610 "shell", NULL, NULL);
611 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100612 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100613 shell->client = s;
614 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400615 "binding-modifier", &s, "super");
616 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200617 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400618 weston_config_section_get_string(section, "animation", &s, "none");
619 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200620 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700621 weston_config_section_get_string(section,
622 "startup-animation", &s, "fade");
623 shell->startup_animation_type = get_animation_type(s);
624 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700625 if (shell->startup_animation_type == ANIMATION_ZOOM)
626 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100627 weston_config_section_get_string(section, "focus-animation", &s, "none");
628 shell->focus_animation_type = get_animation_type(s);
629 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400630 weston_config_section_get_uint(section, "num-workspaces",
631 &shell->workspaces.num,
632 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200633}
634
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100635static struct weston_output *
636get_default_output(struct weston_compositor *compositor)
637{
638 return container_of(compositor->output_list.next,
639 struct weston_output, link);
640}
641
642
643/* no-op func for checking focus surface */
644static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600645focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100646{
647}
648
649static struct focus_surface *
650get_focus_surface(struct weston_surface *surface)
651{
652 if (surface->configure == focus_surface_configure)
653 return surface->configure_private;
654 else
655 return NULL;
656}
657
658static bool
659is_focus_surface (struct weston_surface *es)
660{
661 return (es->configure == focus_surface_configure);
662}
663
664static bool
665is_focus_view (struct weston_view *view)
666{
667 return is_focus_surface (view->surface);
668}
669
670static struct focus_surface *
671create_focus_surface(struct weston_compositor *ec,
672 struct weston_output *output)
673{
674 struct focus_surface *fsurf = NULL;
675 struct weston_surface *surface = NULL;
676
677 fsurf = malloc(sizeof *fsurf);
678 if (!fsurf)
679 return NULL;
680
681 fsurf->surface = weston_surface_create(ec);
682 surface = fsurf->surface;
683 if (surface == NULL) {
684 free(fsurf);
685 return NULL;
686 }
687
688 surface->configure = focus_surface_configure;
689 surface->output = output;
690 surface->configure_private = fsurf;
691
692 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100693 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600695 surface->width = output->width;
696 surface->height = output->height;
697 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100698 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
699 pixman_region32_fini(&surface->opaque);
700 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
701 output->width, output->height);
702 pixman_region32_fini(&surface->input);
703 pixman_region32_init(&surface->input);
704
705 wl_list_init(&fsurf->workspace_transform.link);
706
707 return fsurf;
708}
709
710static void
711focus_surface_destroy(struct focus_surface *fsurf)
712{
713 weston_surface_destroy(fsurf->surface);
714 free(fsurf);
715}
716
717static void
718focus_animation_done(struct weston_view_animation *animation, void *data)
719{
720 struct workspace *ws = data;
721
722 ws->focus_animation = NULL;
723}
724
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200725static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200726focus_state_destroy(struct focus_state *state)
727{
728 wl_list_remove(&state->seat_destroy_listener.link);
729 wl_list_remove(&state->surface_destroy_listener.link);
730 free(state);
731}
732
733static void
734focus_state_seat_destroy(struct wl_listener *listener, void *data)
735{
736 struct focus_state *state = container_of(listener,
737 struct focus_state,
738 seat_destroy_listener);
739
740 wl_list_remove(&state->link);
741 focus_state_destroy(state);
742}
743
744static void
745focus_state_surface_destroy(struct wl_listener *listener, void *data)
746{
747 struct focus_state *state = container_of(listener,
748 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400749 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400750 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500751 struct weston_surface *main_surface, *next;
752 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200753
Pekka Paalanen01388e22013-04-25 13:57:44 +0300754 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
755
Kristian Høgsberge3778222012-07-31 17:29:30 -0400756 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500757 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
758 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400759 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100760 if (is_focus_view(view))
761 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400762
Jason Ekstranda7af7042013-10-12 22:38:11 -0500763 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400764 break;
765 }
766
Pekka Paalanen01388e22013-04-25 13:57:44 +0300767 /* if the focus was a sub-surface, activate its main surface */
768 if (main_surface != state->keyboard_focus)
769 next = main_surface;
770
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100771 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400772 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100773 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400774 activate(shell, next, state->seat);
775 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100776 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
777 if (state->ws->focus_animation)
778 weston_view_animation_destroy(state->ws->focus_animation);
779
780 state->ws->focus_animation = weston_fade_run(
781 state->ws->fsurf_front->view,
782 state->ws->fsurf_front->view->alpha, 0.0, 300,
783 focus_animation_done, state->ws);
784 }
785
Kristian Høgsberge3778222012-07-31 17:29:30 -0400786 wl_list_remove(&state->link);
787 focus_state_destroy(state);
788 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200789}
790
791static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400792focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200793{
Jonas Ådahl04769742012-06-13 00:01:24 +0200794 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200795
796 state = malloc(sizeof *state);
797 if (state == NULL)
798 return NULL;
799
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100800 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400801 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200802 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400803 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200804
805 state->seat_destroy_listener.notify = focus_state_seat_destroy;
806 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400807 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200808 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400809 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200810
811 return state;
812}
813
Jonas Ådahl8538b222012-08-29 22:13:03 +0200814static struct focus_state *
815ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
816{
817 struct workspace *ws = get_current_workspace(shell);
818 struct focus_state *state;
819
820 wl_list_for_each(state, &ws->focus_list, link)
821 if (state->seat == seat)
822 break;
823
824 if (&state->link == &ws->focus_list)
825 state = focus_state_create(seat, ws);
826
827 return state;
828}
829
Jonas Ådahl04769742012-06-13 00:01:24 +0200830static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400831restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200832{
833 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400834 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200835
836 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400837 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200838
Kristian Høgsberge3148752013-05-06 23:19:49 -0400839 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200840 }
841}
842
843static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200844replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
845 struct weston_seat *seat)
846{
847 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400848 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200849
850 wl_list_for_each(state, &ws->focus_list, link) {
851 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400852 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500853 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200854 return;
855 }
856 }
857}
858
859static void
860drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
861 struct weston_surface *surface)
862{
863 struct focus_state *state;
864
865 wl_list_for_each(state, &ws->focus_list, link)
866 if (state->keyboard_focus == surface)
867 state->keyboard_focus = NULL;
868}
869
870static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100871animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
872 struct weston_view *from, struct weston_view *to)
873{
874 struct weston_output *output;
875 bool focus_surface_created = false;
876
877 /* FIXME: Only support dim animation using two layers */
878 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
879 return;
880
881 output = get_default_output(shell->compositor);
882 if (ws->fsurf_front == NULL && (from || to)) {
883 ws->fsurf_front = create_focus_surface(shell->compositor, output);
884 ws->fsurf_back = create_focus_surface(shell->compositor, output);
885 ws->fsurf_front->view->alpha = 0.0;
886 ws->fsurf_back->view->alpha = 0.0;
887 focus_surface_created = true;
888 } else {
889 wl_list_remove(&ws->fsurf_front->view->layer_link);
890 wl_list_remove(&ws->fsurf_back->view->layer_link);
891 }
892
893 if (ws->focus_animation) {
894 weston_view_animation_destroy(ws->focus_animation);
895 ws->focus_animation = NULL;
896 }
897
898 if (to)
899 wl_list_insert(&to->layer_link,
900 &ws->fsurf_front->view->layer_link);
901 else if (from)
902 wl_list_insert(&ws->layer.view_list,
903 &ws->fsurf_front->view->layer_link);
904
905 if (focus_surface_created) {
906 ws->focus_animation = weston_fade_run(
907 ws->fsurf_front->view,
908 ws->fsurf_front->view->alpha, 0.6, 300,
909 focus_animation_done, ws);
910 } else if (from) {
911 wl_list_insert(&from->layer_link,
912 &ws->fsurf_back->view->layer_link);
913 ws->focus_animation = weston_stable_fade_run(
914 ws->fsurf_front->view, 0.0,
915 ws->fsurf_back->view, 0.6,
916 focus_animation_done, ws);
917 } else if (to) {
918 wl_list_insert(&ws->layer.view_list,
919 &ws->fsurf_back->view->layer_link);
920 ws->focus_animation = weston_stable_fade_run(
921 ws->fsurf_front->view, 0.0,
922 ws->fsurf_back->view, 0.6,
923 focus_animation_done, ws);
924 }
925}
926
927static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200928workspace_destroy(struct workspace *ws)
929{
Jonas Ådahl04769742012-06-13 00:01:24 +0200930 struct focus_state *state, *next;
931
932 wl_list_for_each_safe(state, next, &ws->focus_list, link)
933 focus_state_destroy(state);
934
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100935 if (ws->fsurf_front)
936 focus_surface_destroy(ws->fsurf_front);
937 if (ws->fsurf_back)
938 focus_surface_destroy(ws->fsurf_back);
939
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200940 free(ws);
941}
942
Jonas Ådahl04769742012-06-13 00:01:24 +0200943static void
944seat_destroyed(struct wl_listener *listener, void *data)
945{
946 struct weston_seat *seat = data;
947 struct focus_state *state, *next;
948 struct workspace *ws = container_of(listener,
949 struct workspace,
950 seat_destroyed_listener);
951
952 wl_list_for_each_safe(state, next, &ws->focus_list, link)
953 if (state->seat == seat)
954 wl_list_remove(&state->link);
955}
956
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200957static struct workspace *
958workspace_create(void)
959{
960 struct workspace *ws = malloc(sizeof *ws);
961 if (ws == NULL)
962 return NULL;
963
964 weston_layer_init(&ws->layer, NULL);
965
Jonas Ådahl04769742012-06-13 00:01:24 +0200966 wl_list_init(&ws->focus_list);
967 wl_list_init(&ws->seat_destroyed_listener.link);
968 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100969 ws->fsurf_front = NULL;
970 ws->fsurf_back = NULL;
971 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200972
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200973 return ws;
974}
975
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200976static int
977workspace_is_empty(struct workspace *ws)
978{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980}
981
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200982static struct workspace *
983get_workspace(struct desktop_shell *shell, unsigned int index)
984{
985 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200986 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200987 pws += index;
988 return *pws;
989}
990
991static struct workspace *
992get_current_workspace(struct desktop_shell *shell)
993{
994 return get_workspace(shell, shell->workspaces.current);
995}
996
997static void
998activate_workspace(struct desktop_shell *shell, unsigned int index)
999{
1000 struct workspace *ws;
1001
1002 ws = get_workspace(shell, index);
1003 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1004
1005 shell->workspaces.current = index;
1006}
1007
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001008static unsigned int
1009get_output_height(struct weston_output *output)
1010{
1011 return abs(output->region.extents.y1 - output->region.extents.y2);
1012}
1013
1014static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001016{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017 struct weston_transform *transform;
1018
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001019 if (is_focus_view(view)) {
1020 struct focus_surface *fsurf = get_focus_surface(view->surface);
1021 transform = &fsurf->workspace_transform;
1022 } else {
1023 struct shell_surface *shsurf = get_shell_surface(view->surface);
1024 transform = &shsurf->workspace_transform;
1025 }
1026
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001027 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001029 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001030
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031 weston_matrix_init(&transform->matrix);
1032 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035}
1036
1037static void
1038workspace_translate_out(struct workspace *ws, double fraction)
1039{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001041 unsigned int height;
1042 double d;
1043
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1045 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001046 d = height * fraction;
1047
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001048 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001049 }
1050}
1051
1052static void
1053workspace_translate_in(struct workspace *ws, double fraction)
1054{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001056 unsigned int height;
1057 double d;
1058
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1060 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001061
1062 if (fraction > 0)
1063 d = -(height - height * fraction);
1064 else
1065 d = height + height * fraction;
1066
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001067 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001068 }
1069}
1070
1071static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001072broadcast_current_workspace_state(struct desktop_shell *shell)
1073{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001074 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001075
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001076 wl_resource_for_each(resource, &shell->workspaces.client_list)
1077 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001078 shell->workspaces.current,
1079 shell->workspaces.num);
1080}
1081
1082static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001083reverse_workspace_change_animation(struct desktop_shell *shell,
1084 unsigned int index,
1085 struct workspace *from,
1086 struct workspace *to)
1087{
1088 shell->workspaces.current = index;
1089
1090 shell->workspaces.anim_to = to;
1091 shell->workspaces.anim_from = from;
1092 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1093 shell->workspaces.anim_timestamp = 0;
1094
Scott Moreau4272e632012-08-13 09:58:41 -06001095 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001096}
1097
1098static void
1099workspace_deactivate_transforms(struct workspace *ws)
1100{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001102 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001103
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001105 if (is_focus_view(view)) {
1106 struct focus_surface *fsurf = get_focus_surface(view->surface);
1107 transform = &fsurf->workspace_transform;
1108 } else {
1109 struct shell_surface *shsurf = get_shell_surface(view->surface);
1110 transform = &shsurf->workspace_transform;
1111 }
1112
1113 if (!wl_list_empty(&transform->link)) {
1114 wl_list_remove(&transform->link);
1115 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001116 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001117 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001118 }
1119}
1120
1121static void
1122finish_workspace_change_animation(struct desktop_shell *shell,
1123 struct workspace *from,
1124 struct workspace *to)
1125{
Scott Moreau4272e632012-08-13 09:58:41 -06001126 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127
1128 wl_list_remove(&shell->workspaces.animation.link);
1129 workspace_deactivate_transforms(from);
1130 workspace_deactivate_transforms(to);
1131 shell->workspaces.anim_to = NULL;
1132
1133 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1134}
1135
1136static void
1137animate_workspace_change_frame(struct weston_animation *animation,
1138 struct weston_output *output, uint32_t msecs)
1139{
1140 struct desktop_shell *shell =
1141 container_of(animation, struct desktop_shell,
1142 workspaces.animation);
1143 struct workspace *from = shell->workspaces.anim_from;
1144 struct workspace *to = shell->workspaces.anim_to;
1145 uint32_t t;
1146 double x, y;
1147
1148 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1149 finish_workspace_change_animation(shell, from, to);
1150 return;
1151 }
1152
1153 if (shell->workspaces.anim_timestamp == 0) {
1154 if (shell->workspaces.anim_current == 0.0)
1155 shell->workspaces.anim_timestamp = msecs;
1156 else
1157 shell->workspaces.anim_timestamp =
1158 msecs -
1159 /* Invers of movement function 'y' below. */
1160 (asin(1.0 - shell->workspaces.anim_current) *
1161 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1162 M_2_PI);
1163 }
1164
1165 t = msecs - shell->workspaces.anim_timestamp;
1166
1167 /*
1168 * x = [0, π/2]
1169 * y(x) = sin(x)
1170 */
1171 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1172 y = sin(x);
1173
1174 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001175 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176
1177 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1178 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1179 shell->workspaces.anim_current = y;
1180
Scott Moreau4272e632012-08-13 09:58:41 -06001181 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001183 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001184 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185}
1186
1187static void
1188animate_workspace_change(struct desktop_shell *shell,
1189 unsigned int index,
1190 struct workspace *from,
1191 struct workspace *to)
1192{
1193 struct weston_output *output;
1194
1195 int dir;
1196
1197 if (index > shell->workspaces.current)
1198 dir = -1;
1199 else
1200 dir = 1;
1201
1202 shell->workspaces.current = index;
1203
1204 shell->workspaces.anim_dir = dir;
1205 shell->workspaces.anim_from = from;
1206 shell->workspaces.anim_to = to;
1207 shell->workspaces.anim_current = 0.0;
1208 shell->workspaces.anim_timestamp = 0;
1209
1210 output = container_of(shell->compositor->output_list.next,
1211 struct weston_output, link);
1212 wl_list_insert(&output->animation_list,
1213 &shell->workspaces.animation.link);
1214
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001215 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001216
1217 workspace_translate_in(to, 0);
1218
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001219 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001220
Scott Moreau4272e632012-08-13 09:58:41 -06001221 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001222}
1223
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001224static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001225update_workspace(struct desktop_shell *shell, unsigned int index,
1226 struct workspace *from, struct workspace *to)
1227{
1228 shell->workspaces.current = index;
1229 wl_list_insert(&from->layer.link, &to->layer.link);
1230 wl_list_remove(&from->layer.link);
1231}
1232
1233static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001234change_workspace(struct desktop_shell *shell, unsigned int index)
1235{
1236 struct workspace *from;
1237 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001238 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001239
1240 if (index == shell->workspaces.current)
1241 return;
1242
1243 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001245 return;
1246
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001247 from = get_current_workspace(shell);
1248 to = get_workspace(shell, index);
1249
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001250 if (shell->workspaces.anim_from == to &&
1251 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001252 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001253 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001254 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001255 return;
1256 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001257
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001258 if (shell->workspaces.anim_to != NULL)
1259 finish_workspace_change_animation(shell,
1260 shell->workspaces.anim_from,
1261 shell->workspaces.anim_to);
1262
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001263 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001264
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001265 if (shell->focus_animation_type != ANIMATION_NONE) {
1266 wl_list_for_each(state, &from->focus_list, link)
1267 if (state->keyboard_focus)
1268 animate_focus_change(shell, from,
1269 get_default_view(state->keyboard_focus), NULL);
1270
1271 wl_list_for_each(state, &to->focus_list, link)
1272 if (state->keyboard_focus)
1273 animate_focus_change(shell, to,
1274 NULL, get_default_view(state->keyboard_focus));
1275 }
1276
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001277 if (workspace_is_empty(to) && workspace_is_empty(from))
1278 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001279 else
1280 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001281
1282 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001283}
1284
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001285static bool
1286workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1287{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 struct wl_list *e;
1290
1291 if (wl_list_empty(list))
1292 return false;
1293
1294 e = list->next;
1295
1296 if (e->next != list)
1297 return false;
1298
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001300}
1301
1302static void
Philip Withnall659163d2013-11-25 18:01:36 +00001303move_surface_to_workspace(struct desktop_shell *shell,
1304 struct shell_surface *shsurf,
1305 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001306{
1307 struct workspace *from;
1308 struct workspace *to;
1309 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001310 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001311 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001312
1313 if (workspace == shell->workspaces.current)
1314 return;
1315
Philip Withnall659163d2013-11-25 18:01:36 +00001316 view = get_default_view(shsurf->surface);
1317 if (!view)
1318 return;
1319
1320 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1321
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001322 if (workspace >= shell->workspaces.num)
1323 workspace = shell->workspaces.num - 1;
1324
Jonas Ådahle9d22502012-08-29 22:13:01 +02001325 from = get_current_workspace(shell);
1326 to = get_workspace(shell, workspace);
1327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 wl_list_remove(&view->layer_link);
1329 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001330
Philip Withnall648a4dd2013-11-25 18:01:44 +00001331 shell_surface_update_child_surface_layers(shsurf);
1332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001334 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1335 if (!seat->keyboard)
1336 continue;
1337
1338 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001340 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001341 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001342
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001344}
1345
1346static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001347take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001348 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001349 unsigned int index)
1350{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001351 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001353 struct shell_surface *shsurf;
1354 struct workspace *from;
1355 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001356 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001357
Pekka Paalanen01388e22013-04-25 13:57:44 +03001358 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 view = get_default_view(surface);
1360 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001361 index == shell->workspaces.current ||
1362 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001363 return;
1364
1365 from = get_current_workspace(shell);
1366 to = get_workspace(shell, index);
1367
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 wl_list_remove(&view->layer_link);
1369 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001370
Philip Withnall659163d2013-11-25 18:01:36 +00001371 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001372 if (shsurf != NULL)
1373 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001374
Jonas Ådahle9d22502012-08-29 22:13:01 +02001375 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001376 drop_focus_state(shell, from, surface);
1377
1378 if (shell->workspaces.anim_from == to &&
1379 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001380 wl_list_remove(&to->layer.link);
1381 wl_list_insert(from->layer.link.prev, &to->layer.link);
1382
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001383 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001384 broadcast_current_workspace_state(shell);
1385
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001386 return;
1387 }
1388
1389 if (shell->workspaces.anim_to != NULL)
1390 finish_workspace_change_animation(shell,
1391 shell->workspaces.anim_from,
1392 shell->workspaces.anim_to);
1393
1394 if (workspace_is_empty(from) &&
1395 workspace_has_only(to, surface))
1396 update_workspace(shell, index, from, to);
1397 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001398 if (shsurf != NULL &&
1399 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001400 wl_list_insert(&shell->workspaces.anim_sticky_list,
1401 &shsurf->workspace_transform.link);
1402
1403 animate_workspace_change(shell, index, from, to);
1404 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001405
1406 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001407
1408 state = ensure_focus_state(shell, seat);
1409 if (state != NULL)
1410 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001411}
1412
1413static void
1414workspace_manager_move_surface(struct wl_client *client,
1415 struct wl_resource *resource,
1416 struct wl_resource *surface_resource,
1417 uint32_t workspace)
1418{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001419 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001420 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001421 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001422 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001423 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001424
Pekka Paalanen01388e22013-04-25 13:57:44 +03001425 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001426 shell_surface = get_shell_surface(main_surface);
1427 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001429
1430 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001431}
1432
1433static const struct workspace_manager_interface workspace_manager_implementation = {
1434 workspace_manager_move_surface,
1435};
1436
1437static void
1438unbind_resource(struct wl_resource *resource)
1439{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001440 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001441}
1442
1443static void
1444bind_workspace_manager(struct wl_client *client,
1445 void *data, uint32_t version, uint32_t id)
1446{
1447 struct desktop_shell *shell = data;
1448 struct wl_resource *resource;
1449
Jason Ekstranda85118c2013-06-27 20:17:02 -05001450 resource = wl_resource_create(client,
1451 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001452
1453 if (resource == NULL) {
1454 weston_log("couldn't add workspace manager object");
1455 return;
1456 }
1457
Jason Ekstranda85118c2013-06-27 20:17:02 -05001458 wl_resource_set_implementation(resource,
1459 &workspace_manager_implementation,
1460 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001461 wl_list_insert(&shell->workspaces.client_list,
1462 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001463
1464 workspace_manager_send_state(resource,
1465 shell->workspaces.current,
1466 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001467}
1468
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001469static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001470touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1471 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1472{
1473}
1474
1475static void
1476touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1477{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001478 struct weston_touch_move_grab *move =
1479 (struct weston_touch_move_grab *) container_of(
1480 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001481
Jonas Ådahl9484b692013-12-02 22:05:03 +01001482 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001483 shell_touch_grab_end(&move->base);
1484 free(move);
1485 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001486}
1487
1488static void
1489touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1490 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1491{
1492 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1493 struct shell_surface *shsurf = move->base.shsurf;
1494 struct weston_surface *es;
1495 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1496 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1497
1498 if (!shsurf)
1499 return;
1500
1501 es = shsurf->surface;
1502
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001503 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001504
1505 weston_compositor_schedule_repaint(es->compositor);
1506}
1507
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001508static void
1509touch_move_grab_cancel(struct weston_touch_grab *grab)
1510{
1511 struct weston_touch_move_grab *move =
1512 (struct weston_touch_move_grab *) container_of(
1513 grab, struct shell_touch_grab, grab);
1514
1515 shell_touch_grab_end(&move->base);
1516 free(move);
1517}
1518
Rusty Lynch1084da52013-08-15 09:10:08 -07001519static const struct weston_touch_grab_interface touch_move_grab_interface = {
1520 touch_move_grab_down,
1521 touch_move_grab_up,
1522 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001523 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001524};
1525
1526static int
1527surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1528{
1529 struct weston_touch_move_grab *move;
1530
1531 if (!shsurf)
1532 return -1;
1533
Rafael Antognolli03b16592013-12-03 15:35:42 -02001534 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001535 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001536 if (shsurf->grabbed)
1537 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001538
1539 move = malloc(sizeof *move);
1540 if (!move)
1541 return -1;
1542
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001544 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001546 seat->touch->grab_y;
1547
1548 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1549 seat->touch);
1550
1551 return 0;
1552}
1553
1554static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001555noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001556{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001557}
1558
1559static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001560move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1561 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001562{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001563 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001564 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001565 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001566 int dx, dy;
1567
1568 weston_pointer_move(pointer, x, y);
1569 dx = wl_fixed_to_int(pointer->x + move->dx);
1570 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001571
1572 if (!shsurf)
1573 return;
1574
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001575 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001576
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001578}
1579
1580static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001581move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001582 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001583{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001584 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1585 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001586 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001587 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001588
Daniel Stone4dbadb12012-05-30 16:31:51 +01001589 if (pointer->button_count == 0 &&
1590 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001591 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001592 free(grab);
1593 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001594}
1595
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001596static void
1597move_grab_cancel(struct weston_pointer_grab *grab)
1598{
1599 struct shell_grab *shell_grab =
1600 container_of(grab, struct shell_grab, grab);
1601
1602 shell_grab_end(shell_grab);
1603 free(grab);
1604}
1605
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001606static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001607 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001608 move_grab_motion,
1609 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001610 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001611};
1612
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001613static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001614surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001615{
1616 struct weston_move_grab *move;
1617
1618 if (!shsurf)
1619 return -1;
1620
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001621 if (shsurf->grabbed)
1622 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001623 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001624 return 0;
1625
1626 move = malloc(sizeof *move);
1627 if (!move)
1628 return -1;
1629
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001631 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001633 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001634
1635 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001636 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001637
1638 return 0;
1639}
1640
1641static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001642common_surface_move(struct wl_resource *resource,
1643 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001645 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001646 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001647 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001648
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001649 if (seat->pointer &&
1650 seat->pointer->button_count > 0 &&
1651 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001653 if ((surface == shsurf->surface) &&
1654 (surface_move(shsurf, seat) < 0))
1655 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001656 } else if (seat->touch &&
1657 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001659 if ((surface == shsurf->surface) &&
1660 (surface_touch_move(shsurf, seat) < 0))
1661 wl_resource_post_no_memory(resource);
1662 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001663}
1664
Rafael Antognollie2a34552013-12-03 15:35:45 -02001665static void
1666shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1667 struct wl_resource *seat_resource, uint32_t serial)
1668{
1669 common_surface_move(resource, seat_resource, serial);
1670}
1671
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001672struct weston_resize_grab {
1673 struct shell_grab base;
1674 uint32_t edges;
1675 int32_t width, height;
1676};
1677
1678static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001679resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1680 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001681{
1682 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001683 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001684 struct shell_surface *shsurf = resize->base.shsurf;
1685 int32_t width, height;
1686 wl_fixed_t from_x, from_y;
1687 wl_fixed_t to_x, to_y;
1688
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001689 weston_pointer_move(pointer, x, y);
1690
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001691 if (!shsurf)
1692 return;
1693
Jason Ekstranda7af7042013-10-12 22:38:11 -05001694 weston_view_from_global_fixed(shsurf->view,
1695 pointer->grab_x, pointer->grab_y,
1696 &from_x, &from_y);
1697 weston_view_from_global_fixed(shsurf->view,
1698 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699
1700 width = resize->width;
1701 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1702 width += wl_fixed_to_int(from_x - to_x);
1703 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1704 width += wl_fixed_to_int(to_x - from_x);
1705 }
1706
1707 height = resize->height;
1708 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1709 height += wl_fixed_to_int(from_y - to_y);
1710 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1711 height += wl_fixed_to_int(to_y - from_y);
1712 }
1713
1714 shsurf->client->send_configure(shsurf->surface,
1715 resize->edges, width, height);
1716}
1717
1718static void
1719send_configure(struct weston_surface *surface,
1720 uint32_t edges, int32_t width, int32_t height)
1721{
1722 struct shell_surface *shsurf = get_shell_surface(surface);
1723
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001724 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725 edges, width, height);
1726}
1727
1728static const struct weston_shell_client shell_client = {
1729 send_configure
1730};
1731
1732static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001733resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001734 uint32_t time, uint32_t button, uint32_t state_w)
1735{
1736 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001737 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001738 enum wl_pointer_button_state state = state_w;
1739
1740 if (pointer->button_count == 0 &&
1741 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1742 shell_grab_end(&resize->base);
1743 free(grab);
1744 }
1745}
1746
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001747static void
1748resize_grab_cancel(struct weston_pointer_grab *grab)
1749{
1750 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1751
1752 shell_grab_end(&resize->base);
1753 free(grab);
1754}
1755
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001756static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001757 noop_grab_focus,
1758 resize_grab_motion,
1759 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001760 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001761};
1762
Giulio Camuffob8366642013-04-25 13:57:46 +03001763/*
1764 * Returns the bounding box of a surface and all its sub-surfaces,
1765 * in the surface coordinates system. */
1766static void
1767surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1768 int32_t *y, int32_t *w, int32_t *h) {
1769 pixman_region32_t region;
1770 pixman_box32_t *box;
1771 struct weston_subsurface *subsurface;
1772
1773 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 surface->width,
1775 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001776
1777 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1778 pixman_region32_union_rect(&region, &region,
1779 subsurface->position.x,
1780 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781 subsurface->surface->width,
1782 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001783 }
1784
1785 box = pixman_region32_extents(&region);
1786 if (x)
1787 *x = box->x1;
1788 if (y)
1789 *y = box->y1;
1790 if (w)
1791 *w = box->x2 - box->x1;
1792 if (h)
1793 *h = box->y2 - box->y1;
1794
1795 pixman_region32_fini(&region);
1796}
1797
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001798static int
1799surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001800 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001801{
1802 struct weston_resize_grab *resize;
1803
Rafael Antognolli03b16592013-12-03 15:35:42 -02001804 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001805 return 0;
1806
1807 if (edges == 0 || edges > 15 ||
1808 (edges & 3) == 3 || (edges & 12) == 12)
1809 return 0;
1810
1811 resize = malloc(sizeof *resize);
1812 if (!resize)
1813 return -1;
1814
1815 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001816 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1817 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001818
1819 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001820 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001821
1822 return 0;
1823}
1824
1825static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001826common_surface_resize(struct wl_resource *resource,
1827 struct wl_resource *seat_resource, uint32_t serial,
1828 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001829{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001830 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001831 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001832 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001833
Rafael Antognolli03b16592013-12-03 15:35:42 -02001834 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001835 return;
1836
Jason Ekstranda7af7042013-10-12 22:38:11 -05001837 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001838 if (seat->pointer->button_count == 0 ||
1839 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001840 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001841 return;
1842
Kristian Høgsberge3148752013-05-06 23:19:49 -04001843 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001844 wl_resource_post_no_memory(resource);
1845}
1846
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001847static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001848shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1849 struct wl_resource *seat_resource, uint32_t serial,
1850 uint32_t edges)
1851{
1852 common_surface_resize(resource, seat_resource, serial, edges);
1853}
1854
1855static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001856end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1857
1858static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001859busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001860{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001861 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001862 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001864 wl_fixed_t sx, sy;
1865
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866 view = weston_compositor_pick_view(pointer->seat->compositor,
1867 pointer->x, pointer->y,
1868 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001869
Jason Ekstranda7af7042013-10-12 22:38:11 -05001870 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001871 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001872}
1873
1874static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001875busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1876 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001877{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001878 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001879}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001880
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001881static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001882busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001883 uint32_t time, uint32_t button, uint32_t state)
1884{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001885 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001886 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001887 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001888
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001889 if (shsurf && button == BTN_LEFT && state) {
1890 activate(shsurf->shell, shsurf->surface, seat);
1891 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001892 } else if (shsurf && button == BTN_RIGHT && state) {
1893 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001894 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001895 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001896}
1897
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001898static void
1899busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1900{
1901 struct shell_grab *grab = (struct shell_grab *) base;
1902
1903 shell_grab_end(grab);
1904 free(grab);
1905}
1906
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001907static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001908 busy_cursor_grab_focus,
1909 busy_cursor_grab_motion,
1910 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001911 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001912};
1913
1914static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001915set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001916{
1917 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001918
1919 grab = malloc(sizeof *grab);
1920 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001921 return;
1922
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001923 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1924 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001925}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001926
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001927static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001928end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001929{
1930 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1931
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001932 if (grab->grab.interface == &busy_cursor_grab_interface &&
1933 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001934 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001935 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001936 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001937}
1938
Scott Moreau9521d5e2012-04-19 13:06:17 -06001939static void
1940ping_timer_destroy(struct shell_surface *shsurf)
1941{
1942 if (!shsurf || !shsurf->ping_timer)
1943 return;
1944
1945 if (shsurf->ping_timer->source)
1946 wl_event_source_remove(shsurf->ping_timer->source);
1947
1948 free(shsurf->ping_timer);
1949 shsurf->ping_timer = NULL;
1950}
1951
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001952static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001953ping_timeout_handler(void *data)
1954{
1955 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001956 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001957
Scott Moreau9521d5e2012-04-19 13:06:17 -06001958 /* Client is not responding */
1959 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001960
1961 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001962 if (seat->pointer->focus &&
1963 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001964 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001965
1966 return 1;
1967}
1968
1969static void
1970ping_handler(struct weston_surface *surface, uint32_t serial)
1971{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001972 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001973 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001974 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001975
1976 if (!shsurf)
1977 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001978 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001979 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001980
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001981 if (shsurf->surface == shsurf->shell->grab_surface)
1982 return;
1983
Scott Moreauff1db4a2012-04-17 19:06:18 -06001984 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001985 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001986 if (!shsurf->ping_timer)
1987 return;
1988
1989 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001990 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1991 shsurf->ping_timer->source =
1992 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1993 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1994
Rafael Antognollie2a34552013-12-03 15:35:45 -02001995 if (shell_surface_is_wl_shell_surface(shsurf))
1996 wl_shell_surface_send_ping(shsurf->resource, serial);
1997 else if (shell_surface_is_xdg_surface(shsurf))
1998 xdg_surface_send_ping(shsurf->resource, serial);
1999 else if (shell_surface_is_xdg_popup(shsurf))
2000 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002001 }
2002}
2003
2004static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002005handle_pointer_focus(struct wl_listener *listener, void *data)
2006{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002007 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002009 struct weston_compositor *compositor;
2010 struct shell_surface *shsurf;
2011 uint32_t serial;
2012
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002014 return;
2015
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 compositor = view->surface->compositor;
2017 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002018
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03002019 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002020 set_busy_cursor(shsurf, pointer);
2021 } else {
2022 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002024 }
2025}
2026
2027static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002028create_pointer_focus_listener(struct weston_seat *seat)
2029{
2030 struct wl_listener *listener;
2031
Kristian Høgsberge3148752013-05-06 23:19:49 -04002032 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002033 return;
2034
2035 listener = malloc(sizeof *listener);
2036 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002037 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04002038}
2039
2040static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002041xdg_surface_set_transient_for(struct wl_client *client,
2042 struct wl_resource *resource,
2043 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06002044{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002045 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002046 struct weston_surface *parent =
2047 wl_resource_get_user_data(parent_resource);
2048
2049 shell_surface_set_parent(shsurf, parent);
2050}
2051
2052static void
2053surface_pong(struct shell_surface *shsurf, uint32_t serial)
2054{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002055 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002056 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002057
Kristian Høgsberg92374e12012-08-11 22:39:12 -04002058 if (shsurf->ping_timer == NULL)
2059 /* Just ignore unsolicited pong. */
2060 return;
2061
Scott Moreauff1db4a2012-04-17 19:06:18 -06002062 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002063 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02002064 wl_list_for_each(seat, &ec->seat_list, link) {
2065 if(seat->pointer)
2066 end_busy_cursor(shsurf, seat->pointer);
2067 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06002068 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002069 }
2070}
2071
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002072static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002073shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
2074 uint32_t serial)
2075{
2076 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2077
2078 surface_pong(shsurf, serial);
2079
2080}
2081
2082static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002083set_title(struct shell_surface *shsurf, const char *title)
2084{
2085 free(shsurf->title);
2086 shsurf->title = strdup(title);
2087}
2088
2089static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002090shell_surface_set_title(struct wl_client *client,
2091 struct wl_resource *resource, const char *title)
2092{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002093 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002094
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002095 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002096}
2097
2098static void
2099shell_surface_set_class(struct wl_client *client,
2100 struct wl_resource *resource, const char *class)
2101{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002102 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002103
2104 free(shsurf->class);
2105 shsurf->class = strdup(class);
2106}
2107
Alex Wu4539b082012-03-01 12:57:46 +08002108static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002109restore_output_mode(struct weston_output *output)
2110{
Hardening57388e42013-09-18 23:56:36 +02002111 if (output->current_mode != output->original_mode ||
2112 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002113 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002114 output->original_mode,
2115 output->original_scale,
2116 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002117}
2118
2119static void
2120restore_all_output_modes(struct weston_compositor *compositor)
2121{
2122 struct weston_output *output;
2123
2124 wl_list_for_each(output, &compositor->output_list, link)
2125 restore_output_mode(output);
2126}
2127
Philip Withnallbecb77e2013-11-25 18:01:30 +00002128static int
2129get_output_panel_height(struct desktop_shell *shell,
2130 struct weston_output *output)
2131{
2132 struct weston_view *view;
2133 int panel_height = 0;
2134
2135 if (!output)
2136 return 0;
2137
2138 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2139 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002140 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002141 break;
2142 }
2143 }
2144
2145 return panel_height;
2146}
2147
Philip Withnall07926d92013-11-25 18:01:40 +00002148/* The surface will be inserted into the list immediately after the link
2149 * returned by this function (i.e. will be stacked immediately above the
2150 * returned link). */
2151static struct wl_list *
2152shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2153{
2154 struct workspace *ws;
2155
2156 switch (shsurf->type) {
Philip Withnallda704d92013-11-25 18:01:46 +00002157 case SHELL_SURFACE_POPUP: {
2158 /* Popups should go at the front of the workspace of their
2159 * parent surface, rather than just in front of the parent. This
2160 * fixes the situation where there are two top-level windows:
2161 * - Above
2162 * - Below
2163 * and a pop-up menu is created for 'Below'. We want:
2164 * - Popup
2165 * - Above
2166 * - Below
2167 * not:
2168 * - Above
2169 * - Popup
2170 * - Below
2171 */
2172 struct shell_surface *parent_shsurf;
2173
2174 parent_shsurf = get_shell_surface(shsurf->parent);
2175 if (parent_shsurf != NULL)
2176 return shell_surface_calculate_layer_link(parent_shsurf);
2177
2178 break;
2179 }
2180
Rafael Antognolli03b16592013-12-03 15:35:42 -02002181 case SHELL_SURFACE_TOPLEVEL: {
Rafael Antognollied207b42013-12-03 15:35:43 -02002182 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002183 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002184 } else if (shsurf->parent) {
2185 /* Move the surface to its parent layer so that
2186 * surfaces which are transient for fullscreen surfaces
2187 * don't get hidden by the fullscreen surfaces.
2188 * However, unlike popups, transient surfaces are
2189 * stacked in front of their parent but not in front of
2190 * other surfaces of the same type. */
2191 struct weston_view *parent;
2192
2193 /* TODO: Handle a parent with multiple views */
2194 parent = get_default_view(shsurf->parent);
2195 if (parent)
2196 return parent->layer_link.prev;
2197 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002198 break;
2199 }
Philip Withnall07926d92013-11-25 18:01:40 +00002200
2201 case SHELL_SURFACE_XWAYLAND:
Philip Withnall07926d92013-11-25 18:01:40 +00002202 case SHELL_SURFACE_NONE:
2203 default:
2204 /* Go to the fallback, below. */
2205 break;
2206 }
2207
2208 /* Move the surface to a normal workspace layer so that surfaces
2209 * which were previously fullscreen or transient are no longer
2210 * rendered on top. */
2211 ws = get_current_workspace(shsurf->shell);
2212 return &ws->layer.view_list;
2213}
2214
Philip Withnall648a4dd2013-11-25 18:01:44 +00002215static void
2216shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2217{
2218 struct shell_surface *child;
2219
2220 /* Move the child layers to the same workspace as shsurf. They will be
2221 * stacked above shsurf. */
2222 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2223 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2224 weston_view_geometry_dirty(child->view);
2225 wl_list_remove(&child->view->layer_link);
2226 wl_list_insert(shsurf->view->layer_link.prev,
2227 &child->view->layer_link);
2228 weston_view_geometry_dirty(child->view);
2229 weston_surface_damage(child->surface);
2230
2231 /* Recurse. We don’t expect this to recurse very far (if
2232 * at all) because that would imply we have transient
2233 * (or popup) children of transient surfaces, which
2234 * would be unusual. */
2235 shell_surface_update_child_surface_layers(child);
2236 }
2237 }
2238}
2239
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002240/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002241 * geometry to ensure the changes are redrawn.
2242 *
2243 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2244 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002245static void
2246shell_surface_update_layer(struct shell_surface *shsurf)
2247{
2248 struct wl_list *new_layer_link;
2249
2250 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2251
2252 if (new_layer_link == &shsurf->view->layer_link)
2253 return;
2254
2255 weston_view_geometry_dirty(shsurf->view);
2256 wl_list_remove(&shsurf->view->layer_link);
2257 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2258 weston_view_geometry_dirty(shsurf->view);
2259 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002260
2261 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002262}
2263
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002264static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002265shell_surface_set_parent(struct shell_surface *shsurf,
2266 struct weston_surface *parent)
2267{
2268 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002269
2270 wl_list_remove(&shsurf->children_link);
2271 wl_list_init(&shsurf->children_link);
2272
2273 /* Insert into the parent surface’s child list. */
2274 if (parent != NULL) {
2275 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2276 if (parent_shsurf != NULL)
2277 wl_list_insert(&parent_shsurf->children_list,
2278 &shsurf->children_link);
2279 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002280}
2281
2282static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002283shell_surface_set_output(struct shell_surface *shsurf,
2284 struct weston_output *output)
2285{
2286 struct weston_surface *es = shsurf->surface;
2287
2288 /* get the default output, if the client set it as NULL
2289 check whether the ouput is available */
2290 if (output)
2291 shsurf->output = output;
2292 else if (es->output)
2293 shsurf->output = es->output;
2294 else
2295 shsurf->output = get_default_output(es->compositor);
2296}
2297
2298static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002299surface_clear_next_states(struct shell_surface *shsurf)
2300{
2301 shsurf->next_state.maximized = false;
2302 shsurf->next_state.fullscreen = false;
2303
2304 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2305 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2306 shsurf->state_changed = true;
2307}
2308
2309static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002310set_toplevel(struct shell_surface *shsurf)
2311{
2312 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002313
2314 /* The layer_link is updated in set_surface_type(),
2315 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002316}
2317
2318static void
2319shell_surface_set_toplevel(struct wl_client *client,
2320 struct wl_resource *resource)
2321{
2322 struct shell_surface *surface = wl_resource_get_user_data(resource);
2323
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002324 shell_surface_set_parent(surface, NULL);
2325
Rafael Antognolli03b16592013-12-03 15:35:42 -02002326 surface_clear_next_states(surface);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002327 set_toplevel(surface);
2328}
2329
2330static void
2331set_transient(struct shell_surface *shsurf,
2332 struct weston_surface *parent, int x, int y, uint32_t flags)
2333{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002334 assert(parent != NULL);
2335
Philip Withnallbecb77e2013-11-25 18:01:30 +00002336 shsurf->transient.x = x;
2337 shsurf->transient.y = y;
2338 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002339
Rafael Antognollied207b42013-12-03 15:35:43 -02002340 shsurf->next_state.relative = true;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002341
2342 /* The layer_link is updated in set_surface_type(),
2343 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002344}
2345
2346static void
2347shell_surface_set_transient(struct wl_client *client,
2348 struct wl_resource *resource,
2349 struct wl_resource *parent_resource,
2350 int x, int y, uint32_t flags)
2351{
2352 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2353 struct weston_surface *parent =
2354 wl_resource_get_user_data(parent_resource);
2355
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002356 shell_surface_set_parent(shsurf, parent);
2357
Rafael Antognolli03b16592013-12-03 15:35:42 -02002358 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002359 set_transient(shsurf, parent, x, y, flags);
2360}
2361
2362static void
2363set_fullscreen(struct shell_surface *shsurf,
2364 uint32_t method,
2365 uint32_t framerate,
2366 struct weston_output *output)
2367{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002368 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002369
2370 shsurf->fullscreen_output = shsurf->output;
2371 shsurf->fullscreen.type = method;
2372 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002373
Rafael Antognolli03b16592013-12-03 15:35:42 -02002374 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002375
2376 shsurf->client->send_configure(shsurf->surface, 0,
2377 shsurf->output->width,
2378 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002379
2380 /* The layer_link is updated in set_surface_type(),
2381 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002382}
2383
2384static void
2385unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002386{
Philip Withnallf85fe842013-11-25 18:01:37 +00002387 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002388 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2389 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002390 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002391 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002392 shsurf->fullscreen_output = NULL;
2393
Alex Wu4539b082012-03-01 12:57:46 +08002394 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2395 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002396
Alex Wu4539b082012-03-01 12:57:46 +08002397 wl_list_remove(&shsurf->fullscreen.transform.link);
2398 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002399
Jason Ekstranda7af7042013-10-12 22:38:11 -05002400 if (shsurf->fullscreen.black_view)
2401 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2402 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002403
Jason Ekstranda7af7042013-10-12 22:38:11 -05002404 weston_view_set_position(shsurf->view,
2405 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002406 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002407 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002408 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002409 shsurf->saved_rotation_valid = false;
2410 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002411
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002412 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002413}
2414
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002415static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002416shell_surface_set_fullscreen(struct wl_client *client,
2417 struct wl_resource *resource,
2418 uint32_t method,
2419 uint32_t framerate,
2420 struct wl_resource *output_resource)
2421{
2422 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2423 struct weston_output *output;
2424
2425 if (output_resource)
2426 output = wl_resource_get_user_data(output_resource);
2427 else
2428 output = NULL;
2429
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002430 shell_surface_set_parent(shsurf, NULL);
2431
Rafael Antognolli03b16592013-12-03 15:35:42 -02002432 surface_clear_next_states(shsurf);
2433 shsurf->next_state.fullscreen = true;
2434 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002435 set_fullscreen(shsurf, method, framerate, output);
2436}
2437
2438static void
2439set_popup(struct shell_surface *shsurf,
2440 struct weston_surface *parent,
2441 struct weston_seat *seat,
2442 uint32_t serial,
2443 int32_t x,
2444 int32_t y)
2445{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002446 assert(parent != NULL);
2447
Philip Withnallbecb77e2013-11-25 18:01:30 +00002448 shsurf->popup.shseat = get_shell_seat(seat);
2449 shsurf->popup.serial = serial;
2450 shsurf->popup.x = x;
2451 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002452
Philip Withnallb995e1d2013-11-25 18:01:39 +00002453 shsurf->next_type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002454}
2455
2456static void
2457shell_surface_set_popup(struct wl_client *client,
2458 struct wl_resource *resource,
2459 struct wl_resource *seat_resource,
2460 uint32_t serial,
2461 struct wl_resource *parent_resource,
2462 int32_t x, int32_t y, uint32_t flags)
2463{
2464 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002465 struct weston_surface *parent =
2466 wl_resource_get_user_data(parent_resource);
2467
2468 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002469
Rafael Antognolli03b16592013-12-03 15:35:42 -02002470 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002471 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002472 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002473 wl_resource_get_user_data(seat_resource),
2474 serial, x, y);
2475}
2476
2477static void
2478set_maximized(struct shell_surface *shsurf,
2479 struct weston_output *output)
2480{
2481 struct desktop_shell *shell;
2482 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002483
Philip Withnall352e7ed2013-11-25 18:01:35 +00002484 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002485
2486 shell = shell_surface_get_shell(shsurf);
2487 panel_height = get_output_panel_height(shell, shsurf->output);
2488 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2489
2490 shsurf->client->send_configure(shsurf->surface, edges,
2491 shsurf->output->width,
2492 shsurf->output->height - panel_height);
2493
Rafael Antognolli03b16592013-12-03 15:35:42 -02002494 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002495}
2496
2497static void
2498unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002499{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002500 /* undo all maximized things here */
2501 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002502 weston_view_set_position(shsurf->view,
2503 shsurf->saved_x,
2504 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002505
2506 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 wl_list_insert(&shsurf->view->geometry.transformation_list,
2508 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002509 shsurf->saved_rotation_valid = false;
2510 }
2511
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002512 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002513}
2514
Philip Withnallbecb77e2013-11-25 18:01:30 +00002515static void
2516shell_surface_set_maximized(struct wl_client *client,
2517 struct wl_resource *resource,
2518 struct wl_resource *output_resource)
2519{
2520 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2521 struct weston_output *output;
2522
2523 if (output_resource)
2524 output = wl_resource_get_user_data(output_resource);
2525 else
2526 output = NULL;
2527
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002528 shell_surface_set_parent(shsurf, NULL);
2529
Rafael Antognolli03b16592013-12-03 15:35:42 -02002530 surface_clear_next_states(shsurf);
2531 shsurf->next_state.maximized = true;
2532 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002533 set_maximized(shsurf, output);
2534}
2535
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002536/* This is only ever called from set_surface_type(), so there’s no need to
2537 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002538static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002539reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002540{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002541 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002542 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002543 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002544 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002545
2546 surface->type = SHELL_SURFACE_NONE;
2547 return 0;
2548}
2549
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002550static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002551set_full_output(struct shell_surface *shsurf)
2552{
2553 shsurf->saved_x = shsurf->view->geometry.x;
2554 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002555 shsurf->saved_width = shsurf->surface->width;
2556 shsurf->saved_height = shsurf->surface->height;
2557 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002558 shsurf->saved_position_valid = true;
2559
2560 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2561 wl_list_remove(&shsurf->rotation.transform.link);
2562 wl_list_init(&shsurf->rotation.transform.link);
2563 weston_view_geometry_dirty(shsurf->view);
2564 shsurf->saved_rotation_valid = true;
2565 }
2566}
2567
2568static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002569set_surface_type(struct shell_surface *shsurf)
2570{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002571 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002572 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002573
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002575
2576 shsurf->type = shsurf->next_type;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002577 shsurf->state = shsurf->next_state;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002578 shsurf->next_type = SHELL_SURFACE_NONE;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002579 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002580
2581 switch (shsurf->type) {
2582 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002583 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002584 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002585 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002586 weston_view_set_position(shsurf->view,
2587 pev->geometry.x + shsurf->transient.x,
2588 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002589 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002590
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002591 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592 weston_view_set_position(shsurf->view, shsurf->transient.x,
2593 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002594 break;
2595
Philip Withnall0f640e22013-11-25 18:01:31 +00002596 case SHELL_SURFACE_POPUP:
2597 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002598 default:
2599 break;
2600 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002601
2602 /* Update the surface’s layer. */
2603 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002604}
2605
Tiago Vignattibe143262012-04-16 17:31:41 +03002606static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002607shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002608{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002609 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002610}
2611
Alex Wu21858432012-04-01 20:13:08 +08002612static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002613black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002614
Jason Ekstranda7af7042013-10-12 22:38:11 -05002615static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002616create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002617 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002618 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002619{
2620 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002621 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002622
2623 surface = weston_surface_create(ec);
2624 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002625 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002626 return NULL;
2627 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628 view = weston_view_create(surface);
2629 if (surface == NULL) {
2630 weston_log("no memory\n");
2631 weston_surface_destroy(surface);
2632 return NULL;
2633 }
Alex Wu4539b082012-03-01 12:57:46 +08002634
Alex Wu21858432012-04-01 20:13:08 +08002635 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002636 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002637 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002638 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002639 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002640 pixman_region32_fini(&surface->input);
2641 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002642
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002643 surface->width = w;
2644 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002645 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002646
2647 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002648}
2649
Philip Withnalled8f1a92013-11-25 18:01:43 +00002650static void
2651shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2652{
2653 struct weston_output *output = shsurf->fullscreen_output;
2654
Rafael Antognolli03b16592013-12-03 15:35:42 -02002655 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002656
2657 if (!shsurf->fullscreen.black_view)
2658 shsurf->fullscreen.black_view =
2659 create_black_surface(shsurf->surface->compositor,
2660 shsurf->surface,
2661 output->x, output->y,
2662 output->width,
2663 output->height);
2664
2665 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2666 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2667 wl_list_insert(&shsurf->view->layer_link,
2668 &shsurf->fullscreen.black_view->layer_link);
2669 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2670 weston_surface_damage(shsurf->surface);
2671}
2672
Alex Wu4539b082012-03-01 12:57:46 +08002673/* Create black surface and append it to the associated fullscreen surface.
2674 * Handle size dismatch and positioning according to the method. */
2675static void
2676shell_configure_fullscreen(struct shell_surface *shsurf)
2677{
2678 struct weston_output *output = shsurf->fullscreen_output;
2679 struct weston_surface *surface = shsurf->surface;
2680 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002681 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002682 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002683
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002684 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2685 restore_output_mode(output);
2686
Philip Withnalled8f1a92013-11-25 18:01:43 +00002687 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002688
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002690 &surf_width, &surf_height);
2691
Alex Wu4539b082012-03-01 12:57:46 +08002692 switch (shsurf->fullscreen.type) {
2693 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002694 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002696 break;
2697 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002698 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002699 if (output->width == surf_width &&
2700 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002701 weston_view_set_position(shsurf->view,
2702 output->x - surf_x,
2703 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002704 break;
2705 }
2706
Alex Wu4539b082012-03-01 12:57:46 +08002707 matrix = &shsurf->fullscreen.transform.matrix;
2708 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002709
Scott Moreau1bad5db2012-08-18 01:04:05 -06002710 output_aspect = (float) output->width /
2711 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002712 /* XXX: Use surf_width and surf_height here? */
2713 surface_aspect = (float) surface->width /
2714 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002715 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002716 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002717 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002718 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002719 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002720 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002721
Alex Wu4539b082012-03-01 12:57:46 +08002722 weston_matrix_scale(matrix, scale, scale, 1);
2723 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002725 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002726 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2727 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002729
Alex Wu4539b082012-03-01 12:57:46 +08002730 break;
2731 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002732 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002733 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002734 surf_width * surface->buffer_viewport.scale,
2735 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002736 shsurf->fullscreen.framerate};
2737
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002738 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002739 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 weston_view_set_position(shsurf->view,
2741 output->x - surf_x,
2742 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002743 shsurf->fullscreen.black_view->surface->width = output->width;
2744 shsurf->fullscreen.black_view->surface->height = output->height;
2745 weston_view_set_position(shsurf->fullscreen.black_view,
2746 output->x - surf_x,
2747 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002748 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002749 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002750 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002752 }
Alex Wubd3354b2012-04-17 17:20:49 +08002753 }
Alex Wu4539b082012-03-01 12:57:46 +08002754 break;
2755 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002757 break;
2758 default:
2759 break;
2760 }
2761}
2762
Alex Wu4539b082012-03-01 12:57:46 +08002763static void
2764shell_map_fullscreen(struct shell_surface *shsurf)
2765{
Alex Wubd3354b2012-04-17 17:20:49 +08002766 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002767}
2768
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002769static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002770set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2771{
2772 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002773 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002774 shsurf->transient.x = x;
2775 shsurf->transient.y = y;
2776 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002777
2778 shell_surface_set_parent(shsurf, NULL);
2779
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002780 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2781}
2782
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002783static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002784
2785static void
2786destroy_shell_seat(struct wl_listener *listener, void *data)
2787{
2788 struct shell_seat *shseat =
2789 container_of(listener,
2790 struct shell_seat, seat_destroy_listener);
2791 struct shell_surface *shsurf, *prev = NULL;
2792
2793 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002794 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002795 shseat->popup_grab.client = NULL;
2796
2797 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2798 shsurf->popup.shseat = NULL;
2799 if (prev) {
2800 wl_list_init(&prev->popup.grab_link);
2801 }
2802 prev = shsurf;
2803 }
2804 wl_list_init(&prev->popup.grab_link);
2805 }
2806
2807 wl_list_remove(&shseat->seat_destroy_listener.link);
2808 free(shseat);
2809}
2810
2811static struct shell_seat *
2812create_shell_seat(struct weston_seat *seat)
2813{
2814 struct shell_seat *shseat;
2815
2816 shseat = calloc(1, sizeof *shseat);
2817 if (!shseat) {
2818 weston_log("no memory to allocate shell seat\n");
2819 return NULL;
2820 }
2821
2822 shseat->seat = seat;
2823 wl_list_init(&shseat->popup_grab.surfaces_list);
2824
2825 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2826 wl_signal_add(&seat->destroy_signal,
2827 &shseat->seat_destroy_listener);
2828
2829 return shseat;
2830}
2831
2832static struct shell_seat *
2833get_shell_seat(struct weston_seat *seat)
2834{
2835 struct wl_listener *listener;
2836
2837 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2838 if (listener == NULL)
2839 return create_shell_seat(seat);
2840
2841 return container_of(listener,
2842 struct shell_seat, seat_destroy_listener);
2843}
2844
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002845static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002846popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002847{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002848 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002849 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002850 struct shell_seat *shseat =
2851 container_of(grab, struct shell_seat, popup_grab.grab);
2852 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002853 wl_fixed_t sx, sy;
2854
Jason Ekstranda7af7042013-10-12 22:38:11 -05002855 view = weston_compositor_pick_view(pointer->seat->compositor,
2856 pointer->x, pointer->y,
2857 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002858
Jason Ekstranda7af7042013-10-12 22:38:11 -05002859 if (view && view->surface->resource &&
2860 wl_resource_get_client(view->surface->resource) == client) {
2861 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002862 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002863 weston_pointer_set_focus(pointer, NULL,
2864 wl_fixed_from_int(0),
2865 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002866 }
2867}
2868
2869static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002870popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2871 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002872{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002873 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002874 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002875 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002876
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002877 weston_pointer_move(pointer, x, y);
2878
Neil Roberts96d790e2013-09-19 17:32:00 +01002879 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002880 weston_view_from_global_fixed(pointer->focus,
2881 pointer->x, pointer->y,
2882 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002883 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002884 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002885}
2886
2887static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002888popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002889 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002890{
2891 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002892 struct shell_seat *shseat =
2893 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002894 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002895 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002896 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002897 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002898
Neil Roberts96d790e2013-09-19 17:32:00 +01002899 resource_list = &grab->pointer->focus_resource_list;
2900 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002901 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002902 wl_resource_for_each(resource, resource_list) {
2903 wl_pointer_send_button(resource, serial,
2904 time, button, state);
2905 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002906 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002907 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002908 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002909 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002910 }
2911
Daniel Stone4dbadb12012-05-30 16:31:51 +01002912 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002913 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002914}
2915
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002916static void
2917popup_grab_cancel(struct weston_pointer_grab *grab)
2918{
2919 popup_grab_end(grab->pointer);
2920}
2921
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002922static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002923 popup_grab_focus,
2924 popup_grab_motion,
2925 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002926 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002927};
2928
2929static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002930shell_surface_send_popup_done(struct shell_surface *shsurf)
2931{
2932 if (shell_surface_is_wl_shell_surface(shsurf))
2933 wl_shell_surface_send_popup_done(shsurf->resource);
2934 else if (shell_surface_is_xdg_popup(shsurf))
2935 xdg_popup_send_popup_done(shsurf->resource,
2936 shsurf->popup.serial);
2937}
2938
2939static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002940popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002941{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002942 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002943 struct shell_seat *shseat =
2944 container_of(grab, struct shell_seat, popup_grab.grab);
2945 struct shell_surface *shsurf;
2946 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002947
2948 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002949 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002950 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002951 shseat->popup_grab.grab.interface = NULL;
2952 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002953 /* Send the popup_done event to all the popups open */
2954 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002955 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002956 shsurf->popup.shseat = NULL;
2957 if (prev) {
2958 wl_list_init(&prev->popup.grab_link);
2959 }
2960 prev = shsurf;
2961 }
2962 wl_list_init(&prev->popup.grab_link);
2963 wl_list_init(&shseat->popup_grab.surfaces_list);
2964 }
2965}
2966
2967static void
2968add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2969{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002970 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002971
2972 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002973 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002974 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002975 /* We must make sure here that this popup was opened after
2976 * a mouse press, and not just by moving around with other
2977 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002978 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002979 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002980
Rob Bradforddfe31052013-06-26 19:49:11 +01002981 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002982 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002983 } else {
2984 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002985 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002986}
2987
2988static void
2989remove_popup_grab(struct shell_surface *shsurf)
2990{
2991 struct shell_seat *shseat = shsurf->popup.shseat;
2992
2993 wl_list_remove(&shsurf->popup.grab_link);
2994 wl_list_init(&shsurf->popup.grab_link);
2995 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002996 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002997 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002998 }
2999}
3000
3001static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003002shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003003{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003004 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003005 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003006
Jason Ekstranda7af7042013-10-12 22:38:11 -05003007 shsurf->surface->output = parent_view->output;
3008 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003009
Jason Ekstranda7af7042013-10-12 22:38:11 -05003010 weston_view_set_transform_parent(shsurf->view, parent_view);
3011 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3012 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003013
Kristian Høgsberge3148752013-05-06 23:19:49 -04003014 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003015 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003016 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003017 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003018 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003019 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003020}
3021
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003022static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003023 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003024 shell_surface_move,
3025 shell_surface_resize,
3026 shell_surface_set_toplevel,
3027 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003028 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003029 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003030 shell_surface_set_maximized,
3031 shell_surface_set_title,
3032 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003033};
3034
3035static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003036destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003037{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003038 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3039
Giulio Camuffo5085a752013-03-25 21:42:45 +01003040 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3041 remove_popup_grab(shsurf);
3042 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003043
Alex Wubd3354b2012-04-17 17:20:49 +08003044 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003045 shell_surface_is_top_fullscreen(shsurf))
3046 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003047
Jason Ekstranda7af7042013-10-12 22:38:11 -05003048 if (shsurf->fullscreen.black_view)
3049 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003050
Alex Wubd3354b2012-04-17 17:20:49 +08003051 /* As destroy_resource() use wl_list_for_each_safe(),
3052 * we can always remove the listener.
3053 */
3054 wl_list_remove(&shsurf->surface_destroy_listener.link);
3055 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06003056 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07003057 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003058
Jason Ekstranda7af7042013-10-12 22:38:11 -05003059 weston_view_destroy(shsurf->view);
3060
Philip Withnall648a4dd2013-11-25 18:01:44 +00003061 wl_list_remove(&shsurf->children_link);
3062
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003063 wl_list_remove(&shsurf->link);
3064 free(shsurf);
3065}
3066
3067static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003068shell_destroy_shell_surface(struct wl_resource *resource)
3069{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003070 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003071
3072 destroy_shell_surface(shsurf);
3073}
3074
3075static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003076shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003077{
3078 struct shell_surface *shsurf = container_of(listener,
3079 struct shell_surface,
3080 surface_destroy_listener);
3081
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003082 if (shsurf->resource)
3083 wl_resource_destroy(shsurf->resource);
3084 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003085 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003086}
3087
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003088static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003089shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003090
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003091static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003092get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003093{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003094 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003095 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003096 else
3097 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003098}
3099
Rafael Antognollie2a34552013-12-03 15:35:45 -02003100static struct shell_surface *
3101create_common_surface(void *shell, struct weston_surface *surface,
3102 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003103{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003104 struct shell_surface *shsurf;
3105
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003106 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003107 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003108 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003109 }
3110
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003111 shsurf = calloc(1, sizeof *shsurf);
3112 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003113 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003114 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003115 }
3116
Jason Ekstranda7af7042013-10-12 22:38:11 -05003117 shsurf->view = weston_view_create(surface);
3118 if (!shsurf->view) {
3119 weston_log("no memory to allocate shell surface\n");
3120 free(shsurf);
3121 return NULL;
3122 }
3123
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003124 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003125 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003126
Tiago Vignattibc052c92012-04-19 16:18:18 +03003127 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003128 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003129 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003130 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003131 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003132 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003133 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3134 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003135 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003136 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003137 wl_list_init(&shsurf->fullscreen.transform.link);
3138
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003139 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003140 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003141 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003142 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003143
3144 /* init link so its safe to always remove it in destroy_shell_surface */
3145 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003146 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003147
Pekka Paalanen460099f2012-01-20 16:48:25 +02003148 /* empty when not in use */
3149 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003150 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003151
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003152 wl_list_init(&shsurf->workspace_transform.link);
3153
Philip Withnall648a4dd2013-11-25 18:01:44 +00003154 wl_list_init(&shsurf->children_link);
3155 wl_list_init(&shsurf->children_list);
3156 shsurf->parent = NULL;
3157
Pekka Paalanen98262232011-12-01 10:42:22 +02003158 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003159 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003160
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003161 shsurf->client = client;
3162
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003163 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003164}
3165
Rafael Antognollie2a34552013-12-03 15:35:45 -02003166static struct shell_surface *
3167create_shell_surface(void *shell, struct weston_surface *surface,
3168 const struct weston_shell_client *client)
3169{
3170 struct shell_surface *shsurf;
3171 shsurf = create_common_surface(shell, surface, client);
3172
3173 shsurf->type = SHELL_SURFACE_NONE;
3174 shsurf->next_type = SHELL_SURFACE_NONE;
3175
3176 return shsurf;
3177}
3178
Jason Ekstranda7af7042013-10-12 22:38:11 -05003179static struct weston_view *
3180get_primary_view(void *shell, struct shell_surface *shsurf)
3181{
3182 return shsurf->view;
3183}
3184
Tiago Vignattibc052c92012-04-19 16:18:18 +03003185static void
3186shell_get_shell_surface(struct wl_client *client,
3187 struct wl_resource *resource,
3188 uint32_t id,
3189 struct wl_resource *surface_resource)
3190{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003191 struct weston_surface *surface =
3192 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003193 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003194 struct shell_surface *shsurf;
3195
3196 if (get_shell_surface(surface)) {
3197 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003198 WL_DISPLAY_ERROR_INVALID_OBJECT,
3199 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003200 return;
3201 }
3202
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003203 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003204 if (!shsurf) {
3205 wl_resource_post_error(surface_resource,
3206 WL_DISPLAY_ERROR_INVALID_OBJECT,
3207 "surface->configure already set");
3208 return;
3209 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003210
Jason Ekstranda85118c2013-06-27 20:17:02 -05003211 shsurf->resource =
3212 wl_resource_create(client,
3213 &wl_shell_surface_interface, 1, id);
3214 wl_resource_set_implementation(shsurf->resource,
3215 &shell_surface_implementation,
3216 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003217}
3218
Rafael Antognollie2a34552013-12-03 15:35:45 -02003219static bool
3220shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3221{
3222 return wl_resource_instance_of(shsurf->resource,
3223 &wl_shell_surface_interface,
3224 &shell_surface_implementation);
3225}
3226
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003227static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003228 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003229};
3230
Rafael Antognollie2a34552013-12-03 15:35:45 -02003231/****************************
3232 * xdg-shell implementation */
3233
3234static void
3235xdg_surface_destroy(struct wl_client *client,
3236 struct wl_resource *resource)
3237{
3238 wl_resource_destroy(resource);
3239}
3240
3241static void
3242xdg_surface_pong(struct wl_client *client,
3243 struct wl_resource *resource,
3244 uint32_t serial)
3245{
3246 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3247
3248 surface_pong(shsurf, serial);
3249}
3250
3251static void
3252xdg_surface_set_app_id(struct wl_client *client,
3253 struct wl_resource *resource,
3254 const char *app_id)
3255{
3256 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3257
3258 free(shsurf->class);
3259 shsurf->class = strdup(app_id);
3260}
3261
3262static void
3263xdg_surface_set_title(struct wl_client *client,
3264 struct wl_resource *resource, const char *title)
3265{
3266 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3267
3268 set_title(shsurf, title);
3269}
3270
3271static void
3272xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3273 struct wl_resource *seat_resource, uint32_t serial)
3274{
3275 common_surface_move(resource, seat_resource, serial);
3276}
3277
3278static void
3279xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3280 struct wl_resource *seat_resource, uint32_t serial,
3281 uint32_t edges)
3282{
3283 common_surface_resize(resource, seat_resource, serial, edges);
3284}
3285
3286static void
3287xdg_surface_set_output(struct wl_client *client,
3288 struct wl_resource *resource,
3289 struct wl_resource *output_resource)
3290{
3291 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3292 struct weston_output *output;
3293
3294 if (output_resource)
3295 output = wl_resource_get_user_data(output_resource);
3296 else
3297 output = NULL;
3298
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003299 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003300}
3301
3302static void
3303xdg_surface_set_fullscreen(struct wl_client *client,
3304 struct wl_resource *resource)
3305{
3306 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3307
3308 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3309 return;
3310
3311 if (!shsurf->next_state.fullscreen) {
3312 shsurf->next_state.fullscreen = true;
3313 shsurf->state_changed = true;
3314 set_fullscreen(shsurf,
3315 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003316 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003317 }
3318}
3319
3320static void
3321xdg_surface_unset_fullscreen(struct wl_client *client,
3322 struct wl_resource *resource)
3323{
3324 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003325 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003326
3327 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3328 return;
3329
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003330 if (!shsurf->next_state.fullscreen)
3331 return;
3332
Rafael Antognollie2a34552013-12-03 15:35:45 -02003333 shsurf->next_state.fullscreen = false;
3334 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003335
3336 if (shsurf->saved_size_valid) {
3337 width = shsurf->saved_width;
3338 height = shsurf->saved_height;
3339 shsurf->saved_size_valid = false;
3340 } else {
3341 width = shsurf->surface->width;
3342 height = shsurf->surface->height;
3343 }
3344
3345 shsurf->client->send_configure(shsurf->surface, 0, width, height);
3346 shsurf->next_type = shsurf->type;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003347}
3348
3349static void
3350xdg_surface_set_maximized(struct wl_client *client,
3351 struct wl_resource *resource)
3352{
3353 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3354
3355 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3356 return;
3357
3358 if (!shsurf->next_state.maximized) {
3359 shsurf->next_state.maximized = true;
3360 shsurf->state_changed = true;
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003361 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003362 }
3363}
3364
3365static void
3366xdg_surface_unset_maximized(struct wl_client *client,
3367 struct wl_resource *resource)
3368{
3369 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003370 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003371
3372 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3373 return;
3374
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003375 if (!shsurf->next_state.maximized)
3376 return;
3377
Rafael Antognollie2a34552013-12-03 15:35:45 -02003378 shsurf->next_state.maximized = false;
3379 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003380
3381 if (shsurf->saved_size_valid) {
3382 width = shsurf->saved_width;
3383 height = shsurf->saved_height;
3384 shsurf->saved_size_valid = false;
3385 } else {
3386 width = shsurf->surface->width;
3387 height = shsurf->surface->height;
3388 }
3389
3390 shsurf->client->send_configure(shsurf->surface, 0, width, height);
3391 shsurf->next_type = shsurf->type;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003392}
3393
3394static const struct xdg_surface_interface xdg_surface_implementation = {
3395 xdg_surface_destroy,
3396 xdg_surface_set_transient_for,
3397 xdg_surface_set_title,
3398 xdg_surface_set_app_id,
3399 xdg_surface_pong,
3400 xdg_surface_move,
3401 xdg_surface_resize,
3402 xdg_surface_set_output,
3403 xdg_surface_set_fullscreen,
3404 xdg_surface_unset_fullscreen,
3405 xdg_surface_set_maximized,
3406 xdg_surface_unset_maximized,
3407 NULL /* set_minimized */
3408};
3409
3410static void
3411xdg_send_configure(struct weston_surface *surface,
3412 uint32_t edges, int32_t width, int32_t height)
3413{
3414 struct shell_surface *shsurf = get_shell_surface(surface);
3415
3416 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3417}
3418
3419static const struct weston_shell_client xdg_client = {
3420 xdg_send_configure
3421};
3422
3423static void
3424xdg_use_unstable_version(struct wl_client *client,
3425 struct wl_resource *resource,
3426 int32_t version)
3427{
3428 if (version > 1) {
3429 wl_resource_post_error(resource,
3430 1,
3431 "xdg-shell:: version not implemented yet.");
3432 return;
3433 }
3434}
3435
3436static struct shell_surface *
3437create_xdg_surface(void *shell, struct weston_surface *surface,
3438 const struct weston_shell_client *client)
3439{
3440 struct shell_surface *shsurf;
3441 shsurf = create_common_surface(shell, surface, client);
3442
3443 shsurf->type = SHELL_SURFACE_NONE;
3444 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
3445
3446 return shsurf;
3447}
3448
3449static void
3450xdg_get_xdg_surface(struct wl_client *client,
3451 struct wl_resource *resource,
3452 uint32_t id,
3453 struct wl_resource *surface_resource)
3454{
3455 struct weston_surface *surface =
3456 wl_resource_get_user_data(surface_resource);
3457 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3458 struct shell_surface *shsurf;
3459
3460 if (get_shell_surface(surface)) {
3461 wl_resource_post_error(surface_resource,
3462 WL_DISPLAY_ERROR_INVALID_OBJECT,
3463 "desktop_shell::get_shell_surface already requested");
3464 return;
3465 }
3466
3467 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3468 if (!shsurf) {
3469 wl_resource_post_error(surface_resource,
3470 WL_DISPLAY_ERROR_INVALID_OBJECT,
3471 "surface->configure already set");
3472 return;
3473 }
3474
3475 shsurf->resource =
3476 wl_resource_create(client,
3477 &xdg_surface_interface, 1, id);
3478 wl_resource_set_implementation(shsurf->resource,
3479 &xdg_surface_implementation,
3480 shsurf, shell_destroy_shell_surface);
3481}
3482
3483static bool
3484shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3485{
3486 return wl_resource_instance_of(shsurf->resource,
3487 &xdg_surface_interface,
3488 &xdg_surface_implementation);
3489}
3490
3491/* xdg-popup implementation */
3492
3493static void
3494xdg_popup_destroy(struct wl_client *client,
3495 struct wl_resource *resource)
3496{
3497 wl_resource_destroy(resource);
3498}
3499
3500static void
3501xdg_popup_pong(struct wl_client *client,
3502 struct wl_resource *resource,
3503 uint32_t serial)
3504{
3505 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3506
3507 surface_pong(shsurf, serial);
3508}
3509
3510static const struct xdg_popup_interface xdg_popup_implementation = {
3511 xdg_popup_destroy,
3512 xdg_popup_pong
3513};
3514
3515static void
3516xdg_popup_send_configure(struct weston_surface *surface,
3517 uint32_t edges, int32_t width, int32_t height)
3518{
3519}
3520
3521static const struct weston_shell_client xdg_popup_client = {
3522 xdg_popup_send_configure
3523};
3524
3525static struct shell_surface *
3526create_xdg_popup(void *shell, struct weston_surface *surface,
3527 const struct weston_shell_client *client,
3528 struct weston_surface *parent,
3529 struct shell_seat *seat,
3530 uint32_t serial,
3531 int32_t x, int32_t y)
3532{
3533 struct shell_surface *shsurf;
3534 shsurf = create_common_surface(shell, surface, client);
3535
3536 shsurf->type = SHELL_SURFACE_NONE;
3537 shsurf->next_type = SHELL_SURFACE_POPUP;
3538 shsurf->popup.shseat = seat;
3539 shsurf->popup.serial = serial;
3540 shsurf->popup.x = x;
3541 shsurf->popup.y = y;
3542 shell_surface_set_parent(shsurf, parent);
3543
3544 return shsurf;
3545}
3546
3547static void
3548xdg_get_xdg_popup(struct wl_client *client,
3549 struct wl_resource *resource,
3550 uint32_t id,
3551 struct wl_resource *surface_resource,
3552 struct wl_resource *parent_resource,
3553 struct wl_resource *seat_resource,
3554 uint32_t serial,
3555 int32_t x, int32_t y, uint32_t flags)
3556{
3557 struct weston_surface *surface =
3558 wl_resource_get_user_data(surface_resource);
3559 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3560 struct shell_surface *shsurf;
3561 struct weston_surface *parent;
3562 struct shell_seat *seat;
3563
3564 if (get_shell_surface(surface)) {
3565 wl_resource_post_error(surface_resource,
3566 WL_DISPLAY_ERROR_INVALID_OBJECT,
3567 "desktop_shell::get_shell_surface already requested");
3568 return;
3569 }
3570
3571 if (!parent_resource) {
3572 wl_resource_post_error(surface_resource,
3573 WL_DISPLAY_ERROR_INVALID_OBJECT,
3574 "xdg_shell::get_xdg_popup requires a parent shell surface");
3575 }
3576
3577 parent = wl_resource_get_user_data(parent_resource);
3578 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3579
3580 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3581 parent, seat, serial, x, y);
3582 if (!shsurf) {
3583 wl_resource_post_error(surface_resource,
3584 WL_DISPLAY_ERROR_INVALID_OBJECT,
3585 "surface->configure already set");
3586 return;
3587 }
3588
3589 shsurf->resource =
3590 wl_resource_create(client,
3591 &xdg_popup_interface, 1, id);
3592 wl_resource_set_implementation(shsurf->resource,
3593 &xdg_popup_implementation,
3594 shsurf, shell_destroy_shell_surface);
3595}
3596
3597static bool
3598shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3599{
3600 return wl_resource_instance_of(shsurf->resource,
3601 &xdg_popup_interface,
3602 &xdg_popup_implementation);
3603}
3604
3605static const struct xdg_shell_interface xdg_implementation = {
3606 xdg_use_unstable_version,
3607 xdg_get_xdg_surface,
3608 xdg_get_xdg_popup
3609};
3610
3611static int
3612xdg_shell_unversioned_dispatch(const void *implementation,
3613 void *_target, uint32_t opcode,
3614 const struct wl_message *message,
3615 union wl_argument *args)
3616{
3617 struct wl_resource *resource = _target;
3618 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3619
3620 if (opcode != 0) {
3621 wl_resource_post_error(resource,
3622 WL_DISPLAY_ERROR_INVALID_OBJECT,
3623 "must call use_unstable_version first");
3624 return 0;
3625 }
3626
3627#define XDG_SERVER_VERSION 1
3628
3629 if (args[0].i != XDG_SERVER_VERSION) {
3630 wl_resource_post_error(resource,
3631 WL_DISPLAY_ERROR_INVALID_OBJECT,
3632 "incompatible version, server is %d "
3633 "client wants %d",
3634 XDG_SERVER_VERSION, args[0].i);
3635 return 0;
3636 }
3637
3638 wl_resource_set_implementation(resource, &xdg_implementation,
3639 shell, NULL);
3640
3641 return 1;
3642}
3643
3644/* end of xdg-shell implementation */
3645/***********************************/
3646
Kristian Høgsberg07937562011-04-12 17:25:42 -04003647static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003648shell_fade(struct desktop_shell *shell, enum fade_type type);
3649
3650static int
3651screensaver_timeout(void *data)
3652{
3653 struct desktop_shell *shell = data;
3654
3655 shell_fade(shell, FADE_OUT);
3656
3657 return 1;
3658}
3659
3660static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003661handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003662{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003663 struct desktop_shell *shell =
3664 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003665
Pekka Paalanen18027e52011-12-02 16:31:49 +02003666 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003667
3668 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003669 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003670}
3671
3672static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003673launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003674{
3675 if (shell->screensaver.binding)
3676 return;
3677
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003678 if (!shell->screensaver.path) {
3679 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003680 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003681 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003682
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003683 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003684 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003685 return;
3686 }
3687
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003688 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003689 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003690 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003691 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003692}
3693
3694static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003695terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003696{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003697 if (shell->screensaver.process.pid == 0)
3698 return;
3699
3700 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003701}
3702
3703static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003704configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003705{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003706 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003707
Jason Ekstranda7af7042013-10-12 22:38:11 -05003708 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3709 if (v->output == ev->output && v != ev) {
3710 weston_view_unmap(v);
3711 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003712 }
3713 }
3714
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003715 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003716
Jason Ekstranda7af7042013-10-12 22:38:11 -05003717 if (wl_list_empty(&ev->layer_link)) {
3718 wl_list_insert(&layer->view_list, &ev->layer_link);
3719 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003720 }
3721}
3722
3723static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003724background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003725{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003726 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003727 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003728
Jason Ekstranda7af7042013-10-12 22:38:11 -05003729 view = container_of(es->views.next, struct weston_view, surface_link);
3730
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003731 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003732}
3733
3734static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003735desktop_shell_set_background(struct wl_client *client,
3736 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003737 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003738 struct wl_resource *surface_resource)
3739{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003740 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003741 struct weston_surface *surface =
3742 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003743 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003744
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003745 if (surface->configure) {
3746 wl_resource_post_error(surface_resource,
3747 WL_DISPLAY_ERROR_INVALID_OBJECT,
3748 "surface role already assigned");
3749 return;
3750 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003751
Jason Ekstranda7af7042013-10-12 22:38:11 -05003752 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3753 weston_view_destroy(view);
3754 view = weston_view_create(surface);
3755
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003756 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003757 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003758 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003759 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003760 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003761 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003762 surface->output->width,
3763 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003764}
3765
3766static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003767panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003768{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003769 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003770 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003771
Jason Ekstranda7af7042013-10-12 22:38:11 -05003772 view = container_of(es->views.next, struct weston_view, surface_link);
3773
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003774 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003775}
3776
3777static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003778desktop_shell_set_panel(struct wl_client *client,
3779 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003780 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003781 struct wl_resource *surface_resource)
3782{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003783 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003784 struct weston_surface *surface =
3785 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003786 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003787
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003788 if (surface->configure) {
3789 wl_resource_post_error(surface_resource,
3790 WL_DISPLAY_ERROR_INVALID_OBJECT,
3791 "surface role already assigned");
3792 return;
3793 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003794
Jason Ekstranda7af7042013-10-12 22:38:11 -05003795 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3796 weston_view_destroy(view);
3797 view = weston_view_create(surface);
3798
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003799 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003800 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003801 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003802 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003803 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003804 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003805 surface->output->width,
3806 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003807}
3808
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003809static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003810lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003811{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003812 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003813 struct weston_view *view;
3814
3815 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003816
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003817 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003818 return;
3819
Jason Ekstranda7af7042013-10-12 22:38:11 -05003820 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003821
3822 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003823 wl_list_insert(&shell->lock_layer.view_list,
3824 &view->layer_link);
3825 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003826 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003827 }
3828}
3829
3830static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003831handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003832{
Tiago Vignattibe143262012-04-16 17:31:41 +03003833 struct desktop_shell *shell =
3834 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003835
Martin Minarik6d118362012-06-07 18:01:59 +02003836 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003837 shell->lock_surface = NULL;
3838}
3839
3840static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003841desktop_shell_set_lock_surface(struct wl_client *client,
3842 struct wl_resource *resource,
3843 struct wl_resource *surface_resource)
3844{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003845 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003846 struct weston_surface *surface =
3847 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003848
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003849 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003850
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003851 if (!shell->locked)
3852 return;
3853
Pekka Paalanen98262232011-12-01 10:42:22 +02003854 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003855
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003856 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003857 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003858 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003859
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003860 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003861 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003862 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003863}
3864
3865static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003866resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003867{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003868 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003869
Pekka Paalanen77346a62011-11-30 16:26:35 +02003870 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003871
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003872 wl_list_remove(&shell->lock_layer.link);
3873 wl_list_insert(&shell->compositor->cursor_layer.link,
3874 &shell->fullscreen_layer.link);
3875 wl_list_insert(&shell->fullscreen_layer.link,
3876 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003877 if (shell->showing_input_panels) {
3878 wl_list_insert(&shell->panel_layer.link,
3879 &shell->input_panel_layer.link);
3880 wl_list_insert(&shell->input_panel_layer.link,
3881 &ws->layer.link);
3882 } else {
3883 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3884 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003885
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003886 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003887
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003888 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003889 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003890 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003891}
3892
3893static void
3894desktop_shell_unlock(struct wl_client *client,
3895 struct wl_resource *resource)
3896{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003897 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003898
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003899 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003900
3901 if (shell->locked)
3902 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003903}
3904
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003905static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003906desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003907 struct wl_resource *resource,
3908 struct wl_resource *surface_resource)
3909{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003910 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003911
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003912 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003913 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003914}
3915
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003916static void
3917desktop_shell_desktop_ready(struct wl_client *client,
3918 struct wl_resource *resource)
3919{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003920 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003921
3922 shell_fade_startup(shell);
3923}
3924
Kristian Høgsberg75840622011-09-06 13:48:16 -04003925static const struct desktop_shell_interface desktop_shell_implementation = {
3926 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003927 desktop_shell_set_panel,
3928 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003929 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003930 desktop_shell_set_grab_surface,
3931 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003932};
3933
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003934static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003935get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003936{
3937 struct shell_surface *shsurf;
3938
3939 shsurf = get_shell_surface(surface);
3940 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003941 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003942 return shsurf->type;
3943}
3944
Kristian Høgsberg75840622011-09-06 13:48:16 -04003945static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003946move_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øgsberg938b8fa2012-05-18 13:46:27 -04003950 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003951
Pekka Paalanen01388e22013-04-25 13:57:44 +03003952 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003953 if (surface == NULL)
3954 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003955
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003956 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003957 if (shsurf == NULL || shsurf->state.fullscreen ||
3958 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003959 return;
3960
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003961 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003962}
3963
3964static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003965touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3966{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003967 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003968 struct weston_surface *surface;
3969 struct shell_surface *shsurf;
3970
3971 surface = weston_surface_get_main_surface(focus);
3972 if (surface == NULL)
3973 return;
3974
3975 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003976 if (shsurf == NULL || shsurf->state.fullscreen ||
3977 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003978 return;
3979
3980 surface_touch_move(shsurf, (struct weston_seat *) seat);
3981}
3982
3983static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003984resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003985{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003986 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003987 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003988 uint32_t edges = 0;
3989 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003990 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003991
Pekka Paalanen01388e22013-04-25 13:57:44 +03003992 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003993 if (surface == NULL)
3994 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003995
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003996 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003997 if (shsurf == NULL || shsurf->state.fullscreen ||
3998 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003999 return;
4000
Jason Ekstranda7af7042013-10-12 22:38:11 -05004001 weston_view_from_global(shsurf->view,
4002 wl_fixed_to_int(seat->pointer->grab_x),
4003 wl_fixed_to_int(seat->pointer->grab_y),
4004 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004005
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004006 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004007 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004008 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004009 edges |= 0;
4010 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004011 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004012
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004013 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004014 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004015 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004016 edges |= 0;
4017 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004018 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004019
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004020 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004021}
4022
4023static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004024surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004025 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004026{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004027 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004028 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004029 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004030 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004031
Pekka Paalanen01388e22013-04-25 13:57:44 +03004032 /* XXX: broken for windows containing sub-surfaces */
4033 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004034 if (surface == NULL)
4035 return;
4036
4037 shsurf = get_shell_surface(surface);
4038 if (!shsurf)
4039 return;
4040
Jason Ekstranda7af7042013-10-12 22:38:11 -05004041 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004042
Jason Ekstranda7af7042013-10-12 22:38:11 -05004043 if (shsurf->view->alpha > 1.0)
4044 shsurf->view->alpha = 1.0;
4045 if (shsurf->view->alpha < step)
4046 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004047
Jason Ekstranda7af7042013-10-12 22:38:11 -05004048 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004049 weston_surface_damage(surface);
4050}
4051
4052static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004053do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004054 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004055{
Daniel Stone37816df2012-05-16 18:45:18 +01004056 struct weston_seat *ws = (struct weston_seat *) seat;
4057 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004058 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004059 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004060
4061 wl_list_for_each(output, &compositor->output_list, link) {
4062 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004063 wl_fixed_to_double(seat->pointer->x),
4064 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004065 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004066 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004067 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004068 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004069 increment = -output->zoom.increment;
4070 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004071 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004072 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004073 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004074 else
4075 increment = 0;
4076
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004077 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004078
Scott Moreaue6603982012-06-11 13:07:51 -06004079 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004080 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004081 else if (output->zoom.level > output->zoom.max_level)
4082 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004083 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004084 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004085 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004086
Scott Moreaue6603982012-06-11 13:07:51 -06004087 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004088
Jason Ekstranda7af7042013-10-12 22:38:11 -05004089 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004090 }
4091 }
4092}
4093
Scott Moreauccbf29d2012-02-22 14:21:41 -07004094static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004095zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004096 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004097{
4098 do_zoom(seat, time, 0, axis, value);
4099}
4100
4101static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004102zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004103 void *data)
4104{
4105 do_zoom(seat, time, key, 0, 0);
4106}
4107
4108static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004109terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004110 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004111{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004112 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004113
Daniel Stone325fc2d2012-05-30 16:31:58 +01004114 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004115}
4116
4117static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004118lower_fullscreen_layer(struct desktop_shell *shell);
4119
4120struct alt_tab {
4121 struct desktop_shell *shell;
4122 struct weston_keyboard_grab grab;
4123
4124 struct wl_list *current;
4125
4126 struct wl_list preview_list;
4127};
4128
4129struct alt_tab_preview {
4130 struct alt_tab *alt_tab;
4131
4132 struct weston_view *view;
4133 struct weston_transform transform;
4134
4135 struct wl_listener listener;
4136
4137 struct wl_list link;
4138};
4139
4140static void
4141alt_tab_next(struct alt_tab *alt_tab)
4142{
4143 alt_tab->current = alt_tab->current->next;
4144
4145 /* Make sure we're not pointing to the list header e.g. after
4146 * cycling through the whole list. */
4147 if (alt_tab->current->next == alt_tab->preview_list.next)
4148 alt_tab->current = alt_tab->current->next;
4149}
4150
4151static void
4152alt_tab_destroy(struct alt_tab *alt_tab)
4153{
4154 struct alt_tab_preview *preview, *next;
4155 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
4156
4157 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
4158 preview = wl_container_of(alt_tab->current, preview, link);
4159
4160 activate(alt_tab->shell, preview->view->surface,
4161 (struct weston_seat *) keyboard->seat);
4162 }
4163
4164 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
4165 wl_list_remove(&preview->link);
4166 wl_list_remove(&preview->listener.link);
4167 weston_view_destroy(preview->view);
4168 free(preview);
4169 }
4170
4171 weston_keyboard_end_grab(keyboard);
4172 if (keyboard->input_method_resource)
4173 keyboard->grab = &keyboard->input_method_grab;
4174
4175 free(alt_tab);
4176}
4177
4178static void
4179alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
4180{
4181 struct alt_tab_preview *preview =
4182 container_of(listener, struct alt_tab_preview, listener);
4183 struct alt_tab *alt_tab = preview->alt_tab;
4184 int advance = 0;
4185
4186 /* If the preview that we're removing is the currently selected one,
4187 * we want to move to the next one. So we move to ->prev and then
4188 * call _next() after removing the preview. */
4189 if (alt_tab->current == &preview->link) {
4190 alt_tab->current = alt_tab->current->prev;
4191 advance = 1;
4192 }
4193
4194 wl_list_remove(&preview->listener.link);
4195 wl_list_remove(&preview->link);
4196
4197 free(preview);
4198
4199 if (advance)
4200 alt_tab_next(alt_tab);
4201
4202 /* If the last preview goes away, stop the alt-tab */
4203 if (wl_list_empty(alt_tab->current))
4204 alt_tab_destroy(alt_tab);
4205}
4206
4207static void
4208alt_tab_key(struct weston_keyboard_grab *grab,
4209 uint32_t time, uint32_t key, uint32_t state_w)
4210{
4211 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4212 enum wl_keyboard_key_state state = state_w;
4213
4214 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4215 alt_tab_next(alt_tab);
4216}
4217
4218static void
4219alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4220 uint32_t mods_depressed, uint32_t mods_latched,
4221 uint32_t mods_locked, uint32_t group)
4222{
4223 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4224 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
4225
4226 if ((seat->modifier_state & MODIFIER_ALT) == 0)
4227 alt_tab_destroy(alt_tab);
4228}
4229
4230static void
4231alt_tab_cancel(struct weston_keyboard_grab *grab)
4232{
4233 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4234
4235 alt_tab_destroy(alt_tab);
4236}
4237
4238static const struct weston_keyboard_grab_interface alt_tab_grab = {
4239 alt_tab_key,
4240 alt_tab_modifier,
4241 alt_tab_cancel,
4242};
4243
4244static int
4245view_for_alt_tab(struct weston_view *view)
4246{
Rafael Antognollied207b42013-12-03 15:35:43 -02004247 struct shell_surface *shsurf = get_shell_surface(view->surface);
4248 if (!shsurf)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004249 return 0;
4250
Rafael Antognollied207b42013-12-03 15:35:43 -02004251 if (shsurf->parent)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004252 return 0;
4253
4254 if (view != get_default_view(view->surface))
4255 return 0;
4256
4257 return 1;
4258}
4259
4260static void
4261alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4262 void *data)
4263{
4264 struct alt_tab *alt_tab;
4265 struct desktop_shell *shell = data;
4266 struct weston_output *output = get_default_output(shell->compositor);
4267 struct workspace *ws;
4268 struct weston_view *view;
4269 int num_surfaces = 0;
4270 int x, y, side, margin;
4271
4272 wl_list_for_each(view, &shell->compositor->view_list, link) {
4273
4274 if (view_for_alt_tab(view))
4275 num_surfaces++;
4276 }
4277
4278 if (!num_surfaces)
4279 return;
4280
4281 alt_tab = malloc(sizeof *alt_tab);
4282 if (!alt_tab)
4283 return;
4284
4285 alt_tab->shell = shell;
4286 wl_list_init(&alt_tab->preview_list);
4287 alt_tab->current = &alt_tab->preview_list;
4288
4289 restore_all_output_modes(shell->compositor);
4290 lower_fullscreen_layer(alt_tab->shell);
4291
4292 alt_tab->grab.interface = &alt_tab_grab;
4293 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
4294 weston_keyboard_set_focus(seat->keyboard, NULL);
4295
4296 ws = get_current_workspace(shell);
4297
4298 /* FIXME: add some visual candy e.g. prelight the selected view
4299 * and/or add a black surrounding background à la gnome-shell */
4300
4301 /* Determine the size for each preview */
4302 side = output->width / num_surfaces;
4303 if (side > 200)
4304 side = 200;
4305 margin = side / 4;
4306 side -= margin;
4307
4308 x = margin;
4309 y = (output->height - side) / 2;
4310
4311 /* Create a view for each surface */
4312 wl_list_for_each(view, &shell->compositor->view_list, link) {
4313 struct alt_tab_preview *preview;
4314 struct weston_view *v;
4315 float scale;
4316
4317 if (!view_for_alt_tab(view))
4318 continue;
4319
4320 preview = malloc(sizeof *preview);
4321 if (!preview) {
4322 alt_tab_destroy(alt_tab);
4323 return;
4324 }
4325
4326 preview->alt_tab = alt_tab;
4327
4328 preview->view = v = weston_view_create(view->surface);
4329 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004330
4331 wl_list_remove(&v->layer_link);
4332 wl_list_insert(&ws->layer.view_list, &v->layer_link);
4333 weston_view_damage_below(v);
4334 weston_surface_damage(v->surface);
4335
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004336 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004337
4338 preview->listener.notify = alt_tab_handle_surface_destroy;
4339 wl_signal_add(&v->destroy_signal, &preview->listener);
4340
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004341 if (view->surface->width > view->surface->height)
4342 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004343 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004344 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004345
4346 wl_list_insert(&v->geometry.transformation_list,
4347 &preview->transform.link);
4348 weston_matrix_init(&preview->transform.matrix);
4349 weston_matrix_scale(&preview->transform.matrix,
4350 scale, scale, 1.0f);
4351
4352 weston_view_geometry_dirty(v);
4353 weston_compositor_schedule_repaint(v->surface->compositor);
4354
4355 /* Insert at the end of the list */
4356 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
4357
4358 x += side + margin;
4359 }
4360
4361 /* Start at the second preview so a simple <alt>tab changes window.
4362 * We set `current' to the first preview and not the second because
4363 * we're going to receive a key press callback for the initial
4364 * <alt>tab which will make `current' point to the second element. */
4365 alt_tab_next(alt_tab);
4366}
4367
4368static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004369rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4370 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004371{
4372 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004373 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004374 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004375 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004376 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004377
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004378 weston_pointer_move(pointer, x, y);
4379
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004380 if (!shsurf)
4381 return;
4382
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004383 cx = 0.5f * shsurf->surface->width;
4384 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004385
Daniel Stone37816df2012-05-16 18:45:18 +01004386 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4387 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004388 r = sqrtf(dx * dx + dy * dy);
4389
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004390 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004391 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004392
4393 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004394 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004395 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004396
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004397 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004398 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004399
4400 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004401 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004402 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004403 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004404 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004405
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004406 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004407 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004408 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004409 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004410 wl_list_init(&shsurf->rotation.transform.link);
4411 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004412 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004413 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004414
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004415 /* We need to adjust the position of the surface
4416 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004417 cposx = shsurf->view->geometry.x + cx;
4418 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004419 dposx = rotate->center.x - cposx;
4420 dposy = rotate->center.y - cposy;
4421 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004422 weston_view_set_position(shsurf->view,
4423 shsurf->view->geometry.x + dposx,
4424 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004425 }
4426
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004427 /* Repaint implies weston_surface_update_transform(), which
4428 * lazily applies the damage due to rotation update.
4429 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004430 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004431}
4432
4433static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004434rotate_grab_button(struct weston_pointer_grab *grab,
4435 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004436{
4437 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004438 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004439 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004440 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004441 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004442
Daniel Stone4dbadb12012-05-30 16:31:51 +01004443 if (pointer->button_count == 0 &&
4444 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004445 if (shsurf)
4446 weston_matrix_multiply(&shsurf->rotation.rotation,
4447 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004448 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004449 free(rotate);
4450 }
4451}
4452
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004453static void
4454rotate_grab_cancel(struct weston_pointer_grab *grab)
4455{
4456 struct rotate_grab *rotate =
4457 container_of(grab, struct rotate_grab, base.grab);
4458
4459 shell_grab_end(&rotate->base);
4460 free(rotate);
4461}
4462
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004463static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004464 noop_grab_focus,
4465 rotate_grab_motion,
4466 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004467 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004468};
4469
4470static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004471surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004472{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004473 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004474 float dx, dy;
4475 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004476
Pekka Paalanen460099f2012-01-20 16:48:25 +02004477 rotate = malloc(sizeof *rotate);
4478 if (!rotate)
4479 return;
4480
Jason Ekstranda7af7042013-10-12 22:38:11 -05004481 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004482 surface->surface->width * 0.5f,
4483 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004484 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004485
Daniel Stone37816df2012-05-16 18:45:18 +01004486 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4487 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004488 r = sqrtf(dx * dx + dy * dy);
4489 if (r > 20.0f) {
4490 struct weston_matrix inverse;
4491
4492 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004493 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004494 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004495
4496 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004497 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004498 } else {
4499 weston_matrix_init(&surface->rotation.rotation);
4500 weston_matrix_init(&rotate->rotation);
4501 }
4502
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004503 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4504 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004505}
4506
4507static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004508rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004509 void *data)
4510{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004511 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004512 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004513 struct shell_surface *surface;
4514
Pekka Paalanen01388e22013-04-25 13:57:44 +03004515 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004516 if (base_surface == NULL)
4517 return;
4518
4519 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004520 if (surface == NULL || surface->state.fullscreen ||
4521 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004522 return;
4523
4524 surface_rotate(surface, seat);
4525}
4526
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004527/* Move all fullscreen layers down to the current workspace in a non-reversible
4528 * manner. This should be used when implementing shell-wide overlays, such as
4529 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004530static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004531lower_fullscreen_layer(struct desktop_shell *shell)
4532{
4533 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004534 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004535
4536 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004537 wl_list_for_each_reverse_safe(view, prev,
4538 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004539 layer_link) {
4540 wl_list_remove(&view->layer_link);
4541 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4542 weston_view_damage_below(view);
4543 weston_surface_damage(view->surface);
4544 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004545}
4546
4547static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004548activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004549 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004550{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004551 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004552 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004553 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004554 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004555 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004556
Pekka Paalanen01388e22013-04-25 13:57:44 +03004557 main_surface = weston_surface_get_main_surface(es);
4558
Daniel Stone37816df2012-05-16 18:45:18 +01004559 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004560
Jonas Ådahl8538b222012-08-29 22:13:03 +02004561 state = ensure_focus_state(shell, seat);
4562 if (state == NULL)
4563 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004564
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004565 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004566 state->keyboard_focus = es;
4567 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004568 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004569
Rafael Antognolli03b16592013-12-03 15:35:42 -02004570 shsurf = get_shell_surface(main_surface);
4571 if (shsurf->state.fullscreen)
4572 shell_configure_fullscreen(shsurf);
4573 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004574 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004575
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004576 if (shell->focus_animation_type != ANIMATION_NONE) {
4577 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004578 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004579 }
4580
4581 /* Update the surface’s layer. This brings it to the top of the stacking
4582 * order as appropriate. */
4583 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004584}
4585
Alex Wu21858432012-04-01 20:13:08 +08004586/* no-op func for checking black surface */
4587static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004588black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004589{
4590}
4591
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004592static bool
Alex Wu21858432012-04-01 20:13:08 +08004593is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4594{
4595 if (es->configure == black_surface_configure) {
4596 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004597 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004598 return true;
4599 }
4600 return false;
4601}
4602
Kristian Høgsberg75840622011-09-06 13:48:16 -04004603static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004604activate_binding(struct weston_seat *seat,
4605 struct desktop_shell *shell,
4606 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004607{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004608 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004609
Alex Wu9c35e6b2012-03-05 14:13:13 +08004610 if (!focus)
4611 return;
4612
Pekka Paalanen01388e22013-04-25 13:57:44 +03004613 if (is_black_surface(focus, &main_surface))
4614 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004615
Pekka Paalanen01388e22013-04-25 13:57:44 +03004616 main_surface = weston_surface_get_main_surface(focus);
4617 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004618 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004619
Neil Robertsa28c6932013-10-03 16:43:04 +01004620 activate(shell, focus, seat);
4621}
4622
4623static void
4624click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4625 void *data)
4626{
4627 if (seat->pointer->grab != &seat->pointer->default_grab)
4628 return;
4629
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004630 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004631}
4632
4633static void
4634touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4635{
4636 if (seat->touch->grab != &seat->touch->default_grab)
4637 return;
4638
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004639 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004640}
4641
4642static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004643lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004644{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004645 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004646
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004647 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004648 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004649 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004650 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004651
4652 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004653
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004654 /* Hide all surfaces by removing the fullscreen, panel and
4655 * toplevel layers. This way nothing else can show or receive
4656 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004657
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004658 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004659 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004660 if (shell->showing_input_panels)
4661 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004662 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004663 wl_list_insert(&shell->compositor->cursor_layer.link,
4664 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004665
Pekka Paalanen77346a62011-11-30 16:26:35 +02004666 launch_screensaver(shell);
4667
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004668 /* TODO: disable bindings that should not work while locked. */
4669
4670 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004671}
4672
4673static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004674unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004675{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004676 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004677 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004678 return;
4679 }
4680
4681 /* If desktop-shell client has gone away, unlock immediately. */
4682 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004683 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004684 return;
4685 }
4686
4687 if (shell->prepare_event_sent)
4688 return;
4689
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004690 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004691 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004692}
4693
4694static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004695shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004696{
4697 struct desktop_shell *shell = data;
4698
4699 shell->fade.animation = NULL;
4700
4701 switch (shell->fade.type) {
4702 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004703 weston_surface_destroy(shell->fade.view->surface);
4704 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004705 break;
4706 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004707 lock(shell);
4708 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004709 default:
4710 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004711 }
4712}
4713
Jason Ekstranda7af7042013-10-12 22:38:11 -05004714static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004715shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004716{
4717 struct weston_compositor *compositor = shell->compositor;
4718 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004719 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004720
4721 surface = weston_surface_create(compositor);
4722 if (!surface)
4723 return NULL;
4724
Jason Ekstranda7af7042013-10-12 22:38:11 -05004725 view = weston_view_create(surface);
4726 if (!view) {
4727 weston_surface_destroy(surface);
4728 return NULL;
4729 }
4730
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004731 surface->width = 8192;
4732 surface->height = 8192;
4733 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004734 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004735 wl_list_insert(&compositor->fade_layer.view_list,
4736 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004737 pixman_region32_init(&surface->input);
4738
Jason Ekstranda7af7042013-10-12 22:38:11 -05004739 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004740}
4741
4742static void
4743shell_fade(struct desktop_shell *shell, enum fade_type type)
4744{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004745 float tint;
4746
4747 switch (type) {
4748 case FADE_IN:
4749 tint = 0.0;
4750 break;
4751 case FADE_OUT:
4752 tint = 1.0;
4753 break;
4754 default:
4755 weston_log("shell: invalid fade type\n");
4756 return;
4757 }
4758
4759 shell->fade.type = type;
4760
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 if (shell->fade.view == NULL) {
4762 shell->fade.view = shell_fade_create_surface(shell);
4763 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004764 return;
4765
Jason Ekstranda7af7042013-10-12 22:38:11 -05004766 shell->fade.view->alpha = 1.0 - tint;
4767 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004768 }
4769
4770 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004771 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004772 else
4773 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004774 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004775 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004776 shell_fade_done, shell);
4777}
4778
4779static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004780do_shell_fade_startup(void *data)
4781{
4782 struct desktop_shell *shell = data;
4783
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004784 if (shell->startup_animation_type == ANIMATION_FADE)
4785 shell_fade(shell, FADE_IN);
4786 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004787 weston_surface_destroy(shell->fade.view->surface);
4788 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004789 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004790}
4791
4792static void
4793shell_fade_startup(struct desktop_shell *shell)
4794{
4795 struct wl_event_loop *loop;
4796
4797 if (!shell->fade.startup_timer)
4798 return;
4799
4800 wl_event_source_remove(shell->fade.startup_timer);
4801 shell->fade.startup_timer = NULL;
4802
4803 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4804 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4805}
4806
4807static int
4808fade_startup_timeout(void *data)
4809{
4810 struct desktop_shell *shell = data;
4811
4812 shell_fade_startup(shell);
4813 return 0;
4814}
4815
4816static void
4817shell_fade_init(struct desktop_shell *shell)
4818{
4819 /* Make compositor output all black, and wait for the desktop-shell
4820 * client to signal it is ready, then fade in. The timer triggers a
4821 * fade-in, in case the desktop-shell client takes too long.
4822 */
4823
4824 struct wl_event_loop *loop;
4825
Jason Ekstranda7af7042013-10-12 22:38:11 -05004826 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004827 weston_log("%s: warning: fade surface already exists\n",
4828 __func__);
4829 return;
4830 }
4831
Jason Ekstranda7af7042013-10-12 22:38:11 -05004832 shell->fade.view = shell_fade_create_surface(shell);
4833 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004834 return;
4835
Jason Ekstranda7af7042013-10-12 22:38:11 -05004836 weston_view_update_transform(shell->fade.view);
4837 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004838
4839 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4840 shell->fade.startup_timer =
4841 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4842 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4843}
4844
4845static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004846idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004847{
4848 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004849 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004850
4851 shell_fade(shell, FADE_OUT);
4852 /* lock() is called from shell_fade_done() */
4853}
4854
4855static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004856wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004857{
4858 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004859 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004860
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004861 unlock(shell);
4862}
4863
4864static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004865show_input_panels(struct wl_listener *listener, void *data)
4866{
4867 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004868 container_of(listener, struct desktop_shell,
4869 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004870 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004871
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004872 shell->text_input.surface = (struct weston_surface*)data;
4873
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004874 if (shell->showing_input_panels)
4875 return;
4876
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004877 shell->showing_input_panels = true;
4878
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004879 if (!shell->locked)
4880 wl_list_insert(&shell->panel_layer.link,
4881 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004882
Jason Ekstranda7af7042013-10-12 22:38:11 -05004883 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004884 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004885 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004886 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004887 wl_list_insert(&shell->input_panel_layer.view_list,
4888 &ipsurf->view->layer_link);
4889 weston_view_geometry_dirty(ipsurf->view);
4890 weston_view_update_transform(ipsurf->view);
4891 weston_surface_damage(ipsurf->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004892 weston_slide_run(ipsurf->view, ipsurf->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004893 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004894 }
4895}
4896
4897static void
4898hide_input_panels(struct wl_listener *listener, void *data)
4899{
4900 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004901 container_of(listener, struct desktop_shell,
4902 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004903 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004904
Jan Arne Petersen61381972013-01-31 15:52:21 +01004905 if (!shell->showing_input_panels)
4906 return;
4907
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004908 shell->showing_input_panels = false;
4909
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004910 if (!shell->locked)
4911 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004912
Jason Ekstranda7af7042013-10-12 22:38:11 -05004913 wl_list_for_each_safe(view, next,
4914 &shell->input_panel_layer.view_list, layer_link)
4915 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004916}
4917
4918static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004919update_input_panels(struct wl_listener *listener, void *data)
4920{
4921 struct desktop_shell *shell =
4922 container_of(listener, struct desktop_shell,
4923 update_input_panel_listener);
4924
4925 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4926}
4927
4928static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004929center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004930{
Giulio Camuffob8366642013-04-25 13:57:46 +03004931 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004932 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004933
Jason Ekstranda7af7042013-10-12 22:38:11 -05004934 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004935
4936 x = output->x + (output->width - width) / 2 - surf_x / 2;
4937 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004938
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004939 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004940}
4941
4942static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004943weston_view_set_initial_position(struct weston_view *view,
4944 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004945{
4946 struct weston_compositor *compositor = shell->compositor;
4947 int ix = 0, iy = 0;
4948 int range_x, range_y;
4949 int dx, dy, x, y, panel_height;
4950 struct weston_output *output, *target_output = NULL;
4951 struct weston_seat *seat;
4952
4953 /* As a heuristic place the new window on the same output as the
4954 * pointer. Falling back to the output containing 0, 0.
4955 *
4956 * TODO: Do something clever for touch too?
4957 */
4958 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004959 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004960 ix = wl_fixed_to_int(seat->pointer->x);
4961 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004962 break;
4963 }
4964 }
4965
4966 wl_list_for_each(output, &compositor->output_list, link) {
4967 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4968 target_output = output;
4969 break;
4970 }
4971 }
4972
4973 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004974 weston_view_set_position(view, 10 + random() % 400,
4975 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004976 return;
4977 }
4978
4979 /* Valid range within output where the surface will still be onscreen.
4980 * If this is negative it means that the surface is bigger than
4981 * output.
4982 */
4983 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004984 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004985 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004986 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004987
Rob Bradford4cb88c72012-08-13 15:18:44 +01004988 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004989 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004990 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004991 dx = 0;
4992
4993 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004994 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004995 else
4996 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004997
4998 x = target_output->x + dx;
4999 y = target_output->y + dy;
5000
Jason Ekstranda7af7042013-10-12 22:38:11 -05005001 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005002}
5003
5004static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005005map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005006 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005007{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005008 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005009 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08005010 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03005011 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005012
Pekka Paalanen77346a62011-11-30 16:26:35 +02005013 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005014 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005015 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005016 if (shsurf->state.fullscreen) {
5017 center_on_output(shsurf->view, shsurf->fullscreen_output);
5018 shell_map_fullscreen(shsurf);
5019 } else if (shsurf->state.maximized) {
5020 /* use surface configure to set the geometry */
5021 panel_height = get_output_panel_height(shell, shsurf->output);
5022 surface_subsurfaces_boundingbox(shsurf->surface,
5023 &surf_x, &surf_y, NULL, NULL);
5024 weston_view_set_position(shsurf->view,
5025 shsurf->output->x - surf_x,
5026 shsurf->output->y +
5027 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02005028 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005029 weston_view_set_initial_position(shsurf->view, shell);
5030 }
Juan Zhao96879df2012-02-07 08:45:41 +08005031 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005032 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005033 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005034 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005035 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005036 weston_view_set_position(shsurf->view,
5037 shsurf->view->geometry.x + sx,
5038 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005039 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005040 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005041 default:
5042 ;
5043 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005044
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005045 /* Surface stacking order, see also activate(). */
5046 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005047
Jason Ekstranda7af7042013-10-12 22:38:11 -05005048 if (shsurf->type != SHELL_SURFACE_NONE) {
5049 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005050 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005051 shsurf->surface->output = shsurf->output;
5052 shsurf->view->output = shsurf->output;
5053 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005054 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005055
Rafael Antognollied207b42013-12-03 15:35:43 -02005056 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
5057 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
5058 }
5059
Jason Ekstranda7af7042013-10-12 22:38:11 -05005060 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005061 /* XXX: xwayland's using the same fields for transient type */
5062 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005063 if (shsurf->transient.flags ==
5064 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5065 break;
5066 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005067 if (shsurf->state.relative &&
5068 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5069 break;
5070 if (!shell->locked)
5071 break;
5072 wl_list_for_each(seat, &compositor->seat_list, link)
5073 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005074 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005075 case SHELL_SURFACE_POPUP:
5076 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005077 default:
5078 break;
5079 }
5080
Rafael Antognolli03b16592013-12-03 15:35:42 -02005081 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5082 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005083 {
5084 switch (shell->win_animation_type) {
5085 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005086 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005087 break;
5088 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005089 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005090 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005091 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005092 default:
5093 break;
5094 }
5095 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005096}
5097
5098static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005099configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005100 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005101{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005102 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005103 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005104 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005105
Pekka Paalanen77346a62011-11-30 16:26:35 +02005106 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005107
Rafael Antognolli03b16592013-12-03 15:35:42 -02005108 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005109 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005110 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005111 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005112 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5113 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005114 mx = shsurf->output->x - surf_x;
5115 my = shsurf->output->y +
5116 get_output_panel_height(shell,shsurf->output) - surf_y;
5117 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005118 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005119 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005120 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005121
Alex Wu4539b082012-03-01 12:57:46 +08005122 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005123 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005124 wl_list_for_each(view, &surface->views, surface_link)
5125 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005126
Rafael Antognolli03b16592013-12-03 15:35:42 -02005127 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005128 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005129 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005130}
5131
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005132static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005133shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005134{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005135 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03005136 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01005137
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005138 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005139
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005140 if (!weston_surface_is_mapped(es) &&
5141 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005142 remove_popup_grab(shsurf);
5143 }
5144
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005145 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005146 return;
5147
Rafael Antognolli03b16592013-12-03 15:35:42 -02005148 if ((shsurf->next_type != SHELL_SURFACE_NONE &&
5149 shsurf->type != shsurf->next_type) ||
5150 shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005151 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005152 type_changed = 1;
5153 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005154
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005155 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005156 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005157 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005158 shsurf->last_width != es->width ||
5159 shsurf->last_height != es->height) {
5160 shsurf->last_width = es->width;
5161 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005162 float from_x, from_y;
5163 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005164
Jason Ekstranda7af7042013-10-12 22:38:11 -05005165 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5166 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005167 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005168 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005169 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005170 }
5171}
5172
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005173static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005174
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005175static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005176desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005177{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005178 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005179 struct desktop_shell *shell =
5180 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005181
5182 shell->child.process.pid = 0;
5183 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005184
5185 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5186 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005187 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005188 shell->child.deathstamp = time;
5189 shell->child.deathcount = 0;
5190 }
5191
5192 shell->child.deathcount++;
5193 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005194 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005195 return;
5196 }
5197
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005198 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005199 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005200 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005201}
5202
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005203static void
5204launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005205{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005206 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005207
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005208 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005209 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005210 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005211 desktop_shell_sigchld);
5212
5213 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005214 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005215}
5216
5217static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005218bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5219{
Tiago Vignattibe143262012-04-16 17:31:41 +03005220 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005221 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005222
Jason Ekstranda85118c2013-06-27 20:17:02 -05005223 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
5224 if (resource)
5225 wl_resource_set_implementation(resource, &shell_implementation,
5226 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005227}
5228
Kristian Høgsberg75840622011-09-06 13:48:16 -04005229static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02005230bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5231{
5232 struct desktop_shell *shell = data;
5233 struct wl_resource *resource;
5234
5235 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
5236 if (resource)
5237 wl_resource_set_dispatcher(resource,
5238 xdg_shell_unversioned_dispatch,
5239 NULL, shell, NULL);
5240}
5241
5242static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005243unbind_desktop_shell(struct wl_resource *resource)
5244{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005245 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005246
5247 if (shell->locked)
5248 resume_desktop(shell);
5249
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005250 shell->child.desktop_shell = NULL;
5251 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005252}
5253
5254static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005255bind_desktop_shell(struct wl_client *client,
5256 void *data, uint32_t version, uint32_t id)
5257{
Tiago Vignattibe143262012-04-16 17:31:41 +03005258 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005259 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005260
Jason Ekstranda85118c2013-06-27 20:17:02 -05005261 resource = wl_resource_create(client, &desktop_shell_interface,
5262 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005263
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005264 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005265 wl_resource_set_implementation(resource,
5266 &desktop_shell_implementation,
5267 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005268 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005269
5270 if (version < 2)
5271 shell_fade_startup(shell);
5272
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005273 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005274 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005275
5276 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5277 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005278 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005279}
5280
Pekka Paalanen6e168112011-11-24 11:34:05 +02005281static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005282screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005283{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005284 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005285 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005286
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005287 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005288 return;
5289
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005290 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005291 if (!shell->locked)
5292 return;
5293
Jason Ekstranda7af7042013-10-12 22:38:11 -05005294 view = container_of(surface->views.next, struct weston_view, surface_link);
5295 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005296
Jason Ekstranda7af7042013-10-12 22:38:11 -05005297 if (wl_list_empty(&view->layer_link)) {
5298 wl_list_insert(shell->lock_layer.view_list.prev,
5299 &view->layer_link);
5300 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005301 wl_event_source_timer_update(shell->screensaver.timer,
5302 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005303 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005304 }
5305}
5306
5307static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005308screensaver_set_surface(struct wl_client *client,
5309 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005310 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005311 struct wl_resource *output_resource)
5312{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005313 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005314 struct weston_surface *surface =
5315 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005316 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005317 struct weston_view *view, *next;
5318
5319 /* Make sure we only have one view */
5320 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5321 weston_view_destroy(view);
5322 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005323
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005324 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005325 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005326 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005327}
5328
5329static const struct screensaver_interface screensaver_implementation = {
5330 screensaver_set_surface
5331};
5332
5333static void
5334unbind_screensaver(struct wl_resource *resource)
5335{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005336 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005337
Pekka Paalanen77346a62011-11-30 16:26:35 +02005338 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005339}
5340
5341static void
5342bind_screensaver(struct wl_client *client,
5343 void *data, uint32_t version, uint32_t id)
5344{
Tiago Vignattibe143262012-04-16 17:31:41 +03005345 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005346 struct wl_resource *resource;
5347
Jason Ekstranda85118c2013-06-27 20:17:02 -05005348 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005349
Pekka Paalanen77346a62011-11-30 16:26:35 +02005350 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005351 wl_resource_set_implementation(resource,
5352 &screensaver_implementation,
5353 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005354 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005355 return;
5356 }
5357
5358 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5359 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005360 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005361}
5362
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005363static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005364input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005365{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005366 struct input_panel_surface *ip_surface = surface->configure_private;
5367 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005368 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005369 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005370
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005371 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005372 return;
5373
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005374 if (!weston_surface_is_mapped(surface)) {
5375 if (!shell->showing_input_panels)
5376 return;
5377
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005378 show_surface = 1;
5379 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005380
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005381 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005382
5383 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005384 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
5385 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005386 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005387 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
5388 y = ip_surface->output->y + ip_surface->output->height - surface->height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005389 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005390
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005391 weston_view_set_position(ip_surface->view, x, y);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005392
5393 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005394 wl_list_insert(&shell->input_panel_layer.view_list,
5395 &ip_surface->view->layer_link);
5396 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005397 weston_surface_damage(surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005398 weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005399 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005400}
5401
5402static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005403destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005404{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005405 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
5406
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005407 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005408 wl_list_remove(&input_panel_surface->link);
5409
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005410 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005411 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005412
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005413 free(input_panel_surface);
5414}
5415
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005416static struct input_panel_surface *
5417get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005418{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005419 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005420 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005421 } else {
5422 return NULL;
5423 }
5424}
5425
5426static void
5427input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
5428{
5429 struct input_panel_surface *ipsurface = container_of(listener,
5430 struct input_panel_surface,
5431 surface_destroy_listener);
5432
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005433 if (ipsurface->resource) {
5434 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005435 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005436 destroy_input_panel_surface(ipsurface);
5437 }
5438}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005439
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005440static struct input_panel_surface *
5441create_input_panel_surface(struct desktop_shell *shell,
5442 struct weston_surface *surface)
5443{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005444 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005445
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005446 input_panel_surface = calloc(1, sizeof *input_panel_surface);
5447 if (!input_panel_surface)
5448 return NULL;
5449
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005450 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005451 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005452
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005453 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005454
5455 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005456 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005457
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005458 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005459 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005460 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005461 &input_panel_surface->surface_destroy_listener);
5462
5463 wl_list_init(&input_panel_surface->link);
5464
5465 return input_panel_surface;
5466}
5467
5468static void
5469input_panel_surface_set_toplevel(struct wl_client *client,
5470 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005471 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005472 uint32_t position)
5473{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005474 struct input_panel_surface *input_panel_surface =
5475 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005476 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005477
5478 wl_list_insert(&shell->input_panel.surfaces,
5479 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005480
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005481 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005482 input_panel_surface->panel = 0;
5483}
5484
5485static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02005486input_panel_surface_set_overlay_panel(struct wl_client *client,
5487 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005488{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005489 struct input_panel_surface *input_panel_surface =
5490 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005491 struct desktop_shell *shell = input_panel_surface->shell;
5492
5493 wl_list_insert(&shell->input_panel.surfaces,
5494 &input_panel_surface->link);
5495
5496 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005497}
5498
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005499static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005500 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02005501 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005502};
5503
5504static void
5505destroy_input_panel_surface_resource(struct wl_resource *resource)
5506{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005507 struct input_panel_surface *ipsurf =
5508 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005509
5510 destroy_input_panel_surface(ipsurf);
5511}
5512
5513static void
5514input_panel_get_input_panel_surface(struct wl_client *client,
5515 struct wl_resource *resource,
5516 uint32_t id,
5517 struct wl_resource *surface_resource)
5518{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005519 struct weston_surface *surface =
5520 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005521 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005522 struct input_panel_surface *ipsurf;
5523
5524 if (get_input_panel_surface(surface)) {
5525 wl_resource_post_error(surface_resource,
5526 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005527 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005528 return;
5529 }
5530
5531 ipsurf = create_input_panel_surface(shell, surface);
5532 if (!ipsurf) {
5533 wl_resource_post_error(surface_resource,
5534 WL_DISPLAY_ERROR_INVALID_OBJECT,
5535 "surface->configure already set");
5536 return;
5537 }
5538
Jason Ekstranda85118c2013-06-27 20:17:02 -05005539 ipsurf->resource =
5540 wl_resource_create(client,
5541 &wl_input_panel_surface_interface, 1, id);
5542 wl_resource_set_implementation(ipsurf->resource,
5543 &input_panel_surface_implementation,
5544 ipsurf,
5545 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005546}
5547
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005548static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005549 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005550};
5551
5552static void
5553unbind_input_panel(struct wl_resource *resource)
5554{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005555 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005556
5557 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005558}
5559
5560static void
5561bind_input_panel(struct wl_client *client,
5562 void *data, uint32_t version, uint32_t id)
5563{
5564 struct desktop_shell *shell = data;
5565 struct wl_resource *resource;
5566
Jason Ekstranda85118c2013-06-27 20:17:02 -05005567 resource = wl_resource_create(client,
5568 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005569
5570 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005571 wl_resource_set_implementation(resource,
5572 &input_panel_implementation,
5573 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005574 shell->input_panel.binding = resource;
5575 return;
5576 }
5577
5578 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5579 "interface object already bound");
5580 wl_resource_destroy(resource);
5581}
5582
Kristian Høgsberg07045392012-02-19 18:52:44 -05005583struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005584 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005585 struct weston_surface *current;
5586 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005587 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005588};
5589
5590static void
5591switcher_next(struct switcher *switcher)
5592{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005593 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005594 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005595 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005596 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005597
Jason Ekstranda7af7042013-10-12 22:38:11 -05005598 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005599 shsurf = get_shell_surface(view->surface);
5600 switch (shsurf->type) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005601 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005602 if (shsurf->parent)
5603 break;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005604 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005605 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005606 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005607 next = view->surface;
5608 prev = view->surface;
5609 view->alpha = 0.25;
5610 weston_view_geometry_dirty(view);
5611 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005612 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005613 case SHELL_SURFACE_POPUP:
5614 case SHELL_SURFACE_XWAYLAND:
5615 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005616 default:
5617 break;
5618 }
Alex Wu1659daa2012-04-01 20:13:09 +08005619
Jason Ekstranda7af7042013-10-12 22:38:11 -05005620 if (is_black_surface(view->surface, NULL)) {
5621 view->alpha = 0.25;
5622 weston_view_geometry_dirty(view);
5623 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005624 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005625 }
5626
5627 if (next == NULL)
5628 next = first;
5629
Alex Wu07b26062012-03-12 16:06:01 +08005630 if (next == NULL)
5631 return;
5632
Kristian Høgsberg07045392012-02-19 18:52:44 -05005633 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005634 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005635
5636 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005637 wl_list_for_each(view, &next->views, surface_link)
5638 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005639
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005640 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005641 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005642 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005643}
5644
5645static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005646switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005647{
5648 struct switcher *switcher =
5649 container_of(listener, struct switcher, listener);
5650
5651 switcher_next(switcher);
5652}
5653
5654static void
Daniel Stone351eb612012-05-31 15:27:47 -04005655switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005656{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005657 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005658 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005659 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005660
Jason Ekstranda7af7042013-10-12 22:38:11 -05005661 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005662 if (is_focus_view(view))
5663 continue;
5664
Jason Ekstranda7af7042013-10-12 22:38:11 -05005665 view->alpha = 1.0;
5666 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005667 }
5668
Alex Wu07b26062012-03-12 16:06:01 +08005669 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005670 activate(switcher->shell, switcher->current,
5671 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005672 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005673 weston_keyboard_end_grab(keyboard);
5674 if (keyboard->input_method_resource)
5675 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005676 free(switcher);
5677}
5678
5679static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005680switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005681 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005682{
5683 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005684 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005685
Daniel Stonec9785ea2012-05-30 16:31:52 +01005686 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005687 switcher_next(switcher);
5688}
5689
5690static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005691switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005692 uint32_t mods_depressed, uint32_t mods_latched,
5693 uint32_t mods_locked, uint32_t group)
5694{
5695 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005696 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005697
Daniel Stone351eb612012-05-31 15:27:47 -04005698 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5699 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005700}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005701
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005702static void
5703switcher_cancel(struct weston_keyboard_grab *grab)
5704{
5705 struct switcher *switcher = container_of(grab, struct switcher, grab);
5706
5707 switcher_destroy(switcher);
5708}
5709
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005710static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005711 switcher_key,
5712 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005713 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005714};
5715
5716static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005717switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005718 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005719{
Tiago Vignattibe143262012-04-16 17:31:41 +03005720 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005721 struct switcher *switcher;
5722
5723 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005724 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005725 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005726 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005727 wl_list_init(&switcher->listener.link);
5728
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005729 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005730 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005731 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005732 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5733 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005734 switcher_next(switcher);
5735}
5736
Daniel Stonedf8133b2013-11-19 11:37:14 +01005737struct exposay_surface {
5738 struct desktop_shell *shell;
5739 struct weston_surface *surface;
5740 struct weston_view *view;
5741 struct wl_list link;
5742
5743 int x;
5744 int y;
5745 int width;
5746 int height;
5747 double scale;
5748
5749 int row;
5750 int column;
5751
5752 /* The animations only apply a transformation for their own lifetime,
5753 * and don't have an option to indefinitely maintain the
5754 * transformation in a steady state - so, we apply our own once the
5755 * animation has finished. */
5756 struct weston_transform transform;
5757};
5758
5759static void exposay_set_state(struct desktop_shell *shell,
5760 enum exposay_target_state state,
5761 struct weston_seat *seat);
5762static void exposay_check_state(struct desktop_shell *shell);
5763
5764static void
5765exposay_in_flight_inc(struct desktop_shell *shell)
5766{
5767 shell->exposay.in_flight++;
5768}
5769
5770static void
5771exposay_in_flight_dec(struct desktop_shell *shell)
5772{
5773 if (--shell->exposay.in_flight > 0)
5774 return;
5775
5776 exposay_check_state(shell);
5777}
5778
5779static void
5780exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5781{
5782 struct exposay_surface *esurface = data;
5783
5784 wl_list_insert(&esurface->view->geometry.transformation_list,
5785 &esurface->transform.link);
5786 weston_matrix_init(&esurface->transform.matrix);
5787 weston_matrix_scale(&esurface->transform.matrix,
5788 esurface->scale, esurface->scale, 1.0f);
5789 weston_matrix_translate(&esurface->transform.matrix,
5790 esurface->x - esurface->view->geometry.x,
5791 esurface->y - esurface->view->geometry.y,
5792 0);
5793
5794 weston_view_geometry_dirty(esurface->view);
5795 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5796
5797 exposay_in_flight_dec(esurface->shell);
5798}
5799
5800static void
5801exposay_animate_in(struct exposay_surface *esurface)
5802{
5803 exposay_in_flight_inc(esurface->shell);
5804
5805 weston_move_scale_run(esurface->view,
5806 esurface->x - esurface->view->geometry.x,
5807 esurface->y - esurface->view->geometry.y,
5808 1.0, esurface->scale, 0,
5809 exposay_animate_in_done, esurface);
5810}
5811
5812static void
5813exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5814{
5815 struct exposay_surface *esurface = data;
5816 struct desktop_shell *shell = esurface->shell;
5817
5818 wl_list_remove(&esurface->link);
5819 free(esurface);
5820
5821 exposay_in_flight_dec(shell);
5822}
5823
5824static void
5825exposay_animate_out(struct exposay_surface *esurface)
5826{
5827 exposay_in_flight_inc(esurface->shell);
5828
5829 /* Remove the static transformation set up by
5830 * exposay_transform_in_done(). */
5831 wl_list_remove(&esurface->transform.link);
5832 weston_view_geometry_dirty(esurface->view);
5833
5834 weston_move_scale_run(esurface->view,
5835 esurface->x - esurface->view->geometry.x,
5836 esurface->y - esurface->view->geometry.y,
5837 1.0, esurface->scale, 1,
5838 exposay_animate_out_done, esurface);
5839}
5840
5841static void
5842exposay_highlight_surface(struct desktop_shell *shell,
5843 struct exposay_surface *esurface)
5844{
5845 struct weston_view *view = NULL;
5846
5847 if (esurface) {
5848 shell->exposay.row_current = esurface->row;
5849 shell->exposay.column_current = esurface->column;
5850 view = esurface->view;
5851 }
5852
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005853 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005854 shell->exposay.focus_current = view;
5855}
5856
5857static int
5858exposay_is_animating(struct desktop_shell *shell)
5859{
5860 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5861 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5862 return 0;
5863
5864 return (shell->exposay.in_flight > 0);
5865}
5866
5867static void
5868exposay_pick(struct desktop_shell *shell, int x, int y)
5869{
5870 struct exposay_surface *esurface;
5871
5872 if (exposay_is_animating(shell))
5873 return;
5874
5875 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5876 if (x < esurface->x || x > esurface->x + esurface->width)
5877 continue;
5878 if (y < esurface->y || y > esurface->y + esurface->height)
5879 continue;
5880
5881 exposay_highlight_surface(shell, esurface);
5882 return;
5883 }
5884}
5885
5886/* Pretty lame layout for now; just tries to make a square. Should take
5887 * aspect ratio into account really. Also needs to be notified of surface
5888 * addition and removal and adjust layout/animate accordingly. */
5889static enum exposay_layout_state
5890exposay_layout(struct desktop_shell *shell)
5891{
5892 struct workspace *workspace = shell->exposay.workspace;
5893 struct weston_compositor *compositor = shell->compositor;
5894 struct weston_output *output = get_default_output(compositor);
5895 struct weston_view *view;
5896 struct exposay_surface *esurface;
5897 int w, h;
5898 int i;
5899 int last_row_removed = 0;
5900
5901 wl_list_init(&shell->exposay.surface_list);
5902
5903 shell->exposay.num_surfaces = 0;
5904 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5905 if (!get_shell_surface(view->surface))
5906 continue;
5907 shell->exposay.num_surfaces++;
5908 }
5909
5910 if (shell->exposay.num_surfaces == 0) {
5911 shell->exposay.grid_size = 0;
5912 shell->exposay.hpadding_outer = 0;
5913 shell->exposay.vpadding_outer = 0;
5914 shell->exposay.padding_inner = 0;
5915 shell->exposay.surface_size = 0;
5916 return EXPOSAY_LAYOUT_OVERVIEW;
5917 }
5918
5919 /* Lay the grid out as square as possible, losing surfaces from the
5920 * bottom row if required. Start with fixed padding of a 10% margin
5921 * around the outside and 80px internal padding between surfaces, and
5922 * maximise the area made available to surfaces after this, but only
5923 * to a maximum of 1/3rd the total output size.
5924 *
5925 * If we can't make a square grid, add one extra row at the bottom
5926 * which will have a smaller number of columns.
5927 *
5928 * XXX: Surely there has to be a better way to express this maths,
5929 * right?!
5930 */
5931 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5932 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5933 shell->exposay.grid_size++;
5934 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5935
5936 shell->exposay.hpadding_outer = (output->width / 10);
5937 shell->exposay.vpadding_outer = (output->height / 10);
5938 shell->exposay.padding_inner = 80;
5939
5940 w = output->width - (shell->exposay.hpadding_outer * 2);
5941 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5942 w /= shell->exposay.grid_size;
5943
5944 h = output->height - (shell->exposay.vpadding_outer * 2);
5945 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5946 h /= shell->exposay.grid_size;
5947
5948 shell->exposay.surface_size = (w < h) ? w : h;
5949 if (shell->exposay.surface_size > (output->width / 2))
5950 shell->exposay.surface_size = output->width / 2;
5951 if (shell->exposay.surface_size > (output->height / 2))
5952 shell->exposay.surface_size = output->height / 2;
5953
5954 i = 0;
5955 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5956 int pad;
5957
5958 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5959
5960 if (!get_shell_surface(view->surface))
5961 continue;
5962
5963 esurface = malloc(sizeof(*esurface));
5964 if (!esurface) {
5965 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5966 shell->exposay.seat);
5967 break;
5968 }
5969
5970 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5971 esurface->shell = shell;
5972 esurface->view = view;
5973
5974 esurface->row = i / shell->exposay.grid_size;
5975 esurface->column = i % shell->exposay.grid_size;
5976
5977 esurface->x = shell->exposay.hpadding_outer;
5978 esurface->x += pad * esurface->column;
5979 esurface->y = shell->exposay.vpadding_outer;
5980 esurface->y += pad * esurface->row;
5981
5982 if (esurface->row == shell->exposay.grid_size - 1)
5983 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5984
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005985 if (view->surface->width > view->surface->height)
5986 esurface->scale = shell->exposay.surface_size / (float) view->surface->width;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005987 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005988 esurface->scale = shell->exposay.surface_size / (float) view->surface->height;
5989 esurface->width = view->surface->width * esurface->scale;
5990 esurface->height = view->surface->height * esurface->scale;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005991
5992 if (shell->exposay.focus_current == esurface->view)
5993 exposay_highlight_surface(shell, esurface);
5994
5995 exposay_animate_in(esurface);
5996
5997 i++;
5998 }
5999
6000 weston_compositor_schedule_repaint(shell->compositor);
6001
6002 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
6003}
6004
6005static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01006006exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
6007 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01006008{
6009 struct desktop_shell *shell =
6010 container_of(grab, struct desktop_shell, exposay.grab_ptr);
6011
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01006012 weston_pointer_move(grab->pointer, x, y);
6013
Daniel Stonedf8133b2013-11-19 11:37:14 +01006014 exposay_pick(shell,
6015 wl_fixed_to_int(grab->pointer->x),
6016 wl_fixed_to_int(grab->pointer->y));
6017}
6018
6019static void
6020exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
6021 uint32_t state_w)
6022{
6023 struct desktop_shell *shell =
6024 container_of(grab, struct desktop_shell, exposay.grab_ptr);
6025 struct weston_seat *seat = grab->pointer->seat;
6026 enum wl_pointer_button_state state = state_w;
6027
6028 if (button != BTN_LEFT)
6029 return;
6030
6031 /* Store the surface we clicked on, and don't do anything if we end up
6032 * releasing on a different surface. */
6033 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
6034 shell->exposay.clicked = shell->exposay.focus_current;
6035 return;
6036 }
6037
6038 if (shell->exposay.focus_current == shell->exposay.clicked)
6039 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
6040 else
6041 shell->exposay.clicked = NULL;
6042}
6043
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01006044static void
6045exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
6046{
6047 struct desktop_shell *shell =
6048 container_of(grab, struct desktop_shell, exposay.grab_ptr);
6049
6050 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
6051}
6052
Daniel Stonedf8133b2013-11-19 11:37:14 +01006053static const struct weston_pointer_grab_interface exposay_ptr_grab = {
6054 noop_grab_focus,
6055 exposay_motion,
6056 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01006057 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01006058};
6059
6060static int
6061exposay_maybe_move(struct desktop_shell *shell, int row, int column)
6062{
6063 struct exposay_surface *esurface;
6064
6065 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
6066 if (esurface->row != row || esurface->column != column)
6067 continue;
6068
6069 exposay_highlight_surface(shell, esurface);
6070 return 1;
6071 }
6072
6073 return 0;
6074}
6075
6076static void
6077exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
6078 uint32_t state_w)
6079{
6080 struct weston_seat *seat = grab->keyboard->seat;
6081 struct desktop_shell *shell =
6082 container_of(grab, struct desktop_shell, exposay.grab_kbd);
6083 enum wl_keyboard_key_state state = state_w;
6084
6085 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
6086 return;
6087
6088 switch (key) {
6089 case KEY_ESC:
6090 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
6091 break;
6092 case KEY_ENTER:
6093 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
6094 break;
6095 case KEY_UP:
6096 exposay_maybe_move(shell, shell->exposay.row_current - 1,
6097 shell->exposay.column_current);
6098 break;
6099 case KEY_DOWN:
6100 /* Special case for trying to move to the bottom row when it
6101 * has fewer items than all the others. */
6102 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
6103 shell->exposay.column_current) &&
6104 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
6105 exposay_maybe_move(shell, shell->exposay.row_current + 1,
6106 (shell->exposay.num_surfaces %
6107 shell->exposay.grid_size) - 1);
6108 }
6109 break;
6110 case KEY_LEFT:
6111 exposay_maybe_move(shell, shell->exposay.row_current,
6112 shell->exposay.column_current - 1);
6113 break;
6114 case KEY_RIGHT:
6115 exposay_maybe_move(shell, shell->exposay.row_current,
6116 shell->exposay.column_current + 1);
6117 break;
6118 case KEY_TAB:
6119 /* Try to move right, then down (and to the leftmost column),
6120 * then if all else fails, to the top left. */
6121 if (!exposay_maybe_move(shell, shell->exposay.row_current,
6122 shell->exposay.column_current + 1) &&
6123 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
6124 exposay_maybe_move(shell, 0, 0);
6125 break;
6126 default:
6127 break;
6128 }
6129}
6130
6131static void
6132exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
6133 uint32_t mods_depressed, uint32_t mods_latched,
6134 uint32_t mods_locked, uint32_t group)
6135{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01006136 struct desktop_shell *shell =
6137 container_of(grab, struct desktop_shell, exposay.grab_kbd);
6138 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
6139
6140 /* We want to know when mod has been pressed and released.
6141 * FIXME: There is a problem here: if mod is pressed, then a key
6142 * is pressed and released, then mod is released, we will treat that
6143 * as if only mod had been pressed and released. */
6144 if (seat->modifier_state) {
6145 if (seat->modifier_state == shell->binding_modifier) {
6146 shell->exposay.mod_pressed = true;
6147 } else {
6148 shell->exposay.mod_invalid = true;
6149 }
6150 } else {
6151 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
6152 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
6153
6154 shell->exposay.mod_invalid = false;
6155 shell->exposay.mod_pressed = false;
6156 }
6157
6158 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01006159}
6160
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01006161static void
6162exposay_cancel(struct weston_keyboard_grab *grab)
6163{
6164 struct desktop_shell *shell =
6165 container_of(grab, struct desktop_shell, exposay.grab_kbd);
6166
6167 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
6168}
6169
Daniel Stonedf8133b2013-11-19 11:37:14 +01006170static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
6171 exposay_key,
6172 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01006173 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01006174};
6175
6176/**
6177 * Called when the transition from overview -> inactive has completed.
6178 */
6179static enum exposay_layout_state
6180exposay_set_inactive(struct desktop_shell *shell)
6181{
6182 struct weston_seat *seat = shell->exposay.seat;
6183
6184 weston_keyboard_end_grab(seat->keyboard);
6185 weston_pointer_end_grab(seat->pointer);
6186 if (seat->keyboard->input_method_resource)
6187 seat->keyboard->grab = &seat->keyboard->input_method_grab;
6188
6189 return EXPOSAY_LAYOUT_INACTIVE;
6190}
6191
6192/**
6193 * Begins the transition from overview to inactive. */
6194static enum exposay_layout_state
6195exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
6196{
6197 struct exposay_surface *esurface;
6198
6199 /* Call activate() before we start the animations to avoid
6200 * animating back the old state and then immediately transitioning
6201 * to the new. */
6202 if (switch_focus && shell->exposay.focus_current)
6203 activate(shell, shell->exposay.focus_current->surface,
6204 shell->exposay.seat);
6205 else if (shell->exposay.focus_prev)
6206 activate(shell, shell->exposay.focus_prev->surface,
6207 shell->exposay.seat);
6208
6209 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
6210 exposay_animate_out(esurface);
6211 weston_compositor_schedule_repaint(shell->compositor);
6212
6213 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
6214}
6215
6216static enum exposay_layout_state
6217exposay_transition_active(struct desktop_shell *shell)
6218{
6219 struct weston_seat *seat = shell->exposay.seat;
6220
6221 shell->exposay.workspace = get_current_workspace(shell);
6222 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
6223 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
6224 shell->exposay.clicked = NULL;
6225 wl_list_init(&shell->exposay.surface_list);
6226
6227 lower_fullscreen_layer(shell);
6228 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
6229 weston_keyboard_start_grab(seat->keyboard,
6230 &shell->exposay.grab_kbd);
6231 weston_keyboard_set_focus(seat->keyboard, NULL);
6232
6233 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
6234 weston_pointer_start_grab(seat->pointer,
6235 &shell->exposay.grab_ptr);
6236 weston_pointer_set_focus(seat->pointer, NULL,
6237 seat->pointer->x, seat->pointer->y);
6238
6239 return exposay_layout(shell);
6240}
6241
6242static void
6243exposay_check_state(struct desktop_shell *shell)
6244{
6245 enum exposay_layout_state state_new = shell->exposay.state_cur;
6246 int do_switch = 0;
6247
6248 /* Don't do anything whilst animations are running, just store up
6249 * target state changes and only act on them when the animations have
6250 * completed. */
6251 if (exposay_is_animating(shell))
6252 return;
6253
6254 switch (shell->exposay.state_target) {
6255 case EXPOSAY_TARGET_OVERVIEW:
6256 switch (shell->exposay.state_cur) {
6257 case EXPOSAY_LAYOUT_OVERVIEW:
6258 goto out;
6259 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
6260 state_new = EXPOSAY_LAYOUT_OVERVIEW;
6261 break;
6262 default:
6263 state_new = exposay_transition_active(shell);
6264 break;
6265 }
6266 break;
6267
6268 case EXPOSAY_TARGET_SWITCH:
6269 do_switch = 1; /* fallthrough */
6270 case EXPOSAY_TARGET_CANCEL:
6271 switch (shell->exposay.state_cur) {
6272 case EXPOSAY_LAYOUT_INACTIVE:
6273 goto out;
6274 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
6275 state_new = exposay_set_inactive(shell);
6276 break;
6277 default:
6278 state_new = exposay_transition_inactive(shell, do_switch);
6279 break;
6280 }
6281
6282 break;
6283 }
6284
6285out:
6286 shell->exposay.state_cur = state_new;
6287}
6288
6289static void
6290exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
6291 struct weston_seat *seat)
6292{
6293 shell->exposay.state_target = state;
6294 shell->exposay.seat = seat;
6295 exposay_check_state(shell);
6296}
6297
6298static void
6299exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
6300 void *data)
6301{
6302 struct desktop_shell *shell = data;
6303
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01006304 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006305}
6306
Pekka Paalanen3c647232011-12-22 13:43:43 +02006307static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006308backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006309 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006310{
6311 struct weston_compositor *compositor = data;
6312 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006313 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006314
6315 /* TODO: we're limiting to simple use cases, where we assume just
6316 * control on the primary display. We'd have to extend later if we
6317 * ever get support for setting backlights on random desktop LCD
6318 * panels though */
6319 output = get_default_output(compositor);
6320 if (!output)
6321 return;
6322
6323 if (!output->set_backlight)
6324 return;
6325
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006326 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6327 backlight_new = output->backlight_current - 25;
6328 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6329 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006330
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006331 if (backlight_new < 5)
6332 backlight_new = 5;
6333 if (backlight_new > 255)
6334 backlight_new = 255;
6335
6336 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006337 output->set_backlight(output, output->backlight_current);
6338}
6339
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006340struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006341 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006342 struct weston_seat *seat;
6343 uint32_t key[2];
6344 int key_released[2];
6345};
6346
6347static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006348debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006349 uint32_t key, uint32_t state)
6350{
6351 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01006352 struct weston_compositor *ec = db->seat->compositor;
6353 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006354 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006355 uint32_t serial;
6356 int send = 0, terminate = 0;
6357 int check_binding = 1;
6358 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01006359 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006360
6361 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
6362 /* Do not run bindings on key releases */
6363 check_binding = 0;
6364
6365 for (i = 0; i < 2; i++)
6366 if (key == db->key[i])
6367 db->key_released[i] = 1;
6368
6369 if (db->key_released[0] && db->key_released[1]) {
6370 /* All key releases been swalled so end the grab */
6371 terminate = 1;
6372 } else if (key != db->key[0] && key != db->key[1]) {
6373 /* Should not swallow release of other keys */
6374 send = 1;
6375 }
6376 } else if (key == db->key[0] && !db->key_released[0]) {
6377 /* Do not check bindings for the first press of the binding
6378 * key. This allows it to be used as a debug shortcut.
6379 * We still need to swallow this event. */
6380 check_binding = 0;
6381 } else if (db->key[1]) {
6382 /* If we already ran a binding don't process another one since
6383 * we can't keep track of all the binding keys that were
6384 * pressed in order to swallow the release events. */
6385 send = 1;
6386 check_binding = 0;
6387 }
6388
6389 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006390 if (weston_compositor_run_debug_binding(ec, db->seat, time,
6391 key, state)) {
6392 /* We ran a binding so swallow the press and keep the
6393 * grab to swallow the released too. */
6394 send = 0;
6395 terminate = 0;
6396 db->key[1] = key;
6397 } else {
6398 /* Terminate the grab since the key pressed is not a
6399 * debug binding key. */
6400 send = 1;
6401 terminate = 1;
6402 }
6403 }
6404
6405 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01006406 serial = wl_display_next_serial(display);
6407 resource_list = &grab->keyboard->focus_resource_list;
6408 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006409 wl_keyboard_send_key(resource, serial, time, key, state);
6410 }
6411 }
6412
6413 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006414 weston_keyboard_end_grab(grab->keyboard);
6415 if (grab->keyboard->input_method_resource)
6416 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006417 free(db);
6418 }
6419}
6420
6421static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006422debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006423 uint32_t mods_depressed, uint32_t mods_latched,
6424 uint32_t mods_locked, uint32_t group)
6425{
6426 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01006427 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006428
Neil Roberts96d790e2013-09-19 17:32:00 +01006429 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006430
Neil Roberts96d790e2013-09-19 17:32:00 +01006431 wl_resource_for_each(resource, resource_list) {
6432 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
6433 mods_latched, mods_locked, group);
6434 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006435}
6436
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006437static void
6438debug_binding_cancel(struct weston_keyboard_grab *grab)
6439{
6440 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
6441
6442 weston_keyboard_end_grab(grab->keyboard);
6443 free(db);
6444}
6445
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006446struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006447 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006448 debug_binding_modifiers,
6449 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006450};
6451
6452static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006453debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006454{
6455 struct debug_binding_grab *grab;
6456
6457 grab = calloc(1, sizeof *grab);
6458 if (!grab)
6459 return;
6460
6461 grab->seat = (struct weston_seat *) seat;
6462 grab->key[0] = key;
6463 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006464 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006465}
6466
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006467static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006468force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006469 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006470{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006471 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006472 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006473 struct desktop_shell *shell = data;
6474 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006475 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006476
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006477 focus_surface = seat->keyboard->focus;
6478 if (!focus_surface)
6479 return;
6480
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006481 wl_signal_emit(&compositor->kill_signal, focus_surface);
6482
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006483 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006484 wl_client_get_credentials(client, &pid, NULL, NULL);
6485
6486 /* Skip clients that we launched ourselves (the credentials of
6487 * the socketpair is ours) */
6488 if (pid == getpid())
6489 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006490
Daniel Stone325fc2d2012-05-30 16:31:58 +01006491 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006492}
6493
6494static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006495workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006496 uint32_t key, void *data)
6497{
6498 struct desktop_shell *shell = data;
6499 unsigned int new_index = shell->workspaces.current;
6500
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006501 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006502 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006503 if (new_index != 0)
6504 new_index--;
6505
6506 change_workspace(shell, new_index);
6507}
6508
6509static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006510workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006511 uint32_t key, void *data)
6512{
6513 struct desktop_shell *shell = data;
6514 unsigned int new_index = shell->workspaces.current;
6515
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006516 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006517 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006518 if (new_index < shell->workspaces.num - 1)
6519 new_index++;
6520
6521 change_workspace(shell, new_index);
6522}
6523
6524static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006525workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006526 uint32_t key, void *data)
6527{
6528 struct desktop_shell *shell = data;
6529 unsigned int new_index;
6530
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006531 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006532 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006533 new_index = key - KEY_F1;
6534 if (new_index >= shell->workspaces.num)
6535 new_index = shell->workspaces.num - 1;
6536
6537 change_workspace(shell, new_index);
6538}
6539
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006540static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006541workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006542 uint32_t key, void *data)
6543{
6544 struct desktop_shell *shell = data;
6545 unsigned int new_index = shell->workspaces.current;
6546
6547 if (shell->locked)
6548 return;
6549
6550 if (new_index != 0)
6551 new_index--;
6552
6553 take_surface_to_workspace_by_seat(shell, seat, new_index);
6554}
6555
6556static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006557workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006558 uint32_t key, void *data)
6559{
6560 struct desktop_shell *shell = data;
6561 unsigned int new_index = shell->workspaces.current;
6562
6563 if (shell->locked)
6564 return;
6565
6566 if (new_index < shell->workspaces.num - 1)
6567 new_index++;
6568
6569 take_surface_to_workspace_by_seat(shell, seat, new_index);
6570}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006571
6572static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006573handle_output_destroy(struct wl_listener *listener, void *data)
6574{
6575 struct shell_output *output_listener =
6576 container_of(listener, struct shell_output, destroy_listener);
6577
6578 wl_list_remove(&output_listener->destroy_listener.link);
6579 wl_list_remove(&output_listener->link);
6580 free(output_listener);
6581}
6582
6583static void
6584create_shell_output(struct desktop_shell *shell,
6585 struct weston_output *output)
6586{
6587 struct shell_output *shell_output;
6588
6589 shell_output = zalloc(sizeof *shell_output);
6590 if (shell_output == NULL)
6591 return;
6592
6593 shell_output->output = output;
6594 shell_output->shell = shell;
6595 shell_output->destroy_listener.notify = handle_output_destroy;
6596 wl_signal_add(&output->destroy_signal,
6597 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006598 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006599}
6600
6601static void
6602handle_output_create(struct wl_listener *listener, void *data)
6603{
6604 struct desktop_shell *shell =
6605 container_of(listener, struct desktop_shell, output_create_listener);
6606 struct weston_output *output = (struct weston_output *)data;
6607
6608 create_shell_output(shell, output);
6609}
6610
6611static void
6612setup_output_destroy_handler(struct weston_compositor *ec,
6613 struct desktop_shell *shell)
6614{
6615 struct weston_output *output;
6616
6617 wl_list_init(&shell->output_list);
6618 wl_list_for_each(output, &ec->output_list, link)
6619 create_shell_output(shell, output);
6620
6621 shell->output_create_listener.notify = handle_output_create;
6622 wl_signal_add(&ec->output_created_signal,
6623 &shell->output_create_listener);
6624}
6625
6626static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006627shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006628{
Tiago Vignattibe143262012-04-16 17:31:41 +03006629 struct desktop_shell *shell =
6630 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006631 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006632 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006633
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006634 if (shell->child.client)
6635 wl_client_destroy(shell->child.client);
6636
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006637 wl_list_remove(&shell->idle_listener.link);
6638 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006639 wl_list_remove(&shell->show_input_panel_listener.link);
6640 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006641
Xiong Zhang6b481422013-10-23 13:58:32 +08006642 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6643 wl_list_remove(&shell_output->destroy_listener.link);
6644 wl_list_remove(&shell_output->link);
6645 free(shell_output);
6646 }
6647
6648 wl_list_remove(&shell->output_create_listener.link);
6649
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006650 wl_array_for_each(ws, &shell->workspaces.array)
6651 workspace_destroy(*ws);
6652 wl_array_release(&shell->workspaces.array);
6653
Pekka Paalanen3c647232011-12-22 13:43:43 +02006654 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006655 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006656 free(shell);
6657}
6658
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006659static void
6660shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6661{
6662 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006663 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006664
6665 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006666 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6667 MODIFIER_CTRL | MODIFIER_ALT,
6668 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01006669 weston_compositor_add_key_binding(ec, KEY_TAB,
6670 MODIFIER_ALT,
6671 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006672 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6673 click_to_activate_binding,
6674 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006675 weston_compositor_add_touch_binding(ec, 0,
6676 touch_to_activate_binding,
6677 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006678 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6679 MODIFIER_SUPER | MODIFIER_ALT,
6680 surface_opacity_binding, NULL);
6681 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6682 MODIFIER_SUPER, zoom_axis_binding,
6683 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006684
6685 /* configurable bindings */
6686 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006687 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6688 zoom_key_binding, NULL);
6689 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6690 zoom_key_binding, NULL);
6691 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6692 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006693 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006694 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6695 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006696
6697 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6698 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6699 rotate_binding, NULL);
6700
Daniel Stone325fc2d2012-05-30 16:31:58 +01006701 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6702 shell);
6703 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6704 ec);
6705 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6706 backlight_binding, ec);
6707 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6708 ec);
6709 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6710 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006711 weston_compositor_add_key_binding(ec, KEY_K, mod,
6712 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006713 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6714 workspace_up_binding, shell);
6715 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6716 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006717 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6718 workspace_move_surface_up_binding,
6719 shell);
6720 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6721 workspace_move_surface_down_binding,
6722 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006723
Daniel Stonedf8133b2013-11-19 11:37:14 +01006724 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6725
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006726 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6727 if (shell->workspaces.num > 1) {
6728 num_workspace_bindings = shell->workspaces.num;
6729 if (num_workspace_bindings > 6)
6730 num_workspace_bindings = 6;
6731 for (i = 0; i < num_workspace_bindings; i++)
6732 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6733 workspace_f_binding,
6734 shell);
6735 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006736
6737 /* Debug bindings */
6738 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6739 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006740}
6741
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006742WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006743module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006744 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006745{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006746 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006747 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006748 struct workspace **pws;
6749 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006750 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006751
Peter Huttererf3d62272013-08-08 11:57:05 +10006752 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006753 if (shell == NULL)
6754 return -1;
6755
Kristian Høgsberg75840622011-09-06 13:48:16 -04006756 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006757
6758 shell->destroy_listener.notify = shell_destroy;
6759 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006760 shell->idle_listener.notify = idle_handler;
6761 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6762 shell->wake_listener.notify = wake_handler;
6763 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006764 shell->show_input_panel_listener.notify = show_input_panels;
6765 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6766 shell->hide_input_panel_listener.notify = hide_input_panels;
6767 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006768 shell->update_input_panel_listener.notify = update_input_panels;
6769 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006770 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006771 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006772 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006773 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006774 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006775 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006776 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006777 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006778 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006779 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006780 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006781
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006782 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006783
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006784 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6785 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006786 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6787 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006788 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006789
6790 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006791 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006792
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006793 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006794
Daniel Stonedf8133b2013-11-19 11:37:14 +01006795 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6796 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6797
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006798 for (i = 0; i < shell->workspaces.num; i++) {
6799 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6800 if (pws == NULL)
6801 return -1;
6802
6803 *pws = workspace_create();
6804 if (*pws == NULL)
6805 return -1;
6806 }
6807 activate_workspace(shell, 0);
6808
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006809 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006810 wl_list_init(&shell->workspaces.animation.link);
6811 shell->workspaces.animation.frame = animate_workspace_change_frame;
6812
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006813 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006814 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006815 return -1;
6816
Rafael Antognollie2a34552013-12-03 15:35:45 -02006817 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6818 shell, bind_xdg_shell) == NULL)
6819 return -1;
6820
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006821 if (wl_global_create(ec->wl_display,
6822 &desktop_shell_interface, 2,
6823 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006824 return -1;
6825
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006826 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6827 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006828 return -1;
6829
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006830 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006831 shell, bind_input_panel) == NULL)
6832 return -1;
6833
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006834 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6835 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006836 return -1;
6837
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006838 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006839
Xiong Zhang6b481422013-10-23 13:58:32 +08006840 setup_output_destroy_handler(ec, shell);
6841
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006842 loop = wl_display_get_event_loop(ec->wl_display);
6843 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006844
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006845 shell->screensaver.timer =
6846 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6847
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006848 wl_list_for_each(seat, &ec->seat_list, link)
6849 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006850
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006851 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006852
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006853 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006854
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006855 return 0;
6856}