blob: e9d733c15fe68e32a0933356f23f1421fb983329 [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
Philip Withnall659163d2013-11-25 18:01:36 +00001238move_surface_to_workspace(struct desktop_shell *shell,
1239 struct shell_surface *shsurf,
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;
Philip Withnall659163d2013-11-25 18:01:36 +00001246 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001247
1248 if (workspace == shell->workspaces.current)
1249 return;
1250
Philip Withnall659163d2013-11-25 18:01:36 +00001251 view = get_default_view(shsurf->surface);
1252 if (!view)
1253 return;
1254
1255 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1256
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001257 if (workspace >= shell->workspaces.num)
1258 workspace = shell->workspaces.num - 1;
1259
Jonas Ådahle9d22502012-08-29 22:13:01 +02001260 from = get_current_workspace(shell);
1261 to = get_workspace(shell, workspace);
1262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 wl_list_remove(&view->layer_link);
1264 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001267 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1268 if (!seat->keyboard)
1269 continue;
1270
1271 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001273 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001274 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001277}
1278
1279static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001281 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282 unsigned int index)
1283{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001284 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001286 struct shell_surface *shsurf;
1287 struct workspace *from;
1288 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001289 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001290
Pekka Paalanen01388e22013-04-25 13:57:44 +03001291 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 view = get_default_view(surface);
1293 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001294 index == shell->workspaces.current ||
1295 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001296 return;
1297
1298 from = get_current_workspace(shell);
1299 to = get_workspace(shell, index);
1300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 wl_list_remove(&view->layer_link);
1302 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001303
Philip Withnall659163d2013-11-25 18:01:36 +00001304 shsurf = get_shell_surface(surface);
1305
Jonas Ådahle9d22502012-08-29 22:13:01 +02001306 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001307 drop_focus_state(shell, from, surface);
1308
1309 if (shell->workspaces.anim_from == to &&
1310 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001311 wl_list_remove(&to->layer.link);
1312 wl_list_insert(from->layer.link.prev, &to->layer.link);
1313
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001314 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001315 broadcast_current_workspace_state(shell);
1316
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001317 return;
1318 }
1319
1320 if (shell->workspaces.anim_to != NULL)
1321 finish_workspace_change_animation(shell,
1322 shell->workspaces.anim_from,
1323 shell->workspaces.anim_to);
1324
1325 if (workspace_is_empty(from) &&
1326 workspace_has_only(to, surface))
1327 update_workspace(shell, index, from, to);
1328 else {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001329 if (wl_list_empty(&shsurf->workspace_transform.link))
1330 wl_list_insert(&shell->workspaces.anim_sticky_list,
1331 &shsurf->workspace_transform.link);
1332
1333 animate_workspace_change(shell, index, from, to);
1334 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001335
1336 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001337
1338 state = ensure_focus_state(shell, seat);
1339 if (state != NULL)
1340 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001341}
1342
1343static void
1344workspace_manager_move_surface(struct wl_client *client,
1345 struct wl_resource *resource,
1346 struct wl_resource *surface_resource,
1347 uint32_t workspace)
1348{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001349 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001350 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001351 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001352 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001353 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001354
Pekka Paalanen01388e22013-04-25 13:57:44 +03001355 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001356 shell_surface = get_shell_surface(main_surface);
1357 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001359
1360 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001361}
1362
1363static const struct workspace_manager_interface workspace_manager_implementation = {
1364 workspace_manager_move_surface,
1365};
1366
1367static void
1368unbind_resource(struct wl_resource *resource)
1369{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001370 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001371}
1372
1373static void
1374bind_workspace_manager(struct wl_client *client,
1375 void *data, uint32_t version, uint32_t id)
1376{
1377 struct desktop_shell *shell = data;
1378 struct wl_resource *resource;
1379
Jason Ekstranda85118c2013-06-27 20:17:02 -05001380 resource = wl_resource_create(client,
1381 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001382
1383 if (resource == NULL) {
1384 weston_log("couldn't add workspace manager object");
1385 return;
1386 }
1387
Jason Ekstranda85118c2013-06-27 20:17:02 -05001388 wl_resource_set_implementation(resource,
1389 &workspace_manager_implementation,
1390 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001391 wl_list_insert(&shell->workspaces.client_list,
1392 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001393
1394 workspace_manager_send_state(resource,
1395 shell->workspaces.current,
1396 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001397}
1398
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001399static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001400touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1401 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1402{
1403}
1404
1405static void
1406touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1407{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001408 struct weston_touch_move_grab *move =
1409 (struct weston_touch_move_grab *) container_of(
1410 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001411
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001412 if (grab->touch->seat->num_tp == 0) {
1413 shell_touch_grab_end(&move->base);
1414 free(move);
1415 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001416}
1417
1418static void
1419touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1420 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1421{
1422 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1423 struct shell_surface *shsurf = move->base.shsurf;
1424 struct weston_surface *es;
1425 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1426 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1427
1428 if (!shsurf)
1429 return;
1430
1431 es = shsurf->surface;
1432
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 weston_view_configure(shsurf->view, dx, dy,
1434 shsurf->view->geometry.width,
1435 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001436
1437 weston_compositor_schedule_repaint(es->compositor);
1438}
1439
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001440static void
1441touch_move_grab_cancel(struct weston_touch_grab *grab)
1442{
1443 struct weston_touch_move_grab *move =
1444 (struct weston_touch_move_grab *) container_of(
1445 grab, struct shell_touch_grab, grab);
1446
1447 shell_touch_grab_end(&move->base);
1448 free(move);
1449}
1450
Rusty Lynch1084da52013-08-15 09:10:08 -07001451static const struct weston_touch_grab_interface touch_move_grab_interface = {
1452 touch_move_grab_down,
1453 touch_move_grab_up,
1454 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001455 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001456};
1457
1458static int
1459surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1460{
1461 struct weston_touch_move_grab *move;
1462
1463 if (!shsurf)
1464 return -1;
1465
1466 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1467 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001468 if (shsurf->grabbed)
1469 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001470
1471 move = malloc(sizeof *move);
1472 if (!move)
1473 return -1;
1474
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001476 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001478 seat->touch->grab_y;
1479
1480 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1481 seat->touch);
1482
1483 return 0;
1484}
1485
1486static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001487noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001488{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001489}
1490
1491static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001492move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1493 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001494{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001495 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001496 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001497 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001498 int dx, dy;
1499
1500 weston_pointer_move(pointer, x, y);
1501 dx = wl_fixed_to_int(pointer->x + move->dx);
1502 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001503
1504 if (!shsurf)
1505 return;
1506
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 weston_view_configure(shsurf->view, dx, dy,
1508 shsurf->view->geometry.width,
1509 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001512}
1513
1514static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001515move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001516 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001517{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001518 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1519 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001520 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001521 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001522
Daniel Stone4dbadb12012-05-30 16:31:51 +01001523 if (pointer->button_count == 0 &&
1524 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001525 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001526 free(grab);
1527 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001528}
1529
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001530static void
1531move_grab_cancel(struct weston_pointer_grab *grab)
1532{
1533 struct shell_grab *shell_grab =
1534 container_of(grab, struct shell_grab, grab);
1535
1536 shell_grab_end(shell_grab);
1537 free(grab);
1538}
1539
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001540static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001541 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001542 move_grab_motion,
1543 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001544 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001545};
1546
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001547static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001548surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549{
1550 struct weston_move_grab *move;
1551
1552 if (!shsurf)
1553 return -1;
1554
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001555 if (shsurf->grabbed)
1556 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001557 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1558 return 0;
1559
1560 move = malloc(sizeof *move);
1561 if (!move)
1562 return -1;
1563
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001565 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001567 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568
1569 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001570 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001571
1572 return 0;
1573}
1574
1575static void
1576shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1577 struct wl_resource *seat_resource, uint32_t serial)
1578{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001579 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001580 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001581 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001582
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001583 if (seat->pointer &&
1584 seat->pointer->button_count > 0 &&
1585 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001587 if ((surface == shsurf->surface) &&
1588 (surface_move(shsurf, seat) < 0))
1589 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001590 } else if (seat->touch &&
1591 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001593 if ((surface == shsurf->surface) &&
1594 (surface_touch_move(shsurf, seat) < 0))
1595 wl_resource_post_no_memory(resource);
1596 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597}
1598
1599struct weston_resize_grab {
1600 struct shell_grab base;
1601 uint32_t edges;
1602 int32_t width, height;
1603};
1604
1605static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001606resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1607 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001608{
1609 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001610 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001611 struct shell_surface *shsurf = resize->base.shsurf;
1612 int32_t width, height;
1613 wl_fixed_t from_x, from_y;
1614 wl_fixed_t to_x, to_y;
1615
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001616 weston_pointer_move(pointer, x, y);
1617
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001618 if (!shsurf)
1619 return;
1620
Jason Ekstranda7af7042013-10-12 22:38:11 -05001621 weston_view_from_global_fixed(shsurf->view,
1622 pointer->grab_x, pointer->grab_y,
1623 &from_x, &from_y);
1624 weston_view_from_global_fixed(shsurf->view,
1625 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001626
1627 width = resize->width;
1628 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1629 width += wl_fixed_to_int(from_x - to_x);
1630 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1631 width += wl_fixed_to_int(to_x - from_x);
1632 }
1633
1634 height = resize->height;
1635 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1636 height += wl_fixed_to_int(from_y - to_y);
1637 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1638 height += wl_fixed_to_int(to_y - from_y);
1639 }
1640
1641 shsurf->client->send_configure(shsurf->surface,
1642 resize->edges, width, height);
1643}
1644
1645static void
1646send_configure(struct weston_surface *surface,
1647 uint32_t edges, int32_t width, int32_t height)
1648{
1649 struct shell_surface *shsurf = get_shell_surface(surface);
1650
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001651 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001652 edges, width, height);
1653}
1654
1655static const struct weston_shell_client shell_client = {
1656 send_configure
1657};
1658
1659static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001660resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001661 uint32_t time, uint32_t button, uint32_t state_w)
1662{
1663 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001664 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001665 enum wl_pointer_button_state state = state_w;
1666
1667 if (pointer->button_count == 0 &&
1668 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1669 shell_grab_end(&resize->base);
1670 free(grab);
1671 }
1672}
1673
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001674static void
1675resize_grab_cancel(struct weston_pointer_grab *grab)
1676{
1677 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1678
1679 shell_grab_end(&resize->base);
1680 free(grab);
1681}
1682
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001683static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001684 noop_grab_focus,
1685 resize_grab_motion,
1686 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001687 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688};
1689
Giulio Camuffob8366642013-04-25 13:57:46 +03001690/*
1691 * Returns the bounding box of a surface and all its sub-surfaces,
1692 * in the surface coordinates system. */
1693static void
1694surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1695 int32_t *y, int32_t *w, int32_t *h) {
1696 pixman_region32_t region;
1697 pixman_box32_t *box;
1698 struct weston_subsurface *subsurface;
1699
1700 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 surface->width,
1702 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001703
1704 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1705 pixman_region32_union_rect(&region, &region,
1706 subsurface->position.x,
1707 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 subsurface->surface->width,
1709 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001710 }
1711
1712 box = pixman_region32_extents(&region);
1713 if (x)
1714 *x = box->x1;
1715 if (y)
1716 *y = box->y1;
1717 if (w)
1718 *w = box->x2 - box->x1;
1719 if (h)
1720 *h = box->y2 - box->y1;
1721
1722 pixman_region32_fini(&region);
1723}
1724
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725static int
1726surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001727 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728{
1729 struct weston_resize_grab *resize;
1730
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001731 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1732 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001733 return 0;
1734
1735 if (edges == 0 || edges > 15 ||
1736 (edges & 3) == 3 || (edges & 12) == 12)
1737 return 0;
1738
1739 resize = malloc(sizeof *resize);
1740 if (!resize)
1741 return -1;
1742
1743 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001744 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1745 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001746
1747 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001748 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749
1750 return 0;
1751}
1752
1753static void
1754shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1755 struct wl_resource *seat_resource, uint32_t serial,
1756 uint32_t edges)
1757{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001758 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001759 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001760 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001761
1762 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1763 return;
1764
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001766 if (seat->pointer->button_count == 0 ||
1767 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001768 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001769 return;
1770
Kristian Høgsberge3148752013-05-06 23:19:49 -04001771 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001772 wl_resource_post_no_memory(resource);
1773}
1774
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001775static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001776end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1777
1778static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001779busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001780{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001781 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001782 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001784 wl_fixed_t sx, sy;
1785
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 view = weston_compositor_pick_view(pointer->seat->compositor,
1787 pointer->x, pointer->y,
1788 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001789
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001791 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001792}
1793
1794static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001795busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1796 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001797{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001798 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001799}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001800
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001801static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001802busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803 uint32_t time, uint32_t button, uint32_t state)
1804{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001805 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001806 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001807 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001808
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001809 if (shsurf && button == BTN_LEFT && state) {
1810 activate(shsurf->shell, shsurf->surface, seat);
1811 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001812 } else if (shsurf && button == BTN_RIGHT && state) {
1813 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001814 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001815 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001816}
1817
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001818static void
1819busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1820{
1821 struct shell_grab *grab = (struct shell_grab *) base;
1822
1823 shell_grab_end(grab);
1824 free(grab);
1825}
1826
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001827static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001828 busy_cursor_grab_focus,
1829 busy_cursor_grab_motion,
1830 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001831 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832};
1833
1834static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001835set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836{
1837 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001838
1839 grab = malloc(sizeof *grab);
1840 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001841 return;
1842
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001843 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1844 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001845}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001846
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001847static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001848end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001849{
1850 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1851
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001852 if (grab->grab.interface == &busy_cursor_grab_interface &&
1853 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001854 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001855 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001856 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001857}
1858
Scott Moreau9521d5e2012-04-19 13:06:17 -06001859static void
1860ping_timer_destroy(struct shell_surface *shsurf)
1861{
1862 if (!shsurf || !shsurf->ping_timer)
1863 return;
1864
1865 if (shsurf->ping_timer->source)
1866 wl_event_source_remove(shsurf->ping_timer->source);
1867
1868 free(shsurf->ping_timer);
1869 shsurf->ping_timer = NULL;
1870}
1871
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001872static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001873ping_timeout_handler(void *data)
1874{
1875 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001876 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001877
Scott Moreau9521d5e2012-04-19 13:06:17 -06001878 /* Client is not responding */
1879 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001880
1881 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001882 if (seat->pointer->focus &&
1883 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001884 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001885
1886 return 1;
1887}
1888
1889static void
1890ping_handler(struct weston_surface *surface, uint32_t serial)
1891{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001892 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001893 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001894 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001895
1896 if (!shsurf)
1897 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001898 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001899 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001900
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001901 if (shsurf->surface == shsurf->shell->grab_surface)
1902 return;
1903
Scott Moreauff1db4a2012-04-17 19:06:18 -06001904 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001905 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001906 if (!shsurf->ping_timer)
1907 return;
1908
1909 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001910 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1911 shsurf->ping_timer->source =
1912 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1913 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1914
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001915 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001916 }
1917}
1918
1919static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001920handle_pointer_focus(struct wl_listener *listener, void *data)
1921{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001922 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001923 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001924 struct weston_compositor *compositor;
1925 struct shell_surface *shsurf;
1926 uint32_t serial;
1927
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001929 return;
1930
Jason Ekstranda7af7042013-10-12 22:38:11 -05001931 compositor = view->surface->compositor;
1932 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001933
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001934 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001935 set_busy_cursor(shsurf, pointer);
1936 } else {
1937 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001938 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001939 }
1940}
1941
1942static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001943create_pointer_focus_listener(struct weston_seat *seat)
1944{
1945 struct wl_listener *listener;
1946
Kristian Høgsberge3148752013-05-06 23:19:49 -04001947 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001948 return;
1949
1950 listener = malloc(sizeof *listener);
1951 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001952 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001953}
1954
1955static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001956shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1957 uint32_t serial)
1958{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001959 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001960 struct weston_seat *seat;
1961 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001962
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001963 if (shsurf->ping_timer == NULL)
1964 /* Just ignore unsolicited pong. */
1965 return;
1966
Scott Moreauff1db4a2012-04-17 19:06:18 -06001967 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001968 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001969 wl_list_for_each(seat, &ec->seat_list, link) {
1970 if(seat->pointer)
1971 end_busy_cursor(shsurf, seat->pointer);
1972 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001973 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001974 }
1975}
1976
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001977static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001978set_title(struct shell_surface *shsurf, const char *title)
1979{
1980 free(shsurf->title);
1981 shsurf->title = strdup(title);
1982}
1983
1984static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001985shell_surface_set_title(struct wl_client *client,
1986 struct wl_resource *resource, const char *title)
1987{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001988 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001989
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001990 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001991}
1992
1993static void
1994shell_surface_set_class(struct wl_client *client,
1995 struct wl_resource *resource, const char *class)
1996{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001997 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001998
1999 free(shsurf->class);
2000 shsurf->class = strdup(class);
2001}
2002
Alex Wu4539b082012-03-01 12:57:46 +08002003static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002004restore_output_mode(struct weston_output *output)
2005{
Hardening57388e42013-09-18 23:56:36 +02002006 if (output->current_mode != output->original_mode ||
2007 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002008 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002009 output->original_mode,
2010 output->original_scale,
2011 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002012}
2013
2014static void
2015restore_all_output_modes(struct weston_compositor *compositor)
2016{
2017 struct weston_output *output;
2018
2019 wl_list_for_each(output, &compositor->output_list, link)
2020 restore_output_mode(output);
2021}
2022
Philip Withnallbecb77e2013-11-25 18:01:30 +00002023static int
2024get_output_panel_height(struct desktop_shell *shell,
2025 struct weston_output *output)
2026{
2027 struct weston_view *view;
2028 int panel_height = 0;
2029
2030 if (!output)
2031 return 0;
2032
2033 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2034 if (view->surface->output == output) {
2035 panel_height = view->geometry.height;
2036 break;
2037 }
2038 }
2039
2040 return panel_height;
2041}
2042
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002043static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002044shell_surface_set_parent(struct shell_surface *shsurf,
2045 struct weston_surface *parent)
2046{
2047 shsurf->parent = parent;
2048}
2049
2050static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002051shell_surface_set_output(struct shell_surface *shsurf,
2052 struct weston_output *output)
2053{
2054 struct weston_surface *es = shsurf->surface;
2055
2056 /* get the default output, if the client set it as NULL
2057 check whether the ouput is available */
2058 if (output)
2059 shsurf->output = output;
2060 else if (es->output)
2061 shsurf->output = es->output;
2062 else
2063 shsurf->output = get_default_output(es->compositor);
2064}
2065
2066static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002067set_toplevel(struct shell_surface *shsurf)
2068{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002069 shell_surface_set_parent(shsurf, NULL);
2070
Philip Withnallbecb77e2013-11-25 18:01:30 +00002071 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
2072}
2073
2074static void
2075shell_surface_set_toplevel(struct wl_client *client,
2076 struct wl_resource *resource)
2077{
2078 struct shell_surface *surface = wl_resource_get_user_data(resource);
2079
2080 set_toplevel(surface);
2081}
2082
2083static void
2084set_transient(struct shell_surface *shsurf,
2085 struct weston_surface *parent, int x, int y, uint32_t flags)
2086{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002087 assert(parent != NULL);
2088
Philip Withnallbecb77e2013-11-25 18:01:30 +00002089 shsurf->transient.x = x;
2090 shsurf->transient.y = y;
2091 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002092
2093 shell_surface_set_parent(shsurf, parent);
2094
Philip Withnallbecb77e2013-11-25 18:01:30 +00002095 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2096}
2097
2098static void
2099shell_surface_set_transient(struct wl_client *client,
2100 struct wl_resource *resource,
2101 struct wl_resource *parent_resource,
2102 int x, int y, uint32_t flags)
2103{
2104 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2105 struct weston_surface *parent =
2106 wl_resource_get_user_data(parent_resource);
2107
2108 set_transient(shsurf, parent, x, y, flags);
2109}
2110
2111static void
2112set_fullscreen(struct shell_surface *shsurf,
2113 uint32_t method,
2114 uint32_t framerate,
2115 struct weston_output *output)
2116{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002117 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002118
2119 shsurf->fullscreen_output = shsurf->output;
2120 shsurf->fullscreen.type = method;
2121 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002122
2123 shell_surface_set_parent(shsurf, NULL);
2124
Philip Withnallbecb77e2013-11-25 18:01:30 +00002125 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
2126
2127 shsurf->client->send_configure(shsurf->surface, 0,
2128 shsurf->output->width,
2129 shsurf->output->height);
2130}
2131
2132static void
2133unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002134{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002135 struct workspace *ws;
Philip Withnallf85fe842013-11-25 18:01:37 +00002136
2137 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002138 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2139 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002140 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002141 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002142 shsurf->fullscreen_output = NULL;
2143
Alex Wu4539b082012-03-01 12:57:46 +08002144 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2145 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002146
Alex Wu4539b082012-03-01 12:57:46 +08002147 wl_list_remove(&shsurf->fullscreen.transform.link);
2148 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002149
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 if (shsurf->fullscreen.black_view)
2151 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2152 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002153
Jason Ekstranda7af7042013-10-12 22:38:11 -05002154 weston_view_set_position(shsurf->view,
2155 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002156 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002158 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002159 shsurf->saved_rotation_valid = false;
2160 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002161
2162 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163 wl_list_remove(&shsurf->view->layer_link);
2164 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08002165}
2166
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002167static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002168shell_surface_set_fullscreen(struct wl_client *client,
2169 struct wl_resource *resource,
2170 uint32_t method,
2171 uint32_t framerate,
2172 struct wl_resource *output_resource)
2173{
2174 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2175 struct weston_output *output;
2176
2177 if (output_resource)
2178 output = wl_resource_get_user_data(output_resource);
2179 else
2180 output = NULL;
2181
2182 set_fullscreen(shsurf, method, framerate, output);
2183}
2184
2185static void
2186set_popup(struct shell_surface *shsurf,
2187 struct weston_surface *parent,
2188 struct weston_seat *seat,
2189 uint32_t serial,
2190 int32_t x,
2191 int32_t y)
2192{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002193 assert(parent != NULL);
2194
Philip Withnallbecb77e2013-11-25 18:01:30 +00002195 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002196 shsurf->popup.shseat = get_shell_seat(seat);
2197 shsurf->popup.serial = serial;
2198 shsurf->popup.x = x;
2199 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002200
2201 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002202}
2203
2204static void
2205shell_surface_set_popup(struct wl_client *client,
2206 struct wl_resource *resource,
2207 struct wl_resource *seat_resource,
2208 uint32_t serial,
2209 struct wl_resource *parent_resource,
2210 int32_t x, int32_t y, uint32_t flags)
2211{
2212 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2213
2214 set_popup(shsurf,
2215 wl_resource_get_user_data(parent_resource),
2216 wl_resource_get_user_data(seat_resource),
2217 serial, x, y);
2218}
2219
2220static void
2221set_maximized(struct shell_surface *shsurf,
2222 struct weston_output *output)
2223{
2224 struct desktop_shell *shell;
2225 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002226
Philip Withnall352e7ed2013-11-25 18:01:35 +00002227 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002228
2229 shell = shell_surface_get_shell(shsurf);
2230 panel_height = get_output_panel_height(shell, shsurf->output);
2231 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2232
2233 shsurf->client->send_configure(shsurf->surface, edges,
2234 shsurf->output->width,
2235 shsurf->output->height - panel_height);
2236
Philip Withnalldc4332f2013-11-25 18:01:38 +00002237 shell_surface_set_parent(shsurf, NULL);
2238
Philip Withnallbecb77e2013-11-25 18:01:30 +00002239 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
2240}
2241
2242static void
2243unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002244{
2245 struct workspace *ws;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002246
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002247 /* undo all maximized things here */
2248 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249 weston_view_set_position(shsurf->view,
2250 shsurf->saved_x,
2251 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002252
2253 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002254 wl_list_insert(&shsurf->view->geometry.transformation_list,
2255 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002256 shsurf->saved_rotation_valid = false;
2257 }
2258
2259 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002260 wl_list_remove(&shsurf->view->layer_link);
2261 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002262}
2263
Philip Withnallbecb77e2013-11-25 18:01:30 +00002264static void
2265shell_surface_set_maximized(struct wl_client *client,
2266 struct wl_resource *resource,
2267 struct wl_resource *output_resource)
2268{
2269 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2270 struct weston_output *output;
2271
2272 if (output_resource)
2273 output = wl_resource_get_user_data(output_resource);
2274 else
2275 output = NULL;
2276
2277 set_maximized(shsurf, output);
2278}
2279
Pekka Paalanen98262232011-12-01 10:42:22 +02002280static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002281reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002282{
2283 switch (surface->type) {
2284 case SHELL_SURFACE_FULLSCREEN:
Philip Withnallbecb77e2013-11-25 18:01:30 +00002285 unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002286 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002287 case SHELL_SURFACE_MAXIMIZED:
Philip Withnallbecb77e2013-11-25 18:01:30 +00002288 unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002289 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002290 case SHELL_SURFACE_NONE:
2291 case SHELL_SURFACE_TOPLEVEL:
2292 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002293 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002294 case SHELL_SURFACE_XWAYLAND:
Philip Withnall0f640e22013-11-25 18:01:31 +00002295 default:
Pekka Paalanen98262232011-12-01 10:42:22 +02002296 break;
2297 }
2298
2299 surface->type = SHELL_SURFACE_NONE;
2300 return 0;
2301}
2302
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002303static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002304set_surface_type(struct shell_surface *shsurf)
2305{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002306 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002307 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002308
Philip Withnallbecb77e2013-11-25 18:01:30 +00002309 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002310
2311 shsurf->type = shsurf->next_type;
2312 shsurf->next_type = SHELL_SURFACE_NONE;
2313
2314 switch (shsurf->type) {
2315 case SHELL_SURFACE_TOPLEVEL:
2316 break;
2317 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002318 if (pev)
2319 weston_view_set_position(shsurf->view,
2320 pev->geometry.x + shsurf->transient.x,
2321 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002322 break;
2323
2324 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002325 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002326 shsurf->saved_x = shsurf->view->geometry.x;
2327 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002328 shsurf->saved_position_valid = true;
2329
2330 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2331 wl_list_remove(&shsurf->rotation.transform.link);
2332 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002333 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002334 shsurf->saved_rotation_valid = true;
2335 }
2336 break;
2337
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002338 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002339 weston_view_set_position(shsurf->view, shsurf->transient.x,
2340 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002341 break;
2342
Philip Withnall0f640e22013-11-25 18:01:31 +00002343 case SHELL_SURFACE_POPUP:
2344 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002345 default:
2346 break;
2347 }
2348}
2349
Tiago Vignattibe143262012-04-16 17:31:41 +03002350static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002351shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002352{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002353 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002354}
2355
Alex Wu21858432012-04-01 20:13:08 +08002356static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002357black_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 +08002358
Jason Ekstranda7af7042013-10-12 22:38:11 -05002359static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002360create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002361 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002362 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002363{
2364 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002365 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002366
2367 surface = weston_surface_create(ec);
2368 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002369 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002370 return NULL;
2371 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002372 view = weston_view_create(surface);
2373 if (surface == NULL) {
2374 weston_log("no memory\n");
2375 weston_surface_destroy(surface);
2376 return NULL;
2377 }
Alex Wu4539b082012-03-01 12:57:46 +08002378
Alex Wu21858432012-04-01 20:13:08 +08002379 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002380 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002381 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002382 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002383 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002384 pixman_region32_fini(&surface->input);
2385 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002386 surface->width = w;
2387 surface->height = h;
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002388
Jason Ekstranda7af7042013-10-12 22:38:11 -05002389 weston_view_configure(view, x, y, w, h);
2390
2391 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002392}
2393
2394/* Create black surface and append it to the associated fullscreen surface.
2395 * Handle size dismatch and positioning according to the method. */
2396static void
2397shell_configure_fullscreen(struct shell_surface *shsurf)
2398{
2399 struct weston_output *output = shsurf->fullscreen_output;
2400 struct weston_surface *surface = shsurf->surface;
2401 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002402 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002403 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002404
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002405 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2406 restore_output_mode(output);
2407
Jason Ekstranda7af7042013-10-12 22:38:11 -05002408 if (!shsurf->fullscreen.black_view)
2409 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002410 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002411 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002412 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002413 output->width,
2414 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002415
Jason Ekstranda7af7042013-10-12 22:38:11 -05002416 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2417 wl_list_insert(&shsurf->view->layer_link,
2418 &shsurf->fullscreen.black_view->layer_link);
2419 shsurf->fullscreen.black_view->surface->output = output;
2420 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002421
Jason Ekstranda7af7042013-10-12 22:38:11 -05002422 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002423 &surf_width, &surf_height);
2424
Alex Wu4539b082012-03-01 12:57:46 +08002425 switch (shsurf->fullscreen.type) {
2426 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002427 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002428 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002429 break;
2430 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002431 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002432 if (output->width == surf_width &&
2433 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434 weston_view_set_position(shsurf->view,
2435 output->x - surf_x,
2436 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002437 break;
2438 }
2439
Alex Wu4539b082012-03-01 12:57:46 +08002440 matrix = &shsurf->fullscreen.transform.matrix;
2441 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002442
Scott Moreau1bad5db2012-08-18 01:04:05 -06002443 output_aspect = (float) output->width /
2444 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002445 /* XXX: Use surf_width and surf_height here? */
2446 surface_aspect = (float) surface->width /
2447 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002448 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002449 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002450 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002451 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002452 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002453 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002454
Alex Wu4539b082012-03-01 12:57:46 +08002455 weston_matrix_scale(matrix, scale, scale, 1);
2456 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002457 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002458 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002459 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2460 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002461 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002462
Alex Wu4539b082012-03-01 12:57:46 +08002463 break;
2464 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002465 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002466 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002467 surf_width * surface->buffer_viewport.scale,
2468 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002469 shsurf->fullscreen.framerate};
2470
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002471 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002472 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002473 weston_view_set_position(shsurf->view,
2474 output->x - surf_x,
2475 output->y - surf_y);
2476 weston_view_configure(shsurf->fullscreen.black_view,
2477 output->x - surf_x,
2478 output->y - surf_y,
2479 output->width,
2480 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002481 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002482 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002483 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002484 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002485 }
Alex Wubd3354b2012-04-17 17:20:49 +08002486 }
Alex Wu4539b082012-03-01 12:57:46 +08002487 break;
2488 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002489 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002490 break;
2491 default:
2492 break;
2493 }
2494}
2495
2496/* make the fullscreen and black surface at the top */
2497static void
2498shell_stack_fullscreen(struct shell_surface *shsurf)
2499{
Alex Wubd3354b2012-04-17 17:20:49 +08002500 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002501 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002502
Jason Ekstranda7af7042013-10-12 22:38:11 -05002503 wl_list_remove(&shsurf->view->layer_link);
2504 wl_list_insert(&shell->fullscreen_layer.view_list,
2505 &shsurf->view->layer_link);
2506 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002507
Jason Ekstranda7af7042013-10-12 22:38:11 -05002508 if (!shsurf->fullscreen.black_view)
2509 shsurf->fullscreen.black_view =
2510 create_black_surface(shsurf->surface->compositor,
2511 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002512 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002513 output->width,
2514 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002515
Jason Ekstranda7af7042013-10-12 22:38:11 -05002516 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2517 wl_list_insert(&shsurf->view->layer_link,
2518 &shsurf->fullscreen.black_view->layer_link);
2519 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002520}
2521
2522static void
2523shell_map_fullscreen(struct shell_surface *shsurf)
2524{
Alex Wu4539b082012-03-01 12:57:46 +08002525 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002526 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002527}
2528
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002529static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002530set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2531{
2532 /* XXX: using the same fields for transient type */
2533 shsurf->transient.x = x;
2534 shsurf->transient.y = y;
2535 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002536
2537 shell_surface_set_parent(shsurf, NULL);
2538
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002539 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2540}
2541
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002542static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002543
2544static void
2545destroy_shell_seat(struct wl_listener *listener, void *data)
2546{
2547 struct shell_seat *shseat =
2548 container_of(listener,
2549 struct shell_seat, seat_destroy_listener);
2550 struct shell_surface *shsurf, *prev = NULL;
2551
2552 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002553 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002554 shseat->popup_grab.client = NULL;
2555
2556 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2557 shsurf->popup.shseat = NULL;
2558 if (prev) {
2559 wl_list_init(&prev->popup.grab_link);
2560 }
2561 prev = shsurf;
2562 }
2563 wl_list_init(&prev->popup.grab_link);
2564 }
2565
2566 wl_list_remove(&shseat->seat_destroy_listener.link);
2567 free(shseat);
2568}
2569
2570static struct shell_seat *
2571create_shell_seat(struct weston_seat *seat)
2572{
2573 struct shell_seat *shseat;
2574
2575 shseat = calloc(1, sizeof *shseat);
2576 if (!shseat) {
2577 weston_log("no memory to allocate shell seat\n");
2578 return NULL;
2579 }
2580
2581 shseat->seat = seat;
2582 wl_list_init(&shseat->popup_grab.surfaces_list);
2583
2584 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2585 wl_signal_add(&seat->destroy_signal,
2586 &shseat->seat_destroy_listener);
2587
2588 return shseat;
2589}
2590
2591static struct shell_seat *
2592get_shell_seat(struct weston_seat *seat)
2593{
2594 struct wl_listener *listener;
2595
2596 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2597 if (listener == NULL)
2598 return create_shell_seat(seat);
2599
2600 return container_of(listener,
2601 struct shell_seat, seat_destroy_listener);
2602}
2603
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002604static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002605popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002606{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002607 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002608 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002609 struct shell_seat *shseat =
2610 container_of(grab, struct shell_seat, popup_grab.grab);
2611 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002612 wl_fixed_t sx, sy;
2613
Jason Ekstranda7af7042013-10-12 22:38:11 -05002614 view = weston_compositor_pick_view(pointer->seat->compositor,
2615 pointer->x, pointer->y,
2616 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002617
Jason Ekstranda7af7042013-10-12 22:38:11 -05002618 if (view && view->surface->resource &&
2619 wl_resource_get_client(view->surface->resource) == client) {
2620 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002621 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002622 weston_pointer_set_focus(pointer, NULL,
2623 wl_fixed_from_int(0),
2624 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002625 }
2626}
2627
2628static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002629popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2630 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002631{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002632 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002633 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002634 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002635
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002636 weston_pointer_move(pointer, x, y);
2637
Neil Roberts96d790e2013-09-19 17:32:00 +01002638 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639 weston_view_from_global_fixed(pointer->focus,
2640 pointer->x, pointer->y,
2641 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002642 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002643 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002644}
2645
2646static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002647popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002648 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002649{
2650 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002651 struct shell_seat *shseat =
2652 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002653 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002654 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002655 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002656 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002657
Neil Roberts96d790e2013-09-19 17:32:00 +01002658 resource_list = &grab->pointer->focus_resource_list;
2659 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002660 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002661 wl_resource_for_each(resource, resource_list) {
2662 wl_pointer_send_button(resource, serial,
2663 time, button, state);
2664 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002665 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002666 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002667 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002668 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002669 }
2670
Daniel Stone4dbadb12012-05-30 16:31:51 +01002671 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002672 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002673}
2674
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002675static void
2676popup_grab_cancel(struct weston_pointer_grab *grab)
2677{
2678 popup_grab_end(grab->pointer);
2679}
2680
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002681static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002682 popup_grab_focus,
2683 popup_grab_motion,
2684 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002685 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002686};
2687
2688static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002689popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002690{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002691 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002692 struct shell_seat *shseat =
2693 container_of(grab, struct shell_seat, popup_grab.grab);
2694 struct shell_surface *shsurf;
2695 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002696
2697 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002698 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002699 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002700 shseat->popup_grab.grab.interface = NULL;
2701 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002702 /* Send the popup_done event to all the popups open */
2703 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002704 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002705 shsurf->popup.shseat = NULL;
2706 if (prev) {
2707 wl_list_init(&prev->popup.grab_link);
2708 }
2709 prev = shsurf;
2710 }
2711 wl_list_init(&prev->popup.grab_link);
2712 wl_list_init(&shseat->popup_grab.surfaces_list);
2713 }
2714}
2715
2716static void
2717add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2718{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002719 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002720
2721 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002722 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002723 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002724 /* We must make sure here that this popup was opened after
2725 * a mouse press, and not just by moving around with other
2726 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002727 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002728 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002729
Rob Bradforddfe31052013-06-26 19:49:11 +01002730 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002731 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002732 } else {
2733 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002734 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002735}
2736
2737static void
2738remove_popup_grab(struct shell_surface *shsurf)
2739{
2740 struct shell_seat *shseat = shsurf->popup.shseat;
2741
2742 wl_list_remove(&shsurf->popup.grab_link);
2743 wl_list_init(&shsurf->popup.grab_link);
2744 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002745 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002746 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002747 }
2748}
2749
2750static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002751shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002752{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002753 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002754 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002755
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 shsurf->surface->output = parent_view->output;
2757 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002758
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759 weston_view_set_transform_parent(shsurf->view, parent_view);
2760 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2761 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002762
Kristian Høgsberge3148752013-05-06 23:19:49 -04002763 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002764 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002765 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002766 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002767 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002768 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002769}
2770
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002771static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002772 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002773 shell_surface_move,
2774 shell_surface_resize,
2775 shell_surface_set_toplevel,
2776 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002777 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002778 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002779 shell_surface_set_maximized,
2780 shell_surface_set_title,
2781 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002782};
2783
2784static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002785destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002786{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002787 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2788
Giulio Camuffo5085a752013-03-25 21:42:45 +01002789 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2790 remove_popup_grab(shsurf);
2791 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002792
Alex Wubd3354b2012-04-17 17:20:49 +08002793 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002794 shell_surface_is_top_fullscreen(shsurf))
2795 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002796
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797 if (shsurf->fullscreen.black_view)
2798 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002799
Alex Wubd3354b2012-04-17 17:20:49 +08002800 /* As destroy_resource() use wl_list_for_each_safe(),
2801 * we can always remove the listener.
2802 */
2803 wl_list_remove(&shsurf->surface_destroy_listener.link);
2804 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002805 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002806 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002807
Jason Ekstranda7af7042013-10-12 22:38:11 -05002808 weston_view_destroy(shsurf->view);
2809
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002810 wl_list_remove(&shsurf->link);
2811 free(shsurf);
2812}
2813
2814static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002815shell_destroy_shell_surface(struct wl_resource *resource)
2816{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002817 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002818
2819 destroy_shell_surface(shsurf);
2820}
2821
2822static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002823shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002824{
2825 struct shell_surface *shsurf = container_of(listener,
2826 struct shell_surface,
2827 surface_destroy_listener);
2828
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002829 if (shsurf->resource)
2830 wl_resource_destroy(shsurf->resource);
2831 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002832 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002833}
2834
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002835static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002836shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002837
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002838static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002839get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002840{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002841 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002842 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002843 else
2844 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002845}
2846
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002847static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002848create_shell_surface(void *shell, struct weston_surface *surface,
2849 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002850{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002851 struct shell_surface *shsurf;
2852
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002853 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002854 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002855 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002856 }
2857
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002858 shsurf = calloc(1, sizeof *shsurf);
2859 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002860 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002861 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002862 }
2863
Jason Ekstranda7af7042013-10-12 22:38:11 -05002864 shsurf->view = weston_view_create(surface);
2865 if (!shsurf->view) {
2866 weston_log("no memory to allocate shell surface\n");
2867 free(shsurf);
2868 return NULL;
2869 }
2870
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002871 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002872 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002873
Tiago Vignattibc052c92012-04-19 16:18:18 +03002874 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002875 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002876 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002877 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002878 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002879 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2880 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002881 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002882 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002883 wl_list_init(&shsurf->fullscreen.transform.link);
2884
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002885 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002886 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002887 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002888 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002889
2890 /* init link so its safe to always remove it in destroy_shell_surface */
2891 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002892 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002893
Pekka Paalanen460099f2012-01-20 16:48:25 +02002894 /* empty when not in use */
2895 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002896 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002897
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002898 wl_list_init(&shsurf->workspace_transform.link);
2899
Pekka Paalanen98262232011-12-01 10:42:22 +02002900 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002901 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002902
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002903 shsurf->client = client;
2904
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002905 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002906}
2907
Jason Ekstranda7af7042013-10-12 22:38:11 -05002908static struct weston_view *
2909get_primary_view(void *shell, struct shell_surface *shsurf)
2910{
2911 return shsurf->view;
2912}
2913
Tiago Vignattibc052c92012-04-19 16:18:18 +03002914static void
2915shell_get_shell_surface(struct wl_client *client,
2916 struct wl_resource *resource,
2917 uint32_t id,
2918 struct wl_resource *surface_resource)
2919{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002920 struct weston_surface *surface =
2921 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002922 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002923 struct shell_surface *shsurf;
2924
2925 if (get_shell_surface(surface)) {
2926 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002927 WL_DISPLAY_ERROR_INVALID_OBJECT,
2928 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002929 return;
2930 }
2931
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002932 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002933 if (!shsurf) {
2934 wl_resource_post_error(surface_resource,
2935 WL_DISPLAY_ERROR_INVALID_OBJECT,
2936 "surface->configure already set");
2937 return;
2938 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002939
Jason Ekstranda85118c2013-06-27 20:17:02 -05002940 shsurf->resource =
2941 wl_resource_create(client,
2942 &wl_shell_surface_interface, 1, id);
2943 wl_resource_set_implementation(shsurf->resource,
2944 &shell_surface_implementation,
2945 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002946}
2947
2948static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002949 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002950};
2951
Kristian Høgsberg07937562011-04-12 17:25:42 -04002952static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002953shell_fade(struct desktop_shell *shell, enum fade_type type);
2954
2955static int
2956screensaver_timeout(void *data)
2957{
2958 struct desktop_shell *shell = data;
2959
2960 shell_fade(shell, FADE_OUT);
2961
2962 return 1;
2963}
2964
2965static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002966handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002967{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002968 struct desktop_shell *shell =
2969 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002970
Pekka Paalanen18027e52011-12-02 16:31:49 +02002971 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002972
2973 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002974 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002975}
2976
2977static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002978launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002979{
2980 if (shell->screensaver.binding)
2981 return;
2982
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002983 if (!shell->screensaver.path) {
2984 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002985 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002986 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002987
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002988 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002989 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002990 return;
2991 }
2992
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002993 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002994 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002995 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002996 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002997}
2998
2999static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003000terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003001{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003002 if (shell->screensaver.process.pid == 0)
3003 return;
3004
3005 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003006}
3007
3008static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003009configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003010{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003011 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003012
Giulio Camuffo184df502013-02-21 11:29:21 +01003013 if (width == 0)
3014 return;
3015
Jason Ekstranda7af7042013-10-12 22:38:11 -05003016 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3017 if (v->output == ev->output && v != ev) {
3018 weston_view_unmap(v);
3019 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003020 }
3021 }
3022
Jason Ekstranda7af7042013-10-12 22:38:11 -05003023 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003024
Jason Ekstranda7af7042013-10-12 22:38:11 -05003025 if (wl_list_empty(&ev->layer_link)) {
3026 wl_list_insert(&layer->view_list, &ev->layer_link);
3027 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003028 }
3029}
3030
3031static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003032background_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 -04003033{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003034 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003035 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003036
Jason Ekstranda7af7042013-10-12 22:38:11 -05003037 view = container_of(es->views.next, struct weston_view, surface_link);
3038
3039 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003040}
3041
3042static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003043desktop_shell_set_background(struct wl_client *client,
3044 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003045 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003046 struct wl_resource *surface_resource)
3047{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003048 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003049 struct weston_surface *surface =
3050 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003051 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003052
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003053 if (surface->configure) {
3054 wl_resource_post_error(surface_resource,
3055 WL_DISPLAY_ERROR_INVALID_OBJECT,
3056 "surface role already assigned");
3057 return;
3058 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003059
Jason Ekstranda7af7042013-10-12 22:38:11 -05003060 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3061 weston_view_destroy(view);
3062 view = weston_view_create(surface);
3063
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003064 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003065 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003066 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003067 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003068 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003069 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003070 surface->output->width,
3071 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003072}
3073
3074static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003075panel_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 -04003076{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003077 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003078 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003079
Jason Ekstranda7af7042013-10-12 22:38:11 -05003080 view = container_of(es->views.next, struct weston_view, surface_link);
3081
3082 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003083}
3084
3085static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003086desktop_shell_set_panel(struct wl_client *client,
3087 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003088 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003089 struct wl_resource *surface_resource)
3090{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003091 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003092 struct weston_surface *surface =
3093 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003094 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003095
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003096 if (surface->configure) {
3097 wl_resource_post_error(surface_resource,
3098 WL_DISPLAY_ERROR_INVALID_OBJECT,
3099 "surface role already assigned");
3100 return;
3101 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003102
Jason Ekstranda7af7042013-10-12 22:38:11 -05003103 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3104 weston_view_destroy(view);
3105 view = weston_view_create(surface);
3106
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003107 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003108 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003109 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003110 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003111 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003112 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003113 surface->output->width,
3114 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003115}
3116
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003117static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003118lock_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 -04003119{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003120 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003121 struct weston_view *view;
3122
3123 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003124
Giulio Camuffo184df502013-02-21 11:29:21 +01003125 if (width == 0)
3126 return;
3127
Jason Ekstranda7af7042013-10-12 22:38:11 -05003128 surface->width = width;
3129 surface->height = height;
3130 view->geometry.width = width;
3131 view->geometry.height = height;
3132 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003133
3134 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003135 wl_list_insert(&shell->lock_layer.view_list,
3136 &view->layer_link);
3137 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003138 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003139 }
3140}
3141
3142static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003143handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003144{
Tiago Vignattibe143262012-04-16 17:31:41 +03003145 struct desktop_shell *shell =
3146 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003147
Martin Minarik6d118362012-06-07 18:01:59 +02003148 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003149 shell->lock_surface = NULL;
3150}
3151
3152static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003153desktop_shell_set_lock_surface(struct wl_client *client,
3154 struct wl_resource *resource,
3155 struct wl_resource *surface_resource)
3156{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003157 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003158 struct weston_surface *surface =
3159 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003160
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003161 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003162
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003163 if (!shell->locked)
3164 return;
3165
Pekka Paalanen98262232011-12-01 10:42:22 +02003166 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003167
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003168 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003169 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003170 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003171
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003172 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003173 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003174 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003175}
3176
3177static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003178resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003179{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003180 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003181
Pekka Paalanen77346a62011-11-30 16:26:35 +02003182 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003183
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003184 wl_list_remove(&shell->lock_layer.link);
3185 wl_list_insert(&shell->compositor->cursor_layer.link,
3186 &shell->fullscreen_layer.link);
3187 wl_list_insert(&shell->fullscreen_layer.link,
3188 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003189 if (shell->showing_input_panels) {
3190 wl_list_insert(&shell->panel_layer.link,
3191 &shell->input_panel_layer.link);
3192 wl_list_insert(&shell->input_panel_layer.link,
3193 &ws->layer.link);
3194 } else {
3195 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3196 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003197
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003198 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003199
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003200 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003201 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003202 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003203}
3204
3205static void
3206desktop_shell_unlock(struct wl_client *client,
3207 struct wl_resource *resource)
3208{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003209 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003210
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003211 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003212
3213 if (shell->locked)
3214 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003215}
3216
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003217static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003218desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003219 struct wl_resource *resource,
3220 struct wl_resource *surface_resource)
3221{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003222 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003223
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003224 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003225 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003226}
3227
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003228static void
3229desktop_shell_desktop_ready(struct wl_client *client,
3230 struct wl_resource *resource)
3231{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003232 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003233
3234 shell_fade_startup(shell);
3235}
3236
Kristian Høgsberg75840622011-09-06 13:48:16 -04003237static const struct desktop_shell_interface desktop_shell_implementation = {
3238 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003239 desktop_shell_set_panel,
3240 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003241 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003242 desktop_shell_set_grab_surface,
3243 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003244};
3245
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003246static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003247get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003248{
3249 struct shell_surface *shsurf;
3250
3251 shsurf = get_shell_surface(surface);
3252 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003253 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003254 return shsurf->type;
3255}
3256
Kristian Høgsberg75840622011-09-06 13:48:16 -04003257static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003258move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003259{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003260 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003261 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003262 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003263
Pekka Paalanen01388e22013-04-25 13:57:44 +03003264 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003265 if (surface == NULL)
3266 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003267
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003268 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003269 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3270 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003271 return;
3272
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003273 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003274}
3275
3276static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003277touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3278{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003279 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003280 struct weston_surface *surface;
3281 struct shell_surface *shsurf;
3282
3283 surface = weston_surface_get_main_surface(focus);
3284 if (surface == NULL)
3285 return;
3286
3287 shsurf = get_shell_surface(surface);
3288 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3289 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3290 return;
3291
3292 surface_touch_move(shsurf, (struct weston_seat *) seat);
3293}
3294
3295static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003296resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003297{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003298 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003299 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003300 uint32_t edges = 0;
3301 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003302 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003303
Pekka Paalanen01388e22013-04-25 13:57:44 +03003304 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003305 if (surface == NULL)
3306 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003307
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003308 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003309 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3310 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003311 return;
3312
Jason Ekstranda7af7042013-10-12 22:38:11 -05003313 weston_view_from_global(shsurf->view,
3314 wl_fixed_to_int(seat->pointer->grab_x),
3315 wl_fixed_to_int(seat->pointer->grab_y),
3316 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003317
Jason Ekstranda7af7042013-10-12 22:38:11 -05003318 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003319 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003320 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003321 edges |= 0;
3322 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003323 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003324
Jason Ekstranda7af7042013-10-12 22:38:11 -05003325 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003326 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003327 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003328 edges |= 0;
3329 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003330 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003331
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003332 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003333}
3334
3335static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003336surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003337 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003338{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003339 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003340 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003341 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003342 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003343
Pekka Paalanen01388e22013-04-25 13:57:44 +03003344 /* XXX: broken for windows containing sub-surfaces */
3345 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003346 if (surface == NULL)
3347 return;
3348
3349 shsurf = get_shell_surface(surface);
3350 if (!shsurf)
3351 return;
3352
Jason Ekstranda7af7042013-10-12 22:38:11 -05003353 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003354
Jason Ekstranda7af7042013-10-12 22:38:11 -05003355 if (shsurf->view->alpha > 1.0)
3356 shsurf->view->alpha = 1.0;
3357 if (shsurf->view->alpha < step)
3358 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003359
Jason Ekstranda7af7042013-10-12 22:38:11 -05003360 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003361 weston_surface_damage(surface);
3362}
3363
3364static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003365do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003366 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003367{
Daniel Stone37816df2012-05-16 18:45:18 +01003368 struct weston_seat *ws = (struct weston_seat *) seat;
3369 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003370 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003371 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003372
3373 wl_list_for_each(output, &compositor->output_list, link) {
3374 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003375 wl_fixed_to_double(seat->pointer->x),
3376 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003377 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003378 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003379 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003380 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003381 increment = -output->zoom.increment;
3382 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003383 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003384 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003385 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003386 else
3387 increment = 0;
3388
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003389 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003390
Scott Moreaue6603982012-06-11 13:07:51 -06003391 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003392 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003393 else if (output->zoom.level > output->zoom.max_level)
3394 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003395 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003396 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003397 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003398
Scott Moreaue6603982012-06-11 13:07:51 -06003399 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003400
Jason Ekstranda7af7042013-10-12 22:38:11 -05003401 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003402 }
3403 }
3404}
3405
Scott Moreauccbf29d2012-02-22 14:21:41 -07003406static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003407zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003408 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003409{
3410 do_zoom(seat, time, 0, axis, value);
3411}
3412
3413static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003414zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003415 void *data)
3416{
3417 do_zoom(seat, time, key, 0, 0);
3418}
3419
3420static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003421terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003422 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003423{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003424 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003425
Daniel Stone325fc2d2012-05-30 16:31:58 +01003426 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003427}
3428
3429static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003430lower_fullscreen_layer(struct desktop_shell *shell);
3431
3432struct alt_tab {
3433 struct desktop_shell *shell;
3434 struct weston_keyboard_grab grab;
3435
3436 struct wl_list *current;
3437
3438 struct wl_list preview_list;
3439};
3440
3441struct alt_tab_preview {
3442 struct alt_tab *alt_tab;
3443
3444 struct weston_view *view;
3445 struct weston_transform transform;
3446
3447 struct wl_listener listener;
3448
3449 struct wl_list link;
3450};
3451
3452static void
3453alt_tab_next(struct alt_tab *alt_tab)
3454{
3455 alt_tab->current = alt_tab->current->next;
3456
3457 /* Make sure we're not pointing to the list header e.g. after
3458 * cycling through the whole list. */
3459 if (alt_tab->current->next == alt_tab->preview_list.next)
3460 alt_tab->current = alt_tab->current->next;
3461}
3462
3463static void
3464alt_tab_destroy(struct alt_tab *alt_tab)
3465{
3466 struct alt_tab_preview *preview, *next;
3467 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3468
3469 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3470 preview = wl_container_of(alt_tab->current, preview, link);
3471
3472 activate(alt_tab->shell, preview->view->surface,
3473 (struct weston_seat *) keyboard->seat);
3474 }
3475
3476 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3477 wl_list_remove(&preview->link);
3478 wl_list_remove(&preview->listener.link);
3479 weston_view_destroy(preview->view);
3480 free(preview);
3481 }
3482
3483 weston_keyboard_end_grab(keyboard);
3484 if (keyboard->input_method_resource)
3485 keyboard->grab = &keyboard->input_method_grab;
3486
3487 free(alt_tab);
3488}
3489
3490static void
3491alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3492{
3493 struct alt_tab_preview *preview =
3494 container_of(listener, struct alt_tab_preview, listener);
3495 struct alt_tab *alt_tab = preview->alt_tab;
3496 int advance = 0;
3497
3498 /* If the preview that we're removing is the currently selected one,
3499 * we want to move to the next one. So we move to ->prev and then
3500 * call _next() after removing the preview. */
3501 if (alt_tab->current == &preview->link) {
3502 alt_tab->current = alt_tab->current->prev;
3503 advance = 1;
3504 }
3505
3506 wl_list_remove(&preview->listener.link);
3507 wl_list_remove(&preview->link);
3508
3509 free(preview);
3510
3511 if (advance)
3512 alt_tab_next(alt_tab);
3513
3514 /* If the last preview goes away, stop the alt-tab */
3515 if (wl_list_empty(alt_tab->current))
3516 alt_tab_destroy(alt_tab);
3517}
3518
3519static void
3520alt_tab_key(struct weston_keyboard_grab *grab,
3521 uint32_t time, uint32_t key, uint32_t state_w)
3522{
3523 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3524 enum wl_keyboard_key_state state = state_w;
3525
3526 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3527 alt_tab_next(alt_tab);
3528}
3529
3530static void
3531alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3532 uint32_t mods_depressed, uint32_t mods_latched,
3533 uint32_t mods_locked, uint32_t group)
3534{
3535 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3536 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3537
3538 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3539 alt_tab_destroy(alt_tab);
3540}
3541
3542static void
3543alt_tab_cancel(struct weston_keyboard_grab *grab)
3544{
3545 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3546
3547 alt_tab_destroy(alt_tab);
3548}
3549
3550static const struct weston_keyboard_grab_interface alt_tab_grab = {
3551 alt_tab_key,
3552 alt_tab_modifier,
3553 alt_tab_cancel,
3554};
3555
3556static int
3557view_for_alt_tab(struct weston_view *view)
3558{
3559 if (!get_shell_surface(view->surface))
3560 return 0;
3561
3562 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3563 return 0;
3564
3565 if (view != get_default_view(view->surface))
3566 return 0;
3567
3568 return 1;
3569}
3570
3571static void
3572alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3573 void *data)
3574{
3575 struct alt_tab *alt_tab;
3576 struct desktop_shell *shell = data;
3577 struct weston_output *output = get_default_output(shell->compositor);
3578 struct workspace *ws;
3579 struct weston_view *view;
3580 int num_surfaces = 0;
3581 int x, y, side, margin;
3582
3583 wl_list_for_each(view, &shell->compositor->view_list, link) {
3584
3585 if (view_for_alt_tab(view))
3586 num_surfaces++;
3587 }
3588
3589 if (!num_surfaces)
3590 return;
3591
3592 alt_tab = malloc(sizeof *alt_tab);
3593 if (!alt_tab)
3594 return;
3595
3596 alt_tab->shell = shell;
3597 wl_list_init(&alt_tab->preview_list);
3598 alt_tab->current = &alt_tab->preview_list;
3599
3600 restore_all_output_modes(shell->compositor);
3601 lower_fullscreen_layer(alt_tab->shell);
3602
3603 alt_tab->grab.interface = &alt_tab_grab;
3604 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3605 weston_keyboard_set_focus(seat->keyboard, NULL);
3606
3607 ws = get_current_workspace(shell);
3608
3609 /* FIXME: add some visual candy e.g. prelight the selected view
3610 * and/or add a black surrounding background à la gnome-shell */
3611
3612 /* Determine the size for each preview */
3613 side = output->width / num_surfaces;
3614 if (side > 200)
3615 side = 200;
3616 margin = side / 4;
3617 side -= margin;
3618
3619 x = margin;
3620 y = (output->height - side) / 2;
3621
3622 /* Create a view for each surface */
3623 wl_list_for_each(view, &shell->compositor->view_list, link) {
3624 struct alt_tab_preview *preview;
3625 struct weston_view *v;
3626 float scale;
3627
3628 if (!view_for_alt_tab(view))
3629 continue;
3630
3631 preview = malloc(sizeof *preview);
3632 if (!preview) {
3633 alt_tab_destroy(alt_tab);
3634 return;
3635 }
3636
3637 preview->alt_tab = alt_tab;
3638
3639 preview->view = v = weston_view_create(view->surface);
3640 v->output = view->output;
3641 weston_view_restack(v, &ws->layer.view_list);
3642 weston_view_configure(v, x, y, view->geometry.width, view->geometry.height);
3643
3644 preview->listener.notify = alt_tab_handle_surface_destroy;
3645 wl_signal_add(&v->destroy_signal, &preview->listener);
3646
3647 if (view->geometry.width > view->geometry.height)
3648 scale = side / (float) view->geometry.width;
3649 else
3650 scale = side / (float) view->geometry.height;
3651
3652 wl_list_insert(&v->geometry.transformation_list,
3653 &preview->transform.link);
3654 weston_matrix_init(&preview->transform.matrix);
3655 weston_matrix_scale(&preview->transform.matrix,
3656 scale, scale, 1.0f);
3657
3658 weston_view_geometry_dirty(v);
3659 weston_compositor_schedule_repaint(v->surface->compositor);
3660
3661 /* Insert at the end of the list */
3662 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3663
3664 x += side + margin;
3665 }
3666
3667 /* Start at the second preview so a simple <alt>tab changes window.
3668 * We set `current' to the first preview and not the second because
3669 * we're going to receive a key press callback for the initial
3670 * <alt>tab which will make `current' point to the second element. */
3671 alt_tab_next(alt_tab);
3672}
3673
3674static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003675rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3676 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003677{
3678 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003679 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003680 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003681 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003682 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003683
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003684 weston_pointer_move(pointer, x, y);
3685
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003686 if (!shsurf)
3687 return;
3688
Jason Ekstranda7af7042013-10-12 22:38:11 -05003689 cx = 0.5f * shsurf->view->geometry.width;
3690 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003691
Daniel Stone37816df2012-05-16 18:45:18 +01003692 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3693 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003694 r = sqrtf(dx * dx + dy * dy);
3695
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003696 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003697 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003698
3699 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003700 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003701 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003702
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003703 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003704 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003705
3706 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003707 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003708 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003709 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003710 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003711
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003712 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003713 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003714 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003715 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003716 wl_list_init(&shsurf->rotation.transform.link);
3717 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003718 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003719 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003720
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003721 /* We need to adjust the position of the surface
3722 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003723 cposx = shsurf->view->geometry.x + cx;
3724 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003725 dposx = rotate->center.x - cposx;
3726 dposy = rotate->center.y - cposy;
3727 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003728 weston_view_set_position(shsurf->view,
3729 shsurf->view->geometry.x + dposx,
3730 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003731 }
3732
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003733 /* Repaint implies weston_surface_update_transform(), which
3734 * lazily applies the damage due to rotation update.
3735 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003736 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003737}
3738
3739static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003740rotate_grab_button(struct weston_pointer_grab *grab,
3741 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003742{
3743 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003744 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003745 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003746 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003747 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003748
Daniel Stone4dbadb12012-05-30 16:31:51 +01003749 if (pointer->button_count == 0 &&
3750 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003751 if (shsurf)
3752 weston_matrix_multiply(&shsurf->rotation.rotation,
3753 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003754 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003755 free(rotate);
3756 }
3757}
3758
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003759static void
3760rotate_grab_cancel(struct weston_pointer_grab *grab)
3761{
3762 struct rotate_grab *rotate =
3763 container_of(grab, struct rotate_grab, base.grab);
3764
3765 shell_grab_end(&rotate->base);
3766 free(rotate);
3767}
3768
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003769static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003770 noop_grab_focus,
3771 rotate_grab_motion,
3772 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003773 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003774};
3775
3776static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003777surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003778{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003779 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003780 float dx, dy;
3781 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003782
Pekka Paalanen460099f2012-01-20 16:48:25 +02003783 rotate = malloc(sizeof *rotate);
3784 if (!rotate)
3785 return;
3786
Jason Ekstranda7af7042013-10-12 22:38:11 -05003787 weston_view_to_global_float(surface->view,
3788 surface->view->geometry.width * 0.5f,
3789 surface->view->geometry.height * 0.5f,
3790 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003791
Daniel Stone37816df2012-05-16 18:45:18 +01003792 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3793 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003794 r = sqrtf(dx * dx + dy * dy);
3795 if (r > 20.0f) {
3796 struct weston_matrix inverse;
3797
3798 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003799 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003800 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003801
3802 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003803 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003804 } else {
3805 weston_matrix_init(&surface->rotation.rotation);
3806 weston_matrix_init(&rotate->rotation);
3807 }
3808
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003809 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3810 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003811}
3812
3813static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003814rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003815 void *data)
3816{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003817 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003818 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003819 struct shell_surface *surface;
3820
Pekka Paalanen01388e22013-04-25 13:57:44 +03003821 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003822 if (base_surface == NULL)
3823 return;
3824
3825 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003826 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3827 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003828 return;
3829
3830 surface_rotate(surface, seat);
3831}
3832
3833static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003834lower_fullscreen_layer(struct desktop_shell *shell)
3835{
3836 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003837 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003838
3839 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003840 wl_list_for_each_reverse_safe(view, prev,
3841 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003842 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003843 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003844}
3845
3846static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003847activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003848 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003849{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003850 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003851 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003852 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003853 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003854 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003855
Pekka Paalanen01388e22013-04-25 13:57:44 +03003856 main_surface = weston_surface_get_main_surface(es);
3857
Daniel Stone37816df2012-05-16 18:45:18 +01003858 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003859
Jonas Ådahl8538b222012-08-29 22:13:03 +02003860 state = ensure_focus_state(shell, seat);
3861 if (state == NULL)
3862 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003863
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003864 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003865 state->keyboard_focus = es;
3866 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003867 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003868
Pekka Paalanen01388e22013-04-25 13:57:44 +03003869 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003870 case SHELL_SURFACE_FULLSCREEN:
3871 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003872 shell_stack_fullscreen(get_shell_surface(main_surface));
3873 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003874 return;
Philip Withnall0f640e22013-11-25 18:01:31 +00003875 case SHELL_SURFACE_TOPLEVEL:
3876 case SHELL_SURFACE_TRANSIENT:
3877 case SHELL_SURFACE_MAXIMIZED:
3878 case SHELL_SURFACE_POPUP:
3879 case SHELL_SURFACE_XWAYLAND:
3880 case SHELL_SURFACE_NONE:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003881 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003882 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003883 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003884 main_view = get_default_view(main_surface);
3885 if (main_view)
3886 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003887 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003888 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003889
3890 if (shell->focus_animation_type != ANIMATION_NONE)
3891 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003892}
3893
Alex Wu21858432012-04-01 20:13:08 +08003894/* no-op func for checking black surface */
3895static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003896black_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 +08003897{
3898}
3899
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003900static bool
Alex Wu21858432012-04-01 20:13:08 +08003901is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3902{
3903 if (es->configure == black_surface_configure) {
3904 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003905 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003906 return true;
3907 }
3908 return false;
3909}
3910
Kristian Høgsberg75840622011-09-06 13:48:16 -04003911static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003912activate_binding(struct weston_seat *seat,
3913 struct desktop_shell *shell,
3914 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003915{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003916 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003917
Alex Wu9c35e6b2012-03-05 14:13:13 +08003918 if (!focus)
3919 return;
3920
Pekka Paalanen01388e22013-04-25 13:57:44 +03003921 if (is_black_surface(focus, &main_surface))
3922 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003923
Pekka Paalanen01388e22013-04-25 13:57:44 +03003924 main_surface = weston_surface_get_main_surface(focus);
3925 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003926 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003927
Neil Robertsa28c6932013-10-03 16:43:04 +01003928 activate(shell, focus, seat);
3929}
3930
3931static void
3932click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3933 void *data)
3934{
3935 if (seat->pointer->grab != &seat->pointer->default_grab)
3936 return;
3937
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003938 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003939}
3940
3941static void
3942touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3943{
3944 if (seat->touch->grab != &seat->touch->default_grab)
3945 return;
3946
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003947 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003948}
3949
3950static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003951lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003952{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003953 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003954
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003955 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003956 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003957 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003958 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003959
3960 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003961
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003962 /* Hide all surfaces by removing the fullscreen, panel and
3963 * toplevel layers. This way nothing else can show or receive
3964 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003965
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003966 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003967 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003968 if (shell->showing_input_panels)
3969 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003970 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003971 wl_list_insert(&shell->compositor->cursor_layer.link,
3972 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003973
Pekka Paalanen77346a62011-11-30 16:26:35 +02003974 launch_screensaver(shell);
3975
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003976 /* TODO: disable bindings that should not work while locked. */
3977
3978 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003979}
3980
3981static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003982unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003983{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003984 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003985 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003986 return;
3987 }
3988
3989 /* If desktop-shell client has gone away, unlock immediately. */
3990 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003991 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003992 return;
3993 }
3994
3995 if (shell->prepare_event_sent)
3996 return;
3997
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003998 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003999 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004000}
4001
4002static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004003shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004004{
4005 struct desktop_shell *shell = data;
4006
4007 shell->fade.animation = NULL;
4008
4009 switch (shell->fade.type) {
4010 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004011 weston_surface_destroy(shell->fade.view->surface);
4012 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004013 break;
4014 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004015 lock(shell);
4016 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004017 default:
4018 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004019 }
4020}
4021
Jason Ekstranda7af7042013-10-12 22:38:11 -05004022static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004023shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004024{
4025 struct weston_compositor *compositor = shell->compositor;
4026 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004027 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004028
4029 surface = weston_surface_create(compositor);
4030 if (!surface)
4031 return NULL;
4032
Jason Ekstranda7af7042013-10-12 22:38:11 -05004033 view = weston_view_create(surface);
4034 if (!view) {
4035 weston_surface_destroy(surface);
4036 return NULL;
4037 }
4038
4039 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004040 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004041 wl_list_insert(&compositor->fade_layer.view_list,
4042 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004043 pixman_region32_init(&surface->input);
4044
Jason Ekstranda7af7042013-10-12 22:38:11 -05004045 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004046}
4047
4048static void
4049shell_fade(struct desktop_shell *shell, enum fade_type type)
4050{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004051 float tint;
4052
4053 switch (type) {
4054 case FADE_IN:
4055 tint = 0.0;
4056 break;
4057 case FADE_OUT:
4058 tint = 1.0;
4059 break;
4060 default:
4061 weston_log("shell: invalid fade type\n");
4062 return;
4063 }
4064
4065 shell->fade.type = type;
4066
Jason Ekstranda7af7042013-10-12 22:38:11 -05004067 if (shell->fade.view == NULL) {
4068 shell->fade.view = shell_fade_create_surface(shell);
4069 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004070 return;
4071
Jason Ekstranda7af7042013-10-12 22:38:11 -05004072 shell->fade.view->alpha = 1.0 - tint;
4073 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004074 }
4075
4076 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004077 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004078 else
4079 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004080 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004081 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004082 shell_fade_done, shell);
4083}
4084
4085static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004086do_shell_fade_startup(void *data)
4087{
4088 struct desktop_shell *shell = data;
4089
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004090 if (shell->startup_animation_type == ANIMATION_FADE)
4091 shell_fade(shell, FADE_IN);
4092 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004093 weston_surface_destroy(shell->fade.view->surface);
4094 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004095 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004096}
4097
4098static void
4099shell_fade_startup(struct desktop_shell *shell)
4100{
4101 struct wl_event_loop *loop;
4102
4103 if (!shell->fade.startup_timer)
4104 return;
4105
4106 wl_event_source_remove(shell->fade.startup_timer);
4107 shell->fade.startup_timer = NULL;
4108
4109 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4110 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4111}
4112
4113static int
4114fade_startup_timeout(void *data)
4115{
4116 struct desktop_shell *shell = data;
4117
4118 shell_fade_startup(shell);
4119 return 0;
4120}
4121
4122static void
4123shell_fade_init(struct desktop_shell *shell)
4124{
4125 /* Make compositor output all black, and wait for the desktop-shell
4126 * client to signal it is ready, then fade in. The timer triggers a
4127 * fade-in, in case the desktop-shell client takes too long.
4128 */
4129
4130 struct wl_event_loop *loop;
4131
Jason Ekstranda7af7042013-10-12 22:38:11 -05004132 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004133 weston_log("%s: warning: fade surface already exists\n",
4134 __func__);
4135 return;
4136 }
4137
Jason Ekstranda7af7042013-10-12 22:38:11 -05004138 shell->fade.view = shell_fade_create_surface(shell);
4139 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004140 return;
4141
Jason Ekstranda7af7042013-10-12 22:38:11 -05004142 weston_view_update_transform(shell->fade.view);
4143 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004144
4145 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4146 shell->fade.startup_timer =
4147 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4148 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4149}
4150
4151static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004152idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004153{
4154 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004155 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004156
4157 shell_fade(shell, FADE_OUT);
4158 /* lock() is called from shell_fade_done() */
4159}
4160
4161static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004162wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004163{
4164 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004165 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004166
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004167 unlock(shell);
4168}
4169
4170static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004171show_input_panels(struct wl_listener *listener, void *data)
4172{
4173 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004174 container_of(listener, struct desktop_shell,
4175 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004176 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004177
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004178 shell->text_input.surface = (struct weston_surface*)data;
4179
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004180 if (shell->showing_input_panels)
4181 return;
4182
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004183 shell->showing_input_panels = true;
4184
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004185 if (!shell->locked)
4186 wl_list_insert(&shell->panel_layer.link,
4187 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004188
Jason Ekstranda7af7042013-10-12 22:38:11 -05004189 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004190 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004191 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004192 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004193 wl_list_insert(&shell->input_panel_layer.view_list,
4194 &ipsurf->view->layer_link);
4195 weston_view_geometry_dirty(ipsurf->view);
4196 weston_view_update_transform(ipsurf->view);
4197 weston_surface_damage(ipsurf->surface);
4198 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
4199 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004200 }
4201}
4202
4203static void
4204hide_input_panels(struct wl_listener *listener, void *data)
4205{
4206 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004207 container_of(listener, struct desktop_shell,
4208 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004209 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004210
Jan Arne Petersen61381972013-01-31 15:52:21 +01004211 if (!shell->showing_input_panels)
4212 return;
4213
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004214 shell->showing_input_panels = false;
4215
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004216 if (!shell->locked)
4217 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004218
Jason Ekstranda7af7042013-10-12 22:38:11 -05004219 wl_list_for_each_safe(view, next,
4220 &shell->input_panel_layer.view_list, layer_link)
4221 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004222}
4223
4224static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004225update_input_panels(struct wl_listener *listener, void *data)
4226{
4227 struct desktop_shell *shell =
4228 container_of(listener, struct desktop_shell,
4229 update_input_panel_listener);
4230
4231 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4232}
4233
4234static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004235center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004236{
Giulio Camuffob8366642013-04-25 13:57:46 +03004237 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004238 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004239
Jason Ekstranda7af7042013-10-12 22:38:11 -05004240 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004241
4242 x = output->x + (output->width - width) / 2 - surf_x / 2;
4243 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004244
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004246}
4247
4248static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249weston_view_set_initial_position(struct weston_view *view,
4250 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004251{
4252 struct weston_compositor *compositor = shell->compositor;
4253 int ix = 0, iy = 0;
4254 int range_x, range_y;
4255 int dx, dy, x, y, panel_height;
4256 struct weston_output *output, *target_output = NULL;
4257 struct weston_seat *seat;
4258
4259 /* As a heuristic place the new window on the same output as the
4260 * pointer. Falling back to the output containing 0, 0.
4261 *
4262 * TODO: Do something clever for touch too?
4263 */
4264 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004265 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004266 ix = wl_fixed_to_int(seat->pointer->x);
4267 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004268 break;
4269 }
4270 }
4271
4272 wl_list_for_each(output, &compositor->output_list, link) {
4273 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4274 target_output = output;
4275 break;
4276 }
4277 }
4278
4279 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 weston_view_set_position(view, 10 + random() % 400,
4281 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004282 return;
4283 }
4284
4285 /* Valid range within output where the surface will still be onscreen.
4286 * If this is negative it means that the surface is bigger than
4287 * output.
4288 */
4289 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004290 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004291 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05004292 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004293
Rob Bradford4cb88c72012-08-13 15:18:44 +01004294 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004295 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004296 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004297 dx = 0;
4298
4299 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004300 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004301 else
4302 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004303
4304 x = target_output->x + dx;
4305 y = target_output->y + dy;
4306
Jason Ekstranda7af7042013-10-12 22:38:11 -05004307 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004308}
4309
4310static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004311map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004312 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004313{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004314 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004315 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01004316 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004317 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08004318 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004319 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004320
Jason Ekstranda7af7042013-10-12 22:38:11 -05004321 shsurf->view->geometry.width = width;
4322 shsurf->view->geometry.height = height;
4323 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004324
4325 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004327 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004328 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004329 break;
Alex Wu4539b082012-03-01 12:57:46 +08004330 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004331 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004332 shell_map_fullscreen(shsurf);
4333 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004334 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004335 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004336 panel_height = get_output_panel_height(shell, shsurf->output);
4337 surface_subsurfaces_boundingbox(shsurf->surface,
4338 &surf_x, &surf_y, NULL, NULL);
4339 weston_view_set_position(shsurf->view,
4340 shsurf->output->x - surf_x,
4341 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004342 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004343 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004344 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004345 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004346 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004347 weston_view_set_position(shsurf->view,
4348 shsurf->view->geometry.x + sx,
4349 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004350 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004351 case SHELL_SURFACE_TRANSIENT:
4352 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004353 default:
4354 ;
4355 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004356
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02004357 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004358 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004359 case SHELL_SURFACE_POPUP:
4360 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 /* TODO: Handle a parent with multiple views */
4362 parent = get_default_view(shsurf->parent);
4363 if (parent) {
4364 wl_list_remove(&shsurf->view->layer_link);
4365 wl_list_insert(parent->layer_link.prev,
4366 &shsurf->view->layer_link);
4367 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004368 break;
Alex Wu4539b082012-03-01 12:57:46 +08004369 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02004370 case SHELL_SURFACE_NONE:
4371 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004372 case SHELL_SURFACE_XWAYLAND:
Philip Withnall0f640e22013-11-25 18:01:31 +00004373 case SHELL_SURFACE_TOPLEVEL:
4374 case SHELL_SURFACE_MAXIMIZED:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004375 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004376 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004377 wl_list_remove(&shsurf->view->layer_link);
4378 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004379 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004380 }
4381
Jason Ekstranda7af7042013-10-12 22:38:11 -05004382 if (shsurf->type != SHELL_SURFACE_NONE) {
4383 weston_view_update_transform(shsurf->view);
4384 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4385 shsurf->surface->output = shsurf->output;
4386 shsurf->view->output = shsurf->output;
4387 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004388 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004389
Jason Ekstranda7af7042013-10-12 22:38:11 -05004390 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004391 /* XXX: xwayland's using the same fields for transient type */
4392 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004393 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004394 if (shsurf->transient.flags ==
4395 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4396 break;
4397 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004398 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004399 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004400 if (!shell->locked) {
4401 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004403 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004404 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004405 case SHELL_SURFACE_POPUP:
4406 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004407 default:
4408 break;
4409 }
4410
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004412 {
4413 switch (shell->win_animation_type) {
4414 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004416 break;
4417 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004418 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004419 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004420 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004421 default:
4422 break;
4423 }
4424 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004425}
4426
4427static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004428configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004429 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004430{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004431 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4432 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004433 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004434 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004435
Pekka Paalanen77346a62011-11-30 16:26:35 +02004436 shsurf = get_shell_surface(surface);
4437 if (shsurf)
4438 surface_type = shsurf->type;
4439
Jason Ekstranda7af7042013-10-12 22:38:11 -05004440 /* TODO:
4441 * This should probably be changed to be more shell_surface
4442 * dependent
4443 */
4444 wl_list_for_each(view, &surface->views, surface_link)
4445 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004446
4447 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004448 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004449 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004450 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004451 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004452 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004453 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004454 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4455 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004456 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4457 shsurf->view->geometry.y = shsurf->output->y +
4458 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004459 break;
Alex Wu4539b082012-03-01 12:57:46 +08004460 case SHELL_SURFACE_TOPLEVEL:
Philip Withnall0f640e22013-11-25 18:01:31 +00004461 case SHELL_SURFACE_TRANSIENT:
4462 case SHELL_SURFACE_POPUP:
4463 case SHELL_SURFACE_XWAYLAND:
4464 case SHELL_SURFACE_NONE:
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004465 default:
4466 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004467 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004468
Alex Wu4539b082012-03-01 12:57:46 +08004469 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004470 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004471 wl_list_for_each(view, &surface->views, surface_link)
4472 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004473
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004474 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004475 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004476 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004477}
4478
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004479static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004480shell_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 +03004481{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004482 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004483 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004484
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004485 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004486
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004487 if (!weston_surface_is_mapped(es) &&
4488 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004489 remove_popup_grab(shsurf);
4490 }
4491
Giulio Camuffo184df502013-02-21 11:29:21 +01004492 if (width == 0)
4493 return;
4494
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004495 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004496 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004497 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004498 type_changed = 1;
4499 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004500
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004501 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004502 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004503 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004504 shsurf->view->geometry.width != width ||
4505 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004506 float from_x, from_y;
4507 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004508
Jason Ekstranda7af7042013-10-12 22:38:11 -05004509 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4510 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004511 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004512 shsurf->view->geometry.x + to_x - from_x,
4513 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004514 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004515 }
4516}
4517
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004518static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004519
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004520static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004521desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004522{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004523 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004524 struct desktop_shell *shell =
4525 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004526
4527 shell->child.process.pid = 0;
4528 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004529
4530 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4531 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004532 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004533 shell->child.deathstamp = time;
4534 shell->child.deathcount = 0;
4535 }
4536
4537 shell->child.deathcount++;
4538 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004539 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004540 return;
4541 }
4542
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004543 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004544 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004545 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004546}
4547
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004548static void
4549launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004550{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004551 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004552
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004553 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004554 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004555 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004556 desktop_shell_sigchld);
4557
4558 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004559 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004560}
4561
4562static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004563bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4564{
Tiago Vignattibe143262012-04-16 17:31:41 +03004565 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004566 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004567
Jason Ekstranda85118c2013-06-27 20:17:02 -05004568 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4569 if (resource)
4570 wl_resource_set_implementation(resource, &shell_implementation,
4571 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004572}
4573
Kristian Høgsberg75840622011-09-06 13:48:16 -04004574static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004575unbind_desktop_shell(struct wl_resource *resource)
4576{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004577 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004578
4579 if (shell->locked)
4580 resume_desktop(shell);
4581
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004582 shell->child.desktop_shell = NULL;
4583 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004584}
4585
4586static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004587bind_desktop_shell(struct wl_client *client,
4588 void *data, uint32_t version, uint32_t id)
4589{
Tiago Vignattibe143262012-04-16 17:31:41 +03004590 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004591 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004592
Jason Ekstranda85118c2013-06-27 20:17:02 -05004593 resource = wl_resource_create(client, &desktop_shell_interface,
4594 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004595
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004596 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004597 wl_resource_set_implementation(resource,
4598 &desktop_shell_implementation,
4599 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004600 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004601
4602 if (version < 2)
4603 shell_fade_startup(shell);
4604
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004605 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004606 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004607
4608 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4609 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004610 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004611}
4612
Pekka Paalanen6e168112011-11-24 11:34:05 +02004613static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004614screensaver_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 -04004615{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004616 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004617 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004618
Giulio Camuffo184df502013-02-21 11:29:21 +01004619 if (width == 0)
4620 return;
4621
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004622 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004623 if (!shell->locked)
4624 return;
4625
Jason Ekstranda7af7042013-10-12 22:38:11 -05004626 view = container_of(surface->views.next, struct weston_view, surface_link);
4627 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004628
Jason Ekstranda7af7042013-10-12 22:38:11 -05004629 if (wl_list_empty(&view->layer_link)) {
4630 wl_list_insert(shell->lock_layer.view_list.prev,
4631 &view->layer_link);
4632 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004633 wl_event_source_timer_update(shell->screensaver.timer,
4634 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004635 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004636 }
4637}
4638
4639static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004640screensaver_set_surface(struct wl_client *client,
4641 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004642 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004643 struct wl_resource *output_resource)
4644{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004645 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004646 struct weston_surface *surface =
4647 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004648 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004649 struct weston_view *view, *next;
4650
4651 /* Make sure we only have one view */
4652 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4653 weston_view_destroy(view);
4654 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004655
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004656 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004657 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004658 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004659}
4660
4661static const struct screensaver_interface screensaver_implementation = {
4662 screensaver_set_surface
4663};
4664
4665static void
4666unbind_screensaver(struct wl_resource *resource)
4667{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004668 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004669
Pekka Paalanen77346a62011-11-30 16:26:35 +02004670 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004671}
4672
4673static void
4674bind_screensaver(struct wl_client *client,
4675 void *data, uint32_t version, uint32_t id)
4676{
Tiago Vignattibe143262012-04-16 17:31:41 +03004677 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004678 struct wl_resource *resource;
4679
Jason Ekstranda85118c2013-06-27 20:17:02 -05004680 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004681
Pekka Paalanen77346a62011-11-30 16:26:35 +02004682 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004683 wl_resource_set_implementation(resource,
4684 &screensaver_implementation,
4685 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004686 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004687 return;
4688 }
4689
4690 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4691 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004692 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004693}
4694
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004695static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004696input_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 -04004697{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004698 struct input_panel_surface *ip_surface = surface->configure_private;
4699 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004700 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004701 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004702
Giulio Camuffo184df502013-02-21 11:29:21 +01004703 if (width == 0)
4704 return;
4705
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004706 if (!weston_surface_is_mapped(surface)) {
4707 if (!shell->showing_input_panels)
4708 return;
4709
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004710 show_surface = 1;
4711 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004712
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004713 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004714
4715 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004716 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4717 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004718 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004719 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4720 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004721 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004722
Jason Ekstranda7af7042013-10-12 22:38:11 -05004723 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004724
4725 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004726 wl_list_insert(&shell->input_panel_layer.view_list,
4727 &ip_surface->view->layer_link);
4728 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004729 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004730 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004731 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004732}
4733
4734static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004735destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004736{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004737 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4738
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004739 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004740 wl_list_remove(&input_panel_surface->link);
4741
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004742 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004743 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004744
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004745 free(input_panel_surface);
4746}
4747
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004748static struct input_panel_surface *
4749get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004750{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004751 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004752 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004753 } else {
4754 return NULL;
4755 }
4756}
4757
4758static void
4759input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4760{
4761 struct input_panel_surface *ipsurface = container_of(listener,
4762 struct input_panel_surface,
4763 surface_destroy_listener);
4764
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004765 if (ipsurface->resource) {
4766 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004767 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004768 destroy_input_panel_surface(ipsurface);
4769 }
4770}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004771
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004772static struct input_panel_surface *
4773create_input_panel_surface(struct desktop_shell *shell,
4774 struct weston_surface *surface)
4775{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004776 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004777
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004778 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4779 if (!input_panel_surface)
4780 return NULL;
4781
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004782 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004783 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004784
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004785 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004786
4787 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004788 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004789
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004790 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004791 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004792 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004793 &input_panel_surface->surface_destroy_listener);
4794
4795 wl_list_init(&input_panel_surface->link);
4796
4797 return input_panel_surface;
4798}
4799
4800static void
4801input_panel_surface_set_toplevel(struct wl_client *client,
4802 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004803 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004804 uint32_t position)
4805{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004806 struct input_panel_surface *input_panel_surface =
4807 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004808 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004809
4810 wl_list_insert(&shell->input_panel.surfaces,
4811 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004812
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004813 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004814 input_panel_surface->panel = 0;
4815}
4816
4817static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004818input_panel_surface_set_overlay_panel(struct wl_client *client,
4819 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004820{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004821 struct input_panel_surface *input_panel_surface =
4822 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004823 struct desktop_shell *shell = input_panel_surface->shell;
4824
4825 wl_list_insert(&shell->input_panel.surfaces,
4826 &input_panel_surface->link);
4827
4828 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004829}
4830
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004831static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004832 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004833 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004834};
4835
4836static void
4837destroy_input_panel_surface_resource(struct wl_resource *resource)
4838{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004839 struct input_panel_surface *ipsurf =
4840 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004841
4842 destroy_input_panel_surface(ipsurf);
4843}
4844
4845static void
4846input_panel_get_input_panel_surface(struct wl_client *client,
4847 struct wl_resource *resource,
4848 uint32_t id,
4849 struct wl_resource *surface_resource)
4850{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004851 struct weston_surface *surface =
4852 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004853 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004854 struct input_panel_surface *ipsurf;
4855
4856 if (get_input_panel_surface(surface)) {
4857 wl_resource_post_error(surface_resource,
4858 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004859 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004860 return;
4861 }
4862
4863 ipsurf = create_input_panel_surface(shell, surface);
4864 if (!ipsurf) {
4865 wl_resource_post_error(surface_resource,
4866 WL_DISPLAY_ERROR_INVALID_OBJECT,
4867 "surface->configure already set");
4868 return;
4869 }
4870
Jason Ekstranda85118c2013-06-27 20:17:02 -05004871 ipsurf->resource =
4872 wl_resource_create(client,
4873 &wl_input_panel_surface_interface, 1, id);
4874 wl_resource_set_implementation(ipsurf->resource,
4875 &input_panel_surface_implementation,
4876 ipsurf,
4877 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004878}
4879
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004880static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004881 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004882};
4883
4884static void
4885unbind_input_panel(struct wl_resource *resource)
4886{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004887 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004888
4889 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004890}
4891
4892static void
4893bind_input_panel(struct wl_client *client,
4894 void *data, uint32_t version, uint32_t id)
4895{
4896 struct desktop_shell *shell = data;
4897 struct wl_resource *resource;
4898
Jason Ekstranda85118c2013-06-27 20:17:02 -05004899 resource = wl_resource_create(client,
4900 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004901
4902 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004903 wl_resource_set_implementation(resource,
4904 &input_panel_implementation,
4905 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004906 shell->input_panel.binding = resource;
4907 return;
4908 }
4909
4910 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4911 "interface object already bound");
4912 wl_resource_destroy(resource);
4913}
4914
Kristian Høgsberg07045392012-02-19 18:52:44 -05004915struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004916 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004917 struct weston_surface *current;
4918 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004919 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004920};
4921
4922static void
4923switcher_next(struct switcher *switcher)
4924{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004925 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004926 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004927 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004928 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004929
Jason Ekstranda7af7042013-10-12 22:38:11 -05004930 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4931 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004932 case SHELL_SURFACE_TOPLEVEL:
4933 case SHELL_SURFACE_FULLSCREEN:
4934 case SHELL_SURFACE_MAXIMIZED:
4935 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004936 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004937 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004938 next = view->surface;
4939 prev = view->surface;
4940 view->alpha = 0.25;
4941 weston_view_geometry_dirty(view);
4942 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004943 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004944 case SHELL_SURFACE_TRANSIENT:
4945 case SHELL_SURFACE_POPUP:
4946 case SHELL_SURFACE_XWAYLAND:
4947 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05004948 default:
4949 break;
4950 }
Alex Wu1659daa2012-04-01 20:13:09 +08004951
Jason Ekstranda7af7042013-10-12 22:38:11 -05004952 if (is_black_surface(view->surface, NULL)) {
4953 view->alpha = 0.25;
4954 weston_view_geometry_dirty(view);
4955 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004956 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004957 }
4958
4959 if (next == NULL)
4960 next = first;
4961
Alex Wu07b26062012-03-12 16:06:01 +08004962 if (next == NULL)
4963 return;
4964
Kristian Høgsberg07045392012-02-19 18:52:44 -05004965 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004966 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004967
4968 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004969 wl_list_for_each(view, &next->views, surface_link)
4970 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004971
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004972 shsurf = get_shell_surface(switcher->current);
4973 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004974 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004975}
4976
4977static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004978switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004979{
4980 struct switcher *switcher =
4981 container_of(listener, struct switcher, listener);
4982
4983 switcher_next(switcher);
4984}
4985
4986static void
Daniel Stone351eb612012-05-31 15:27:47 -04004987switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004988{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004989 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004990 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004991 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004992
Jason Ekstranda7af7042013-10-12 22:38:11 -05004993 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004994 if (is_focus_view(view))
4995 continue;
4996
Jason Ekstranda7af7042013-10-12 22:38:11 -05004997 view->alpha = 1.0;
4998 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004999 }
5000
Alex Wu07b26062012-03-12 16:06:01 +08005001 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005002 activate(switcher->shell, switcher->current,
5003 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005004 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005005 weston_keyboard_end_grab(keyboard);
5006 if (keyboard->input_method_resource)
5007 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005008 free(switcher);
5009}
5010
5011static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005012switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005013 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005014{
5015 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005016 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005017
Daniel Stonec9785ea2012-05-30 16:31:52 +01005018 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005019 switcher_next(switcher);
5020}
5021
5022static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005023switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005024 uint32_t mods_depressed, uint32_t mods_latched,
5025 uint32_t mods_locked, uint32_t group)
5026{
5027 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005028 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005029
Daniel Stone351eb612012-05-31 15:27:47 -04005030 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5031 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005032}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005033
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005034static void
5035switcher_cancel(struct weston_keyboard_grab *grab)
5036{
5037 struct switcher *switcher = container_of(grab, struct switcher, grab);
5038
5039 switcher_destroy(switcher);
5040}
5041
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005042static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005043 switcher_key,
5044 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005045 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005046};
5047
5048static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005049switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005050 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005051{
Tiago Vignattibe143262012-04-16 17:31:41 +03005052 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005053 struct switcher *switcher;
5054
5055 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005056 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005057 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005058 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005059 wl_list_init(&switcher->listener.link);
5060
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005061 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005062 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005063 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005064 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5065 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005066 switcher_next(switcher);
5067}
5068
Daniel Stonedf8133b2013-11-19 11:37:14 +01005069struct exposay_surface {
5070 struct desktop_shell *shell;
5071 struct weston_surface *surface;
5072 struct weston_view *view;
5073 struct wl_list link;
5074
5075 int x;
5076 int y;
5077 int width;
5078 int height;
5079 double scale;
5080
5081 int row;
5082 int column;
5083
5084 /* The animations only apply a transformation for their own lifetime,
5085 * and don't have an option to indefinitely maintain the
5086 * transformation in a steady state - so, we apply our own once the
5087 * animation has finished. */
5088 struct weston_transform transform;
5089};
5090
5091static void exposay_set_state(struct desktop_shell *shell,
5092 enum exposay_target_state state,
5093 struct weston_seat *seat);
5094static void exposay_check_state(struct desktop_shell *shell);
5095
5096static void
5097exposay_in_flight_inc(struct desktop_shell *shell)
5098{
5099 shell->exposay.in_flight++;
5100}
5101
5102static void
5103exposay_in_flight_dec(struct desktop_shell *shell)
5104{
5105 if (--shell->exposay.in_flight > 0)
5106 return;
5107
5108 exposay_check_state(shell);
5109}
5110
5111static void
5112exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5113{
5114 struct exposay_surface *esurface = data;
5115
5116 wl_list_insert(&esurface->view->geometry.transformation_list,
5117 &esurface->transform.link);
5118 weston_matrix_init(&esurface->transform.matrix);
5119 weston_matrix_scale(&esurface->transform.matrix,
5120 esurface->scale, esurface->scale, 1.0f);
5121 weston_matrix_translate(&esurface->transform.matrix,
5122 esurface->x - esurface->view->geometry.x,
5123 esurface->y - esurface->view->geometry.y,
5124 0);
5125
5126 weston_view_geometry_dirty(esurface->view);
5127 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5128
5129 exposay_in_flight_dec(esurface->shell);
5130}
5131
5132static void
5133exposay_animate_in(struct exposay_surface *esurface)
5134{
5135 exposay_in_flight_inc(esurface->shell);
5136
5137 weston_move_scale_run(esurface->view,
5138 esurface->x - esurface->view->geometry.x,
5139 esurface->y - esurface->view->geometry.y,
5140 1.0, esurface->scale, 0,
5141 exposay_animate_in_done, esurface);
5142}
5143
5144static void
5145exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5146{
5147 struct exposay_surface *esurface = data;
5148 struct desktop_shell *shell = esurface->shell;
5149
5150 wl_list_remove(&esurface->link);
5151 free(esurface);
5152
5153 exposay_in_flight_dec(shell);
5154}
5155
5156static void
5157exposay_animate_out(struct exposay_surface *esurface)
5158{
5159 exposay_in_flight_inc(esurface->shell);
5160
5161 /* Remove the static transformation set up by
5162 * exposay_transform_in_done(). */
5163 wl_list_remove(&esurface->transform.link);
5164 weston_view_geometry_dirty(esurface->view);
5165
5166 weston_move_scale_run(esurface->view,
5167 esurface->x - esurface->view->geometry.x,
5168 esurface->y - esurface->view->geometry.y,
5169 1.0, esurface->scale, 1,
5170 exposay_animate_out_done, esurface);
5171}
5172
5173static void
5174exposay_highlight_surface(struct desktop_shell *shell,
5175 struct exposay_surface *esurface)
5176{
5177 struct weston_view *view = NULL;
5178
5179 if (esurface) {
5180 shell->exposay.row_current = esurface->row;
5181 shell->exposay.column_current = esurface->column;
5182 view = esurface->view;
5183 }
5184
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005185 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005186 shell->exposay.focus_current = view;
5187}
5188
5189static int
5190exposay_is_animating(struct desktop_shell *shell)
5191{
5192 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5193 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5194 return 0;
5195
5196 return (shell->exposay.in_flight > 0);
5197}
5198
5199static void
5200exposay_pick(struct desktop_shell *shell, int x, int y)
5201{
5202 struct exposay_surface *esurface;
5203
5204 if (exposay_is_animating(shell))
5205 return;
5206
5207 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5208 if (x < esurface->x || x > esurface->x + esurface->width)
5209 continue;
5210 if (y < esurface->y || y > esurface->y + esurface->height)
5211 continue;
5212
5213 exposay_highlight_surface(shell, esurface);
5214 return;
5215 }
5216}
5217
5218/* Pretty lame layout for now; just tries to make a square. Should take
5219 * aspect ratio into account really. Also needs to be notified of surface
5220 * addition and removal and adjust layout/animate accordingly. */
5221static enum exposay_layout_state
5222exposay_layout(struct desktop_shell *shell)
5223{
5224 struct workspace *workspace = shell->exposay.workspace;
5225 struct weston_compositor *compositor = shell->compositor;
5226 struct weston_output *output = get_default_output(compositor);
5227 struct weston_view *view;
5228 struct exposay_surface *esurface;
5229 int w, h;
5230 int i;
5231 int last_row_removed = 0;
5232
5233 wl_list_init(&shell->exposay.surface_list);
5234
5235 shell->exposay.num_surfaces = 0;
5236 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5237 if (!get_shell_surface(view->surface))
5238 continue;
5239 shell->exposay.num_surfaces++;
5240 }
5241
5242 if (shell->exposay.num_surfaces == 0) {
5243 shell->exposay.grid_size = 0;
5244 shell->exposay.hpadding_outer = 0;
5245 shell->exposay.vpadding_outer = 0;
5246 shell->exposay.padding_inner = 0;
5247 shell->exposay.surface_size = 0;
5248 return EXPOSAY_LAYOUT_OVERVIEW;
5249 }
5250
5251 /* Lay the grid out as square as possible, losing surfaces from the
5252 * bottom row if required. Start with fixed padding of a 10% margin
5253 * around the outside and 80px internal padding between surfaces, and
5254 * maximise the area made available to surfaces after this, but only
5255 * to a maximum of 1/3rd the total output size.
5256 *
5257 * If we can't make a square grid, add one extra row at the bottom
5258 * which will have a smaller number of columns.
5259 *
5260 * XXX: Surely there has to be a better way to express this maths,
5261 * right?!
5262 */
5263 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5264 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5265 shell->exposay.grid_size++;
5266 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5267
5268 shell->exposay.hpadding_outer = (output->width / 10);
5269 shell->exposay.vpadding_outer = (output->height / 10);
5270 shell->exposay.padding_inner = 80;
5271
5272 w = output->width - (shell->exposay.hpadding_outer * 2);
5273 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5274 w /= shell->exposay.grid_size;
5275
5276 h = output->height - (shell->exposay.vpadding_outer * 2);
5277 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5278 h /= shell->exposay.grid_size;
5279
5280 shell->exposay.surface_size = (w < h) ? w : h;
5281 if (shell->exposay.surface_size > (output->width / 2))
5282 shell->exposay.surface_size = output->width / 2;
5283 if (shell->exposay.surface_size > (output->height / 2))
5284 shell->exposay.surface_size = output->height / 2;
5285
5286 i = 0;
5287 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5288 int pad;
5289
5290 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5291
5292 if (!get_shell_surface(view->surface))
5293 continue;
5294
5295 esurface = malloc(sizeof(*esurface));
5296 if (!esurface) {
5297 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5298 shell->exposay.seat);
5299 break;
5300 }
5301
5302 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5303 esurface->shell = shell;
5304 esurface->view = view;
5305
5306 esurface->row = i / shell->exposay.grid_size;
5307 esurface->column = i % shell->exposay.grid_size;
5308
5309 esurface->x = shell->exposay.hpadding_outer;
5310 esurface->x += pad * esurface->column;
5311 esurface->y = shell->exposay.vpadding_outer;
5312 esurface->y += pad * esurface->row;
5313
5314 if (esurface->row == shell->exposay.grid_size - 1)
5315 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5316
5317 if (view->geometry.width > view->geometry.height)
5318 esurface->scale = shell->exposay.surface_size / (float) view->geometry.width;
5319 else
5320 esurface->scale = shell->exposay.surface_size / (float) view->geometry.height;
5321 esurface->width = view->geometry.width * esurface->scale;
5322 esurface->height = view->geometry.height * esurface->scale;
5323
5324 if (shell->exposay.focus_current == esurface->view)
5325 exposay_highlight_surface(shell, esurface);
5326
5327 exposay_animate_in(esurface);
5328
5329 i++;
5330 }
5331
5332 weston_compositor_schedule_repaint(shell->compositor);
5333
5334 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5335}
5336
5337static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005338exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5339 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005340{
5341 struct desktop_shell *shell =
5342 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5343
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005344 weston_pointer_move(grab->pointer, x, y);
5345
Daniel Stonedf8133b2013-11-19 11:37:14 +01005346 exposay_pick(shell,
5347 wl_fixed_to_int(grab->pointer->x),
5348 wl_fixed_to_int(grab->pointer->y));
5349}
5350
5351static void
5352exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5353 uint32_t state_w)
5354{
5355 struct desktop_shell *shell =
5356 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5357 struct weston_seat *seat = grab->pointer->seat;
5358 enum wl_pointer_button_state state = state_w;
5359
5360 if (button != BTN_LEFT)
5361 return;
5362
5363 /* Store the surface we clicked on, and don't do anything if we end up
5364 * releasing on a different surface. */
5365 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5366 shell->exposay.clicked = shell->exposay.focus_current;
5367 return;
5368 }
5369
5370 if (shell->exposay.focus_current == shell->exposay.clicked)
5371 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5372 else
5373 shell->exposay.clicked = NULL;
5374}
5375
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005376static void
5377exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
5378{
5379 struct desktop_shell *shell =
5380 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5381
5382 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5383}
5384
Daniel Stonedf8133b2013-11-19 11:37:14 +01005385static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5386 noop_grab_focus,
5387 exposay_motion,
5388 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005389 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005390};
5391
5392static int
5393exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5394{
5395 struct exposay_surface *esurface;
5396
5397 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5398 if (esurface->row != row || esurface->column != column)
5399 continue;
5400
5401 exposay_highlight_surface(shell, esurface);
5402 return 1;
5403 }
5404
5405 return 0;
5406}
5407
5408static void
5409exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5410 uint32_t state_w)
5411{
5412 struct weston_seat *seat = grab->keyboard->seat;
5413 struct desktop_shell *shell =
5414 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5415 enum wl_keyboard_key_state state = state_w;
5416
5417 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5418 return;
5419
5420 switch (key) {
5421 case KEY_ESC:
5422 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5423 break;
5424 case KEY_ENTER:
5425 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5426 break;
5427 case KEY_UP:
5428 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5429 shell->exposay.column_current);
5430 break;
5431 case KEY_DOWN:
5432 /* Special case for trying to move to the bottom row when it
5433 * has fewer items than all the others. */
5434 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5435 shell->exposay.column_current) &&
5436 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5437 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5438 (shell->exposay.num_surfaces %
5439 shell->exposay.grid_size) - 1);
5440 }
5441 break;
5442 case KEY_LEFT:
5443 exposay_maybe_move(shell, shell->exposay.row_current,
5444 shell->exposay.column_current - 1);
5445 break;
5446 case KEY_RIGHT:
5447 exposay_maybe_move(shell, shell->exposay.row_current,
5448 shell->exposay.column_current + 1);
5449 break;
5450 case KEY_TAB:
5451 /* Try to move right, then down (and to the leftmost column),
5452 * then if all else fails, to the top left. */
5453 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5454 shell->exposay.column_current + 1) &&
5455 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5456 exposay_maybe_move(shell, 0, 0);
5457 break;
5458 default:
5459 break;
5460 }
5461}
5462
5463static void
5464exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5465 uint32_t mods_depressed, uint32_t mods_latched,
5466 uint32_t mods_locked, uint32_t group)
5467{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005468 struct desktop_shell *shell =
5469 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5470 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5471
5472 /* We want to know when mod has been pressed and released.
5473 * FIXME: There is a problem here: if mod is pressed, then a key
5474 * is pressed and released, then mod is released, we will treat that
5475 * as if only mod had been pressed and released. */
5476 if (seat->modifier_state) {
5477 if (seat->modifier_state == shell->binding_modifier) {
5478 shell->exposay.mod_pressed = true;
5479 } else {
5480 shell->exposay.mod_invalid = true;
5481 }
5482 } else {
5483 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
5484 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5485
5486 shell->exposay.mod_invalid = false;
5487 shell->exposay.mod_pressed = false;
5488 }
5489
5490 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005491}
5492
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005493static void
5494exposay_cancel(struct weston_keyboard_grab *grab)
5495{
5496 struct desktop_shell *shell =
5497 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5498
5499 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5500}
5501
Daniel Stonedf8133b2013-11-19 11:37:14 +01005502static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5503 exposay_key,
5504 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005505 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005506};
5507
5508/**
5509 * Called when the transition from overview -> inactive has completed.
5510 */
5511static enum exposay_layout_state
5512exposay_set_inactive(struct desktop_shell *shell)
5513{
5514 struct weston_seat *seat = shell->exposay.seat;
5515
5516 weston_keyboard_end_grab(seat->keyboard);
5517 weston_pointer_end_grab(seat->pointer);
5518 if (seat->keyboard->input_method_resource)
5519 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5520
5521 return EXPOSAY_LAYOUT_INACTIVE;
5522}
5523
5524/**
5525 * Begins the transition from overview to inactive. */
5526static enum exposay_layout_state
5527exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5528{
5529 struct exposay_surface *esurface;
5530
5531 /* Call activate() before we start the animations to avoid
5532 * animating back the old state and then immediately transitioning
5533 * to the new. */
5534 if (switch_focus && shell->exposay.focus_current)
5535 activate(shell, shell->exposay.focus_current->surface,
5536 shell->exposay.seat);
5537 else if (shell->exposay.focus_prev)
5538 activate(shell, shell->exposay.focus_prev->surface,
5539 shell->exposay.seat);
5540
5541 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5542 exposay_animate_out(esurface);
5543 weston_compositor_schedule_repaint(shell->compositor);
5544
5545 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5546}
5547
5548static enum exposay_layout_state
5549exposay_transition_active(struct desktop_shell *shell)
5550{
5551 struct weston_seat *seat = shell->exposay.seat;
5552
5553 shell->exposay.workspace = get_current_workspace(shell);
5554 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5555 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5556 shell->exposay.clicked = NULL;
5557 wl_list_init(&shell->exposay.surface_list);
5558
5559 lower_fullscreen_layer(shell);
5560 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5561 weston_keyboard_start_grab(seat->keyboard,
5562 &shell->exposay.grab_kbd);
5563 weston_keyboard_set_focus(seat->keyboard, NULL);
5564
5565 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5566 weston_pointer_start_grab(seat->pointer,
5567 &shell->exposay.grab_ptr);
5568 weston_pointer_set_focus(seat->pointer, NULL,
5569 seat->pointer->x, seat->pointer->y);
5570
5571 return exposay_layout(shell);
5572}
5573
5574static void
5575exposay_check_state(struct desktop_shell *shell)
5576{
5577 enum exposay_layout_state state_new = shell->exposay.state_cur;
5578 int do_switch = 0;
5579
5580 /* Don't do anything whilst animations are running, just store up
5581 * target state changes and only act on them when the animations have
5582 * completed. */
5583 if (exposay_is_animating(shell))
5584 return;
5585
5586 switch (shell->exposay.state_target) {
5587 case EXPOSAY_TARGET_OVERVIEW:
5588 switch (shell->exposay.state_cur) {
5589 case EXPOSAY_LAYOUT_OVERVIEW:
5590 goto out;
5591 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5592 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5593 break;
5594 default:
5595 state_new = exposay_transition_active(shell);
5596 break;
5597 }
5598 break;
5599
5600 case EXPOSAY_TARGET_SWITCH:
5601 do_switch = 1; /* fallthrough */
5602 case EXPOSAY_TARGET_CANCEL:
5603 switch (shell->exposay.state_cur) {
5604 case EXPOSAY_LAYOUT_INACTIVE:
5605 goto out;
5606 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5607 state_new = exposay_set_inactive(shell);
5608 break;
5609 default:
5610 state_new = exposay_transition_inactive(shell, do_switch);
5611 break;
5612 }
5613
5614 break;
5615 }
5616
5617out:
5618 shell->exposay.state_cur = state_new;
5619}
5620
5621static void
5622exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5623 struct weston_seat *seat)
5624{
5625 shell->exposay.state_target = state;
5626 shell->exposay.seat = seat;
5627 exposay_check_state(shell);
5628}
5629
5630static void
5631exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5632 void *data)
5633{
5634 struct desktop_shell *shell = data;
5635
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005636 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005637}
5638
Pekka Paalanen3c647232011-12-22 13:43:43 +02005639static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005640backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005641 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005642{
5643 struct weston_compositor *compositor = data;
5644 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005645 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005646
5647 /* TODO: we're limiting to simple use cases, where we assume just
5648 * control on the primary display. We'd have to extend later if we
5649 * ever get support for setting backlights on random desktop LCD
5650 * panels though */
5651 output = get_default_output(compositor);
5652 if (!output)
5653 return;
5654
5655 if (!output->set_backlight)
5656 return;
5657
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005658 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5659 backlight_new = output->backlight_current - 25;
5660 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5661 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005662
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005663 if (backlight_new < 5)
5664 backlight_new = 5;
5665 if (backlight_new > 255)
5666 backlight_new = 255;
5667
5668 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005669 output->set_backlight(output, output->backlight_current);
5670}
5671
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005672struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005673 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005674 struct weston_seat *seat;
5675 uint32_t key[2];
5676 int key_released[2];
5677};
5678
5679static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005680debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005681 uint32_t key, uint32_t state)
5682{
5683 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005684 struct weston_compositor *ec = db->seat->compositor;
5685 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005686 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005687 uint32_t serial;
5688 int send = 0, terminate = 0;
5689 int check_binding = 1;
5690 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005691 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005692
5693 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5694 /* Do not run bindings on key releases */
5695 check_binding = 0;
5696
5697 for (i = 0; i < 2; i++)
5698 if (key == db->key[i])
5699 db->key_released[i] = 1;
5700
5701 if (db->key_released[0] && db->key_released[1]) {
5702 /* All key releases been swalled so end the grab */
5703 terminate = 1;
5704 } else if (key != db->key[0] && key != db->key[1]) {
5705 /* Should not swallow release of other keys */
5706 send = 1;
5707 }
5708 } else if (key == db->key[0] && !db->key_released[0]) {
5709 /* Do not check bindings for the first press of the binding
5710 * key. This allows it to be used as a debug shortcut.
5711 * We still need to swallow this event. */
5712 check_binding = 0;
5713 } else if (db->key[1]) {
5714 /* If we already ran a binding don't process another one since
5715 * we can't keep track of all the binding keys that were
5716 * pressed in order to swallow the release events. */
5717 send = 1;
5718 check_binding = 0;
5719 }
5720
5721 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005722 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5723 key, state)) {
5724 /* We ran a binding so swallow the press and keep the
5725 * grab to swallow the released too. */
5726 send = 0;
5727 terminate = 0;
5728 db->key[1] = key;
5729 } else {
5730 /* Terminate the grab since the key pressed is not a
5731 * debug binding key. */
5732 send = 1;
5733 terminate = 1;
5734 }
5735 }
5736
5737 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005738 serial = wl_display_next_serial(display);
5739 resource_list = &grab->keyboard->focus_resource_list;
5740 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005741 wl_keyboard_send_key(resource, serial, time, key, state);
5742 }
5743 }
5744
5745 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005746 weston_keyboard_end_grab(grab->keyboard);
5747 if (grab->keyboard->input_method_resource)
5748 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005749 free(db);
5750 }
5751}
5752
5753static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005754debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005755 uint32_t mods_depressed, uint32_t mods_latched,
5756 uint32_t mods_locked, uint32_t group)
5757{
5758 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005759 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005760
Neil Roberts96d790e2013-09-19 17:32:00 +01005761 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005762
Neil Roberts96d790e2013-09-19 17:32:00 +01005763 wl_resource_for_each(resource, resource_list) {
5764 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5765 mods_latched, mods_locked, group);
5766 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005767}
5768
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005769static void
5770debug_binding_cancel(struct weston_keyboard_grab *grab)
5771{
5772 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5773
5774 weston_keyboard_end_grab(grab->keyboard);
5775 free(db);
5776}
5777
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005778struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005779 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005780 debug_binding_modifiers,
5781 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005782};
5783
5784static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005785debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005786{
5787 struct debug_binding_grab *grab;
5788
5789 grab = calloc(1, sizeof *grab);
5790 if (!grab)
5791 return;
5792
5793 grab->seat = (struct weston_seat *) seat;
5794 grab->key[0] = key;
5795 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005796 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005797}
5798
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005799static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005800force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005801 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005802{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005803 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005804 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005805 struct desktop_shell *shell = data;
5806 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005807 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005808
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005809 focus_surface = seat->keyboard->focus;
5810 if (!focus_surface)
5811 return;
5812
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005813 wl_signal_emit(&compositor->kill_signal, focus_surface);
5814
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005815 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005816 wl_client_get_credentials(client, &pid, NULL, NULL);
5817
5818 /* Skip clients that we launched ourselves (the credentials of
5819 * the socketpair is ours) */
5820 if (pid == getpid())
5821 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005822
Daniel Stone325fc2d2012-05-30 16:31:58 +01005823 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005824}
5825
5826static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005827workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005828 uint32_t key, void *data)
5829{
5830 struct desktop_shell *shell = data;
5831 unsigned int new_index = shell->workspaces.current;
5832
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005833 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005834 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005835 if (new_index != 0)
5836 new_index--;
5837
5838 change_workspace(shell, new_index);
5839}
5840
5841static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005842workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005843 uint32_t key, void *data)
5844{
5845 struct desktop_shell *shell = data;
5846 unsigned int new_index = shell->workspaces.current;
5847
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005848 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005849 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005850 if (new_index < shell->workspaces.num - 1)
5851 new_index++;
5852
5853 change_workspace(shell, new_index);
5854}
5855
5856static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005857workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005858 uint32_t key, void *data)
5859{
5860 struct desktop_shell *shell = data;
5861 unsigned int new_index;
5862
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005863 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005864 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005865 new_index = key - KEY_F1;
5866 if (new_index >= shell->workspaces.num)
5867 new_index = shell->workspaces.num - 1;
5868
5869 change_workspace(shell, new_index);
5870}
5871
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005872static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005873workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005874 uint32_t key, void *data)
5875{
5876 struct desktop_shell *shell = data;
5877 unsigned int new_index = shell->workspaces.current;
5878
5879 if (shell->locked)
5880 return;
5881
5882 if (new_index != 0)
5883 new_index--;
5884
5885 take_surface_to_workspace_by_seat(shell, seat, new_index);
5886}
5887
5888static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005889workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005890 uint32_t key, void *data)
5891{
5892 struct desktop_shell *shell = data;
5893 unsigned int new_index = shell->workspaces.current;
5894
5895 if (shell->locked)
5896 return;
5897
5898 if (new_index < shell->workspaces.num - 1)
5899 new_index++;
5900
5901 take_surface_to_workspace_by_seat(shell, seat, new_index);
5902}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005903
5904static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005905handle_output_destroy(struct wl_listener *listener, void *data)
5906{
5907 struct shell_output *output_listener =
5908 container_of(listener, struct shell_output, destroy_listener);
5909
5910 wl_list_remove(&output_listener->destroy_listener.link);
5911 wl_list_remove(&output_listener->link);
5912 free(output_listener);
5913}
5914
5915static void
5916create_shell_output(struct desktop_shell *shell,
5917 struct weston_output *output)
5918{
5919 struct shell_output *shell_output;
5920
5921 shell_output = zalloc(sizeof *shell_output);
5922 if (shell_output == NULL)
5923 return;
5924
5925 shell_output->output = output;
5926 shell_output->shell = shell;
5927 shell_output->destroy_listener.notify = handle_output_destroy;
5928 wl_signal_add(&output->destroy_signal,
5929 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005930 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005931}
5932
5933static void
5934handle_output_create(struct wl_listener *listener, void *data)
5935{
5936 struct desktop_shell *shell =
5937 container_of(listener, struct desktop_shell, output_create_listener);
5938 struct weston_output *output = (struct weston_output *)data;
5939
5940 create_shell_output(shell, output);
5941}
5942
5943static void
5944setup_output_destroy_handler(struct weston_compositor *ec,
5945 struct desktop_shell *shell)
5946{
5947 struct weston_output *output;
5948
5949 wl_list_init(&shell->output_list);
5950 wl_list_for_each(output, &ec->output_list, link)
5951 create_shell_output(shell, output);
5952
5953 shell->output_create_listener.notify = handle_output_create;
5954 wl_signal_add(&ec->output_created_signal,
5955 &shell->output_create_listener);
5956}
5957
5958static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005959shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005960{
Tiago Vignattibe143262012-04-16 17:31:41 +03005961 struct desktop_shell *shell =
5962 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005963 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005964 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005965
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005966 if (shell->child.client)
5967 wl_client_destroy(shell->child.client);
5968
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005969 wl_list_remove(&shell->idle_listener.link);
5970 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005971 wl_list_remove(&shell->show_input_panel_listener.link);
5972 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005973
Xiong Zhang6b481422013-10-23 13:58:32 +08005974 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5975 wl_list_remove(&shell_output->destroy_listener.link);
5976 wl_list_remove(&shell_output->link);
5977 free(shell_output);
5978 }
5979
5980 wl_list_remove(&shell->output_create_listener.link);
5981
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005982 wl_array_for_each(ws, &shell->workspaces.array)
5983 workspace_destroy(*ws);
5984 wl_array_release(&shell->workspaces.array);
5985
Pekka Paalanen3c647232011-12-22 13:43:43 +02005986 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005987 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005988 free(shell);
5989}
5990
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005991static void
5992shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5993{
5994 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005995 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005996
5997 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005998 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5999 MODIFIER_CTRL | MODIFIER_ALT,
6000 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01006001 weston_compositor_add_key_binding(ec, KEY_TAB,
6002 MODIFIER_ALT,
6003 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006004 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6005 click_to_activate_binding,
6006 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006007 weston_compositor_add_touch_binding(ec, 0,
6008 touch_to_activate_binding,
6009 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006010 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6011 MODIFIER_SUPER | MODIFIER_ALT,
6012 surface_opacity_binding, NULL);
6013 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6014 MODIFIER_SUPER, zoom_axis_binding,
6015 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006016
6017 /* configurable bindings */
6018 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006019 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6020 zoom_key_binding, NULL);
6021 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6022 zoom_key_binding, NULL);
6023 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6024 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006025 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006026 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6027 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006028
6029 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6030 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6031 rotate_binding, NULL);
6032
Daniel Stone325fc2d2012-05-30 16:31:58 +01006033 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6034 shell);
6035 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6036 ec);
6037 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6038 backlight_binding, ec);
6039 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6040 ec);
6041 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6042 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006043 weston_compositor_add_key_binding(ec, KEY_K, mod,
6044 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006045 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6046 workspace_up_binding, shell);
6047 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6048 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006049 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6050 workspace_move_surface_up_binding,
6051 shell);
6052 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6053 workspace_move_surface_down_binding,
6054 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006055
Daniel Stonedf8133b2013-11-19 11:37:14 +01006056 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6057
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006058 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6059 if (shell->workspaces.num > 1) {
6060 num_workspace_bindings = shell->workspaces.num;
6061 if (num_workspace_bindings > 6)
6062 num_workspace_bindings = 6;
6063 for (i = 0; i < num_workspace_bindings; i++)
6064 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6065 workspace_f_binding,
6066 shell);
6067 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006068
6069 /* Debug bindings */
6070 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6071 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006072}
6073
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006074WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006075module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006076 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006077{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006078 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006079 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006080 struct workspace **pws;
6081 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006082 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006083
Peter Huttererf3d62272013-08-08 11:57:05 +10006084 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006085 if (shell == NULL)
6086 return -1;
6087
Kristian Høgsberg75840622011-09-06 13:48:16 -04006088 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006089
6090 shell->destroy_listener.notify = shell_destroy;
6091 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006092 shell->idle_listener.notify = idle_handler;
6093 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6094 shell->wake_listener.notify = wake_handler;
6095 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006096 shell->show_input_panel_listener.notify = show_input_panels;
6097 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6098 shell->hide_input_panel_listener.notify = hide_input_panels;
6099 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006100 shell->update_input_panel_listener.notify = update_input_panels;
6101 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006102 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006103 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006104 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006105 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006106 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006107 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006108 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006109 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006110 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006111 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006112 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006113
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006114 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006115
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006116 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6117 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006118 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6119 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006120 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006121
6122 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006123 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006124
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006125 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006126
Daniel Stonedf8133b2013-11-19 11:37:14 +01006127 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6128 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6129
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006130 for (i = 0; i < shell->workspaces.num; i++) {
6131 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6132 if (pws == NULL)
6133 return -1;
6134
6135 *pws = workspace_create();
6136 if (*pws == NULL)
6137 return -1;
6138 }
6139 activate_workspace(shell, 0);
6140
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006141 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006142 wl_list_init(&shell->workspaces.animation.link);
6143 shell->workspaces.animation.frame = animate_workspace_change_frame;
6144
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006145 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006146 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006147 return -1;
6148
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006149 if (wl_global_create(ec->wl_display,
6150 &desktop_shell_interface, 2,
6151 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006152 return -1;
6153
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006154 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6155 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006156 return -1;
6157
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006158 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006159 shell, bind_input_panel) == NULL)
6160 return -1;
6161
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006162 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6163 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006164 return -1;
6165
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006166 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006167
Xiong Zhang6b481422013-10-23 13:58:32 +08006168 setup_output_destroy_handler(ec, shell);
6169
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006170 loop = wl_display_get_event_loop(ec->wl_display);
6171 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006172
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006173 shell->screensaver.timer =
6174 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6175
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006176 wl_list_for_each(seat, &ec->seat_list, link)
6177 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006178
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006179 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006180
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006181 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006182
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006183 return 0;
6184}