blob: 0d386cf8b1dffcc88e3aeee443f1d38a138c2fea [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020029#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050030#include <string.h>
31#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040032#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020034#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020035#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040036#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010051 ANIMATION_FADE,
52 ANIMATION_DIM_LAYER,
Juan Zhaoe10d2792012-04-25 19:09:52 +080053};
54
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020055enum fade_type {
56 FADE_IN,
57 FADE_OUT
58};
59
Daniel Stonedf8133b2013-11-19 11:37:14 +010060enum exposay_target_state {
61 EXPOSAY_TARGET_OVERVIEW, /* show all windows */
62 EXPOSAY_TARGET_CANCEL, /* return to normal, same focus */
63 EXPOSAY_TARGET_SWITCH, /* return to normal, switch focus */
64};
65
66enum exposay_layout_state {
67 EXPOSAY_LAYOUT_INACTIVE = 0, /* normal desktop */
68 EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE, /* in transition to normal */
69 EXPOSAY_LAYOUT_OVERVIEW, /* show all windows */
70 EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW, /* in transition to all windows */
71};
72
Jonas Ådahl04769742012-06-13 00:01:24 +020073struct focus_state {
74 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040075 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020076 struct weston_surface *keyboard_focus;
77 struct wl_list link;
78 struct wl_listener seat_destroy_listener;
79 struct wl_listener surface_destroy_listener;
80};
81
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010082struct focus_surface {
83 struct weston_surface *surface;
84 struct weston_view *view;
85 struct weston_transform workspace_transform;
86};
87
Jonas Ådahle3cddce2012-06-13 00:01:22 +020088struct workspace {
89 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020090
91 struct wl_list focus_list;
92 struct wl_listener seat_destroyed_listener;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +010093
94 struct focus_surface *fsurf_front;
95 struct focus_surface *fsurf_back;
96 struct weston_view_animation *focus_animation;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020097};
98
Philipp Brüschweiler88013572012-08-06 13:44:42 +020099struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -0500100 struct wl_resource *resource;
101 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100102
103 struct desktop_shell *shell;
104
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200105 struct wl_list link;
106 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500107 struct weston_view *view;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +0100108 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200109
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +0200110 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200111 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +0200112};
113
Xiong Zhang6b481422013-10-23 13:58:32 +0800114struct shell_output {
115 struct desktop_shell *shell;
116 struct weston_output *output;
117 struct wl_listener destroy_listener;
118 struct wl_list link;
119};
120
Tiago Vignattibe143262012-04-16 17:31:41 +0300121struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400123
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200124 struct wl_listener idle_listener;
125 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400126 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200127 struct wl_listener show_input_panel_listener;
128 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200129 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200130
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500131 struct weston_layer fullscreen_layer;
132 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500133 struct weston_layer background_layer;
134 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200135 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500136
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400137 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300138 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400139
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200140 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500141 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200142 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200143 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200144
145 unsigned deathcount;
146 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200147 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200148
149 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200150 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200151 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200152
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200153 struct {
154 struct weston_surface *surface;
155 pixman_box32_t cursor_rectangle;
156 } text_input;
157
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400158 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500159 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160
Pekka Paalanen77346a62011-11-30 16:26:35 +0200161 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200162 struct wl_array array;
163 unsigned int current;
164 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200165
Jonas Ådahle9d22502012-08-29 22:13:01 +0200166 struct wl_list client_list;
167
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200168 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200169 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200170 int anim_dir;
171 uint32_t anim_timestamp;
172 double anim_current;
173 struct workspace *anim_from;
174 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200175 } workspaces;
176
177 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200178 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200179 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200180 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500181 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200182 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200183 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500184
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200185 struct {
186 struct wl_resource *binding;
187 struct wl_list surfaces;
188 } input_panel;
189
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200190 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500191 struct weston_view *view;
192 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200193 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300194 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200195 } fade;
196
Daniel Stonedf8133b2013-11-19 11:37:14 +0100197 struct exposay {
198 /* XXX: Make these exposay_surfaces. */
199 struct weston_view *focus_prev;
200 struct weston_view *focus_current;
201 struct weston_view *clicked;
202 struct workspace *workspace;
203 struct weston_seat *seat;
204 struct wl_list surface_list;
205
206 struct weston_keyboard_grab grab_kbd;
207 struct weston_pointer_grab grab_ptr;
208
209 enum exposay_target_state state_target;
210 enum exposay_layout_state state_cur;
211 int in_flight; /* number of animations still running */
212
213 int num_surfaces;
214 int grid_size;
215 int surface_size;
216
217 int hpadding_outer;
218 int vpadding_outer;
219 int padding_inner;
220
221 int row_current;
222 int column_current;
223 } exposay;
224
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300225 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800226 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700227 enum animation_type startup_animation_type;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100228 enum animation_type focus_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800229
230 struct wl_listener output_create_listener;
231 struct wl_list output_list;
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100232
233 char *client;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400234};
235
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500236enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200237 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500238 SHELL_SURFACE_TOPLEVEL,
239 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500240 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800241 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300242 SHELL_SURFACE_POPUP,
243 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200244};
245
Scott Moreauff1db4a2012-04-17 19:06:18 -0600246struct ping_timer {
247 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600248 uint32_t serial;
249};
250
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200251struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500252 struct wl_resource *resource;
253 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200254
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500255 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500256 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200257 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400258 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300259 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200260
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400261 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400262 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500263 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800264 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800265 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700266 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100267
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500268 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200269 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500270 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200271 } rotation;
272
273 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100274 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500275 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100276 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400277 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500278 } popup;
279
Alex Wu4539b082012-03-01 12:57:46 +0800280 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300281 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400282 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300283 } transient;
284
285 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800286 enum wl_shell_surface_fullscreen_method type;
287 struct weston_transform transform; /* matrix from x, y */
288 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800290 } fullscreen;
291
Scott Moreauff1db4a2012-04-17 19:06:18 -0600292 struct ping_timer *ping_timer;
293
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200294 struct weston_transform workspace_transform;
295
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500296 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500297 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100298 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400299
300 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200301};
302
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300303struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400304 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300305 struct shell_surface *shsurf;
306 struct wl_listener shsurf_destroy_listener;
307};
308
Rusty Lynch1084da52013-08-15 09:10:08 -0700309struct shell_touch_grab {
310 struct weston_touch_grab grab;
311 struct shell_surface *shsurf;
312 struct wl_listener shsurf_destroy_listener;
313 struct weston_touch *touch;
314};
315
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300316struct weston_move_grab {
317 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100318 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500319};
320
Rusty Lynch1084da52013-08-15 09:10:08 -0700321struct weston_touch_move_grab {
322 struct shell_touch_grab base;
323 wl_fixed_t dx, dy;
324};
325
Pekka Paalanen460099f2012-01-20 16:48:25 +0200326struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500328 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200329 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200330 float x;
331 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200332 } center;
333};
334
Giulio Camuffo5085a752013-03-25 21:42:45 +0100335struct shell_seat {
336 struct weston_seat *seat;
337 struct wl_listener seat_destroy_listener;
338
339 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400340 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100341 struct wl_list surfaces_list;
342 struct wl_client *client;
343 int32_t initial_up;
344 } popup_grab;
345};
346
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400347static void
348activate(struct desktop_shell *shell, struct weston_surface *es,
349 struct weston_seat *seat);
350
351static struct workspace *
352get_current_workspace(struct desktop_shell *shell);
353
Alex Wubd3354b2012-04-17 17:20:49 +0800354static struct shell_surface *
355get_shell_surface(struct weston_surface *surface);
356
357static struct desktop_shell *
358shell_surface_get_shell(struct shell_surface *shsurf);
359
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500360static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400361surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500362
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300363static void
364shell_fade_startup(struct desktop_shell *shell);
365
Alex Wubd3354b2012-04-17 17:20:49 +0800366static bool
367shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
368{
369 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500370 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800371
372 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100373
Jason Ekstranda7af7042013-10-12 22:38:11 -0500374 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800375 return false;
376
Jason Ekstranda7af7042013-10-12 22:38:11 -0500377 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
378 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800379 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500380 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800381}
382
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500383static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400384destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300385{
386 struct shell_grab *grab;
387
388 grab = container_of(listener, struct shell_grab,
389 shsurf_destroy_listener);
390
391 grab->shsurf = NULL;
392}
393
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394static struct weston_view *
395get_default_view(struct weston_surface *surface)
396{
397 struct shell_surface *shsurf;
398 struct weston_view *view;
399
400 if (!surface || wl_list_empty(&surface->views))
401 return NULL;
402
403 shsurf = get_shell_surface(surface);
404 if (shsurf)
405 return shsurf->view;
406
407 wl_list_for_each(view, &surface->views, surface_link)
408 if (weston_view_is_mapped(view))
409 return view;
410
411 return container_of(surface->views.next, struct weston_view, surface_link);
412}
413
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300414static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400415popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400416
417static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300418shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400419 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300420 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400421 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300422 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300423{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300424 struct desktop_shell *shell = shsurf->shell;
425
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400426 popup_grab_end(pointer);
427
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300428 grab->grab.interface = interface;
429 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400430 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500431 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400432 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300433
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700434 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400435 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400436 if (shell->child.desktop_shell) {
437 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
438 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500439 weston_pointer_set_focus(pointer,
440 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400441 wl_fixed_from_int(0),
442 wl_fixed_from_int(0));
443 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300444}
445
446static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300447shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300448{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700449 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400450 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700451 grab->shsurf->grabbed = 0;
452 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300453
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700454 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300455}
456
457static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700458shell_touch_grab_start(struct shell_touch_grab *grab,
459 const struct weston_touch_grab_interface *interface,
460 struct shell_surface *shsurf,
461 struct weston_touch *touch)
462{
463 struct desktop_shell *shell = shsurf->shell;
464
465 grab->grab.interface = interface;
466 grab->shsurf = shsurf;
467 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
468 wl_signal_add(&shsurf->destroy_signal,
469 &grab->shsurf_destroy_listener);
470
471 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700472 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700473
474 weston_touch_start_grab(touch, &grab->grab);
475 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500476 weston_touch_set_focus(touch->seat,
477 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700478}
479
480static void
481shell_touch_grab_end(struct shell_touch_grab *grab)
482{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700483 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700484 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700485 grab->shsurf->grabbed = 0;
486 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700487
488 weston_touch_end_grab(grab->touch);
489}
490
491static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500492center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800493 struct weston_output *output);
494
Daniel Stone496ca172012-05-30 16:31:42 +0100495static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300496get_modifier(char *modifier)
497{
498 if (!modifier)
499 return MODIFIER_SUPER;
500
501 if (!strcmp("ctrl", modifier))
502 return MODIFIER_CTRL;
503 else if (!strcmp("alt", modifier))
504 return MODIFIER_ALT;
505 else if (!strcmp("super", modifier))
506 return MODIFIER_SUPER;
507 else
508 return MODIFIER_SUPER;
509}
510
Juan Zhaoe10d2792012-04-25 19:09:52 +0800511static enum animation_type
512get_animation_type(char *animation)
513{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800514 if (!strcmp("zoom", animation))
515 return ANIMATION_ZOOM;
516 else if (!strcmp("fade", animation))
517 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100518 else if (!strcmp("dim-layer", animation))
519 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800520 else
521 return ANIMATION_NONE;
522}
523
Alex Wu4539b082012-03-01 12:57:46 +0800524static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400525shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200526{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400527 struct weston_config_section *section;
528 int duration;
529 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200530
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400531 section = weston_config_get_section(shell->compositor->config,
532 "screensaver", NULL, NULL);
533 weston_config_section_get_string(section,
534 "path", &shell->screensaver.path, NULL);
535 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200536 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400537
538 section = weston_config_get_section(shell->compositor->config,
539 "shell", NULL, NULL);
540 weston_config_section_get_string(section,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100541 "client", &s, LIBEXECDIR "/weston-desktop-shell");
542 shell->client = s;
543 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400544 "binding-modifier", &s, "super");
545 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200546 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400547 weston_config_section_get_string(section, "animation", &s, "none");
548 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200549 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700550 weston_config_section_get_string(section,
551 "startup-animation", &s, "fade");
552 shell->startup_animation_type = get_animation_type(s);
553 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700554 if (shell->startup_animation_type == ANIMATION_ZOOM)
555 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100556 weston_config_section_get_string(section, "focus-animation", &s, "none");
557 shell->focus_animation_type = get_animation_type(s);
558 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400559 weston_config_section_get_uint(section, "num-workspaces",
560 &shell->workspaces.num,
561 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200562}
563
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100564static struct weston_output *
565get_default_output(struct weston_compositor *compositor)
566{
567 return container_of(compositor->output_list.next,
568 struct weston_output, link);
569}
570
571
572/* no-op func for checking focus surface */
573static void
574focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy,
575 int32_t width, int32_t height)
576{
577}
578
579static struct focus_surface *
580get_focus_surface(struct weston_surface *surface)
581{
582 if (surface->configure == focus_surface_configure)
583 return surface->configure_private;
584 else
585 return NULL;
586}
587
588static bool
589is_focus_surface (struct weston_surface *es)
590{
591 return (es->configure == focus_surface_configure);
592}
593
594static bool
595is_focus_view (struct weston_view *view)
596{
597 return is_focus_surface (view->surface);
598}
599
600static struct focus_surface *
601create_focus_surface(struct weston_compositor *ec,
602 struct weston_output *output)
603{
604 struct focus_surface *fsurf = NULL;
605 struct weston_surface *surface = NULL;
606
607 fsurf = malloc(sizeof *fsurf);
608 if (!fsurf)
609 return NULL;
610
611 fsurf->surface = weston_surface_create(ec);
612 surface = fsurf->surface;
613 if (surface == NULL) {
614 free(fsurf);
615 return NULL;
616 }
617
618 surface->configure = focus_surface_configure;
619 surface->output = output;
620 surface->configure_private = fsurf;
621
622 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100623 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100624
625 weston_view_configure(fsurf->view, output->x, output->y,
626 output->width, output->height);
627 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
628 pixman_region32_fini(&surface->opaque);
629 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
630 output->width, output->height);
631 pixman_region32_fini(&surface->input);
632 pixman_region32_init(&surface->input);
633
634 wl_list_init(&fsurf->workspace_transform.link);
635
636 return fsurf;
637}
638
639static void
640focus_surface_destroy(struct focus_surface *fsurf)
641{
642 weston_surface_destroy(fsurf->surface);
643 free(fsurf);
644}
645
646static void
647focus_animation_done(struct weston_view_animation *animation, void *data)
648{
649 struct workspace *ws = data;
650
651 ws->focus_animation = NULL;
652}
653
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200654static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200655focus_state_destroy(struct focus_state *state)
656{
657 wl_list_remove(&state->seat_destroy_listener.link);
658 wl_list_remove(&state->surface_destroy_listener.link);
659 free(state);
660}
661
662static void
663focus_state_seat_destroy(struct wl_listener *listener, void *data)
664{
665 struct focus_state *state = container_of(listener,
666 struct focus_state,
667 seat_destroy_listener);
668
669 wl_list_remove(&state->link);
670 focus_state_destroy(state);
671}
672
673static void
674focus_state_surface_destroy(struct wl_listener *listener, void *data)
675{
676 struct focus_state *state = container_of(listener,
677 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400678 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400679 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500680 struct weston_surface *main_surface, *next;
681 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200682
Pekka Paalanen01388e22013-04-25 13:57:44 +0300683 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
684
Kristian Høgsberge3778222012-07-31 17:29:30 -0400685 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500686 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
687 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400688 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100689 if (is_focus_view(view))
690 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400691
Jason Ekstranda7af7042013-10-12 22:38:11 -0500692 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400693 break;
694 }
695
Pekka Paalanen01388e22013-04-25 13:57:44 +0300696 /* if the focus was a sub-surface, activate its main surface */
697 if (main_surface != state->keyboard_focus)
698 next = main_surface;
699
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100700 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400701 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100702 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400703 activate(shell, next, state->seat);
704 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100705 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
706 if (state->ws->focus_animation)
707 weston_view_animation_destroy(state->ws->focus_animation);
708
709 state->ws->focus_animation = weston_fade_run(
710 state->ws->fsurf_front->view,
711 state->ws->fsurf_front->view->alpha, 0.0, 300,
712 focus_animation_done, state->ws);
713 }
714
Kristian Høgsberge3778222012-07-31 17:29:30 -0400715 wl_list_remove(&state->link);
716 focus_state_destroy(state);
717 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200718}
719
720static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400721focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200722{
Jonas Ådahl04769742012-06-13 00:01:24 +0200723 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200724
725 state = malloc(sizeof *state);
726 if (state == NULL)
727 return NULL;
728
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100729 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400730 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200731 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400732 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200733
734 state->seat_destroy_listener.notify = focus_state_seat_destroy;
735 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400736 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200737 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400738 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200739
740 return state;
741}
742
Jonas Ådahl8538b222012-08-29 22:13:03 +0200743static struct focus_state *
744ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
745{
746 struct workspace *ws = get_current_workspace(shell);
747 struct focus_state *state;
748
749 wl_list_for_each(state, &ws->focus_list, link)
750 if (state->seat == seat)
751 break;
752
753 if (&state->link == &ws->focus_list)
754 state = focus_state_create(seat, ws);
755
756 return state;
757}
758
Jonas Ådahl04769742012-06-13 00:01:24 +0200759static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400760restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200761{
762 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400763 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200764
765 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400766 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200767
Kristian Høgsberge3148752013-05-06 23:19:49 -0400768 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200769 }
770}
771
772static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200773replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
774 struct weston_seat *seat)
775{
776 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400777 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200778
779 wl_list_for_each(state, &ws->focus_list, link) {
780 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400781 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500782 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200783 return;
784 }
785 }
786}
787
788static void
789drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
790 struct weston_surface *surface)
791{
792 struct focus_state *state;
793
794 wl_list_for_each(state, &ws->focus_list, link)
795 if (state->keyboard_focus == surface)
796 state->keyboard_focus = NULL;
797}
798
799static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100800animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
801 struct weston_view *from, struct weston_view *to)
802{
803 struct weston_output *output;
804 bool focus_surface_created = false;
805
806 /* FIXME: Only support dim animation using two layers */
807 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
808 return;
809
810 output = get_default_output(shell->compositor);
811 if (ws->fsurf_front == NULL && (from || to)) {
812 ws->fsurf_front = create_focus_surface(shell->compositor, output);
813 ws->fsurf_back = create_focus_surface(shell->compositor, output);
814 ws->fsurf_front->view->alpha = 0.0;
815 ws->fsurf_back->view->alpha = 0.0;
816 focus_surface_created = true;
817 } else {
818 wl_list_remove(&ws->fsurf_front->view->layer_link);
819 wl_list_remove(&ws->fsurf_back->view->layer_link);
820 }
821
822 if (ws->focus_animation) {
823 weston_view_animation_destroy(ws->focus_animation);
824 ws->focus_animation = NULL;
825 }
826
827 if (to)
828 wl_list_insert(&to->layer_link,
829 &ws->fsurf_front->view->layer_link);
830 else if (from)
831 wl_list_insert(&ws->layer.view_list,
832 &ws->fsurf_front->view->layer_link);
833
834 if (focus_surface_created) {
835 ws->focus_animation = weston_fade_run(
836 ws->fsurf_front->view,
837 ws->fsurf_front->view->alpha, 0.6, 300,
838 focus_animation_done, ws);
839 } else if (from) {
840 wl_list_insert(&from->layer_link,
841 &ws->fsurf_back->view->layer_link);
842 ws->focus_animation = weston_stable_fade_run(
843 ws->fsurf_front->view, 0.0,
844 ws->fsurf_back->view, 0.6,
845 focus_animation_done, ws);
846 } else if (to) {
847 wl_list_insert(&ws->layer.view_list,
848 &ws->fsurf_back->view->layer_link);
849 ws->focus_animation = weston_stable_fade_run(
850 ws->fsurf_front->view, 0.0,
851 ws->fsurf_back->view, 0.6,
852 focus_animation_done, ws);
853 }
854}
855
856static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200857workspace_destroy(struct workspace *ws)
858{
Jonas Ådahl04769742012-06-13 00:01:24 +0200859 struct focus_state *state, *next;
860
861 wl_list_for_each_safe(state, next, &ws->focus_list, link)
862 focus_state_destroy(state);
863
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100864 if (ws->fsurf_front)
865 focus_surface_destroy(ws->fsurf_front);
866 if (ws->fsurf_back)
867 focus_surface_destroy(ws->fsurf_back);
868
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200869 free(ws);
870}
871
Jonas Ådahl04769742012-06-13 00:01:24 +0200872static void
873seat_destroyed(struct wl_listener *listener, void *data)
874{
875 struct weston_seat *seat = data;
876 struct focus_state *state, *next;
877 struct workspace *ws = container_of(listener,
878 struct workspace,
879 seat_destroyed_listener);
880
881 wl_list_for_each_safe(state, next, &ws->focus_list, link)
882 if (state->seat == seat)
883 wl_list_remove(&state->link);
884}
885
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200886static struct workspace *
887workspace_create(void)
888{
889 struct workspace *ws = malloc(sizeof *ws);
890 if (ws == NULL)
891 return NULL;
892
893 weston_layer_init(&ws->layer, NULL);
894
Jonas Ådahl04769742012-06-13 00:01:24 +0200895 wl_list_init(&ws->focus_list);
896 wl_list_init(&ws->seat_destroyed_listener.link);
897 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100898 ws->fsurf_front = NULL;
899 ws->fsurf_back = NULL;
900 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200901
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200902 return ws;
903}
904
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200905static int
906workspace_is_empty(struct workspace *ws)
907{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200909}
910
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200911static struct workspace *
912get_workspace(struct desktop_shell *shell, unsigned int index)
913{
914 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200915 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200916 pws += index;
917 return *pws;
918}
919
920static struct workspace *
921get_current_workspace(struct desktop_shell *shell)
922{
923 return get_workspace(shell, shell->workspaces.current);
924}
925
926static void
927activate_workspace(struct desktop_shell *shell, unsigned int index)
928{
929 struct workspace *ws;
930
931 ws = get_workspace(shell, index);
932 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
933
934 shell->workspaces.current = index;
935}
936
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200937static unsigned int
938get_output_height(struct weston_output *output)
939{
940 return abs(output->region.extents.y1 - output->region.extents.y2);
941}
942
943static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100944view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200945{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200946 struct weston_transform *transform;
947
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100948 if (is_focus_view(view)) {
949 struct focus_surface *fsurf = get_focus_surface(view->surface);
950 transform = &fsurf->workspace_transform;
951 } else {
952 struct shell_surface *shsurf = get_shell_surface(view->surface);
953 transform = &shsurf->workspace_transform;
954 }
955
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200956 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100958 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200959
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100960 weston_matrix_init(&transform->matrix);
961 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964}
965
966static void
967workspace_translate_out(struct workspace *ws, double fraction)
968{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200970 unsigned int height;
971 double d;
972
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
974 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975 d = height * fraction;
976
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978 }
979}
980
981static void
982workspace_translate_in(struct workspace *ws, double fraction)
983{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200985 unsigned int height;
986 double d;
987
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
989 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200990
991 if (fraction > 0)
992 d = -(height - height * fraction);
993 else
994 d = height + height * fraction;
995
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997 }
998}
999
1000static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001001broadcast_current_workspace_state(struct desktop_shell *shell)
1002{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001003 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001004
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001005 wl_resource_for_each(resource, &shell->workspaces.client_list)
1006 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001007 shell->workspaces.current,
1008 shell->workspaces.num);
1009}
1010
1011static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001012reverse_workspace_change_animation(struct desktop_shell *shell,
1013 unsigned int index,
1014 struct workspace *from,
1015 struct workspace *to)
1016{
1017 shell->workspaces.current = index;
1018
1019 shell->workspaces.anim_to = to;
1020 shell->workspaces.anim_from = from;
1021 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1022 shell->workspaces.anim_timestamp = 0;
1023
Scott Moreau4272e632012-08-13 09:58:41 -06001024 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001025}
1026
1027static void
1028workspace_deactivate_transforms(struct workspace *ws)
1029{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001032
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 if (is_focus_view(view)) {
1035 struct focus_surface *fsurf = get_focus_surface(view->surface);
1036 transform = &fsurf->workspace_transform;
1037 } else {
1038 struct shell_surface *shsurf = get_shell_surface(view->surface);
1039 transform = &shsurf->workspace_transform;
1040 }
1041
1042 if (!wl_list_empty(&transform->link)) {
1043 wl_list_remove(&transform->link);
1044 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001045 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001047 }
1048}
1049
1050static void
1051finish_workspace_change_animation(struct desktop_shell *shell,
1052 struct workspace *from,
1053 struct workspace *to)
1054{
Scott Moreau4272e632012-08-13 09:58:41 -06001055 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001056
1057 wl_list_remove(&shell->workspaces.animation.link);
1058 workspace_deactivate_transforms(from);
1059 workspace_deactivate_transforms(to);
1060 shell->workspaces.anim_to = NULL;
1061
1062 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1063}
1064
1065static void
1066animate_workspace_change_frame(struct weston_animation *animation,
1067 struct weston_output *output, uint32_t msecs)
1068{
1069 struct desktop_shell *shell =
1070 container_of(animation, struct desktop_shell,
1071 workspaces.animation);
1072 struct workspace *from = shell->workspaces.anim_from;
1073 struct workspace *to = shell->workspaces.anim_to;
1074 uint32_t t;
1075 double x, y;
1076
1077 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1078 finish_workspace_change_animation(shell, from, to);
1079 return;
1080 }
1081
1082 if (shell->workspaces.anim_timestamp == 0) {
1083 if (shell->workspaces.anim_current == 0.0)
1084 shell->workspaces.anim_timestamp = msecs;
1085 else
1086 shell->workspaces.anim_timestamp =
1087 msecs -
1088 /* Invers of movement function 'y' below. */
1089 (asin(1.0 - shell->workspaces.anim_current) *
1090 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1091 M_2_PI);
1092 }
1093
1094 t = msecs - shell->workspaces.anim_timestamp;
1095
1096 /*
1097 * x = [0, π/2]
1098 * y(x) = sin(x)
1099 */
1100 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1101 y = sin(x);
1102
1103 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001104 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105
1106 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1107 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1108 shell->workspaces.anim_current = y;
1109
Scott Moreau4272e632012-08-13 09:58:41 -06001110 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001112 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001113 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114}
1115
1116static void
1117animate_workspace_change(struct desktop_shell *shell,
1118 unsigned int index,
1119 struct workspace *from,
1120 struct workspace *to)
1121{
1122 struct weston_output *output;
1123
1124 int dir;
1125
1126 if (index > shell->workspaces.current)
1127 dir = -1;
1128 else
1129 dir = 1;
1130
1131 shell->workspaces.current = index;
1132
1133 shell->workspaces.anim_dir = dir;
1134 shell->workspaces.anim_from = from;
1135 shell->workspaces.anim_to = to;
1136 shell->workspaces.anim_current = 0.0;
1137 shell->workspaces.anim_timestamp = 0;
1138
1139 output = container_of(shell->compositor->output_list.next,
1140 struct weston_output, link);
1141 wl_list_insert(&output->animation_list,
1142 &shell->workspaces.animation.link);
1143
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001144 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145
1146 workspace_translate_in(to, 0);
1147
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001148 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001149
Scott Moreau4272e632012-08-13 09:58:41 -06001150 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001151}
1152
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001153static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001154update_workspace(struct desktop_shell *shell, unsigned int index,
1155 struct workspace *from, struct workspace *to)
1156{
1157 shell->workspaces.current = index;
1158 wl_list_insert(&from->layer.link, &to->layer.link);
1159 wl_list_remove(&from->layer.link);
1160}
1161
1162static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001163change_workspace(struct desktop_shell *shell, unsigned int index)
1164{
1165 struct workspace *from;
1166 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001167 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001168
1169 if (index == shell->workspaces.current)
1170 return;
1171
1172 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001174 return;
1175
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001176 from = get_current_workspace(shell);
1177 to = get_workspace(shell, index);
1178
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001179 if (shell->workspaces.anim_from == to &&
1180 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001181 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001183 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001184 return;
1185 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001186
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001187 if (shell->workspaces.anim_to != NULL)
1188 finish_workspace_change_animation(shell,
1189 shell->workspaces.anim_from,
1190 shell->workspaces.anim_to);
1191
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001192 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001193
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001194 if (shell->focus_animation_type != ANIMATION_NONE) {
1195 wl_list_for_each(state, &from->focus_list, link)
1196 if (state->keyboard_focus)
1197 animate_focus_change(shell, from,
1198 get_default_view(state->keyboard_focus), NULL);
1199
1200 wl_list_for_each(state, &to->focus_list, link)
1201 if (state->keyboard_focus)
1202 animate_focus_change(shell, to,
1203 NULL, get_default_view(state->keyboard_focus));
1204 }
1205
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001206 if (workspace_is_empty(to) && workspace_is_empty(from))
1207 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001208 else
1209 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001210
1211 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001212}
1213
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214static bool
1215workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1216{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001218 struct wl_list *e;
1219
1220 if (wl_list_empty(list))
1221 return false;
1222
1223 e = list->next;
1224
1225 if (e->next != list)
1226 return false;
1227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001229}
1230
1231static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232move_view_to_workspace(struct desktop_shell *shell,
1233 struct weston_view *view,
1234 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001235{
1236 struct workspace *from;
1237 struct workspace *to;
1238 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001239 struct weston_surface *focus;
1240
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001242
1243 if (workspace == shell->workspaces.current)
1244 return;
1245
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001246 if (workspace >= shell->workspaces.num)
1247 workspace = shell->workspaces.num - 1;
1248
Jonas Ådahle9d22502012-08-29 22:13:01 +02001249 from = get_current_workspace(shell);
1250 to = get_workspace(shell, workspace);
1251
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 wl_list_remove(&view->layer_link);
1253 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001254
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001256 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1257 if (!seat->keyboard)
1258 continue;
1259
1260 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001262 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001263 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001266}
1267
1268static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001269take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001270 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001271 unsigned int index)
1272{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001273 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001275 struct shell_surface *shsurf;
1276 struct workspace *from;
1277 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001278 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279
Pekka Paalanen01388e22013-04-25 13:57:44 +03001280 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 view = get_default_view(surface);
1282 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001283 index == shell->workspaces.current ||
1284 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001285 return;
1286
1287 from = get_current_workspace(shell);
1288 to = get_workspace(shell, index);
1289
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 wl_list_remove(&view->layer_link);
1291 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001292
Jonas Ådahle9d22502012-08-29 22:13:01 +02001293 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001294 drop_focus_state(shell, from, surface);
1295
1296 if (shell->workspaces.anim_from == to &&
1297 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001298 wl_list_remove(&to->layer.link);
1299 wl_list_insert(from->layer.link.prev, &to->layer.link);
1300
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001301 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001302 broadcast_current_workspace_state(shell);
1303
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304 return;
1305 }
1306
1307 if (shell->workspaces.anim_to != NULL)
1308 finish_workspace_change_animation(shell,
1309 shell->workspaces.anim_from,
1310 shell->workspaces.anim_to);
1311
1312 if (workspace_is_empty(from) &&
1313 workspace_has_only(to, surface))
1314 update_workspace(shell, index, from, to);
1315 else {
1316 shsurf = get_shell_surface(surface);
1317 if (wl_list_empty(&shsurf->workspace_transform.link))
1318 wl_list_insert(&shell->workspaces.anim_sticky_list,
1319 &shsurf->workspace_transform.link);
1320
1321 animate_workspace_change(shell, index, from, to);
1322 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323
1324 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001325
1326 state = ensure_focus_state(shell, seat);
1327 if (state != NULL)
1328 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001329}
1330
1331static void
1332workspace_manager_move_surface(struct wl_client *client,
1333 struct wl_resource *resource,
1334 struct wl_resource *surface_resource,
1335 uint32_t workspace)
1336{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001337 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001338 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001339 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001340 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001342
Pekka Paalanen01388e22013-04-25 13:57:44 +03001343 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 view = get_default_view(main_surface);
1345 if (!view)
1346 return;
1347 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001348}
1349
1350static const struct workspace_manager_interface workspace_manager_implementation = {
1351 workspace_manager_move_surface,
1352};
1353
1354static void
1355unbind_resource(struct wl_resource *resource)
1356{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001357 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001358}
1359
1360static void
1361bind_workspace_manager(struct wl_client *client,
1362 void *data, uint32_t version, uint32_t id)
1363{
1364 struct desktop_shell *shell = data;
1365 struct wl_resource *resource;
1366
Jason Ekstranda85118c2013-06-27 20:17:02 -05001367 resource = wl_resource_create(client,
1368 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001369
1370 if (resource == NULL) {
1371 weston_log("couldn't add workspace manager object");
1372 return;
1373 }
1374
Jason Ekstranda85118c2013-06-27 20:17:02 -05001375 wl_resource_set_implementation(resource,
1376 &workspace_manager_implementation,
1377 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001378 wl_list_insert(&shell->workspaces.client_list,
1379 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001380
1381 workspace_manager_send_state(resource,
1382 shell->workspaces.current,
1383 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001384}
1385
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001386static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001387touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1388 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1389{
1390}
1391
1392static void
1393touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1394{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001395 struct weston_touch_move_grab *move =
1396 (struct weston_touch_move_grab *) container_of(
1397 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001398
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001399 if (grab->touch->seat->num_tp == 0) {
1400 shell_touch_grab_end(&move->base);
1401 free(move);
1402 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001403}
1404
1405static void
1406touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1407 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1408{
1409 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1410 struct shell_surface *shsurf = move->base.shsurf;
1411 struct weston_surface *es;
1412 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1413 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1414
1415 if (!shsurf)
1416 return;
1417
1418 es = shsurf->surface;
1419
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 weston_view_configure(shsurf->view, dx, dy,
1421 shsurf->view->geometry.width,
1422 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001423
1424 weston_compositor_schedule_repaint(es->compositor);
1425}
1426
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001427static void
1428touch_move_grab_cancel(struct weston_touch_grab *grab)
1429{
1430 struct weston_touch_move_grab *move =
1431 (struct weston_touch_move_grab *) container_of(
1432 grab, struct shell_touch_grab, grab);
1433
1434 shell_touch_grab_end(&move->base);
1435 free(move);
1436}
1437
Rusty Lynch1084da52013-08-15 09:10:08 -07001438static const struct weston_touch_grab_interface touch_move_grab_interface = {
1439 touch_move_grab_down,
1440 touch_move_grab_up,
1441 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001442 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001443};
1444
1445static int
1446surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1447{
1448 struct weston_touch_move_grab *move;
1449
1450 if (!shsurf)
1451 return -1;
1452
1453 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1454 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001455 if (shsurf->grabbed)
1456 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001457
1458 move = malloc(sizeof *move);
1459 if (!move)
1460 return -1;
1461
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001463 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001465 seat->touch->grab_y;
1466
1467 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1468 seat->touch);
1469
1470 return 0;
1471}
1472
1473static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001474noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001475{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001476}
1477
1478static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001479move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1480 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001481{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001482 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001483 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001484 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001485 int dx, dy;
1486
1487 weston_pointer_move(pointer, x, y);
1488 dx = wl_fixed_to_int(pointer->x + move->dx);
1489 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001490
1491 if (!shsurf)
1492 return;
1493
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494 weston_view_configure(shsurf->view, dx, dy,
1495 shsurf->view->geometry.width,
1496 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001497
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001499}
1500
1501static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001502move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001503 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001504{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001505 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1506 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001507 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001508 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001509
Daniel Stone4dbadb12012-05-30 16:31:51 +01001510 if (pointer->button_count == 0 &&
1511 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001512 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001513 free(grab);
1514 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001515}
1516
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001517static void
1518move_grab_cancel(struct weston_pointer_grab *grab)
1519{
1520 struct shell_grab *shell_grab =
1521 container_of(grab, struct shell_grab, grab);
1522
1523 shell_grab_end(shell_grab);
1524 free(grab);
1525}
1526
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001527static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001528 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001529 move_grab_motion,
1530 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001531 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001532};
1533
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001534static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001535surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001536{
1537 struct weston_move_grab *move;
1538
1539 if (!shsurf)
1540 return -1;
1541
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001542 if (shsurf->grabbed)
1543 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001544 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1545 return 0;
1546
1547 move = malloc(sizeof *move);
1548 if (!move)
1549 return -1;
1550
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001552 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001554 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001555
1556 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001557 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558
1559 return 0;
1560}
1561
1562static void
1563shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1564 struct wl_resource *seat_resource, uint32_t serial)
1565{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001566 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001567 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001568 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001570 if (seat->pointer &&
1571 seat->pointer->button_count > 0 &&
1572 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001574 if ((surface == shsurf->surface) &&
1575 (surface_move(shsurf, seat) < 0))
1576 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001577 } else if (seat->touch &&
1578 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001580 if ((surface == shsurf->surface) &&
1581 (surface_touch_move(shsurf, seat) < 0))
1582 wl_resource_post_no_memory(resource);
1583 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584}
1585
1586struct weston_resize_grab {
1587 struct shell_grab base;
1588 uint32_t edges;
1589 int32_t width, height;
1590};
1591
1592static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001593resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1594 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595{
1596 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001597 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598 struct shell_surface *shsurf = resize->base.shsurf;
1599 int32_t width, height;
1600 wl_fixed_t from_x, from_y;
1601 wl_fixed_t to_x, to_y;
1602
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001603 weston_pointer_move(pointer, x, y);
1604
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001605 if (!shsurf)
1606 return;
1607
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608 weston_view_from_global_fixed(shsurf->view,
1609 pointer->grab_x, pointer->grab_y,
1610 &from_x, &from_y);
1611 weston_view_from_global_fixed(shsurf->view,
1612 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001613
1614 width = resize->width;
1615 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1616 width += wl_fixed_to_int(from_x - to_x);
1617 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1618 width += wl_fixed_to_int(to_x - from_x);
1619 }
1620
1621 height = resize->height;
1622 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1623 height += wl_fixed_to_int(from_y - to_y);
1624 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1625 height += wl_fixed_to_int(to_y - from_y);
1626 }
1627
1628 shsurf->client->send_configure(shsurf->surface,
1629 resize->edges, width, height);
1630}
1631
1632static void
1633send_configure(struct weston_surface *surface,
1634 uint32_t edges, int32_t width, int32_t height)
1635{
1636 struct shell_surface *shsurf = get_shell_surface(surface);
1637
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001638 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001639 edges, width, height);
1640}
1641
1642static const struct weston_shell_client shell_client = {
1643 send_configure
1644};
1645
1646static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001647resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001648 uint32_t time, uint32_t button, uint32_t state_w)
1649{
1650 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001651 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001652 enum wl_pointer_button_state state = state_w;
1653
1654 if (pointer->button_count == 0 &&
1655 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1656 shell_grab_end(&resize->base);
1657 free(grab);
1658 }
1659}
1660
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001661static void
1662resize_grab_cancel(struct weston_pointer_grab *grab)
1663{
1664 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1665
1666 shell_grab_end(&resize->base);
1667 free(grab);
1668}
1669
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001670static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001671 noop_grab_focus,
1672 resize_grab_motion,
1673 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001674 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001675};
1676
Giulio Camuffob8366642013-04-25 13:57:46 +03001677/*
1678 * Returns the bounding box of a surface and all its sub-surfaces,
1679 * in the surface coordinates system. */
1680static void
1681surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1682 int32_t *y, int32_t *w, int32_t *h) {
1683 pixman_region32_t region;
1684 pixman_box32_t *box;
1685 struct weston_subsurface *subsurface;
1686
1687 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 surface->width,
1689 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001690
1691 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1692 pixman_region32_union_rect(&region, &region,
1693 subsurface->position.x,
1694 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 subsurface->surface->width,
1696 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001697 }
1698
1699 box = pixman_region32_extents(&region);
1700 if (x)
1701 *x = box->x1;
1702 if (y)
1703 *y = box->y1;
1704 if (w)
1705 *w = box->x2 - box->x1;
1706 if (h)
1707 *h = box->y2 - box->y1;
1708
1709 pixman_region32_fini(&region);
1710}
1711
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001712static int
1713surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001714 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715{
1716 struct weston_resize_grab *resize;
1717
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001718 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1719 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001720 return 0;
1721
1722 if (edges == 0 || edges > 15 ||
1723 (edges & 3) == 3 || (edges & 12) == 12)
1724 return 0;
1725
1726 resize = malloc(sizeof *resize);
1727 if (!resize)
1728 return -1;
1729
1730 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001731 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1732 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001733
1734 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001735 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736
1737 return 0;
1738}
1739
1740static void
1741shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1742 struct wl_resource *seat_resource, uint32_t serial,
1743 uint32_t edges)
1744{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001745 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001746 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001747 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001748
1749 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1750 return;
1751
Jason Ekstranda7af7042013-10-12 22:38:11 -05001752 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001753 if (seat->pointer->button_count == 0 ||
1754 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001755 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001756 return;
1757
Kristian Høgsberge3148752013-05-06 23:19:49 -04001758 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001759 wl_resource_post_no_memory(resource);
1760}
1761
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001762static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001763end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1764
1765static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001766busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001767{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001768 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001769 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001771 wl_fixed_t sx, sy;
1772
Jason Ekstranda7af7042013-10-12 22:38:11 -05001773 view = weston_compositor_pick_view(pointer->seat->compositor,
1774 pointer->x, pointer->y,
1775 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001776
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001778 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001779}
1780
1781static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001782busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1783 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001784{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001785 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001786}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001787
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001788static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001789busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001790 uint32_t time, uint32_t button, uint32_t state)
1791{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001792 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001793 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001794 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001795
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001796 if (shsurf && button == BTN_LEFT && state) {
1797 activate(shsurf->shell, shsurf->surface, seat);
1798 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001799 } else if (shsurf && button == BTN_RIGHT && state) {
1800 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001801 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001802 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803}
1804
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001805static void
1806busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1807{
1808 struct shell_grab *grab = (struct shell_grab *) base;
1809
1810 shell_grab_end(grab);
1811 free(grab);
1812}
1813
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001814static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001815 busy_cursor_grab_focus,
1816 busy_cursor_grab_motion,
1817 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001818 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001819};
1820
1821static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001822set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001823{
1824 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825
1826 grab = malloc(sizeof *grab);
1827 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001828 return;
1829
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001830 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1831 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001833
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001834static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001835end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836{
1837 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1838
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001839 if (grab->grab.interface == &busy_cursor_grab_interface &&
1840 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001841 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001842 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001843 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001844}
1845
Scott Moreau9521d5e2012-04-19 13:06:17 -06001846static void
1847ping_timer_destroy(struct shell_surface *shsurf)
1848{
1849 if (!shsurf || !shsurf->ping_timer)
1850 return;
1851
1852 if (shsurf->ping_timer->source)
1853 wl_event_source_remove(shsurf->ping_timer->source);
1854
1855 free(shsurf->ping_timer);
1856 shsurf->ping_timer = NULL;
1857}
1858
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001859static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001860ping_timeout_handler(void *data)
1861{
1862 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001863 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001864
Scott Moreau9521d5e2012-04-19 13:06:17 -06001865 /* Client is not responding */
1866 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001867
1868 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001870 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001871
1872 return 1;
1873}
1874
1875static void
1876ping_handler(struct weston_surface *surface, uint32_t serial)
1877{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001878 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001879 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001880 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001881
1882 if (!shsurf)
1883 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001884 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001885 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001886
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001887 if (shsurf->surface == shsurf->shell->grab_surface)
1888 return;
1889
Scott Moreauff1db4a2012-04-17 19:06:18 -06001890 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001891 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001892 if (!shsurf->ping_timer)
1893 return;
1894
1895 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001896 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1897 shsurf->ping_timer->source =
1898 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1899 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1900
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001901 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001902 }
1903}
1904
1905static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001906handle_pointer_focus(struct wl_listener *listener, void *data)
1907{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001908 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001909 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001910 struct weston_compositor *compositor;
1911 struct shell_surface *shsurf;
1912 uint32_t serial;
1913
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001915 return;
1916
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 compositor = view->surface->compositor;
1918 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001919
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001920 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001921 set_busy_cursor(shsurf, pointer);
1922 } else {
1923 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001924 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001925 }
1926}
1927
1928static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001929create_pointer_focus_listener(struct weston_seat *seat)
1930{
1931 struct wl_listener *listener;
1932
Kristian Høgsberge3148752013-05-06 23:19:49 -04001933 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001934 return;
1935
1936 listener = malloc(sizeof *listener);
1937 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001938 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001939}
1940
1941static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001942shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1943 uint32_t serial)
1944{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001945 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001946 struct weston_seat *seat;
1947 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001948
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001949 if (shsurf->ping_timer == NULL)
1950 /* Just ignore unsolicited pong. */
1951 return;
1952
Scott Moreauff1db4a2012-04-17 19:06:18 -06001953 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001954 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001955 wl_list_for_each(seat, &ec->seat_list, link) {
1956 if(seat->pointer)
1957 end_busy_cursor(shsurf, seat->pointer);
1958 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001959 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001960 }
1961}
1962
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001963static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001964set_title(struct shell_surface *shsurf, const char *title)
1965{
1966 free(shsurf->title);
1967 shsurf->title = strdup(title);
1968}
1969
1970static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001971shell_surface_set_title(struct wl_client *client,
1972 struct wl_resource *resource, const char *title)
1973{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001974 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001975
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001976 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001977}
1978
1979static void
1980shell_surface_set_class(struct wl_client *client,
1981 struct wl_resource *resource, const char *class)
1982{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001983 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001984
1985 free(shsurf->class);
1986 shsurf->class = strdup(class);
1987}
1988
Alex Wu4539b082012-03-01 12:57:46 +08001989static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001990restore_output_mode(struct weston_output *output)
1991{
Hardening57388e42013-09-18 23:56:36 +02001992 if (output->current_mode != output->original_mode ||
1993 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001994 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001995 output->original_mode,
1996 output->original_scale,
1997 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001998}
1999
2000static void
2001restore_all_output_modes(struct weston_compositor *compositor)
2002{
2003 struct weston_output *output;
2004
2005 wl_list_for_each(output, &compositor->output_list, link)
2006 restore_output_mode(output);
2007}
2008
2009static void
Alex Wu4539b082012-03-01 12:57:46 +08002010shell_unset_fullscreen(struct shell_surface *shsurf)
2011{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002012 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08002013 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08002014 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2015 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002016 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002017 }
Alex Wu4539b082012-03-01 12:57:46 +08002018 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2019 shsurf->fullscreen.framerate = 0;
2020 wl_list_remove(&shsurf->fullscreen.transform.link);
2021 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022 if (shsurf->fullscreen.black_view)
2023 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2024 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002025 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002026 weston_view_set_position(shsurf->view,
2027 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002028 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002030 &shsurf->rotation.transform.link);
2031 shsurf->saved_rotation_valid = false;
2032 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002033
2034 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 wl_list_remove(&shsurf->view->layer_link);
2036 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08002037}
2038
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002039static void
2040shell_unset_maximized(struct shell_surface *shsurf)
2041{
2042 struct workspace *ws;
2043 /* undo all maximized things here */
2044 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 weston_view_set_position(shsurf->view,
2046 shsurf->saved_x,
2047 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002048
2049 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 wl_list_insert(&shsurf->view->geometry.transformation_list,
2051 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002052 shsurf->saved_rotation_valid = false;
2053 }
2054
2055 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002056 wl_list_remove(&shsurf->view->layer_link);
2057 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002058}
2059
Pekka Paalanen98262232011-12-01 10:42:22 +02002060static int
2061reset_shell_surface_type(struct shell_surface *surface)
2062{
2063 switch (surface->type) {
2064 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08002065 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002066 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002067 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002068 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002069 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002070 case SHELL_SURFACE_NONE:
2071 case SHELL_SURFACE_TOPLEVEL:
2072 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002073 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002074 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02002075 break;
2076 }
2077
2078 surface->type = SHELL_SURFACE_NONE;
2079 return 0;
2080}
2081
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002082static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002083set_surface_type(struct shell_surface *shsurf)
2084{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002085 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002087
2088 reset_shell_surface_type(shsurf);
2089
2090 shsurf->type = shsurf->next_type;
2091 shsurf->next_type = SHELL_SURFACE_NONE;
2092
2093 switch (shsurf->type) {
2094 case SHELL_SURFACE_TOPLEVEL:
2095 break;
2096 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097 if (pev)
2098 weston_view_set_position(shsurf->view,
2099 pev->geometry.x + shsurf->transient.x,
2100 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002101 break;
2102
2103 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002104 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002105 shsurf->saved_x = shsurf->view->geometry.x;
2106 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002107 shsurf->saved_position_valid = true;
2108
2109 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2110 wl_list_remove(&shsurf->rotation.transform.link);
2111 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002113 shsurf->saved_rotation_valid = true;
2114 }
2115 break;
2116
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002117 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002118 weston_view_set_position(shsurf->view, shsurf->transient.x,
2119 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002120 break;
2121
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002122 default:
2123 break;
2124 }
2125}
2126
2127static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002128set_toplevel(struct shell_surface *shsurf)
2129{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002130 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002131}
2132
2133static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002134shell_surface_set_toplevel(struct wl_client *client,
2135 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002136{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002137 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002138
Tiago Vignattibc052c92012-04-19 16:18:18 +03002139 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002140}
2141
2142static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04002143set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002144 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04002145{
2146 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002147 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002148 shsurf->transient.x = x;
2149 shsurf->transient.y = y;
2150 shsurf->transient.flags = flags;
2151 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2152}
2153
2154static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002155shell_surface_set_transient(struct wl_client *client,
2156 struct wl_resource *resource,
2157 struct wl_resource *parent_resource,
2158 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002159{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002160 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002161 struct weston_surface *parent =
2162 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002163
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002164 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002165}
2166
Tiago Vignattibe143262012-04-16 17:31:41 +03002167static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002168shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002169{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002170 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002171}
2172
2173static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002174get_output_panel_height(struct desktop_shell *shell,
2175 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08002176{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08002178 int panel_height = 0;
2179
2180 if (!output)
2181 return 0;
2182
Jason Ekstranda7af7042013-10-12 22:38:11 -05002183 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2184 if (view->surface->output == output) {
2185 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08002186 break;
2187 }
2188 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002189
Juan Zhao96879df2012-02-07 08:45:41 +08002190 return panel_height;
2191}
2192
2193static void
2194shell_surface_set_maximized(struct wl_client *client,
2195 struct wl_resource *resource,
2196 struct wl_resource *output_resource )
2197{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002198 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08002199 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03002200 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08002201 uint32_t edges = 0, panel_height = 0;
2202
2203 /* get the default output, if the client set it as NULL
2204 check whether the ouput is available */
2205 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002206 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002207 else if (es->output)
2208 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002209 else
2210 shsurf->output = get_default_output(es->compositor);
2211
Tiago Vignattibe143262012-04-16 17:31:41 +03002212 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01002213 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002214 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002215
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002216 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002217 shsurf->output->width,
2218 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002219
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002220 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002221}
2222
Alex Wu21858432012-04-01 20:13:08 +08002223static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002224black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height);
Alex Wu21858432012-04-01 20:13:08 +08002225
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002227create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002228 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002229 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002230{
2231 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002233
2234 surface = weston_surface_create(ec);
2235 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002236 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002237 return NULL;
2238 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002239 view = weston_view_create(surface);
2240 if (surface == NULL) {
2241 weston_log("no memory\n");
2242 weston_surface_destroy(surface);
2243 return NULL;
2244 }
Alex Wu4539b082012-03-01 12:57:46 +08002245
Alex Wu21858432012-04-01 20:13:08 +08002246 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002247 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002248 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002249 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002250 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002251 pixman_region32_fini(&surface->input);
2252 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002253
Jason Ekstranda7af7042013-10-12 22:38:11 -05002254 weston_view_configure(view, x, y, w, h);
2255
2256 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002257}
2258
2259/* Create black surface and append it to the associated fullscreen surface.
2260 * Handle size dismatch and positioning according to the method. */
2261static void
2262shell_configure_fullscreen(struct shell_surface *shsurf)
2263{
2264 struct weston_output *output = shsurf->fullscreen_output;
2265 struct weston_surface *surface = shsurf->surface;
2266 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002267 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002268 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002269
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002270 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2271 restore_output_mode(output);
2272
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273 if (!shsurf->fullscreen.black_view)
2274 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002275 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002276 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002277 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002278 output->width,
2279 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002280
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2282 wl_list_insert(&shsurf->view->layer_link,
2283 &shsurf->fullscreen.black_view->layer_link);
2284 shsurf->fullscreen.black_view->surface->output = output;
2285 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002286
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002288 &surf_width, &surf_height);
2289
Alex Wu4539b082012-03-01 12:57:46 +08002290 switch (shsurf->fullscreen.type) {
2291 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002292 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002294 break;
2295 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002296 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002297 if (output->width == surf_width &&
2298 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002299 weston_view_set_position(shsurf->view,
2300 output->x - surf_x,
2301 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002302 break;
2303 }
2304
Alex Wu4539b082012-03-01 12:57:46 +08002305 matrix = &shsurf->fullscreen.transform.matrix;
2306 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002307
Scott Moreau1bad5db2012-08-18 01:04:05 -06002308 output_aspect = (float) output->width /
2309 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002310 /* XXX: Use surf_width and surf_height here? */
2311 surface_aspect = (float) surface->width /
2312 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002313 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002314 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002315 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002316 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002317 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002318 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002319
Alex Wu4539b082012-03-01 12:57:46 +08002320 weston_matrix_scale(matrix, scale, scale, 1);
2321 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002322 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002323 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002324 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2325 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002326 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002327
Alex Wu4539b082012-03-01 12:57:46 +08002328 break;
2329 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002330 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002331 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002332 surf_width * surface->buffer_scale,
2333 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002334 shsurf->fullscreen.framerate};
2335
Hardening57388e42013-09-18 23:56:36 +02002336 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2337 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002338 weston_view_set_position(shsurf->view,
2339 output->x - surf_x,
2340 output->y - surf_y);
2341 weston_view_configure(shsurf->fullscreen.black_view,
2342 output->x - surf_x,
2343 output->y - surf_y,
2344 output->width,
2345 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002346 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002347 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002348 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002350 }
Alex Wubd3354b2012-04-17 17:20:49 +08002351 }
Alex Wu4539b082012-03-01 12:57:46 +08002352 break;
2353 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002354 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002355 break;
2356 default:
2357 break;
2358 }
2359}
2360
2361/* make the fullscreen and black surface at the top */
2362static void
2363shell_stack_fullscreen(struct shell_surface *shsurf)
2364{
Alex Wubd3354b2012-04-17 17:20:49 +08002365 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002366 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002367
Jason Ekstranda7af7042013-10-12 22:38:11 -05002368 wl_list_remove(&shsurf->view->layer_link);
2369 wl_list_insert(&shell->fullscreen_layer.view_list,
2370 &shsurf->view->layer_link);
2371 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002372
Jason Ekstranda7af7042013-10-12 22:38:11 -05002373 if (!shsurf->fullscreen.black_view)
2374 shsurf->fullscreen.black_view =
2375 create_black_surface(shsurf->surface->compositor,
2376 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002377 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002378 output->width,
2379 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002380
Jason Ekstranda7af7042013-10-12 22:38:11 -05002381 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2382 wl_list_insert(&shsurf->view->layer_link,
2383 &shsurf->fullscreen.black_view->layer_link);
2384 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002385}
2386
2387static void
2388shell_map_fullscreen(struct shell_surface *shsurf)
2389{
Alex Wu4539b082012-03-01 12:57:46 +08002390 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002391 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002392}
2393
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002394static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002395set_fullscreen(struct shell_surface *shsurf,
2396 uint32_t method,
2397 uint32_t framerate,
2398 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002399{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002400 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002401
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002402 if (output)
2403 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002404 else if (es->output)
2405 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002406 else
2407 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002408
Alex Wu4539b082012-03-01 12:57:46 +08002409 shsurf->fullscreen_output = shsurf->output;
2410 shsurf->fullscreen.type = method;
2411 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002412 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002413
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002414 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002415 shsurf->output->width,
2416 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002417}
2418
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002419static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002420shell_surface_set_fullscreen(struct wl_client *client,
2421 struct wl_resource *resource,
2422 uint32_t method,
2423 uint32_t framerate,
2424 struct wl_resource *output_resource)
2425{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002426 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002427 struct weston_output *output;
2428
2429 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002430 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002431 else
2432 output = NULL;
2433
2434 set_fullscreen(shsurf, method, framerate, output);
2435}
2436
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002437static void
2438set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2439{
2440 /* XXX: using the same fields for transient type */
2441 shsurf->transient.x = x;
2442 shsurf->transient.y = y;
2443 shsurf->transient.flags = flags;
2444 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2445}
2446
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002447static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002448
2449static void
2450destroy_shell_seat(struct wl_listener *listener, void *data)
2451{
2452 struct shell_seat *shseat =
2453 container_of(listener,
2454 struct shell_seat, seat_destroy_listener);
2455 struct shell_surface *shsurf, *prev = NULL;
2456
2457 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002458 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002459 shseat->popup_grab.client = NULL;
2460
2461 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2462 shsurf->popup.shseat = NULL;
2463 if (prev) {
2464 wl_list_init(&prev->popup.grab_link);
2465 }
2466 prev = shsurf;
2467 }
2468 wl_list_init(&prev->popup.grab_link);
2469 }
2470
2471 wl_list_remove(&shseat->seat_destroy_listener.link);
2472 free(shseat);
2473}
2474
2475static struct shell_seat *
2476create_shell_seat(struct weston_seat *seat)
2477{
2478 struct shell_seat *shseat;
2479
2480 shseat = calloc(1, sizeof *shseat);
2481 if (!shseat) {
2482 weston_log("no memory to allocate shell seat\n");
2483 return NULL;
2484 }
2485
2486 shseat->seat = seat;
2487 wl_list_init(&shseat->popup_grab.surfaces_list);
2488
2489 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2490 wl_signal_add(&seat->destroy_signal,
2491 &shseat->seat_destroy_listener);
2492
2493 return shseat;
2494}
2495
2496static struct shell_seat *
2497get_shell_seat(struct weston_seat *seat)
2498{
2499 struct wl_listener *listener;
2500
2501 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2502 if (listener == NULL)
2503 return create_shell_seat(seat);
2504
2505 return container_of(listener,
2506 struct shell_seat, seat_destroy_listener);
2507}
2508
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002509static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002510popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002511{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002512 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002513 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002514 struct shell_seat *shseat =
2515 container_of(grab, struct shell_seat, popup_grab.grab);
2516 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002517 wl_fixed_t sx, sy;
2518
Jason Ekstranda7af7042013-10-12 22:38:11 -05002519 view = weston_compositor_pick_view(pointer->seat->compositor,
2520 pointer->x, pointer->y,
2521 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002522
Jason Ekstranda7af7042013-10-12 22:38:11 -05002523 if (view && view->surface->resource &&
2524 wl_resource_get_client(view->surface->resource) == client) {
2525 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002526 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002527 weston_pointer_set_focus(pointer, NULL,
2528 wl_fixed_from_int(0),
2529 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002530 }
2531}
2532
2533static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002534popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2535 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002536{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002537 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002538 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002539 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002540
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002541 weston_pointer_move(pointer, x, y);
2542
Neil Roberts96d790e2013-09-19 17:32:00 +01002543 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002544 weston_view_from_global_fixed(pointer->focus,
2545 pointer->x, pointer->y,
2546 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002547 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002548 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002549}
2550
2551static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002552popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002553 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002554{
2555 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002556 struct shell_seat *shseat =
2557 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002558 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002559 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002560 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002561 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002562
Neil Roberts96d790e2013-09-19 17:32:00 +01002563 resource_list = &grab->pointer->focus_resource_list;
2564 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002565 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002566 wl_resource_for_each(resource, resource_list) {
2567 wl_pointer_send_button(resource, serial,
2568 time, button, state);
2569 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002570 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002571 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002572 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002573 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002574 }
2575
Daniel Stone4dbadb12012-05-30 16:31:51 +01002576 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002577 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002578}
2579
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002580static void
2581popup_grab_cancel(struct weston_pointer_grab *grab)
2582{
2583 popup_grab_end(grab->pointer);
2584}
2585
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002586static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002587 popup_grab_focus,
2588 popup_grab_motion,
2589 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002590 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002591};
2592
2593static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002594popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002595{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002596 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002597 struct shell_seat *shseat =
2598 container_of(grab, struct shell_seat, popup_grab.grab);
2599 struct shell_surface *shsurf;
2600 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002601
2602 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002603 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002604 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002605 shseat->popup_grab.grab.interface = NULL;
2606 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002607 /* Send the popup_done event to all the popups open */
2608 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002609 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002610 shsurf->popup.shseat = NULL;
2611 if (prev) {
2612 wl_list_init(&prev->popup.grab_link);
2613 }
2614 prev = shsurf;
2615 }
2616 wl_list_init(&prev->popup.grab_link);
2617 wl_list_init(&shseat->popup_grab.surfaces_list);
2618 }
2619}
2620
2621static void
2622add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2623{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002624 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002625
2626 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002627 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002628 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002629 /* We must make sure here that this popup was opened after
2630 * a mouse press, and not just by moving around with other
2631 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002632 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002633 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002634
Rob Bradforddfe31052013-06-26 19:49:11 +01002635 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002636 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002637 } else {
2638 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002639 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002640}
2641
2642static void
2643remove_popup_grab(struct shell_surface *shsurf)
2644{
2645 struct shell_seat *shseat = shsurf->popup.shseat;
2646
2647 wl_list_remove(&shsurf->popup.grab_link);
2648 wl_list_init(&shsurf->popup.grab_link);
2649 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002650 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002651 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002652 }
2653}
2654
2655static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002656shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002657{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002658 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002659 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002660
Jason Ekstranda7af7042013-10-12 22:38:11 -05002661 shsurf->surface->output = parent_view->output;
2662 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002663
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 weston_view_set_transform_parent(shsurf->view, parent_view);
2665 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2666 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002667
Kristian Høgsberge3148752013-05-06 23:19:49 -04002668 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002669 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002670 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002671 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002672 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002673 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002674}
2675
2676static void
2677shell_surface_set_popup(struct wl_client *client,
2678 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002679 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002680 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002681 struct wl_resource *parent_resource,
2682 int32_t x, int32_t y, uint32_t flags)
2683{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002684 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002685
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002686 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002687 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002688 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002689 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002690 shsurf->popup.x = x;
2691 shsurf->popup.y = y;
2692}
2693
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002694static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002695 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002696 shell_surface_move,
2697 shell_surface_resize,
2698 shell_surface_set_toplevel,
2699 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002700 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002701 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002702 shell_surface_set_maximized,
2703 shell_surface_set_title,
2704 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002705};
2706
2707static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002708destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002709{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002710 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2711
Giulio Camuffo5085a752013-03-25 21:42:45 +01002712 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2713 remove_popup_grab(shsurf);
2714 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002715
Alex Wubd3354b2012-04-17 17:20:49 +08002716 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002717 shell_surface_is_top_fullscreen(shsurf))
2718 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002719
Jason Ekstranda7af7042013-10-12 22:38:11 -05002720 if (shsurf->fullscreen.black_view)
2721 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002722
Alex Wubd3354b2012-04-17 17:20:49 +08002723 /* As destroy_resource() use wl_list_for_each_safe(),
2724 * we can always remove the listener.
2725 */
2726 wl_list_remove(&shsurf->surface_destroy_listener.link);
2727 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002728 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002729 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002730
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731 weston_view_destroy(shsurf->view);
2732
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002733 wl_list_remove(&shsurf->link);
2734 free(shsurf);
2735}
2736
2737static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002738shell_destroy_shell_surface(struct wl_resource *resource)
2739{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002740 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002741
2742 destroy_shell_surface(shsurf);
2743}
2744
2745static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002746shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002747{
2748 struct shell_surface *shsurf = container_of(listener,
2749 struct shell_surface,
2750 surface_destroy_listener);
2751
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002752 if (shsurf->resource)
2753 wl_resource_destroy(shsurf->resource);
2754 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002755 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002756}
2757
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002758static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002759shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002760
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002761static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002762get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002763{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002764 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002765 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002766 else
2767 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002768}
2769
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002770static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002771create_shell_surface(void *shell, struct weston_surface *surface,
2772 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002773{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002774 struct shell_surface *shsurf;
2775
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002776 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002777 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002778 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002779 }
2780
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002781 shsurf = calloc(1, sizeof *shsurf);
2782 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002783 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002784 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002785 }
2786
Jason Ekstranda7af7042013-10-12 22:38:11 -05002787 shsurf->view = weston_view_create(surface);
2788 if (!shsurf->view) {
2789 weston_log("no memory to allocate shell surface\n");
2790 free(shsurf);
2791 return NULL;
2792 }
2793
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002794 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002795 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002796
Tiago Vignattibc052c92012-04-19 16:18:18 +03002797 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002798 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002799 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002800 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002801 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002802 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2803 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002804 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002805 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002806 wl_list_init(&shsurf->fullscreen.transform.link);
2807
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002808 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002809 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002810 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002811 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002812
2813 /* init link so its safe to always remove it in destroy_shell_surface */
2814 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002815 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002816
Pekka Paalanen460099f2012-01-20 16:48:25 +02002817 /* empty when not in use */
2818 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002819 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002820
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002821 wl_list_init(&shsurf->workspace_transform.link);
2822
Pekka Paalanen98262232011-12-01 10:42:22 +02002823 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002824 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002825
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002826 shsurf->client = client;
2827
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002828 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002829}
2830
Jason Ekstranda7af7042013-10-12 22:38:11 -05002831static struct weston_view *
2832get_primary_view(void *shell, struct shell_surface *shsurf)
2833{
2834 return shsurf->view;
2835}
2836
Tiago Vignattibc052c92012-04-19 16:18:18 +03002837static void
2838shell_get_shell_surface(struct wl_client *client,
2839 struct wl_resource *resource,
2840 uint32_t id,
2841 struct wl_resource *surface_resource)
2842{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002843 struct weston_surface *surface =
2844 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002845 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002846 struct shell_surface *shsurf;
2847
2848 if (get_shell_surface(surface)) {
2849 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002850 WL_DISPLAY_ERROR_INVALID_OBJECT,
2851 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002852 return;
2853 }
2854
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002855 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002856 if (!shsurf) {
2857 wl_resource_post_error(surface_resource,
2858 WL_DISPLAY_ERROR_INVALID_OBJECT,
2859 "surface->configure already set");
2860 return;
2861 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002862
Jason Ekstranda85118c2013-06-27 20:17:02 -05002863 shsurf->resource =
2864 wl_resource_create(client,
2865 &wl_shell_surface_interface, 1, id);
2866 wl_resource_set_implementation(shsurf->resource,
2867 &shell_surface_implementation,
2868 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002869}
2870
2871static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002872 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002873};
2874
Kristian Høgsberg07937562011-04-12 17:25:42 -04002875static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002876shell_fade(struct desktop_shell *shell, enum fade_type type);
2877
2878static int
2879screensaver_timeout(void *data)
2880{
2881 struct desktop_shell *shell = data;
2882
2883 shell_fade(shell, FADE_OUT);
2884
2885 return 1;
2886}
2887
2888static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002889handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002890{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002891 struct desktop_shell *shell =
2892 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002893
Pekka Paalanen18027e52011-12-02 16:31:49 +02002894 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002895
2896 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002897 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002898}
2899
2900static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002901launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002902{
2903 if (shell->screensaver.binding)
2904 return;
2905
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002906 if (!shell->screensaver.path) {
2907 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002908 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002909 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002910
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002911 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002912 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002913 return;
2914 }
2915
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002916 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002917 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002918 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002919 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002920}
2921
2922static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002923terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002924{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002925 if (shell->screensaver.process.pid == 0)
2926 return;
2927
2928 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002929}
2930
2931static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002932configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002933{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002935
Giulio Camuffo184df502013-02-21 11:29:21 +01002936 if (width == 0)
2937 return;
2938
Jason Ekstranda7af7042013-10-12 22:38:11 -05002939 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2940 if (v->output == ev->output && v != ev) {
2941 weston_view_unmap(v);
2942 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002943 }
2944 }
2945
Jason Ekstranda7af7042013-10-12 22:38:11 -05002946 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002947
Jason Ekstranda7af7042013-10-12 22:38:11 -05002948 if (wl_list_empty(&ev->layer_link)) {
2949 wl_list_insert(&layer->view_list, &ev->layer_link);
2950 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002951 }
2952}
2953
2954static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002955background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002956{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002957 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002958 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002959
Jason Ekstranda7af7042013-10-12 22:38:11 -05002960 view = container_of(es->views.next, struct weston_view, surface_link);
2961
2962 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002963}
2964
2965static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002966desktop_shell_set_background(struct wl_client *client,
2967 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002968 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002969 struct wl_resource *surface_resource)
2970{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002971 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002972 struct weston_surface *surface =
2973 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002974 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002975
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002976 if (surface->configure) {
2977 wl_resource_post_error(surface_resource,
2978 WL_DISPLAY_ERROR_INVALID_OBJECT,
2979 "surface role already assigned");
2980 return;
2981 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002982
Jason Ekstranda7af7042013-10-12 22:38:11 -05002983 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2984 weston_view_destroy(view);
2985 view = weston_view_create(surface);
2986
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002987 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002988 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002989 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002990 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002991 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002992 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002993 surface->output->width,
2994 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002995}
2996
2997static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002998panel_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002999{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003000 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003001 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003002
Jason Ekstranda7af7042013-10-12 22:38:11 -05003003 view = container_of(es->views.next, struct weston_view, surface_link);
3004
3005 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003006}
3007
3008static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003009desktop_shell_set_panel(struct wl_client *client,
3010 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003011 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003012 struct wl_resource *surface_resource)
3013{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003014 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003015 struct weston_surface *surface =
3016 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003017 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003018
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003019 if (surface->configure) {
3020 wl_resource_post_error(surface_resource,
3021 WL_DISPLAY_ERROR_INVALID_OBJECT,
3022 "surface role already assigned");
3023 return;
3024 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003025
Jason Ekstranda7af7042013-10-12 22:38:11 -05003026 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3027 weston_view_destroy(view);
3028 view = weston_view_create(surface);
3029
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003030 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003031 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003032 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003033 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003034 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003035 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003036 surface->output->width,
3037 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003038}
3039
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003040static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003041lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003042{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003043 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003044 struct weston_view *view;
3045
3046 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003047
Giulio Camuffo184df502013-02-21 11:29:21 +01003048 if (width == 0)
3049 return;
3050
Jason Ekstranda7af7042013-10-12 22:38:11 -05003051 surface->width = width;
3052 surface->height = height;
3053 view->geometry.width = width;
3054 view->geometry.height = height;
3055 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003056
3057 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003058 wl_list_insert(&shell->lock_layer.view_list,
3059 &view->layer_link);
3060 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003061 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003062 }
3063}
3064
3065static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003066handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003067{
Tiago Vignattibe143262012-04-16 17:31:41 +03003068 struct desktop_shell *shell =
3069 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003070
Martin Minarik6d118362012-06-07 18:01:59 +02003071 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003072 shell->lock_surface = NULL;
3073}
3074
3075static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003076desktop_shell_set_lock_surface(struct wl_client *client,
3077 struct wl_resource *resource,
3078 struct wl_resource *surface_resource)
3079{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003080 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003081 struct weston_surface *surface =
3082 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003083
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003084 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003085
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003086 if (!shell->locked)
3087 return;
3088
Pekka Paalanen98262232011-12-01 10:42:22 +02003089 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003090
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003091 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003092 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003093 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003094
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003095 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003096 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003097 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003098}
3099
3100static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003101resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003102{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003103 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003104
Pekka Paalanen77346a62011-11-30 16:26:35 +02003105 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003106
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003107 wl_list_remove(&shell->lock_layer.link);
3108 wl_list_insert(&shell->compositor->cursor_layer.link,
3109 &shell->fullscreen_layer.link);
3110 wl_list_insert(&shell->fullscreen_layer.link,
3111 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003112 if (shell->showing_input_panels) {
3113 wl_list_insert(&shell->panel_layer.link,
3114 &shell->input_panel_layer.link);
3115 wl_list_insert(&shell->input_panel_layer.link,
3116 &ws->layer.link);
3117 } else {
3118 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3119 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003120
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003121 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003122
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003123 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003124 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003125 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003126}
3127
3128static void
3129desktop_shell_unlock(struct wl_client *client,
3130 struct wl_resource *resource)
3131{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003132 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003133
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003134 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003135
3136 if (shell->locked)
3137 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003138}
3139
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003140static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003141desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003142 struct wl_resource *resource,
3143 struct wl_resource *surface_resource)
3144{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003145 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003146
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003147 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003148 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003149}
3150
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003151static void
3152desktop_shell_desktop_ready(struct wl_client *client,
3153 struct wl_resource *resource)
3154{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003155 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003156
3157 shell_fade_startup(shell);
3158}
3159
Kristian Høgsberg75840622011-09-06 13:48:16 -04003160static const struct desktop_shell_interface desktop_shell_implementation = {
3161 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003162 desktop_shell_set_panel,
3163 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003164 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003165 desktop_shell_set_grab_surface,
3166 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003167};
3168
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003169static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003170get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003171{
3172 struct shell_surface *shsurf;
3173
3174 shsurf = get_shell_surface(surface);
3175 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003176 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003177 return shsurf->type;
3178}
3179
Kristian Høgsberg75840622011-09-06 13:48:16 -04003180static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003181move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003182{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003183 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003184 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003185 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003186
Pekka Paalanen01388e22013-04-25 13:57:44 +03003187 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003188 if (surface == NULL)
3189 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003190
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003191 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003192 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3193 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003194 return;
3195
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003196 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003197}
3198
3199static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003200touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3201{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003202 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003203 struct weston_surface *surface;
3204 struct shell_surface *shsurf;
3205
3206 surface = weston_surface_get_main_surface(focus);
3207 if (surface == NULL)
3208 return;
3209
3210 shsurf = get_shell_surface(surface);
3211 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3212 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3213 return;
3214
3215 surface_touch_move(shsurf, (struct weston_seat *) seat);
3216}
3217
3218static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003219resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003220{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003221 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003222 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003223 uint32_t edges = 0;
3224 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003225 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003226
Pekka Paalanen01388e22013-04-25 13:57:44 +03003227 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003228 if (surface == NULL)
3229 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003230
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003231 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003232 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3233 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003234 return;
3235
Jason Ekstranda7af7042013-10-12 22:38:11 -05003236 weston_view_from_global(shsurf->view,
3237 wl_fixed_to_int(seat->pointer->grab_x),
3238 wl_fixed_to_int(seat->pointer->grab_y),
3239 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003240
Jason Ekstranda7af7042013-10-12 22:38:11 -05003241 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003242 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003243 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003244 edges |= 0;
3245 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003246 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003247
Jason Ekstranda7af7042013-10-12 22:38:11 -05003248 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003249 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003250 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003251 edges |= 0;
3252 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003253 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003254
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003255 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003256}
3257
3258static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003259surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003260 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003261{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003262 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003263 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003264 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003265 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003266
Pekka Paalanen01388e22013-04-25 13:57:44 +03003267 /* XXX: broken for windows containing sub-surfaces */
3268 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003269 if (surface == NULL)
3270 return;
3271
3272 shsurf = get_shell_surface(surface);
3273 if (!shsurf)
3274 return;
3275
Jason Ekstranda7af7042013-10-12 22:38:11 -05003276 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003277
Jason Ekstranda7af7042013-10-12 22:38:11 -05003278 if (shsurf->view->alpha > 1.0)
3279 shsurf->view->alpha = 1.0;
3280 if (shsurf->view->alpha < step)
3281 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003282
Jason Ekstranda7af7042013-10-12 22:38:11 -05003283 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003284 weston_surface_damage(surface);
3285}
3286
3287static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003288do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003289 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003290{
Daniel Stone37816df2012-05-16 18:45:18 +01003291 struct weston_seat *ws = (struct weston_seat *) seat;
3292 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003293 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003294 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003295
3296 wl_list_for_each(output, &compositor->output_list, link) {
3297 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003298 wl_fixed_to_double(seat->pointer->x),
3299 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003300 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003301 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003302 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003303 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003304 increment = -output->zoom.increment;
3305 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003306 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003307 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003308 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003309 else
3310 increment = 0;
3311
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003312 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003313
Scott Moreaue6603982012-06-11 13:07:51 -06003314 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003315 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003316 else if (output->zoom.level > output->zoom.max_level)
3317 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003318 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003319 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003320 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003321
Scott Moreaue6603982012-06-11 13:07:51 -06003322 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003323
Jason Ekstranda7af7042013-10-12 22:38:11 -05003324 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003325 }
3326 }
3327}
3328
Scott Moreauccbf29d2012-02-22 14:21:41 -07003329static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003330zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003331 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003332{
3333 do_zoom(seat, time, 0, axis, value);
3334}
3335
3336static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003337zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003338 void *data)
3339{
3340 do_zoom(seat, time, key, 0, 0);
3341}
3342
3343static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003344terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003345 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003346{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003347 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003348
Daniel Stone325fc2d2012-05-30 16:31:58 +01003349 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003350}
3351
3352static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003353lower_fullscreen_layer(struct desktop_shell *shell);
3354
3355struct alt_tab {
3356 struct desktop_shell *shell;
3357 struct weston_keyboard_grab grab;
3358
3359 struct wl_list *current;
3360
3361 struct wl_list preview_list;
3362};
3363
3364struct alt_tab_preview {
3365 struct alt_tab *alt_tab;
3366
3367 struct weston_view *view;
3368 struct weston_transform transform;
3369
3370 struct wl_listener listener;
3371
3372 struct wl_list link;
3373};
3374
3375static void
3376alt_tab_next(struct alt_tab *alt_tab)
3377{
3378 alt_tab->current = alt_tab->current->next;
3379
3380 /* Make sure we're not pointing to the list header e.g. after
3381 * cycling through the whole list. */
3382 if (alt_tab->current->next == alt_tab->preview_list.next)
3383 alt_tab->current = alt_tab->current->next;
3384}
3385
3386static void
3387alt_tab_destroy(struct alt_tab *alt_tab)
3388{
3389 struct alt_tab_preview *preview, *next;
3390 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3391
3392 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3393 preview = wl_container_of(alt_tab->current, preview, link);
3394
3395 activate(alt_tab->shell, preview->view->surface,
3396 (struct weston_seat *) keyboard->seat);
3397 }
3398
3399 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3400 wl_list_remove(&preview->link);
3401 wl_list_remove(&preview->listener.link);
3402 weston_view_destroy(preview->view);
3403 free(preview);
3404 }
3405
3406 weston_keyboard_end_grab(keyboard);
3407 if (keyboard->input_method_resource)
3408 keyboard->grab = &keyboard->input_method_grab;
3409
3410 free(alt_tab);
3411}
3412
3413static void
3414alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3415{
3416 struct alt_tab_preview *preview =
3417 container_of(listener, struct alt_tab_preview, listener);
3418 struct alt_tab *alt_tab = preview->alt_tab;
3419 int advance = 0;
3420
3421 /* If the preview that we're removing is the currently selected one,
3422 * we want to move to the next one. So we move to ->prev and then
3423 * call _next() after removing the preview. */
3424 if (alt_tab->current == &preview->link) {
3425 alt_tab->current = alt_tab->current->prev;
3426 advance = 1;
3427 }
3428
3429 wl_list_remove(&preview->listener.link);
3430 wl_list_remove(&preview->link);
3431
3432 free(preview);
3433
3434 if (advance)
3435 alt_tab_next(alt_tab);
3436
3437 /* If the last preview goes away, stop the alt-tab */
3438 if (wl_list_empty(alt_tab->current))
3439 alt_tab_destroy(alt_tab);
3440}
3441
3442static void
3443alt_tab_key(struct weston_keyboard_grab *grab,
3444 uint32_t time, uint32_t key, uint32_t state_w)
3445{
3446 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3447 enum wl_keyboard_key_state state = state_w;
3448
3449 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3450 alt_tab_next(alt_tab);
3451}
3452
3453static void
3454alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3455 uint32_t mods_depressed, uint32_t mods_latched,
3456 uint32_t mods_locked, uint32_t group)
3457{
3458 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3459 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3460
3461 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3462 alt_tab_destroy(alt_tab);
3463}
3464
3465static void
3466alt_tab_cancel(struct weston_keyboard_grab *grab)
3467{
3468 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3469
3470 alt_tab_destroy(alt_tab);
3471}
3472
3473static const struct weston_keyboard_grab_interface alt_tab_grab = {
3474 alt_tab_key,
3475 alt_tab_modifier,
3476 alt_tab_cancel,
3477};
3478
3479static int
3480view_for_alt_tab(struct weston_view *view)
3481{
3482 if (!get_shell_surface(view->surface))
3483 return 0;
3484
3485 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3486 return 0;
3487
3488 if (view != get_default_view(view->surface))
3489 return 0;
3490
3491 return 1;
3492}
3493
3494static void
3495alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3496 void *data)
3497{
3498 struct alt_tab *alt_tab;
3499 struct desktop_shell *shell = data;
3500 struct weston_output *output = get_default_output(shell->compositor);
3501 struct workspace *ws;
3502 struct weston_view *view;
3503 int num_surfaces = 0;
3504 int x, y, side, margin;
3505
3506 wl_list_for_each(view, &shell->compositor->view_list, link) {
3507
3508 if (view_for_alt_tab(view))
3509 num_surfaces++;
3510 }
3511
3512 if (!num_surfaces)
3513 return;
3514
3515 alt_tab = malloc(sizeof *alt_tab);
3516 if (!alt_tab)
3517 return;
3518
3519 alt_tab->shell = shell;
3520 wl_list_init(&alt_tab->preview_list);
3521 alt_tab->current = &alt_tab->preview_list;
3522
3523 restore_all_output_modes(shell->compositor);
3524 lower_fullscreen_layer(alt_tab->shell);
3525
3526 alt_tab->grab.interface = &alt_tab_grab;
3527 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3528 weston_keyboard_set_focus(seat->keyboard, NULL);
3529
3530 ws = get_current_workspace(shell);
3531
3532 /* FIXME: add some visual candy e.g. prelight the selected view
3533 * and/or add a black surrounding background à la gnome-shell */
3534
3535 /* Determine the size for each preview */
3536 side = output->width / num_surfaces;
3537 if (side > 200)
3538 side = 200;
3539 margin = side / 4;
3540 side -= margin;
3541
3542 x = margin;
3543 y = (output->height - side) / 2;
3544
3545 /* Create a view for each surface */
3546 wl_list_for_each(view, &shell->compositor->view_list, link) {
3547 struct alt_tab_preview *preview;
3548 struct weston_view *v;
3549 float scale;
3550
3551 if (!view_for_alt_tab(view))
3552 continue;
3553
3554 preview = malloc(sizeof *preview);
3555 if (!preview) {
3556 alt_tab_destroy(alt_tab);
3557 return;
3558 }
3559
3560 preview->alt_tab = alt_tab;
3561
3562 preview->view = v = weston_view_create(view->surface);
3563 v->output = view->output;
3564 weston_view_restack(v, &ws->layer.view_list);
3565 weston_view_configure(v, x, y, view->geometry.width, view->geometry.height);
3566
3567 preview->listener.notify = alt_tab_handle_surface_destroy;
3568 wl_signal_add(&v->destroy_signal, &preview->listener);
3569
3570 if (view->geometry.width > view->geometry.height)
3571 scale = side / (float) view->geometry.width;
3572 else
3573 scale = side / (float) view->geometry.height;
3574
3575 wl_list_insert(&v->geometry.transformation_list,
3576 &preview->transform.link);
3577 weston_matrix_init(&preview->transform.matrix);
3578 weston_matrix_scale(&preview->transform.matrix,
3579 scale, scale, 1.0f);
3580
3581 weston_view_geometry_dirty(v);
3582 weston_compositor_schedule_repaint(v->surface->compositor);
3583
3584 /* Insert at the end of the list */
3585 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3586
3587 x += side + margin;
3588 }
3589
3590 /* Start at the second preview so a simple <alt>tab changes window.
3591 * We set `current' to the first preview and not the second because
3592 * we're going to receive a key press callback for the initial
3593 * <alt>tab which will make `current' point to the second element. */
3594 alt_tab_next(alt_tab);
3595}
3596
3597static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003598rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3599 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003600{
3601 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003602 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003603 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003604 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003605 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003606
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003607 weston_pointer_move(pointer, x, y);
3608
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003609 if (!shsurf)
3610 return;
3611
Jason Ekstranda7af7042013-10-12 22:38:11 -05003612 cx = 0.5f * shsurf->view->geometry.width;
3613 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003614
Daniel Stone37816df2012-05-16 18:45:18 +01003615 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3616 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003617 r = sqrtf(dx * dx + dy * dy);
3618
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003619 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003620 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003621
3622 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003623 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003624 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003625
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003626 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003627 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003628
3629 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003630 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003631 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003632 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003633 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003634
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003635 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003636 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003637 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003638 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003639 wl_list_init(&shsurf->rotation.transform.link);
3640 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003641 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003642 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003643
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003644 /* We need to adjust the position of the surface
3645 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003646 cposx = shsurf->view->geometry.x + cx;
3647 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003648 dposx = rotate->center.x - cposx;
3649 dposy = rotate->center.y - cposy;
3650 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003651 weston_view_set_position(shsurf->view,
3652 shsurf->view->geometry.x + dposx,
3653 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003654 }
3655
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003656 /* Repaint implies weston_surface_update_transform(), which
3657 * lazily applies the damage due to rotation update.
3658 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003659 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003660}
3661
3662static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003663rotate_grab_button(struct weston_pointer_grab *grab,
3664 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003665{
3666 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003667 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003668 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003669 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003670 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003671
Daniel Stone4dbadb12012-05-30 16:31:51 +01003672 if (pointer->button_count == 0 &&
3673 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003674 if (shsurf)
3675 weston_matrix_multiply(&shsurf->rotation.rotation,
3676 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003677 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003678 free(rotate);
3679 }
3680}
3681
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003682static void
3683rotate_grab_cancel(struct weston_pointer_grab *grab)
3684{
3685 struct rotate_grab *rotate =
3686 container_of(grab, struct rotate_grab, base.grab);
3687
3688 shell_grab_end(&rotate->base);
3689 free(rotate);
3690}
3691
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003692static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003693 noop_grab_focus,
3694 rotate_grab_motion,
3695 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003696 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003697};
3698
3699static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003700surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003701{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003702 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003703 float dx, dy;
3704 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003705
Pekka Paalanen460099f2012-01-20 16:48:25 +02003706 rotate = malloc(sizeof *rotate);
3707 if (!rotate)
3708 return;
3709
Jason Ekstranda7af7042013-10-12 22:38:11 -05003710 weston_view_to_global_float(surface->view,
3711 surface->view->geometry.width * 0.5f,
3712 surface->view->geometry.height * 0.5f,
3713 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003714
Daniel Stone37816df2012-05-16 18:45:18 +01003715 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3716 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003717 r = sqrtf(dx * dx + dy * dy);
3718 if (r > 20.0f) {
3719 struct weston_matrix inverse;
3720
3721 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003722 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003723 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003724
3725 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003726 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003727 } else {
3728 weston_matrix_init(&surface->rotation.rotation);
3729 weston_matrix_init(&rotate->rotation);
3730 }
3731
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003732 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3733 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003734}
3735
3736static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003737rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003738 void *data)
3739{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003740 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003741 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003742 struct shell_surface *surface;
3743
Pekka Paalanen01388e22013-04-25 13:57:44 +03003744 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003745 if (base_surface == NULL)
3746 return;
3747
3748 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003749 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3750 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003751 return;
3752
3753 surface_rotate(surface, seat);
3754}
3755
3756static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003757lower_fullscreen_layer(struct desktop_shell *shell)
3758{
3759 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003760 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003761
3762 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003763 wl_list_for_each_reverse_safe(view, prev,
3764 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003765 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003766 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003767}
3768
3769static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003770activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003771 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003772{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003773 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003774 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003775 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003776 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003777 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003778
Pekka Paalanen01388e22013-04-25 13:57:44 +03003779 main_surface = weston_surface_get_main_surface(es);
3780
Daniel Stone37816df2012-05-16 18:45:18 +01003781 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003782
Jonas Ådahl8538b222012-08-29 22:13:03 +02003783 state = ensure_focus_state(shell, seat);
3784 if (state == NULL)
3785 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003786
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003787 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003788 state->keyboard_focus = es;
3789 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003790 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003791
Pekka Paalanen01388e22013-04-25 13:57:44 +03003792 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003793 case SHELL_SURFACE_FULLSCREEN:
3794 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003795 shell_stack_fullscreen(get_shell_surface(main_surface));
3796 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003797 return;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003798 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003799 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003800 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003801 main_view = get_default_view(main_surface);
3802 if (main_view)
3803 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003804 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003805 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003806
3807 if (shell->focus_animation_type != ANIMATION_NONE)
3808 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003809}
3810
Alex Wu21858432012-04-01 20:13:08 +08003811/* no-op func for checking black surface */
3812static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003813black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Alex Wu21858432012-04-01 20:13:08 +08003814{
3815}
3816
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003817static bool
Alex Wu21858432012-04-01 20:13:08 +08003818is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3819{
3820 if (es->configure == black_surface_configure) {
3821 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003822 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003823 return true;
3824 }
3825 return false;
3826}
3827
Kristian Høgsberg75840622011-09-06 13:48:16 -04003828static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003829activate_binding(struct weston_seat *seat,
3830 struct desktop_shell *shell,
3831 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003832{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003833 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003834
Alex Wu9c35e6b2012-03-05 14:13:13 +08003835 if (!focus)
3836 return;
3837
Pekka Paalanen01388e22013-04-25 13:57:44 +03003838 if (is_black_surface(focus, &main_surface))
3839 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003840
Pekka Paalanen01388e22013-04-25 13:57:44 +03003841 main_surface = weston_surface_get_main_surface(focus);
3842 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003843 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003844
Neil Robertsa28c6932013-10-03 16:43:04 +01003845 activate(shell, focus, seat);
3846}
3847
3848static void
3849click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3850 void *data)
3851{
3852 if (seat->pointer->grab != &seat->pointer->default_grab)
3853 return;
3854
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003855 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003856}
3857
3858static void
3859touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3860{
3861 if (seat->touch->grab != &seat->touch->default_grab)
3862 return;
3863
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003864 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003865}
3866
3867static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003868lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003869{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003870 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003871
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003872 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003873 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003874 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003875 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003876
3877 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003878
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003879 /* Hide all surfaces by removing the fullscreen, panel and
3880 * toplevel layers. This way nothing else can show or receive
3881 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003882
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003883 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003884 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003885 if (shell->showing_input_panels)
3886 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003887 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003888 wl_list_insert(&shell->compositor->cursor_layer.link,
3889 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003890
Pekka Paalanen77346a62011-11-30 16:26:35 +02003891 launch_screensaver(shell);
3892
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003893 /* TODO: disable bindings that should not work while locked. */
3894
3895 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003896}
3897
3898static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003899unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003900{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003901 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003902 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003903 return;
3904 }
3905
3906 /* If desktop-shell client has gone away, unlock immediately. */
3907 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003908 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003909 return;
3910 }
3911
3912 if (shell->prepare_event_sent)
3913 return;
3914
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003915 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003916 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003917}
3918
3919static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003920shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003921{
3922 struct desktop_shell *shell = data;
3923
3924 shell->fade.animation = NULL;
3925
3926 switch (shell->fade.type) {
3927 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003928 weston_surface_destroy(shell->fade.view->surface);
3929 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003930 break;
3931 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003932 lock(shell);
3933 break;
3934 }
3935}
3936
Jason Ekstranda7af7042013-10-12 22:38:11 -05003937static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003938shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003939{
3940 struct weston_compositor *compositor = shell->compositor;
3941 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003942 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003943
3944 surface = weston_surface_create(compositor);
3945 if (!surface)
3946 return NULL;
3947
Jason Ekstranda7af7042013-10-12 22:38:11 -05003948 view = weston_view_create(surface);
3949 if (!view) {
3950 weston_surface_destroy(surface);
3951 return NULL;
3952 }
3953
3954 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003955 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003956 wl_list_insert(&compositor->fade_layer.view_list,
3957 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003958 pixman_region32_init(&surface->input);
3959
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003961}
3962
3963static void
3964shell_fade(struct desktop_shell *shell, enum fade_type type)
3965{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003966 float tint;
3967
3968 switch (type) {
3969 case FADE_IN:
3970 tint = 0.0;
3971 break;
3972 case FADE_OUT:
3973 tint = 1.0;
3974 break;
3975 default:
3976 weston_log("shell: invalid fade type\n");
3977 return;
3978 }
3979
3980 shell->fade.type = type;
3981
Jason Ekstranda7af7042013-10-12 22:38:11 -05003982 if (shell->fade.view == NULL) {
3983 shell->fade.view = shell_fade_create_surface(shell);
3984 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003985 return;
3986
Jason Ekstranda7af7042013-10-12 22:38:11 -05003987 shell->fade.view->alpha = 1.0 - tint;
3988 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003989 }
3990
3991 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003992 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003993 else
3994 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003995 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003996 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003997 shell_fade_done, shell);
3998}
3999
4000static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004001do_shell_fade_startup(void *data)
4002{
4003 struct desktop_shell *shell = data;
4004
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004005 if (shell->startup_animation_type == ANIMATION_FADE)
4006 shell_fade(shell, FADE_IN);
4007 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004008 weston_surface_destroy(shell->fade.view->surface);
4009 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004010 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004011}
4012
4013static void
4014shell_fade_startup(struct desktop_shell *shell)
4015{
4016 struct wl_event_loop *loop;
4017
4018 if (!shell->fade.startup_timer)
4019 return;
4020
4021 wl_event_source_remove(shell->fade.startup_timer);
4022 shell->fade.startup_timer = NULL;
4023
4024 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4025 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4026}
4027
4028static int
4029fade_startup_timeout(void *data)
4030{
4031 struct desktop_shell *shell = data;
4032
4033 shell_fade_startup(shell);
4034 return 0;
4035}
4036
4037static void
4038shell_fade_init(struct desktop_shell *shell)
4039{
4040 /* Make compositor output all black, and wait for the desktop-shell
4041 * client to signal it is ready, then fade in. The timer triggers a
4042 * fade-in, in case the desktop-shell client takes too long.
4043 */
4044
4045 struct wl_event_loop *loop;
4046
Jason Ekstranda7af7042013-10-12 22:38:11 -05004047 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004048 weston_log("%s: warning: fade surface already exists\n",
4049 __func__);
4050 return;
4051 }
4052
Jason Ekstranda7af7042013-10-12 22:38:11 -05004053 shell->fade.view = shell_fade_create_surface(shell);
4054 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004055 return;
4056
Jason Ekstranda7af7042013-10-12 22:38:11 -05004057 weston_view_update_transform(shell->fade.view);
4058 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004059
4060 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4061 shell->fade.startup_timer =
4062 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4063 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4064}
4065
4066static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004067idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004068{
4069 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004070 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004071
4072 shell_fade(shell, FADE_OUT);
4073 /* lock() is called from shell_fade_done() */
4074}
4075
4076static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004077wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004078{
4079 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004080 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004081
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004082 unlock(shell);
4083}
4084
4085static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004086show_input_panels(struct wl_listener *listener, void *data)
4087{
4088 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004089 container_of(listener, struct desktop_shell,
4090 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004091 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004092
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004093 shell->text_input.surface = (struct weston_surface*)data;
4094
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004095 if (shell->showing_input_panels)
4096 return;
4097
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004098 shell->showing_input_panels = true;
4099
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004100 if (!shell->locked)
4101 wl_list_insert(&shell->panel_layer.link,
4102 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004103
Jason Ekstranda7af7042013-10-12 22:38:11 -05004104 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004105 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004106 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004107 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004108 wl_list_insert(&shell->input_panel_layer.view_list,
4109 &ipsurf->view->layer_link);
4110 weston_view_geometry_dirty(ipsurf->view);
4111 weston_view_update_transform(ipsurf->view);
4112 weston_surface_damage(ipsurf->surface);
4113 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
4114 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004115 }
4116}
4117
4118static void
4119hide_input_panels(struct wl_listener *listener, void *data)
4120{
4121 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004122 container_of(listener, struct desktop_shell,
4123 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004124 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004125
Jan Arne Petersen61381972013-01-31 15:52:21 +01004126 if (!shell->showing_input_panels)
4127 return;
4128
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004129 shell->showing_input_panels = false;
4130
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004131 if (!shell->locked)
4132 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004133
Jason Ekstranda7af7042013-10-12 22:38:11 -05004134 wl_list_for_each_safe(view, next,
4135 &shell->input_panel_layer.view_list, layer_link)
4136 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004137}
4138
4139static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004140update_input_panels(struct wl_listener *listener, void *data)
4141{
4142 struct desktop_shell *shell =
4143 container_of(listener, struct desktop_shell,
4144 update_input_panel_listener);
4145
4146 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4147}
4148
4149static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004150center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004151{
Giulio Camuffob8366642013-04-25 13:57:46 +03004152 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004153 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004154
Jason Ekstranda7af7042013-10-12 22:38:11 -05004155 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004156
4157 x = output->x + (output->width - width) / 2 - surf_x / 2;
4158 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004159
Jason Ekstranda7af7042013-10-12 22:38:11 -05004160 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004161}
4162
4163static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004164weston_view_set_initial_position(struct weston_view *view,
4165 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004166{
4167 struct weston_compositor *compositor = shell->compositor;
4168 int ix = 0, iy = 0;
4169 int range_x, range_y;
4170 int dx, dy, x, y, panel_height;
4171 struct weston_output *output, *target_output = NULL;
4172 struct weston_seat *seat;
4173
4174 /* As a heuristic place the new window on the same output as the
4175 * pointer. Falling back to the output containing 0, 0.
4176 *
4177 * TODO: Do something clever for touch too?
4178 */
4179 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004180 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004181 ix = wl_fixed_to_int(seat->pointer->x);
4182 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004183 break;
4184 }
4185 }
4186
4187 wl_list_for_each(output, &compositor->output_list, link) {
4188 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4189 target_output = output;
4190 break;
4191 }
4192 }
4193
4194 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004195 weston_view_set_position(view, 10 + random() % 400,
4196 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004197 return;
4198 }
4199
4200 /* Valid range within output where the surface will still be onscreen.
4201 * If this is negative it means that the surface is bigger than
4202 * output.
4203 */
4204 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004205 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004206 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05004207 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004208
Rob Bradford4cb88c72012-08-13 15:18:44 +01004209 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004210 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004211 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004212 dx = 0;
4213
4214 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004215 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004216 else
4217 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004218
4219 x = target_output->x + dx;
4220 y = target_output->y + dy;
4221
Jason Ekstranda7af7042013-10-12 22:38:11 -05004222 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004223}
4224
4225static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004226map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004227 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004228{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004229 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004230 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01004231 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004232 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08004233 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004234 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004235
Jason Ekstranda7af7042013-10-12 22:38:11 -05004236 shsurf->view->geometry.width = width;
4237 shsurf->view->geometry.height = height;
4238 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004239
4240 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004241 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004242 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004243 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004244 break;
Alex Wu4539b082012-03-01 12:57:46 +08004245 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004246 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004247 shell_map_fullscreen(shsurf);
4248 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004249 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004250 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004251 panel_height = get_output_panel_height(shell, shsurf->output);
4252 surface_subsurfaces_boundingbox(shsurf->surface,
4253 &surf_x, &surf_y, NULL, NULL);
4254 weston_view_set_position(shsurf->view,
4255 shsurf->output->x - surf_x,
4256 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004257 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004258 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004259 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004260 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004261 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004262 weston_view_set_position(shsurf->view,
4263 shsurf->view->geometry.x + sx,
4264 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004265 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004266 default:
4267 ;
4268 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004269
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02004270 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004271 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004272 case SHELL_SURFACE_POPUP:
4273 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004274 /* TODO: Handle a parent with multiple views */
4275 parent = get_default_view(shsurf->parent);
4276 if (parent) {
4277 wl_list_remove(&shsurf->view->layer_link);
4278 wl_list_insert(parent->layer_link.prev,
4279 &shsurf->view->layer_link);
4280 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004281 break;
Alex Wu4539b082012-03-01 12:57:46 +08004282 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02004283 case SHELL_SURFACE_NONE:
4284 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004285 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004286 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004287 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004288 wl_list_remove(&shsurf->view->layer_link);
4289 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004290 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004291 }
4292
Jason Ekstranda7af7042013-10-12 22:38:11 -05004293 if (shsurf->type != SHELL_SURFACE_NONE) {
4294 weston_view_update_transform(shsurf->view);
4295 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4296 shsurf->surface->output = shsurf->output;
4297 shsurf->view->output = shsurf->output;
4298 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004299 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004300
Jason Ekstranda7af7042013-10-12 22:38:11 -05004301 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004302 /* XXX: xwayland's using the same fields for transient type */
4303 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004304 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004305 if (shsurf->transient.flags ==
4306 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4307 break;
4308 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004309 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004310 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004311 if (!shell->locked) {
4312 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004313 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004314 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004315 break;
4316 default:
4317 break;
4318 }
4319
Jason Ekstranda7af7042013-10-12 22:38:11 -05004320 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004321 {
4322 switch (shell->win_animation_type) {
4323 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004324 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004325 break;
4326 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004327 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004328 break;
4329 default:
4330 break;
4331 }
4332 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004333}
4334
4335static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004336configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004337 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004338{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004339 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4340 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004341 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004342 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004343
Pekka Paalanen77346a62011-11-30 16:26:35 +02004344 shsurf = get_shell_surface(surface);
4345 if (shsurf)
4346 surface_type = shsurf->type;
4347
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 /* TODO:
4349 * This should probably be changed to be more shell_surface
4350 * dependent
4351 */
4352 wl_list_for_each(view, &surface->views, surface_link)
4353 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004354
4355 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004356 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004357 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004358 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004359 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004360 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004361 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004362 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4363 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004364 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4365 shsurf->view->geometry.y = shsurf->output->y +
4366 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004367 break;
Alex Wu4539b082012-03-01 12:57:46 +08004368 case SHELL_SURFACE_TOPLEVEL:
4369 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004370 default:
4371 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004372 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004373
Alex Wu4539b082012-03-01 12:57:46 +08004374 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004375 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 wl_list_for_each(view, &surface->views, surface_link)
4377 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004378
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004379 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004380 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004381 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004382}
4383
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004384static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004385shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004386{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004387 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004388 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004389
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004390 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004391
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004392 if (!weston_surface_is_mapped(es) &&
4393 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004394 remove_popup_grab(shsurf);
4395 }
4396
Giulio Camuffo184df502013-02-21 11:29:21 +01004397 if (width == 0)
4398 return;
4399
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004400 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004401 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004402 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004403 type_changed = 1;
4404 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004405
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004406 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004407 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004408 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004409 shsurf->view->geometry.width != width ||
4410 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004411 float from_x, from_y;
4412 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004413
Jason Ekstranda7af7042013-10-12 22:38:11 -05004414 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4415 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004416 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004417 shsurf->view->geometry.x + to_x - from_x,
4418 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004419 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004420 }
4421}
4422
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004423static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004424
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004425static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004426desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004427{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004428 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004429 struct desktop_shell *shell =
4430 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004431
4432 shell->child.process.pid = 0;
4433 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004434
4435 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4436 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004437 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004438 shell->child.deathstamp = time;
4439 shell->child.deathcount = 0;
4440 }
4441
4442 shell->child.deathcount++;
4443 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004444 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004445 return;
4446 }
4447
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004448 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004449 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004450 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004451}
4452
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004453static void
4454launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004455{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004456 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004457
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004458 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004459 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004460 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004461 desktop_shell_sigchld);
4462
4463 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004464 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004465}
4466
4467static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004468bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4469{
Tiago Vignattibe143262012-04-16 17:31:41 +03004470 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004471 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004472
Jason Ekstranda85118c2013-06-27 20:17:02 -05004473 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4474 if (resource)
4475 wl_resource_set_implementation(resource, &shell_implementation,
4476 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004477}
4478
Kristian Høgsberg75840622011-09-06 13:48:16 -04004479static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004480unbind_desktop_shell(struct wl_resource *resource)
4481{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004482 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004483
4484 if (shell->locked)
4485 resume_desktop(shell);
4486
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004487 shell->child.desktop_shell = NULL;
4488 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004489}
4490
4491static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004492bind_desktop_shell(struct wl_client *client,
4493 void *data, uint32_t version, uint32_t id)
4494{
Tiago Vignattibe143262012-04-16 17:31:41 +03004495 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004496 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004497
Jason Ekstranda85118c2013-06-27 20:17:02 -05004498 resource = wl_resource_create(client, &desktop_shell_interface,
4499 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004500
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004501 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004502 wl_resource_set_implementation(resource,
4503 &desktop_shell_implementation,
4504 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004505 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004506
4507 if (version < 2)
4508 shell_fade_startup(shell);
4509
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004510 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004511 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004512
4513 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4514 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004515 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004516}
4517
Pekka Paalanen6e168112011-11-24 11:34:05 +02004518static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004519screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004520{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004521 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004522 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004523
Giulio Camuffo184df502013-02-21 11:29:21 +01004524 if (width == 0)
4525 return;
4526
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004527 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004528 if (!shell->locked)
4529 return;
4530
Jason Ekstranda7af7042013-10-12 22:38:11 -05004531 view = container_of(surface->views.next, struct weston_view, surface_link);
4532 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004533
Jason Ekstranda7af7042013-10-12 22:38:11 -05004534 if (wl_list_empty(&view->layer_link)) {
4535 wl_list_insert(shell->lock_layer.view_list.prev,
4536 &view->layer_link);
4537 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004538 wl_event_source_timer_update(shell->screensaver.timer,
4539 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004540 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004541 }
4542}
4543
4544static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004545screensaver_set_surface(struct wl_client *client,
4546 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004547 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004548 struct wl_resource *output_resource)
4549{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004550 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004551 struct weston_surface *surface =
4552 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004553 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004554 struct weston_view *view, *next;
4555
4556 /* Make sure we only have one view */
4557 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4558 weston_view_destroy(view);
4559 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004560
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004561 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004562 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004563 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004564}
4565
4566static const struct screensaver_interface screensaver_implementation = {
4567 screensaver_set_surface
4568};
4569
4570static void
4571unbind_screensaver(struct wl_resource *resource)
4572{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004573 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004574
Pekka Paalanen77346a62011-11-30 16:26:35 +02004575 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004576}
4577
4578static void
4579bind_screensaver(struct wl_client *client,
4580 void *data, uint32_t version, uint32_t id)
4581{
Tiago Vignattibe143262012-04-16 17:31:41 +03004582 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004583 struct wl_resource *resource;
4584
Jason Ekstranda85118c2013-06-27 20:17:02 -05004585 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004586
Pekka Paalanen77346a62011-11-30 16:26:35 +02004587 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004588 wl_resource_set_implementation(resource,
4589 &screensaver_implementation,
4590 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004591 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004592 return;
4593 }
4594
4595 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4596 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004597 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004598}
4599
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004600static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004601input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004602{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004603 struct input_panel_surface *ip_surface = surface->configure_private;
4604 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004605 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004606 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004607
Giulio Camuffo184df502013-02-21 11:29:21 +01004608 if (width == 0)
4609 return;
4610
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004611 if (!weston_surface_is_mapped(surface)) {
4612 if (!shell->showing_input_panels)
4613 return;
4614
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004615 show_surface = 1;
4616 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004617
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004618 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004619
4620 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004621 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4622 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004623 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004624 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4625 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004626 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004627
Jason Ekstranda7af7042013-10-12 22:38:11 -05004628 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004629
4630 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004631 wl_list_insert(&shell->input_panel_layer.view_list,
4632 &ip_surface->view->layer_link);
4633 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004634 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004635 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004636 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004637}
4638
4639static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004640destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004641{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004642 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4643
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004644 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004645 wl_list_remove(&input_panel_surface->link);
4646
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004647 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004648 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004649
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004650 free(input_panel_surface);
4651}
4652
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004653static struct input_panel_surface *
4654get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004655{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004656 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004657 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004658 } else {
4659 return NULL;
4660 }
4661}
4662
4663static void
4664input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4665{
4666 struct input_panel_surface *ipsurface = container_of(listener,
4667 struct input_panel_surface,
4668 surface_destroy_listener);
4669
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004670 if (ipsurface->resource) {
4671 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004672 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004673 destroy_input_panel_surface(ipsurface);
4674 }
4675}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004676
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004677static struct input_panel_surface *
4678create_input_panel_surface(struct desktop_shell *shell,
4679 struct weston_surface *surface)
4680{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004681 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004682
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004683 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4684 if (!input_panel_surface)
4685 return NULL;
4686
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004687 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004688 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004689
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004690 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004691
4692 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004693 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004694
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004695 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004696 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004697 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004698 &input_panel_surface->surface_destroy_listener);
4699
4700 wl_list_init(&input_panel_surface->link);
4701
4702 return input_panel_surface;
4703}
4704
4705static void
4706input_panel_surface_set_toplevel(struct wl_client *client,
4707 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004708 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004709 uint32_t position)
4710{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004711 struct input_panel_surface *input_panel_surface =
4712 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004713 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004714
4715 wl_list_insert(&shell->input_panel.surfaces,
4716 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004717
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004718 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004719 input_panel_surface->panel = 0;
4720}
4721
4722static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004723input_panel_surface_set_overlay_panel(struct wl_client *client,
4724 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004725{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004726 struct input_panel_surface *input_panel_surface =
4727 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004728 struct desktop_shell *shell = input_panel_surface->shell;
4729
4730 wl_list_insert(&shell->input_panel.surfaces,
4731 &input_panel_surface->link);
4732
4733 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004734}
4735
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004736static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004737 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004738 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004739};
4740
4741static void
4742destroy_input_panel_surface_resource(struct wl_resource *resource)
4743{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004744 struct input_panel_surface *ipsurf =
4745 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004746
4747 destroy_input_panel_surface(ipsurf);
4748}
4749
4750static void
4751input_panel_get_input_panel_surface(struct wl_client *client,
4752 struct wl_resource *resource,
4753 uint32_t id,
4754 struct wl_resource *surface_resource)
4755{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004756 struct weston_surface *surface =
4757 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004758 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004759 struct input_panel_surface *ipsurf;
4760
4761 if (get_input_panel_surface(surface)) {
4762 wl_resource_post_error(surface_resource,
4763 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004764 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004765 return;
4766 }
4767
4768 ipsurf = create_input_panel_surface(shell, surface);
4769 if (!ipsurf) {
4770 wl_resource_post_error(surface_resource,
4771 WL_DISPLAY_ERROR_INVALID_OBJECT,
4772 "surface->configure already set");
4773 return;
4774 }
4775
Jason Ekstranda85118c2013-06-27 20:17:02 -05004776 ipsurf->resource =
4777 wl_resource_create(client,
4778 &wl_input_panel_surface_interface, 1, id);
4779 wl_resource_set_implementation(ipsurf->resource,
4780 &input_panel_surface_implementation,
4781 ipsurf,
4782 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004783}
4784
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004785static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004786 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004787};
4788
4789static void
4790unbind_input_panel(struct wl_resource *resource)
4791{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004792 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004793
4794 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004795}
4796
4797static void
4798bind_input_panel(struct wl_client *client,
4799 void *data, uint32_t version, uint32_t id)
4800{
4801 struct desktop_shell *shell = data;
4802 struct wl_resource *resource;
4803
Jason Ekstranda85118c2013-06-27 20:17:02 -05004804 resource = wl_resource_create(client,
4805 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004806
4807 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004808 wl_resource_set_implementation(resource,
4809 &input_panel_implementation,
4810 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004811 shell->input_panel.binding = resource;
4812 return;
4813 }
4814
4815 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4816 "interface object already bound");
4817 wl_resource_destroy(resource);
4818}
4819
Kristian Høgsberg07045392012-02-19 18:52:44 -05004820struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004821 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004822 struct weston_surface *current;
4823 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004824 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004825};
4826
4827static void
4828switcher_next(struct switcher *switcher)
4829{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004830 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004831 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004832 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004833 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004834
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4836 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004837 case SHELL_SURFACE_TOPLEVEL:
4838 case SHELL_SURFACE_FULLSCREEN:
4839 case SHELL_SURFACE_MAXIMIZED:
4840 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004841 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004842 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004843 next = view->surface;
4844 prev = view->surface;
4845 view->alpha = 0.25;
4846 weston_view_geometry_dirty(view);
4847 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004848 break;
4849 default:
4850 break;
4851 }
Alex Wu1659daa2012-04-01 20:13:09 +08004852
Jason Ekstranda7af7042013-10-12 22:38:11 -05004853 if (is_black_surface(view->surface, NULL)) {
4854 view->alpha = 0.25;
4855 weston_view_geometry_dirty(view);
4856 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004857 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004858 }
4859
4860 if (next == NULL)
4861 next = first;
4862
Alex Wu07b26062012-03-12 16:06:01 +08004863 if (next == NULL)
4864 return;
4865
Kristian Høgsberg07045392012-02-19 18:52:44 -05004866 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004867 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004868
4869 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004870 wl_list_for_each(view, &next->views, surface_link)
4871 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004872
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004873 shsurf = get_shell_surface(switcher->current);
4874 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004875 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004876}
4877
4878static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004879switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004880{
4881 struct switcher *switcher =
4882 container_of(listener, struct switcher, listener);
4883
4884 switcher_next(switcher);
4885}
4886
4887static void
Daniel Stone351eb612012-05-31 15:27:47 -04004888switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004889{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004890 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004891 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004892 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004893
Jason Ekstranda7af7042013-10-12 22:38:11 -05004894 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004895 if (is_focus_view(view))
4896 continue;
4897
Jason Ekstranda7af7042013-10-12 22:38:11 -05004898 view->alpha = 1.0;
4899 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004900 }
4901
Alex Wu07b26062012-03-12 16:06:01 +08004902 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004903 activate(switcher->shell, switcher->current,
4904 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004905 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004906 weston_keyboard_end_grab(keyboard);
4907 if (keyboard->input_method_resource)
4908 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004909 free(switcher);
4910}
4911
4912static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004913switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004914 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004915{
4916 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004917 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004918
Daniel Stonec9785ea2012-05-30 16:31:52 +01004919 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004920 switcher_next(switcher);
4921}
4922
4923static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004924switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004925 uint32_t mods_depressed, uint32_t mods_latched,
4926 uint32_t mods_locked, uint32_t group)
4927{
4928 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004929 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004930
Daniel Stone351eb612012-05-31 15:27:47 -04004931 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4932 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004933}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004934
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004935static void
4936switcher_cancel(struct weston_keyboard_grab *grab)
4937{
4938 struct switcher *switcher = container_of(grab, struct switcher, grab);
4939
4940 switcher_destroy(switcher);
4941}
4942
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004943static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004944 switcher_key,
4945 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004946 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004947};
4948
4949static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004950switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004951 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004952{
Tiago Vignattibe143262012-04-16 17:31:41 +03004953 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004954 struct switcher *switcher;
4955
4956 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004957 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004958 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004959 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004960 wl_list_init(&switcher->listener.link);
4961
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004962 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004963 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004964 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004965 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4966 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004967 switcher_next(switcher);
4968}
4969
Daniel Stonedf8133b2013-11-19 11:37:14 +01004970struct exposay_surface {
4971 struct desktop_shell *shell;
4972 struct weston_surface *surface;
4973 struct weston_view *view;
4974 struct wl_list link;
4975
4976 int x;
4977 int y;
4978 int width;
4979 int height;
4980 double scale;
4981
4982 int row;
4983 int column;
4984
4985 /* The animations only apply a transformation for their own lifetime,
4986 * and don't have an option to indefinitely maintain the
4987 * transformation in a steady state - so, we apply our own once the
4988 * animation has finished. */
4989 struct weston_transform transform;
4990};
4991
4992static void exposay_set_state(struct desktop_shell *shell,
4993 enum exposay_target_state state,
4994 struct weston_seat *seat);
4995static void exposay_check_state(struct desktop_shell *shell);
4996
4997static void
4998exposay_in_flight_inc(struct desktop_shell *shell)
4999{
5000 shell->exposay.in_flight++;
5001}
5002
5003static void
5004exposay_in_flight_dec(struct desktop_shell *shell)
5005{
5006 if (--shell->exposay.in_flight > 0)
5007 return;
5008
5009 exposay_check_state(shell);
5010}
5011
5012static void
5013exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5014{
5015 struct exposay_surface *esurface = data;
5016
5017 wl_list_insert(&esurface->view->geometry.transformation_list,
5018 &esurface->transform.link);
5019 weston_matrix_init(&esurface->transform.matrix);
5020 weston_matrix_scale(&esurface->transform.matrix,
5021 esurface->scale, esurface->scale, 1.0f);
5022 weston_matrix_translate(&esurface->transform.matrix,
5023 esurface->x - esurface->view->geometry.x,
5024 esurface->y - esurface->view->geometry.y,
5025 0);
5026
5027 weston_view_geometry_dirty(esurface->view);
5028 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5029
5030 exposay_in_flight_dec(esurface->shell);
5031}
5032
5033static void
5034exposay_animate_in(struct exposay_surface *esurface)
5035{
5036 exposay_in_flight_inc(esurface->shell);
5037
5038 weston_move_scale_run(esurface->view,
5039 esurface->x - esurface->view->geometry.x,
5040 esurface->y - esurface->view->geometry.y,
5041 1.0, esurface->scale, 0,
5042 exposay_animate_in_done, esurface);
5043}
5044
5045static void
5046exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5047{
5048 struct exposay_surface *esurface = data;
5049 struct desktop_shell *shell = esurface->shell;
5050
5051 wl_list_remove(&esurface->link);
5052 free(esurface);
5053
5054 exposay_in_flight_dec(shell);
5055}
5056
5057static void
5058exposay_animate_out(struct exposay_surface *esurface)
5059{
5060 exposay_in_flight_inc(esurface->shell);
5061
5062 /* Remove the static transformation set up by
5063 * exposay_transform_in_done(). */
5064 wl_list_remove(&esurface->transform.link);
5065 weston_view_geometry_dirty(esurface->view);
5066
5067 weston_move_scale_run(esurface->view,
5068 esurface->x - esurface->view->geometry.x,
5069 esurface->y - esurface->view->geometry.y,
5070 1.0, esurface->scale, 1,
5071 exposay_animate_out_done, esurface);
5072}
5073
5074static void
5075exposay_highlight_surface(struct desktop_shell *shell,
5076 struct exposay_surface *esurface)
5077{
5078 struct weston_view *view = NULL;
5079
5080 if (esurface) {
5081 shell->exposay.row_current = esurface->row;
5082 shell->exposay.column_current = esurface->column;
5083 view = esurface->view;
5084 }
5085
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005086 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005087 shell->exposay.focus_current = view;
5088}
5089
5090static int
5091exposay_is_animating(struct desktop_shell *shell)
5092{
5093 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5094 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5095 return 0;
5096
5097 return (shell->exposay.in_flight > 0);
5098}
5099
5100static void
5101exposay_pick(struct desktop_shell *shell, int x, int y)
5102{
5103 struct exposay_surface *esurface;
5104
5105 if (exposay_is_animating(shell))
5106 return;
5107
5108 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5109 if (x < esurface->x || x > esurface->x + esurface->width)
5110 continue;
5111 if (y < esurface->y || y > esurface->y + esurface->height)
5112 continue;
5113
5114 exposay_highlight_surface(shell, esurface);
5115 return;
5116 }
5117}
5118
5119/* Pretty lame layout for now; just tries to make a square. Should take
5120 * aspect ratio into account really. Also needs to be notified of surface
5121 * addition and removal and adjust layout/animate accordingly. */
5122static enum exposay_layout_state
5123exposay_layout(struct desktop_shell *shell)
5124{
5125 struct workspace *workspace = shell->exposay.workspace;
5126 struct weston_compositor *compositor = shell->compositor;
5127 struct weston_output *output = get_default_output(compositor);
5128 struct weston_view *view;
5129 struct exposay_surface *esurface;
5130 int w, h;
5131 int i;
5132 int last_row_removed = 0;
5133
5134 wl_list_init(&shell->exposay.surface_list);
5135
5136 shell->exposay.num_surfaces = 0;
5137 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5138 if (!get_shell_surface(view->surface))
5139 continue;
5140 shell->exposay.num_surfaces++;
5141 }
5142
5143 if (shell->exposay.num_surfaces == 0) {
5144 shell->exposay.grid_size = 0;
5145 shell->exposay.hpadding_outer = 0;
5146 shell->exposay.vpadding_outer = 0;
5147 shell->exposay.padding_inner = 0;
5148 shell->exposay.surface_size = 0;
5149 return EXPOSAY_LAYOUT_OVERVIEW;
5150 }
5151
5152 /* Lay the grid out as square as possible, losing surfaces from the
5153 * bottom row if required. Start with fixed padding of a 10% margin
5154 * around the outside and 80px internal padding between surfaces, and
5155 * maximise the area made available to surfaces after this, but only
5156 * to a maximum of 1/3rd the total output size.
5157 *
5158 * If we can't make a square grid, add one extra row at the bottom
5159 * which will have a smaller number of columns.
5160 *
5161 * XXX: Surely there has to be a better way to express this maths,
5162 * right?!
5163 */
5164 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5165 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5166 shell->exposay.grid_size++;
5167 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5168
5169 shell->exposay.hpadding_outer = (output->width / 10);
5170 shell->exposay.vpadding_outer = (output->height / 10);
5171 shell->exposay.padding_inner = 80;
5172
5173 w = output->width - (shell->exposay.hpadding_outer * 2);
5174 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5175 w /= shell->exposay.grid_size;
5176
5177 h = output->height - (shell->exposay.vpadding_outer * 2);
5178 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5179 h /= shell->exposay.grid_size;
5180
5181 shell->exposay.surface_size = (w < h) ? w : h;
5182 if (shell->exposay.surface_size > (output->width / 2))
5183 shell->exposay.surface_size = output->width / 2;
5184 if (shell->exposay.surface_size > (output->height / 2))
5185 shell->exposay.surface_size = output->height / 2;
5186
5187 i = 0;
5188 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5189 int pad;
5190
5191 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5192
5193 if (!get_shell_surface(view->surface))
5194 continue;
5195
5196 esurface = malloc(sizeof(*esurface));
5197 if (!esurface) {
5198 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5199 shell->exposay.seat);
5200 break;
5201 }
5202
5203 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5204 esurface->shell = shell;
5205 esurface->view = view;
5206
5207 esurface->row = i / shell->exposay.grid_size;
5208 esurface->column = i % shell->exposay.grid_size;
5209
5210 esurface->x = shell->exposay.hpadding_outer;
5211 esurface->x += pad * esurface->column;
5212 esurface->y = shell->exposay.vpadding_outer;
5213 esurface->y += pad * esurface->row;
5214
5215 if (esurface->row == shell->exposay.grid_size - 1)
5216 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5217
5218 if (view->geometry.width > view->geometry.height)
5219 esurface->scale = shell->exposay.surface_size / (float) view->geometry.width;
5220 else
5221 esurface->scale = shell->exposay.surface_size / (float) view->geometry.height;
5222 esurface->width = view->geometry.width * esurface->scale;
5223 esurface->height = view->geometry.height * esurface->scale;
5224
5225 if (shell->exposay.focus_current == esurface->view)
5226 exposay_highlight_surface(shell, esurface);
5227
5228 exposay_animate_in(esurface);
5229
5230 i++;
5231 }
5232
5233 weston_compositor_schedule_repaint(shell->compositor);
5234
5235 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5236}
5237
5238static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005239exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5240 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005241{
5242 struct desktop_shell *shell =
5243 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5244
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005245 weston_pointer_move(grab->pointer, x, y);
5246
Daniel Stonedf8133b2013-11-19 11:37:14 +01005247 exposay_pick(shell,
5248 wl_fixed_to_int(grab->pointer->x),
5249 wl_fixed_to_int(grab->pointer->y));
5250}
5251
5252static void
5253exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5254 uint32_t state_w)
5255{
5256 struct desktop_shell *shell =
5257 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5258 struct weston_seat *seat = grab->pointer->seat;
5259 enum wl_pointer_button_state state = state_w;
5260
5261 if (button != BTN_LEFT)
5262 return;
5263
5264 /* Store the surface we clicked on, and don't do anything if we end up
5265 * releasing on a different surface. */
5266 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5267 shell->exposay.clicked = shell->exposay.focus_current;
5268 return;
5269 }
5270
5271 if (shell->exposay.focus_current == shell->exposay.clicked)
5272 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5273 else
5274 shell->exposay.clicked = NULL;
5275}
5276
5277static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5278 noop_grab_focus,
5279 exposay_motion,
5280 exposay_button,
5281};
5282
5283static int
5284exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5285{
5286 struct exposay_surface *esurface;
5287
5288 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5289 if (esurface->row != row || esurface->column != column)
5290 continue;
5291
5292 exposay_highlight_surface(shell, esurface);
5293 return 1;
5294 }
5295
5296 return 0;
5297}
5298
5299static void
5300exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5301 uint32_t state_w)
5302{
5303 struct weston_seat *seat = grab->keyboard->seat;
5304 struct desktop_shell *shell =
5305 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5306 enum wl_keyboard_key_state state = state_w;
5307
5308 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5309 return;
5310
5311 switch (key) {
5312 case KEY_ESC:
5313 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5314 break;
5315 case KEY_ENTER:
5316 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5317 break;
5318 case KEY_UP:
5319 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5320 shell->exposay.column_current);
5321 break;
5322 case KEY_DOWN:
5323 /* Special case for trying to move to the bottom row when it
5324 * has fewer items than all the others. */
5325 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5326 shell->exposay.column_current) &&
5327 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5328 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5329 (shell->exposay.num_surfaces %
5330 shell->exposay.grid_size) - 1);
5331 }
5332 break;
5333 case KEY_LEFT:
5334 exposay_maybe_move(shell, shell->exposay.row_current,
5335 shell->exposay.column_current - 1);
5336 break;
5337 case KEY_RIGHT:
5338 exposay_maybe_move(shell, shell->exposay.row_current,
5339 shell->exposay.column_current + 1);
5340 break;
5341 case KEY_TAB:
5342 /* Try to move right, then down (and to the leftmost column),
5343 * then if all else fails, to the top left. */
5344 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5345 shell->exposay.column_current + 1) &&
5346 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5347 exposay_maybe_move(shell, 0, 0);
5348 break;
5349 default:
5350 break;
5351 }
5352}
5353
5354static void
5355exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5356 uint32_t mods_depressed, uint32_t mods_latched,
5357 uint32_t mods_locked, uint32_t group)
5358{
5359}
5360
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005361static void
5362exposay_cancel(struct weston_keyboard_grab *grab)
5363{
5364 struct desktop_shell *shell =
5365 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5366
5367 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5368}
5369
Daniel Stonedf8133b2013-11-19 11:37:14 +01005370static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5371 exposay_key,
5372 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005373 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005374};
5375
5376/**
5377 * Called when the transition from overview -> inactive has completed.
5378 */
5379static enum exposay_layout_state
5380exposay_set_inactive(struct desktop_shell *shell)
5381{
5382 struct weston_seat *seat = shell->exposay.seat;
5383
5384 weston_keyboard_end_grab(seat->keyboard);
5385 weston_pointer_end_grab(seat->pointer);
5386 if (seat->keyboard->input_method_resource)
5387 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5388
5389 return EXPOSAY_LAYOUT_INACTIVE;
5390}
5391
5392/**
5393 * Begins the transition from overview to inactive. */
5394static enum exposay_layout_state
5395exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5396{
5397 struct exposay_surface *esurface;
5398
5399 /* Call activate() before we start the animations to avoid
5400 * animating back the old state and then immediately transitioning
5401 * to the new. */
5402 if (switch_focus && shell->exposay.focus_current)
5403 activate(shell, shell->exposay.focus_current->surface,
5404 shell->exposay.seat);
5405 else if (shell->exposay.focus_prev)
5406 activate(shell, shell->exposay.focus_prev->surface,
5407 shell->exposay.seat);
5408
5409 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5410 exposay_animate_out(esurface);
5411 weston_compositor_schedule_repaint(shell->compositor);
5412
5413 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5414}
5415
5416static enum exposay_layout_state
5417exposay_transition_active(struct desktop_shell *shell)
5418{
5419 struct weston_seat *seat = shell->exposay.seat;
5420
5421 shell->exposay.workspace = get_current_workspace(shell);
5422 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5423 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5424 shell->exposay.clicked = NULL;
5425 wl_list_init(&shell->exposay.surface_list);
5426
5427 lower_fullscreen_layer(shell);
5428 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5429 weston_keyboard_start_grab(seat->keyboard,
5430 &shell->exposay.grab_kbd);
5431 weston_keyboard_set_focus(seat->keyboard, NULL);
5432
5433 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5434 weston_pointer_start_grab(seat->pointer,
5435 &shell->exposay.grab_ptr);
5436 weston_pointer_set_focus(seat->pointer, NULL,
5437 seat->pointer->x, seat->pointer->y);
5438
5439 return exposay_layout(shell);
5440}
5441
5442static void
5443exposay_check_state(struct desktop_shell *shell)
5444{
5445 enum exposay_layout_state state_new = shell->exposay.state_cur;
5446 int do_switch = 0;
5447
5448 /* Don't do anything whilst animations are running, just store up
5449 * target state changes and only act on them when the animations have
5450 * completed. */
5451 if (exposay_is_animating(shell))
5452 return;
5453
5454 switch (shell->exposay.state_target) {
5455 case EXPOSAY_TARGET_OVERVIEW:
5456 switch (shell->exposay.state_cur) {
5457 case EXPOSAY_LAYOUT_OVERVIEW:
5458 goto out;
5459 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5460 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5461 break;
5462 default:
5463 state_new = exposay_transition_active(shell);
5464 break;
5465 }
5466 break;
5467
5468 case EXPOSAY_TARGET_SWITCH:
5469 do_switch = 1; /* fallthrough */
5470 case EXPOSAY_TARGET_CANCEL:
5471 switch (shell->exposay.state_cur) {
5472 case EXPOSAY_LAYOUT_INACTIVE:
5473 goto out;
5474 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5475 state_new = exposay_set_inactive(shell);
5476 break;
5477 default:
5478 state_new = exposay_transition_inactive(shell, do_switch);
5479 break;
5480 }
5481
5482 break;
5483 }
5484
5485out:
5486 shell->exposay.state_cur = state_new;
5487}
5488
5489static void
5490exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5491 struct weston_seat *seat)
5492{
5493 shell->exposay.state_target = state;
5494 shell->exposay.seat = seat;
5495 exposay_check_state(shell);
5496}
5497
5498static void
5499exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5500 void *data)
5501{
5502 struct desktop_shell *shell = data;
5503
5504 if (shell->exposay.state_target == EXPOSAY_TARGET_OVERVIEW)
5505 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5506 else
5507 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
5508}
5509
Pekka Paalanen3c647232011-12-22 13:43:43 +02005510static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005511backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005512 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005513{
5514 struct weston_compositor *compositor = data;
5515 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005516 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005517
5518 /* TODO: we're limiting to simple use cases, where we assume just
5519 * control on the primary display. We'd have to extend later if we
5520 * ever get support for setting backlights on random desktop LCD
5521 * panels though */
5522 output = get_default_output(compositor);
5523 if (!output)
5524 return;
5525
5526 if (!output->set_backlight)
5527 return;
5528
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005529 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5530 backlight_new = output->backlight_current - 25;
5531 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5532 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005533
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005534 if (backlight_new < 5)
5535 backlight_new = 5;
5536 if (backlight_new > 255)
5537 backlight_new = 255;
5538
5539 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005540 output->set_backlight(output, output->backlight_current);
5541}
5542
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005543struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005544 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005545 struct weston_seat *seat;
5546 uint32_t key[2];
5547 int key_released[2];
5548};
5549
5550static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005551debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005552 uint32_t key, uint32_t state)
5553{
5554 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005555 struct weston_compositor *ec = db->seat->compositor;
5556 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005557 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005558 uint32_t serial;
5559 int send = 0, terminate = 0;
5560 int check_binding = 1;
5561 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005562 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005563
5564 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5565 /* Do not run bindings on key releases */
5566 check_binding = 0;
5567
5568 for (i = 0; i < 2; i++)
5569 if (key == db->key[i])
5570 db->key_released[i] = 1;
5571
5572 if (db->key_released[0] && db->key_released[1]) {
5573 /* All key releases been swalled so end the grab */
5574 terminate = 1;
5575 } else if (key != db->key[0] && key != db->key[1]) {
5576 /* Should not swallow release of other keys */
5577 send = 1;
5578 }
5579 } else if (key == db->key[0] && !db->key_released[0]) {
5580 /* Do not check bindings for the first press of the binding
5581 * key. This allows it to be used as a debug shortcut.
5582 * We still need to swallow this event. */
5583 check_binding = 0;
5584 } else if (db->key[1]) {
5585 /* If we already ran a binding don't process another one since
5586 * we can't keep track of all the binding keys that were
5587 * pressed in order to swallow the release events. */
5588 send = 1;
5589 check_binding = 0;
5590 }
5591
5592 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005593 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5594 key, state)) {
5595 /* We ran a binding so swallow the press and keep the
5596 * grab to swallow the released too. */
5597 send = 0;
5598 terminate = 0;
5599 db->key[1] = key;
5600 } else {
5601 /* Terminate the grab since the key pressed is not a
5602 * debug binding key. */
5603 send = 1;
5604 terminate = 1;
5605 }
5606 }
5607
5608 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005609 serial = wl_display_next_serial(display);
5610 resource_list = &grab->keyboard->focus_resource_list;
5611 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005612 wl_keyboard_send_key(resource, serial, time, key, state);
5613 }
5614 }
5615
5616 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005617 weston_keyboard_end_grab(grab->keyboard);
5618 if (grab->keyboard->input_method_resource)
5619 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005620 free(db);
5621 }
5622}
5623
5624static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005625debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005626 uint32_t mods_depressed, uint32_t mods_latched,
5627 uint32_t mods_locked, uint32_t group)
5628{
5629 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005630 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005631
Neil Roberts96d790e2013-09-19 17:32:00 +01005632 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005633
Neil Roberts96d790e2013-09-19 17:32:00 +01005634 wl_resource_for_each(resource, resource_list) {
5635 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5636 mods_latched, mods_locked, group);
5637 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005638}
5639
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005640static void
5641debug_binding_cancel(struct weston_keyboard_grab *grab)
5642{
5643 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5644
5645 weston_keyboard_end_grab(grab->keyboard);
5646 free(db);
5647}
5648
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005649struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005650 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005651 debug_binding_modifiers,
5652 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005653};
5654
5655static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005656debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005657{
5658 struct debug_binding_grab *grab;
5659
5660 grab = calloc(1, sizeof *grab);
5661 if (!grab)
5662 return;
5663
5664 grab->seat = (struct weston_seat *) seat;
5665 grab->key[0] = key;
5666 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005667 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005668}
5669
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005670static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005671force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005672 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005673{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005674 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005675 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005676 struct desktop_shell *shell = data;
5677 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005678 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005679
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005680 focus_surface = seat->keyboard->focus;
5681 if (!focus_surface)
5682 return;
5683
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005684 wl_signal_emit(&compositor->kill_signal, focus_surface);
5685
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005686 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005687 wl_client_get_credentials(client, &pid, NULL, NULL);
5688
5689 /* Skip clients that we launched ourselves (the credentials of
5690 * the socketpair is ours) */
5691 if (pid == getpid())
5692 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005693
Daniel Stone325fc2d2012-05-30 16:31:58 +01005694 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005695}
5696
5697static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005698workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005699 uint32_t key, void *data)
5700{
5701 struct desktop_shell *shell = data;
5702 unsigned int new_index = shell->workspaces.current;
5703
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005704 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005705 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005706 if (new_index != 0)
5707 new_index--;
5708
5709 change_workspace(shell, new_index);
5710}
5711
5712static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005713workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005714 uint32_t key, void *data)
5715{
5716 struct desktop_shell *shell = data;
5717 unsigned int new_index = shell->workspaces.current;
5718
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005719 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005720 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005721 if (new_index < shell->workspaces.num - 1)
5722 new_index++;
5723
5724 change_workspace(shell, new_index);
5725}
5726
5727static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005728workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005729 uint32_t key, void *data)
5730{
5731 struct desktop_shell *shell = data;
5732 unsigned int new_index;
5733
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005734 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005735 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005736 new_index = key - KEY_F1;
5737 if (new_index >= shell->workspaces.num)
5738 new_index = shell->workspaces.num - 1;
5739
5740 change_workspace(shell, new_index);
5741}
5742
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005743static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005744workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005745 uint32_t key, void *data)
5746{
5747 struct desktop_shell *shell = data;
5748 unsigned int new_index = shell->workspaces.current;
5749
5750 if (shell->locked)
5751 return;
5752
5753 if (new_index != 0)
5754 new_index--;
5755
5756 take_surface_to_workspace_by_seat(shell, seat, new_index);
5757}
5758
5759static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005760workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005761 uint32_t key, void *data)
5762{
5763 struct desktop_shell *shell = data;
5764 unsigned int new_index = shell->workspaces.current;
5765
5766 if (shell->locked)
5767 return;
5768
5769 if (new_index < shell->workspaces.num - 1)
5770 new_index++;
5771
5772 take_surface_to_workspace_by_seat(shell, seat, new_index);
5773}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005774
5775static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005776handle_output_destroy(struct wl_listener *listener, void *data)
5777{
5778 struct shell_output *output_listener =
5779 container_of(listener, struct shell_output, destroy_listener);
5780
5781 wl_list_remove(&output_listener->destroy_listener.link);
5782 wl_list_remove(&output_listener->link);
5783 free(output_listener);
5784}
5785
5786static void
5787create_shell_output(struct desktop_shell *shell,
5788 struct weston_output *output)
5789{
5790 struct shell_output *shell_output;
5791
5792 shell_output = zalloc(sizeof *shell_output);
5793 if (shell_output == NULL)
5794 return;
5795
5796 shell_output->output = output;
5797 shell_output->shell = shell;
5798 shell_output->destroy_listener.notify = handle_output_destroy;
5799 wl_signal_add(&output->destroy_signal,
5800 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005801 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005802}
5803
5804static void
5805handle_output_create(struct wl_listener *listener, void *data)
5806{
5807 struct desktop_shell *shell =
5808 container_of(listener, struct desktop_shell, output_create_listener);
5809 struct weston_output *output = (struct weston_output *)data;
5810
5811 create_shell_output(shell, output);
5812}
5813
5814static void
5815setup_output_destroy_handler(struct weston_compositor *ec,
5816 struct desktop_shell *shell)
5817{
5818 struct weston_output *output;
5819
5820 wl_list_init(&shell->output_list);
5821 wl_list_for_each(output, &ec->output_list, link)
5822 create_shell_output(shell, output);
5823
5824 shell->output_create_listener.notify = handle_output_create;
5825 wl_signal_add(&ec->output_created_signal,
5826 &shell->output_create_listener);
5827}
5828
5829static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005830shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005831{
Tiago Vignattibe143262012-04-16 17:31:41 +03005832 struct desktop_shell *shell =
5833 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005834 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005835 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005836
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005837 if (shell->child.client)
5838 wl_client_destroy(shell->child.client);
5839
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005840 wl_list_remove(&shell->idle_listener.link);
5841 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005842 wl_list_remove(&shell->show_input_panel_listener.link);
5843 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005844
Xiong Zhang6b481422013-10-23 13:58:32 +08005845 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5846 wl_list_remove(&shell_output->destroy_listener.link);
5847 wl_list_remove(&shell_output->link);
5848 free(shell_output);
5849 }
5850
5851 wl_list_remove(&shell->output_create_listener.link);
5852
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005853 wl_array_for_each(ws, &shell->workspaces.array)
5854 workspace_destroy(*ws);
5855 wl_array_release(&shell->workspaces.array);
5856
Pekka Paalanen3c647232011-12-22 13:43:43 +02005857 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005858 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005859 free(shell);
5860}
5861
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005862static void
5863shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5864{
5865 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005866 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005867
5868 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005869 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5870 MODIFIER_CTRL | MODIFIER_ALT,
5871 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005872 weston_compositor_add_key_binding(ec, KEY_TAB,
5873 MODIFIER_ALT,
5874 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005875 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5876 click_to_activate_binding,
5877 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005878 weston_compositor_add_touch_binding(ec, 0,
5879 touch_to_activate_binding,
5880 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005881 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5882 MODIFIER_SUPER | MODIFIER_ALT,
5883 surface_opacity_binding, NULL);
5884 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5885 MODIFIER_SUPER, zoom_axis_binding,
5886 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005887
5888 /* configurable bindings */
5889 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005890 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5891 zoom_key_binding, NULL);
5892 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5893 zoom_key_binding, NULL);
5894 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5895 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005896 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005897 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5898 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005899
5900 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5901 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5902 rotate_binding, NULL);
5903
Daniel Stone325fc2d2012-05-30 16:31:58 +01005904 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5905 shell);
5906 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5907 ec);
5908 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5909 backlight_binding, ec);
5910 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5911 ec);
5912 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5913 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005914 weston_compositor_add_key_binding(ec, KEY_K, mod,
5915 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005916 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5917 workspace_up_binding, shell);
5918 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5919 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005920 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5921 workspace_move_surface_up_binding,
5922 shell);
5923 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5924 workspace_move_surface_down_binding,
5925 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005926
Daniel Stonedf8133b2013-11-19 11:37:14 +01005927 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5928
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005929 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5930 if (shell->workspaces.num > 1) {
5931 num_workspace_bindings = shell->workspaces.num;
5932 if (num_workspace_bindings > 6)
5933 num_workspace_bindings = 6;
5934 for (i = 0; i < num_workspace_bindings; i++)
5935 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5936 workspace_f_binding,
5937 shell);
5938 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005939
5940 /* Debug bindings */
5941 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5942 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005943}
5944
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005945WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005946module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005947 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005948{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005949 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005950 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005951 struct workspace **pws;
5952 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005953 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005954
Peter Huttererf3d62272013-08-08 11:57:05 +10005955 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005956 if (shell == NULL)
5957 return -1;
5958
Kristian Høgsberg75840622011-09-06 13:48:16 -04005959 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005960
5961 shell->destroy_listener.notify = shell_destroy;
5962 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005963 shell->idle_listener.notify = idle_handler;
5964 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5965 shell->wake_listener.notify = wake_handler;
5966 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005967 shell->show_input_panel_listener.notify = show_input_panels;
5968 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
5969 shell->hide_input_panel_listener.notify = hide_input_panels;
5970 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005971 shell->update_input_panel_listener.notify = update_input_panels;
5972 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06005973 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005974 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005975 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005976 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005977 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005978 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005979 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005980 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005981 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005982 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005983 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005984
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005985 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005986
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005987 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5988 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005989 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5990 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005991 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005992
5993 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005994 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005995
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005996 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005997
Daniel Stonedf8133b2013-11-19 11:37:14 +01005998 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5999 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6000
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006001 for (i = 0; i < shell->workspaces.num; i++) {
6002 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6003 if (pws == NULL)
6004 return -1;
6005
6006 *pws = workspace_create();
6007 if (*pws == NULL)
6008 return -1;
6009 }
6010 activate_workspace(shell, 0);
6011
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006012 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006013 wl_list_init(&shell->workspaces.animation.link);
6014 shell->workspaces.animation.frame = animate_workspace_change_frame;
6015
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006016 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006017 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006018 return -1;
6019
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006020 if (wl_global_create(ec->wl_display,
6021 &desktop_shell_interface, 2,
6022 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006023 return -1;
6024
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006025 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6026 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006027 return -1;
6028
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006029 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006030 shell, bind_input_panel) == NULL)
6031 return -1;
6032
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006033 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6034 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006035 return -1;
6036
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006037 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006038
Xiong Zhang6b481422013-10-23 13:58:32 +08006039 setup_output_destroy_handler(ec, shell);
6040
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006041 loop = wl_display_get_event_loop(ec->wl_display);
6042 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006043
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006044 shell->screensaver.timer =
6045 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6046
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006047 wl_list_for_each(seat, &ec->seat_list, link)
6048 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006049
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006050 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006051
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006052 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006053
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006054 return 0;
6055}