blob: f102e9a7d45ddd650f73c63acee00f254b9c6659 [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)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001869 if (seat->pointer->focus &&
1870 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001871 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001872
1873 return 1;
1874}
1875
1876static void
1877ping_handler(struct weston_surface *surface, uint32_t serial)
1878{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001879 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001880 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001881 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001882
1883 if (!shsurf)
1884 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001885 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001886 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001887
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001888 if (shsurf->surface == shsurf->shell->grab_surface)
1889 return;
1890
Scott Moreauff1db4a2012-04-17 19:06:18 -06001891 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001892 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001893 if (!shsurf->ping_timer)
1894 return;
1895
1896 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001897 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1898 shsurf->ping_timer->source =
1899 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1900 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1901
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001902 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001903 }
1904}
1905
1906static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001907handle_pointer_focus(struct wl_listener *listener, void *data)
1908{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001909 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001911 struct weston_compositor *compositor;
1912 struct shell_surface *shsurf;
1913 uint32_t serial;
1914
Jason Ekstranda7af7042013-10-12 22:38:11 -05001915 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001916 return;
1917
Jason Ekstranda7af7042013-10-12 22:38:11 -05001918 compositor = view->surface->compositor;
1919 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001920
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001921 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001922 set_busy_cursor(shsurf, pointer);
1923 } else {
1924 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001926 }
1927}
1928
1929static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001930create_pointer_focus_listener(struct weston_seat *seat)
1931{
1932 struct wl_listener *listener;
1933
Kristian Høgsberge3148752013-05-06 23:19:49 -04001934 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001935 return;
1936
1937 listener = malloc(sizeof *listener);
1938 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001939 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001940}
1941
1942static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001943shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1944 uint32_t serial)
1945{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001946 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001947 struct weston_seat *seat;
1948 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001949
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001950 if (shsurf->ping_timer == NULL)
1951 /* Just ignore unsolicited pong. */
1952 return;
1953
Scott Moreauff1db4a2012-04-17 19:06:18 -06001954 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001955 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001956 wl_list_for_each(seat, &ec->seat_list, link) {
1957 if(seat->pointer)
1958 end_busy_cursor(shsurf, seat->pointer);
1959 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001960 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001961 }
1962}
1963
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001964static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001965set_title(struct shell_surface *shsurf, const char *title)
1966{
1967 free(shsurf->title);
1968 shsurf->title = strdup(title);
1969}
1970
1971static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001972shell_surface_set_title(struct wl_client *client,
1973 struct wl_resource *resource, const char *title)
1974{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001975 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001976
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001977 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001978}
1979
1980static void
1981shell_surface_set_class(struct wl_client *client,
1982 struct wl_resource *resource, const char *class)
1983{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001984 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001985
1986 free(shsurf->class);
1987 shsurf->class = strdup(class);
1988}
1989
Alex Wu4539b082012-03-01 12:57:46 +08001990static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001991restore_output_mode(struct weston_output *output)
1992{
Hardening57388e42013-09-18 23:56:36 +02001993 if (output->current_mode != output->original_mode ||
1994 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001995 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001996 output->original_mode,
1997 output->original_scale,
1998 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001999}
2000
2001static void
2002restore_all_output_modes(struct weston_compositor *compositor)
2003{
2004 struct weston_output *output;
2005
2006 wl_list_for_each(output, &compositor->output_list, link)
2007 restore_output_mode(output);
2008}
2009
2010static void
Alex Wu4539b082012-03-01 12:57:46 +08002011shell_unset_fullscreen(struct shell_surface *shsurf)
2012{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002013 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08002014 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08002015 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2016 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002017 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002018 }
Alex Wu4539b082012-03-01 12:57:46 +08002019 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2020 shsurf->fullscreen.framerate = 0;
2021 wl_list_remove(&shsurf->fullscreen.transform.link);
2022 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 if (shsurf->fullscreen.black_view)
2024 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2025 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002026 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002027 weston_view_set_position(shsurf->view,
2028 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002029 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002031 &shsurf->rotation.transform.link);
2032 shsurf->saved_rotation_valid = false;
2033 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002034
2035 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002036 wl_list_remove(&shsurf->view->layer_link);
2037 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08002038}
2039
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002040static void
2041shell_unset_maximized(struct shell_surface *shsurf)
2042{
2043 struct workspace *ws;
2044 /* undo all maximized things here */
2045 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046 weston_view_set_position(shsurf->view,
2047 shsurf->saved_x,
2048 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002049
2050 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051 wl_list_insert(&shsurf->view->geometry.transformation_list,
2052 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002053 shsurf->saved_rotation_valid = false;
2054 }
2055
2056 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 wl_list_remove(&shsurf->view->layer_link);
2058 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002059}
2060
Pekka Paalanen98262232011-12-01 10:42:22 +02002061static int
2062reset_shell_surface_type(struct shell_surface *surface)
2063{
2064 switch (surface->type) {
2065 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08002066 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002067 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002068 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002069 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002070 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002071 case SHELL_SURFACE_NONE:
2072 case SHELL_SURFACE_TOPLEVEL:
2073 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002074 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002075 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02002076 break;
2077 }
2078
2079 surface->type = SHELL_SURFACE_NONE;
2080 return 0;
2081}
2082
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002083static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002084set_surface_type(struct shell_surface *shsurf)
2085{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002086 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002088
2089 reset_shell_surface_type(shsurf);
2090
2091 shsurf->type = shsurf->next_type;
2092 shsurf->next_type = SHELL_SURFACE_NONE;
2093
2094 switch (shsurf->type) {
2095 case SHELL_SURFACE_TOPLEVEL:
2096 break;
2097 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002098 if (pev)
2099 weston_view_set_position(shsurf->view,
2100 pev->geometry.x + shsurf->transient.x,
2101 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002102 break;
2103
2104 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002105 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 shsurf->saved_x = shsurf->view->geometry.x;
2107 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002108 shsurf->saved_position_valid = true;
2109
2110 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2111 wl_list_remove(&shsurf->rotation.transform.link);
2112 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002113 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002114 shsurf->saved_rotation_valid = true;
2115 }
2116 break;
2117
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002118 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119 weston_view_set_position(shsurf->view, shsurf->transient.x,
2120 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002121 break;
2122
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002123 default:
2124 break;
2125 }
2126}
2127
2128static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002129set_toplevel(struct shell_surface *shsurf)
2130{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002131 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002132}
2133
2134static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002135shell_surface_set_toplevel(struct wl_client *client,
2136 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002137{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002138 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002139
Tiago Vignattibc052c92012-04-19 16:18:18 +03002140 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002141}
2142
2143static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04002144set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002145 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04002146{
2147 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002148 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002149 shsurf->transient.x = x;
2150 shsurf->transient.y = y;
2151 shsurf->transient.flags = flags;
2152 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2153}
2154
2155static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002156shell_surface_set_transient(struct wl_client *client,
2157 struct wl_resource *resource,
2158 struct wl_resource *parent_resource,
2159 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002160{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002161 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002162 struct weston_surface *parent =
2163 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002164
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002165 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002166}
2167
Tiago Vignattibe143262012-04-16 17:31:41 +03002168static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002169shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002170{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002171 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002172}
2173
2174static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002175get_output_panel_height(struct desktop_shell *shell,
2176 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08002177{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08002179 int panel_height = 0;
2180
2181 if (!output)
2182 return 0;
2183
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2185 if (view->surface->output == output) {
2186 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08002187 break;
2188 }
2189 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002190
Juan Zhao96879df2012-02-07 08:45:41 +08002191 return panel_height;
2192}
2193
2194static void
2195shell_surface_set_maximized(struct wl_client *client,
2196 struct wl_resource *resource,
2197 struct wl_resource *output_resource )
2198{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002199 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08002200 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03002201 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08002202 uint32_t edges = 0, panel_height = 0;
2203
2204 /* get the default output, if the client set it as NULL
2205 check whether the ouput is available */
2206 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002207 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002208 else if (es->output)
2209 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002210 else
2211 shsurf->output = get_default_output(es->compositor);
2212
Tiago Vignattibe143262012-04-16 17:31:41 +03002213 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01002214 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002215 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002216
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002217 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002218 shsurf->output->width,
2219 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002220
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002221 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002222}
2223
Alex Wu21858432012-04-01 20:13:08 +08002224static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002225black_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 +08002226
Jason Ekstranda7af7042013-10-12 22:38:11 -05002227static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002228create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002229 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002230 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002231{
2232 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002234
2235 surface = weston_surface_create(ec);
2236 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002237 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002238 return NULL;
2239 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002240 view = weston_view_create(surface);
2241 if (surface == NULL) {
2242 weston_log("no memory\n");
2243 weston_surface_destroy(surface);
2244 return NULL;
2245 }
Alex Wu4539b082012-03-01 12:57:46 +08002246
Alex Wu21858432012-04-01 20:13:08 +08002247 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002248 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002249 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002250 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002251 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002252 pixman_region32_fini(&surface->input);
2253 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002254
Jason Ekstranda7af7042013-10-12 22:38:11 -05002255 weston_view_configure(view, x, y, w, h);
2256
2257 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002258}
2259
2260/* Create black surface and append it to the associated fullscreen surface.
2261 * Handle size dismatch and positioning according to the method. */
2262static void
2263shell_configure_fullscreen(struct shell_surface *shsurf)
2264{
2265 struct weston_output *output = shsurf->fullscreen_output;
2266 struct weston_surface *surface = shsurf->surface;
2267 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002268 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002269 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002270
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002271 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2272 restore_output_mode(output);
2273
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 if (!shsurf->fullscreen.black_view)
2275 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002276 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002277 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002278 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002279 output->width,
2280 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002281
Jason Ekstranda7af7042013-10-12 22:38:11 -05002282 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2283 wl_list_insert(&shsurf->view->layer_link,
2284 &shsurf->fullscreen.black_view->layer_link);
2285 shsurf->fullscreen.black_view->surface->output = output;
2286 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002287
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002289 &surf_width, &surf_height);
2290
Alex Wu4539b082012-03-01 12:57:46 +08002291 switch (shsurf->fullscreen.type) {
2292 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002293 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002295 break;
2296 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002297 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002298 if (output->width == surf_width &&
2299 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002300 weston_view_set_position(shsurf->view,
2301 output->x - surf_x,
2302 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002303 break;
2304 }
2305
Alex Wu4539b082012-03-01 12:57:46 +08002306 matrix = &shsurf->fullscreen.transform.matrix;
2307 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002308
Scott Moreau1bad5db2012-08-18 01:04:05 -06002309 output_aspect = (float) output->width /
2310 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002311 /* XXX: Use surf_width and surf_height here? */
2312 surface_aspect = (float) surface->width /
2313 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002314 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002315 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002316 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002317 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002318 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002319 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002320
Alex Wu4539b082012-03-01 12:57:46 +08002321 weston_matrix_scale(matrix, scale, scale, 1);
2322 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002323 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002324 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002325 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2326 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002327 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002328
Alex Wu4539b082012-03-01 12:57:46 +08002329 break;
2330 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002331 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002332 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002333 surf_width * surface->buffer_scale,
2334 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002335 shsurf->fullscreen.framerate};
2336
Hardening57388e42013-09-18 23:56:36 +02002337 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2338 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002339 weston_view_set_position(shsurf->view,
2340 output->x - surf_x,
2341 output->y - surf_y);
2342 weston_view_configure(shsurf->fullscreen.black_view,
2343 output->x - surf_x,
2344 output->y - surf_y,
2345 output->width,
2346 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002347 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002348 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002349 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002350 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002351 }
Alex Wubd3354b2012-04-17 17:20:49 +08002352 }
Alex Wu4539b082012-03-01 12:57:46 +08002353 break;
2354 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002355 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002356 break;
2357 default:
2358 break;
2359 }
2360}
2361
2362/* make the fullscreen and black surface at the top */
2363static void
2364shell_stack_fullscreen(struct shell_surface *shsurf)
2365{
Alex Wubd3354b2012-04-17 17:20:49 +08002366 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002367 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002368
Jason Ekstranda7af7042013-10-12 22:38:11 -05002369 wl_list_remove(&shsurf->view->layer_link);
2370 wl_list_insert(&shell->fullscreen_layer.view_list,
2371 &shsurf->view->layer_link);
2372 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002373
Jason Ekstranda7af7042013-10-12 22:38:11 -05002374 if (!shsurf->fullscreen.black_view)
2375 shsurf->fullscreen.black_view =
2376 create_black_surface(shsurf->surface->compositor,
2377 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002378 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002379 output->width,
2380 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002381
Jason Ekstranda7af7042013-10-12 22:38:11 -05002382 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2383 wl_list_insert(&shsurf->view->layer_link,
2384 &shsurf->fullscreen.black_view->layer_link);
2385 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002386}
2387
2388static void
2389shell_map_fullscreen(struct shell_surface *shsurf)
2390{
Alex Wu4539b082012-03-01 12:57:46 +08002391 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002392 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002393}
2394
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002395static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002396set_fullscreen(struct shell_surface *shsurf,
2397 uint32_t method,
2398 uint32_t framerate,
2399 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002400{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002401 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002402
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002403 if (output)
2404 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002405 else if (es->output)
2406 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002407 else
2408 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002409
Alex Wu4539b082012-03-01 12:57:46 +08002410 shsurf->fullscreen_output = shsurf->output;
2411 shsurf->fullscreen.type = method;
2412 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002413 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002414
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002415 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002416 shsurf->output->width,
2417 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002418}
2419
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002420static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002421shell_surface_set_fullscreen(struct wl_client *client,
2422 struct wl_resource *resource,
2423 uint32_t method,
2424 uint32_t framerate,
2425 struct wl_resource *output_resource)
2426{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002427 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002428 struct weston_output *output;
2429
2430 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002431 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002432 else
2433 output = NULL;
2434
2435 set_fullscreen(shsurf, method, framerate, output);
2436}
2437
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002438static void
2439set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2440{
2441 /* XXX: using the same fields for transient type */
2442 shsurf->transient.x = x;
2443 shsurf->transient.y = y;
2444 shsurf->transient.flags = flags;
2445 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2446}
2447
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002448static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002449
2450static void
2451destroy_shell_seat(struct wl_listener *listener, void *data)
2452{
2453 struct shell_seat *shseat =
2454 container_of(listener,
2455 struct shell_seat, seat_destroy_listener);
2456 struct shell_surface *shsurf, *prev = NULL;
2457
2458 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002459 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002460 shseat->popup_grab.client = NULL;
2461
2462 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2463 shsurf->popup.shseat = NULL;
2464 if (prev) {
2465 wl_list_init(&prev->popup.grab_link);
2466 }
2467 prev = shsurf;
2468 }
2469 wl_list_init(&prev->popup.grab_link);
2470 }
2471
2472 wl_list_remove(&shseat->seat_destroy_listener.link);
2473 free(shseat);
2474}
2475
2476static struct shell_seat *
2477create_shell_seat(struct weston_seat *seat)
2478{
2479 struct shell_seat *shseat;
2480
2481 shseat = calloc(1, sizeof *shseat);
2482 if (!shseat) {
2483 weston_log("no memory to allocate shell seat\n");
2484 return NULL;
2485 }
2486
2487 shseat->seat = seat;
2488 wl_list_init(&shseat->popup_grab.surfaces_list);
2489
2490 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2491 wl_signal_add(&seat->destroy_signal,
2492 &shseat->seat_destroy_listener);
2493
2494 return shseat;
2495}
2496
2497static struct shell_seat *
2498get_shell_seat(struct weston_seat *seat)
2499{
2500 struct wl_listener *listener;
2501
2502 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2503 if (listener == NULL)
2504 return create_shell_seat(seat);
2505
2506 return container_of(listener,
2507 struct shell_seat, seat_destroy_listener);
2508}
2509
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002510static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002511popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002512{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002513 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002514 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002515 struct shell_seat *shseat =
2516 container_of(grab, struct shell_seat, popup_grab.grab);
2517 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002518 wl_fixed_t sx, sy;
2519
Jason Ekstranda7af7042013-10-12 22:38:11 -05002520 view = weston_compositor_pick_view(pointer->seat->compositor,
2521 pointer->x, pointer->y,
2522 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002523
Jason Ekstranda7af7042013-10-12 22:38:11 -05002524 if (view && view->surface->resource &&
2525 wl_resource_get_client(view->surface->resource) == client) {
2526 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002527 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002528 weston_pointer_set_focus(pointer, NULL,
2529 wl_fixed_from_int(0),
2530 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002531 }
2532}
2533
2534static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002535popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2536 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002537{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002538 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002539 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002540 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002541
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002542 weston_pointer_move(pointer, x, y);
2543
Neil Roberts96d790e2013-09-19 17:32:00 +01002544 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545 weston_view_from_global_fixed(pointer->focus,
2546 pointer->x, pointer->y,
2547 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002548 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002549 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002550}
2551
2552static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002553popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002554 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002555{
2556 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002557 struct shell_seat *shseat =
2558 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002559 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002560 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002561 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002562 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002563
Neil Roberts96d790e2013-09-19 17:32:00 +01002564 resource_list = &grab->pointer->focus_resource_list;
2565 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002566 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002567 wl_resource_for_each(resource, resource_list) {
2568 wl_pointer_send_button(resource, serial,
2569 time, button, state);
2570 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002571 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002572 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002573 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002574 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002575 }
2576
Daniel Stone4dbadb12012-05-30 16:31:51 +01002577 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002578 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002579}
2580
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002581static void
2582popup_grab_cancel(struct weston_pointer_grab *grab)
2583{
2584 popup_grab_end(grab->pointer);
2585}
2586
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002587static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002588 popup_grab_focus,
2589 popup_grab_motion,
2590 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002591 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002592};
2593
2594static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002595popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002596{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002597 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002598 struct shell_seat *shseat =
2599 container_of(grab, struct shell_seat, popup_grab.grab);
2600 struct shell_surface *shsurf;
2601 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002602
2603 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002604 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002605 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002606 shseat->popup_grab.grab.interface = NULL;
2607 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002608 /* Send the popup_done event to all the popups open */
2609 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002610 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002611 shsurf->popup.shseat = NULL;
2612 if (prev) {
2613 wl_list_init(&prev->popup.grab_link);
2614 }
2615 prev = shsurf;
2616 }
2617 wl_list_init(&prev->popup.grab_link);
2618 wl_list_init(&shseat->popup_grab.surfaces_list);
2619 }
2620}
2621
2622static void
2623add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2624{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002625 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002626
2627 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002628 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002629 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002630 /* We must make sure here that this popup was opened after
2631 * a mouse press, and not just by moving around with other
2632 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002633 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002634 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002635
Rob Bradforddfe31052013-06-26 19:49:11 +01002636 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002637 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002638 } else {
2639 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002640 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002641}
2642
2643static void
2644remove_popup_grab(struct shell_surface *shsurf)
2645{
2646 struct shell_seat *shseat = shsurf->popup.shseat;
2647
2648 wl_list_remove(&shsurf->popup.grab_link);
2649 wl_list_init(&shsurf->popup.grab_link);
2650 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002651 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002652 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002653 }
2654}
2655
2656static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002657shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002658{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002659 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002660 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002661
Jason Ekstranda7af7042013-10-12 22:38:11 -05002662 shsurf->surface->output = parent_view->output;
2663 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002664
Jason Ekstranda7af7042013-10-12 22:38:11 -05002665 weston_view_set_transform_parent(shsurf->view, parent_view);
2666 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2667 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002668
Kristian Høgsberge3148752013-05-06 23:19:49 -04002669 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002670 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002671 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002672 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002673 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002674 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002675}
2676
2677static void
2678shell_surface_set_popup(struct wl_client *client,
2679 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002680 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002681 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002682 struct wl_resource *parent_resource,
2683 int32_t x, int32_t y, uint32_t flags)
2684{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002685 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002686
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002687 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002688 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002689 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002690 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002691 shsurf->popup.x = x;
2692 shsurf->popup.y = y;
2693}
2694
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002695static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002696 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002697 shell_surface_move,
2698 shell_surface_resize,
2699 shell_surface_set_toplevel,
2700 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002701 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002702 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002703 shell_surface_set_maximized,
2704 shell_surface_set_title,
2705 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002706};
2707
2708static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002709destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002710{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002711 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2712
Giulio Camuffo5085a752013-03-25 21:42:45 +01002713 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2714 remove_popup_grab(shsurf);
2715 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002716
Alex Wubd3354b2012-04-17 17:20:49 +08002717 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002718 shell_surface_is_top_fullscreen(shsurf))
2719 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002720
Jason Ekstranda7af7042013-10-12 22:38:11 -05002721 if (shsurf->fullscreen.black_view)
2722 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002723
Alex Wubd3354b2012-04-17 17:20:49 +08002724 /* As destroy_resource() use wl_list_for_each_safe(),
2725 * we can always remove the listener.
2726 */
2727 wl_list_remove(&shsurf->surface_destroy_listener.link);
2728 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002729 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002730 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002731
Jason Ekstranda7af7042013-10-12 22:38:11 -05002732 weston_view_destroy(shsurf->view);
2733
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002734 wl_list_remove(&shsurf->link);
2735 free(shsurf);
2736}
2737
2738static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002739shell_destroy_shell_surface(struct wl_resource *resource)
2740{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002741 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002742
2743 destroy_shell_surface(shsurf);
2744}
2745
2746static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002747shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002748{
2749 struct shell_surface *shsurf = container_of(listener,
2750 struct shell_surface,
2751 surface_destroy_listener);
2752
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002753 if (shsurf->resource)
2754 wl_resource_destroy(shsurf->resource);
2755 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002756 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002757}
2758
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002759static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002760shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002761
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002762static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002763get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002764{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002765 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002766 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002767 else
2768 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002769}
2770
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002771static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002772create_shell_surface(void *shell, struct weston_surface *surface,
2773 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002774{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002775 struct shell_surface *shsurf;
2776
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002777 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002778 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002779 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002780 }
2781
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002782 shsurf = calloc(1, sizeof *shsurf);
2783 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002784 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002785 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002786 }
2787
Jason Ekstranda7af7042013-10-12 22:38:11 -05002788 shsurf->view = weston_view_create(surface);
2789 if (!shsurf->view) {
2790 weston_log("no memory to allocate shell surface\n");
2791 free(shsurf);
2792 return NULL;
2793 }
2794
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002795 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002796 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002797
Tiago Vignattibc052c92012-04-19 16:18:18 +03002798 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002799 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002800 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002801 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002802 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002803 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2804 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002805 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002806 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002807 wl_list_init(&shsurf->fullscreen.transform.link);
2808
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002809 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002810 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002811 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002812 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002813
2814 /* init link so its safe to always remove it in destroy_shell_surface */
2815 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002816 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002817
Pekka Paalanen460099f2012-01-20 16:48:25 +02002818 /* empty when not in use */
2819 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002820 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002821
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002822 wl_list_init(&shsurf->workspace_transform.link);
2823
Pekka Paalanen98262232011-12-01 10:42:22 +02002824 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002825 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002826
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002827 shsurf->client = client;
2828
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002829 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002830}
2831
Jason Ekstranda7af7042013-10-12 22:38:11 -05002832static struct weston_view *
2833get_primary_view(void *shell, struct shell_surface *shsurf)
2834{
2835 return shsurf->view;
2836}
2837
Tiago Vignattibc052c92012-04-19 16:18:18 +03002838static void
2839shell_get_shell_surface(struct wl_client *client,
2840 struct wl_resource *resource,
2841 uint32_t id,
2842 struct wl_resource *surface_resource)
2843{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002844 struct weston_surface *surface =
2845 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002846 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002847 struct shell_surface *shsurf;
2848
2849 if (get_shell_surface(surface)) {
2850 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002851 WL_DISPLAY_ERROR_INVALID_OBJECT,
2852 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002853 return;
2854 }
2855
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002856 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002857 if (!shsurf) {
2858 wl_resource_post_error(surface_resource,
2859 WL_DISPLAY_ERROR_INVALID_OBJECT,
2860 "surface->configure already set");
2861 return;
2862 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002863
Jason Ekstranda85118c2013-06-27 20:17:02 -05002864 shsurf->resource =
2865 wl_resource_create(client,
2866 &wl_shell_surface_interface, 1, id);
2867 wl_resource_set_implementation(shsurf->resource,
2868 &shell_surface_implementation,
2869 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002870}
2871
2872static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002873 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002874};
2875
Kristian Høgsberg07937562011-04-12 17:25:42 -04002876static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002877shell_fade(struct desktop_shell *shell, enum fade_type type);
2878
2879static int
2880screensaver_timeout(void *data)
2881{
2882 struct desktop_shell *shell = data;
2883
2884 shell_fade(shell, FADE_OUT);
2885
2886 return 1;
2887}
2888
2889static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002890handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002891{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002892 struct desktop_shell *shell =
2893 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002894
Pekka Paalanen18027e52011-12-02 16:31:49 +02002895 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002896
2897 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002898 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002899}
2900
2901static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002902launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002903{
2904 if (shell->screensaver.binding)
2905 return;
2906
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002907 if (!shell->screensaver.path) {
2908 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002909 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002910 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002911
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002912 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002913 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002914 return;
2915 }
2916
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002917 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002918 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002919 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002920 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002921}
2922
2923static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002924terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002925{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002926 if (shell->screensaver.process.pid == 0)
2927 return;
2928
2929 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002930}
2931
2932static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002933configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002934{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002936
Giulio Camuffo184df502013-02-21 11:29:21 +01002937 if (width == 0)
2938 return;
2939
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2941 if (v->output == ev->output && v != ev) {
2942 weston_view_unmap(v);
2943 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002944 }
2945 }
2946
Jason Ekstranda7af7042013-10-12 22:38:11 -05002947 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002948
Jason Ekstranda7af7042013-10-12 22:38:11 -05002949 if (wl_list_empty(&ev->layer_link)) {
2950 wl_list_insert(&layer->view_list, &ev->layer_link);
2951 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002952 }
2953}
2954
2955static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002956background_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 -04002957{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002958 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002959 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002960
Jason Ekstranda7af7042013-10-12 22:38:11 -05002961 view = container_of(es->views.next, struct weston_view, surface_link);
2962
2963 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002964}
2965
2966static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002967desktop_shell_set_background(struct wl_client *client,
2968 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002969 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002970 struct wl_resource *surface_resource)
2971{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002972 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002973 struct weston_surface *surface =
2974 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002975 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002976
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002977 if (surface->configure) {
2978 wl_resource_post_error(surface_resource,
2979 WL_DISPLAY_ERROR_INVALID_OBJECT,
2980 "surface role already assigned");
2981 return;
2982 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002983
Jason Ekstranda7af7042013-10-12 22:38:11 -05002984 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2985 weston_view_destroy(view);
2986 view = weston_view_create(surface);
2987
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002988 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002989 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002990 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002992 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002993 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002994 surface->output->width,
2995 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002996}
2997
2998static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002999panel_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 -04003000{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003001 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003002 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003003
Jason Ekstranda7af7042013-10-12 22:38:11 -05003004 view = container_of(es->views.next, struct weston_view, surface_link);
3005
3006 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003007}
3008
3009static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003010desktop_shell_set_panel(struct wl_client *client,
3011 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003012 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003013 struct wl_resource *surface_resource)
3014{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003015 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003016 struct weston_surface *surface =
3017 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003018 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003019
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003020 if (surface->configure) {
3021 wl_resource_post_error(surface_resource,
3022 WL_DISPLAY_ERROR_INVALID_OBJECT,
3023 "surface role already assigned");
3024 return;
3025 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003026
Jason Ekstranda7af7042013-10-12 22:38:11 -05003027 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3028 weston_view_destroy(view);
3029 view = weston_view_create(surface);
3030
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003031 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003032 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003033 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003034 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003035 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003036 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003037 surface->output->width,
3038 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003039}
3040
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003041static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003042lock_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 -04003043{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003044 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045 struct weston_view *view;
3046
3047 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003048
Giulio Camuffo184df502013-02-21 11:29:21 +01003049 if (width == 0)
3050 return;
3051
Jason Ekstranda7af7042013-10-12 22:38:11 -05003052 surface->width = width;
3053 surface->height = height;
3054 view->geometry.width = width;
3055 view->geometry.height = height;
3056 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003057
3058 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003059 wl_list_insert(&shell->lock_layer.view_list,
3060 &view->layer_link);
3061 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003062 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003063 }
3064}
3065
3066static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003067handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003068{
Tiago Vignattibe143262012-04-16 17:31:41 +03003069 struct desktop_shell *shell =
3070 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003071
Martin Minarik6d118362012-06-07 18:01:59 +02003072 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003073 shell->lock_surface = NULL;
3074}
3075
3076static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003077desktop_shell_set_lock_surface(struct wl_client *client,
3078 struct wl_resource *resource,
3079 struct wl_resource *surface_resource)
3080{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003081 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003082 struct weston_surface *surface =
3083 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003084
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003085 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003086
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003087 if (!shell->locked)
3088 return;
3089
Pekka Paalanen98262232011-12-01 10:42:22 +02003090 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003091
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003092 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003093 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003094 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003095
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003096 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003097 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003098 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003099}
3100
3101static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003102resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003103{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003104 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003105
Pekka Paalanen77346a62011-11-30 16:26:35 +02003106 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003107
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003108 wl_list_remove(&shell->lock_layer.link);
3109 wl_list_insert(&shell->compositor->cursor_layer.link,
3110 &shell->fullscreen_layer.link);
3111 wl_list_insert(&shell->fullscreen_layer.link,
3112 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003113 if (shell->showing_input_panels) {
3114 wl_list_insert(&shell->panel_layer.link,
3115 &shell->input_panel_layer.link);
3116 wl_list_insert(&shell->input_panel_layer.link,
3117 &ws->layer.link);
3118 } else {
3119 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3120 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003121
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003122 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003123
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003124 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003125 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003126 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003127}
3128
3129static void
3130desktop_shell_unlock(struct wl_client *client,
3131 struct wl_resource *resource)
3132{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003133 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003134
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003135 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003136
3137 if (shell->locked)
3138 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003139}
3140
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003141static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003142desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003143 struct wl_resource *resource,
3144 struct wl_resource *surface_resource)
3145{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003146 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003147
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003148 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003149 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003150}
3151
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003152static void
3153desktop_shell_desktop_ready(struct wl_client *client,
3154 struct wl_resource *resource)
3155{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003156 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003157
3158 shell_fade_startup(shell);
3159}
3160
Kristian Høgsberg75840622011-09-06 13:48:16 -04003161static const struct desktop_shell_interface desktop_shell_implementation = {
3162 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003163 desktop_shell_set_panel,
3164 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003165 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003166 desktop_shell_set_grab_surface,
3167 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003168};
3169
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003170static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003171get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003172{
3173 struct shell_surface *shsurf;
3174
3175 shsurf = get_shell_surface(surface);
3176 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003177 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003178 return shsurf->type;
3179}
3180
Kristian Høgsberg75840622011-09-06 13:48:16 -04003181static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003182move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003183{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003184 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003185 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003186 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003187
Pekka Paalanen01388e22013-04-25 13:57:44 +03003188 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003189 if (surface == NULL)
3190 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003191
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003192 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003193 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3194 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003195 return;
3196
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003197 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003198}
3199
3200static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003201touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3202{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003203 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003204 struct weston_surface *surface;
3205 struct shell_surface *shsurf;
3206
3207 surface = weston_surface_get_main_surface(focus);
3208 if (surface == NULL)
3209 return;
3210
3211 shsurf = get_shell_surface(surface);
3212 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3213 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3214 return;
3215
3216 surface_touch_move(shsurf, (struct weston_seat *) seat);
3217}
3218
3219static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003220resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003221{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003222 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003223 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003224 uint32_t edges = 0;
3225 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003226 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003227
Pekka Paalanen01388e22013-04-25 13:57:44 +03003228 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003229 if (surface == NULL)
3230 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003231
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003232 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003233 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3234 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003235 return;
3236
Jason Ekstranda7af7042013-10-12 22:38:11 -05003237 weston_view_from_global(shsurf->view,
3238 wl_fixed_to_int(seat->pointer->grab_x),
3239 wl_fixed_to_int(seat->pointer->grab_y),
3240 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003241
Jason Ekstranda7af7042013-10-12 22:38:11 -05003242 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003243 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003244 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003245 edges |= 0;
3246 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003247 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003248
Jason Ekstranda7af7042013-10-12 22:38:11 -05003249 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003250 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003251 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003252 edges |= 0;
3253 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003254 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003255
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003256 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003257}
3258
3259static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003260surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003261 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003262{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003263 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003264 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003265 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003266 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003267
Pekka Paalanen01388e22013-04-25 13:57:44 +03003268 /* XXX: broken for windows containing sub-surfaces */
3269 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003270 if (surface == NULL)
3271 return;
3272
3273 shsurf = get_shell_surface(surface);
3274 if (!shsurf)
3275 return;
3276
Jason Ekstranda7af7042013-10-12 22:38:11 -05003277 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003278
Jason Ekstranda7af7042013-10-12 22:38:11 -05003279 if (shsurf->view->alpha > 1.0)
3280 shsurf->view->alpha = 1.0;
3281 if (shsurf->view->alpha < step)
3282 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003283
Jason Ekstranda7af7042013-10-12 22:38:11 -05003284 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003285 weston_surface_damage(surface);
3286}
3287
3288static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003289do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003290 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003291{
Daniel Stone37816df2012-05-16 18:45:18 +01003292 struct weston_seat *ws = (struct weston_seat *) seat;
3293 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003294 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003295 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003296
3297 wl_list_for_each(output, &compositor->output_list, link) {
3298 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003299 wl_fixed_to_double(seat->pointer->x),
3300 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003301 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003302 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003303 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003304 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003305 increment = -output->zoom.increment;
3306 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003307 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003308 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003309 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003310 else
3311 increment = 0;
3312
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003313 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003314
Scott Moreaue6603982012-06-11 13:07:51 -06003315 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003316 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003317 else if (output->zoom.level > output->zoom.max_level)
3318 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003319 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003320 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003321 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003322
Scott Moreaue6603982012-06-11 13:07:51 -06003323 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003324
Jason Ekstranda7af7042013-10-12 22:38:11 -05003325 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003326 }
3327 }
3328}
3329
Scott Moreauccbf29d2012-02-22 14:21:41 -07003330static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003331zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003332 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003333{
3334 do_zoom(seat, time, 0, axis, value);
3335}
3336
3337static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003338zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003339 void *data)
3340{
3341 do_zoom(seat, time, key, 0, 0);
3342}
3343
3344static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003345terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003346 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003347{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003348 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003349
Daniel Stone325fc2d2012-05-30 16:31:58 +01003350 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003351}
3352
3353static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003354lower_fullscreen_layer(struct desktop_shell *shell);
3355
3356struct alt_tab {
3357 struct desktop_shell *shell;
3358 struct weston_keyboard_grab grab;
3359
3360 struct wl_list *current;
3361
3362 struct wl_list preview_list;
3363};
3364
3365struct alt_tab_preview {
3366 struct alt_tab *alt_tab;
3367
3368 struct weston_view *view;
3369 struct weston_transform transform;
3370
3371 struct wl_listener listener;
3372
3373 struct wl_list link;
3374};
3375
3376static void
3377alt_tab_next(struct alt_tab *alt_tab)
3378{
3379 alt_tab->current = alt_tab->current->next;
3380
3381 /* Make sure we're not pointing to the list header e.g. after
3382 * cycling through the whole list. */
3383 if (alt_tab->current->next == alt_tab->preview_list.next)
3384 alt_tab->current = alt_tab->current->next;
3385}
3386
3387static void
3388alt_tab_destroy(struct alt_tab *alt_tab)
3389{
3390 struct alt_tab_preview *preview, *next;
3391 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3392
3393 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3394 preview = wl_container_of(alt_tab->current, preview, link);
3395
3396 activate(alt_tab->shell, preview->view->surface,
3397 (struct weston_seat *) keyboard->seat);
3398 }
3399
3400 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3401 wl_list_remove(&preview->link);
3402 wl_list_remove(&preview->listener.link);
3403 weston_view_destroy(preview->view);
3404 free(preview);
3405 }
3406
3407 weston_keyboard_end_grab(keyboard);
3408 if (keyboard->input_method_resource)
3409 keyboard->grab = &keyboard->input_method_grab;
3410
3411 free(alt_tab);
3412}
3413
3414static void
3415alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3416{
3417 struct alt_tab_preview *preview =
3418 container_of(listener, struct alt_tab_preview, listener);
3419 struct alt_tab *alt_tab = preview->alt_tab;
3420 int advance = 0;
3421
3422 /* If the preview that we're removing is the currently selected one,
3423 * we want to move to the next one. So we move to ->prev and then
3424 * call _next() after removing the preview. */
3425 if (alt_tab->current == &preview->link) {
3426 alt_tab->current = alt_tab->current->prev;
3427 advance = 1;
3428 }
3429
3430 wl_list_remove(&preview->listener.link);
3431 wl_list_remove(&preview->link);
3432
3433 free(preview);
3434
3435 if (advance)
3436 alt_tab_next(alt_tab);
3437
3438 /* If the last preview goes away, stop the alt-tab */
3439 if (wl_list_empty(alt_tab->current))
3440 alt_tab_destroy(alt_tab);
3441}
3442
3443static void
3444alt_tab_key(struct weston_keyboard_grab *grab,
3445 uint32_t time, uint32_t key, uint32_t state_w)
3446{
3447 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3448 enum wl_keyboard_key_state state = state_w;
3449
3450 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3451 alt_tab_next(alt_tab);
3452}
3453
3454static void
3455alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3456 uint32_t mods_depressed, uint32_t mods_latched,
3457 uint32_t mods_locked, uint32_t group)
3458{
3459 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3460 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3461
3462 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3463 alt_tab_destroy(alt_tab);
3464}
3465
3466static void
3467alt_tab_cancel(struct weston_keyboard_grab *grab)
3468{
3469 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3470
3471 alt_tab_destroy(alt_tab);
3472}
3473
3474static const struct weston_keyboard_grab_interface alt_tab_grab = {
3475 alt_tab_key,
3476 alt_tab_modifier,
3477 alt_tab_cancel,
3478};
3479
3480static int
3481view_for_alt_tab(struct weston_view *view)
3482{
3483 if (!get_shell_surface(view->surface))
3484 return 0;
3485
3486 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3487 return 0;
3488
3489 if (view != get_default_view(view->surface))
3490 return 0;
3491
3492 return 1;
3493}
3494
3495static void
3496alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3497 void *data)
3498{
3499 struct alt_tab *alt_tab;
3500 struct desktop_shell *shell = data;
3501 struct weston_output *output = get_default_output(shell->compositor);
3502 struct workspace *ws;
3503 struct weston_view *view;
3504 int num_surfaces = 0;
3505 int x, y, side, margin;
3506
3507 wl_list_for_each(view, &shell->compositor->view_list, link) {
3508
3509 if (view_for_alt_tab(view))
3510 num_surfaces++;
3511 }
3512
3513 if (!num_surfaces)
3514 return;
3515
3516 alt_tab = malloc(sizeof *alt_tab);
3517 if (!alt_tab)
3518 return;
3519
3520 alt_tab->shell = shell;
3521 wl_list_init(&alt_tab->preview_list);
3522 alt_tab->current = &alt_tab->preview_list;
3523
3524 restore_all_output_modes(shell->compositor);
3525 lower_fullscreen_layer(alt_tab->shell);
3526
3527 alt_tab->grab.interface = &alt_tab_grab;
3528 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3529 weston_keyboard_set_focus(seat->keyboard, NULL);
3530
3531 ws = get_current_workspace(shell);
3532
3533 /* FIXME: add some visual candy e.g. prelight the selected view
3534 * and/or add a black surrounding background à la gnome-shell */
3535
3536 /* Determine the size for each preview */
3537 side = output->width / num_surfaces;
3538 if (side > 200)
3539 side = 200;
3540 margin = side / 4;
3541 side -= margin;
3542
3543 x = margin;
3544 y = (output->height - side) / 2;
3545
3546 /* Create a view for each surface */
3547 wl_list_for_each(view, &shell->compositor->view_list, link) {
3548 struct alt_tab_preview *preview;
3549 struct weston_view *v;
3550 float scale;
3551
3552 if (!view_for_alt_tab(view))
3553 continue;
3554
3555 preview = malloc(sizeof *preview);
3556 if (!preview) {
3557 alt_tab_destroy(alt_tab);
3558 return;
3559 }
3560
3561 preview->alt_tab = alt_tab;
3562
3563 preview->view = v = weston_view_create(view->surface);
3564 v->output = view->output;
3565 weston_view_restack(v, &ws->layer.view_list);
3566 weston_view_configure(v, x, y, view->geometry.width, view->geometry.height);
3567
3568 preview->listener.notify = alt_tab_handle_surface_destroy;
3569 wl_signal_add(&v->destroy_signal, &preview->listener);
3570
3571 if (view->geometry.width > view->geometry.height)
3572 scale = side / (float) view->geometry.width;
3573 else
3574 scale = side / (float) view->geometry.height;
3575
3576 wl_list_insert(&v->geometry.transformation_list,
3577 &preview->transform.link);
3578 weston_matrix_init(&preview->transform.matrix);
3579 weston_matrix_scale(&preview->transform.matrix,
3580 scale, scale, 1.0f);
3581
3582 weston_view_geometry_dirty(v);
3583 weston_compositor_schedule_repaint(v->surface->compositor);
3584
3585 /* Insert at the end of the list */
3586 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3587
3588 x += side + margin;
3589 }
3590
3591 /* Start at the second preview so a simple <alt>tab changes window.
3592 * We set `current' to the first preview and not the second because
3593 * we're going to receive a key press callback for the initial
3594 * <alt>tab which will make `current' point to the second element. */
3595 alt_tab_next(alt_tab);
3596}
3597
3598static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003599rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3600 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003601{
3602 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003603 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003604 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003605 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003606 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003607
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003608 weston_pointer_move(pointer, x, y);
3609
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003610 if (!shsurf)
3611 return;
3612
Jason Ekstranda7af7042013-10-12 22:38:11 -05003613 cx = 0.5f * shsurf->view->geometry.width;
3614 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003615
Daniel Stone37816df2012-05-16 18:45:18 +01003616 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3617 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003618 r = sqrtf(dx * dx + dy * dy);
3619
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003620 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003621 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003622
3623 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003624 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003625 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003626
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003627 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003628 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003629
3630 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003631 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003632 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003633 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003634 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003635
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003636 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003637 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003638 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003639 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003640 wl_list_init(&shsurf->rotation.transform.link);
3641 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003642 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003643 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003644
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003645 /* We need to adjust the position of the surface
3646 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003647 cposx = shsurf->view->geometry.x + cx;
3648 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003649 dposx = rotate->center.x - cposx;
3650 dposy = rotate->center.y - cposy;
3651 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003652 weston_view_set_position(shsurf->view,
3653 shsurf->view->geometry.x + dposx,
3654 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003655 }
3656
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003657 /* Repaint implies weston_surface_update_transform(), which
3658 * lazily applies the damage due to rotation update.
3659 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003660 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003661}
3662
3663static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003664rotate_grab_button(struct weston_pointer_grab *grab,
3665 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003666{
3667 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003668 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003669 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003670 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003671 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003672
Daniel Stone4dbadb12012-05-30 16:31:51 +01003673 if (pointer->button_count == 0 &&
3674 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003675 if (shsurf)
3676 weston_matrix_multiply(&shsurf->rotation.rotation,
3677 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003678 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003679 free(rotate);
3680 }
3681}
3682
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003683static void
3684rotate_grab_cancel(struct weston_pointer_grab *grab)
3685{
3686 struct rotate_grab *rotate =
3687 container_of(grab, struct rotate_grab, base.grab);
3688
3689 shell_grab_end(&rotate->base);
3690 free(rotate);
3691}
3692
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003693static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003694 noop_grab_focus,
3695 rotate_grab_motion,
3696 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003697 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003698};
3699
3700static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003701surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003702{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003703 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003704 float dx, dy;
3705 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003706
Pekka Paalanen460099f2012-01-20 16:48:25 +02003707 rotate = malloc(sizeof *rotate);
3708 if (!rotate)
3709 return;
3710
Jason Ekstranda7af7042013-10-12 22:38:11 -05003711 weston_view_to_global_float(surface->view,
3712 surface->view->geometry.width * 0.5f,
3713 surface->view->geometry.height * 0.5f,
3714 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003715
Daniel Stone37816df2012-05-16 18:45:18 +01003716 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3717 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003718 r = sqrtf(dx * dx + dy * dy);
3719 if (r > 20.0f) {
3720 struct weston_matrix inverse;
3721
3722 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003723 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003724 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003725
3726 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003727 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003728 } else {
3729 weston_matrix_init(&surface->rotation.rotation);
3730 weston_matrix_init(&rotate->rotation);
3731 }
3732
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003733 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3734 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003735}
3736
3737static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003738rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003739 void *data)
3740{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003741 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003742 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003743 struct shell_surface *surface;
3744
Pekka Paalanen01388e22013-04-25 13:57:44 +03003745 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003746 if (base_surface == NULL)
3747 return;
3748
3749 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003750 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3751 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003752 return;
3753
3754 surface_rotate(surface, seat);
3755}
3756
3757static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003758lower_fullscreen_layer(struct desktop_shell *shell)
3759{
3760 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003761 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003762
3763 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003764 wl_list_for_each_reverse_safe(view, prev,
3765 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003766 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003767 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003768}
3769
3770static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003771activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003772 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003773{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003774 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003775 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003776 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003777 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003778 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003779
Pekka Paalanen01388e22013-04-25 13:57:44 +03003780 main_surface = weston_surface_get_main_surface(es);
3781
Daniel Stone37816df2012-05-16 18:45:18 +01003782 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003783
Jonas Ådahl8538b222012-08-29 22:13:03 +02003784 state = ensure_focus_state(shell, seat);
3785 if (state == NULL)
3786 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003787
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003788 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003789 state->keyboard_focus = es;
3790 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003791 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003792
Pekka Paalanen01388e22013-04-25 13:57:44 +03003793 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003794 case SHELL_SURFACE_FULLSCREEN:
3795 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003796 shell_stack_fullscreen(get_shell_surface(main_surface));
3797 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003798 return;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003799 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003800 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003801 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003802 main_view = get_default_view(main_surface);
3803 if (main_view)
3804 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003805 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003806 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003807
3808 if (shell->focus_animation_type != ANIMATION_NONE)
3809 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003810}
3811
Alex Wu21858432012-04-01 20:13:08 +08003812/* no-op func for checking black surface */
3813static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003814black_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 +08003815{
3816}
3817
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003818static bool
Alex Wu21858432012-04-01 20:13:08 +08003819is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3820{
3821 if (es->configure == black_surface_configure) {
3822 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003823 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003824 return true;
3825 }
3826 return false;
3827}
3828
Kristian Høgsberg75840622011-09-06 13:48:16 -04003829static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003830activate_binding(struct weston_seat *seat,
3831 struct desktop_shell *shell,
3832 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003833{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003834 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003835
Alex Wu9c35e6b2012-03-05 14:13:13 +08003836 if (!focus)
3837 return;
3838
Pekka Paalanen01388e22013-04-25 13:57:44 +03003839 if (is_black_surface(focus, &main_surface))
3840 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003841
Pekka Paalanen01388e22013-04-25 13:57:44 +03003842 main_surface = weston_surface_get_main_surface(focus);
3843 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003844 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003845
Neil Robertsa28c6932013-10-03 16:43:04 +01003846 activate(shell, focus, seat);
3847}
3848
3849static void
3850click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3851 void *data)
3852{
3853 if (seat->pointer->grab != &seat->pointer->default_grab)
3854 return;
3855
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003856 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003857}
3858
3859static void
3860touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3861{
3862 if (seat->touch->grab != &seat->touch->default_grab)
3863 return;
3864
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003865 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003866}
3867
3868static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003869lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003870{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003871 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003872
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003873 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003874 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003875 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003876 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003877
3878 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003879
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003880 /* Hide all surfaces by removing the fullscreen, panel and
3881 * toplevel layers. This way nothing else can show or receive
3882 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003883
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003884 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003885 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003886 if (shell->showing_input_panels)
3887 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003888 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003889 wl_list_insert(&shell->compositor->cursor_layer.link,
3890 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003891
Pekka Paalanen77346a62011-11-30 16:26:35 +02003892 launch_screensaver(shell);
3893
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003894 /* TODO: disable bindings that should not work while locked. */
3895
3896 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003897}
3898
3899static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003900unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003901{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003902 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003903 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003904 return;
3905 }
3906
3907 /* If desktop-shell client has gone away, unlock immediately. */
3908 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003909 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003910 return;
3911 }
3912
3913 if (shell->prepare_event_sent)
3914 return;
3915
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003916 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003917 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003918}
3919
3920static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003921shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003922{
3923 struct desktop_shell *shell = data;
3924
3925 shell->fade.animation = NULL;
3926
3927 switch (shell->fade.type) {
3928 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003929 weston_surface_destroy(shell->fade.view->surface);
3930 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003931 break;
3932 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003933 lock(shell);
3934 break;
3935 }
3936}
3937
Jason Ekstranda7af7042013-10-12 22:38:11 -05003938static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003939shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003940{
3941 struct weston_compositor *compositor = shell->compositor;
3942 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003943 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003944
3945 surface = weston_surface_create(compositor);
3946 if (!surface)
3947 return NULL;
3948
Jason Ekstranda7af7042013-10-12 22:38:11 -05003949 view = weston_view_create(surface);
3950 if (!view) {
3951 weston_surface_destroy(surface);
3952 return NULL;
3953 }
3954
3955 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003956 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003957 wl_list_insert(&compositor->fade_layer.view_list,
3958 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003959 pixman_region32_init(&surface->input);
3960
Jason Ekstranda7af7042013-10-12 22:38:11 -05003961 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003962}
3963
3964static void
3965shell_fade(struct desktop_shell *shell, enum fade_type type)
3966{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003967 float tint;
3968
3969 switch (type) {
3970 case FADE_IN:
3971 tint = 0.0;
3972 break;
3973 case FADE_OUT:
3974 tint = 1.0;
3975 break;
3976 default:
3977 weston_log("shell: invalid fade type\n");
3978 return;
3979 }
3980
3981 shell->fade.type = type;
3982
Jason Ekstranda7af7042013-10-12 22:38:11 -05003983 if (shell->fade.view == NULL) {
3984 shell->fade.view = shell_fade_create_surface(shell);
3985 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003986 return;
3987
Jason Ekstranda7af7042013-10-12 22:38:11 -05003988 shell->fade.view->alpha = 1.0 - tint;
3989 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003990 }
3991
3992 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003993 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003994 else
3995 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003996 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003997 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003998 shell_fade_done, shell);
3999}
4000
4001static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004002do_shell_fade_startup(void *data)
4003{
4004 struct desktop_shell *shell = data;
4005
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004006 if (shell->startup_animation_type == ANIMATION_FADE)
4007 shell_fade(shell, FADE_IN);
4008 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004009 weston_surface_destroy(shell->fade.view->surface);
4010 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004011 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004012}
4013
4014static void
4015shell_fade_startup(struct desktop_shell *shell)
4016{
4017 struct wl_event_loop *loop;
4018
4019 if (!shell->fade.startup_timer)
4020 return;
4021
4022 wl_event_source_remove(shell->fade.startup_timer);
4023 shell->fade.startup_timer = NULL;
4024
4025 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4026 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4027}
4028
4029static int
4030fade_startup_timeout(void *data)
4031{
4032 struct desktop_shell *shell = data;
4033
4034 shell_fade_startup(shell);
4035 return 0;
4036}
4037
4038static void
4039shell_fade_init(struct desktop_shell *shell)
4040{
4041 /* Make compositor output all black, and wait for the desktop-shell
4042 * client to signal it is ready, then fade in. The timer triggers a
4043 * fade-in, in case the desktop-shell client takes too long.
4044 */
4045
4046 struct wl_event_loop *loop;
4047
Jason Ekstranda7af7042013-10-12 22:38:11 -05004048 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004049 weston_log("%s: warning: fade surface already exists\n",
4050 __func__);
4051 return;
4052 }
4053
Jason Ekstranda7af7042013-10-12 22:38:11 -05004054 shell->fade.view = shell_fade_create_surface(shell);
4055 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004056 return;
4057
Jason Ekstranda7af7042013-10-12 22:38:11 -05004058 weston_view_update_transform(shell->fade.view);
4059 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004060
4061 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4062 shell->fade.startup_timer =
4063 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4064 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4065}
4066
4067static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004068idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004069{
4070 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004071 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004072
4073 shell_fade(shell, FADE_OUT);
4074 /* lock() is called from shell_fade_done() */
4075}
4076
4077static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004078wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004079{
4080 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004081 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004082
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004083 unlock(shell);
4084}
4085
4086static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004087show_input_panels(struct wl_listener *listener, void *data)
4088{
4089 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004090 container_of(listener, struct desktop_shell,
4091 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004092 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004093
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004094 shell->text_input.surface = (struct weston_surface*)data;
4095
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004096 if (shell->showing_input_panels)
4097 return;
4098
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004099 shell->showing_input_panels = true;
4100
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004101 if (!shell->locked)
4102 wl_list_insert(&shell->panel_layer.link,
4103 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004104
Jason Ekstranda7af7042013-10-12 22:38:11 -05004105 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004106 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004107 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004108 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004109 wl_list_insert(&shell->input_panel_layer.view_list,
4110 &ipsurf->view->layer_link);
4111 weston_view_geometry_dirty(ipsurf->view);
4112 weston_view_update_transform(ipsurf->view);
4113 weston_surface_damage(ipsurf->surface);
4114 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
4115 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004116 }
4117}
4118
4119static void
4120hide_input_panels(struct wl_listener *listener, void *data)
4121{
4122 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004123 container_of(listener, struct desktop_shell,
4124 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004125 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004126
Jan Arne Petersen61381972013-01-31 15:52:21 +01004127 if (!shell->showing_input_panels)
4128 return;
4129
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004130 shell->showing_input_panels = false;
4131
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004132 if (!shell->locked)
4133 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004134
Jason Ekstranda7af7042013-10-12 22:38:11 -05004135 wl_list_for_each_safe(view, next,
4136 &shell->input_panel_layer.view_list, layer_link)
4137 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004138}
4139
4140static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004141update_input_panels(struct wl_listener *listener, void *data)
4142{
4143 struct desktop_shell *shell =
4144 container_of(listener, struct desktop_shell,
4145 update_input_panel_listener);
4146
4147 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4148}
4149
4150static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004151center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004152{
Giulio Camuffob8366642013-04-25 13:57:46 +03004153 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004154 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004155
Jason Ekstranda7af7042013-10-12 22:38:11 -05004156 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004157
4158 x = output->x + (output->width - width) / 2 - surf_x / 2;
4159 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004160
Jason Ekstranda7af7042013-10-12 22:38:11 -05004161 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004162}
4163
4164static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004165weston_view_set_initial_position(struct weston_view *view,
4166 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004167{
4168 struct weston_compositor *compositor = shell->compositor;
4169 int ix = 0, iy = 0;
4170 int range_x, range_y;
4171 int dx, dy, x, y, panel_height;
4172 struct weston_output *output, *target_output = NULL;
4173 struct weston_seat *seat;
4174
4175 /* As a heuristic place the new window on the same output as the
4176 * pointer. Falling back to the output containing 0, 0.
4177 *
4178 * TODO: Do something clever for touch too?
4179 */
4180 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004181 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004182 ix = wl_fixed_to_int(seat->pointer->x);
4183 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004184 break;
4185 }
4186 }
4187
4188 wl_list_for_each(output, &compositor->output_list, link) {
4189 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4190 target_output = output;
4191 break;
4192 }
4193 }
4194
4195 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004196 weston_view_set_position(view, 10 + random() % 400,
4197 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004198 return;
4199 }
4200
4201 /* Valid range within output where the surface will still be onscreen.
4202 * If this is negative it means that the surface is bigger than
4203 * output.
4204 */
4205 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004206 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004207 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05004208 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004209
Rob Bradford4cb88c72012-08-13 15:18:44 +01004210 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004211 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004212 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004213 dx = 0;
4214
4215 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004216 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004217 else
4218 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004219
4220 x = target_output->x + dx;
4221 y = target_output->y + dy;
4222
Jason Ekstranda7af7042013-10-12 22:38:11 -05004223 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004224}
4225
4226static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004227map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004228 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004229{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004230 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004231 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01004232 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004233 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08004234 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004235 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004236
Jason Ekstranda7af7042013-10-12 22:38:11 -05004237 shsurf->view->geometry.width = width;
4238 shsurf->view->geometry.height = height;
4239 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004240
4241 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004242 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004243 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004244 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004245 break;
Alex Wu4539b082012-03-01 12:57:46 +08004246 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004247 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004248 shell_map_fullscreen(shsurf);
4249 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004250 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004251 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004252 panel_height = get_output_panel_height(shell, shsurf->output);
4253 surface_subsurfaces_boundingbox(shsurf->surface,
4254 &surf_x, &surf_y, NULL, NULL);
4255 weston_view_set_position(shsurf->view,
4256 shsurf->output->x - surf_x,
4257 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004258 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004259 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004260 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004261 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004262 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004263 weston_view_set_position(shsurf->view,
4264 shsurf->view->geometry.x + sx,
4265 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004266 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004267 default:
4268 ;
4269 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004270
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02004271 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004272 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004273 case SHELL_SURFACE_POPUP:
4274 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004275 /* TODO: Handle a parent with multiple views */
4276 parent = get_default_view(shsurf->parent);
4277 if (parent) {
4278 wl_list_remove(&shsurf->view->layer_link);
4279 wl_list_insert(parent->layer_link.prev,
4280 &shsurf->view->layer_link);
4281 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004282 break;
Alex Wu4539b082012-03-01 12:57:46 +08004283 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02004284 case SHELL_SURFACE_NONE:
4285 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004286 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004287 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004288 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004289 wl_list_remove(&shsurf->view->layer_link);
4290 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004291 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004292 }
4293
Jason Ekstranda7af7042013-10-12 22:38:11 -05004294 if (shsurf->type != SHELL_SURFACE_NONE) {
4295 weston_view_update_transform(shsurf->view);
4296 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4297 shsurf->surface->output = shsurf->output;
4298 shsurf->view->output = shsurf->output;
4299 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004300 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004301
Jason Ekstranda7af7042013-10-12 22:38:11 -05004302 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004303 /* XXX: xwayland's using the same fields for transient type */
4304 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004305 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004306 if (shsurf->transient.flags ==
4307 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4308 break;
4309 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004310 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004311 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004312 if (!shell->locked) {
4313 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004314 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004315 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004316 break;
4317 default:
4318 break;
4319 }
4320
Jason Ekstranda7af7042013-10-12 22:38:11 -05004321 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004322 {
4323 switch (shell->win_animation_type) {
4324 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004325 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004326 break;
4327 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004328 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004329 break;
4330 default:
4331 break;
4332 }
4333 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004334}
4335
4336static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004337configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004338 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004339{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004340 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4341 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004343 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004344
Pekka Paalanen77346a62011-11-30 16:26:35 +02004345 shsurf = get_shell_surface(surface);
4346 if (shsurf)
4347 surface_type = shsurf->type;
4348
Jason Ekstranda7af7042013-10-12 22:38:11 -05004349 /* TODO:
4350 * This should probably be changed to be more shell_surface
4351 * dependent
4352 */
4353 wl_list_for_each(view, &surface->views, surface_link)
4354 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004355
4356 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004357 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004358 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004359 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004360 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004361 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004362 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004363 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4364 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004365 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4366 shsurf->view->geometry.y = shsurf->output->y +
4367 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004368 break;
Alex Wu4539b082012-03-01 12:57:46 +08004369 case SHELL_SURFACE_TOPLEVEL:
4370 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004371 default:
4372 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004373 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004374
Alex Wu4539b082012-03-01 12:57:46 +08004375 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004376 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004377 wl_list_for_each(view, &surface->views, surface_link)
4378 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004379
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004380 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004381 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004382 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004383}
4384
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004385static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004386shell_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 +03004387{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004388 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004389 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004390
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004391 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004392
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004393 if (!weston_surface_is_mapped(es) &&
4394 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004395 remove_popup_grab(shsurf);
4396 }
4397
Giulio Camuffo184df502013-02-21 11:29:21 +01004398 if (width == 0)
4399 return;
4400
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004401 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004402 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004403 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004404 type_changed = 1;
4405 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004406
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004407 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004408 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004409 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004410 shsurf->view->geometry.width != width ||
4411 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004412 float from_x, from_y;
4413 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004414
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4416 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004417 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004418 shsurf->view->geometry.x + to_x - from_x,
4419 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004420 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004421 }
4422}
4423
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004424static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004425
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004426static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004427desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004428{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004429 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004430 struct desktop_shell *shell =
4431 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004432
4433 shell->child.process.pid = 0;
4434 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004435
4436 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4437 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004438 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004439 shell->child.deathstamp = time;
4440 shell->child.deathcount = 0;
4441 }
4442
4443 shell->child.deathcount++;
4444 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004445 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004446 return;
4447 }
4448
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004449 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004450 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004451 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004452}
4453
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004454static void
4455launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004456{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004457 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004458
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004459 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004460 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004461 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004462 desktop_shell_sigchld);
4463
4464 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004465 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004466}
4467
4468static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004469bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4470{
Tiago Vignattibe143262012-04-16 17:31:41 +03004471 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004472 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004473
Jason Ekstranda85118c2013-06-27 20:17:02 -05004474 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4475 if (resource)
4476 wl_resource_set_implementation(resource, &shell_implementation,
4477 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004478}
4479
Kristian Høgsberg75840622011-09-06 13:48:16 -04004480static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004481unbind_desktop_shell(struct wl_resource *resource)
4482{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004483 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004484
4485 if (shell->locked)
4486 resume_desktop(shell);
4487
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004488 shell->child.desktop_shell = NULL;
4489 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004490}
4491
4492static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004493bind_desktop_shell(struct wl_client *client,
4494 void *data, uint32_t version, uint32_t id)
4495{
Tiago Vignattibe143262012-04-16 17:31:41 +03004496 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004497 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004498
Jason Ekstranda85118c2013-06-27 20:17:02 -05004499 resource = wl_resource_create(client, &desktop_shell_interface,
4500 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004501
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004502 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004503 wl_resource_set_implementation(resource,
4504 &desktop_shell_implementation,
4505 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004506 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004507
4508 if (version < 2)
4509 shell_fade_startup(shell);
4510
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004511 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004512 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004513
4514 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4515 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004516 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004517}
4518
Pekka Paalanen6e168112011-11-24 11:34:05 +02004519static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004520screensaver_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 -04004521{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004522 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004523 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004524
Giulio Camuffo184df502013-02-21 11:29:21 +01004525 if (width == 0)
4526 return;
4527
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004528 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004529 if (!shell->locked)
4530 return;
4531
Jason Ekstranda7af7042013-10-12 22:38:11 -05004532 view = container_of(surface->views.next, struct weston_view, surface_link);
4533 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004534
Jason Ekstranda7af7042013-10-12 22:38:11 -05004535 if (wl_list_empty(&view->layer_link)) {
4536 wl_list_insert(shell->lock_layer.view_list.prev,
4537 &view->layer_link);
4538 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004539 wl_event_source_timer_update(shell->screensaver.timer,
4540 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004541 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004542 }
4543}
4544
4545static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004546screensaver_set_surface(struct wl_client *client,
4547 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004548 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004549 struct wl_resource *output_resource)
4550{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004551 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004552 struct weston_surface *surface =
4553 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004554 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004555 struct weston_view *view, *next;
4556
4557 /* Make sure we only have one view */
4558 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4559 weston_view_destroy(view);
4560 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004561
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004562 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004563 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004564 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004565}
4566
4567static const struct screensaver_interface screensaver_implementation = {
4568 screensaver_set_surface
4569};
4570
4571static void
4572unbind_screensaver(struct wl_resource *resource)
4573{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004574 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004575
Pekka Paalanen77346a62011-11-30 16:26:35 +02004576 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004577}
4578
4579static void
4580bind_screensaver(struct wl_client *client,
4581 void *data, uint32_t version, uint32_t id)
4582{
Tiago Vignattibe143262012-04-16 17:31:41 +03004583 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004584 struct wl_resource *resource;
4585
Jason Ekstranda85118c2013-06-27 20:17:02 -05004586 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004587
Pekka Paalanen77346a62011-11-30 16:26:35 +02004588 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004589 wl_resource_set_implementation(resource,
4590 &screensaver_implementation,
4591 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004592 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004593 return;
4594 }
4595
4596 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4597 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004598 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004599}
4600
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004601static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004602input_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 -04004603{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004604 struct input_panel_surface *ip_surface = surface->configure_private;
4605 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004606 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004607 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004608
Giulio Camuffo184df502013-02-21 11:29:21 +01004609 if (width == 0)
4610 return;
4611
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004612 if (!weston_surface_is_mapped(surface)) {
4613 if (!shell->showing_input_panels)
4614 return;
4615
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004616 show_surface = 1;
4617 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004618
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004619 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004620
4621 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004622 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4623 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004624 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004625 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4626 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004627 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004628
Jason Ekstranda7af7042013-10-12 22:38:11 -05004629 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004630
4631 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004632 wl_list_insert(&shell->input_panel_layer.view_list,
4633 &ip_surface->view->layer_link);
4634 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004635 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004636 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004637 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004638}
4639
4640static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004641destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004642{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004643 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4644
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004645 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004646 wl_list_remove(&input_panel_surface->link);
4647
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004648 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004649 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004650
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004651 free(input_panel_surface);
4652}
4653
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004654static struct input_panel_surface *
4655get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004656{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004657 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004658 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004659 } else {
4660 return NULL;
4661 }
4662}
4663
4664static void
4665input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4666{
4667 struct input_panel_surface *ipsurface = container_of(listener,
4668 struct input_panel_surface,
4669 surface_destroy_listener);
4670
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004671 if (ipsurface->resource) {
4672 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004673 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004674 destroy_input_panel_surface(ipsurface);
4675 }
4676}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004677
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004678static struct input_panel_surface *
4679create_input_panel_surface(struct desktop_shell *shell,
4680 struct weston_surface *surface)
4681{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004682 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004683
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004684 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4685 if (!input_panel_surface)
4686 return NULL;
4687
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004688 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004689 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004690
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004691 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004692
4693 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004694 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004695
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004696 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004697 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004698 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004699 &input_panel_surface->surface_destroy_listener);
4700
4701 wl_list_init(&input_panel_surface->link);
4702
4703 return input_panel_surface;
4704}
4705
4706static void
4707input_panel_surface_set_toplevel(struct wl_client *client,
4708 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004709 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004710 uint32_t position)
4711{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004712 struct input_panel_surface *input_panel_surface =
4713 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004714 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004715
4716 wl_list_insert(&shell->input_panel.surfaces,
4717 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004718
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004719 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004720 input_panel_surface->panel = 0;
4721}
4722
4723static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004724input_panel_surface_set_overlay_panel(struct wl_client *client,
4725 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004726{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004727 struct input_panel_surface *input_panel_surface =
4728 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004729 struct desktop_shell *shell = input_panel_surface->shell;
4730
4731 wl_list_insert(&shell->input_panel.surfaces,
4732 &input_panel_surface->link);
4733
4734 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004735}
4736
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004737static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004738 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004739 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004740};
4741
4742static void
4743destroy_input_panel_surface_resource(struct wl_resource *resource)
4744{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004745 struct input_panel_surface *ipsurf =
4746 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004747
4748 destroy_input_panel_surface(ipsurf);
4749}
4750
4751static void
4752input_panel_get_input_panel_surface(struct wl_client *client,
4753 struct wl_resource *resource,
4754 uint32_t id,
4755 struct wl_resource *surface_resource)
4756{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004757 struct weston_surface *surface =
4758 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004759 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004760 struct input_panel_surface *ipsurf;
4761
4762 if (get_input_panel_surface(surface)) {
4763 wl_resource_post_error(surface_resource,
4764 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004765 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004766 return;
4767 }
4768
4769 ipsurf = create_input_panel_surface(shell, surface);
4770 if (!ipsurf) {
4771 wl_resource_post_error(surface_resource,
4772 WL_DISPLAY_ERROR_INVALID_OBJECT,
4773 "surface->configure already set");
4774 return;
4775 }
4776
Jason Ekstranda85118c2013-06-27 20:17:02 -05004777 ipsurf->resource =
4778 wl_resource_create(client,
4779 &wl_input_panel_surface_interface, 1, id);
4780 wl_resource_set_implementation(ipsurf->resource,
4781 &input_panel_surface_implementation,
4782 ipsurf,
4783 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004784}
4785
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004786static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004787 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004788};
4789
4790static void
4791unbind_input_panel(struct wl_resource *resource)
4792{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004793 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004794
4795 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004796}
4797
4798static void
4799bind_input_panel(struct wl_client *client,
4800 void *data, uint32_t version, uint32_t id)
4801{
4802 struct desktop_shell *shell = data;
4803 struct wl_resource *resource;
4804
Jason Ekstranda85118c2013-06-27 20:17:02 -05004805 resource = wl_resource_create(client,
4806 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004807
4808 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004809 wl_resource_set_implementation(resource,
4810 &input_panel_implementation,
4811 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004812 shell->input_panel.binding = resource;
4813 return;
4814 }
4815
4816 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4817 "interface object already bound");
4818 wl_resource_destroy(resource);
4819}
4820
Kristian Høgsberg07045392012-02-19 18:52:44 -05004821struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004822 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004823 struct weston_surface *current;
4824 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004825 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004826};
4827
4828static void
4829switcher_next(struct switcher *switcher)
4830{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004831 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004832 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004833 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004834 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004835
Jason Ekstranda7af7042013-10-12 22:38:11 -05004836 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4837 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004838 case SHELL_SURFACE_TOPLEVEL:
4839 case SHELL_SURFACE_FULLSCREEN:
4840 case SHELL_SURFACE_MAXIMIZED:
4841 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004842 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004843 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004844 next = view->surface;
4845 prev = view->surface;
4846 view->alpha = 0.25;
4847 weston_view_geometry_dirty(view);
4848 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004849 break;
4850 default:
4851 break;
4852 }
Alex Wu1659daa2012-04-01 20:13:09 +08004853
Jason Ekstranda7af7042013-10-12 22:38:11 -05004854 if (is_black_surface(view->surface, NULL)) {
4855 view->alpha = 0.25;
4856 weston_view_geometry_dirty(view);
4857 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004858 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004859 }
4860
4861 if (next == NULL)
4862 next = first;
4863
Alex Wu07b26062012-03-12 16:06:01 +08004864 if (next == NULL)
4865 return;
4866
Kristian Høgsberg07045392012-02-19 18:52:44 -05004867 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004868 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004869
4870 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004871 wl_list_for_each(view, &next->views, surface_link)
4872 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004873
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004874 shsurf = get_shell_surface(switcher->current);
4875 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004876 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004877}
4878
4879static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004880switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004881{
4882 struct switcher *switcher =
4883 container_of(listener, struct switcher, listener);
4884
4885 switcher_next(switcher);
4886}
4887
4888static void
Daniel Stone351eb612012-05-31 15:27:47 -04004889switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004890{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004891 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004892 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004893 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004894
Jason Ekstranda7af7042013-10-12 22:38:11 -05004895 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004896 if (is_focus_view(view))
4897 continue;
4898
Jason Ekstranda7af7042013-10-12 22:38:11 -05004899 view->alpha = 1.0;
4900 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004901 }
4902
Alex Wu07b26062012-03-12 16:06:01 +08004903 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004904 activate(switcher->shell, switcher->current,
4905 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004906 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004907 weston_keyboard_end_grab(keyboard);
4908 if (keyboard->input_method_resource)
4909 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004910 free(switcher);
4911}
4912
4913static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004914switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004915 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004916{
4917 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004918 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004919
Daniel Stonec9785ea2012-05-30 16:31:52 +01004920 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004921 switcher_next(switcher);
4922}
4923
4924static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004925switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004926 uint32_t mods_depressed, uint32_t mods_latched,
4927 uint32_t mods_locked, uint32_t group)
4928{
4929 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004930 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004931
Daniel Stone351eb612012-05-31 15:27:47 -04004932 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4933 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004934}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004935
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004936static void
4937switcher_cancel(struct weston_keyboard_grab *grab)
4938{
4939 struct switcher *switcher = container_of(grab, struct switcher, grab);
4940
4941 switcher_destroy(switcher);
4942}
4943
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004944static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004945 switcher_key,
4946 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004947 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004948};
4949
4950static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004951switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004952 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004953{
Tiago Vignattibe143262012-04-16 17:31:41 +03004954 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004955 struct switcher *switcher;
4956
4957 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004958 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004959 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004960 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004961 wl_list_init(&switcher->listener.link);
4962
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004963 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004964 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004965 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004966 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4967 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004968 switcher_next(switcher);
4969}
4970
Daniel Stonedf8133b2013-11-19 11:37:14 +01004971struct exposay_surface {
4972 struct desktop_shell *shell;
4973 struct weston_surface *surface;
4974 struct weston_view *view;
4975 struct wl_list link;
4976
4977 int x;
4978 int y;
4979 int width;
4980 int height;
4981 double scale;
4982
4983 int row;
4984 int column;
4985
4986 /* The animations only apply a transformation for their own lifetime,
4987 * and don't have an option to indefinitely maintain the
4988 * transformation in a steady state - so, we apply our own once the
4989 * animation has finished. */
4990 struct weston_transform transform;
4991};
4992
4993static void exposay_set_state(struct desktop_shell *shell,
4994 enum exposay_target_state state,
4995 struct weston_seat *seat);
4996static void exposay_check_state(struct desktop_shell *shell);
4997
4998static void
4999exposay_in_flight_inc(struct desktop_shell *shell)
5000{
5001 shell->exposay.in_flight++;
5002}
5003
5004static void
5005exposay_in_flight_dec(struct desktop_shell *shell)
5006{
5007 if (--shell->exposay.in_flight > 0)
5008 return;
5009
5010 exposay_check_state(shell);
5011}
5012
5013static void
5014exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5015{
5016 struct exposay_surface *esurface = data;
5017
5018 wl_list_insert(&esurface->view->geometry.transformation_list,
5019 &esurface->transform.link);
5020 weston_matrix_init(&esurface->transform.matrix);
5021 weston_matrix_scale(&esurface->transform.matrix,
5022 esurface->scale, esurface->scale, 1.0f);
5023 weston_matrix_translate(&esurface->transform.matrix,
5024 esurface->x - esurface->view->geometry.x,
5025 esurface->y - esurface->view->geometry.y,
5026 0);
5027
5028 weston_view_geometry_dirty(esurface->view);
5029 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5030
5031 exposay_in_flight_dec(esurface->shell);
5032}
5033
5034static void
5035exposay_animate_in(struct exposay_surface *esurface)
5036{
5037 exposay_in_flight_inc(esurface->shell);
5038
5039 weston_move_scale_run(esurface->view,
5040 esurface->x - esurface->view->geometry.x,
5041 esurface->y - esurface->view->geometry.y,
5042 1.0, esurface->scale, 0,
5043 exposay_animate_in_done, esurface);
5044}
5045
5046static void
5047exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5048{
5049 struct exposay_surface *esurface = data;
5050 struct desktop_shell *shell = esurface->shell;
5051
5052 wl_list_remove(&esurface->link);
5053 free(esurface);
5054
5055 exposay_in_flight_dec(shell);
5056}
5057
5058static void
5059exposay_animate_out(struct exposay_surface *esurface)
5060{
5061 exposay_in_flight_inc(esurface->shell);
5062
5063 /* Remove the static transformation set up by
5064 * exposay_transform_in_done(). */
5065 wl_list_remove(&esurface->transform.link);
5066 weston_view_geometry_dirty(esurface->view);
5067
5068 weston_move_scale_run(esurface->view,
5069 esurface->x - esurface->view->geometry.x,
5070 esurface->y - esurface->view->geometry.y,
5071 1.0, esurface->scale, 1,
5072 exposay_animate_out_done, esurface);
5073}
5074
5075static void
5076exposay_highlight_surface(struct desktop_shell *shell,
5077 struct exposay_surface *esurface)
5078{
5079 struct weston_view *view = NULL;
5080
5081 if (esurface) {
5082 shell->exposay.row_current = esurface->row;
5083 shell->exposay.column_current = esurface->column;
5084 view = esurface->view;
5085 }
5086
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005087 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005088 shell->exposay.focus_current = view;
5089}
5090
5091static int
5092exposay_is_animating(struct desktop_shell *shell)
5093{
5094 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5095 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5096 return 0;
5097
5098 return (shell->exposay.in_flight > 0);
5099}
5100
5101static void
5102exposay_pick(struct desktop_shell *shell, int x, int y)
5103{
5104 struct exposay_surface *esurface;
5105
5106 if (exposay_is_animating(shell))
5107 return;
5108
5109 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5110 if (x < esurface->x || x > esurface->x + esurface->width)
5111 continue;
5112 if (y < esurface->y || y > esurface->y + esurface->height)
5113 continue;
5114
5115 exposay_highlight_surface(shell, esurface);
5116 return;
5117 }
5118}
5119
5120/* Pretty lame layout for now; just tries to make a square. Should take
5121 * aspect ratio into account really. Also needs to be notified of surface
5122 * addition and removal and adjust layout/animate accordingly. */
5123static enum exposay_layout_state
5124exposay_layout(struct desktop_shell *shell)
5125{
5126 struct workspace *workspace = shell->exposay.workspace;
5127 struct weston_compositor *compositor = shell->compositor;
5128 struct weston_output *output = get_default_output(compositor);
5129 struct weston_view *view;
5130 struct exposay_surface *esurface;
5131 int w, h;
5132 int i;
5133 int last_row_removed = 0;
5134
5135 wl_list_init(&shell->exposay.surface_list);
5136
5137 shell->exposay.num_surfaces = 0;
5138 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5139 if (!get_shell_surface(view->surface))
5140 continue;
5141 shell->exposay.num_surfaces++;
5142 }
5143
5144 if (shell->exposay.num_surfaces == 0) {
5145 shell->exposay.grid_size = 0;
5146 shell->exposay.hpadding_outer = 0;
5147 shell->exposay.vpadding_outer = 0;
5148 shell->exposay.padding_inner = 0;
5149 shell->exposay.surface_size = 0;
5150 return EXPOSAY_LAYOUT_OVERVIEW;
5151 }
5152
5153 /* Lay the grid out as square as possible, losing surfaces from the
5154 * bottom row if required. Start with fixed padding of a 10% margin
5155 * around the outside and 80px internal padding between surfaces, and
5156 * maximise the area made available to surfaces after this, but only
5157 * to a maximum of 1/3rd the total output size.
5158 *
5159 * If we can't make a square grid, add one extra row at the bottom
5160 * which will have a smaller number of columns.
5161 *
5162 * XXX: Surely there has to be a better way to express this maths,
5163 * right?!
5164 */
5165 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5166 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5167 shell->exposay.grid_size++;
5168 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5169
5170 shell->exposay.hpadding_outer = (output->width / 10);
5171 shell->exposay.vpadding_outer = (output->height / 10);
5172 shell->exposay.padding_inner = 80;
5173
5174 w = output->width - (shell->exposay.hpadding_outer * 2);
5175 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5176 w /= shell->exposay.grid_size;
5177
5178 h = output->height - (shell->exposay.vpadding_outer * 2);
5179 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5180 h /= shell->exposay.grid_size;
5181
5182 shell->exposay.surface_size = (w < h) ? w : h;
5183 if (shell->exposay.surface_size > (output->width / 2))
5184 shell->exposay.surface_size = output->width / 2;
5185 if (shell->exposay.surface_size > (output->height / 2))
5186 shell->exposay.surface_size = output->height / 2;
5187
5188 i = 0;
5189 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5190 int pad;
5191
5192 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5193
5194 if (!get_shell_surface(view->surface))
5195 continue;
5196
5197 esurface = malloc(sizeof(*esurface));
5198 if (!esurface) {
5199 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5200 shell->exposay.seat);
5201 break;
5202 }
5203
5204 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5205 esurface->shell = shell;
5206 esurface->view = view;
5207
5208 esurface->row = i / shell->exposay.grid_size;
5209 esurface->column = i % shell->exposay.grid_size;
5210
5211 esurface->x = shell->exposay.hpadding_outer;
5212 esurface->x += pad * esurface->column;
5213 esurface->y = shell->exposay.vpadding_outer;
5214 esurface->y += pad * esurface->row;
5215
5216 if (esurface->row == shell->exposay.grid_size - 1)
5217 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5218
5219 if (view->geometry.width > view->geometry.height)
5220 esurface->scale = shell->exposay.surface_size / (float) view->geometry.width;
5221 else
5222 esurface->scale = shell->exposay.surface_size / (float) view->geometry.height;
5223 esurface->width = view->geometry.width * esurface->scale;
5224 esurface->height = view->geometry.height * esurface->scale;
5225
5226 if (shell->exposay.focus_current == esurface->view)
5227 exposay_highlight_surface(shell, esurface);
5228
5229 exposay_animate_in(esurface);
5230
5231 i++;
5232 }
5233
5234 weston_compositor_schedule_repaint(shell->compositor);
5235
5236 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5237}
5238
5239static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005240exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5241 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005242{
5243 struct desktop_shell *shell =
5244 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5245
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005246 weston_pointer_move(grab->pointer, x, y);
5247
Daniel Stonedf8133b2013-11-19 11:37:14 +01005248 exposay_pick(shell,
5249 wl_fixed_to_int(grab->pointer->x),
5250 wl_fixed_to_int(grab->pointer->y));
5251}
5252
5253static void
5254exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5255 uint32_t state_w)
5256{
5257 struct desktop_shell *shell =
5258 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5259 struct weston_seat *seat = grab->pointer->seat;
5260 enum wl_pointer_button_state state = state_w;
5261
5262 if (button != BTN_LEFT)
5263 return;
5264
5265 /* Store the surface we clicked on, and don't do anything if we end up
5266 * releasing on a different surface. */
5267 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5268 shell->exposay.clicked = shell->exposay.focus_current;
5269 return;
5270 }
5271
5272 if (shell->exposay.focus_current == shell->exposay.clicked)
5273 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5274 else
5275 shell->exposay.clicked = NULL;
5276}
5277
5278static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5279 noop_grab_focus,
5280 exposay_motion,
5281 exposay_button,
5282};
5283
5284static int
5285exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5286{
5287 struct exposay_surface *esurface;
5288
5289 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5290 if (esurface->row != row || esurface->column != column)
5291 continue;
5292
5293 exposay_highlight_surface(shell, esurface);
5294 return 1;
5295 }
5296
5297 return 0;
5298}
5299
5300static void
5301exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5302 uint32_t state_w)
5303{
5304 struct weston_seat *seat = grab->keyboard->seat;
5305 struct desktop_shell *shell =
5306 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5307 enum wl_keyboard_key_state state = state_w;
5308
5309 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5310 return;
5311
5312 switch (key) {
5313 case KEY_ESC:
5314 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5315 break;
5316 case KEY_ENTER:
5317 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5318 break;
5319 case KEY_UP:
5320 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5321 shell->exposay.column_current);
5322 break;
5323 case KEY_DOWN:
5324 /* Special case for trying to move to the bottom row when it
5325 * has fewer items than all the others. */
5326 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5327 shell->exposay.column_current) &&
5328 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5329 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5330 (shell->exposay.num_surfaces %
5331 shell->exposay.grid_size) - 1);
5332 }
5333 break;
5334 case KEY_LEFT:
5335 exposay_maybe_move(shell, shell->exposay.row_current,
5336 shell->exposay.column_current - 1);
5337 break;
5338 case KEY_RIGHT:
5339 exposay_maybe_move(shell, shell->exposay.row_current,
5340 shell->exposay.column_current + 1);
5341 break;
5342 case KEY_TAB:
5343 /* Try to move right, then down (and to the leftmost column),
5344 * then if all else fails, to the top left. */
5345 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5346 shell->exposay.column_current + 1) &&
5347 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5348 exposay_maybe_move(shell, 0, 0);
5349 break;
5350 default:
5351 break;
5352 }
5353}
5354
5355static void
5356exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5357 uint32_t mods_depressed, uint32_t mods_latched,
5358 uint32_t mods_locked, uint32_t group)
5359{
5360}
5361
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005362static void
5363exposay_cancel(struct weston_keyboard_grab *grab)
5364{
5365 struct desktop_shell *shell =
5366 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5367
5368 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5369}
5370
Daniel Stonedf8133b2013-11-19 11:37:14 +01005371static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5372 exposay_key,
5373 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005374 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005375};
5376
5377/**
5378 * Called when the transition from overview -> inactive has completed.
5379 */
5380static enum exposay_layout_state
5381exposay_set_inactive(struct desktop_shell *shell)
5382{
5383 struct weston_seat *seat = shell->exposay.seat;
5384
5385 weston_keyboard_end_grab(seat->keyboard);
5386 weston_pointer_end_grab(seat->pointer);
5387 if (seat->keyboard->input_method_resource)
5388 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5389
5390 return EXPOSAY_LAYOUT_INACTIVE;
5391}
5392
5393/**
5394 * Begins the transition from overview to inactive. */
5395static enum exposay_layout_state
5396exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5397{
5398 struct exposay_surface *esurface;
5399
5400 /* Call activate() before we start the animations to avoid
5401 * animating back the old state and then immediately transitioning
5402 * to the new. */
5403 if (switch_focus && shell->exposay.focus_current)
5404 activate(shell, shell->exposay.focus_current->surface,
5405 shell->exposay.seat);
5406 else if (shell->exposay.focus_prev)
5407 activate(shell, shell->exposay.focus_prev->surface,
5408 shell->exposay.seat);
5409
5410 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5411 exposay_animate_out(esurface);
5412 weston_compositor_schedule_repaint(shell->compositor);
5413
5414 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5415}
5416
5417static enum exposay_layout_state
5418exposay_transition_active(struct desktop_shell *shell)
5419{
5420 struct weston_seat *seat = shell->exposay.seat;
5421
5422 shell->exposay.workspace = get_current_workspace(shell);
5423 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5424 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5425 shell->exposay.clicked = NULL;
5426 wl_list_init(&shell->exposay.surface_list);
5427
5428 lower_fullscreen_layer(shell);
5429 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5430 weston_keyboard_start_grab(seat->keyboard,
5431 &shell->exposay.grab_kbd);
5432 weston_keyboard_set_focus(seat->keyboard, NULL);
5433
5434 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5435 weston_pointer_start_grab(seat->pointer,
5436 &shell->exposay.grab_ptr);
5437 weston_pointer_set_focus(seat->pointer, NULL,
5438 seat->pointer->x, seat->pointer->y);
5439
5440 return exposay_layout(shell);
5441}
5442
5443static void
5444exposay_check_state(struct desktop_shell *shell)
5445{
5446 enum exposay_layout_state state_new = shell->exposay.state_cur;
5447 int do_switch = 0;
5448
5449 /* Don't do anything whilst animations are running, just store up
5450 * target state changes and only act on them when the animations have
5451 * completed. */
5452 if (exposay_is_animating(shell))
5453 return;
5454
5455 switch (shell->exposay.state_target) {
5456 case EXPOSAY_TARGET_OVERVIEW:
5457 switch (shell->exposay.state_cur) {
5458 case EXPOSAY_LAYOUT_OVERVIEW:
5459 goto out;
5460 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5461 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5462 break;
5463 default:
5464 state_new = exposay_transition_active(shell);
5465 break;
5466 }
5467 break;
5468
5469 case EXPOSAY_TARGET_SWITCH:
5470 do_switch = 1; /* fallthrough */
5471 case EXPOSAY_TARGET_CANCEL:
5472 switch (shell->exposay.state_cur) {
5473 case EXPOSAY_LAYOUT_INACTIVE:
5474 goto out;
5475 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5476 state_new = exposay_set_inactive(shell);
5477 break;
5478 default:
5479 state_new = exposay_transition_inactive(shell, do_switch);
5480 break;
5481 }
5482
5483 break;
5484 }
5485
5486out:
5487 shell->exposay.state_cur = state_new;
5488}
5489
5490static void
5491exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5492 struct weston_seat *seat)
5493{
5494 shell->exposay.state_target = state;
5495 shell->exposay.seat = seat;
5496 exposay_check_state(shell);
5497}
5498
5499static void
5500exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5501 void *data)
5502{
5503 struct desktop_shell *shell = data;
5504
5505 if (shell->exposay.state_target == EXPOSAY_TARGET_OVERVIEW)
5506 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5507 else
5508 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
5509}
5510
Pekka Paalanen3c647232011-12-22 13:43:43 +02005511static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005512backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005513 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005514{
5515 struct weston_compositor *compositor = data;
5516 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005517 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005518
5519 /* TODO: we're limiting to simple use cases, where we assume just
5520 * control on the primary display. We'd have to extend later if we
5521 * ever get support for setting backlights on random desktop LCD
5522 * panels though */
5523 output = get_default_output(compositor);
5524 if (!output)
5525 return;
5526
5527 if (!output->set_backlight)
5528 return;
5529
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005530 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5531 backlight_new = output->backlight_current - 25;
5532 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5533 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005534
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005535 if (backlight_new < 5)
5536 backlight_new = 5;
5537 if (backlight_new > 255)
5538 backlight_new = 255;
5539
5540 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005541 output->set_backlight(output, output->backlight_current);
5542}
5543
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005544struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005545 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005546 struct weston_seat *seat;
5547 uint32_t key[2];
5548 int key_released[2];
5549};
5550
5551static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005552debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005553 uint32_t key, uint32_t state)
5554{
5555 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005556 struct weston_compositor *ec = db->seat->compositor;
5557 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005558 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005559 uint32_t serial;
5560 int send = 0, terminate = 0;
5561 int check_binding = 1;
5562 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005563 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005564
5565 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5566 /* Do not run bindings on key releases */
5567 check_binding = 0;
5568
5569 for (i = 0; i < 2; i++)
5570 if (key == db->key[i])
5571 db->key_released[i] = 1;
5572
5573 if (db->key_released[0] && db->key_released[1]) {
5574 /* All key releases been swalled so end the grab */
5575 terminate = 1;
5576 } else if (key != db->key[0] && key != db->key[1]) {
5577 /* Should not swallow release of other keys */
5578 send = 1;
5579 }
5580 } else if (key == db->key[0] && !db->key_released[0]) {
5581 /* Do not check bindings for the first press of the binding
5582 * key. This allows it to be used as a debug shortcut.
5583 * We still need to swallow this event. */
5584 check_binding = 0;
5585 } else if (db->key[1]) {
5586 /* If we already ran a binding don't process another one since
5587 * we can't keep track of all the binding keys that were
5588 * pressed in order to swallow the release events. */
5589 send = 1;
5590 check_binding = 0;
5591 }
5592
5593 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005594 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5595 key, state)) {
5596 /* We ran a binding so swallow the press and keep the
5597 * grab to swallow the released too. */
5598 send = 0;
5599 terminate = 0;
5600 db->key[1] = key;
5601 } else {
5602 /* Terminate the grab since the key pressed is not a
5603 * debug binding key. */
5604 send = 1;
5605 terminate = 1;
5606 }
5607 }
5608
5609 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005610 serial = wl_display_next_serial(display);
5611 resource_list = &grab->keyboard->focus_resource_list;
5612 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005613 wl_keyboard_send_key(resource, serial, time, key, state);
5614 }
5615 }
5616
5617 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005618 weston_keyboard_end_grab(grab->keyboard);
5619 if (grab->keyboard->input_method_resource)
5620 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005621 free(db);
5622 }
5623}
5624
5625static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005626debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005627 uint32_t mods_depressed, uint32_t mods_latched,
5628 uint32_t mods_locked, uint32_t group)
5629{
5630 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005631 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005632
Neil Roberts96d790e2013-09-19 17:32:00 +01005633 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005634
Neil Roberts96d790e2013-09-19 17:32:00 +01005635 wl_resource_for_each(resource, resource_list) {
5636 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5637 mods_latched, mods_locked, group);
5638 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005639}
5640
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005641static void
5642debug_binding_cancel(struct weston_keyboard_grab *grab)
5643{
5644 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5645
5646 weston_keyboard_end_grab(grab->keyboard);
5647 free(db);
5648}
5649
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005650struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005651 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005652 debug_binding_modifiers,
5653 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005654};
5655
5656static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005657debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005658{
5659 struct debug_binding_grab *grab;
5660
5661 grab = calloc(1, sizeof *grab);
5662 if (!grab)
5663 return;
5664
5665 grab->seat = (struct weston_seat *) seat;
5666 grab->key[0] = key;
5667 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005668 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005669}
5670
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005671static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005672force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005673 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005674{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005675 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005676 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005677 struct desktop_shell *shell = data;
5678 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005679 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005680
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005681 focus_surface = seat->keyboard->focus;
5682 if (!focus_surface)
5683 return;
5684
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005685 wl_signal_emit(&compositor->kill_signal, focus_surface);
5686
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005687 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005688 wl_client_get_credentials(client, &pid, NULL, NULL);
5689
5690 /* Skip clients that we launched ourselves (the credentials of
5691 * the socketpair is ours) */
5692 if (pid == getpid())
5693 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005694
Daniel Stone325fc2d2012-05-30 16:31:58 +01005695 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005696}
5697
5698static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005699workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005700 uint32_t key, void *data)
5701{
5702 struct desktop_shell *shell = data;
5703 unsigned int new_index = shell->workspaces.current;
5704
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005705 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005706 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005707 if (new_index != 0)
5708 new_index--;
5709
5710 change_workspace(shell, new_index);
5711}
5712
5713static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005714workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005715 uint32_t key, void *data)
5716{
5717 struct desktop_shell *shell = data;
5718 unsigned int new_index = shell->workspaces.current;
5719
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005720 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005721 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005722 if (new_index < shell->workspaces.num - 1)
5723 new_index++;
5724
5725 change_workspace(shell, new_index);
5726}
5727
5728static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005729workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005730 uint32_t key, void *data)
5731{
5732 struct desktop_shell *shell = data;
5733 unsigned int new_index;
5734
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005735 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005736 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005737 new_index = key - KEY_F1;
5738 if (new_index >= shell->workspaces.num)
5739 new_index = shell->workspaces.num - 1;
5740
5741 change_workspace(shell, new_index);
5742}
5743
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005744static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005745workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005746 uint32_t key, void *data)
5747{
5748 struct desktop_shell *shell = data;
5749 unsigned int new_index = shell->workspaces.current;
5750
5751 if (shell->locked)
5752 return;
5753
5754 if (new_index != 0)
5755 new_index--;
5756
5757 take_surface_to_workspace_by_seat(shell, seat, new_index);
5758}
5759
5760static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005761workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005762 uint32_t key, void *data)
5763{
5764 struct desktop_shell *shell = data;
5765 unsigned int new_index = shell->workspaces.current;
5766
5767 if (shell->locked)
5768 return;
5769
5770 if (new_index < shell->workspaces.num - 1)
5771 new_index++;
5772
5773 take_surface_to_workspace_by_seat(shell, seat, new_index);
5774}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005775
5776static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005777handle_output_destroy(struct wl_listener *listener, void *data)
5778{
5779 struct shell_output *output_listener =
5780 container_of(listener, struct shell_output, destroy_listener);
5781
5782 wl_list_remove(&output_listener->destroy_listener.link);
5783 wl_list_remove(&output_listener->link);
5784 free(output_listener);
5785}
5786
5787static void
5788create_shell_output(struct desktop_shell *shell,
5789 struct weston_output *output)
5790{
5791 struct shell_output *shell_output;
5792
5793 shell_output = zalloc(sizeof *shell_output);
5794 if (shell_output == NULL)
5795 return;
5796
5797 shell_output->output = output;
5798 shell_output->shell = shell;
5799 shell_output->destroy_listener.notify = handle_output_destroy;
5800 wl_signal_add(&output->destroy_signal,
5801 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005802 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005803}
5804
5805static void
5806handle_output_create(struct wl_listener *listener, void *data)
5807{
5808 struct desktop_shell *shell =
5809 container_of(listener, struct desktop_shell, output_create_listener);
5810 struct weston_output *output = (struct weston_output *)data;
5811
5812 create_shell_output(shell, output);
5813}
5814
5815static void
5816setup_output_destroy_handler(struct weston_compositor *ec,
5817 struct desktop_shell *shell)
5818{
5819 struct weston_output *output;
5820
5821 wl_list_init(&shell->output_list);
5822 wl_list_for_each(output, &ec->output_list, link)
5823 create_shell_output(shell, output);
5824
5825 shell->output_create_listener.notify = handle_output_create;
5826 wl_signal_add(&ec->output_created_signal,
5827 &shell->output_create_listener);
5828}
5829
5830static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005831shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005832{
Tiago Vignattibe143262012-04-16 17:31:41 +03005833 struct desktop_shell *shell =
5834 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005835 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005836 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005837
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005838 if (shell->child.client)
5839 wl_client_destroy(shell->child.client);
5840
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005841 wl_list_remove(&shell->idle_listener.link);
5842 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005843 wl_list_remove(&shell->show_input_panel_listener.link);
5844 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005845
Xiong Zhang6b481422013-10-23 13:58:32 +08005846 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5847 wl_list_remove(&shell_output->destroy_listener.link);
5848 wl_list_remove(&shell_output->link);
5849 free(shell_output);
5850 }
5851
5852 wl_list_remove(&shell->output_create_listener.link);
5853
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005854 wl_array_for_each(ws, &shell->workspaces.array)
5855 workspace_destroy(*ws);
5856 wl_array_release(&shell->workspaces.array);
5857
Pekka Paalanen3c647232011-12-22 13:43:43 +02005858 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005859 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005860 free(shell);
5861}
5862
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005863static void
5864shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5865{
5866 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005867 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005868
5869 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005870 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5871 MODIFIER_CTRL | MODIFIER_ALT,
5872 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005873 weston_compositor_add_key_binding(ec, KEY_TAB,
5874 MODIFIER_ALT,
5875 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005876 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5877 click_to_activate_binding,
5878 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005879 weston_compositor_add_touch_binding(ec, 0,
5880 touch_to_activate_binding,
5881 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005882 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5883 MODIFIER_SUPER | MODIFIER_ALT,
5884 surface_opacity_binding, NULL);
5885 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5886 MODIFIER_SUPER, zoom_axis_binding,
5887 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005888
5889 /* configurable bindings */
5890 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005891 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5892 zoom_key_binding, NULL);
5893 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5894 zoom_key_binding, NULL);
5895 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5896 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005897 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005898 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5899 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005900
5901 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5902 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5903 rotate_binding, NULL);
5904
Daniel Stone325fc2d2012-05-30 16:31:58 +01005905 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5906 shell);
5907 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5908 ec);
5909 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5910 backlight_binding, ec);
5911 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5912 ec);
5913 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5914 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005915 weston_compositor_add_key_binding(ec, KEY_K, mod,
5916 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005917 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5918 workspace_up_binding, shell);
5919 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5920 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005921 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5922 workspace_move_surface_up_binding,
5923 shell);
5924 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5925 workspace_move_surface_down_binding,
5926 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005927
Daniel Stonedf8133b2013-11-19 11:37:14 +01005928 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5929
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005930 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5931 if (shell->workspaces.num > 1) {
5932 num_workspace_bindings = shell->workspaces.num;
5933 if (num_workspace_bindings > 6)
5934 num_workspace_bindings = 6;
5935 for (i = 0; i < num_workspace_bindings; i++)
5936 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5937 workspace_f_binding,
5938 shell);
5939 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005940
5941 /* Debug bindings */
5942 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5943 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005944}
5945
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005946WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005947module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005948 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005949{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005950 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005951 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005952 struct workspace **pws;
5953 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005954 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005955
Peter Huttererf3d62272013-08-08 11:57:05 +10005956 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005957 if (shell == NULL)
5958 return -1;
5959
Kristian Høgsberg75840622011-09-06 13:48:16 -04005960 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005961
5962 shell->destroy_listener.notify = shell_destroy;
5963 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005964 shell->idle_listener.notify = idle_handler;
5965 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5966 shell->wake_listener.notify = wake_handler;
5967 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005968 shell->show_input_panel_listener.notify = show_input_panels;
5969 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
5970 shell->hide_input_panel_listener.notify = hide_input_panels;
5971 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005972 shell->update_input_panel_listener.notify = update_input_panels;
5973 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06005974 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005975 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005976 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005977 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005978 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005979 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005980 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005981 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005982 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005983 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005984 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005985
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005986 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005987
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005988 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5989 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005990 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5991 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005992 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005993
5994 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005995 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005996
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005997 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005998
Daniel Stonedf8133b2013-11-19 11:37:14 +01005999 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6000 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6001
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006002 for (i = 0; i < shell->workspaces.num; i++) {
6003 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6004 if (pws == NULL)
6005 return -1;
6006
6007 *pws = workspace_create();
6008 if (*pws == NULL)
6009 return -1;
6010 }
6011 activate_workspace(shell, 0);
6012
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006013 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006014 wl_list_init(&shell->workspaces.animation.link);
6015 shell->workspaces.animation.frame = animate_workspace_change_frame;
6016
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006017 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006018 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006019 return -1;
6020
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006021 if (wl_global_create(ec->wl_display,
6022 &desktop_shell_interface, 2,
6023 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006024 return -1;
6025
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006026 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6027 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006028 return -1;
6029
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006030 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006031 shell, bind_input_panel) == NULL)
6032 return -1;
6033
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006034 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6035 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006036 return -1;
6037
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006038 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006039
Xiong Zhang6b481422013-10-23 13:58:32 +08006040 setup_output_destroy_handler(ec, shell);
6041
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006042 loop = wl_display_get_event_loop(ec->wl_display);
6043 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006044
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006045 shell->screensaver.timer =
6046 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6047
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006048 wl_list_for_each(seat, &ec->seat_list, link)
6049 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006050
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006051 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006052
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006053 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006054
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006055 return 0;
6056}