blob: f1d0686c676aa056c60c0fcdd169762fc56724d7 [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;
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +0100223
224 bool mod_pressed;
225 bool mod_invalid;
Daniel Stonedf8133b2013-11-19 11:37:14 +0100226 } exposay;
227
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300228 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800229 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700230 enum animation_type startup_animation_type;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100231 enum animation_type focus_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800232
233 struct wl_listener output_create_listener;
234 struct wl_list output_list;
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100235
236 char *client;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400237};
238
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500239enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200240 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500241 SHELL_SURFACE_TOPLEVEL,
242 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500243 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800244 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300245 SHELL_SURFACE_POPUP,
246 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200247};
248
Scott Moreauff1db4a2012-04-17 19:06:18 -0600249struct ping_timer {
250 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600251 uint32_t serial;
252};
253
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200254struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500255 struct wl_resource *resource;
256 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200257
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500258 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500259 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200260 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400261 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300262 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200263
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400264 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400265 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500266 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800267 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800268 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700269 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100270
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500271 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200272 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500273 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200274 } rotation;
275
276 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100277 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500278 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100279 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400280 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500281 } popup;
282
Alex Wu4539b082012-03-01 12:57:46 +0800283 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300284 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400285 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300286 } transient;
287
288 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800289 enum wl_shell_surface_fullscreen_method type;
290 struct weston_transform transform; /* matrix from x, y */
291 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800293 } fullscreen;
294
Scott Moreauff1db4a2012-04-17 19:06:18 -0600295 struct ping_timer *ping_timer;
296
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200297 struct weston_transform workspace_transform;
298
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500299 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500300 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100301 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400302
303 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200304};
305
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400307 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300308 struct shell_surface *shsurf;
309 struct wl_listener shsurf_destroy_listener;
310};
311
Rusty Lynch1084da52013-08-15 09:10:08 -0700312struct shell_touch_grab {
313 struct weston_touch_grab grab;
314 struct shell_surface *shsurf;
315 struct wl_listener shsurf_destroy_listener;
316 struct weston_touch *touch;
317};
318
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300319struct weston_move_grab {
320 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100321 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500322};
323
Rusty Lynch1084da52013-08-15 09:10:08 -0700324struct weston_touch_move_grab {
325 struct shell_touch_grab base;
326 wl_fixed_t dx, dy;
327};
328
Pekka Paalanen460099f2012-01-20 16:48:25 +0200329struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300330 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500331 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200332 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200333 float x;
334 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200335 } center;
336};
337
Giulio Camuffo5085a752013-03-25 21:42:45 +0100338struct shell_seat {
339 struct weston_seat *seat;
340 struct wl_listener seat_destroy_listener;
341
342 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400343 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100344 struct wl_list surfaces_list;
345 struct wl_client *client;
346 int32_t initial_up;
347 } popup_grab;
348};
349
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400350static void
351activate(struct desktop_shell *shell, struct weston_surface *es,
352 struct weston_seat *seat);
353
354static struct workspace *
355get_current_workspace(struct desktop_shell *shell);
356
Alex Wubd3354b2012-04-17 17:20:49 +0800357static struct shell_surface *
358get_shell_surface(struct weston_surface *surface);
359
360static struct desktop_shell *
361shell_surface_get_shell(struct shell_surface *shsurf);
362
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500363static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400364surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500365
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300366static void
367shell_fade_startup(struct desktop_shell *shell);
368
Philip Withnallbecb77e2013-11-25 18:01:30 +0000369static struct shell_seat *
370get_shell_seat(struct weston_seat *seat);
371
Alex Wubd3354b2012-04-17 17:20:49 +0800372static bool
373shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
374{
375 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500376 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800377
378 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100379
Jason Ekstranda7af7042013-10-12 22:38:11 -0500380 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800381 return false;
382
Jason Ekstranda7af7042013-10-12 22:38:11 -0500383 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
384 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800385 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500386 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800387}
388
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500389static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400390destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300391{
392 struct shell_grab *grab;
393
394 grab = container_of(listener, struct shell_grab,
395 shsurf_destroy_listener);
396
397 grab->shsurf = NULL;
398}
399
Jason Ekstranda7af7042013-10-12 22:38:11 -0500400static struct weston_view *
401get_default_view(struct weston_surface *surface)
402{
403 struct shell_surface *shsurf;
404 struct weston_view *view;
405
406 if (!surface || wl_list_empty(&surface->views))
407 return NULL;
408
409 shsurf = get_shell_surface(surface);
410 if (shsurf)
411 return shsurf->view;
412
413 wl_list_for_each(view, &surface->views, surface_link)
414 if (weston_view_is_mapped(view))
415 return view;
416
417 return container_of(surface->views.next, struct weston_view, surface_link);
418}
419
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300420static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400421popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400422
423static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300424shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400425 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300426 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400427 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300428 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300429{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300430 struct desktop_shell *shell = shsurf->shell;
431
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400432 popup_grab_end(pointer);
433
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300434 grab->grab.interface = interface;
435 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400436 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500437 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400438 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300439
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700440 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400441 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400442 if (shell->child.desktop_shell) {
443 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
444 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 weston_pointer_set_focus(pointer,
446 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400447 wl_fixed_from_int(0),
448 wl_fixed_from_int(0));
449 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300450}
451
452static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300453shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300454{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700455 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400456 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700457 grab->shsurf->grabbed = 0;
458 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300459
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700460 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300461}
462
463static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700464shell_touch_grab_start(struct shell_touch_grab *grab,
465 const struct weston_touch_grab_interface *interface,
466 struct shell_surface *shsurf,
467 struct weston_touch *touch)
468{
469 struct desktop_shell *shell = shsurf->shell;
470
471 grab->grab.interface = interface;
472 grab->shsurf = shsurf;
473 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
474 wl_signal_add(&shsurf->destroy_signal,
475 &grab->shsurf_destroy_listener);
476
477 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700478 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700479
480 weston_touch_start_grab(touch, &grab->grab);
481 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500482 weston_touch_set_focus(touch->seat,
483 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700484}
485
486static void
487shell_touch_grab_end(struct shell_touch_grab *grab)
488{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700489 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700490 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700491 grab->shsurf->grabbed = 0;
492 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700493
494 weston_touch_end_grab(grab->touch);
495}
496
497static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500498center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800499 struct weston_output *output);
500
Daniel Stone496ca172012-05-30 16:31:42 +0100501static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300502get_modifier(char *modifier)
503{
504 if (!modifier)
505 return MODIFIER_SUPER;
506
507 if (!strcmp("ctrl", modifier))
508 return MODIFIER_CTRL;
509 else if (!strcmp("alt", modifier))
510 return MODIFIER_ALT;
511 else if (!strcmp("super", modifier))
512 return MODIFIER_SUPER;
513 else
514 return MODIFIER_SUPER;
515}
516
Juan Zhaoe10d2792012-04-25 19:09:52 +0800517static enum animation_type
518get_animation_type(char *animation)
519{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800520 if (!strcmp("zoom", animation))
521 return ANIMATION_ZOOM;
522 else if (!strcmp("fade", animation))
523 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100524 else if (!strcmp("dim-layer", animation))
525 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800526 else
527 return ANIMATION_NONE;
528}
529
Alex Wu4539b082012-03-01 12:57:46 +0800530static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400531shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200532{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400533 struct weston_config_section *section;
534 int duration;
535 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200536
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400537 section = weston_config_get_section(shell->compositor->config,
538 "screensaver", NULL, NULL);
539 weston_config_section_get_string(section,
540 "path", &shell->screensaver.path, NULL);
541 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200542 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400543
544 section = weston_config_get_section(shell->compositor->config,
545 "shell", NULL, NULL);
546 weston_config_section_get_string(section,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100547 "client", &s, LIBEXECDIR "/weston-desktop-shell");
548 shell->client = s;
549 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400550 "binding-modifier", &s, "super");
551 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200552 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400553 weston_config_section_get_string(section, "animation", &s, "none");
554 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200555 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700556 weston_config_section_get_string(section,
557 "startup-animation", &s, "fade");
558 shell->startup_animation_type = get_animation_type(s);
559 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700560 if (shell->startup_animation_type == ANIMATION_ZOOM)
561 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100562 weston_config_section_get_string(section, "focus-animation", &s, "none");
563 shell->focus_animation_type = get_animation_type(s);
564 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400565 weston_config_section_get_uint(section, "num-workspaces",
566 &shell->workspaces.num,
567 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200568}
569
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100570static struct weston_output *
571get_default_output(struct weston_compositor *compositor)
572{
573 return container_of(compositor->output_list.next,
574 struct weston_output, link);
575}
576
577
578/* no-op func for checking focus surface */
579static void
580focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy,
581 int32_t width, int32_t height)
582{
583}
584
585static struct focus_surface *
586get_focus_surface(struct weston_surface *surface)
587{
588 if (surface->configure == focus_surface_configure)
589 return surface->configure_private;
590 else
591 return NULL;
592}
593
594static bool
595is_focus_surface (struct weston_surface *es)
596{
597 return (es->configure == focus_surface_configure);
598}
599
600static bool
601is_focus_view (struct weston_view *view)
602{
603 return is_focus_surface (view->surface);
604}
605
606static struct focus_surface *
607create_focus_surface(struct weston_compositor *ec,
608 struct weston_output *output)
609{
610 struct focus_surface *fsurf = NULL;
611 struct weston_surface *surface = NULL;
612
613 fsurf = malloc(sizeof *fsurf);
614 if (!fsurf)
615 return NULL;
616
617 fsurf->surface = weston_surface_create(ec);
618 surface = fsurf->surface;
619 if (surface == NULL) {
620 free(fsurf);
621 return NULL;
622 }
623
624 surface->configure = focus_surface_configure;
625 surface->output = output;
626 surface->configure_private = fsurf;
627
628 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100629 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100630
631 weston_view_configure(fsurf->view, output->x, output->y,
632 output->width, output->height);
633 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
634 pixman_region32_fini(&surface->opaque);
635 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
636 output->width, output->height);
637 pixman_region32_fini(&surface->input);
638 pixman_region32_init(&surface->input);
639
640 wl_list_init(&fsurf->workspace_transform.link);
641
642 return fsurf;
643}
644
645static void
646focus_surface_destroy(struct focus_surface *fsurf)
647{
648 weston_surface_destroy(fsurf->surface);
649 free(fsurf);
650}
651
652static void
653focus_animation_done(struct weston_view_animation *animation, void *data)
654{
655 struct workspace *ws = data;
656
657 ws->focus_animation = NULL;
658}
659
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200660static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200661focus_state_destroy(struct focus_state *state)
662{
663 wl_list_remove(&state->seat_destroy_listener.link);
664 wl_list_remove(&state->surface_destroy_listener.link);
665 free(state);
666}
667
668static void
669focus_state_seat_destroy(struct wl_listener *listener, void *data)
670{
671 struct focus_state *state = container_of(listener,
672 struct focus_state,
673 seat_destroy_listener);
674
675 wl_list_remove(&state->link);
676 focus_state_destroy(state);
677}
678
679static void
680focus_state_surface_destroy(struct wl_listener *listener, void *data)
681{
682 struct focus_state *state = container_of(listener,
683 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400684 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400685 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500686 struct weston_surface *main_surface, *next;
687 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200688
Pekka Paalanen01388e22013-04-25 13:57:44 +0300689 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
690
Kristian Høgsberge3778222012-07-31 17:29:30 -0400691 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500692 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
693 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400694 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100695 if (is_focus_view(view))
696 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400697
Jason Ekstranda7af7042013-10-12 22:38:11 -0500698 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400699 break;
700 }
701
Pekka Paalanen01388e22013-04-25 13:57:44 +0300702 /* if the focus was a sub-surface, activate its main surface */
703 if (main_surface != state->keyboard_focus)
704 next = main_surface;
705
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100706 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400707 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100708 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400709 activate(shell, next, state->seat);
710 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100711 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
712 if (state->ws->focus_animation)
713 weston_view_animation_destroy(state->ws->focus_animation);
714
715 state->ws->focus_animation = weston_fade_run(
716 state->ws->fsurf_front->view,
717 state->ws->fsurf_front->view->alpha, 0.0, 300,
718 focus_animation_done, state->ws);
719 }
720
Kristian Høgsberge3778222012-07-31 17:29:30 -0400721 wl_list_remove(&state->link);
722 focus_state_destroy(state);
723 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200724}
725
726static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400727focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200728{
Jonas Ådahl04769742012-06-13 00:01:24 +0200729 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200730
731 state = malloc(sizeof *state);
732 if (state == NULL)
733 return NULL;
734
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100735 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400736 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200737 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400738 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200739
740 state->seat_destroy_listener.notify = focus_state_seat_destroy;
741 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400742 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200743 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400744 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200745
746 return state;
747}
748
Jonas Ådahl8538b222012-08-29 22:13:03 +0200749static struct focus_state *
750ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
751{
752 struct workspace *ws = get_current_workspace(shell);
753 struct focus_state *state;
754
755 wl_list_for_each(state, &ws->focus_list, link)
756 if (state->seat == seat)
757 break;
758
759 if (&state->link == &ws->focus_list)
760 state = focus_state_create(seat, ws);
761
762 return state;
763}
764
Jonas Ådahl04769742012-06-13 00:01:24 +0200765static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400766restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200767{
768 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400769 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200770
771 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400772 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200773
Kristian Høgsberge3148752013-05-06 23:19:49 -0400774 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200775 }
776}
777
778static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200779replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
780 struct weston_seat *seat)
781{
782 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400783 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200784
785 wl_list_for_each(state, &ws->focus_list, link) {
786 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400787 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500788 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200789 return;
790 }
791 }
792}
793
794static void
795drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
796 struct weston_surface *surface)
797{
798 struct focus_state *state;
799
800 wl_list_for_each(state, &ws->focus_list, link)
801 if (state->keyboard_focus == surface)
802 state->keyboard_focus = NULL;
803}
804
805static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100806animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
807 struct weston_view *from, struct weston_view *to)
808{
809 struct weston_output *output;
810 bool focus_surface_created = false;
811
812 /* FIXME: Only support dim animation using two layers */
813 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
814 return;
815
816 output = get_default_output(shell->compositor);
817 if (ws->fsurf_front == NULL && (from || to)) {
818 ws->fsurf_front = create_focus_surface(shell->compositor, output);
819 ws->fsurf_back = create_focus_surface(shell->compositor, output);
820 ws->fsurf_front->view->alpha = 0.0;
821 ws->fsurf_back->view->alpha = 0.0;
822 focus_surface_created = true;
823 } else {
824 wl_list_remove(&ws->fsurf_front->view->layer_link);
825 wl_list_remove(&ws->fsurf_back->view->layer_link);
826 }
827
828 if (ws->focus_animation) {
829 weston_view_animation_destroy(ws->focus_animation);
830 ws->focus_animation = NULL;
831 }
832
833 if (to)
834 wl_list_insert(&to->layer_link,
835 &ws->fsurf_front->view->layer_link);
836 else if (from)
837 wl_list_insert(&ws->layer.view_list,
838 &ws->fsurf_front->view->layer_link);
839
840 if (focus_surface_created) {
841 ws->focus_animation = weston_fade_run(
842 ws->fsurf_front->view,
843 ws->fsurf_front->view->alpha, 0.6, 300,
844 focus_animation_done, ws);
845 } else if (from) {
846 wl_list_insert(&from->layer_link,
847 &ws->fsurf_back->view->layer_link);
848 ws->focus_animation = weston_stable_fade_run(
849 ws->fsurf_front->view, 0.0,
850 ws->fsurf_back->view, 0.6,
851 focus_animation_done, ws);
852 } else if (to) {
853 wl_list_insert(&ws->layer.view_list,
854 &ws->fsurf_back->view->layer_link);
855 ws->focus_animation = weston_stable_fade_run(
856 ws->fsurf_front->view, 0.0,
857 ws->fsurf_back->view, 0.6,
858 focus_animation_done, ws);
859 }
860}
861
862static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200863workspace_destroy(struct workspace *ws)
864{
Jonas Ådahl04769742012-06-13 00:01:24 +0200865 struct focus_state *state, *next;
866
867 wl_list_for_each_safe(state, next, &ws->focus_list, link)
868 focus_state_destroy(state);
869
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 if (ws->fsurf_front)
871 focus_surface_destroy(ws->fsurf_front);
872 if (ws->fsurf_back)
873 focus_surface_destroy(ws->fsurf_back);
874
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200875 free(ws);
876}
877
Jonas Ådahl04769742012-06-13 00:01:24 +0200878static void
879seat_destroyed(struct wl_listener *listener, void *data)
880{
881 struct weston_seat *seat = data;
882 struct focus_state *state, *next;
883 struct workspace *ws = container_of(listener,
884 struct workspace,
885 seat_destroyed_listener);
886
887 wl_list_for_each_safe(state, next, &ws->focus_list, link)
888 if (state->seat == seat)
889 wl_list_remove(&state->link);
890}
891
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200892static struct workspace *
893workspace_create(void)
894{
895 struct workspace *ws = malloc(sizeof *ws);
896 if (ws == NULL)
897 return NULL;
898
899 weston_layer_init(&ws->layer, NULL);
900
Jonas Ådahl04769742012-06-13 00:01:24 +0200901 wl_list_init(&ws->focus_list);
902 wl_list_init(&ws->seat_destroyed_listener.link);
903 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100904 ws->fsurf_front = NULL;
905 ws->fsurf_back = NULL;
906 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200907
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200908 return ws;
909}
910
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200911static int
912workspace_is_empty(struct workspace *ws)
913{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200915}
916
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200917static struct workspace *
918get_workspace(struct desktop_shell *shell, unsigned int index)
919{
920 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200921 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200922 pws += index;
923 return *pws;
924}
925
926static struct workspace *
927get_current_workspace(struct desktop_shell *shell)
928{
929 return get_workspace(shell, shell->workspaces.current);
930}
931
932static void
933activate_workspace(struct desktop_shell *shell, unsigned int index)
934{
935 struct workspace *ws;
936
937 ws = get_workspace(shell, index);
938 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
939
940 shell->workspaces.current = index;
941}
942
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200943static unsigned int
944get_output_height(struct weston_output *output)
945{
946 return abs(output->region.extents.y1 - output->region.extents.y2);
947}
948
949static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100950view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200951{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200952 struct weston_transform *transform;
953
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100954 if (is_focus_view(view)) {
955 struct focus_surface *fsurf = get_focus_surface(view->surface);
956 transform = &fsurf->workspace_transform;
957 } else {
958 struct shell_surface *shsurf = get_shell_surface(view->surface);
959 transform = &shsurf->workspace_transform;
960 }
961
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100964 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 weston_matrix_init(&transform->matrix);
967 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200968 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200970}
971
972static void
973workspace_translate_out(struct workspace *ws, double fraction)
974{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200976 unsigned int height;
977 double d;
978
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
980 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200981 d = height * fraction;
982
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100983 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984 }
985}
986
987static void
988workspace_translate_in(struct workspace *ws, double fraction)
989{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991 unsigned int height;
992 double d;
993
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
995 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996
997 if (fraction > 0)
998 d = -(height - height * fraction);
999 else
1000 d = height + height * fraction;
1001
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001002 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001003 }
1004}
1005
1006static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001007broadcast_current_workspace_state(struct desktop_shell *shell)
1008{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001009 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001010
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001011 wl_resource_for_each(resource, &shell->workspaces.client_list)
1012 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001013 shell->workspaces.current,
1014 shell->workspaces.num);
1015}
1016
1017static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001018reverse_workspace_change_animation(struct desktop_shell *shell,
1019 unsigned int index,
1020 struct workspace *from,
1021 struct workspace *to)
1022{
1023 shell->workspaces.current = index;
1024
1025 shell->workspaces.anim_to = to;
1026 shell->workspaces.anim_from = from;
1027 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1028 shell->workspaces.anim_timestamp = 0;
1029
Scott Moreau4272e632012-08-13 09:58:41 -06001030 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001031}
1032
1033static void
1034workspace_deactivate_transforms(struct workspace *ws)
1035{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001037 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001038
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001040 if (is_focus_view(view)) {
1041 struct focus_surface *fsurf = get_focus_surface(view->surface);
1042 transform = &fsurf->workspace_transform;
1043 } else {
1044 struct shell_surface *shsurf = get_shell_surface(view->surface);
1045 transform = &shsurf->workspace_transform;
1046 }
1047
1048 if (!wl_list_empty(&transform->link)) {
1049 wl_list_remove(&transform->link);
1050 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001051 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001053 }
1054}
1055
1056static void
1057finish_workspace_change_animation(struct desktop_shell *shell,
1058 struct workspace *from,
1059 struct workspace *to)
1060{
Scott Moreau4272e632012-08-13 09:58:41 -06001061 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001062
1063 wl_list_remove(&shell->workspaces.animation.link);
1064 workspace_deactivate_transforms(from);
1065 workspace_deactivate_transforms(to);
1066 shell->workspaces.anim_to = NULL;
1067
1068 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1069}
1070
1071static void
1072animate_workspace_change_frame(struct weston_animation *animation,
1073 struct weston_output *output, uint32_t msecs)
1074{
1075 struct desktop_shell *shell =
1076 container_of(animation, struct desktop_shell,
1077 workspaces.animation);
1078 struct workspace *from = shell->workspaces.anim_from;
1079 struct workspace *to = shell->workspaces.anim_to;
1080 uint32_t t;
1081 double x, y;
1082
1083 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1084 finish_workspace_change_animation(shell, from, to);
1085 return;
1086 }
1087
1088 if (shell->workspaces.anim_timestamp == 0) {
1089 if (shell->workspaces.anim_current == 0.0)
1090 shell->workspaces.anim_timestamp = msecs;
1091 else
1092 shell->workspaces.anim_timestamp =
1093 msecs -
1094 /* Invers of movement function 'y' below. */
1095 (asin(1.0 - shell->workspaces.anim_current) *
1096 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1097 M_2_PI);
1098 }
1099
1100 t = msecs - shell->workspaces.anim_timestamp;
1101
1102 /*
1103 * x = [0, π/2]
1104 * y(x) = sin(x)
1105 */
1106 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1107 y = sin(x);
1108
1109 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001110 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111
1112 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1113 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1114 shell->workspaces.anim_current = y;
1115
Scott Moreau4272e632012-08-13 09:58:41 -06001116 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001118 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120}
1121
1122static void
1123animate_workspace_change(struct desktop_shell *shell,
1124 unsigned int index,
1125 struct workspace *from,
1126 struct workspace *to)
1127{
1128 struct weston_output *output;
1129
1130 int dir;
1131
1132 if (index > shell->workspaces.current)
1133 dir = -1;
1134 else
1135 dir = 1;
1136
1137 shell->workspaces.current = index;
1138
1139 shell->workspaces.anim_dir = dir;
1140 shell->workspaces.anim_from = from;
1141 shell->workspaces.anim_to = to;
1142 shell->workspaces.anim_current = 0.0;
1143 shell->workspaces.anim_timestamp = 0;
1144
1145 output = container_of(shell->compositor->output_list.next,
1146 struct weston_output, link);
1147 wl_list_insert(&output->animation_list,
1148 &shell->workspaces.animation.link);
1149
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001150 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001151
1152 workspace_translate_in(to, 0);
1153
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001154 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001155
Scott Moreau4272e632012-08-13 09:58:41 -06001156 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157}
1158
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001159static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160update_workspace(struct desktop_shell *shell, unsigned int index,
1161 struct workspace *from, struct workspace *to)
1162{
1163 shell->workspaces.current = index;
1164 wl_list_insert(&from->layer.link, &to->layer.link);
1165 wl_list_remove(&from->layer.link);
1166}
1167
1168static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001169change_workspace(struct desktop_shell *shell, unsigned int index)
1170{
1171 struct workspace *from;
1172 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001173 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001174
1175 if (index == shell->workspaces.current)
1176 return;
1177
1178 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001180 return;
1181
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001182 from = get_current_workspace(shell);
1183 to = get_workspace(shell, index);
1184
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185 if (shell->workspaces.anim_from == to &&
1186 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001187 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001188 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001189 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001190 return;
1191 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001192
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001193 if (shell->workspaces.anim_to != NULL)
1194 finish_workspace_change_animation(shell,
1195 shell->workspaces.anim_from,
1196 shell->workspaces.anim_to);
1197
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001199
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001200 if (shell->focus_animation_type != ANIMATION_NONE) {
1201 wl_list_for_each(state, &from->focus_list, link)
1202 if (state->keyboard_focus)
1203 animate_focus_change(shell, from,
1204 get_default_view(state->keyboard_focus), NULL);
1205
1206 wl_list_for_each(state, &to->focus_list, link)
1207 if (state->keyboard_focus)
1208 animate_focus_change(shell, to,
1209 NULL, get_default_view(state->keyboard_focus));
1210 }
1211
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001212 if (workspace_is_empty(to) && workspace_is_empty(from))
1213 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001214 else
1215 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001216
1217 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001218}
1219
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001220static bool
1221workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1222{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 struct wl_list *e;
1225
1226 if (wl_list_empty(list))
1227 return false;
1228
1229 e = list->next;
1230
1231 if (e->next != list)
1232 return false;
1233
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001235}
1236
1237static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238move_view_to_workspace(struct desktop_shell *shell,
1239 struct weston_view *view,
1240 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001241{
1242 struct workspace *from;
1243 struct workspace *to;
1244 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001245 struct weston_surface *focus;
1246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001248
1249 if (workspace == shell->workspaces.current)
1250 return;
1251
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001252 if (workspace >= shell->workspaces.num)
1253 workspace = shell->workspaces.num - 1;
1254
Jonas Ådahle9d22502012-08-29 22:13:01 +02001255 from = get_current_workspace(shell);
1256 to = get_workspace(shell, workspace);
1257
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 wl_list_remove(&view->layer_link);
1259 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001260
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001262 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1263 if (!seat->keyboard)
1264 continue;
1265
1266 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001268 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001269 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001272}
1273
1274static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001275take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001276 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001277 unsigned int index)
1278{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001279 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001281 struct shell_surface *shsurf;
1282 struct workspace *from;
1283 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001284 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001285
Pekka Paalanen01388e22013-04-25 13:57:44 +03001286 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 view = get_default_view(surface);
1288 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001289 index == shell->workspaces.current ||
1290 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001291 return;
1292
1293 from = get_current_workspace(shell);
1294 to = get_workspace(shell, index);
1295
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 wl_list_remove(&view->layer_link);
1297 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001298
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001300 drop_focus_state(shell, from, surface);
1301
1302 if (shell->workspaces.anim_from == to &&
1303 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001304 wl_list_remove(&to->layer.link);
1305 wl_list_insert(from->layer.link.prev, &to->layer.link);
1306
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001307 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001308 broadcast_current_workspace_state(shell);
1309
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001310 return;
1311 }
1312
1313 if (shell->workspaces.anim_to != NULL)
1314 finish_workspace_change_animation(shell,
1315 shell->workspaces.anim_from,
1316 shell->workspaces.anim_to);
1317
1318 if (workspace_is_empty(from) &&
1319 workspace_has_only(to, surface))
1320 update_workspace(shell, index, from, to);
1321 else {
1322 shsurf = get_shell_surface(surface);
1323 if (wl_list_empty(&shsurf->workspace_transform.link))
1324 wl_list_insert(&shell->workspaces.anim_sticky_list,
1325 &shsurf->workspace_transform.link);
1326
1327 animate_workspace_change(shell, index, from, to);
1328 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001329
1330 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001331
1332 state = ensure_focus_state(shell, seat);
1333 if (state != NULL)
1334 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001335}
1336
1337static void
1338workspace_manager_move_surface(struct wl_client *client,
1339 struct wl_resource *resource,
1340 struct wl_resource *surface_resource,
1341 uint32_t workspace)
1342{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001343 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001344 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001345 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001346 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001348
Pekka Paalanen01388e22013-04-25 13:57:44 +03001349 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 view = get_default_view(main_surface);
1351 if (!view)
1352 return;
1353 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001354}
1355
1356static const struct workspace_manager_interface workspace_manager_implementation = {
1357 workspace_manager_move_surface,
1358};
1359
1360static void
1361unbind_resource(struct wl_resource *resource)
1362{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001363 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001364}
1365
1366static void
1367bind_workspace_manager(struct wl_client *client,
1368 void *data, uint32_t version, uint32_t id)
1369{
1370 struct desktop_shell *shell = data;
1371 struct wl_resource *resource;
1372
Jason Ekstranda85118c2013-06-27 20:17:02 -05001373 resource = wl_resource_create(client,
1374 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001375
1376 if (resource == NULL) {
1377 weston_log("couldn't add workspace manager object");
1378 return;
1379 }
1380
Jason Ekstranda85118c2013-06-27 20:17:02 -05001381 wl_resource_set_implementation(resource,
1382 &workspace_manager_implementation,
1383 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001384 wl_list_insert(&shell->workspaces.client_list,
1385 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001386
1387 workspace_manager_send_state(resource,
1388 shell->workspaces.current,
1389 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001390}
1391
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001392static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001393touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1394 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1395{
1396}
1397
1398static void
1399touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1400{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001401 struct weston_touch_move_grab *move =
1402 (struct weston_touch_move_grab *) container_of(
1403 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001404
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001405 if (grab->touch->seat->num_tp == 0) {
1406 shell_touch_grab_end(&move->base);
1407 free(move);
1408 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001409}
1410
1411static void
1412touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1413 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1414{
1415 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1416 struct shell_surface *shsurf = move->base.shsurf;
1417 struct weston_surface *es;
1418 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1419 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1420
1421 if (!shsurf)
1422 return;
1423
1424 es = shsurf->surface;
1425
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 weston_view_configure(shsurf->view, dx, dy,
1427 shsurf->view->geometry.width,
1428 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001429
1430 weston_compositor_schedule_repaint(es->compositor);
1431}
1432
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001433static void
1434touch_move_grab_cancel(struct weston_touch_grab *grab)
1435{
1436 struct weston_touch_move_grab *move =
1437 (struct weston_touch_move_grab *) container_of(
1438 grab, struct shell_touch_grab, grab);
1439
1440 shell_touch_grab_end(&move->base);
1441 free(move);
1442}
1443
Rusty Lynch1084da52013-08-15 09:10:08 -07001444static const struct weston_touch_grab_interface touch_move_grab_interface = {
1445 touch_move_grab_down,
1446 touch_move_grab_up,
1447 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001448 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001449};
1450
1451static int
1452surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1453{
1454 struct weston_touch_move_grab *move;
1455
1456 if (!shsurf)
1457 return -1;
1458
1459 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1460 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001461 if (shsurf->grabbed)
1462 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001463
1464 move = malloc(sizeof *move);
1465 if (!move)
1466 return -1;
1467
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001469 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001471 seat->touch->grab_y;
1472
1473 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1474 seat->touch);
1475
1476 return 0;
1477}
1478
1479static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001480noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001481{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001482}
1483
1484static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001485move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1486 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001487{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001488 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001489 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001490 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001491 int dx, dy;
1492
1493 weston_pointer_move(pointer, x, y);
1494 dx = wl_fixed_to_int(pointer->x + move->dx);
1495 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001496
1497 if (!shsurf)
1498 return;
1499
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500 weston_view_configure(shsurf->view, dx, dy,
1501 shsurf->view->geometry.width,
1502 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001503
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001505}
1506
1507static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001508move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001509 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001510{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001511 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1512 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001513 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001514 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001515
Daniel Stone4dbadb12012-05-30 16:31:51 +01001516 if (pointer->button_count == 0 &&
1517 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001518 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001519 free(grab);
1520 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001521}
1522
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001523static void
1524move_grab_cancel(struct weston_pointer_grab *grab)
1525{
1526 struct shell_grab *shell_grab =
1527 container_of(grab, struct shell_grab, grab);
1528
1529 shell_grab_end(shell_grab);
1530 free(grab);
1531}
1532
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001533static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001534 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001535 move_grab_motion,
1536 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001537 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001538};
1539
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001540static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001541surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001542{
1543 struct weston_move_grab *move;
1544
1545 if (!shsurf)
1546 return -1;
1547
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001548 if (shsurf->grabbed)
1549 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001550 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1551 return 0;
1552
1553 move = malloc(sizeof *move);
1554 if (!move)
1555 return -1;
1556
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001558 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001560 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561
1562 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001563 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001564
1565 return 0;
1566}
1567
1568static void
1569shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1570 struct wl_resource *seat_resource, uint32_t serial)
1571{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001572 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001573 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001574 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001575
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001576 if (seat->pointer &&
1577 seat->pointer->button_count > 0 &&
1578 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001580 if ((surface == shsurf->surface) &&
1581 (surface_move(shsurf, seat) < 0))
1582 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001583 } else if (seat->touch &&
1584 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001586 if ((surface == shsurf->surface) &&
1587 (surface_touch_move(shsurf, seat) < 0))
1588 wl_resource_post_no_memory(resource);
1589 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001590}
1591
1592struct weston_resize_grab {
1593 struct shell_grab base;
1594 uint32_t edges;
1595 int32_t width, height;
1596};
1597
1598static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001599resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1600 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001601{
1602 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001603 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604 struct shell_surface *shsurf = resize->base.shsurf;
1605 int32_t width, height;
1606 wl_fixed_t from_x, from_y;
1607 wl_fixed_t to_x, to_y;
1608
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001609 weston_pointer_move(pointer, x, y);
1610
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001611 if (!shsurf)
1612 return;
1613
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614 weston_view_from_global_fixed(shsurf->view,
1615 pointer->grab_x, pointer->grab_y,
1616 &from_x, &from_y);
1617 weston_view_from_global_fixed(shsurf->view,
1618 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001619
1620 width = resize->width;
1621 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1622 width += wl_fixed_to_int(from_x - to_x);
1623 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1624 width += wl_fixed_to_int(to_x - from_x);
1625 }
1626
1627 height = resize->height;
1628 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1629 height += wl_fixed_to_int(from_y - to_y);
1630 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1631 height += wl_fixed_to_int(to_y - from_y);
1632 }
1633
1634 shsurf->client->send_configure(shsurf->surface,
1635 resize->edges, width, height);
1636}
1637
1638static void
1639send_configure(struct weston_surface *surface,
1640 uint32_t edges, int32_t width, int32_t height)
1641{
1642 struct shell_surface *shsurf = get_shell_surface(surface);
1643
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001644 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645 edges, width, height);
1646}
1647
1648static const struct weston_shell_client shell_client = {
1649 send_configure
1650};
1651
1652static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001653resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001654 uint32_t time, uint32_t button, uint32_t state_w)
1655{
1656 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001657 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001658 enum wl_pointer_button_state state = state_w;
1659
1660 if (pointer->button_count == 0 &&
1661 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1662 shell_grab_end(&resize->base);
1663 free(grab);
1664 }
1665}
1666
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001667static void
1668resize_grab_cancel(struct weston_pointer_grab *grab)
1669{
1670 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1671
1672 shell_grab_end(&resize->base);
1673 free(grab);
1674}
1675
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001676static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677 noop_grab_focus,
1678 resize_grab_motion,
1679 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001680 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001681};
1682
Giulio Camuffob8366642013-04-25 13:57:46 +03001683/*
1684 * Returns the bounding box of a surface and all its sub-surfaces,
1685 * in the surface coordinates system. */
1686static void
1687surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1688 int32_t *y, int32_t *w, int32_t *h) {
1689 pixman_region32_t region;
1690 pixman_box32_t *box;
1691 struct weston_subsurface *subsurface;
1692
1693 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001694 surface->width,
1695 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001696
1697 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1698 pixman_region32_union_rect(&region, &region,
1699 subsurface->position.x,
1700 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 subsurface->surface->width,
1702 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001703 }
1704
1705 box = pixman_region32_extents(&region);
1706 if (x)
1707 *x = box->x1;
1708 if (y)
1709 *y = box->y1;
1710 if (w)
1711 *w = box->x2 - box->x1;
1712 if (h)
1713 *h = box->y2 - box->y1;
1714
1715 pixman_region32_fini(&region);
1716}
1717
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001718static int
1719surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001720 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001721{
1722 struct weston_resize_grab *resize;
1723
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001724 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1725 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001726 return 0;
1727
1728 if (edges == 0 || edges > 15 ||
1729 (edges & 3) == 3 || (edges & 12) == 12)
1730 return 0;
1731
1732 resize = malloc(sizeof *resize);
1733 if (!resize)
1734 return -1;
1735
1736 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001737 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1738 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739
1740 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001741 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001742
1743 return 0;
1744}
1745
1746static void
1747shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1748 struct wl_resource *seat_resource, uint32_t serial,
1749 uint32_t edges)
1750{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001751 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001752 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001753 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001754
1755 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1756 return;
1757
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001759 if (seat->pointer->button_count == 0 ||
1760 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001761 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762 return;
1763
Kristian Høgsberge3148752013-05-06 23:19:49 -04001764 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001765 wl_resource_post_no_memory(resource);
1766}
1767
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001768static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001769end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1770
1771static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001772busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001773{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001774 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001775 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001777 wl_fixed_t sx, sy;
1778
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 view = weston_compositor_pick_view(pointer->seat->compositor,
1780 pointer->x, pointer->y,
1781 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001782
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001784 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001785}
1786
1787static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001788busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1789 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001790{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001791 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001793
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001795busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001796 uint32_t time, uint32_t button, uint32_t state)
1797{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001798 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001799 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001800 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001801
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001802 if (shsurf && button == BTN_LEFT && state) {
1803 activate(shsurf->shell, shsurf->surface, seat);
1804 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001805 } else if (shsurf && button == BTN_RIGHT && state) {
1806 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001807 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001808 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001809}
1810
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001811static void
1812busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1813{
1814 struct shell_grab *grab = (struct shell_grab *) base;
1815
1816 shell_grab_end(grab);
1817 free(grab);
1818}
1819
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001820static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001821 busy_cursor_grab_focus,
1822 busy_cursor_grab_motion,
1823 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001824 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825};
1826
1827static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001828set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001829{
1830 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831
1832 grab = malloc(sizeof *grab);
1833 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001834 return;
1835
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001836 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1837 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001838}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001839
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001840static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001841end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001842{
1843 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1844
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001845 if (grab->grab.interface == &busy_cursor_grab_interface &&
1846 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001847 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001849 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001850}
1851
Scott Moreau9521d5e2012-04-19 13:06:17 -06001852static void
1853ping_timer_destroy(struct shell_surface *shsurf)
1854{
1855 if (!shsurf || !shsurf->ping_timer)
1856 return;
1857
1858 if (shsurf->ping_timer->source)
1859 wl_event_source_remove(shsurf->ping_timer->source);
1860
1861 free(shsurf->ping_timer);
1862 shsurf->ping_timer = NULL;
1863}
1864
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001865static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001866ping_timeout_handler(void *data)
1867{
1868 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001869 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001870
Scott Moreau9521d5e2012-04-19 13:06:17 -06001871 /* Client is not responding */
1872 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001873
1874 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001875 if (seat->pointer->focus &&
1876 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001877 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001878
1879 return 1;
1880}
1881
1882static void
1883ping_handler(struct weston_surface *surface, uint32_t serial)
1884{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001885 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001886 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001887 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001888
1889 if (!shsurf)
1890 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001891 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001892 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001893
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001894 if (shsurf->surface == shsurf->shell->grab_surface)
1895 return;
1896
Scott Moreauff1db4a2012-04-17 19:06:18 -06001897 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001898 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001899 if (!shsurf->ping_timer)
1900 return;
1901
1902 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001903 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1904 shsurf->ping_timer->source =
1905 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1906 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1907
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001908 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001909 }
1910}
1911
1912static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001913handle_pointer_focus(struct wl_listener *listener, void *data)
1914{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001915 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001917 struct weston_compositor *compositor;
1918 struct shell_surface *shsurf;
1919 uint32_t serial;
1920
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001922 return;
1923
Jason Ekstranda7af7042013-10-12 22:38:11 -05001924 compositor = view->surface->compositor;
1925 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001926
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001927 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001928 set_busy_cursor(shsurf, pointer);
1929 } else {
1930 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001931 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001932 }
1933}
1934
1935static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001936create_pointer_focus_listener(struct weston_seat *seat)
1937{
1938 struct wl_listener *listener;
1939
Kristian Høgsberge3148752013-05-06 23:19:49 -04001940 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001941 return;
1942
1943 listener = malloc(sizeof *listener);
1944 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001945 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001946}
1947
1948static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001949shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1950 uint32_t serial)
1951{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001952 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001953 struct weston_seat *seat;
1954 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001955
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001956 if (shsurf->ping_timer == NULL)
1957 /* Just ignore unsolicited pong. */
1958 return;
1959
Scott Moreauff1db4a2012-04-17 19:06:18 -06001960 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001961 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001962 wl_list_for_each(seat, &ec->seat_list, link) {
1963 if(seat->pointer)
1964 end_busy_cursor(shsurf, seat->pointer);
1965 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001966 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001967 }
1968}
1969
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001970static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001971set_title(struct shell_surface *shsurf, const char *title)
1972{
1973 free(shsurf->title);
1974 shsurf->title = strdup(title);
1975}
1976
1977static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001978shell_surface_set_title(struct wl_client *client,
1979 struct wl_resource *resource, const char *title)
1980{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001981 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001982
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001983 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001984}
1985
1986static void
1987shell_surface_set_class(struct wl_client *client,
1988 struct wl_resource *resource, const char *class)
1989{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001990 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001991
1992 free(shsurf->class);
1993 shsurf->class = strdup(class);
1994}
1995
Alex Wu4539b082012-03-01 12:57:46 +08001996static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001997restore_output_mode(struct weston_output *output)
1998{
Hardening57388e42013-09-18 23:56:36 +02001999 if (output->current_mode != output->original_mode ||
2000 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002001 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002002 output->original_mode,
2003 output->original_scale,
2004 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002005}
2006
2007static void
2008restore_all_output_modes(struct weston_compositor *compositor)
2009{
2010 struct weston_output *output;
2011
2012 wl_list_for_each(output, &compositor->output_list, link)
2013 restore_output_mode(output);
2014}
2015
Philip Withnallbecb77e2013-11-25 18:01:30 +00002016static int
2017get_output_panel_height(struct desktop_shell *shell,
2018 struct weston_output *output)
2019{
2020 struct weston_view *view;
2021 int panel_height = 0;
2022
2023 if (!output)
2024 return 0;
2025
2026 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2027 if (view->surface->output == output) {
2028 panel_height = view->geometry.height;
2029 break;
2030 }
2031 }
2032
2033 return panel_height;
2034}
2035
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002036static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002037set_toplevel(struct shell_surface *shsurf)
2038{
2039 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
2040}
2041
2042static void
2043shell_surface_set_toplevel(struct wl_client *client,
2044 struct wl_resource *resource)
2045{
2046 struct shell_surface *surface = wl_resource_get_user_data(resource);
2047
2048 set_toplevel(surface);
2049}
2050
2051static void
2052set_transient(struct shell_surface *shsurf,
2053 struct weston_surface *parent, int x, int y, uint32_t flags)
2054{
2055 /* assign to parents output */
2056 shsurf->parent = parent;
2057 shsurf->transient.x = x;
2058 shsurf->transient.y = y;
2059 shsurf->transient.flags = flags;
2060 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2061}
2062
2063static void
2064shell_surface_set_transient(struct wl_client *client,
2065 struct wl_resource *resource,
2066 struct wl_resource *parent_resource,
2067 int x, int y, uint32_t flags)
2068{
2069 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2070 struct weston_surface *parent =
2071 wl_resource_get_user_data(parent_resource);
2072
2073 set_transient(shsurf, parent, x, y, flags);
2074}
2075
2076static void
2077set_fullscreen(struct shell_surface *shsurf,
2078 uint32_t method,
2079 uint32_t framerate,
2080 struct weston_output *output)
2081{
2082 struct weston_surface *es = shsurf->surface;
2083
2084 if (output)
2085 shsurf->output = output;
2086 else if (es->output)
2087 shsurf->output = es->output;
2088 else
2089 shsurf->output = get_default_output(es->compositor);
2090
2091 shsurf->fullscreen_output = shsurf->output;
2092 shsurf->fullscreen.type = method;
2093 shsurf->fullscreen.framerate = framerate;
2094 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
2095
2096 shsurf->client->send_configure(shsurf->surface, 0,
2097 shsurf->output->width,
2098 shsurf->output->height);
2099}
2100
2101static void
2102unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002103{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002104 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08002105 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08002106 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2107 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002108 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002109 }
Alex Wu4539b082012-03-01 12:57:46 +08002110 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2111 shsurf->fullscreen.framerate = 0;
2112 wl_list_remove(&shsurf->fullscreen.transform.link);
2113 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002114 if (shsurf->fullscreen.black_view)
2115 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2116 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002117 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002118 weston_view_set_position(shsurf->view,
2119 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002120 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002122 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002123 shsurf->saved_rotation_valid = false;
2124 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002125
2126 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127 wl_list_remove(&shsurf->view->layer_link);
2128 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08002129}
2130
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002131static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002132shell_surface_set_fullscreen(struct wl_client *client,
2133 struct wl_resource *resource,
2134 uint32_t method,
2135 uint32_t framerate,
2136 struct wl_resource *output_resource)
2137{
2138 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2139 struct weston_output *output;
2140
2141 if (output_resource)
2142 output = wl_resource_get_user_data(output_resource);
2143 else
2144 output = NULL;
2145
2146 set_fullscreen(shsurf, method, framerate, output);
2147}
2148
2149static void
2150set_popup(struct shell_surface *shsurf,
2151 struct weston_surface *parent,
2152 struct weston_seat *seat,
2153 uint32_t serial,
2154 int32_t x,
2155 int32_t y)
2156{
2157 shsurf->type = SHELL_SURFACE_POPUP;
2158 shsurf->parent = parent;
2159 shsurf->popup.shseat = get_shell_seat(seat);
2160 shsurf->popup.serial = serial;
2161 shsurf->popup.x = x;
2162 shsurf->popup.y = y;
2163}
2164
2165static void
2166shell_surface_set_popup(struct wl_client *client,
2167 struct wl_resource *resource,
2168 struct wl_resource *seat_resource,
2169 uint32_t serial,
2170 struct wl_resource *parent_resource,
2171 int32_t x, int32_t y, uint32_t flags)
2172{
2173 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2174
2175 set_popup(shsurf,
2176 wl_resource_get_user_data(parent_resource),
2177 wl_resource_get_user_data(seat_resource),
2178 serial, x, y);
2179}
2180
2181static void
2182set_maximized(struct shell_surface *shsurf,
2183 struct weston_output *output)
2184{
2185 struct desktop_shell *shell;
2186 uint32_t edges = 0, panel_height = 0;
2187 struct weston_surface *es = shsurf->surface;
2188
2189 /* get the default output, if the client set it as NULL
2190 check whether the ouput is available */
2191 if (output)
2192 shsurf->output = output;
2193 else if (es->output)
2194 shsurf->output = es->output;
2195 else
2196 shsurf->output = get_default_output(es->compositor);
2197
2198 shell = shell_surface_get_shell(shsurf);
2199 panel_height = get_output_panel_height(shell, shsurf->output);
2200 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2201
2202 shsurf->client->send_configure(shsurf->surface, edges,
2203 shsurf->output->width,
2204 shsurf->output->height - panel_height);
2205
2206 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
2207}
2208
2209static void
2210unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002211{
2212 struct workspace *ws;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002213
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002214 /* undo all maximized things here */
2215 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002216 weston_view_set_position(shsurf->view,
2217 shsurf->saved_x,
2218 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002219
2220 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221 wl_list_insert(&shsurf->view->geometry.transformation_list,
2222 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002223 shsurf->saved_rotation_valid = false;
2224 }
2225
2226 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002227 wl_list_remove(&shsurf->view->layer_link);
2228 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002229}
2230
Philip Withnallbecb77e2013-11-25 18:01:30 +00002231static void
2232shell_surface_set_maximized(struct wl_client *client,
2233 struct wl_resource *resource,
2234 struct wl_resource *output_resource)
2235{
2236 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2237 struct weston_output *output;
2238
2239 if (output_resource)
2240 output = wl_resource_get_user_data(output_resource);
2241 else
2242 output = NULL;
2243
2244 set_maximized(shsurf, output);
2245}
2246
Pekka Paalanen98262232011-12-01 10:42:22 +02002247static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002248reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002249{
2250 switch (surface->type) {
2251 case SHELL_SURFACE_FULLSCREEN:
Philip Withnallbecb77e2013-11-25 18:01:30 +00002252 unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002253 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002254 case SHELL_SURFACE_MAXIMIZED:
Philip Withnallbecb77e2013-11-25 18:01:30 +00002255 unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002256 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002257 case SHELL_SURFACE_NONE:
2258 case SHELL_SURFACE_TOPLEVEL:
2259 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002260 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002261 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02002262 break;
2263 }
2264
2265 surface->type = SHELL_SURFACE_NONE;
2266 return 0;
2267}
2268
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002269static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002270set_surface_type(struct shell_surface *shsurf)
2271{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002272 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002274
Philip Withnallbecb77e2013-11-25 18:01:30 +00002275 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002276
2277 shsurf->type = shsurf->next_type;
2278 shsurf->next_type = SHELL_SURFACE_NONE;
2279
2280 switch (shsurf->type) {
2281 case SHELL_SURFACE_TOPLEVEL:
2282 break;
2283 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002284 if (pev)
2285 weston_view_set_position(shsurf->view,
2286 pev->geometry.x + shsurf->transient.x,
2287 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002288 break;
2289
2290 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002291 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292 shsurf->saved_x = shsurf->view->geometry.x;
2293 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002294 shsurf->saved_position_valid = true;
2295
2296 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2297 wl_list_remove(&shsurf->rotation.transform.link);
2298 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002299 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002300 shsurf->saved_rotation_valid = true;
2301 }
2302 break;
2303
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002304 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 weston_view_set_position(shsurf->view, shsurf->transient.x,
2306 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002307 break;
2308
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002309 default:
2310 break;
2311 }
2312}
2313
Tiago Vignattibe143262012-04-16 17:31:41 +03002314static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002315shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002316{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002317 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002318}
2319
Alex Wu21858432012-04-01 20:13:08 +08002320static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002321black_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 +08002322
Jason Ekstranda7af7042013-10-12 22:38:11 -05002323static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002324create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002325 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002326 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002327{
2328 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002329 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002330
2331 surface = weston_surface_create(ec);
2332 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002333 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002334 return NULL;
2335 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002336 view = weston_view_create(surface);
2337 if (surface == NULL) {
2338 weston_log("no memory\n");
2339 weston_surface_destroy(surface);
2340 return NULL;
2341 }
Alex Wu4539b082012-03-01 12:57:46 +08002342
Alex Wu21858432012-04-01 20:13:08 +08002343 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002344 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002345 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002346 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002347 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002348 pixman_region32_fini(&surface->input);
2349 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002350 surface->width = w;
2351 surface->height = h;
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002352
Jason Ekstranda7af7042013-10-12 22:38:11 -05002353 weston_view_configure(view, x, y, w, h);
2354
2355 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002356}
2357
2358/* Create black surface and append it to the associated fullscreen surface.
2359 * Handle size dismatch and positioning according to the method. */
2360static void
2361shell_configure_fullscreen(struct shell_surface *shsurf)
2362{
2363 struct weston_output *output = shsurf->fullscreen_output;
2364 struct weston_surface *surface = shsurf->surface;
2365 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002366 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002367 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002368
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002369 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2370 restore_output_mode(output);
2371
Jason Ekstranda7af7042013-10-12 22:38:11 -05002372 if (!shsurf->fullscreen.black_view)
2373 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002374 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002375 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002376 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002377 output->width,
2378 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002379
Jason Ekstranda7af7042013-10-12 22:38:11 -05002380 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2381 wl_list_insert(&shsurf->view->layer_link,
2382 &shsurf->fullscreen.black_view->layer_link);
2383 shsurf->fullscreen.black_view->surface->output = output;
2384 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002385
Jason Ekstranda7af7042013-10-12 22:38:11 -05002386 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002387 &surf_width, &surf_height);
2388
Alex Wu4539b082012-03-01 12:57:46 +08002389 switch (shsurf->fullscreen.type) {
2390 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002391 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002393 break;
2394 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002395 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002396 if (output->width == surf_width &&
2397 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002398 weston_view_set_position(shsurf->view,
2399 output->x - surf_x,
2400 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002401 break;
2402 }
2403
Alex Wu4539b082012-03-01 12:57:46 +08002404 matrix = &shsurf->fullscreen.transform.matrix;
2405 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002406
Scott Moreau1bad5db2012-08-18 01:04:05 -06002407 output_aspect = (float) output->width /
2408 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002409 /* XXX: Use surf_width and surf_height here? */
2410 surface_aspect = (float) surface->width /
2411 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002412 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002413 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002414 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002415 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002416 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002417 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002418
Alex Wu4539b082012-03-01 12:57:46 +08002419 weston_matrix_scale(matrix, scale, scale, 1);
2420 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002421 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002422 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002423 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2424 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002425 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002426
Alex Wu4539b082012-03-01 12:57:46 +08002427 break;
2428 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002429 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002430 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002431 surf_width * surface->buffer_viewport.scale,
2432 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002433 shsurf->fullscreen.framerate};
2434
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002435 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002436 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002437 weston_view_set_position(shsurf->view,
2438 output->x - surf_x,
2439 output->y - surf_y);
2440 weston_view_configure(shsurf->fullscreen.black_view,
2441 output->x - surf_x,
2442 output->y - surf_y,
2443 output->width,
2444 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002445 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002446 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002447 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002448 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002449 }
Alex Wubd3354b2012-04-17 17:20:49 +08002450 }
Alex Wu4539b082012-03-01 12:57:46 +08002451 break;
2452 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002453 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002454 break;
2455 default:
2456 break;
2457 }
2458}
2459
2460/* make the fullscreen and black surface at the top */
2461static void
2462shell_stack_fullscreen(struct shell_surface *shsurf)
2463{
Alex Wubd3354b2012-04-17 17:20:49 +08002464 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002465 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002466
Jason Ekstranda7af7042013-10-12 22:38:11 -05002467 wl_list_remove(&shsurf->view->layer_link);
2468 wl_list_insert(&shell->fullscreen_layer.view_list,
2469 &shsurf->view->layer_link);
2470 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002471
Jason Ekstranda7af7042013-10-12 22:38:11 -05002472 if (!shsurf->fullscreen.black_view)
2473 shsurf->fullscreen.black_view =
2474 create_black_surface(shsurf->surface->compositor,
2475 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002476 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002477 output->width,
2478 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002479
Jason Ekstranda7af7042013-10-12 22:38:11 -05002480 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2481 wl_list_insert(&shsurf->view->layer_link,
2482 &shsurf->fullscreen.black_view->layer_link);
2483 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002484}
2485
2486static void
2487shell_map_fullscreen(struct shell_surface *shsurf)
2488{
Alex Wu4539b082012-03-01 12:57:46 +08002489 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002490 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002491}
2492
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002493static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002494set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2495{
2496 /* XXX: using the same fields for transient type */
2497 shsurf->transient.x = x;
2498 shsurf->transient.y = y;
2499 shsurf->transient.flags = flags;
2500 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2501}
2502
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002503static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002504
2505static void
2506destroy_shell_seat(struct wl_listener *listener, void *data)
2507{
2508 struct shell_seat *shseat =
2509 container_of(listener,
2510 struct shell_seat, seat_destroy_listener);
2511 struct shell_surface *shsurf, *prev = NULL;
2512
2513 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002514 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002515 shseat->popup_grab.client = NULL;
2516
2517 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2518 shsurf->popup.shseat = NULL;
2519 if (prev) {
2520 wl_list_init(&prev->popup.grab_link);
2521 }
2522 prev = shsurf;
2523 }
2524 wl_list_init(&prev->popup.grab_link);
2525 }
2526
2527 wl_list_remove(&shseat->seat_destroy_listener.link);
2528 free(shseat);
2529}
2530
2531static struct shell_seat *
2532create_shell_seat(struct weston_seat *seat)
2533{
2534 struct shell_seat *shseat;
2535
2536 shseat = calloc(1, sizeof *shseat);
2537 if (!shseat) {
2538 weston_log("no memory to allocate shell seat\n");
2539 return NULL;
2540 }
2541
2542 shseat->seat = seat;
2543 wl_list_init(&shseat->popup_grab.surfaces_list);
2544
2545 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2546 wl_signal_add(&seat->destroy_signal,
2547 &shseat->seat_destroy_listener);
2548
2549 return shseat;
2550}
2551
2552static struct shell_seat *
2553get_shell_seat(struct weston_seat *seat)
2554{
2555 struct wl_listener *listener;
2556
2557 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2558 if (listener == NULL)
2559 return create_shell_seat(seat);
2560
2561 return container_of(listener,
2562 struct shell_seat, seat_destroy_listener);
2563}
2564
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002565static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002566popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002567{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002568 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002569 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002570 struct shell_seat *shseat =
2571 container_of(grab, struct shell_seat, popup_grab.grab);
2572 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002573 wl_fixed_t sx, sy;
2574
Jason Ekstranda7af7042013-10-12 22:38:11 -05002575 view = weston_compositor_pick_view(pointer->seat->compositor,
2576 pointer->x, pointer->y,
2577 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002578
Jason Ekstranda7af7042013-10-12 22:38:11 -05002579 if (view && view->surface->resource &&
2580 wl_resource_get_client(view->surface->resource) == client) {
2581 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002582 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002583 weston_pointer_set_focus(pointer, NULL,
2584 wl_fixed_from_int(0),
2585 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002586 }
2587}
2588
2589static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002590popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2591 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002592{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002593 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002594 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002595 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002596
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002597 weston_pointer_move(pointer, x, y);
2598
Neil Roberts96d790e2013-09-19 17:32:00 +01002599 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002600 weston_view_from_global_fixed(pointer->focus,
2601 pointer->x, pointer->y,
2602 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002603 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002604 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002605}
2606
2607static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002608popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002609 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002610{
2611 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002612 struct shell_seat *shseat =
2613 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002614 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002615 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002616 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002617 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002618
Neil Roberts96d790e2013-09-19 17:32:00 +01002619 resource_list = &grab->pointer->focus_resource_list;
2620 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002621 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002622 wl_resource_for_each(resource, resource_list) {
2623 wl_pointer_send_button(resource, serial,
2624 time, button, state);
2625 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002626 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002627 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002628 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002629 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002630 }
2631
Daniel Stone4dbadb12012-05-30 16:31:51 +01002632 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002633 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002634}
2635
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002636static void
2637popup_grab_cancel(struct weston_pointer_grab *grab)
2638{
2639 popup_grab_end(grab->pointer);
2640}
2641
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002642static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002643 popup_grab_focus,
2644 popup_grab_motion,
2645 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002646 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002647};
2648
2649static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002650popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002651{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002652 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002653 struct shell_seat *shseat =
2654 container_of(grab, struct shell_seat, popup_grab.grab);
2655 struct shell_surface *shsurf;
2656 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002657
2658 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002659 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002660 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002661 shseat->popup_grab.grab.interface = NULL;
2662 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002663 /* Send the popup_done event to all the popups open */
2664 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002665 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002666 shsurf->popup.shseat = NULL;
2667 if (prev) {
2668 wl_list_init(&prev->popup.grab_link);
2669 }
2670 prev = shsurf;
2671 }
2672 wl_list_init(&prev->popup.grab_link);
2673 wl_list_init(&shseat->popup_grab.surfaces_list);
2674 }
2675}
2676
2677static void
2678add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2679{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002680 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002681
2682 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002683 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002684 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002685 /* We must make sure here that this popup was opened after
2686 * a mouse press, and not just by moving around with other
2687 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002688 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002689 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002690
Rob Bradforddfe31052013-06-26 19:49:11 +01002691 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002692 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002693 } else {
2694 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002695 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002696}
2697
2698static void
2699remove_popup_grab(struct shell_surface *shsurf)
2700{
2701 struct shell_seat *shseat = shsurf->popup.shseat;
2702
2703 wl_list_remove(&shsurf->popup.grab_link);
2704 wl_list_init(&shsurf->popup.grab_link);
2705 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002706 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002707 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002708 }
2709}
2710
2711static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002712shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002713{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002714 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002716
Jason Ekstranda7af7042013-10-12 22:38:11 -05002717 shsurf->surface->output = parent_view->output;
2718 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002719
Jason Ekstranda7af7042013-10-12 22:38:11 -05002720 weston_view_set_transform_parent(shsurf->view, parent_view);
2721 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2722 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002723
Kristian Høgsberge3148752013-05-06 23:19:49 -04002724 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002725 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002726 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002727 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002728 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002729 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002730}
2731
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002732static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002733 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002734 shell_surface_move,
2735 shell_surface_resize,
2736 shell_surface_set_toplevel,
2737 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002738 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002739 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002740 shell_surface_set_maximized,
2741 shell_surface_set_title,
2742 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002743};
2744
2745static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002746destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002747{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002748 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2749
Giulio Camuffo5085a752013-03-25 21:42:45 +01002750 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2751 remove_popup_grab(shsurf);
2752 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002753
Alex Wubd3354b2012-04-17 17:20:49 +08002754 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002755 shell_surface_is_top_fullscreen(shsurf))
2756 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002757
Jason Ekstranda7af7042013-10-12 22:38:11 -05002758 if (shsurf->fullscreen.black_view)
2759 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002760
Alex Wubd3354b2012-04-17 17:20:49 +08002761 /* As destroy_resource() use wl_list_for_each_safe(),
2762 * we can always remove the listener.
2763 */
2764 wl_list_remove(&shsurf->surface_destroy_listener.link);
2765 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002766 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002767 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002768
Jason Ekstranda7af7042013-10-12 22:38:11 -05002769 weston_view_destroy(shsurf->view);
2770
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002771 wl_list_remove(&shsurf->link);
2772 free(shsurf);
2773}
2774
2775static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002776shell_destroy_shell_surface(struct wl_resource *resource)
2777{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002778 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002779
2780 destroy_shell_surface(shsurf);
2781}
2782
2783static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002784shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002785{
2786 struct shell_surface *shsurf = container_of(listener,
2787 struct shell_surface,
2788 surface_destroy_listener);
2789
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002790 if (shsurf->resource)
2791 wl_resource_destroy(shsurf->resource);
2792 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002793 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002794}
2795
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002796static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002797shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002798
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002799static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002800get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002801{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002802 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002803 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002804 else
2805 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002806}
2807
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002808static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002809create_shell_surface(void *shell, struct weston_surface *surface,
2810 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002811{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002812 struct shell_surface *shsurf;
2813
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002814 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002815 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002816 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002817 }
2818
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002819 shsurf = calloc(1, sizeof *shsurf);
2820 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002821 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002822 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002823 }
2824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 shsurf->view = weston_view_create(surface);
2826 if (!shsurf->view) {
2827 weston_log("no memory to allocate shell surface\n");
2828 free(shsurf);
2829 return NULL;
2830 }
2831
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002832 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002833 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002834
Tiago Vignattibc052c92012-04-19 16:18:18 +03002835 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002836 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002837 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002838 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002839 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002840 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2841 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002842 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002843 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002844 wl_list_init(&shsurf->fullscreen.transform.link);
2845
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002846 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002847 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002848 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002849 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002850
2851 /* init link so its safe to always remove it in destroy_shell_surface */
2852 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002853 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002854
Pekka Paalanen460099f2012-01-20 16:48:25 +02002855 /* empty when not in use */
2856 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002857 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002858
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002859 wl_list_init(&shsurf->workspace_transform.link);
2860
Pekka Paalanen98262232011-12-01 10:42:22 +02002861 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002862 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002863
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002864 shsurf->client = client;
2865
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002866 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002867}
2868
Jason Ekstranda7af7042013-10-12 22:38:11 -05002869static struct weston_view *
2870get_primary_view(void *shell, struct shell_surface *shsurf)
2871{
2872 return shsurf->view;
2873}
2874
Tiago Vignattibc052c92012-04-19 16:18:18 +03002875static void
2876shell_get_shell_surface(struct wl_client *client,
2877 struct wl_resource *resource,
2878 uint32_t id,
2879 struct wl_resource *surface_resource)
2880{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002881 struct weston_surface *surface =
2882 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002883 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002884 struct shell_surface *shsurf;
2885
2886 if (get_shell_surface(surface)) {
2887 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002888 WL_DISPLAY_ERROR_INVALID_OBJECT,
2889 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002890 return;
2891 }
2892
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002893 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002894 if (!shsurf) {
2895 wl_resource_post_error(surface_resource,
2896 WL_DISPLAY_ERROR_INVALID_OBJECT,
2897 "surface->configure already set");
2898 return;
2899 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002900
Jason Ekstranda85118c2013-06-27 20:17:02 -05002901 shsurf->resource =
2902 wl_resource_create(client,
2903 &wl_shell_surface_interface, 1, id);
2904 wl_resource_set_implementation(shsurf->resource,
2905 &shell_surface_implementation,
2906 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002907}
2908
2909static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002910 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002911};
2912
Kristian Høgsberg07937562011-04-12 17:25:42 -04002913static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002914shell_fade(struct desktop_shell *shell, enum fade_type type);
2915
2916static int
2917screensaver_timeout(void *data)
2918{
2919 struct desktop_shell *shell = data;
2920
2921 shell_fade(shell, FADE_OUT);
2922
2923 return 1;
2924}
2925
2926static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002927handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002928{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002929 struct desktop_shell *shell =
2930 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002931
Pekka Paalanen18027e52011-12-02 16:31:49 +02002932 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002933
2934 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002935 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002936}
2937
2938static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002939launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002940{
2941 if (shell->screensaver.binding)
2942 return;
2943
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002944 if (!shell->screensaver.path) {
2945 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002946 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002947 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002948
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002949 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002950 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002951 return;
2952 }
2953
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002954 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002955 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002956 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002957 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002958}
2959
2960static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002961terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002962{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002963 if (shell->screensaver.process.pid == 0)
2964 return;
2965
2966 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002967}
2968
2969static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002970configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002971{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002972 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002973
Giulio Camuffo184df502013-02-21 11:29:21 +01002974 if (width == 0)
2975 return;
2976
Jason Ekstranda7af7042013-10-12 22:38:11 -05002977 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2978 if (v->output == ev->output && v != ev) {
2979 weston_view_unmap(v);
2980 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002981 }
2982 }
2983
Jason Ekstranda7af7042013-10-12 22:38:11 -05002984 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002985
Jason Ekstranda7af7042013-10-12 22:38:11 -05002986 if (wl_list_empty(&ev->layer_link)) {
2987 wl_list_insert(&layer->view_list, &ev->layer_link);
2988 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002989 }
2990}
2991
2992static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002993background_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 -04002994{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002995 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002996 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002997
Jason Ekstranda7af7042013-10-12 22:38:11 -05002998 view = container_of(es->views.next, struct weston_view, surface_link);
2999
3000 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003001}
3002
3003static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003004desktop_shell_set_background(struct wl_client *client,
3005 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003006 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003007 struct wl_resource *surface_resource)
3008{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003009 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003010 struct weston_surface *surface =
3011 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003012 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003013
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003014 if (surface->configure) {
3015 wl_resource_post_error(surface_resource,
3016 WL_DISPLAY_ERROR_INVALID_OBJECT,
3017 "surface role already assigned");
3018 return;
3019 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003020
Jason Ekstranda7af7042013-10-12 22:38:11 -05003021 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3022 weston_view_destroy(view);
3023 view = weston_view_create(surface);
3024
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003025 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003026 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003027 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003028 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003029 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003030 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003031 surface->output->width,
3032 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003033}
3034
3035static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003036panel_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 -04003037{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003038 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003039 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003040
Jason Ekstranda7af7042013-10-12 22:38:11 -05003041 view = container_of(es->views.next, struct weston_view, surface_link);
3042
3043 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003044}
3045
3046static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003047desktop_shell_set_panel(struct wl_client *client,
3048 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003049 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003050 struct wl_resource *surface_resource)
3051{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003052 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003053 struct weston_surface *surface =
3054 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003055 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003056
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003057 if (surface->configure) {
3058 wl_resource_post_error(surface_resource,
3059 WL_DISPLAY_ERROR_INVALID_OBJECT,
3060 "surface role already assigned");
3061 return;
3062 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003063
Jason Ekstranda7af7042013-10-12 22:38:11 -05003064 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3065 weston_view_destroy(view);
3066 view = weston_view_create(surface);
3067
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003068 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003069 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003070 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003071 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003072 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003073 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003074 surface->output->width,
3075 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003076}
3077
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003078static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003079lock_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 -04003080{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003081 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003082 struct weston_view *view;
3083
3084 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003085
Giulio Camuffo184df502013-02-21 11:29:21 +01003086 if (width == 0)
3087 return;
3088
Jason Ekstranda7af7042013-10-12 22:38:11 -05003089 surface->width = width;
3090 surface->height = height;
3091 view->geometry.width = width;
3092 view->geometry.height = height;
3093 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003094
3095 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003096 wl_list_insert(&shell->lock_layer.view_list,
3097 &view->layer_link);
3098 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003099 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003100 }
3101}
3102
3103static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003104handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003105{
Tiago Vignattibe143262012-04-16 17:31:41 +03003106 struct desktop_shell *shell =
3107 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003108
Martin Minarik6d118362012-06-07 18:01:59 +02003109 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003110 shell->lock_surface = NULL;
3111}
3112
3113static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003114desktop_shell_set_lock_surface(struct wl_client *client,
3115 struct wl_resource *resource,
3116 struct wl_resource *surface_resource)
3117{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003118 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003119 struct weston_surface *surface =
3120 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003121
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003122 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003123
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003124 if (!shell->locked)
3125 return;
3126
Pekka Paalanen98262232011-12-01 10:42:22 +02003127 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003128
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003129 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003130 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003131 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003132
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003133 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003134 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003135 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003136}
3137
3138static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003139resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003140{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003141 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003142
Pekka Paalanen77346a62011-11-30 16:26:35 +02003143 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003144
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003145 wl_list_remove(&shell->lock_layer.link);
3146 wl_list_insert(&shell->compositor->cursor_layer.link,
3147 &shell->fullscreen_layer.link);
3148 wl_list_insert(&shell->fullscreen_layer.link,
3149 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003150 if (shell->showing_input_panels) {
3151 wl_list_insert(&shell->panel_layer.link,
3152 &shell->input_panel_layer.link);
3153 wl_list_insert(&shell->input_panel_layer.link,
3154 &ws->layer.link);
3155 } else {
3156 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3157 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003158
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003159 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003160
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003161 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003162 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003163 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003164}
3165
3166static void
3167desktop_shell_unlock(struct wl_client *client,
3168 struct wl_resource *resource)
3169{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003170 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003171
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003172 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003173
3174 if (shell->locked)
3175 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003176}
3177
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003178static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003179desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003180 struct wl_resource *resource,
3181 struct wl_resource *surface_resource)
3182{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003183 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003184
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003185 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003186 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003187}
3188
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003189static void
3190desktop_shell_desktop_ready(struct wl_client *client,
3191 struct wl_resource *resource)
3192{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003193 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003194
3195 shell_fade_startup(shell);
3196}
3197
Kristian Høgsberg75840622011-09-06 13:48:16 -04003198static const struct desktop_shell_interface desktop_shell_implementation = {
3199 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003200 desktop_shell_set_panel,
3201 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003202 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003203 desktop_shell_set_grab_surface,
3204 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003205};
3206
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003207static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003208get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003209{
3210 struct shell_surface *shsurf;
3211
3212 shsurf = get_shell_surface(surface);
3213 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003214 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003215 return shsurf->type;
3216}
3217
Kristian Høgsberg75840622011-09-06 13:48:16 -04003218static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003219move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003220{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003221 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003222 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003223 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003224
Pekka Paalanen01388e22013-04-25 13:57:44 +03003225 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003226 if (surface == NULL)
3227 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003228
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003229 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003230 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3231 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003232 return;
3233
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003234 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003235}
3236
3237static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003238touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3239{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003240 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003241 struct weston_surface *surface;
3242 struct shell_surface *shsurf;
3243
3244 surface = weston_surface_get_main_surface(focus);
3245 if (surface == NULL)
3246 return;
3247
3248 shsurf = get_shell_surface(surface);
3249 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3250 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3251 return;
3252
3253 surface_touch_move(shsurf, (struct weston_seat *) seat);
3254}
3255
3256static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003257resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003258{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003259 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003260 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003261 uint32_t edges = 0;
3262 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003263 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003264
Pekka Paalanen01388e22013-04-25 13:57:44 +03003265 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003266 if (surface == NULL)
3267 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003268
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003269 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003270 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3271 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003272 return;
3273
Jason Ekstranda7af7042013-10-12 22:38:11 -05003274 weston_view_from_global(shsurf->view,
3275 wl_fixed_to_int(seat->pointer->grab_x),
3276 wl_fixed_to_int(seat->pointer->grab_y),
3277 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003278
Jason Ekstranda7af7042013-10-12 22:38:11 -05003279 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003280 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003281 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003282 edges |= 0;
3283 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003284 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003285
Jason Ekstranda7af7042013-10-12 22:38:11 -05003286 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003287 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003288 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003289 edges |= 0;
3290 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003291 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003292
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003293 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003294}
3295
3296static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003297surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003298 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003299{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003300 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003301 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003302 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003303 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003304
Pekka Paalanen01388e22013-04-25 13:57:44 +03003305 /* XXX: broken for windows containing sub-surfaces */
3306 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003307 if (surface == NULL)
3308 return;
3309
3310 shsurf = get_shell_surface(surface);
3311 if (!shsurf)
3312 return;
3313
Jason Ekstranda7af7042013-10-12 22:38:11 -05003314 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003315
Jason Ekstranda7af7042013-10-12 22:38:11 -05003316 if (shsurf->view->alpha > 1.0)
3317 shsurf->view->alpha = 1.0;
3318 if (shsurf->view->alpha < step)
3319 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003320
Jason Ekstranda7af7042013-10-12 22:38:11 -05003321 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003322 weston_surface_damage(surface);
3323}
3324
3325static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003326do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003327 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003328{
Daniel Stone37816df2012-05-16 18:45:18 +01003329 struct weston_seat *ws = (struct weston_seat *) seat;
3330 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003331 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003332 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003333
3334 wl_list_for_each(output, &compositor->output_list, link) {
3335 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003336 wl_fixed_to_double(seat->pointer->x),
3337 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003338 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003339 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003340 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003341 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003342 increment = -output->zoom.increment;
3343 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003344 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003345 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003346 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003347 else
3348 increment = 0;
3349
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003350 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003351
Scott Moreaue6603982012-06-11 13:07:51 -06003352 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003353 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003354 else if (output->zoom.level > output->zoom.max_level)
3355 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003356 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003357 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003358 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003359
Scott Moreaue6603982012-06-11 13:07:51 -06003360 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003361
Jason Ekstranda7af7042013-10-12 22:38:11 -05003362 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003363 }
3364 }
3365}
3366
Scott Moreauccbf29d2012-02-22 14:21:41 -07003367static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003368zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003369 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003370{
3371 do_zoom(seat, time, 0, axis, value);
3372}
3373
3374static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003375zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003376 void *data)
3377{
3378 do_zoom(seat, time, key, 0, 0);
3379}
3380
3381static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003382terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003383 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003384{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003385 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003386
Daniel Stone325fc2d2012-05-30 16:31:58 +01003387 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003388}
3389
3390static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003391lower_fullscreen_layer(struct desktop_shell *shell);
3392
3393struct alt_tab {
3394 struct desktop_shell *shell;
3395 struct weston_keyboard_grab grab;
3396
3397 struct wl_list *current;
3398
3399 struct wl_list preview_list;
3400};
3401
3402struct alt_tab_preview {
3403 struct alt_tab *alt_tab;
3404
3405 struct weston_view *view;
3406 struct weston_transform transform;
3407
3408 struct wl_listener listener;
3409
3410 struct wl_list link;
3411};
3412
3413static void
3414alt_tab_next(struct alt_tab *alt_tab)
3415{
3416 alt_tab->current = alt_tab->current->next;
3417
3418 /* Make sure we're not pointing to the list header e.g. after
3419 * cycling through the whole list. */
3420 if (alt_tab->current->next == alt_tab->preview_list.next)
3421 alt_tab->current = alt_tab->current->next;
3422}
3423
3424static void
3425alt_tab_destroy(struct alt_tab *alt_tab)
3426{
3427 struct alt_tab_preview *preview, *next;
3428 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3429
3430 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3431 preview = wl_container_of(alt_tab->current, preview, link);
3432
3433 activate(alt_tab->shell, preview->view->surface,
3434 (struct weston_seat *) keyboard->seat);
3435 }
3436
3437 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3438 wl_list_remove(&preview->link);
3439 wl_list_remove(&preview->listener.link);
3440 weston_view_destroy(preview->view);
3441 free(preview);
3442 }
3443
3444 weston_keyboard_end_grab(keyboard);
3445 if (keyboard->input_method_resource)
3446 keyboard->grab = &keyboard->input_method_grab;
3447
3448 free(alt_tab);
3449}
3450
3451static void
3452alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3453{
3454 struct alt_tab_preview *preview =
3455 container_of(listener, struct alt_tab_preview, listener);
3456 struct alt_tab *alt_tab = preview->alt_tab;
3457 int advance = 0;
3458
3459 /* If the preview that we're removing is the currently selected one,
3460 * we want to move to the next one. So we move to ->prev and then
3461 * call _next() after removing the preview. */
3462 if (alt_tab->current == &preview->link) {
3463 alt_tab->current = alt_tab->current->prev;
3464 advance = 1;
3465 }
3466
3467 wl_list_remove(&preview->listener.link);
3468 wl_list_remove(&preview->link);
3469
3470 free(preview);
3471
3472 if (advance)
3473 alt_tab_next(alt_tab);
3474
3475 /* If the last preview goes away, stop the alt-tab */
3476 if (wl_list_empty(alt_tab->current))
3477 alt_tab_destroy(alt_tab);
3478}
3479
3480static void
3481alt_tab_key(struct weston_keyboard_grab *grab,
3482 uint32_t time, uint32_t key, uint32_t state_w)
3483{
3484 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3485 enum wl_keyboard_key_state state = state_w;
3486
3487 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3488 alt_tab_next(alt_tab);
3489}
3490
3491static void
3492alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3493 uint32_t mods_depressed, uint32_t mods_latched,
3494 uint32_t mods_locked, uint32_t group)
3495{
3496 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3497 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3498
3499 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3500 alt_tab_destroy(alt_tab);
3501}
3502
3503static void
3504alt_tab_cancel(struct weston_keyboard_grab *grab)
3505{
3506 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3507
3508 alt_tab_destroy(alt_tab);
3509}
3510
3511static const struct weston_keyboard_grab_interface alt_tab_grab = {
3512 alt_tab_key,
3513 alt_tab_modifier,
3514 alt_tab_cancel,
3515};
3516
3517static int
3518view_for_alt_tab(struct weston_view *view)
3519{
3520 if (!get_shell_surface(view->surface))
3521 return 0;
3522
3523 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3524 return 0;
3525
3526 if (view != get_default_view(view->surface))
3527 return 0;
3528
3529 return 1;
3530}
3531
3532static void
3533alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3534 void *data)
3535{
3536 struct alt_tab *alt_tab;
3537 struct desktop_shell *shell = data;
3538 struct weston_output *output = get_default_output(shell->compositor);
3539 struct workspace *ws;
3540 struct weston_view *view;
3541 int num_surfaces = 0;
3542 int x, y, side, margin;
3543
3544 wl_list_for_each(view, &shell->compositor->view_list, link) {
3545
3546 if (view_for_alt_tab(view))
3547 num_surfaces++;
3548 }
3549
3550 if (!num_surfaces)
3551 return;
3552
3553 alt_tab = malloc(sizeof *alt_tab);
3554 if (!alt_tab)
3555 return;
3556
3557 alt_tab->shell = shell;
3558 wl_list_init(&alt_tab->preview_list);
3559 alt_tab->current = &alt_tab->preview_list;
3560
3561 restore_all_output_modes(shell->compositor);
3562 lower_fullscreen_layer(alt_tab->shell);
3563
3564 alt_tab->grab.interface = &alt_tab_grab;
3565 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3566 weston_keyboard_set_focus(seat->keyboard, NULL);
3567
3568 ws = get_current_workspace(shell);
3569
3570 /* FIXME: add some visual candy e.g. prelight the selected view
3571 * and/or add a black surrounding background à la gnome-shell */
3572
3573 /* Determine the size for each preview */
3574 side = output->width / num_surfaces;
3575 if (side > 200)
3576 side = 200;
3577 margin = side / 4;
3578 side -= margin;
3579
3580 x = margin;
3581 y = (output->height - side) / 2;
3582
3583 /* Create a view for each surface */
3584 wl_list_for_each(view, &shell->compositor->view_list, link) {
3585 struct alt_tab_preview *preview;
3586 struct weston_view *v;
3587 float scale;
3588
3589 if (!view_for_alt_tab(view))
3590 continue;
3591
3592 preview = malloc(sizeof *preview);
3593 if (!preview) {
3594 alt_tab_destroy(alt_tab);
3595 return;
3596 }
3597
3598 preview->alt_tab = alt_tab;
3599
3600 preview->view = v = weston_view_create(view->surface);
3601 v->output = view->output;
3602 weston_view_restack(v, &ws->layer.view_list);
3603 weston_view_configure(v, x, y, view->geometry.width, view->geometry.height);
3604
3605 preview->listener.notify = alt_tab_handle_surface_destroy;
3606 wl_signal_add(&v->destroy_signal, &preview->listener);
3607
3608 if (view->geometry.width > view->geometry.height)
3609 scale = side / (float) view->geometry.width;
3610 else
3611 scale = side / (float) view->geometry.height;
3612
3613 wl_list_insert(&v->geometry.transformation_list,
3614 &preview->transform.link);
3615 weston_matrix_init(&preview->transform.matrix);
3616 weston_matrix_scale(&preview->transform.matrix,
3617 scale, scale, 1.0f);
3618
3619 weston_view_geometry_dirty(v);
3620 weston_compositor_schedule_repaint(v->surface->compositor);
3621
3622 /* Insert at the end of the list */
3623 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3624
3625 x += side + margin;
3626 }
3627
3628 /* Start at the second preview so a simple <alt>tab changes window.
3629 * We set `current' to the first preview and not the second because
3630 * we're going to receive a key press callback for the initial
3631 * <alt>tab which will make `current' point to the second element. */
3632 alt_tab_next(alt_tab);
3633}
3634
3635static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003636rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3637 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003638{
3639 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003640 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003641 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003642 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003643 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003644
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003645 weston_pointer_move(pointer, x, y);
3646
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003647 if (!shsurf)
3648 return;
3649
Jason Ekstranda7af7042013-10-12 22:38:11 -05003650 cx = 0.5f * shsurf->view->geometry.width;
3651 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003652
Daniel Stone37816df2012-05-16 18:45:18 +01003653 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3654 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003655 r = sqrtf(dx * dx + dy * dy);
3656
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003657 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003658 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003659
3660 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003661 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003662 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003663
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003664 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003665 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003666
3667 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003668 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003669 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003670 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003671 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003672
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003673 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003674 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003675 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003676 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003677 wl_list_init(&shsurf->rotation.transform.link);
3678 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003679 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003680 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003681
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003682 /* We need to adjust the position of the surface
3683 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003684 cposx = shsurf->view->geometry.x + cx;
3685 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003686 dposx = rotate->center.x - cposx;
3687 dposy = rotate->center.y - cposy;
3688 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003689 weston_view_set_position(shsurf->view,
3690 shsurf->view->geometry.x + dposx,
3691 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003692 }
3693
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003694 /* Repaint implies weston_surface_update_transform(), which
3695 * lazily applies the damage due to rotation update.
3696 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003697 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003698}
3699
3700static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003701rotate_grab_button(struct weston_pointer_grab *grab,
3702 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003703{
3704 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003705 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003706 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003707 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003708 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003709
Daniel Stone4dbadb12012-05-30 16:31:51 +01003710 if (pointer->button_count == 0 &&
3711 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003712 if (shsurf)
3713 weston_matrix_multiply(&shsurf->rotation.rotation,
3714 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003715 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003716 free(rotate);
3717 }
3718}
3719
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003720static void
3721rotate_grab_cancel(struct weston_pointer_grab *grab)
3722{
3723 struct rotate_grab *rotate =
3724 container_of(grab, struct rotate_grab, base.grab);
3725
3726 shell_grab_end(&rotate->base);
3727 free(rotate);
3728}
3729
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003730static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003731 noop_grab_focus,
3732 rotate_grab_motion,
3733 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003734 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003735};
3736
3737static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003738surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003739{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003740 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003741 float dx, dy;
3742 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003743
Pekka Paalanen460099f2012-01-20 16:48:25 +02003744 rotate = malloc(sizeof *rotate);
3745 if (!rotate)
3746 return;
3747
Jason Ekstranda7af7042013-10-12 22:38:11 -05003748 weston_view_to_global_float(surface->view,
3749 surface->view->geometry.width * 0.5f,
3750 surface->view->geometry.height * 0.5f,
3751 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003752
Daniel Stone37816df2012-05-16 18:45:18 +01003753 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3754 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003755 r = sqrtf(dx * dx + dy * dy);
3756 if (r > 20.0f) {
3757 struct weston_matrix inverse;
3758
3759 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003760 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003761 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003762
3763 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003764 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003765 } else {
3766 weston_matrix_init(&surface->rotation.rotation);
3767 weston_matrix_init(&rotate->rotation);
3768 }
3769
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003770 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3771 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003772}
3773
3774static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003775rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003776 void *data)
3777{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003778 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003779 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003780 struct shell_surface *surface;
3781
Pekka Paalanen01388e22013-04-25 13:57:44 +03003782 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003783 if (base_surface == NULL)
3784 return;
3785
3786 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003787 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3788 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003789 return;
3790
3791 surface_rotate(surface, seat);
3792}
3793
3794static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003795lower_fullscreen_layer(struct desktop_shell *shell)
3796{
3797 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003798 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003799
3800 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003801 wl_list_for_each_reverse_safe(view, prev,
3802 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003803 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003804 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003805}
3806
3807static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003808activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003809 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003810{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003811 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003812 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003813 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003814 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003815 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003816
Pekka Paalanen01388e22013-04-25 13:57:44 +03003817 main_surface = weston_surface_get_main_surface(es);
3818
Daniel Stone37816df2012-05-16 18:45:18 +01003819 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003820
Jonas Ådahl8538b222012-08-29 22:13:03 +02003821 state = ensure_focus_state(shell, seat);
3822 if (state == NULL)
3823 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003824
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003825 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003826 state->keyboard_focus = es;
3827 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003828 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003829
Pekka Paalanen01388e22013-04-25 13:57:44 +03003830 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003831 case SHELL_SURFACE_FULLSCREEN:
3832 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003833 shell_stack_fullscreen(get_shell_surface(main_surface));
3834 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003835 return;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003836 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003837 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003838 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003839 main_view = get_default_view(main_surface);
3840 if (main_view)
3841 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003842 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003843 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003844
3845 if (shell->focus_animation_type != ANIMATION_NONE)
3846 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003847}
3848
Alex Wu21858432012-04-01 20:13:08 +08003849/* no-op func for checking black surface */
3850static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003851black_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 +08003852{
3853}
3854
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003855static bool
Alex Wu21858432012-04-01 20:13:08 +08003856is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3857{
3858 if (es->configure == black_surface_configure) {
3859 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003860 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003861 return true;
3862 }
3863 return false;
3864}
3865
Kristian Høgsberg75840622011-09-06 13:48:16 -04003866static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003867activate_binding(struct weston_seat *seat,
3868 struct desktop_shell *shell,
3869 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003870{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003871 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003872
Alex Wu9c35e6b2012-03-05 14:13:13 +08003873 if (!focus)
3874 return;
3875
Pekka Paalanen01388e22013-04-25 13:57:44 +03003876 if (is_black_surface(focus, &main_surface))
3877 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003878
Pekka Paalanen01388e22013-04-25 13:57:44 +03003879 main_surface = weston_surface_get_main_surface(focus);
3880 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003881 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003882
Neil Robertsa28c6932013-10-03 16:43:04 +01003883 activate(shell, focus, seat);
3884}
3885
3886static void
3887click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3888 void *data)
3889{
3890 if (seat->pointer->grab != &seat->pointer->default_grab)
3891 return;
3892
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003893 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003894}
3895
3896static void
3897touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3898{
3899 if (seat->touch->grab != &seat->touch->default_grab)
3900 return;
3901
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003902 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003903}
3904
3905static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003906lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003907{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003908 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003909
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003910 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003911 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003912 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003913 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003914
3915 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003916
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003917 /* Hide all surfaces by removing the fullscreen, panel and
3918 * toplevel layers. This way nothing else can show or receive
3919 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003920
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003921 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003922 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003923 if (shell->showing_input_panels)
3924 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003925 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003926 wl_list_insert(&shell->compositor->cursor_layer.link,
3927 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003928
Pekka Paalanen77346a62011-11-30 16:26:35 +02003929 launch_screensaver(shell);
3930
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003931 /* TODO: disable bindings that should not work while locked. */
3932
3933 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003934}
3935
3936static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003937unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003938{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003939 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003940 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003941 return;
3942 }
3943
3944 /* If desktop-shell client has gone away, unlock immediately. */
3945 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003946 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003947 return;
3948 }
3949
3950 if (shell->prepare_event_sent)
3951 return;
3952
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003953 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003954 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003955}
3956
3957static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003958shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003959{
3960 struct desktop_shell *shell = data;
3961
3962 shell->fade.animation = NULL;
3963
3964 switch (shell->fade.type) {
3965 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003966 weston_surface_destroy(shell->fade.view->surface);
3967 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003968 break;
3969 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003970 lock(shell);
3971 break;
3972 }
3973}
3974
Jason Ekstranda7af7042013-10-12 22:38:11 -05003975static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003976shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003977{
3978 struct weston_compositor *compositor = shell->compositor;
3979 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003980 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003981
3982 surface = weston_surface_create(compositor);
3983 if (!surface)
3984 return NULL;
3985
Jason Ekstranda7af7042013-10-12 22:38:11 -05003986 view = weston_view_create(surface);
3987 if (!view) {
3988 weston_surface_destroy(surface);
3989 return NULL;
3990 }
3991
3992 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003993 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003994 wl_list_insert(&compositor->fade_layer.view_list,
3995 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003996 pixman_region32_init(&surface->input);
3997
Jason Ekstranda7af7042013-10-12 22:38:11 -05003998 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003999}
4000
4001static void
4002shell_fade(struct desktop_shell *shell, enum fade_type type)
4003{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004004 float tint;
4005
4006 switch (type) {
4007 case FADE_IN:
4008 tint = 0.0;
4009 break;
4010 case FADE_OUT:
4011 tint = 1.0;
4012 break;
4013 default:
4014 weston_log("shell: invalid fade type\n");
4015 return;
4016 }
4017
4018 shell->fade.type = type;
4019
Jason Ekstranda7af7042013-10-12 22:38:11 -05004020 if (shell->fade.view == NULL) {
4021 shell->fade.view = shell_fade_create_surface(shell);
4022 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004023 return;
4024
Jason Ekstranda7af7042013-10-12 22:38:11 -05004025 shell->fade.view->alpha = 1.0 - tint;
4026 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004027 }
4028
4029 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004030 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004031 else
4032 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004033 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004034 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004035 shell_fade_done, shell);
4036}
4037
4038static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004039do_shell_fade_startup(void *data)
4040{
4041 struct desktop_shell *shell = data;
4042
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004043 if (shell->startup_animation_type == ANIMATION_FADE)
4044 shell_fade(shell, FADE_IN);
4045 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004046 weston_surface_destroy(shell->fade.view->surface);
4047 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004048 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004049}
4050
4051static void
4052shell_fade_startup(struct desktop_shell *shell)
4053{
4054 struct wl_event_loop *loop;
4055
4056 if (!shell->fade.startup_timer)
4057 return;
4058
4059 wl_event_source_remove(shell->fade.startup_timer);
4060 shell->fade.startup_timer = NULL;
4061
4062 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4063 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4064}
4065
4066static int
4067fade_startup_timeout(void *data)
4068{
4069 struct desktop_shell *shell = data;
4070
4071 shell_fade_startup(shell);
4072 return 0;
4073}
4074
4075static void
4076shell_fade_init(struct desktop_shell *shell)
4077{
4078 /* Make compositor output all black, and wait for the desktop-shell
4079 * client to signal it is ready, then fade in. The timer triggers a
4080 * fade-in, in case the desktop-shell client takes too long.
4081 */
4082
4083 struct wl_event_loop *loop;
4084
Jason Ekstranda7af7042013-10-12 22:38:11 -05004085 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004086 weston_log("%s: warning: fade surface already exists\n",
4087 __func__);
4088 return;
4089 }
4090
Jason Ekstranda7af7042013-10-12 22:38:11 -05004091 shell->fade.view = shell_fade_create_surface(shell);
4092 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004093 return;
4094
Jason Ekstranda7af7042013-10-12 22:38:11 -05004095 weston_view_update_transform(shell->fade.view);
4096 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004097
4098 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4099 shell->fade.startup_timer =
4100 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4101 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4102}
4103
4104static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004105idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004106{
4107 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004108 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004109
4110 shell_fade(shell, FADE_OUT);
4111 /* lock() is called from shell_fade_done() */
4112}
4113
4114static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004115wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004116{
4117 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004118 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004119
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004120 unlock(shell);
4121}
4122
4123static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004124show_input_panels(struct wl_listener *listener, void *data)
4125{
4126 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004127 container_of(listener, struct desktop_shell,
4128 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004129 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004130
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004131 shell->text_input.surface = (struct weston_surface*)data;
4132
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004133 if (shell->showing_input_panels)
4134 return;
4135
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004136 shell->showing_input_panels = true;
4137
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004138 if (!shell->locked)
4139 wl_list_insert(&shell->panel_layer.link,
4140 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004141
Jason Ekstranda7af7042013-10-12 22:38:11 -05004142 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004143 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004144 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004145 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004146 wl_list_insert(&shell->input_panel_layer.view_list,
4147 &ipsurf->view->layer_link);
4148 weston_view_geometry_dirty(ipsurf->view);
4149 weston_view_update_transform(ipsurf->view);
4150 weston_surface_damage(ipsurf->surface);
4151 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
4152 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004153 }
4154}
4155
4156static void
4157hide_input_panels(struct wl_listener *listener, void *data)
4158{
4159 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004160 container_of(listener, struct desktop_shell,
4161 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004162 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004163
Jan Arne Petersen61381972013-01-31 15:52:21 +01004164 if (!shell->showing_input_panels)
4165 return;
4166
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004167 shell->showing_input_panels = false;
4168
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004169 if (!shell->locked)
4170 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004171
Jason Ekstranda7af7042013-10-12 22:38:11 -05004172 wl_list_for_each_safe(view, next,
4173 &shell->input_panel_layer.view_list, layer_link)
4174 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004175}
4176
4177static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004178update_input_panels(struct wl_listener *listener, void *data)
4179{
4180 struct desktop_shell *shell =
4181 container_of(listener, struct desktop_shell,
4182 update_input_panel_listener);
4183
4184 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4185}
4186
4187static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004188center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004189{
Giulio Camuffob8366642013-04-25 13:57:46 +03004190 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004191 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004192
Jason Ekstranda7af7042013-10-12 22:38:11 -05004193 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004194
4195 x = output->x + (output->width - width) / 2 - surf_x / 2;
4196 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004197
Jason Ekstranda7af7042013-10-12 22:38:11 -05004198 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004199}
4200
4201static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004202weston_view_set_initial_position(struct weston_view *view,
4203 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004204{
4205 struct weston_compositor *compositor = shell->compositor;
4206 int ix = 0, iy = 0;
4207 int range_x, range_y;
4208 int dx, dy, x, y, panel_height;
4209 struct weston_output *output, *target_output = NULL;
4210 struct weston_seat *seat;
4211
4212 /* As a heuristic place the new window on the same output as the
4213 * pointer. Falling back to the output containing 0, 0.
4214 *
4215 * TODO: Do something clever for touch too?
4216 */
4217 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004218 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004219 ix = wl_fixed_to_int(seat->pointer->x);
4220 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004221 break;
4222 }
4223 }
4224
4225 wl_list_for_each(output, &compositor->output_list, link) {
4226 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4227 target_output = output;
4228 break;
4229 }
4230 }
4231
4232 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004233 weston_view_set_position(view, 10 + random() % 400,
4234 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004235 return;
4236 }
4237
4238 /* Valid range within output where the surface will still be onscreen.
4239 * If this is negative it means that the surface is bigger than
4240 * output.
4241 */
4242 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004243 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004244 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004246
Rob Bradford4cb88c72012-08-13 15:18:44 +01004247 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004248 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004249 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004250 dx = 0;
4251
4252 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004253 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004254 else
4255 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004256
4257 x = target_output->x + dx;
4258 y = target_output->y + dy;
4259
Jason Ekstranda7af7042013-10-12 22:38:11 -05004260 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004261}
4262
4263static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004264map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004265 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004267 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01004269 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004270 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08004271 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004272 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004273
Jason Ekstranda7af7042013-10-12 22:38:11 -05004274 shsurf->view->geometry.width = width;
4275 shsurf->view->geometry.height = height;
4276 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004277
4278 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004279 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004280 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004281 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004282 break;
Alex Wu4539b082012-03-01 12:57:46 +08004283 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004284 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004285 shell_map_fullscreen(shsurf);
4286 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004287 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004288 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004289 panel_height = get_output_panel_height(shell, shsurf->output);
4290 surface_subsurfaces_boundingbox(shsurf->surface,
4291 &surf_x, &surf_y, NULL, NULL);
4292 weston_view_set_position(shsurf->view,
4293 shsurf->output->x - surf_x,
4294 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004295 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004296 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004297 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004298 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004299 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004300 weston_view_set_position(shsurf->view,
4301 shsurf->view->geometry.x + sx,
4302 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004303 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004304 default:
4305 ;
4306 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004307
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02004308 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004309 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004310 case SHELL_SURFACE_POPUP:
4311 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004312 /* TODO: Handle a parent with multiple views */
4313 parent = get_default_view(shsurf->parent);
4314 if (parent) {
4315 wl_list_remove(&shsurf->view->layer_link);
4316 wl_list_insert(parent->layer_link.prev,
4317 &shsurf->view->layer_link);
4318 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004319 break;
Alex Wu4539b082012-03-01 12:57:46 +08004320 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02004321 case SHELL_SURFACE_NONE:
4322 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004323 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004324 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004325 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 wl_list_remove(&shsurf->view->layer_link);
4327 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004328 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004329 }
4330
Jason Ekstranda7af7042013-10-12 22:38:11 -05004331 if (shsurf->type != SHELL_SURFACE_NONE) {
4332 weston_view_update_transform(shsurf->view);
4333 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4334 shsurf->surface->output = shsurf->output;
4335 shsurf->view->output = shsurf->output;
4336 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004337 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004338
Jason Ekstranda7af7042013-10-12 22:38:11 -05004339 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004340 /* XXX: xwayland's using the same fields for transient type */
4341 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004342 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004343 if (shsurf->transient.flags ==
4344 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4345 break;
4346 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004347 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004348 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004349 if (!shell->locked) {
4350 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004351 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004352 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004353 break;
4354 default:
4355 break;
4356 }
4357
Jason Ekstranda7af7042013-10-12 22:38:11 -05004358 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004359 {
4360 switch (shell->win_animation_type) {
4361 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004362 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004363 break;
4364 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004365 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004366 break;
4367 default:
4368 break;
4369 }
4370 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004371}
4372
4373static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004374configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004375 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004376{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004377 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4378 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004379 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004380 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004381
Pekka Paalanen77346a62011-11-30 16:26:35 +02004382 shsurf = get_shell_surface(surface);
4383 if (shsurf)
4384 surface_type = shsurf->type;
4385
Jason Ekstranda7af7042013-10-12 22:38:11 -05004386 /* TODO:
4387 * This should probably be changed to be more shell_surface
4388 * dependent
4389 */
4390 wl_list_for_each(view, &surface->views, surface_link)
4391 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004392
4393 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004394 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004395 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004396 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004397 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004398 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004399 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004400 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4401 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4403 shsurf->view->geometry.y = shsurf->output->y +
4404 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004405 break;
Alex Wu4539b082012-03-01 12:57:46 +08004406 case SHELL_SURFACE_TOPLEVEL:
4407 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004408 default:
4409 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004410 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004411
Alex Wu4539b082012-03-01 12:57:46 +08004412 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004413 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004414 wl_list_for_each(view, &surface->views, surface_link)
4415 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004416
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004417 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004418 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004419 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004420}
4421
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004422static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004423shell_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 +03004424{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004425 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004426 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004427
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004428 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004429
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004430 if (!weston_surface_is_mapped(es) &&
4431 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004432 remove_popup_grab(shsurf);
4433 }
4434
Giulio Camuffo184df502013-02-21 11:29:21 +01004435 if (width == 0)
4436 return;
4437
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004438 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004439 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004440 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004441 type_changed = 1;
4442 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004443
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004444 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004445 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004446 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004447 shsurf->view->geometry.width != width ||
4448 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004449 float from_x, from_y;
4450 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004451
Jason Ekstranda7af7042013-10-12 22:38:11 -05004452 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4453 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004454 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004455 shsurf->view->geometry.x + to_x - from_x,
4456 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004457 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004458 }
4459}
4460
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004461static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004462
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004463static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004464desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004465{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004466 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004467 struct desktop_shell *shell =
4468 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004469
4470 shell->child.process.pid = 0;
4471 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004472
4473 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4474 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004475 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004476 shell->child.deathstamp = time;
4477 shell->child.deathcount = 0;
4478 }
4479
4480 shell->child.deathcount++;
4481 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004482 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004483 return;
4484 }
4485
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004486 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004487 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004488 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004489}
4490
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004491static void
4492launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004493{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004494 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004495
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004496 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004497 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004498 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004499 desktop_shell_sigchld);
4500
4501 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004502 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004503}
4504
4505static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004506bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4507{
Tiago Vignattibe143262012-04-16 17:31:41 +03004508 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004509 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004510
Jason Ekstranda85118c2013-06-27 20:17:02 -05004511 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4512 if (resource)
4513 wl_resource_set_implementation(resource, &shell_implementation,
4514 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004515}
4516
Kristian Høgsberg75840622011-09-06 13:48:16 -04004517static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004518unbind_desktop_shell(struct wl_resource *resource)
4519{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004520 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004521
4522 if (shell->locked)
4523 resume_desktop(shell);
4524
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004525 shell->child.desktop_shell = NULL;
4526 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004527}
4528
4529static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004530bind_desktop_shell(struct wl_client *client,
4531 void *data, uint32_t version, uint32_t id)
4532{
Tiago Vignattibe143262012-04-16 17:31:41 +03004533 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004534 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004535
Jason Ekstranda85118c2013-06-27 20:17:02 -05004536 resource = wl_resource_create(client, &desktop_shell_interface,
4537 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004538
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004539 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004540 wl_resource_set_implementation(resource,
4541 &desktop_shell_implementation,
4542 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004543 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004544
4545 if (version < 2)
4546 shell_fade_startup(shell);
4547
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004548 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004549 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004550
4551 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4552 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004553 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004554}
4555
Pekka Paalanen6e168112011-11-24 11:34:05 +02004556static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004557screensaver_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 -04004558{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004559 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004560 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004561
Giulio Camuffo184df502013-02-21 11:29:21 +01004562 if (width == 0)
4563 return;
4564
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004565 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004566 if (!shell->locked)
4567 return;
4568
Jason Ekstranda7af7042013-10-12 22:38:11 -05004569 view = container_of(surface->views.next, struct weston_view, surface_link);
4570 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004571
Jason Ekstranda7af7042013-10-12 22:38:11 -05004572 if (wl_list_empty(&view->layer_link)) {
4573 wl_list_insert(shell->lock_layer.view_list.prev,
4574 &view->layer_link);
4575 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004576 wl_event_source_timer_update(shell->screensaver.timer,
4577 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004578 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004579 }
4580}
4581
4582static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004583screensaver_set_surface(struct wl_client *client,
4584 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004585 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004586 struct wl_resource *output_resource)
4587{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004588 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004589 struct weston_surface *surface =
4590 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004591 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004592 struct weston_view *view, *next;
4593
4594 /* Make sure we only have one view */
4595 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4596 weston_view_destroy(view);
4597 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004598
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004599 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004600 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004601 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004602}
4603
4604static const struct screensaver_interface screensaver_implementation = {
4605 screensaver_set_surface
4606};
4607
4608static void
4609unbind_screensaver(struct wl_resource *resource)
4610{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004611 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004612
Pekka Paalanen77346a62011-11-30 16:26:35 +02004613 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004614}
4615
4616static void
4617bind_screensaver(struct wl_client *client,
4618 void *data, uint32_t version, uint32_t id)
4619{
Tiago Vignattibe143262012-04-16 17:31:41 +03004620 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004621 struct wl_resource *resource;
4622
Jason Ekstranda85118c2013-06-27 20:17:02 -05004623 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004624
Pekka Paalanen77346a62011-11-30 16:26:35 +02004625 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004626 wl_resource_set_implementation(resource,
4627 &screensaver_implementation,
4628 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004629 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004630 return;
4631 }
4632
4633 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4634 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004635 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004636}
4637
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004638static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004639input_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 -04004640{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004641 struct input_panel_surface *ip_surface = surface->configure_private;
4642 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004643 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004644 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004645
Giulio Camuffo184df502013-02-21 11:29:21 +01004646 if (width == 0)
4647 return;
4648
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004649 if (!weston_surface_is_mapped(surface)) {
4650 if (!shell->showing_input_panels)
4651 return;
4652
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004653 show_surface = 1;
4654 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004655
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004656 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004657
4658 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004659 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4660 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004661 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004662 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4663 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004664 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004665
Jason Ekstranda7af7042013-10-12 22:38:11 -05004666 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004667
4668 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004669 wl_list_insert(&shell->input_panel_layer.view_list,
4670 &ip_surface->view->layer_link);
4671 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004672 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004673 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004674 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004675}
4676
4677static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004678destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004679{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004680 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4681
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004682 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004683 wl_list_remove(&input_panel_surface->link);
4684
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004685 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004686 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004687
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004688 free(input_panel_surface);
4689}
4690
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004691static struct input_panel_surface *
4692get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004693{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004694 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004695 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004696 } else {
4697 return NULL;
4698 }
4699}
4700
4701static void
4702input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4703{
4704 struct input_panel_surface *ipsurface = container_of(listener,
4705 struct input_panel_surface,
4706 surface_destroy_listener);
4707
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004708 if (ipsurface->resource) {
4709 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004710 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004711 destroy_input_panel_surface(ipsurface);
4712 }
4713}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004714
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004715static struct input_panel_surface *
4716create_input_panel_surface(struct desktop_shell *shell,
4717 struct weston_surface *surface)
4718{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004719 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004720
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004721 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4722 if (!input_panel_surface)
4723 return NULL;
4724
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004725 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004726 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004727
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004728 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004729
4730 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004731 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004732
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004733 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004734 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004735 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004736 &input_panel_surface->surface_destroy_listener);
4737
4738 wl_list_init(&input_panel_surface->link);
4739
4740 return input_panel_surface;
4741}
4742
4743static void
4744input_panel_surface_set_toplevel(struct wl_client *client,
4745 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004746 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004747 uint32_t position)
4748{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004749 struct input_panel_surface *input_panel_surface =
4750 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004751 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004752
4753 wl_list_insert(&shell->input_panel.surfaces,
4754 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004755
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004756 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004757 input_panel_surface->panel = 0;
4758}
4759
4760static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004761input_panel_surface_set_overlay_panel(struct wl_client *client,
4762 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004763{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004764 struct input_panel_surface *input_panel_surface =
4765 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004766 struct desktop_shell *shell = input_panel_surface->shell;
4767
4768 wl_list_insert(&shell->input_panel.surfaces,
4769 &input_panel_surface->link);
4770
4771 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004772}
4773
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004774static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004775 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004776 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004777};
4778
4779static void
4780destroy_input_panel_surface_resource(struct wl_resource *resource)
4781{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004782 struct input_panel_surface *ipsurf =
4783 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004784
4785 destroy_input_panel_surface(ipsurf);
4786}
4787
4788static void
4789input_panel_get_input_panel_surface(struct wl_client *client,
4790 struct wl_resource *resource,
4791 uint32_t id,
4792 struct wl_resource *surface_resource)
4793{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004794 struct weston_surface *surface =
4795 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004796 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004797 struct input_panel_surface *ipsurf;
4798
4799 if (get_input_panel_surface(surface)) {
4800 wl_resource_post_error(surface_resource,
4801 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004802 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004803 return;
4804 }
4805
4806 ipsurf = create_input_panel_surface(shell, surface);
4807 if (!ipsurf) {
4808 wl_resource_post_error(surface_resource,
4809 WL_DISPLAY_ERROR_INVALID_OBJECT,
4810 "surface->configure already set");
4811 return;
4812 }
4813
Jason Ekstranda85118c2013-06-27 20:17:02 -05004814 ipsurf->resource =
4815 wl_resource_create(client,
4816 &wl_input_panel_surface_interface, 1, id);
4817 wl_resource_set_implementation(ipsurf->resource,
4818 &input_panel_surface_implementation,
4819 ipsurf,
4820 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004821}
4822
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004823static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004824 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004825};
4826
4827static void
4828unbind_input_panel(struct wl_resource *resource)
4829{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004830 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004831
4832 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004833}
4834
4835static void
4836bind_input_panel(struct wl_client *client,
4837 void *data, uint32_t version, uint32_t id)
4838{
4839 struct desktop_shell *shell = data;
4840 struct wl_resource *resource;
4841
Jason Ekstranda85118c2013-06-27 20:17:02 -05004842 resource = wl_resource_create(client,
4843 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004844
4845 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004846 wl_resource_set_implementation(resource,
4847 &input_panel_implementation,
4848 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004849 shell->input_panel.binding = resource;
4850 return;
4851 }
4852
4853 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4854 "interface object already bound");
4855 wl_resource_destroy(resource);
4856}
4857
Kristian Høgsberg07045392012-02-19 18:52:44 -05004858struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004859 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004860 struct weston_surface *current;
4861 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004862 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004863};
4864
4865static void
4866switcher_next(struct switcher *switcher)
4867{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004868 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004869 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004870 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004871 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004872
Jason Ekstranda7af7042013-10-12 22:38:11 -05004873 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4874 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004875 case SHELL_SURFACE_TOPLEVEL:
4876 case SHELL_SURFACE_FULLSCREEN:
4877 case SHELL_SURFACE_MAXIMIZED:
4878 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004879 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004880 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004881 next = view->surface;
4882 prev = view->surface;
4883 view->alpha = 0.25;
4884 weston_view_geometry_dirty(view);
4885 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004886 break;
4887 default:
4888 break;
4889 }
Alex Wu1659daa2012-04-01 20:13:09 +08004890
Jason Ekstranda7af7042013-10-12 22:38:11 -05004891 if (is_black_surface(view->surface, NULL)) {
4892 view->alpha = 0.25;
4893 weston_view_geometry_dirty(view);
4894 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004895 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004896 }
4897
4898 if (next == NULL)
4899 next = first;
4900
Alex Wu07b26062012-03-12 16:06:01 +08004901 if (next == NULL)
4902 return;
4903
Kristian Høgsberg07045392012-02-19 18:52:44 -05004904 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004905 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004906
4907 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004908 wl_list_for_each(view, &next->views, surface_link)
4909 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004910
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004911 shsurf = get_shell_surface(switcher->current);
4912 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004913 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004914}
4915
4916static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004917switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004918{
4919 struct switcher *switcher =
4920 container_of(listener, struct switcher, listener);
4921
4922 switcher_next(switcher);
4923}
4924
4925static void
Daniel Stone351eb612012-05-31 15:27:47 -04004926switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004927{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004928 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004929 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004930 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004931
Jason Ekstranda7af7042013-10-12 22:38:11 -05004932 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004933 if (is_focus_view(view))
4934 continue;
4935
Jason Ekstranda7af7042013-10-12 22:38:11 -05004936 view->alpha = 1.0;
4937 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004938 }
4939
Alex Wu07b26062012-03-12 16:06:01 +08004940 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004941 activate(switcher->shell, switcher->current,
4942 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004943 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004944 weston_keyboard_end_grab(keyboard);
4945 if (keyboard->input_method_resource)
4946 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004947 free(switcher);
4948}
4949
4950static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004951switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004952 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004953{
4954 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004955 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004956
Daniel Stonec9785ea2012-05-30 16:31:52 +01004957 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004958 switcher_next(switcher);
4959}
4960
4961static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004962switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004963 uint32_t mods_depressed, uint32_t mods_latched,
4964 uint32_t mods_locked, uint32_t group)
4965{
4966 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004967 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004968
Daniel Stone351eb612012-05-31 15:27:47 -04004969 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4970 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004971}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004972
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004973static void
4974switcher_cancel(struct weston_keyboard_grab *grab)
4975{
4976 struct switcher *switcher = container_of(grab, struct switcher, grab);
4977
4978 switcher_destroy(switcher);
4979}
4980
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004981static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004982 switcher_key,
4983 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004984 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004985};
4986
4987static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004988switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004989 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004990{
Tiago Vignattibe143262012-04-16 17:31:41 +03004991 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004992 struct switcher *switcher;
4993
4994 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004995 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004996 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004997 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004998 wl_list_init(&switcher->listener.link);
4999
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005000 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005001 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005002 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005003 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5004 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005005 switcher_next(switcher);
5006}
5007
Daniel Stonedf8133b2013-11-19 11:37:14 +01005008struct exposay_surface {
5009 struct desktop_shell *shell;
5010 struct weston_surface *surface;
5011 struct weston_view *view;
5012 struct wl_list link;
5013
5014 int x;
5015 int y;
5016 int width;
5017 int height;
5018 double scale;
5019
5020 int row;
5021 int column;
5022
5023 /* The animations only apply a transformation for their own lifetime,
5024 * and don't have an option to indefinitely maintain the
5025 * transformation in a steady state - so, we apply our own once the
5026 * animation has finished. */
5027 struct weston_transform transform;
5028};
5029
5030static void exposay_set_state(struct desktop_shell *shell,
5031 enum exposay_target_state state,
5032 struct weston_seat *seat);
5033static void exposay_check_state(struct desktop_shell *shell);
5034
5035static void
5036exposay_in_flight_inc(struct desktop_shell *shell)
5037{
5038 shell->exposay.in_flight++;
5039}
5040
5041static void
5042exposay_in_flight_dec(struct desktop_shell *shell)
5043{
5044 if (--shell->exposay.in_flight > 0)
5045 return;
5046
5047 exposay_check_state(shell);
5048}
5049
5050static void
5051exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5052{
5053 struct exposay_surface *esurface = data;
5054
5055 wl_list_insert(&esurface->view->geometry.transformation_list,
5056 &esurface->transform.link);
5057 weston_matrix_init(&esurface->transform.matrix);
5058 weston_matrix_scale(&esurface->transform.matrix,
5059 esurface->scale, esurface->scale, 1.0f);
5060 weston_matrix_translate(&esurface->transform.matrix,
5061 esurface->x - esurface->view->geometry.x,
5062 esurface->y - esurface->view->geometry.y,
5063 0);
5064
5065 weston_view_geometry_dirty(esurface->view);
5066 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5067
5068 exposay_in_flight_dec(esurface->shell);
5069}
5070
5071static void
5072exposay_animate_in(struct exposay_surface *esurface)
5073{
5074 exposay_in_flight_inc(esurface->shell);
5075
5076 weston_move_scale_run(esurface->view,
5077 esurface->x - esurface->view->geometry.x,
5078 esurface->y - esurface->view->geometry.y,
5079 1.0, esurface->scale, 0,
5080 exposay_animate_in_done, esurface);
5081}
5082
5083static void
5084exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5085{
5086 struct exposay_surface *esurface = data;
5087 struct desktop_shell *shell = esurface->shell;
5088
5089 wl_list_remove(&esurface->link);
5090 free(esurface);
5091
5092 exposay_in_flight_dec(shell);
5093}
5094
5095static void
5096exposay_animate_out(struct exposay_surface *esurface)
5097{
5098 exposay_in_flight_inc(esurface->shell);
5099
5100 /* Remove the static transformation set up by
5101 * exposay_transform_in_done(). */
5102 wl_list_remove(&esurface->transform.link);
5103 weston_view_geometry_dirty(esurface->view);
5104
5105 weston_move_scale_run(esurface->view,
5106 esurface->x - esurface->view->geometry.x,
5107 esurface->y - esurface->view->geometry.y,
5108 1.0, esurface->scale, 1,
5109 exposay_animate_out_done, esurface);
5110}
5111
5112static void
5113exposay_highlight_surface(struct desktop_shell *shell,
5114 struct exposay_surface *esurface)
5115{
5116 struct weston_view *view = NULL;
5117
5118 if (esurface) {
5119 shell->exposay.row_current = esurface->row;
5120 shell->exposay.column_current = esurface->column;
5121 view = esurface->view;
5122 }
5123
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005124 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005125 shell->exposay.focus_current = view;
5126}
5127
5128static int
5129exposay_is_animating(struct desktop_shell *shell)
5130{
5131 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5132 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5133 return 0;
5134
5135 return (shell->exposay.in_flight > 0);
5136}
5137
5138static void
5139exposay_pick(struct desktop_shell *shell, int x, int y)
5140{
5141 struct exposay_surface *esurface;
5142
5143 if (exposay_is_animating(shell))
5144 return;
5145
5146 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5147 if (x < esurface->x || x > esurface->x + esurface->width)
5148 continue;
5149 if (y < esurface->y || y > esurface->y + esurface->height)
5150 continue;
5151
5152 exposay_highlight_surface(shell, esurface);
5153 return;
5154 }
5155}
5156
5157/* Pretty lame layout for now; just tries to make a square. Should take
5158 * aspect ratio into account really. Also needs to be notified of surface
5159 * addition and removal and adjust layout/animate accordingly. */
5160static enum exposay_layout_state
5161exposay_layout(struct desktop_shell *shell)
5162{
5163 struct workspace *workspace = shell->exposay.workspace;
5164 struct weston_compositor *compositor = shell->compositor;
5165 struct weston_output *output = get_default_output(compositor);
5166 struct weston_view *view;
5167 struct exposay_surface *esurface;
5168 int w, h;
5169 int i;
5170 int last_row_removed = 0;
5171
5172 wl_list_init(&shell->exposay.surface_list);
5173
5174 shell->exposay.num_surfaces = 0;
5175 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5176 if (!get_shell_surface(view->surface))
5177 continue;
5178 shell->exposay.num_surfaces++;
5179 }
5180
5181 if (shell->exposay.num_surfaces == 0) {
5182 shell->exposay.grid_size = 0;
5183 shell->exposay.hpadding_outer = 0;
5184 shell->exposay.vpadding_outer = 0;
5185 shell->exposay.padding_inner = 0;
5186 shell->exposay.surface_size = 0;
5187 return EXPOSAY_LAYOUT_OVERVIEW;
5188 }
5189
5190 /* Lay the grid out as square as possible, losing surfaces from the
5191 * bottom row if required. Start with fixed padding of a 10% margin
5192 * around the outside and 80px internal padding between surfaces, and
5193 * maximise the area made available to surfaces after this, but only
5194 * to a maximum of 1/3rd the total output size.
5195 *
5196 * If we can't make a square grid, add one extra row at the bottom
5197 * which will have a smaller number of columns.
5198 *
5199 * XXX: Surely there has to be a better way to express this maths,
5200 * right?!
5201 */
5202 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5203 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5204 shell->exposay.grid_size++;
5205 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5206
5207 shell->exposay.hpadding_outer = (output->width / 10);
5208 shell->exposay.vpadding_outer = (output->height / 10);
5209 shell->exposay.padding_inner = 80;
5210
5211 w = output->width - (shell->exposay.hpadding_outer * 2);
5212 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5213 w /= shell->exposay.grid_size;
5214
5215 h = output->height - (shell->exposay.vpadding_outer * 2);
5216 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5217 h /= shell->exposay.grid_size;
5218
5219 shell->exposay.surface_size = (w < h) ? w : h;
5220 if (shell->exposay.surface_size > (output->width / 2))
5221 shell->exposay.surface_size = output->width / 2;
5222 if (shell->exposay.surface_size > (output->height / 2))
5223 shell->exposay.surface_size = output->height / 2;
5224
5225 i = 0;
5226 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5227 int pad;
5228
5229 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5230
5231 if (!get_shell_surface(view->surface))
5232 continue;
5233
5234 esurface = malloc(sizeof(*esurface));
5235 if (!esurface) {
5236 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5237 shell->exposay.seat);
5238 break;
5239 }
5240
5241 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5242 esurface->shell = shell;
5243 esurface->view = view;
5244
5245 esurface->row = i / shell->exposay.grid_size;
5246 esurface->column = i % shell->exposay.grid_size;
5247
5248 esurface->x = shell->exposay.hpadding_outer;
5249 esurface->x += pad * esurface->column;
5250 esurface->y = shell->exposay.vpadding_outer;
5251 esurface->y += pad * esurface->row;
5252
5253 if (esurface->row == shell->exposay.grid_size - 1)
5254 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5255
5256 if (view->geometry.width > view->geometry.height)
5257 esurface->scale = shell->exposay.surface_size / (float) view->geometry.width;
5258 else
5259 esurface->scale = shell->exposay.surface_size / (float) view->geometry.height;
5260 esurface->width = view->geometry.width * esurface->scale;
5261 esurface->height = view->geometry.height * esurface->scale;
5262
5263 if (shell->exposay.focus_current == esurface->view)
5264 exposay_highlight_surface(shell, esurface);
5265
5266 exposay_animate_in(esurface);
5267
5268 i++;
5269 }
5270
5271 weston_compositor_schedule_repaint(shell->compositor);
5272
5273 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5274}
5275
5276static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005277exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5278 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005279{
5280 struct desktop_shell *shell =
5281 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5282
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005283 weston_pointer_move(grab->pointer, x, y);
5284
Daniel Stonedf8133b2013-11-19 11:37:14 +01005285 exposay_pick(shell,
5286 wl_fixed_to_int(grab->pointer->x),
5287 wl_fixed_to_int(grab->pointer->y));
5288}
5289
5290static void
5291exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5292 uint32_t state_w)
5293{
5294 struct desktop_shell *shell =
5295 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5296 struct weston_seat *seat = grab->pointer->seat;
5297 enum wl_pointer_button_state state = state_w;
5298
5299 if (button != BTN_LEFT)
5300 return;
5301
5302 /* Store the surface we clicked on, and don't do anything if we end up
5303 * releasing on a different surface. */
5304 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5305 shell->exposay.clicked = shell->exposay.focus_current;
5306 return;
5307 }
5308
5309 if (shell->exposay.focus_current == shell->exposay.clicked)
5310 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5311 else
5312 shell->exposay.clicked = NULL;
5313}
5314
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005315static void
5316exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
5317{
5318 struct desktop_shell *shell =
5319 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5320
5321 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5322}
5323
Daniel Stonedf8133b2013-11-19 11:37:14 +01005324static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5325 noop_grab_focus,
5326 exposay_motion,
5327 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005328 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005329};
5330
5331static int
5332exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5333{
5334 struct exposay_surface *esurface;
5335
5336 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5337 if (esurface->row != row || esurface->column != column)
5338 continue;
5339
5340 exposay_highlight_surface(shell, esurface);
5341 return 1;
5342 }
5343
5344 return 0;
5345}
5346
5347static void
5348exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5349 uint32_t state_w)
5350{
5351 struct weston_seat *seat = grab->keyboard->seat;
5352 struct desktop_shell *shell =
5353 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5354 enum wl_keyboard_key_state state = state_w;
5355
5356 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5357 return;
5358
5359 switch (key) {
5360 case KEY_ESC:
5361 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5362 break;
5363 case KEY_ENTER:
5364 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5365 break;
5366 case KEY_UP:
5367 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5368 shell->exposay.column_current);
5369 break;
5370 case KEY_DOWN:
5371 /* Special case for trying to move to the bottom row when it
5372 * has fewer items than all the others. */
5373 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5374 shell->exposay.column_current) &&
5375 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5376 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5377 (shell->exposay.num_surfaces %
5378 shell->exposay.grid_size) - 1);
5379 }
5380 break;
5381 case KEY_LEFT:
5382 exposay_maybe_move(shell, shell->exposay.row_current,
5383 shell->exposay.column_current - 1);
5384 break;
5385 case KEY_RIGHT:
5386 exposay_maybe_move(shell, shell->exposay.row_current,
5387 shell->exposay.column_current + 1);
5388 break;
5389 case KEY_TAB:
5390 /* Try to move right, then down (and to the leftmost column),
5391 * then if all else fails, to the top left. */
5392 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5393 shell->exposay.column_current + 1) &&
5394 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5395 exposay_maybe_move(shell, 0, 0);
5396 break;
5397 default:
5398 break;
5399 }
5400}
5401
5402static void
5403exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5404 uint32_t mods_depressed, uint32_t mods_latched,
5405 uint32_t mods_locked, uint32_t group)
5406{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005407 struct desktop_shell *shell =
5408 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5409 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5410
5411 /* We want to know when mod has been pressed and released.
5412 * FIXME: There is a problem here: if mod is pressed, then a key
5413 * is pressed and released, then mod is released, we will treat that
5414 * as if only mod had been pressed and released. */
5415 if (seat->modifier_state) {
5416 if (seat->modifier_state == shell->binding_modifier) {
5417 shell->exposay.mod_pressed = true;
5418 } else {
5419 shell->exposay.mod_invalid = true;
5420 }
5421 } else {
5422 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
5423 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5424
5425 shell->exposay.mod_invalid = false;
5426 shell->exposay.mod_pressed = false;
5427 }
5428
5429 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005430}
5431
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005432static void
5433exposay_cancel(struct weston_keyboard_grab *grab)
5434{
5435 struct desktop_shell *shell =
5436 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5437
5438 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5439}
5440
Daniel Stonedf8133b2013-11-19 11:37:14 +01005441static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5442 exposay_key,
5443 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005444 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005445};
5446
5447/**
5448 * Called when the transition from overview -> inactive has completed.
5449 */
5450static enum exposay_layout_state
5451exposay_set_inactive(struct desktop_shell *shell)
5452{
5453 struct weston_seat *seat = shell->exposay.seat;
5454
5455 weston_keyboard_end_grab(seat->keyboard);
5456 weston_pointer_end_grab(seat->pointer);
5457 if (seat->keyboard->input_method_resource)
5458 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5459
5460 return EXPOSAY_LAYOUT_INACTIVE;
5461}
5462
5463/**
5464 * Begins the transition from overview to inactive. */
5465static enum exposay_layout_state
5466exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5467{
5468 struct exposay_surface *esurface;
5469
5470 /* Call activate() before we start the animations to avoid
5471 * animating back the old state and then immediately transitioning
5472 * to the new. */
5473 if (switch_focus && shell->exposay.focus_current)
5474 activate(shell, shell->exposay.focus_current->surface,
5475 shell->exposay.seat);
5476 else if (shell->exposay.focus_prev)
5477 activate(shell, shell->exposay.focus_prev->surface,
5478 shell->exposay.seat);
5479
5480 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5481 exposay_animate_out(esurface);
5482 weston_compositor_schedule_repaint(shell->compositor);
5483
5484 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5485}
5486
5487static enum exposay_layout_state
5488exposay_transition_active(struct desktop_shell *shell)
5489{
5490 struct weston_seat *seat = shell->exposay.seat;
5491
5492 shell->exposay.workspace = get_current_workspace(shell);
5493 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5494 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5495 shell->exposay.clicked = NULL;
5496 wl_list_init(&shell->exposay.surface_list);
5497
5498 lower_fullscreen_layer(shell);
5499 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5500 weston_keyboard_start_grab(seat->keyboard,
5501 &shell->exposay.grab_kbd);
5502 weston_keyboard_set_focus(seat->keyboard, NULL);
5503
5504 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5505 weston_pointer_start_grab(seat->pointer,
5506 &shell->exposay.grab_ptr);
5507 weston_pointer_set_focus(seat->pointer, NULL,
5508 seat->pointer->x, seat->pointer->y);
5509
5510 return exposay_layout(shell);
5511}
5512
5513static void
5514exposay_check_state(struct desktop_shell *shell)
5515{
5516 enum exposay_layout_state state_new = shell->exposay.state_cur;
5517 int do_switch = 0;
5518
5519 /* Don't do anything whilst animations are running, just store up
5520 * target state changes and only act on them when the animations have
5521 * completed. */
5522 if (exposay_is_animating(shell))
5523 return;
5524
5525 switch (shell->exposay.state_target) {
5526 case EXPOSAY_TARGET_OVERVIEW:
5527 switch (shell->exposay.state_cur) {
5528 case EXPOSAY_LAYOUT_OVERVIEW:
5529 goto out;
5530 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5531 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5532 break;
5533 default:
5534 state_new = exposay_transition_active(shell);
5535 break;
5536 }
5537 break;
5538
5539 case EXPOSAY_TARGET_SWITCH:
5540 do_switch = 1; /* fallthrough */
5541 case EXPOSAY_TARGET_CANCEL:
5542 switch (shell->exposay.state_cur) {
5543 case EXPOSAY_LAYOUT_INACTIVE:
5544 goto out;
5545 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5546 state_new = exposay_set_inactive(shell);
5547 break;
5548 default:
5549 state_new = exposay_transition_inactive(shell, do_switch);
5550 break;
5551 }
5552
5553 break;
5554 }
5555
5556out:
5557 shell->exposay.state_cur = state_new;
5558}
5559
5560static void
5561exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5562 struct weston_seat *seat)
5563{
5564 shell->exposay.state_target = state;
5565 shell->exposay.seat = seat;
5566 exposay_check_state(shell);
5567}
5568
5569static void
5570exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5571 void *data)
5572{
5573 struct desktop_shell *shell = data;
5574
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005575 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005576}
5577
Pekka Paalanen3c647232011-12-22 13:43:43 +02005578static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005579backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005580 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005581{
5582 struct weston_compositor *compositor = data;
5583 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005584 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005585
5586 /* TODO: we're limiting to simple use cases, where we assume just
5587 * control on the primary display. We'd have to extend later if we
5588 * ever get support for setting backlights on random desktop LCD
5589 * panels though */
5590 output = get_default_output(compositor);
5591 if (!output)
5592 return;
5593
5594 if (!output->set_backlight)
5595 return;
5596
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005597 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5598 backlight_new = output->backlight_current - 25;
5599 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5600 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005601
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005602 if (backlight_new < 5)
5603 backlight_new = 5;
5604 if (backlight_new > 255)
5605 backlight_new = 255;
5606
5607 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005608 output->set_backlight(output, output->backlight_current);
5609}
5610
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005611struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005612 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005613 struct weston_seat *seat;
5614 uint32_t key[2];
5615 int key_released[2];
5616};
5617
5618static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005619debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005620 uint32_t key, uint32_t state)
5621{
5622 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005623 struct weston_compositor *ec = db->seat->compositor;
5624 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005625 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005626 uint32_t serial;
5627 int send = 0, terminate = 0;
5628 int check_binding = 1;
5629 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005630 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005631
5632 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5633 /* Do not run bindings on key releases */
5634 check_binding = 0;
5635
5636 for (i = 0; i < 2; i++)
5637 if (key == db->key[i])
5638 db->key_released[i] = 1;
5639
5640 if (db->key_released[0] && db->key_released[1]) {
5641 /* All key releases been swalled so end the grab */
5642 terminate = 1;
5643 } else if (key != db->key[0] && key != db->key[1]) {
5644 /* Should not swallow release of other keys */
5645 send = 1;
5646 }
5647 } else if (key == db->key[0] && !db->key_released[0]) {
5648 /* Do not check bindings for the first press of the binding
5649 * key. This allows it to be used as a debug shortcut.
5650 * We still need to swallow this event. */
5651 check_binding = 0;
5652 } else if (db->key[1]) {
5653 /* If we already ran a binding don't process another one since
5654 * we can't keep track of all the binding keys that were
5655 * pressed in order to swallow the release events. */
5656 send = 1;
5657 check_binding = 0;
5658 }
5659
5660 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005661 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5662 key, state)) {
5663 /* We ran a binding so swallow the press and keep the
5664 * grab to swallow the released too. */
5665 send = 0;
5666 terminate = 0;
5667 db->key[1] = key;
5668 } else {
5669 /* Terminate the grab since the key pressed is not a
5670 * debug binding key. */
5671 send = 1;
5672 terminate = 1;
5673 }
5674 }
5675
5676 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005677 serial = wl_display_next_serial(display);
5678 resource_list = &grab->keyboard->focus_resource_list;
5679 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005680 wl_keyboard_send_key(resource, serial, time, key, state);
5681 }
5682 }
5683
5684 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005685 weston_keyboard_end_grab(grab->keyboard);
5686 if (grab->keyboard->input_method_resource)
5687 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005688 free(db);
5689 }
5690}
5691
5692static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005693debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005694 uint32_t mods_depressed, uint32_t mods_latched,
5695 uint32_t mods_locked, uint32_t group)
5696{
5697 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005698 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005699
Neil Roberts96d790e2013-09-19 17:32:00 +01005700 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005701
Neil Roberts96d790e2013-09-19 17:32:00 +01005702 wl_resource_for_each(resource, resource_list) {
5703 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5704 mods_latched, mods_locked, group);
5705 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005706}
5707
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005708static void
5709debug_binding_cancel(struct weston_keyboard_grab *grab)
5710{
5711 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5712
5713 weston_keyboard_end_grab(grab->keyboard);
5714 free(db);
5715}
5716
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005717struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005718 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005719 debug_binding_modifiers,
5720 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005721};
5722
5723static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005724debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005725{
5726 struct debug_binding_grab *grab;
5727
5728 grab = calloc(1, sizeof *grab);
5729 if (!grab)
5730 return;
5731
5732 grab->seat = (struct weston_seat *) seat;
5733 grab->key[0] = key;
5734 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005735 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005736}
5737
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005738static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005739force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005740 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005741{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005742 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005743 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005744 struct desktop_shell *shell = data;
5745 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005746 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005747
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005748 focus_surface = seat->keyboard->focus;
5749 if (!focus_surface)
5750 return;
5751
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005752 wl_signal_emit(&compositor->kill_signal, focus_surface);
5753
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005754 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005755 wl_client_get_credentials(client, &pid, NULL, NULL);
5756
5757 /* Skip clients that we launched ourselves (the credentials of
5758 * the socketpair is ours) */
5759 if (pid == getpid())
5760 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005761
Daniel Stone325fc2d2012-05-30 16:31:58 +01005762 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005763}
5764
5765static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005766workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005767 uint32_t key, void *data)
5768{
5769 struct desktop_shell *shell = data;
5770 unsigned int new_index = shell->workspaces.current;
5771
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005772 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005773 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005774 if (new_index != 0)
5775 new_index--;
5776
5777 change_workspace(shell, new_index);
5778}
5779
5780static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005781workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005782 uint32_t key, void *data)
5783{
5784 struct desktop_shell *shell = data;
5785 unsigned int new_index = shell->workspaces.current;
5786
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005787 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005788 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005789 if (new_index < shell->workspaces.num - 1)
5790 new_index++;
5791
5792 change_workspace(shell, new_index);
5793}
5794
5795static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005796workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005797 uint32_t key, void *data)
5798{
5799 struct desktop_shell *shell = data;
5800 unsigned int new_index;
5801
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005802 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005803 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005804 new_index = key - KEY_F1;
5805 if (new_index >= shell->workspaces.num)
5806 new_index = shell->workspaces.num - 1;
5807
5808 change_workspace(shell, new_index);
5809}
5810
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005811static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005812workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005813 uint32_t key, void *data)
5814{
5815 struct desktop_shell *shell = data;
5816 unsigned int new_index = shell->workspaces.current;
5817
5818 if (shell->locked)
5819 return;
5820
5821 if (new_index != 0)
5822 new_index--;
5823
5824 take_surface_to_workspace_by_seat(shell, seat, new_index);
5825}
5826
5827static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005828workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005829 uint32_t key, void *data)
5830{
5831 struct desktop_shell *shell = data;
5832 unsigned int new_index = shell->workspaces.current;
5833
5834 if (shell->locked)
5835 return;
5836
5837 if (new_index < shell->workspaces.num - 1)
5838 new_index++;
5839
5840 take_surface_to_workspace_by_seat(shell, seat, new_index);
5841}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005842
5843static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005844handle_output_destroy(struct wl_listener *listener, void *data)
5845{
5846 struct shell_output *output_listener =
5847 container_of(listener, struct shell_output, destroy_listener);
5848
5849 wl_list_remove(&output_listener->destroy_listener.link);
5850 wl_list_remove(&output_listener->link);
5851 free(output_listener);
5852}
5853
5854static void
5855create_shell_output(struct desktop_shell *shell,
5856 struct weston_output *output)
5857{
5858 struct shell_output *shell_output;
5859
5860 shell_output = zalloc(sizeof *shell_output);
5861 if (shell_output == NULL)
5862 return;
5863
5864 shell_output->output = output;
5865 shell_output->shell = shell;
5866 shell_output->destroy_listener.notify = handle_output_destroy;
5867 wl_signal_add(&output->destroy_signal,
5868 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005869 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005870}
5871
5872static void
5873handle_output_create(struct wl_listener *listener, void *data)
5874{
5875 struct desktop_shell *shell =
5876 container_of(listener, struct desktop_shell, output_create_listener);
5877 struct weston_output *output = (struct weston_output *)data;
5878
5879 create_shell_output(shell, output);
5880}
5881
5882static void
5883setup_output_destroy_handler(struct weston_compositor *ec,
5884 struct desktop_shell *shell)
5885{
5886 struct weston_output *output;
5887
5888 wl_list_init(&shell->output_list);
5889 wl_list_for_each(output, &ec->output_list, link)
5890 create_shell_output(shell, output);
5891
5892 shell->output_create_listener.notify = handle_output_create;
5893 wl_signal_add(&ec->output_created_signal,
5894 &shell->output_create_listener);
5895}
5896
5897static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005898shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005899{
Tiago Vignattibe143262012-04-16 17:31:41 +03005900 struct desktop_shell *shell =
5901 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005902 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005903 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005904
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005905 if (shell->child.client)
5906 wl_client_destroy(shell->child.client);
5907
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005908 wl_list_remove(&shell->idle_listener.link);
5909 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005910 wl_list_remove(&shell->show_input_panel_listener.link);
5911 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005912
Xiong Zhang6b481422013-10-23 13:58:32 +08005913 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5914 wl_list_remove(&shell_output->destroy_listener.link);
5915 wl_list_remove(&shell_output->link);
5916 free(shell_output);
5917 }
5918
5919 wl_list_remove(&shell->output_create_listener.link);
5920
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005921 wl_array_for_each(ws, &shell->workspaces.array)
5922 workspace_destroy(*ws);
5923 wl_array_release(&shell->workspaces.array);
5924
Pekka Paalanen3c647232011-12-22 13:43:43 +02005925 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005926 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005927 free(shell);
5928}
5929
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005930static void
5931shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5932{
5933 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005934 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005935
5936 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005937 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5938 MODIFIER_CTRL | MODIFIER_ALT,
5939 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005940 weston_compositor_add_key_binding(ec, KEY_TAB,
5941 MODIFIER_ALT,
5942 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005943 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5944 click_to_activate_binding,
5945 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005946 weston_compositor_add_touch_binding(ec, 0,
5947 touch_to_activate_binding,
5948 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005949 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5950 MODIFIER_SUPER | MODIFIER_ALT,
5951 surface_opacity_binding, NULL);
5952 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5953 MODIFIER_SUPER, zoom_axis_binding,
5954 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005955
5956 /* configurable bindings */
5957 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005958 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5959 zoom_key_binding, NULL);
5960 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5961 zoom_key_binding, NULL);
5962 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5963 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005964 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005965 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5966 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005967
5968 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5969 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5970 rotate_binding, NULL);
5971
Daniel Stone325fc2d2012-05-30 16:31:58 +01005972 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5973 shell);
5974 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5975 ec);
5976 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5977 backlight_binding, ec);
5978 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5979 ec);
5980 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5981 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005982 weston_compositor_add_key_binding(ec, KEY_K, mod,
5983 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005984 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5985 workspace_up_binding, shell);
5986 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5987 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005988 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5989 workspace_move_surface_up_binding,
5990 shell);
5991 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5992 workspace_move_surface_down_binding,
5993 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005994
Daniel Stonedf8133b2013-11-19 11:37:14 +01005995 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5996
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005997 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5998 if (shell->workspaces.num > 1) {
5999 num_workspace_bindings = shell->workspaces.num;
6000 if (num_workspace_bindings > 6)
6001 num_workspace_bindings = 6;
6002 for (i = 0; i < num_workspace_bindings; i++)
6003 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6004 workspace_f_binding,
6005 shell);
6006 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006007
6008 /* Debug bindings */
6009 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6010 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006011}
6012
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006013WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006014module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006015 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006016{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006017 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006018 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006019 struct workspace **pws;
6020 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006021 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006022
Peter Huttererf3d62272013-08-08 11:57:05 +10006023 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006024 if (shell == NULL)
6025 return -1;
6026
Kristian Høgsberg75840622011-09-06 13:48:16 -04006027 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006028
6029 shell->destroy_listener.notify = shell_destroy;
6030 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006031 shell->idle_listener.notify = idle_handler;
6032 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6033 shell->wake_listener.notify = wake_handler;
6034 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006035 shell->show_input_panel_listener.notify = show_input_panels;
6036 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6037 shell->hide_input_panel_listener.notify = hide_input_panels;
6038 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006039 shell->update_input_panel_listener.notify = update_input_panels;
6040 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006041 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006042 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006043 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006044 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006045 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006046 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006047 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006048 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006049 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006050 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006051 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006052
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006053 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006054
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006055 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6056 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006057 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6058 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006059 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006060
6061 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006062 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006063
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006064 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006065
Daniel Stonedf8133b2013-11-19 11:37:14 +01006066 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6067 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6068
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006069 for (i = 0; i < shell->workspaces.num; i++) {
6070 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6071 if (pws == NULL)
6072 return -1;
6073
6074 *pws = workspace_create();
6075 if (*pws == NULL)
6076 return -1;
6077 }
6078 activate_workspace(shell, 0);
6079
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006080 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006081 wl_list_init(&shell->workspaces.animation.link);
6082 shell->workspaces.animation.frame = animate_workspace_change_frame;
6083
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006084 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006085 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006086 return -1;
6087
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006088 if (wl_global_create(ec->wl_display,
6089 &desktop_shell_interface, 2,
6090 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006091 return -1;
6092
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006093 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6094 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006095 return -1;
6096
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006097 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006098 shell, bind_input_panel) == NULL)
6099 return -1;
6100
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006101 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6102 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006103 return -1;
6104
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006105 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006106
Xiong Zhang6b481422013-10-23 13:58:32 +08006107 setup_output_destroy_handler(ec, shell);
6108
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006109 loop = wl_display_get_event_loop(ec->wl_display);
6110 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006111
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006112 shell->screensaver.timer =
6113 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6114
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006115 wl_list_for_each(seat, &ec->seat_list, link)
6116 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006117
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006118 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006119
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006120 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006121
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006122 return 0;
6123}