blob: 909228c4546df49293973d039a0d598efd177721 [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
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003965maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3966{
3967 struct weston_surface *focus = seat->pointer->focus->surface;
3968 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);
3976 if (shsurf == NULL)
3977 return;
3978
3979 if (!shell_surface_is_xdg_surface(shsurf))
3980 return;
3981
3982 if (shsurf->state.maximized)
3983 xdg_surface_send_request_unset_maximized(shsurf->resource);
3984 else
3985 xdg_surface_send_request_set_maximized(shsurf->resource);
3986}
3987
3988static void
3989fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3990{
3991 struct weston_surface *focus = seat->pointer->focus->surface;
3992 struct weston_surface *surface;
3993 struct shell_surface *shsurf;
3994
3995 surface = weston_surface_get_main_surface(focus);
3996 if (surface == NULL)
3997 return;
3998
3999 shsurf = get_shell_surface(surface);
4000 if (shsurf == NULL)
4001 return;
4002
4003 if (!shell_surface_is_xdg_surface(shsurf))
4004 return;
4005
4006 if (shsurf->state.fullscreen)
4007 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
4008 else
4009 xdg_surface_send_request_set_fullscreen(shsurf->resource);
4010}
4011
4012static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004013touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4014{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004015 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004016 struct weston_surface *surface;
4017 struct shell_surface *shsurf;
4018
4019 surface = weston_surface_get_main_surface(focus);
4020 if (surface == NULL)
4021 return;
4022
4023 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004024 if (shsurf == NULL || shsurf->state.fullscreen ||
4025 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004026 return;
4027
4028 surface_touch_move(shsurf, (struct weston_seat *) seat);
4029}
4030
4031static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004032resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004033{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004034 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004035 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004036 uint32_t edges = 0;
4037 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004038 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004039
Pekka Paalanen01388e22013-04-25 13:57:44 +03004040 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004041 if (surface == NULL)
4042 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004043
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004044 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004045 if (shsurf == NULL || shsurf->state.fullscreen ||
4046 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004047 return;
4048
Jason Ekstranda7af7042013-10-12 22:38:11 -05004049 weston_view_from_global(shsurf->view,
4050 wl_fixed_to_int(seat->pointer->grab_x),
4051 wl_fixed_to_int(seat->pointer->grab_y),
4052 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004053
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004054 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004055 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004056 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004057 edges |= 0;
4058 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004059 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004060
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004061 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004062 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004063 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004064 edges |= 0;
4065 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004066 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004067
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004068 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004069}
4070
4071static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004072surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004073 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004074{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004075 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004076 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004077 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004078 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004079
Pekka Paalanen01388e22013-04-25 13:57:44 +03004080 /* XXX: broken for windows containing sub-surfaces */
4081 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004082 if (surface == NULL)
4083 return;
4084
4085 shsurf = get_shell_surface(surface);
4086 if (!shsurf)
4087 return;
4088
Jason Ekstranda7af7042013-10-12 22:38:11 -05004089 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004090
Jason Ekstranda7af7042013-10-12 22:38:11 -05004091 if (shsurf->view->alpha > 1.0)
4092 shsurf->view->alpha = 1.0;
4093 if (shsurf->view->alpha < step)
4094 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004095
Jason Ekstranda7af7042013-10-12 22:38:11 -05004096 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004097 weston_surface_damage(surface);
4098}
4099
4100static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004101do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004102 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004103{
Daniel Stone37816df2012-05-16 18:45:18 +01004104 struct weston_seat *ws = (struct weston_seat *) seat;
4105 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004106 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004107 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004108
4109 wl_list_for_each(output, &compositor->output_list, link) {
4110 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004111 wl_fixed_to_double(seat->pointer->x),
4112 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004113 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004114 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004115 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004116 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004117 increment = -output->zoom.increment;
4118 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004119 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004120 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004121 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004122 else
4123 increment = 0;
4124
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004125 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004126
Scott Moreaue6603982012-06-11 13:07:51 -06004127 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004128 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004129 else if (output->zoom.level > output->zoom.max_level)
4130 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004131 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004132 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004133 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004134
Scott Moreaue6603982012-06-11 13:07:51 -06004135 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004136
Jason Ekstranda7af7042013-10-12 22:38:11 -05004137 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004138 }
4139 }
4140}
4141
Scott Moreauccbf29d2012-02-22 14:21:41 -07004142static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004143zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004144 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004145{
4146 do_zoom(seat, time, 0, axis, value);
4147}
4148
4149static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004150zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004151 void *data)
4152{
4153 do_zoom(seat, time, key, 0, 0);
4154}
4155
4156static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004157terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004158 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004159{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004160 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004161
Daniel Stone325fc2d2012-05-30 16:31:58 +01004162 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004163}
4164
4165static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004166lower_fullscreen_layer(struct desktop_shell *shell);
4167
4168struct alt_tab {
4169 struct desktop_shell *shell;
4170 struct weston_keyboard_grab grab;
4171
4172 struct wl_list *current;
4173
4174 struct wl_list preview_list;
4175};
4176
4177struct alt_tab_preview {
4178 struct alt_tab *alt_tab;
4179
4180 struct weston_view *view;
4181 struct weston_transform transform;
4182
4183 struct wl_listener listener;
4184
4185 struct wl_list link;
4186};
4187
4188static void
4189alt_tab_next(struct alt_tab *alt_tab)
4190{
4191 alt_tab->current = alt_tab->current->next;
4192
4193 /* Make sure we're not pointing to the list header e.g. after
4194 * cycling through the whole list. */
4195 if (alt_tab->current->next == alt_tab->preview_list.next)
4196 alt_tab->current = alt_tab->current->next;
4197}
4198
4199static void
4200alt_tab_destroy(struct alt_tab *alt_tab)
4201{
4202 struct alt_tab_preview *preview, *next;
4203 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
4204
4205 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
4206 preview = wl_container_of(alt_tab->current, preview, link);
4207
4208 activate(alt_tab->shell, preview->view->surface,
4209 (struct weston_seat *) keyboard->seat);
4210 }
4211
4212 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
4213 wl_list_remove(&preview->link);
4214 wl_list_remove(&preview->listener.link);
4215 weston_view_destroy(preview->view);
4216 free(preview);
4217 }
4218
4219 weston_keyboard_end_grab(keyboard);
4220 if (keyboard->input_method_resource)
4221 keyboard->grab = &keyboard->input_method_grab;
4222
4223 free(alt_tab);
4224}
4225
4226static void
4227alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
4228{
4229 struct alt_tab_preview *preview =
4230 container_of(listener, struct alt_tab_preview, listener);
4231 struct alt_tab *alt_tab = preview->alt_tab;
4232 int advance = 0;
4233
4234 /* If the preview that we're removing is the currently selected one,
4235 * we want to move to the next one. So we move to ->prev and then
4236 * call _next() after removing the preview. */
4237 if (alt_tab->current == &preview->link) {
4238 alt_tab->current = alt_tab->current->prev;
4239 advance = 1;
4240 }
4241
4242 wl_list_remove(&preview->listener.link);
4243 wl_list_remove(&preview->link);
4244
4245 free(preview);
4246
4247 if (advance)
4248 alt_tab_next(alt_tab);
4249
4250 /* If the last preview goes away, stop the alt-tab */
4251 if (wl_list_empty(alt_tab->current))
4252 alt_tab_destroy(alt_tab);
4253}
4254
4255static void
4256alt_tab_key(struct weston_keyboard_grab *grab,
4257 uint32_t time, uint32_t key, uint32_t state_w)
4258{
4259 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4260 enum wl_keyboard_key_state state = state_w;
4261
4262 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4263 alt_tab_next(alt_tab);
4264}
4265
4266static void
4267alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4268 uint32_t mods_depressed, uint32_t mods_latched,
4269 uint32_t mods_locked, uint32_t group)
4270{
4271 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4272 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
4273
4274 if ((seat->modifier_state & MODIFIER_ALT) == 0)
4275 alt_tab_destroy(alt_tab);
4276}
4277
4278static void
4279alt_tab_cancel(struct weston_keyboard_grab *grab)
4280{
4281 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4282
4283 alt_tab_destroy(alt_tab);
4284}
4285
4286static const struct weston_keyboard_grab_interface alt_tab_grab = {
4287 alt_tab_key,
4288 alt_tab_modifier,
4289 alt_tab_cancel,
4290};
4291
4292static int
4293view_for_alt_tab(struct weston_view *view)
4294{
Rafael Antognollied207b42013-12-03 15:35:43 -02004295 struct shell_surface *shsurf = get_shell_surface(view->surface);
4296 if (!shsurf)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004297 return 0;
4298
Rafael Antognollied207b42013-12-03 15:35:43 -02004299 if (shsurf->parent)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004300 return 0;
4301
4302 if (view != get_default_view(view->surface))
4303 return 0;
4304
4305 return 1;
4306}
4307
4308static void
4309alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4310 void *data)
4311{
4312 struct alt_tab *alt_tab;
4313 struct desktop_shell *shell = data;
4314 struct weston_output *output = get_default_output(shell->compositor);
4315 struct workspace *ws;
4316 struct weston_view *view;
4317 int num_surfaces = 0;
4318 int x, y, side, margin;
4319
4320 wl_list_for_each(view, &shell->compositor->view_list, link) {
4321
4322 if (view_for_alt_tab(view))
4323 num_surfaces++;
4324 }
4325
4326 if (!num_surfaces)
4327 return;
4328
4329 alt_tab = malloc(sizeof *alt_tab);
4330 if (!alt_tab)
4331 return;
4332
4333 alt_tab->shell = shell;
4334 wl_list_init(&alt_tab->preview_list);
4335 alt_tab->current = &alt_tab->preview_list;
4336
4337 restore_all_output_modes(shell->compositor);
4338 lower_fullscreen_layer(alt_tab->shell);
4339
4340 alt_tab->grab.interface = &alt_tab_grab;
4341 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
4342 weston_keyboard_set_focus(seat->keyboard, NULL);
4343
4344 ws = get_current_workspace(shell);
4345
4346 /* FIXME: add some visual candy e.g. prelight the selected view
4347 * and/or add a black surrounding background à la gnome-shell */
4348
4349 /* Determine the size for each preview */
4350 side = output->width / num_surfaces;
4351 if (side > 200)
4352 side = 200;
4353 margin = side / 4;
4354 side -= margin;
4355
4356 x = margin;
4357 y = (output->height - side) / 2;
4358
4359 /* Create a view for each surface */
4360 wl_list_for_each(view, &shell->compositor->view_list, link) {
4361 struct alt_tab_preview *preview;
4362 struct weston_view *v;
4363 float scale;
4364
4365 if (!view_for_alt_tab(view))
4366 continue;
4367
4368 preview = malloc(sizeof *preview);
4369 if (!preview) {
4370 alt_tab_destroy(alt_tab);
4371 return;
4372 }
4373
4374 preview->alt_tab = alt_tab;
4375
4376 preview->view = v = weston_view_create(view->surface);
4377 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004378
4379 wl_list_remove(&v->layer_link);
4380 wl_list_insert(&ws->layer.view_list, &v->layer_link);
4381 weston_view_damage_below(v);
4382 weston_surface_damage(v->surface);
4383
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004384 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004385
4386 preview->listener.notify = alt_tab_handle_surface_destroy;
4387 wl_signal_add(&v->destroy_signal, &preview->listener);
4388
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004389 if (view->surface->width > view->surface->height)
4390 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004391 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004392 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004393
4394 wl_list_insert(&v->geometry.transformation_list,
4395 &preview->transform.link);
4396 weston_matrix_init(&preview->transform.matrix);
4397 weston_matrix_scale(&preview->transform.matrix,
4398 scale, scale, 1.0f);
4399
4400 weston_view_geometry_dirty(v);
4401 weston_compositor_schedule_repaint(v->surface->compositor);
4402
4403 /* Insert at the end of the list */
4404 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
4405
4406 x += side + margin;
4407 }
4408
4409 /* Start at the second preview so a simple <alt>tab changes window.
4410 * We set `current' to the first preview and not the second because
4411 * we're going to receive a key press callback for the initial
4412 * <alt>tab which will make `current' point to the second element. */
4413 alt_tab_next(alt_tab);
4414}
4415
4416static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004417rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4418 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004419{
4420 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004421 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004422 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004423 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004424 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004425
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004426 weston_pointer_move(pointer, x, y);
4427
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004428 if (!shsurf)
4429 return;
4430
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004431 cx = 0.5f * shsurf->surface->width;
4432 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004433
Daniel Stone37816df2012-05-16 18:45:18 +01004434 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4435 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004436 r = sqrtf(dx * dx + dy * dy);
4437
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004438 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004439 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004440
4441 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004442 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004443 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004444
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004445 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004446 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004447
4448 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004449 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004450 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004451 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004452 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004453
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004454 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004455 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004456 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004457 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004458 wl_list_init(&shsurf->rotation.transform.link);
4459 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004460 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004461 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004462
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004463 /* We need to adjust the position of the surface
4464 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004465 cposx = shsurf->view->geometry.x + cx;
4466 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004467 dposx = rotate->center.x - cposx;
4468 dposy = rotate->center.y - cposy;
4469 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004470 weston_view_set_position(shsurf->view,
4471 shsurf->view->geometry.x + dposx,
4472 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004473 }
4474
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004475 /* Repaint implies weston_surface_update_transform(), which
4476 * lazily applies the damage due to rotation update.
4477 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004478 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004479}
4480
4481static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004482rotate_grab_button(struct weston_pointer_grab *grab,
4483 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004484{
4485 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004486 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004487 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004488 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004489 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004490
Daniel Stone4dbadb12012-05-30 16:31:51 +01004491 if (pointer->button_count == 0 &&
4492 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004493 if (shsurf)
4494 weston_matrix_multiply(&shsurf->rotation.rotation,
4495 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004496 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004497 free(rotate);
4498 }
4499}
4500
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004501static void
4502rotate_grab_cancel(struct weston_pointer_grab *grab)
4503{
4504 struct rotate_grab *rotate =
4505 container_of(grab, struct rotate_grab, base.grab);
4506
4507 shell_grab_end(&rotate->base);
4508 free(rotate);
4509}
4510
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004511static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004512 noop_grab_focus,
4513 rotate_grab_motion,
4514 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004515 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004516};
4517
4518static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004519surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004520{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004521 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004522 float dx, dy;
4523 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004524
Pekka Paalanen460099f2012-01-20 16:48:25 +02004525 rotate = malloc(sizeof *rotate);
4526 if (!rotate)
4527 return;
4528
Jason Ekstranda7af7042013-10-12 22:38:11 -05004529 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004530 surface->surface->width * 0.5f,
4531 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004532 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004533
Daniel Stone37816df2012-05-16 18:45:18 +01004534 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4535 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004536 r = sqrtf(dx * dx + dy * dy);
4537 if (r > 20.0f) {
4538 struct weston_matrix inverse;
4539
4540 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004541 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004542 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004543
4544 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004545 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004546 } else {
4547 weston_matrix_init(&surface->rotation.rotation);
4548 weston_matrix_init(&rotate->rotation);
4549 }
4550
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004551 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4552 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004553}
4554
4555static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004556rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004557 void *data)
4558{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004559 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004560 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004561 struct shell_surface *surface;
4562
Pekka Paalanen01388e22013-04-25 13:57:44 +03004563 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004564 if (base_surface == NULL)
4565 return;
4566
4567 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004568 if (surface == NULL || surface->state.fullscreen ||
4569 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004570 return;
4571
4572 surface_rotate(surface, seat);
4573}
4574
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004575/* Move all fullscreen layers down to the current workspace in a non-reversible
4576 * manner. This should be used when implementing shell-wide overlays, such as
4577 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004578static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004579lower_fullscreen_layer(struct desktop_shell *shell)
4580{
4581 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004582 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004583
4584 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004585 wl_list_for_each_reverse_safe(view, prev,
4586 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004587 layer_link) {
4588 wl_list_remove(&view->layer_link);
4589 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4590 weston_view_damage_below(view);
4591 weston_surface_damage(view->surface);
4592 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004593}
4594
4595static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004596activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004597 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004598{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004599 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004600 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004601 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004602 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004603 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004604
Pekka Paalanen01388e22013-04-25 13:57:44 +03004605 main_surface = weston_surface_get_main_surface(es);
4606
Daniel Stone37816df2012-05-16 18:45:18 +01004607 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004608
Jonas Ådahl8538b222012-08-29 22:13:03 +02004609 state = ensure_focus_state(shell, seat);
4610 if (state == NULL)
4611 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004612
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004613 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004614 state->keyboard_focus = es;
4615 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004616 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004617
Rafael Antognolli03b16592013-12-03 15:35:42 -02004618 shsurf = get_shell_surface(main_surface);
4619 if (shsurf->state.fullscreen)
4620 shell_configure_fullscreen(shsurf);
4621 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004622 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004623
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004624 if (shell->focus_animation_type != ANIMATION_NONE) {
4625 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004626 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004627 }
4628
4629 /* Update the surface’s layer. This brings it to the top of the stacking
4630 * order as appropriate. */
4631 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004632}
4633
Alex Wu21858432012-04-01 20:13:08 +08004634/* no-op func for checking black surface */
4635static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004636black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004637{
4638}
4639
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004640static bool
Alex Wu21858432012-04-01 20:13:08 +08004641is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4642{
4643 if (es->configure == black_surface_configure) {
4644 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004645 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004646 return true;
4647 }
4648 return false;
4649}
4650
Kristian Høgsberg75840622011-09-06 13:48:16 -04004651static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004652activate_binding(struct weston_seat *seat,
4653 struct desktop_shell *shell,
4654 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004655{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004656 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004657
Alex Wu9c35e6b2012-03-05 14:13:13 +08004658 if (!focus)
4659 return;
4660
Pekka Paalanen01388e22013-04-25 13:57:44 +03004661 if (is_black_surface(focus, &main_surface))
4662 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004663
Pekka Paalanen01388e22013-04-25 13:57:44 +03004664 main_surface = weston_surface_get_main_surface(focus);
4665 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004666 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004667
Neil Robertsa28c6932013-10-03 16:43:04 +01004668 activate(shell, focus, seat);
4669}
4670
4671static void
4672click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4673 void *data)
4674{
4675 if (seat->pointer->grab != &seat->pointer->default_grab)
4676 return;
4677
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004678 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004679}
4680
4681static void
4682touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4683{
4684 if (seat->touch->grab != &seat->touch->default_grab)
4685 return;
4686
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004687 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004688}
4689
4690static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004691lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004692{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004693 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004694
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004695 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004696 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004697 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004698 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004699
4700 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004701
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004702 /* Hide all surfaces by removing the fullscreen, panel and
4703 * toplevel layers. This way nothing else can show or receive
4704 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004705
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004706 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004707 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004708 if (shell->showing_input_panels)
4709 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004710 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004711 wl_list_insert(&shell->compositor->cursor_layer.link,
4712 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004713
Pekka Paalanen77346a62011-11-30 16:26:35 +02004714 launch_screensaver(shell);
4715
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004716 /* TODO: disable bindings that should not work while locked. */
4717
4718 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004719}
4720
4721static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004722unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004723{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004724 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004725 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004726 return;
4727 }
4728
4729 /* If desktop-shell client has gone away, unlock immediately. */
4730 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004731 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004732 return;
4733 }
4734
4735 if (shell->prepare_event_sent)
4736 return;
4737
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004738 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004739 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004740}
4741
4742static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004743shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004744{
4745 struct desktop_shell *shell = data;
4746
4747 shell->fade.animation = NULL;
4748
4749 switch (shell->fade.type) {
4750 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004751 weston_surface_destroy(shell->fade.view->surface);
4752 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004753 break;
4754 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004755 lock(shell);
4756 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004757 default:
4758 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004759 }
4760}
4761
Jason Ekstranda7af7042013-10-12 22:38:11 -05004762static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004763shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004764{
4765 struct weston_compositor *compositor = shell->compositor;
4766 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004767 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004768
4769 surface = weston_surface_create(compositor);
4770 if (!surface)
4771 return NULL;
4772
Jason Ekstranda7af7042013-10-12 22:38:11 -05004773 view = weston_view_create(surface);
4774 if (!view) {
4775 weston_surface_destroy(surface);
4776 return NULL;
4777 }
4778
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004779 surface->width = 8192;
4780 surface->height = 8192;
4781 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004782 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004783 wl_list_insert(&compositor->fade_layer.view_list,
4784 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004785 pixman_region32_init(&surface->input);
4786
Jason Ekstranda7af7042013-10-12 22:38:11 -05004787 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004788}
4789
4790static void
4791shell_fade(struct desktop_shell *shell, enum fade_type type)
4792{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004793 float tint;
4794
4795 switch (type) {
4796 case FADE_IN:
4797 tint = 0.0;
4798 break;
4799 case FADE_OUT:
4800 tint = 1.0;
4801 break;
4802 default:
4803 weston_log("shell: invalid fade type\n");
4804 return;
4805 }
4806
4807 shell->fade.type = type;
4808
Jason Ekstranda7af7042013-10-12 22:38:11 -05004809 if (shell->fade.view == NULL) {
4810 shell->fade.view = shell_fade_create_surface(shell);
4811 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004812 return;
4813
Jason Ekstranda7af7042013-10-12 22:38:11 -05004814 shell->fade.view->alpha = 1.0 - tint;
4815 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004816 }
4817
4818 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004819 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004820 else
4821 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004822 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004823 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004824 shell_fade_done, shell);
4825}
4826
4827static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004828do_shell_fade_startup(void *data)
4829{
4830 struct desktop_shell *shell = data;
4831
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004832 if (shell->startup_animation_type == ANIMATION_FADE)
4833 shell_fade(shell, FADE_IN);
4834 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 weston_surface_destroy(shell->fade.view->surface);
4836 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004837 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004838}
4839
4840static void
4841shell_fade_startup(struct desktop_shell *shell)
4842{
4843 struct wl_event_loop *loop;
4844
4845 if (!shell->fade.startup_timer)
4846 return;
4847
4848 wl_event_source_remove(shell->fade.startup_timer);
4849 shell->fade.startup_timer = NULL;
4850
4851 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4852 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4853}
4854
4855static int
4856fade_startup_timeout(void *data)
4857{
4858 struct desktop_shell *shell = data;
4859
4860 shell_fade_startup(shell);
4861 return 0;
4862}
4863
4864static void
4865shell_fade_init(struct desktop_shell *shell)
4866{
4867 /* Make compositor output all black, and wait for the desktop-shell
4868 * client to signal it is ready, then fade in. The timer triggers a
4869 * fade-in, in case the desktop-shell client takes too long.
4870 */
4871
4872 struct wl_event_loop *loop;
4873
Jason Ekstranda7af7042013-10-12 22:38:11 -05004874 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004875 weston_log("%s: warning: fade surface already exists\n",
4876 __func__);
4877 return;
4878 }
4879
Jason Ekstranda7af7042013-10-12 22:38:11 -05004880 shell->fade.view = shell_fade_create_surface(shell);
4881 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004882 return;
4883
Jason Ekstranda7af7042013-10-12 22:38:11 -05004884 weston_view_update_transform(shell->fade.view);
4885 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004886
4887 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4888 shell->fade.startup_timer =
4889 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4890 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4891}
4892
4893static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004894idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004895{
4896 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004897 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004898
4899 shell_fade(shell, FADE_OUT);
4900 /* lock() is called from shell_fade_done() */
4901}
4902
4903static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004904wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004905{
4906 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004907 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004908
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004909 unlock(shell);
4910}
4911
4912static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004913show_input_panels(struct wl_listener *listener, void *data)
4914{
4915 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004916 container_of(listener, struct desktop_shell,
4917 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004918 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004919
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004920 shell->text_input.surface = (struct weston_surface*)data;
4921
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004922 if (shell->showing_input_panels)
4923 return;
4924
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004925 shell->showing_input_panels = true;
4926
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004927 if (!shell->locked)
4928 wl_list_insert(&shell->panel_layer.link,
4929 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004930
Jason Ekstranda7af7042013-10-12 22:38:11 -05004931 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004932 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004933 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004934 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004935 wl_list_insert(&shell->input_panel_layer.view_list,
4936 &ipsurf->view->layer_link);
4937 weston_view_geometry_dirty(ipsurf->view);
4938 weston_view_update_transform(ipsurf->view);
4939 weston_surface_damage(ipsurf->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004940 weston_slide_run(ipsurf->view, ipsurf->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004941 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004942 }
4943}
4944
4945static void
4946hide_input_panels(struct wl_listener *listener, void *data)
4947{
4948 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004949 container_of(listener, struct desktop_shell,
4950 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004951 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004952
Jan Arne Petersen61381972013-01-31 15:52:21 +01004953 if (!shell->showing_input_panels)
4954 return;
4955
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004956 shell->showing_input_panels = false;
4957
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004958 if (!shell->locked)
4959 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004960
Jason Ekstranda7af7042013-10-12 22:38:11 -05004961 wl_list_for_each_safe(view, next,
4962 &shell->input_panel_layer.view_list, layer_link)
4963 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004964}
4965
4966static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004967update_input_panels(struct wl_listener *listener, void *data)
4968{
4969 struct desktop_shell *shell =
4970 container_of(listener, struct desktop_shell,
4971 update_input_panel_listener);
4972
4973 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4974}
4975
4976static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004977center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004978{
Giulio Camuffob8366642013-04-25 13:57:46 +03004979 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004980 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004981
Jason Ekstranda7af7042013-10-12 22:38:11 -05004982 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004983
4984 x = output->x + (output->width - width) / 2 - surf_x / 2;
4985 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004986
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004987 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004988}
4989
4990static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004991weston_view_set_initial_position(struct weston_view *view,
4992 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004993{
4994 struct weston_compositor *compositor = shell->compositor;
4995 int ix = 0, iy = 0;
4996 int range_x, range_y;
4997 int dx, dy, x, y, panel_height;
4998 struct weston_output *output, *target_output = NULL;
4999 struct weston_seat *seat;
5000
5001 /* As a heuristic place the new window on the same output as the
5002 * pointer. Falling back to the output containing 0, 0.
5003 *
5004 * TODO: Do something clever for touch too?
5005 */
5006 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005007 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005008 ix = wl_fixed_to_int(seat->pointer->x);
5009 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005010 break;
5011 }
5012 }
5013
5014 wl_list_for_each(output, &compositor->output_list, link) {
5015 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5016 target_output = output;
5017 break;
5018 }
5019 }
5020
5021 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005022 weston_view_set_position(view, 10 + random() % 400,
5023 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005024 return;
5025 }
5026
5027 /* Valid range within output where the surface will still be onscreen.
5028 * If this is negative it means that the surface is bigger than
5029 * output.
5030 */
5031 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005032 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005033 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005034 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005035
Rob Bradford4cb88c72012-08-13 15:18:44 +01005036 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005037 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005038 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01005039 dx = 0;
5040
5041 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005042 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005043 else
5044 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005045
5046 x = target_output->x + dx;
5047 y = target_output->y + dy;
5048
Jason Ekstranda7af7042013-10-12 22:38:11 -05005049 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005050}
5051
5052static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005053map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005054 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005055{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005056 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005057 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08005058 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03005059 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005060
Pekka Paalanen77346a62011-11-30 16:26:35 +02005061 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005062 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005063 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005064 if (shsurf->state.fullscreen) {
5065 center_on_output(shsurf->view, shsurf->fullscreen_output);
5066 shell_map_fullscreen(shsurf);
5067 } else if (shsurf->state.maximized) {
5068 /* use surface configure to set the geometry */
5069 panel_height = get_output_panel_height(shell, shsurf->output);
5070 surface_subsurfaces_boundingbox(shsurf->surface,
5071 &surf_x, &surf_y, NULL, NULL);
5072 weston_view_set_position(shsurf->view,
5073 shsurf->output->x - surf_x,
5074 shsurf->output->y +
5075 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02005076 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005077 weston_view_set_initial_position(shsurf->view, shell);
5078 }
Juan Zhao96879df2012-02-07 08:45:41 +08005079 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005080 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005081 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005082 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005083 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005084 weston_view_set_position(shsurf->view,
5085 shsurf->view->geometry.x + sx,
5086 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005087 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005088 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005089 default:
5090 ;
5091 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005092
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005093 /* Surface stacking order, see also activate(). */
5094 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005095
Jason Ekstranda7af7042013-10-12 22:38:11 -05005096 if (shsurf->type != SHELL_SURFACE_NONE) {
5097 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005098 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005099 shsurf->surface->output = shsurf->output;
5100 shsurf->view->output = shsurf->output;
5101 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005102 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005103
Rafael Antognollied207b42013-12-03 15:35:43 -02005104 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
5105 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
5106 }
5107
Jason Ekstranda7af7042013-10-12 22:38:11 -05005108 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005109 /* XXX: xwayland's using the same fields for transient type */
5110 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005111 if (shsurf->transient.flags ==
5112 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5113 break;
5114 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005115 if (shsurf->state.relative &&
5116 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5117 break;
5118 if (!shell->locked)
5119 break;
5120 wl_list_for_each(seat, &compositor->seat_list, link)
5121 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005122 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005123 case SHELL_SURFACE_POPUP:
5124 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005125 default:
5126 break;
5127 }
5128
Rafael Antognolli03b16592013-12-03 15:35:42 -02005129 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5130 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005131 {
5132 switch (shell->win_animation_type) {
5133 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005134 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005135 break;
5136 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005137 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005138 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005139 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005140 default:
5141 break;
5142 }
5143 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005144}
5145
5146static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005147configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005148 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005149{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005150 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005151 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005152 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005153
Pekka Paalanen77346a62011-11-30 16:26:35 +02005154 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005155
Rafael Antognolli03b16592013-12-03 15:35:42 -02005156 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005157 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005158 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005159 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005160 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5161 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005162 mx = shsurf->output->x - surf_x;
5163 my = shsurf->output->y +
5164 get_output_panel_height(shell,shsurf->output) - surf_y;
5165 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005166 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005167 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005168 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005169
Alex Wu4539b082012-03-01 12:57:46 +08005170 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005171 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005172 wl_list_for_each(view, &surface->views, surface_link)
5173 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005174
Rafael Antognolli03b16592013-12-03 15:35:42 -02005175 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005176 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005177 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005178}
5179
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005180static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005181shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005182{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005183 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03005184 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01005185
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005186 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005187
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005188 if (!weston_surface_is_mapped(es) &&
5189 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005190 remove_popup_grab(shsurf);
5191 }
5192
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005193 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005194 return;
5195
Rafael Antognolli03b16592013-12-03 15:35:42 -02005196 if ((shsurf->next_type != SHELL_SURFACE_NONE &&
5197 shsurf->type != shsurf->next_type) ||
5198 shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005199 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005200 type_changed = 1;
5201 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005202
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005203 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005204 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005205 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005206 shsurf->last_width != es->width ||
5207 shsurf->last_height != es->height) {
5208 shsurf->last_width = es->width;
5209 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005210 float from_x, from_y;
5211 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005212
Jason Ekstranda7af7042013-10-12 22:38:11 -05005213 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5214 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005215 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005216 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005217 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005218 }
5219}
5220
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005221static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005222
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005223static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005224desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005225{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005226 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005227 struct desktop_shell *shell =
5228 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005229
5230 shell->child.process.pid = 0;
5231 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005232
5233 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5234 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005235 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005236 shell->child.deathstamp = time;
5237 shell->child.deathcount = 0;
5238 }
5239
5240 shell->child.deathcount++;
5241 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005242 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005243 return;
5244 }
5245
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005246 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005247 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005248 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005249}
5250
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005251static void
5252launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005253{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005254 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005255
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005256 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005257 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005258 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005259 desktop_shell_sigchld);
5260
5261 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005262 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005263}
5264
5265static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005266bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5267{
Tiago Vignattibe143262012-04-16 17:31:41 +03005268 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005269 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005270
Jason Ekstranda85118c2013-06-27 20:17:02 -05005271 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
5272 if (resource)
5273 wl_resource_set_implementation(resource, &shell_implementation,
5274 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005275}
5276
Kristian Høgsberg75840622011-09-06 13:48:16 -04005277static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02005278bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5279{
5280 struct desktop_shell *shell = data;
5281 struct wl_resource *resource;
5282
5283 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
5284 if (resource)
5285 wl_resource_set_dispatcher(resource,
5286 xdg_shell_unversioned_dispatch,
5287 NULL, shell, NULL);
5288}
5289
5290static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005291unbind_desktop_shell(struct wl_resource *resource)
5292{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005293 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005294
5295 if (shell->locked)
5296 resume_desktop(shell);
5297
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005298 shell->child.desktop_shell = NULL;
5299 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005300}
5301
5302static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005303bind_desktop_shell(struct wl_client *client,
5304 void *data, uint32_t version, uint32_t id)
5305{
Tiago Vignattibe143262012-04-16 17:31:41 +03005306 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005307 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005308
Jason Ekstranda85118c2013-06-27 20:17:02 -05005309 resource = wl_resource_create(client, &desktop_shell_interface,
5310 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005311
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005312 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005313 wl_resource_set_implementation(resource,
5314 &desktop_shell_implementation,
5315 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005316 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005317
5318 if (version < 2)
5319 shell_fade_startup(shell);
5320
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005321 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005322 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005323
5324 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5325 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005326 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005327}
5328
Pekka Paalanen6e168112011-11-24 11:34:05 +02005329static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005330screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005331{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005332 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005333 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005334
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005335 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005336 return;
5337
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005338 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005339 if (!shell->locked)
5340 return;
5341
Jason Ekstranda7af7042013-10-12 22:38:11 -05005342 view = container_of(surface->views.next, struct weston_view, surface_link);
5343 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005344
Jason Ekstranda7af7042013-10-12 22:38:11 -05005345 if (wl_list_empty(&view->layer_link)) {
5346 wl_list_insert(shell->lock_layer.view_list.prev,
5347 &view->layer_link);
5348 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005349 wl_event_source_timer_update(shell->screensaver.timer,
5350 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005351 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005352 }
5353}
5354
5355static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005356screensaver_set_surface(struct wl_client *client,
5357 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005358 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005359 struct wl_resource *output_resource)
5360{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005361 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005362 struct weston_surface *surface =
5363 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005364 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005365 struct weston_view *view, *next;
5366
5367 /* Make sure we only have one view */
5368 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5369 weston_view_destroy(view);
5370 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005371
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005372 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005373 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005374 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005375}
5376
5377static const struct screensaver_interface screensaver_implementation = {
5378 screensaver_set_surface
5379};
5380
5381static void
5382unbind_screensaver(struct wl_resource *resource)
5383{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005384 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005385
Pekka Paalanen77346a62011-11-30 16:26:35 +02005386 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005387}
5388
5389static void
5390bind_screensaver(struct wl_client *client,
5391 void *data, uint32_t version, uint32_t id)
5392{
Tiago Vignattibe143262012-04-16 17:31:41 +03005393 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005394 struct wl_resource *resource;
5395
Jason Ekstranda85118c2013-06-27 20:17:02 -05005396 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005397
Pekka Paalanen77346a62011-11-30 16:26:35 +02005398 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005399 wl_resource_set_implementation(resource,
5400 &screensaver_implementation,
5401 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005402 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005403 return;
5404 }
5405
5406 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5407 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005408 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005409}
5410
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005411static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005412input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005413{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005414 struct input_panel_surface *ip_surface = surface->configure_private;
5415 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005416 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005417 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005418
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005419 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005420 return;
5421
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005422 if (!weston_surface_is_mapped(surface)) {
5423 if (!shell->showing_input_panels)
5424 return;
5425
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005426 show_surface = 1;
5427 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005428
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005429 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005430
5431 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005432 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
5433 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005434 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005435 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
5436 y = ip_surface->output->y + ip_surface->output->height - surface->height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005437 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005438
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005439 weston_view_set_position(ip_surface->view, x, y);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005440
5441 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005442 wl_list_insert(&shell->input_panel_layer.view_list,
5443 &ip_surface->view->layer_link);
5444 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005445 weston_surface_damage(surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005446 weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005447 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005448}
5449
5450static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005451destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005452{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005453 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
5454
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005455 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005456 wl_list_remove(&input_panel_surface->link);
5457
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005458 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005459 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005460
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005461 free(input_panel_surface);
5462}
5463
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005464static struct input_panel_surface *
5465get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005466{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005467 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005468 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005469 } else {
5470 return NULL;
5471 }
5472}
5473
5474static void
5475input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
5476{
5477 struct input_panel_surface *ipsurface = container_of(listener,
5478 struct input_panel_surface,
5479 surface_destroy_listener);
5480
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005481 if (ipsurface->resource) {
5482 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005483 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005484 destroy_input_panel_surface(ipsurface);
5485 }
5486}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005487
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005488static struct input_panel_surface *
5489create_input_panel_surface(struct desktop_shell *shell,
5490 struct weston_surface *surface)
5491{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005492 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005493
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005494 input_panel_surface = calloc(1, sizeof *input_panel_surface);
5495 if (!input_panel_surface)
5496 return NULL;
5497
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005498 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005499 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005500
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005501 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005502
5503 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005504 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005505
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005506 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005507 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005508 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005509 &input_panel_surface->surface_destroy_listener);
5510
5511 wl_list_init(&input_panel_surface->link);
5512
5513 return input_panel_surface;
5514}
5515
5516static void
5517input_panel_surface_set_toplevel(struct wl_client *client,
5518 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005519 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005520 uint32_t position)
5521{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005522 struct input_panel_surface *input_panel_surface =
5523 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005524 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005525
5526 wl_list_insert(&shell->input_panel.surfaces,
5527 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005528
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005529 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005530 input_panel_surface->panel = 0;
5531}
5532
5533static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02005534input_panel_surface_set_overlay_panel(struct wl_client *client,
5535 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005536{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005537 struct input_panel_surface *input_panel_surface =
5538 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005539 struct desktop_shell *shell = input_panel_surface->shell;
5540
5541 wl_list_insert(&shell->input_panel.surfaces,
5542 &input_panel_surface->link);
5543
5544 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005545}
5546
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005547static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005548 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02005549 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005550};
5551
5552static void
5553destroy_input_panel_surface_resource(struct wl_resource *resource)
5554{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005555 struct input_panel_surface *ipsurf =
5556 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005557
5558 destroy_input_panel_surface(ipsurf);
5559}
5560
5561static void
5562input_panel_get_input_panel_surface(struct wl_client *client,
5563 struct wl_resource *resource,
5564 uint32_t id,
5565 struct wl_resource *surface_resource)
5566{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005567 struct weston_surface *surface =
5568 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005569 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005570 struct input_panel_surface *ipsurf;
5571
5572 if (get_input_panel_surface(surface)) {
5573 wl_resource_post_error(surface_resource,
5574 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005575 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005576 return;
5577 }
5578
5579 ipsurf = create_input_panel_surface(shell, surface);
5580 if (!ipsurf) {
5581 wl_resource_post_error(surface_resource,
5582 WL_DISPLAY_ERROR_INVALID_OBJECT,
5583 "surface->configure already set");
5584 return;
5585 }
5586
Jason Ekstranda85118c2013-06-27 20:17:02 -05005587 ipsurf->resource =
5588 wl_resource_create(client,
5589 &wl_input_panel_surface_interface, 1, id);
5590 wl_resource_set_implementation(ipsurf->resource,
5591 &input_panel_surface_implementation,
5592 ipsurf,
5593 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005594}
5595
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005596static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005597 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005598};
5599
5600static void
5601unbind_input_panel(struct wl_resource *resource)
5602{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005603 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005604
5605 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005606}
5607
5608static void
5609bind_input_panel(struct wl_client *client,
5610 void *data, uint32_t version, uint32_t id)
5611{
5612 struct desktop_shell *shell = data;
5613 struct wl_resource *resource;
5614
Jason Ekstranda85118c2013-06-27 20:17:02 -05005615 resource = wl_resource_create(client,
5616 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005617
5618 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005619 wl_resource_set_implementation(resource,
5620 &input_panel_implementation,
5621 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005622 shell->input_panel.binding = resource;
5623 return;
5624 }
5625
5626 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5627 "interface object already bound");
5628 wl_resource_destroy(resource);
5629}
5630
Kristian Høgsberg07045392012-02-19 18:52:44 -05005631struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005632 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005633 struct weston_surface *current;
5634 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005635 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005636};
5637
5638static void
5639switcher_next(struct switcher *switcher)
5640{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005641 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005642 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005643 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005644 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005645
Jason Ekstranda7af7042013-10-12 22:38:11 -05005646 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005647 shsurf = get_shell_surface(view->surface);
5648 switch (shsurf->type) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005649 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005650 if (shsurf->parent)
5651 break;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005652 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005653 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005654 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005655 next = view->surface;
5656 prev = view->surface;
5657 view->alpha = 0.25;
5658 weston_view_geometry_dirty(view);
5659 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005660 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005661 case SHELL_SURFACE_POPUP:
5662 case SHELL_SURFACE_XWAYLAND:
5663 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005664 default:
5665 break;
5666 }
Alex Wu1659daa2012-04-01 20:13:09 +08005667
Jason Ekstranda7af7042013-10-12 22:38:11 -05005668 if (is_black_surface(view->surface, NULL)) {
5669 view->alpha = 0.25;
5670 weston_view_geometry_dirty(view);
5671 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005672 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005673 }
5674
5675 if (next == NULL)
5676 next = first;
5677
Alex Wu07b26062012-03-12 16:06:01 +08005678 if (next == NULL)
5679 return;
5680
Kristian Høgsberg07045392012-02-19 18:52:44 -05005681 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005682 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005683
5684 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005685 wl_list_for_each(view, &next->views, surface_link)
5686 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005687
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005688 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005689 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005690 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005691}
5692
5693static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005694switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005695{
5696 struct switcher *switcher =
5697 container_of(listener, struct switcher, listener);
5698
5699 switcher_next(switcher);
5700}
5701
5702static void
Daniel Stone351eb612012-05-31 15:27:47 -04005703switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005704{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005705 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005706 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005707 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005708
Jason Ekstranda7af7042013-10-12 22:38:11 -05005709 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005710 if (is_focus_view(view))
5711 continue;
5712
Jason Ekstranda7af7042013-10-12 22:38:11 -05005713 view->alpha = 1.0;
5714 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005715 }
5716
Alex Wu07b26062012-03-12 16:06:01 +08005717 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005718 activate(switcher->shell, switcher->current,
5719 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005720 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005721 weston_keyboard_end_grab(keyboard);
5722 if (keyboard->input_method_resource)
5723 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005724 free(switcher);
5725}
5726
5727static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005728switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005729 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005730{
5731 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005732 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005733
Daniel Stonec9785ea2012-05-30 16:31:52 +01005734 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005735 switcher_next(switcher);
5736}
5737
5738static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005739switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005740 uint32_t mods_depressed, uint32_t mods_latched,
5741 uint32_t mods_locked, uint32_t group)
5742{
5743 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005744 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005745
Daniel Stone351eb612012-05-31 15:27:47 -04005746 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5747 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005748}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005749
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005750static void
5751switcher_cancel(struct weston_keyboard_grab *grab)
5752{
5753 struct switcher *switcher = container_of(grab, struct switcher, grab);
5754
5755 switcher_destroy(switcher);
5756}
5757
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005758static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005759 switcher_key,
5760 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005761 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005762};
5763
5764static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005765switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005766 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005767{
Tiago Vignattibe143262012-04-16 17:31:41 +03005768 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005769 struct switcher *switcher;
5770
5771 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005772 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005773 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005774 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005775 wl_list_init(&switcher->listener.link);
5776
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005777 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005778 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005779 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005780 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5781 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005782 switcher_next(switcher);
5783}
5784
Daniel Stonedf8133b2013-11-19 11:37:14 +01005785struct exposay_surface {
5786 struct desktop_shell *shell;
5787 struct weston_surface *surface;
5788 struct weston_view *view;
5789 struct wl_list link;
5790
5791 int x;
5792 int y;
5793 int width;
5794 int height;
5795 double scale;
5796
5797 int row;
5798 int column;
5799
5800 /* The animations only apply a transformation for their own lifetime,
5801 * and don't have an option to indefinitely maintain the
5802 * transformation in a steady state - so, we apply our own once the
5803 * animation has finished. */
5804 struct weston_transform transform;
5805};
5806
5807static void exposay_set_state(struct desktop_shell *shell,
5808 enum exposay_target_state state,
5809 struct weston_seat *seat);
5810static void exposay_check_state(struct desktop_shell *shell);
5811
5812static void
5813exposay_in_flight_inc(struct desktop_shell *shell)
5814{
5815 shell->exposay.in_flight++;
5816}
5817
5818static void
5819exposay_in_flight_dec(struct desktop_shell *shell)
5820{
5821 if (--shell->exposay.in_flight > 0)
5822 return;
5823
5824 exposay_check_state(shell);
5825}
5826
5827static void
5828exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5829{
5830 struct exposay_surface *esurface = data;
5831
5832 wl_list_insert(&esurface->view->geometry.transformation_list,
5833 &esurface->transform.link);
5834 weston_matrix_init(&esurface->transform.matrix);
5835 weston_matrix_scale(&esurface->transform.matrix,
5836 esurface->scale, esurface->scale, 1.0f);
5837 weston_matrix_translate(&esurface->transform.matrix,
5838 esurface->x - esurface->view->geometry.x,
5839 esurface->y - esurface->view->geometry.y,
5840 0);
5841
5842 weston_view_geometry_dirty(esurface->view);
5843 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5844
5845 exposay_in_flight_dec(esurface->shell);
5846}
5847
5848static void
5849exposay_animate_in(struct exposay_surface *esurface)
5850{
5851 exposay_in_flight_inc(esurface->shell);
5852
5853 weston_move_scale_run(esurface->view,
5854 esurface->x - esurface->view->geometry.x,
5855 esurface->y - esurface->view->geometry.y,
5856 1.0, esurface->scale, 0,
5857 exposay_animate_in_done, esurface);
5858}
5859
5860static void
5861exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5862{
5863 struct exposay_surface *esurface = data;
5864 struct desktop_shell *shell = esurface->shell;
5865
5866 wl_list_remove(&esurface->link);
5867 free(esurface);
5868
5869 exposay_in_flight_dec(shell);
5870}
5871
5872static void
5873exposay_animate_out(struct exposay_surface *esurface)
5874{
5875 exposay_in_flight_inc(esurface->shell);
5876
5877 /* Remove the static transformation set up by
5878 * exposay_transform_in_done(). */
5879 wl_list_remove(&esurface->transform.link);
5880 weston_view_geometry_dirty(esurface->view);
5881
5882 weston_move_scale_run(esurface->view,
5883 esurface->x - esurface->view->geometry.x,
5884 esurface->y - esurface->view->geometry.y,
5885 1.0, esurface->scale, 1,
5886 exposay_animate_out_done, esurface);
5887}
5888
5889static void
5890exposay_highlight_surface(struct desktop_shell *shell,
5891 struct exposay_surface *esurface)
5892{
5893 struct weston_view *view = NULL;
5894
5895 if (esurface) {
5896 shell->exposay.row_current = esurface->row;
5897 shell->exposay.column_current = esurface->column;
5898 view = esurface->view;
5899 }
5900
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005901 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005902 shell->exposay.focus_current = view;
5903}
5904
5905static int
5906exposay_is_animating(struct desktop_shell *shell)
5907{
5908 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5909 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5910 return 0;
5911
5912 return (shell->exposay.in_flight > 0);
5913}
5914
5915static void
5916exposay_pick(struct desktop_shell *shell, int x, int y)
5917{
5918 struct exposay_surface *esurface;
5919
5920 if (exposay_is_animating(shell))
5921 return;
5922
5923 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5924 if (x < esurface->x || x > esurface->x + esurface->width)
5925 continue;
5926 if (y < esurface->y || y > esurface->y + esurface->height)
5927 continue;
5928
5929 exposay_highlight_surface(shell, esurface);
5930 return;
5931 }
5932}
5933
5934/* Pretty lame layout for now; just tries to make a square. Should take
5935 * aspect ratio into account really. Also needs to be notified of surface
5936 * addition and removal and adjust layout/animate accordingly. */
5937static enum exposay_layout_state
5938exposay_layout(struct desktop_shell *shell)
5939{
5940 struct workspace *workspace = shell->exposay.workspace;
5941 struct weston_compositor *compositor = shell->compositor;
5942 struct weston_output *output = get_default_output(compositor);
5943 struct weston_view *view;
5944 struct exposay_surface *esurface;
5945 int w, h;
5946 int i;
5947 int last_row_removed = 0;
5948
5949 wl_list_init(&shell->exposay.surface_list);
5950
5951 shell->exposay.num_surfaces = 0;
5952 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5953 if (!get_shell_surface(view->surface))
5954 continue;
5955 shell->exposay.num_surfaces++;
5956 }
5957
5958 if (shell->exposay.num_surfaces == 0) {
5959 shell->exposay.grid_size = 0;
5960 shell->exposay.hpadding_outer = 0;
5961 shell->exposay.vpadding_outer = 0;
5962 shell->exposay.padding_inner = 0;
5963 shell->exposay.surface_size = 0;
5964 return EXPOSAY_LAYOUT_OVERVIEW;
5965 }
5966
5967 /* Lay the grid out as square as possible, losing surfaces from the
5968 * bottom row if required. Start with fixed padding of a 10% margin
5969 * around the outside and 80px internal padding between surfaces, and
5970 * maximise the area made available to surfaces after this, but only
5971 * to a maximum of 1/3rd the total output size.
5972 *
5973 * If we can't make a square grid, add one extra row at the bottom
5974 * which will have a smaller number of columns.
5975 *
5976 * XXX: Surely there has to be a better way to express this maths,
5977 * right?!
5978 */
5979 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5980 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5981 shell->exposay.grid_size++;
5982 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5983
5984 shell->exposay.hpadding_outer = (output->width / 10);
5985 shell->exposay.vpadding_outer = (output->height / 10);
5986 shell->exposay.padding_inner = 80;
5987
5988 w = output->width - (shell->exposay.hpadding_outer * 2);
5989 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5990 w /= shell->exposay.grid_size;
5991
5992 h = output->height - (shell->exposay.vpadding_outer * 2);
5993 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5994 h /= shell->exposay.grid_size;
5995
5996 shell->exposay.surface_size = (w < h) ? w : h;
5997 if (shell->exposay.surface_size > (output->width / 2))
5998 shell->exposay.surface_size = output->width / 2;
5999 if (shell->exposay.surface_size > (output->height / 2))
6000 shell->exposay.surface_size = output->height / 2;
6001
6002 i = 0;
6003 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
6004 int pad;
6005
6006 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
6007
6008 if (!get_shell_surface(view->surface))
6009 continue;
6010
6011 esurface = malloc(sizeof(*esurface));
6012 if (!esurface) {
6013 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
6014 shell->exposay.seat);
6015 break;
6016 }
6017
6018 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
6019 esurface->shell = shell;
6020 esurface->view = view;
6021
6022 esurface->row = i / shell->exposay.grid_size;
6023 esurface->column = i % shell->exposay.grid_size;
6024
6025 esurface->x = shell->exposay.hpadding_outer;
6026 esurface->x += pad * esurface->column;
6027 esurface->y = shell->exposay.vpadding_outer;
6028 esurface->y += pad * esurface->row;
6029
6030 if (esurface->row == shell->exposay.grid_size - 1)
6031 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
6032
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06006033 if (view->surface->width > view->surface->height)
6034 esurface->scale = shell->exposay.surface_size / (float) view->surface->width;
Daniel Stonedf8133b2013-11-19 11:37:14 +01006035 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06006036 esurface->scale = shell->exposay.surface_size / (float) view->surface->height;
6037 esurface->width = view->surface->width * esurface->scale;
6038 esurface->height = view->surface->height * esurface->scale;
Daniel Stonedf8133b2013-11-19 11:37:14 +01006039
6040 if (shell->exposay.focus_current == esurface->view)
6041 exposay_highlight_surface(shell, esurface);
6042
6043 exposay_animate_in(esurface);
6044
6045 i++;
6046 }
6047
6048 weston_compositor_schedule_repaint(shell->compositor);
6049
6050 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
6051}
6052
6053static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01006054exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
6055 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01006056{
6057 struct desktop_shell *shell =
6058 container_of(grab, struct desktop_shell, exposay.grab_ptr);
6059
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01006060 weston_pointer_move(grab->pointer, x, y);
6061
Daniel Stonedf8133b2013-11-19 11:37:14 +01006062 exposay_pick(shell,
6063 wl_fixed_to_int(grab->pointer->x),
6064 wl_fixed_to_int(grab->pointer->y));
6065}
6066
6067static void
6068exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
6069 uint32_t state_w)
6070{
6071 struct desktop_shell *shell =
6072 container_of(grab, struct desktop_shell, exposay.grab_ptr);
6073 struct weston_seat *seat = grab->pointer->seat;
6074 enum wl_pointer_button_state state = state_w;
6075
6076 if (button != BTN_LEFT)
6077 return;
6078
6079 /* Store the surface we clicked on, and don't do anything if we end up
6080 * releasing on a different surface. */
6081 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
6082 shell->exposay.clicked = shell->exposay.focus_current;
6083 return;
6084 }
6085
6086 if (shell->exposay.focus_current == shell->exposay.clicked)
6087 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
6088 else
6089 shell->exposay.clicked = NULL;
6090}
6091
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01006092static void
6093exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
6094{
6095 struct desktop_shell *shell =
6096 container_of(grab, struct desktop_shell, exposay.grab_ptr);
6097
6098 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
6099}
6100
Daniel Stonedf8133b2013-11-19 11:37:14 +01006101static const struct weston_pointer_grab_interface exposay_ptr_grab = {
6102 noop_grab_focus,
6103 exposay_motion,
6104 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01006105 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01006106};
6107
6108static int
6109exposay_maybe_move(struct desktop_shell *shell, int row, int column)
6110{
6111 struct exposay_surface *esurface;
6112
6113 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
6114 if (esurface->row != row || esurface->column != column)
6115 continue;
6116
6117 exposay_highlight_surface(shell, esurface);
6118 return 1;
6119 }
6120
6121 return 0;
6122}
6123
6124static void
6125exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
6126 uint32_t state_w)
6127{
6128 struct weston_seat *seat = grab->keyboard->seat;
6129 struct desktop_shell *shell =
6130 container_of(grab, struct desktop_shell, exposay.grab_kbd);
6131 enum wl_keyboard_key_state state = state_w;
6132
6133 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
6134 return;
6135
6136 switch (key) {
6137 case KEY_ESC:
6138 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
6139 break;
6140 case KEY_ENTER:
6141 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
6142 break;
6143 case KEY_UP:
6144 exposay_maybe_move(shell, shell->exposay.row_current - 1,
6145 shell->exposay.column_current);
6146 break;
6147 case KEY_DOWN:
6148 /* Special case for trying to move to the bottom row when it
6149 * has fewer items than all the others. */
6150 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
6151 shell->exposay.column_current) &&
6152 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
6153 exposay_maybe_move(shell, shell->exposay.row_current + 1,
6154 (shell->exposay.num_surfaces %
6155 shell->exposay.grid_size) - 1);
6156 }
6157 break;
6158 case KEY_LEFT:
6159 exposay_maybe_move(shell, shell->exposay.row_current,
6160 shell->exposay.column_current - 1);
6161 break;
6162 case KEY_RIGHT:
6163 exposay_maybe_move(shell, shell->exposay.row_current,
6164 shell->exposay.column_current + 1);
6165 break;
6166 case KEY_TAB:
6167 /* Try to move right, then down (and to the leftmost column),
6168 * then if all else fails, to the top left. */
6169 if (!exposay_maybe_move(shell, shell->exposay.row_current,
6170 shell->exposay.column_current + 1) &&
6171 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
6172 exposay_maybe_move(shell, 0, 0);
6173 break;
6174 default:
6175 break;
6176 }
6177}
6178
6179static void
6180exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
6181 uint32_t mods_depressed, uint32_t mods_latched,
6182 uint32_t mods_locked, uint32_t group)
6183{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01006184 struct desktop_shell *shell =
6185 container_of(grab, struct desktop_shell, exposay.grab_kbd);
6186 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
6187
6188 /* We want to know when mod has been pressed and released.
6189 * FIXME: There is a problem here: if mod is pressed, then a key
6190 * is pressed and released, then mod is released, we will treat that
6191 * as if only mod had been pressed and released. */
6192 if (seat->modifier_state) {
6193 if (seat->modifier_state == shell->binding_modifier) {
6194 shell->exposay.mod_pressed = true;
6195 } else {
6196 shell->exposay.mod_invalid = true;
6197 }
6198 } else {
6199 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
6200 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
6201
6202 shell->exposay.mod_invalid = false;
6203 shell->exposay.mod_pressed = false;
6204 }
6205
6206 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01006207}
6208
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01006209static void
6210exposay_cancel(struct weston_keyboard_grab *grab)
6211{
6212 struct desktop_shell *shell =
6213 container_of(grab, struct desktop_shell, exposay.grab_kbd);
6214
6215 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
6216}
6217
Daniel Stonedf8133b2013-11-19 11:37:14 +01006218static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
6219 exposay_key,
6220 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01006221 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01006222};
6223
6224/**
6225 * Called when the transition from overview -> inactive has completed.
6226 */
6227static enum exposay_layout_state
6228exposay_set_inactive(struct desktop_shell *shell)
6229{
6230 struct weston_seat *seat = shell->exposay.seat;
6231
6232 weston_keyboard_end_grab(seat->keyboard);
6233 weston_pointer_end_grab(seat->pointer);
6234 if (seat->keyboard->input_method_resource)
6235 seat->keyboard->grab = &seat->keyboard->input_method_grab;
6236
6237 return EXPOSAY_LAYOUT_INACTIVE;
6238}
6239
6240/**
6241 * Begins the transition from overview to inactive. */
6242static enum exposay_layout_state
6243exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
6244{
6245 struct exposay_surface *esurface;
6246
6247 /* Call activate() before we start the animations to avoid
6248 * animating back the old state and then immediately transitioning
6249 * to the new. */
6250 if (switch_focus && shell->exposay.focus_current)
6251 activate(shell, shell->exposay.focus_current->surface,
6252 shell->exposay.seat);
6253 else if (shell->exposay.focus_prev)
6254 activate(shell, shell->exposay.focus_prev->surface,
6255 shell->exposay.seat);
6256
6257 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
6258 exposay_animate_out(esurface);
6259 weston_compositor_schedule_repaint(shell->compositor);
6260
6261 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
6262}
6263
6264static enum exposay_layout_state
6265exposay_transition_active(struct desktop_shell *shell)
6266{
6267 struct weston_seat *seat = shell->exposay.seat;
6268
6269 shell->exposay.workspace = get_current_workspace(shell);
6270 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
6271 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
6272 shell->exposay.clicked = NULL;
6273 wl_list_init(&shell->exposay.surface_list);
6274
6275 lower_fullscreen_layer(shell);
6276 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
6277 weston_keyboard_start_grab(seat->keyboard,
6278 &shell->exposay.grab_kbd);
6279 weston_keyboard_set_focus(seat->keyboard, NULL);
6280
6281 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
6282 weston_pointer_start_grab(seat->pointer,
6283 &shell->exposay.grab_ptr);
6284 weston_pointer_set_focus(seat->pointer, NULL,
6285 seat->pointer->x, seat->pointer->y);
6286
6287 return exposay_layout(shell);
6288}
6289
6290static void
6291exposay_check_state(struct desktop_shell *shell)
6292{
6293 enum exposay_layout_state state_new = shell->exposay.state_cur;
6294 int do_switch = 0;
6295
6296 /* Don't do anything whilst animations are running, just store up
6297 * target state changes and only act on them when the animations have
6298 * completed. */
6299 if (exposay_is_animating(shell))
6300 return;
6301
6302 switch (shell->exposay.state_target) {
6303 case EXPOSAY_TARGET_OVERVIEW:
6304 switch (shell->exposay.state_cur) {
6305 case EXPOSAY_LAYOUT_OVERVIEW:
6306 goto out;
6307 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
6308 state_new = EXPOSAY_LAYOUT_OVERVIEW;
6309 break;
6310 default:
6311 state_new = exposay_transition_active(shell);
6312 break;
6313 }
6314 break;
6315
6316 case EXPOSAY_TARGET_SWITCH:
6317 do_switch = 1; /* fallthrough */
6318 case EXPOSAY_TARGET_CANCEL:
6319 switch (shell->exposay.state_cur) {
6320 case EXPOSAY_LAYOUT_INACTIVE:
6321 goto out;
6322 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
6323 state_new = exposay_set_inactive(shell);
6324 break;
6325 default:
6326 state_new = exposay_transition_inactive(shell, do_switch);
6327 break;
6328 }
6329
6330 break;
6331 }
6332
6333out:
6334 shell->exposay.state_cur = state_new;
6335}
6336
6337static void
6338exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
6339 struct weston_seat *seat)
6340{
6341 shell->exposay.state_target = state;
6342 shell->exposay.seat = seat;
6343 exposay_check_state(shell);
6344}
6345
6346static void
6347exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
6348 void *data)
6349{
6350 struct desktop_shell *shell = data;
6351
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01006352 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006353}
6354
Pekka Paalanen3c647232011-12-22 13:43:43 +02006355static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006356backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006357 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006358{
6359 struct weston_compositor *compositor = data;
6360 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006361 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006362
6363 /* TODO: we're limiting to simple use cases, where we assume just
6364 * control on the primary display. We'd have to extend later if we
6365 * ever get support for setting backlights on random desktop LCD
6366 * panels though */
6367 output = get_default_output(compositor);
6368 if (!output)
6369 return;
6370
6371 if (!output->set_backlight)
6372 return;
6373
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006374 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6375 backlight_new = output->backlight_current - 25;
6376 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6377 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006378
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006379 if (backlight_new < 5)
6380 backlight_new = 5;
6381 if (backlight_new > 255)
6382 backlight_new = 255;
6383
6384 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006385 output->set_backlight(output, output->backlight_current);
6386}
6387
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006388struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006389 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006390 struct weston_seat *seat;
6391 uint32_t key[2];
6392 int key_released[2];
6393};
6394
6395static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006396debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006397 uint32_t key, uint32_t state)
6398{
6399 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01006400 struct weston_compositor *ec = db->seat->compositor;
6401 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006402 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006403 uint32_t serial;
6404 int send = 0, terminate = 0;
6405 int check_binding = 1;
6406 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01006407 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006408
6409 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
6410 /* Do not run bindings on key releases */
6411 check_binding = 0;
6412
6413 for (i = 0; i < 2; i++)
6414 if (key == db->key[i])
6415 db->key_released[i] = 1;
6416
6417 if (db->key_released[0] && db->key_released[1]) {
6418 /* All key releases been swalled so end the grab */
6419 terminate = 1;
6420 } else if (key != db->key[0] && key != db->key[1]) {
6421 /* Should not swallow release of other keys */
6422 send = 1;
6423 }
6424 } else if (key == db->key[0] && !db->key_released[0]) {
6425 /* Do not check bindings for the first press of the binding
6426 * key. This allows it to be used as a debug shortcut.
6427 * We still need to swallow this event. */
6428 check_binding = 0;
6429 } else if (db->key[1]) {
6430 /* If we already ran a binding don't process another one since
6431 * we can't keep track of all the binding keys that were
6432 * pressed in order to swallow the release events. */
6433 send = 1;
6434 check_binding = 0;
6435 }
6436
6437 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006438 if (weston_compositor_run_debug_binding(ec, db->seat, time,
6439 key, state)) {
6440 /* We ran a binding so swallow the press and keep the
6441 * grab to swallow the released too. */
6442 send = 0;
6443 terminate = 0;
6444 db->key[1] = key;
6445 } else {
6446 /* Terminate the grab since the key pressed is not a
6447 * debug binding key. */
6448 send = 1;
6449 terminate = 1;
6450 }
6451 }
6452
6453 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01006454 serial = wl_display_next_serial(display);
6455 resource_list = &grab->keyboard->focus_resource_list;
6456 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006457 wl_keyboard_send_key(resource, serial, time, key, state);
6458 }
6459 }
6460
6461 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006462 weston_keyboard_end_grab(grab->keyboard);
6463 if (grab->keyboard->input_method_resource)
6464 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006465 free(db);
6466 }
6467}
6468
6469static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006470debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006471 uint32_t mods_depressed, uint32_t mods_latched,
6472 uint32_t mods_locked, uint32_t group)
6473{
6474 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01006475 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006476
Neil Roberts96d790e2013-09-19 17:32:00 +01006477 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006478
Neil Roberts96d790e2013-09-19 17:32:00 +01006479 wl_resource_for_each(resource, resource_list) {
6480 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
6481 mods_latched, mods_locked, group);
6482 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006483}
6484
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006485static void
6486debug_binding_cancel(struct weston_keyboard_grab *grab)
6487{
6488 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
6489
6490 weston_keyboard_end_grab(grab->keyboard);
6491 free(db);
6492}
6493
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006494struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006495 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006496 debug_binding_modifiers,
6497 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006498};
6499
6500static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006501debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006502{
6503 struct debug_binding_grab *grab;
6504
6505 grab = calloc(1, sizeof *grab);
6506 if (!grab)
6507 return;
6508
6509 grab->seat = (struct weston_seat *) seat;
6510 grab->key[0] = key;
6511 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006512 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006513}
6514
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006515static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006516force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006517 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006518{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006519 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006520 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006521 struct desktop_shell *shell = data;
6522 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006523 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006524
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006525 focus_surface = seat->keyboard->focus;
6526 if (!focus_surface)
6527 return;
6528
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006529 wl_signal_emit(&compositor->kill_signal, focus_surface);
6530
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006531 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006532 wl_client_get_credentials(client, &pid, NULL, NULL);
6533
6534 /* Skip clients that we launched ourselves (the credentials of
6535 * the socketpair is ours) */
6536 if (pid == getpid())
6537 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006538
Daniel Stone325fc2d2012-05-30 16:31:58 +01006539 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006540}
6541
6542static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006543workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006544 uint32_t key, void *data)
6545{
6546 struct desktop_shell *shell = data;
6547 unsigned int new_index = shell->workspaces.current;
6548
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006549 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006550 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006551 if (new_index != 0)
6552 new_index--;
6553
6554 change_workspace(shell, new_index);
6555}
6556
6557static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006558workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006559 uint32_t key, void *data)
6560{
6561 struct desktop_shell *shell = data;
6562 unsigned int new_index = shell->workspaces.current;
6563
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006564 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006565 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006566 if (new_index < shell->workspaces.num - 1)
6567 new_index++;
6568
6569 change_workspace(shell, new_index);
6570}
6571
6572static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006573workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006574 uint32_t key, void *data)
6575{
6576 struct desktop_shell *shell = data;
6577 unsigned int new_index;
6578
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006579 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006580 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006581 new_index = key - KEY_F1;
6582 if (new_index >= shell->workspaces.num)
6583 new_index = shell->workspaces.num - 1;
6584
6585 change_workspace(shell, new_index);
6586}
6587
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006588static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006589workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006590 uint32_t key, void *data)
6591{
6592 struct desktop_shell *shell = data;
6593 unsigned int new_index = shell->workspaces.current;
6594
6595 if (shell->locked)
6596 return;
6597
6598 if (new_index != 0)
6599 new_index--;
6600
6601 take_surface_to_workspace_by_seat(shell, seat, new_index);
6602}
6603
6604static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006605workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006606 uint32_t key, void *data)
6607{
6608 struct desktop_shell *shell = data;
6609 unsigned int new_index = shell->workspaces.current;
6610
6611 if (shell->locked)
6612 return;
6613
6614 if (new_index < shell->workspaces.num - 1)
6615 new_index++;
6616
6617 take_surface_to_workspace_by_seat(shell, seat, new_index);
6618}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006619
6620static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006621handle_output_destroy(struct wl_listener *listener, void *data)
6622{
6623 struct shell_output *output_listener =
6624 container_of(listener, struct shell_output, destroy_listener);
6625
6626 wl_list_remove(&output_listener->destroy_listener.link);
6627 wl_list_remove(&output_listener->link);
6628 free(output_listener);
6629}
6630
6631static void
6632create_shell_output(struct desktop_shell *shell,
6633 struct weston_output *output)
6634{
6635 struct shell_output *shell_output;
6636
6637 shell_output = zalloc(sizeof *shell_output);
6638 if (shell_output == NULL)
6639 return;
6640
6641 shell_output->output = output;
6642 shell_output->shell = shell;
6643 shell_output->destroy_listener.notify = handle_output_destroy;
6644 wl_signal_add(&output->destroy_signal,
6645 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006646 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006647}
6648
6649static void
6650handle_output_create(struct wl_listener *listener, void *data)
6651{
6652 struct desktop_shell *shell =
6653 container_of(listener, struct desktop_shell, output_create_listener);
6654 struct weston_output *output = (struct weston_output *)data;
6655
6656 create_shell_output(shell, output);
6657}
6658
6659static void
6660setup_output_destroy_handler(struct weston_compositor *ec,
6661 struct desktop_shell *shell)
6662{
6663 struct weston_output *output;
6664
6665 wl_list_init(&shell->output_list);
6666 wl_list_for_each(output, &ec->output_list, link)
6667 create_shell_output(shell, output);
6668
6669 shell->output_create_listener.notify = handle_output_create;
6670 wl_signal_add(&ec->output_created_signal,
6671 &shell->output_create_listener);
6672}
6673
6674static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006675shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006676{
Tiago Vignattibe143262012-04-16 17:31:41 +03006677 struct desktop_shell *shell =
6678 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006679 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006680 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006681
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006682 if (shell->child.client)
6683 wl_client_destroy(shell->child.client);
6684
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006685 wl_list_remove(&shell->idle_listener.link);
6686 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006687 wl_list_remove(&shell->show_input_panel_listener.link);
6688 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006689
Xiong Zhang6b481422013-10-23 13:58:32 +08006690 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6691 wl_list_remove(&shell_output->destroy_listener.link);
6692 wl_list_remove(&shell_output->link);
6693 free(shell_output);
6694 }
6695
6696 wl_list_remove(&shell->output_create_listener.link);
6697
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006698 wl_array_for_each(ws, &shell->workspaces.array)
6699 workspace_destroy(*ws);
6700 wl_array_release(&shell->workspaces.array);
6701
Pekka Paalanen3c647232011-12-22 13:43:43 +02006702 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006703 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006704 free(shell);
6705}
6706
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006707static void
6708shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6709{
6710 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006711 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006712
6713 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006714 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6715 MODIFIER_CTRL | MODIFIER_ALT,
6716 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01006717 weston_compositor_add_key_binding(ec, KEY_TAB,
6718 MODIFIER_ALT,
6719 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006720 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6721 click_to_activate_binding,
6722 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006723 weston_compositor_add_touch_binding(ec, 0,
6724 touch_to_activate_binding,
6725 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006726 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6727 MODIFIER_SUPER | MODIFIER_ALT,
6728 surface_opacity_binding, NULL);
6729 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6730 MODIFIER_SUPER, zoom_axis_binding,
6731 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006732
6733 /* configurable bindings */
6734 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006735 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6736 zoom_key_binding, NULL);
6737 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6738 zoom_key_binding, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02006739 weston_compositor_add_key_binding(ec, KEY_M, mod, maximize_binding,
6740 NULL);
6741 weston_compositor_add_key_binding(ec, KEY_F, mod, fullscreen_binding,
6742 NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006743 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6744 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006745 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006746 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6747 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006748
6749 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6750 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6751 rotate_binding, NULL);
6752
Daniel Stone325fc2d2012-05-30 16:31:58 +01006753 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6754 shell);
6755 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6756 ec);
6757 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6758 backlight_binding, ec);
6759 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6760 ec);
6761 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6762 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006763 weston_compositor_add_key_binding(ec, KEY_K, mod,
6764 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006765 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6766 workspace_up_binding, shell);
6767 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6768 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006769 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6770 workspace_move_surface_up_binding,
6771 shell);
6772 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6773 workspace_move_surface_down_binding,
6774 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006775
Daniel Stonedf8133b2013-11-19 11:37:14 +01006776 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6777
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006778 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6779 if (shell->workspaces.num > 1) {
6780 num_workspace_bindings = shell->workspaces.num;
6781 if (num_workspace_bindings > 6)
6782 num_workspace_bindings = 6;
6783 for (i = 0; i < num_workspace_bindings; i++)
6784 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6785 workspace_f_binding,
6786 shell);
6787 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006788
6789 /* Debug bindings */
6790 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6791 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006792}
6793
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006794WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006795module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006796 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006797{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006798 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006799 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006800 struct workspace **pws;
6801 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006802 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006803
Peter Huttererf3d62272013-08-08 11:57:05 +10006804 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006805 if (shell == NULL)
6806 return -1;
6807
Kristian Høgsberg75840622011-09-06 13:48:16 -04006808 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006809
6810 shell->destroy_listener.notify = shell_destroy;
6811 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006812 shell->idle_listener.notify = idle_handler;
6813 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6814 shell->wake_listener.notify = wake_handler;
6815 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006816 shell->show_input_panel_listener.notify = show_input_panels;
6817 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6818 shell->hide_input_panel_listener.notify = hide_input_panels;
6819 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006820 shell->update_input_panel_listener.notify = update_input_panels;
6821 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006822 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006823 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006824 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006825 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006826 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006827 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006828 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006829 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006830 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006831 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006832 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006833
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006834 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006835
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006836 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6837 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006838 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6839 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006840 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006841
6842 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006843 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006844
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006845 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006846
Daniel Stonedf8133b2013-11-19 11:37:14 +01006847 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6848 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6849
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006850 for (i = 0; i < shell->workspaces.num; i++) {
6851 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6852 if (pws == NULL)
6853 return -1;
6854
6855 *pws = workspace_create();
6856 if (*pws == NULL)
6857 return -1;
6858 }
6859 activate_workspace(shell, 0);
6860
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006861 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006862 wl_list_init(&shell->workspaces.animation.link);
6863 shell->workspaces.animation.frame = animate_workspace_change_frame;
6864
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006865 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006866 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006867 return -1;
6868
Rafael Antognollie2a34552013-12-03 15:35:45 -02006869 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6870 shell, bind_xdg_shell) == NULL)
6871 return -1;
6872
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006873 if (wl_global_create(ec->wl_display,
6874 &desktop_shell_interface, 2,
6875 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006876 return -1;
6877
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006878 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6879 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006880 return -1;
6881
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006882 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006883 shell, bind_input_panel) == NULL)
6884 return -1;
6885
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006886 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6887 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006888 return -1;
6889
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006890 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006891
Xiong Zhang6b481422013-10-23 13:58:32 +08006892 setup_output_destroy_handler(ec, shell);
6893
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006894 loop = wl_display_get_event_loop(ec->wl_display);
6895 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006896
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006897 shell->screensaver.timer =
6898 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6899
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006900 wl_list_for_each(seat, &ec->seat_list, link)
6901 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006902
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006903 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006904
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006905 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006906
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006907 return 0;
6908}