blob: 605f09093eef47c0c6149d5a58c8e3cd9fb09f2e [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
Alex Wubd3354b2012-04-17 17:20:49 +0800369static bool
370shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
371{
372 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500373 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800374
375 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100376
Jason Ekstranda7af7042013-10-12 22:38:11 -0500377 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800378 return false;
379
Jason Ekstranda7af7042013-10-12 22:38:11 -0500380 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
381 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800382 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500383 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800384}
385
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500386static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400387destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300388{
389 struct shell_grab *grab;
390
391 grab = container_of(listener, struct shell_grab,
392 shsurf_destroy_listener);
393
394 grab->shsurf = NULL;
395}
396
Jason Ekstranda7af7042013-10-12 22:38:11 -0500397static struct weston_view *
398get_default_view(struct weston_surface *surface)
399{
400 struct shell_surface *shsurf;
401 struct weston_view *view;
402
403 if (!surface || wl_list_empty(&surface->views))
404 return NULL;
405
406 shsurf = get_shell_surface(surface);
407 if (shsurf)
408 return shsurf->view;
409
410 wl_list_for_each(view, &surface->views, surface_link)
411 if (weston_view_is_mapped(view))
412 return view;
413
414 return container_of(surface->views.next, struct weston_view, surface_link);
415}
416
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300417static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400418popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400419
420static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300421shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400422 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300423 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400424 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300425 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300426{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300427 struct desktop_shell *shell = shsurf->shell;
428
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400429 popup_grab_end(pointer);
430
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300431 grab->grab.interface = interface;
432 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400433 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500434 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400435 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300436
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700437 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400438 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400439 if (shell->child.desktop_shell) {
440 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
441 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442 weston_pointer_set_focus(pointer,
443 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400444 wl_fixed_from_int(0),
445 wl_fixed_from_int(0));
446 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300447}
448
449static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300450shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300451{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700452 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400453 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700454 grab->shsurf->grabbed = 0;
455 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300456
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700457 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300458}
459
460static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700461shell_touch_grab_start(struct shell_touch_grab *grab,
462 const struct weston_touch_grab_interface *interface,
463 struct shell_surface *shsurf,
464 struct weston_touch *touch)
465{
466 struct desktop_shell *shell = shsurf->shell;
467
468 grab->grab.interface = interface;
469 grab->shsurf = shsurf;
470 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
471 wl_signal_add(&shsurf->destroy_signal,
472 &grab->shsurf_destroy_listener);
473
474 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700475 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700476
477 weston_touch_start_grab(touch, &grab->grab);
478 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500479 weston_touch_set_focus(touch->seat,
480 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700481}
482
483static void
484shell_touch_grab_end(struct shell_touch_grab *grab)
485{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700486 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700487 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700488 grab->shsurf->grabbed = 0;
489 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700490
491 weston_touch_end_grab(grab->touch);
492}
493
494static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500495center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800496 struct weston_output *output);
497
Daniel Stone496ca172012-05-30 16:31:42 +0100498static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300499get_modifier(char *modifier)
500{
501 if (!modifier)
502 return MODIFIER_SUPER;
503
504 if (!strcmp("ctrl", modifier))
505 return MODIFIER_CTRL;
506 else if (!strcmp("alt", modifier))
507 return MODIFIER_ALT;
508 else if (!strcmp("super", modifier))
509 return MODIFIER_SUPER;
510 else
511 return MODIFIER_SUPER;
512}
513
Juan Zhaoe10d2792012-04-25 19:09:52 +0800514static enum animation_type
515get_animation_type(char *animation)
516{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800517 if (!strcmp("zoom", animation))
518 return ANIMATION_ZOOM;
519 else if (!strcmp("fade", animation))
520 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100521 else if (!strcmp("dim-layer", animation))
522 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800523 else
524 return ANIMATION_NONE;
525}
526
Alex Wu4539b082012-03-01 12:57:46 +0800527static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400528shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200529{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400530 struct weston_config_section *section;
531 int duration;
532 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200533
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400534 section = weston_config_get_section(shell->compositor->config,
535 "screensaver", NULL, NULL);
536 weston_config_section_get_string(section,
537 "path", &shell->screensaver.path, NULL);
538 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200539 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400540
541 section = weston_config_get_section(shell->compositor->config,
542 "shell", NULL, NULL);
543 weston_config_section_get_string(section,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100544 "client", &s, LIBEXECDIR "/weston-desktop-shell");
545 shell->client = s;
546 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400547 "binding-modifier", &s, "super");
548 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200549 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400550 weston_config_section_get_string(section, "animation", &s, "none");
551 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200552 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700553 weston_config_section_get_string(section,
554 "startup-animation", &s, "fade");
555 shell->startup_animation_type = get_animation_type(s);
556 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700557 if (shell->startup_animation_type == ANIMATION_ZOOM)
558 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100559 weston_config_section_get_string(section, "focus-animation", &s, "none");
560 shell->focus_animation_type = get_animation_type(s);
561 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400562 weston_config_section_get_uint(section, "num-workspaces",
563 &shell->workspaces.num,
564 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200565}
566
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100567static struct weston_output *
568get_default_output(struct weston_compositor *compositor)
569{
570 return container_of(compositor->output_list.next,
571 struct weston_output, link);
572}
573
574
575/* no-op func for checking focus surface */
576static void
577focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy,
578 int32_t width, int32_t height)
579{
580}
581
582static struct focus_surface *
583get_focus_surface(struct weston_surface *surface)
584{
585 if (surface->configure == focus_surface_configure)
586 return surface->configure_private;
587 else
588 return NULL;
589}
590
591static bool
592is_focus_surface (struct weston_surface *es)
593{
594 return (es->configure == focus_surface_configure);
595}
596
597static bool
598is_focus_view (struct weston_view *view)
599{
600 return is_focus_surface (view->surface);
601}
602
603static struct focus_surface *
604create_focus_surface(struct weston_compositor *ec,
605 struct weston_output *output)
606{
607 struct focus_surface *fsurf = NULL;
608 struct weston_surface *surface = NULL;
609
610 fsurf = malloc(sizeof *fsurf);
611 if (!fsurf)
612 return NULL;
613
614 fsurf->surface = weston_surface_create(ec);
615 surface = fsurf->surface;
616 if (surface == NULL) {
617 free(fsurf);
618 return NULL;
619 }
620
621 surface->configure = focus_surface_configure;
622 surface->output = output;
623 surface->configure_private = fsurf;
624
625 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100626 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100627
628 weston_view_configure(fsurf->view, output->x, output->y,
629 output->width, output->height);
630 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
631 pixman_region32_fini(&surface->opaque);
632 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
633 output->width, output->height);
634 pixman_region32_fini(&surface->input);
635 pixman_region32_init(&surface->input);
636
637 wl_list_init(&fsurf->workspace_transform.link);
638
639 return fsurf;
640}
641
642static void
643focus_surface_destroy(struct focus_surface *fsurf)
644{
645 weston_surface_destroy(fsurf->surface);
646 free(fsurf);
647}
648
649static void
650focus_animation_done(struct weston_view_animation *animation, void *data)
651{
652 struct workspace *ws = data;
653
654 ws->focus_animation = NULL;
655}
656
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200657static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200658focus_state_destroy(struct focus_state *state)
659{
660 wl_list_remove(&state->seat_destroy_listener.link);
661 wl_list_remove(&state->surface_destroy_listener.link);
662 free(state);
663}
664
665static void
666focus_state_seat_destroy(struct wl_listener *listener, void *data)
667{
668 struct focus_state *state = container_of(listener,
669 struct focus_state,
670 seat_destroy_listener);
671
672 wl_list_remove(&state->link);
673 focus_state_destroy(state);
674}
675
676static void
677focus_state_surface_destroy(struct wl_listener *listener, void *data)
678{
679 struct focus_state *state = container_of(listener,
680 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400681 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400682 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500683 struct weston_surface *main_surface, *next;
684 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200685
Pekka Paalanen01388e22013-04-25 13:57:44 +0300686 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
687
Kristian Høgsberge3778222012-07-31 17:29:30 -0400688 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500689 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
690 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400691 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100692 if (is_focus_view(view))
693 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400694
Jason Ekstranda7af7042013-10-12 22:38:11 -0500695 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400696 break;
697 }
698
Pekka Paalanen01388e22013-04-25 13:57:44 +0300699 /* if the focus was a sub-surface, activate its main surface */
700 if (main_surface != state->keyboard_focus)
701 next = main_surface;
702
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100703 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400704 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100705 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400706 activate(shell, next, state->seat);
707 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100708 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
709 if (state->ws->focus_animation)
710 weston_view_animation_destroy(state->ws->focus_animation);
711
712 state->ws->focus_animation = weston_fade_run(
713 state->ws->fsurf_front->view,
714 state->ws->fsurf_front->view->alpha, 0.0, 300,
715 focus_animation_done, state->ws);
716 }
717
Kristian Høgsberge3778222012-07-31 17:29:30 -0400718 wl_list_remove(&state->link);
719 focus_state_destroy(state);
720 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200721}
722
723static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400724focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200725{
Jonas Ådahl04769742012-06-13 00:01:24 +0200726 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200727
728 state = malloc(sizeof *state);
729 if (state == NULL)
730 return NULL;
731
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100732 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400733 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200734 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400735 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200736
737 state->seat_destroy_listener.notify = focus_state_seat_destroy;
738 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400739 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200740 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400741 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200742
743 return state;
744}
745
Jonas Ådahl8538b222012-08-29 22:13:03 +0200746static struct focus_state *
747ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
748{
749 struct workspace *ws = get_current_workspace(shell);
750 struct focus_state *state;
751
752 wl_list_for_each(state, &ws->focus_list, link)
753 if (state->seat == seat)
754 break;
755
756 if (&state->link == &ws->focus_list)
757 state = focus_state_create(seat, ws);
758
759 return state;
760}
761
Jonas Ådahl04769742012-06-13 00:01:24 +0200762static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400763restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200764{
765 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400766 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200767
768 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400769 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200770
Kristian Høgsberge3148752013-05-06 23:19:49 -0400771 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200772 }
773}
774
775static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200776replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
777 struct weston_seat *seat)
778{
779 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400780 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200781
782 wl_list_for_each(state, &ws->focus_list, link) {
783 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400784 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500785 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200786 return;
787 }
788 }
789}
790
791static void
792drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
793 struct weston_surface *surface)
794{
795 struct focus_state *state;
796
797 wl_list_for_each(state, &ws->focus_list, link)
798 if (state->keyboard_focus == surface)
799 state->keyboard_focus = NULL;
800}
801
802static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100803animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
804 struct weston_view *from, struct weston_view *to)
805{
806 struct weston_output *output;
807 bool focus_surface_created = false;
808
809 /* FIXME: Only support dim animation using two layers */
810 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
811 return;
812
813 output = get_default_output(shell->compositor);
814 if (ws->fsurf_front == NULL && (from || to)) {
815 ws->fsurf_front = create_focus_surface(shell->compositor, output);
816 ws->fsurf_back = create_focus_surface(shell->compositor, output);
817 ws->fsurf_front->view->alpha = 0.0;
818 ws->fsurf_back->view->alpha = 0.0;
819 focus_surface_created = true;
820 } else {
821 wl_list_remove(&ws->fsurf_front->view->layer_link);
822 wl_list_remove(&ws->fsurf_back->view->layer_link);
823 }
824
825 if (ws->focus_animation) {
826 weston_view_animation_destroy(ws->focus_animation);
827 ws->focus_animation = NULL;
828 }
829
830 if (to)
831 wl_list_insert(&to->layer_link,
832 &ws->fsurf_front->view->layer_link);
833 else if (from)
834 wl_list_insert(&ws->layer.view_list,
835 &ws->fsurf_front->view->layer_link);
836
837 if (focus_surface_created) {
838 ws->focus_animation = weston_fade_run(
839 ws->fsurf_front->view,
840 ws->fsurf_front->view->alpha, 0.6, 300,
841 focus_animation_done, ws);
842 } else if (from) {
843 wl_list_insert(&from->layer_link,
844 &ws->fsurf_back->view->layer_link);
845 ws->focus_animation = weston_stable_fade_run(
846 ws->fsurf_front->view, 0.0,
847 ws->fsurf_back->view, 0.6,
848 focus_animation_done, ws);
849 } else if (to) {
850 wl_list_insert(&ws->layer.view_list,
851 &ws->fsurf_back->view->layer_link);
852 ws->focus_animation = weston_stable_fade_run(
853 ws->fsurf_front->view, 0.0,
854 ws->fsurf_back->view, 0.6,
855 focus_animation_done, ws);
856 }
857}
858
859static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200860workspace_destroy(struct workspace *ws)
861{
Jonas Ådahl04769742012-06-13 00:01:24 +0200862 struct focus_state *state, *next;
863
864 wl_list_for_each_safe(state, next, &ws->focus_list, link)
865 focus_state_destroy(state);
866
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100867 if (ws->fsurf_front)
868 focus_surface_destroy(ws->fsurf_front);
869 if (ws->fsurf_back)
870 focus_surface_destroy(ws->fsurf_back);
871
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200872 free(ws);
873}
874
Jonas Ådahl04769742012-06-13 00:01:24 +0200875static void
876seat_destroyed(struct wl_listener *listener, void *data)
877{
878 struct weston_seat *seat = data;
879 struct focus_state *state, *next;
880 struct workspace *ws = container_of(listener,
881 struct workspace,
882 seat_destroyed_listener);
883
884 wl_list_for_each_safe(state, next, &ws->focus_list, link)
885 if (state->seat == seat)
886 wl_list_remove(&state->link);
887}
888
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200889static struct workspace *
890workspace_create(void)
891{
892 struct workspace *ws = malloc(sizeof *ws);
893 if (ws == NULL)
894 return NULL;
895
896 weston_layer_init(&ws->layer, NULL);
897
Jonas Ådahl04769742012-06-13 00:01:24 +0200898 wl_list_init(&ws->focus_list);
899 wl_list_init(&ws->seat_destroyed_listener.link);
900 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100901 ws->fsurf_front = NULL;
902 ws->fsurf_back = NULL;
903 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905 return ws;
906}
907
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200908static int
909workspace_is_empty(struct workspace *ws)
910{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912}
913
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200914static struct workspace *
915get_workspace(struct desktop_shell *shell, unsigned int index)
916{
917 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200918 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919 pws += index;
920 return *pws;
921}
922
923static struct workspace *
924get_current_workspace(struct desktop_shell *shell)
925{
926 return get_workspace(shell, shell->workspaces.current);
927}
928
929static void
930activate_workspace(struct desktop_shell *shell, unsigned int index)
931{
932 struct workspace *ws;
933
934 ws = get_workspace(shell, index);
935 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
936
937 shell->workspaces.current = index;
938}
939
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940static unsigned int
941get_output_height(struct weston_output *output)
942{
943 return abs(output->region.extents.y1 - output->region.extents.y2);
944}
945
946static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100947view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200948{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200949 struct weston_transform *transform;
950
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100951 if (is_focus_view(view)) {
952 struct focus_surface *fsurf = get_focus_surface(view->surface);
953 transform = &fsurf->workspace_transform;
954 } else {
955 struct shell_surface *shsurf = get_shell_surface(view->surface);
956 transform = &shsurf->workspace_transform;
957 }
958
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200959 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100961 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100963 weston_matrix_init(&transform->matrix);
964 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967}
968
969static void
970workspace_translate_out(struct workspace *ws, double fraction)
971{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200973 unsigned int height;
974 double d;
975
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
977 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978 d = height * fraction;
979
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100980 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200981 }
982}
983
984static void
985workspace_translate_in(struct workspace *ws, double fraction)
986{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200988 unsigned int height;
989 double d;
990
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
992 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993
994 if (fraction > 0)
995 d = -(height - height * fraction);
996 else
997 d = height + height * fraction;
998
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000 }
1001}
1002
1003static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001004broadcast_current_workspace_state(struct desktop_shell *shell)
1005{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001006 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001007
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001008 wl_resource_for_each(resource, &shell->workspaces.client_list)
1009 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001010 shell->workspaces.current,
1011 shell->workspaces.num);
1012}
1013
1014static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015reverse_workspace_change_animation(struct desktop_shell *shell,
1016 unsigned int index,
1017 struct workspace *from,
1018 struct workspace *to)
1019{
1020 shell->workspaces.current = index;
1021
1022 shell->workspaces.anim_to = to;
1023 shell->workspaces.anim_from = from;
1024 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1025 shell->workspaces.anim_timestamp = 0;
1026
Scott Moreau4272e632012-08-13 09:58:41 -06001027 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001028}
1029
1030static void
1031workspace_deactivate_transforms(struct workspace *ws)
1032{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001037 if (is_focus_view(view)) {
1038 struct focus_surface *fsurf = get_focus_surface(view->surface);
1039 transform = &fsurf->workspace_transform;
1040 } else {
1041 struct shell_surface *shsurf = get_shell_surface(view->surface);
1042 transform = &shsurf->workspace_transform;
1043 }
1044
1045 if (!wl_list_empty(&transform->link)) {
1046 wl_list_remove(&transform->link);
1047 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001048 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001050 }
1051}
1052
1053static void
1054finish_workspace_change_animation(struct desktop_shell *shell,
1055 struct workspace *from,
1056 struct workspace *to)
1057{
Scott Moreau4272e632012-08-13 09:58:41 -06001058 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001059
1060 wl_list_remove(&shell->workspaces.animation.link);
1061 workspace_deactivate_transforms(from);
1062 workspace_deactivate_transforms(to);
1063 shell->workspaces.anim_to = NULL;
1064
1065 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1066}
1067
1068static void
1069animate_workspace_change_frame(struct weston_animation *animation,
1070 struct weston_output *output, uint32_t msecs)
1071{
1072 struct desktop_shell *shell =
1073 container_of(animation, struct desktop_shell,
1074 workspaces.animation);
1075 struct workspace *from = shell->workspaces.anim_from;
1076 struct workspace *to = shell->workspaces.anim_to;
1077 uint32_t t;
1078 double x, y;
1079
1080 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1081 finish_workspace_change_animation(shell, from, to);
1082 return;
1083 }
1084
1085 if (shell->workspaces.anim_timestamp == 0) {
1086 if (shell->workspaces.anim_current == 0.0)
1087 shell->workspaces.anim_timestamp = msecs;
1088 else
1089 shell->workspaces.anim_timestamp =
1090 msecs -
1091 /* Invers of movement function 'y' below. */
1092 (asin(1.0 - shell->workspaces.anim_current) *
1093 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1094 M_2_PI);
1095 }
1096
1097 t = msecs - shell->workspaces.anim_timestamp;
1098
1099 /*
1100 * x = [0, π/2]
1101 * y(x) = sin(x)
1102 */
1103 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1104 y = sin(x);
1105
1106 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001107 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108
1109 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1110 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1111 shell->workspaces.anim_current = y;
1112
Scott Moreau4272e632012-08-13 09:58:41 -06001113 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001115 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001116 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117}
1118
1119static void
1120animate_workspace_change(struct desktop_shell *shell,
1121 unsigned int index,
1122 struct workspace *from,
1123 struct workspace *to)
1124{
1125 struct weston_output *output;
1126
1127 int dir;
1128
1129 if (index > shell->workspaces.current)
1130 dir = -1;
1131 else
1132 dir = 1;
1133
1134 shell->workspaces.current = index;
1135
1136 shell->workspaces.anim_dir = dir;
1137 shell->workspaces.anim_from = from;
1138 shell->workspaces.anim_to = to;
1139 shell->workspaces.anim_current = 0.0;
1140 shell->workspaces.anim_timestamp = 0;
1141
1142 output = container_of(shell->compositor->output_list.next,
1143 struct weston_output, link);
1144 wl_list_insert(&output->animation_list,
1145 &shell->workspaces.animation.link);
1146
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001147 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001148
1149 workspace_translate_in(to, 0);
1150
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001151 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001152
Scott Moreau4272e632012-08-13 09:58:41 -06001153 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154}
1155
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001156static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001157update_workspace(struct desktop_shell *shell, unsigned int index,
1158 struct workspace *from, struct workspace *to)
1159{
1160 shell->workspaces.current = index;
1161 wl_list_insert(&from->layer.link, &to->layer.link);
1162 wl_list_remove(&from->layer.link);
1163}
1164
1165static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001166change_workspace(struct desktop_shell *shell, unsigned int index)
1167{
1168 struct workspace *from;
1169 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001170 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001171
1172 if (index == shell->workspaces.current)
1173 return;
1174
1175 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001177 return;
1178
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001179 from = get_current_workspace(shell);
1180 to = get_workspace(shell, index);
1181
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182 if (shell->workspaces.anim_from == to &&
1183 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001184 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001186 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001187 return;
1188 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001189
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001190 if (shell->workspaces.anim_to != NULL)
1191 finish_workspace_change_animation(shell,
1192 shell->workspaces.anim_from,
1193 shell->workspaces.anim_to);
1194
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001195 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001196
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001197 if (shell->focus_animation_type != ANIMATION_NONE) {
1198 wl_list_for_each(state, &from->focus_list, link)
1199 if (state->keyboard_focus)
1200 animate_focus_change(shell, from,
1201 get_default_view(state->keyboard_focus), NULL);
1202
1203 wl_list_for_each(state, &to->focus_list, link)
1204 if (state->keyboard_focus)
1205 animate_focus_change(shell, to,
1206 NULL, get_default_view(state->keyboard_focus));
1207 }
1208
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001209 if (workspace_is_empty(to) && workspace_is_empty(from))
1210 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001211 else
1212 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001213
1214 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001215}
1216
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001217static bool
1218workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1219{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 struct wl_list *e;
1222
1223 if (wl_list_empty(list))
1224 return false;
1225
1226 e = list->next;
1227
1228 if (e->next != list)
1229 return false;
1230
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232}
1233
1234static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235move_view_to_workspace(struct desktop_shell *shell,
1236 struct weston_view *view,
1237 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001238{
1239 struct workspace *from;
1240 struct workspace *to;
1241 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001242 struct weston_surface *focus;
1243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001245
1246 if (workspace == shell->workspaces.current)
1247 return;
1248
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001249 if (workspace >= shell->workspaces.num)
1250 workspace = shell->workspaces.num - 1;
1251
Jonas Ådahle9d22502012-08-29 22:13:01 +02001252 from = get_current_workspace(shell);
1253 to = get_workspace(shell, workspace);
1254
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 wl_list_remove(&view->layer_link);
1256 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001257
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001259 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1260 if (!seat->keyboard)
1261 continue;
1262
1263 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001265 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001266 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001269}
1270
1271static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001273 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274 unsigned int index)
1275{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001276 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001278 struct shell_surface *shsurf;
1279 struct workspace *from;
1280 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001281 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282
Pekka Paalanen01388e22013-04-25 13:57:44 +03001283 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 view = get_default_view(surface);
1285 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001286 index == shell->workspaces.current ||
1287 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001288 return;
1289
1290 from = get_current_workspace(shell);
1291 to = get_workspace(shell, index);
1292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 wl_list_remove(&view->layer_link);
1294 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295
Jonas Ådahle9d22502012-08-29 22:13:01 +02001296 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001297 drop_focus_state(shell, from, surface);
1298
1299 if (shell->workspaces.anim_from == to &&
1300 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001301 wl_list_remove(&to->layer.link);
1302 wl_list_insert(from->layer.link.prev, &to->layer.link);
1303
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001305 broadcast_current_workspace_state(shell);
1306
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001307 return;
1308 }
1309
1310 if (shell->workspaces.anim_to != NULL)
1311 finish_workspace_change_animation(shell,
1312 shell->workspaces.anim_from,
1313 shell->workspaces.anim_to);
1314
1315 if (workspace_is_empty(from) &&
1316 workspace_has_only(to, surface))
1317 update_workspace(shell, index, from, to);
1318 else {
1319 shsurf = get_shell_surface(surface);
1320 if (wl_list_empty(&shsurf->workspace_transform.link))
1321 wl_list_insert(&shell->workspaces.anim_sticky_list,
1322 &shsurf->workspace_transform.link);
1323
1324 animate_workspace_change(shell, index, from, to);
1325 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001326
1327 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001328
1329 state = ensure_focus_state(shell, seat);
1330 if (state != NULL)
1331 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001332}
1333
1334static void
1335workspace_manager_move_surface(struct wl_client *client,
1336 struct wl_resource *resource,
1337 struct wl_resource *surface_resource,
1338 uint32_t workspace)
1339{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001340 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001341 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001342 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001343 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001345
Pekka Paalanen01388e22013-04-25 13:57:44 +03001346 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 view = get_default_view(main_surface);
1348 if (!view)
1349 return;
1350 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001351}
1352
1353static const struct workspace_manager_interface workspace_manager_implementation = {
1354 workspace_manager_move_surface,
1355};
1356
1357static void
1358unbind_resource(struct wl_resource *resource)
1359{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001360 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001361}
1362
1363static void
1364bind_workspace_manager(struct wl_client *client,
1365 void *data, uint32_t version, uint32_t id)
1366{
1367 struct desktop_shell *shell = data;
1368 struct wl_resource *resource;
1369
Jason Ekstranda85118c2013-06-27 20:17:02 -05001370 resource = wl_resource_create(client,
1371 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001372
1373 if (resource == NULL) {
1374 weston_log("couldn't add workspace manager object");
1375 return;
1376 }
1377
Jason Ekstranda85118c2013-06-27 20:17:02 -05001378 wl_resource_set_implementation(resource,
1379 &workspace_manager_implementation,
1380 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001381 wl_list_insert(&shell->workspaces.client_list,
1382 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001383
1384 workspace_manager_send_state(resource,
1385 shell->workspaces.current,
1386 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001387}
1388
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001389static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001390touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1391 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1392{
1393}
1394
1395static void
1396touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1397{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001398 struct weston_touch_move_grab *move =
1399 (struct weston_touch_move_grab *) container_of(
1400 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001401
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001402 if (grab->touch->seat->num_tp == 0) {
1403 shell_touch_grab_end(&move->base);
1404 free(move);
1405 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001406}
1407
1408static void
1409touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1410 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1411{
1412 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1413 struct shell_surface *shsurf = move->base.shsurf;
1414 struct weston_surface *es;
1415 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1416 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1417
1418 if (!shsurf)
1419 return;
1420
1421 es = shsurf->surface;
1422
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 weston_view_configure(shsurf->view, dx, dy,
1424 shsurf->view->geometry.width,
1425 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001426
1427 weston_compositor_schedule_repaint(es->compositor);
1428}
1429
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001430static void
1431touch_move_grab_cancel(struct weston_touch_grab *grab)
1432{
1433 struct weston_touch_move_grab *move =
1434 (struct weston_touch_move_grab *) container_of(
1435 grab, struct shell_touch_grab, grab);
1436
1437 shell_touch_grab_end(&move->base);
1438 free(move);
1439}
1440
Rusty Lynch1084da52013-08-15 09:10:08 -07001441static const struct weston_touch_grab_interface touch_move_grab_interface = {
1442 touch_move_grab_down,
1443 touch_move_grab_up,
1444 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001445 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001446};
1447
1448static int
1449surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1450{
1451 struct weston_touch_move_grab *move;
1452
1453 if (!shsurf)
1454 return -1;
1455
1456 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1457 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001458 if (shsurf->grabbed)
1459 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001460
1461 move = malloc(sizeof *move);
1462 if (!move)
1463 return -1;
1464
Jason Ekstranda7af7042013-10-12 22:38:11 -05001465 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001466 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001467 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001468 seat->touch->grab_y;
1469
1470 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1471 seat->touch);
1472
1473 return 0;
1474}
1475
1476static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001477noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001478{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001479}
1480
1481static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001482move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1483 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001484{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001485 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001486 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001487 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001488 int dx, dy;
1489
1490 weston_pointer_move(pointer, x, y);
1491 dx = wl_fixed_to_int(pointer->x + move->dx);
1492 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001493
1494 if (!shsurf)
1495 return;
1496
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 weston_view_configure(shsurf->view, dx, dy,
1498 shsurf->view->geometry.width,
1499 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001500
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001502}
1503
1504static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001505move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001506 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001507{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001508 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1509 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001510 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001511 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001512
Daniel Stone4dbadb12012-05-30 16:31:51 +01001513 if (pointer->button_count == 0 &&
1514 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001515 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001516 free(grab);
1517 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001518}
1519
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001520static void
1521move_grab_cancel(struct weston_pointer_grab *grab)
1522{
1523 struct shell_grab *shell_grab =
1524 container_of(grab, struct shell_grab, grab);
1525
1526 shell_grab_end(shell_grab);
1527 free(grab);
1528}
1529
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001530static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001531 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001532 move_grab_motion,
1533 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001534 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001535};
1536
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001537static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001538surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001539{
1540 struct weston_move_grab *move;
1541
1542 if (!shsurf)
1543 return -1;
1544
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001545 if (shsurf->grabbed)
1546 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001547 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1548 return 0;
1549
1550 move = malloc(sizeof *move);
1551 if (!move)
1552 return -1;
1553
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001555 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001557 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558
1559 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001560 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561
1562 return 0;
1563}
1564
1565static void
1566shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1567 struct wl_resource *seat_resource, uint32_t serial)
1568{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001569 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001570 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001571 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001572
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001573 if (seat->pointer &&
1574 seat->pointer->button_count > 0 &&
1575 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001577 if ((surface == shsurf->surface) &&
1578 (surface_move(shsurf, seat) < 0))
1579 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001580 } else if (seat->touch &&
1581 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001583 if ((surface == shsurf->surface) &&
1584 (surface_touch_move(shsurf, seat) < 0))
1585 wl_resource_post_no_memory(resource);
1586 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001587}
1588
1589struct weston_resize_grab {
1590 struct shell_grab base;
1591 uint32_t edges;
1592 int32_t width, height;
1593};
1594
1595static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001596resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1597 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598{
1599 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001600 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001601 struct shell_surface *shsurf = resize->base.shsurf;
1602 int32_t width, height;
1603 wl_fixed_t from_x, from_y;
1604 wl_fixed_t to_x, to_y;
1605
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001606 weston_pointer_move(pointer, x, y);
1607
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001608 if (!shsurf)
1609 return;
1610
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 weston_view_from_global_fixed(shsurf->view,
1612 pointer->grab_x, pointer->grab_y,
1613 &from_x, &from_y);
1614 weston_view_from_global_fixed(shsurf->view,
1615 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001616
1617 width = resize->width;
1618 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1619 width += wl_fixed_to_int(from_x - to_x);
1620 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1621 width += wl_fixed_to_int(to_x - from_x);
1622 }
1623
1624 height = resize->height;
1625 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1626 height += wl_fixed_to_int(from_y - to_y);
1627 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1628 height += wl_fixed_to_int(to_y - from_y);
1629 }
1630
1631 shsurf->client->send_configure(shsurf->surface,
1632 resize->edges, width, height);
1633}
1634
1635static void
1636send_configure(struct weston_surface *surface,
1637 uint32_t edges, int32_t width, int32_t height)
1638{
1639 struct shell_surface *shsurf = get_shell_surface(surface);
1640
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001641 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642 edges, width, height);
1643}
1644
1645static const struct weston_shell_client shell_client = {
1646 send_configure
1647};
1648
1649static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001650resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001651 uint32_t time, uint32_t button, uint32_t state_w)
1652{
1653 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001654 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001655 enum wl_pointer_button_state state = state_w;
1656
1657 if (pointer->button_count == 0 &&
1658 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1659 shell_grab_end(&resize->base);
1660 free(grab);
1661 }
1662}
1663
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001664static void
1665resize_grab_cancel(struct weston_pointer_grab *grab)
1666{
1667 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1668
1669 shell_grab_end(&resize->base);
1670 free(grab);
1671}
1672
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001673static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001674 noop_grab_focus,
1675 resize_grab_motion,
1676 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001677 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678};
1679
Giulio Camuffob8366642013-04-25 13:57:46 +03001680/*
1681 * Returns the bounding box of a surface and all its sub-surfaces,
1682 * in the surface coordinates system. */
1683static void
1684surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1685 int32_t *y, int32_t *w, int32_t *h) {
1686 pixman_region32_t region;
1687 pixman_box32_t *box;
1688 struct weston_subsurface *subsurface;
1689
1690 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691 surface->width,
1692 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001693
1694 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1695 pixman_region32_union_rect(&region, &region,
1696 subsurface->position.x,
1697 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 subsurface->surface->width,
1699 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001700 }
1701
1702 box = pixman_region32_extents(&region);
1703 if (x)
1704 *x = box->x1;
1705 if (y)
1706 *y = box->y1;
1707 if (w)
1708 *w = box->x2 - box->x1;
1709 if (h)
1710 *h = box->y2 - box->y1;
1711
1712 pixman_region32_fini(&region);
1713}
1714
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715static int
1716surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001717 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001718{
1719 struct weston_resize_grab *resize;
1720
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001721 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1722 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001723 return 0;
1724
1725 if (edges == 0 || edges > 15 ||
1726 (edges & 3) == 3 || (edges & 12) == 12)
1727 return 0;
1728
1729 resize = malloc(sizeof *resize);
1730 if (!resize)
1731 return -1;
1732
1733 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001734 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1735 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736
1737 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001738 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739
1740 return 0;
1741}
1742
1743static void
1744shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1745 struct wl_resource *seat_resource, uint32_t serial,
1746 uint32_t edges)
1747{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001748 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001749 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001750 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751
1752 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1753 return;
1754
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001756 if (seat->pointer->button_count == 0 ||
1757 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001758 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001759 return;
1760
Kristian Høgsberge3148752013-05-06 23:19:49 -04001761 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762 wl_resource_post_no_memory(resource);
1763}
1764
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001765static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001766end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1767
1768static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001769busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001770{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001771 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001772 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001773 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001774 wl_fixed_t sx, sy;
1775
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 view = weston_compositor_pick_view(pointer->seat->compositor,
1777 pointer->x, pointer->y,
1778 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001781 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001782}
1783
1784static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001785busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1786 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001787{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001788 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001789}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001790
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001791static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001792busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001793 uint32_t time, uint32_t button, uint32_t state)
1794{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001795 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001796 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001797 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001798
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001799 if (shsurf && button == BTN_LEFT && state) {
1800 activate(shsurf->shell, shsurf->surface, seat);
1801 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001802 } else if (shsurf && button == BTN_RIGHT && state) {
1803 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001804 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001805 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001806}
1807
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001808static void
1809busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1810{
1811 struct shell_grab *grab = (struct shell_grab *) base;
1812
1813 shell_grab_end(grab);
1814 free(grab);
1815}
1816
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001817static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001818 busy_cursor_grab_focus,
1819 busy_cursor_grab_motion,
1820 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001821 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822};
1823
1824static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001825set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826{
1827 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001828
1829 grab = malloc(sizeof *grab);
1830 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001831 return;
1832
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001833 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1834 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001835}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001836
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001837static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001838end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001839{
1840 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1841
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001842 if (grab->grab.interface == &busy_cursor_grab_interface &&
1843 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001844 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001845 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001846 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001847}
1848
Scott Moreau9521d5e2012-04-19 13:06:17 -06001849static void
1850ping_timer_destroy(struct shell_surface *shsurf)
1851{
1852 if (!shsurf || !shsurf->ping_timer)
1853 return;
1854
1855 if (shsurf->ping_timer->source)
1856 wl_event_source_remove(shsurf->ping_timer->source);
1857
1858 free(shsurf->ping_timer);
1859 shsurf->ping_timer = NULL;
1860}
1861
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001862static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001863ping_timeout_handler(void *data)
1864{
1865 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001866 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001867
Scott Moreau9521d5e2012-04-19 13:06:17 -06001868 /* Client is not responding */
1869 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001870
1871 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001872 if (seat->pointer->focus &&
1873 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001874 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001875
1876 return 1;
1877}
1878
1879static void
1880ping_handler(struct weston_surface *surface, uint32_t serial)
1881{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001882 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001883 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001884 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001885
1886 if (!shsurf)
1887 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001888 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001889 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001890
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001891 if (shsurf->surface == shsurf->shell->grab_surface)
1892 return;
1893
Scott Moreauff1db4a2012-04-17 19:06:18 -06001894 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001895 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001896 if (!shsurf->ping_timer)
1897 return;
1898
1899 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001900 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1901 shsurf->ping_timer->source =
1902 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1903 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1904
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001905 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001906 }
1907}
1908
1909static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001910handle_pointer_focus(struct wl_listener *listener, void *data)
1911{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001912 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001914 struct weston_compositor *compositor;
1915 struct shell_surface *shsurf;
1916 uint32_t serial;
1917
Jason Ekstranda7af7042013-10-12 22:38:11 -05001918 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001919 return;
1920
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 compositor = view->surface->compositor;
1922 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001923
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001924 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001925 set_busy_cursor(shsurf, pointer);
1926 } else {
1927 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001929 }
1930}
1931
1932static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001933create_pointer_focus_listener(struct weston_seat *seat)
1934{
1935 struct wl_listener *listener;
1936
Kristian Høgsberge3148752013-05-06 23:19:49 -04001937 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001938 return;
1939
1940 listener = malloc(sizeof *listener);
1941 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001942 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001943}
1944
1945static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001946shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1947 uint32_t serial)
1948{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001949 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001950 struct weston_seat *seat;
1951 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001952
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001953 if (shsurf->ping_timer == NULL)
1954 /* Just ignore unsolicited pong. */
1955 return;
1956
Scott Moreauff1db4a2012-04-17 19:06:18 -06001957 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001958 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001959 wl_list_for_each(seat, &ec->seat_list, link) {
1960 if(seat->pointer)
1961 end_busy_cursor(shsurf, seat->pointer);
1962 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001963 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001964 }
1965}
1966
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001967static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001968set_title(struct shell_surface *shsurf, const char *title)
1969{
1970 free(shsurf->title);
1971 shsurf->title = strdup(title);
1972}
1973
1974static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001975shell_surface_set_title(struct wl_client *client,
1976 struct wl_resource *resource, const char *title)
1977{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001978 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001979
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001980 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001981}
1982
1983static void
1984shell_surface_set_class(struct wl_client *client,
1985 struct wl_resource *resource, const char *class)
1986{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001987 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001988
1989 free(shsurf->class);
1990 shsurf->class = strdup(class);
1991}
1992
Alex Wu4539b082012-03-01 12:57:46 +08001993static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001994restore_output_mode(struct weston_output *output)
1995{
Hardening57388e42013-09-18 23:56:36 +02001996 if (output->current_mode != output->original_mode ||
1997 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001998 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001999 output->original_mode,
2000 output->original_scale,
2001 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002002}
2003
2004static void
2005restore_all_output_modes(struct weston_compositor *compositor)
2006{
2007 struct weston_output *output;
2008
2009 wl_list_for_each(output, &compositor->output_list, link)
2010 restore_output_mode(output);
2011}
2012
2013static void
Alex Wu4539b082012-03-01 12:57:46 +08002014shell_unset_fullscreen(struct shell_surface *shsurf)
2015{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002016 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08002017 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08002018 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2019 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002020 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002021 }
Alex Wu4539b082012-03-01 12:57:46 +08002022 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2023 shsurf->fullscreen.framerate = 0;
2024 wl_list_remove(&shsurf->fullscreen.transform.link);
2025 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002026 if (shsurf->fullscreen.black_view)
2027 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2028 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002029 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 weston_view_set_position(shsurf->view,
2031 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002032 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002033 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002034 &shsurf->rotation.transform.link);
2035 shsurf->saved_rotation_valid = false;
2036 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002037
2038 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039 wl_list_remove(&shsurf->view->layer_link);
2040 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08002041}
2042
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002043static void
2044shell_unset_maximized(struct shell_surface *shsurf)
2045{
2046 struct workspace *ws;
2047 /* undo all maximized things here */
2048 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 weston_view_set_position(shsurf->view,
2050 shsurf->saved_x,
2051 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002052
2053 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 wl_list_insert(&shsurf->view->geometry.transformation_list,
2055 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002056 shsurf->saved_rotation_valid = false;
2057 }
2058
2059 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 wl_list_remove(&shsurf->view->layer_link);
2061 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002062}
2063
Pekka Paalanen98262232011-12-01 10:42:22 +02002064static int
2065reset_shell_surface_type(struct shell_surface *surface)
2066{
2067 switch (surface->type) {
2068 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08002069 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002070 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002071 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002072 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08002073 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02002074 case SHELL_SURFACE_NONE:
2075 case SHELL_SURFACE_TOPLEVEL:
2076 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002077 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002078 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02002079 break;
2080 }
2081
2082 surface->type = SHELL_SURFACE_NONE;
2083 return 0;
2084}
2085
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002086static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002087set_surface_type(struct shell_surface *shsurf)
2088{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002089 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002091
2092 reset_shell_surface_type(shsurf);
2093
2094 shsurf->type = shsurf->next_type;
2095 shsurf->next_type = SHELL_SURFACE_NONE;
2096
2097 switch (shsurf->type) {
2098 case SHELL_SURFACE_TOPLEVEL:
2099 break;
2100 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002101 if (pev)
2102 weston_view_set_position(shsurf->view,
2103 pev->geometry.x + shsurf->transient.x,
2104 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002105 break;
2106
2107 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002108 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 shsurf->saved_x = shsurf->view->geometry.x;
2110 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002111 shsurf->saved_position_valid = true;
2112
2113 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2114 wl_list_remove(&shsurf->rotation.transform.link);
2115 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002117 shsurf->saved_rotation_valid = true;
2118 }
2119 break;
2120
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002121 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 weston_view_set_position(shsurf->view, shsurf->transient.x,
2123 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002124 break;
2125
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002126 default:
2127 break;
2128 }
2129}
2130
2131static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002132set_toplevel(struct shell_surface *shsurf)
2133{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002134 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002135}
2136
2137static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002138shell_surface_set_toplevel(struct wl_client *client,
2139 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002140{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002141 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002142
Tiago Vignattibc052c92012-04-19 16:18:18 +03002143 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002144}
2145
2146static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04002147set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002148 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04002149{
2150 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002151 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002152 shsurf->transient.x = x;
2153 shsurf->transient.y = y;
2154 shsurf->transient.flags = flags;
2155 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
2156}
2157
2158static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002159shell_surface_set_transient(struct wl_client *client,
2160 struct wl_resource *resource,
2161 struct wl_resource *parent_resource,
2162 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002163{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002164 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002165 struct weston_surface *parent =
2166 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002167
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002168 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002169}
2170
Tiago Vignattibe143262012-04-16 17:31:41 +03002171static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002172shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002173{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002174 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002175}
2176
2177static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002178get_output_panel_height(struct desktop_shell *shell,
2179 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08002180{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08002182 int panel_height = 0;
2183
2184 if (!output)
2185 return 0;
2186
Jason Ekstranda7af7042013-10-12 22:38:11 -05002187 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2188 if (view->surface->output == output) {
2189 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08002190 break;
2191 }
2192 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002193
Juan Zhao96879df2012-02-07 08:45:41 +08002194 return panel_height;
2195}
2196
2197static void
2198shell_surface_set_maximized(struct wl_client *client,
2199 struct wl_resource *resource,
2200 struct wl_resource *output_resource )
2201{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002202 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08002203 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03002204 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08002205 uint32_t edges = 0, panel_height = 0;
2206
2207 /* get the default output, if the client set it as NULL
2208 check whether the ouput is available */
2209 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002210 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002211 else if (es->output)
2212 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002213 else
2214 shsurf->output = get_default_output(es->compositor);
2215
Tiago Vignattibe143262012-04-16 17:31:41 +03002216 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01002217 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002218 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002219
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002220 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002221 shsurf->output->width,
2222 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002223
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002224 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002225}
2226
Alex Wu21858432012-04-01 20:13:08 +08002227static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002228black_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 +08002229
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002231create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002232 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002233 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002234{
2235 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002237
2238 surface = weston_surface_create(ec);
2239 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002240 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002241 return NULL;
2242 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 view = weston_view_create(surface);
2244 if (surface == NULL) {
2245 weston_log("no memory\n");
2246 weston_surface_destroy(surface);
2247 return NULL;
2248 }
Alex Wu4539b082012-03-01 12:57:46 +08002249
Alex Wu21858432012-04-01 20:13:08 +08002250 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002251 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002252 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002253 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002254 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002255 pixman_region32_fini(&surface->input);
2256 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002257
Jason Ekstranda7af7042013-10-12 22:38:11 -05002258 weston_view_configure(view, x, y, w, h);
2259
2260 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002261}
2262
2263/* Create black surface and append it to the associated fullscreen surface.
2264 * Handle size dismatch and positioning according to the method. */
2265static void
2266shell_configure_fullscreen(struct shell_surface *shsurf)
2267{
2268 struct weston_output *output = shsurf->fullscreen_output;
2269 struct weston_surface *surface = shsurf->surface;
2270 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002271 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002272 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002273
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002274 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2275 restore_output_mode(output);
2276
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 if (!shsurf->fullscreen.black_view)
2278 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002279 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002280 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002281 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002282 output->width,
2283 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002284
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2286 wl_list_insert(&shsurf->view->layer_link,
2287 &shsurf->fullscreen.black_view->layer_link);
2288 shsurf->fullscreen.black_view->surface->output = output;
2289 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002290
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002292 &surf_width, &surf_height);
2293
Alex Wu4539b082012-03-01 12:57:46 +08002294 switch (shsurf->fullscreen.type) {
2295 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002296 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002297 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002298 break;
2299 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002300 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002301 if (output->width == surf_width &&
2302 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002303 weston_view_set_position(shsurf->view,
2304 output->x - surf_x,
2305 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002306 break;
2307 }
2308
Alex Wu4539b082012-03-01 12:57:46 +08002309 matrix = &shsurf->fullscreen.transform.matrix;
2310 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002311
Scott Moreau1bad5db2012-08-18 01:04:05 -06002312 output_aspect = (float) output->width /
2313 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002314 /* XXX: Use surf_width and surf_height here? */
2315 surface_aspect = (float) surface->width /
2316 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002317 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002318 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002319 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002320 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002321 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002322 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002323
Alex Wu4539b082012-03-01 12:57:46 +08002324 weston_matrix_scale(matrix, scale, scale, 1);
2325 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002326 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002327 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002328 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2329 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002331
Alex Wu4539b082012-03-01 12:57:46 +08002332 break;
2333 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002334 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002335 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002336 surf_width * surface->buffer_viewport.scale,
2337 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002338 shsurf->fullscreen.framerate};
2339
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002340 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002341 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002342 weston_view_set_position(shsurf->view,
2343 output->x - surf_x,
2344 output->y - surf_y);
2345 weston_view_configure(shsurf->fullscreen.black_view,
2346 output->x - surf_x,
2347 output->y - surf_y,
2348 output->width,
2349 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002350 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002351 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002352 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002353 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002354 }
Alex Wubd3354b2012-04-17 17:20:49 +08002355 }
Alex Wu4539b082012-03-01 12:57:46 +08002356 break;
2357 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002358 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002359 break;
2360 default:
2361 break;
2362 }
2363}
2364
2365/* make the fullscreen and black surface at the top */
2366static void
2367shell_stack_fullscreen(struct shell_surface *shsurf)
2368{
Alex Wubd3354b2012-04-17 17:20:49 +08002369 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002370 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002371
Jason Ekstranda7af7042013-10-12 22:38:11 -05002372 wl_list_remove(&shsurf->view->layer_link);
2373 wl_list_insert(&shell->fullscreen_layer.view_list,
2374 &shsurf->view->layer_link);
2375 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002376
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377 if (!shsurf->fullscreen.black_view)
2378 shsurf->fullscreen.black_view =
2379 create_black_surface(shsurf->surface->compositor,
2380 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002381 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002382 output->width,
2383 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002384
Jason Ekstranda7af7042013-10-12 22:38:11 -05002385 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2386 wl_list_insert(&shsurf->view->layer_link,
2387 &shsurf->fullscreen.black_view->layer_link);
2388 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002389}
2390
2391static void
2392shell_map_fullscreen(struct shell_surface *shsurf)
2393{
Alex Wu4539b082012-03-01 12:57:46 +08002394 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002395 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002396}
2397
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002398static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002399set_fullscreen(struct shell_surface *shsurf,
2400 uint32_t method,
2401 uint32_t framerate,
2402 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002403{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002404 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002405
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002406 if (output)
2407 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002408 else if (es->output)
2409 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002410 else
2411 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002412
Alex Wu4539b082012-03-01 12:57:46 +08002413 shsurf->fullscreen_output = shsurf->output;
2414 shsurf->fullscreen.type = method;
2415 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002416 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002417
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002418 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002419 shsurf->output->width,
2420 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002421}
2422
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002423static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002424shell_surface_set_fullscreen(struct wl_client *client,
2425 struct wl_resource *resource,
2426 uint32_t method,
2427 uint32_t framerate,
2428 struct wl_resource *output_resource)
2429{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002430 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002431 struct weston_output *output;
2432
2433 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002434 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002435 else
2436 output = NULL;
2437
2438 set_fullscreen(shsurf, method, framerate, output);
2439}
2440
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002441static void
2442set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2443{
2444 /* XXX: using the same fields for transient type */
2445 shsurf->transient.x = x;
2446 shsurf->transient.y = y;
2447 shsurf->transient.flags = flags;
2448 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2449}
2450
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002451static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002452
2453static void
2454destroy_shell_seat(struct wl_listener *listener, void *data)
2455{
2456 struct shell_seat *shseat =
2457 container_of(listener,
2458 struct shell_seat, seat_destroy_listener);
2459 struct shell_surface *shsurf, *prev = NULL;
2460
2461 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002462 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002463 shseat->popup_grab.client = NULL;
2464
2465 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2466 shsurf->popup.shseat = NULL;
2467 if (prev) {
2468 wl_list_init(&prev->popup.grab_link);
2469 }
2470 prev = shsurf;
2471 }
2472 wl_list_init(&prev->popup.grab_link);
2473 }
2474
2475 wl_list_remove(&shseat->seat_destroy_listener.link);
2476 free(shseat);
2477}
2478
2479static struct shell_seat *
2480create_shell_seat(struct weston_seat *seat)
2481{
2482 struct shell_seat *shseat;
2483
2484 shseat = calloc(1, sizeof *shseat);
2485 if (!shseat) {
2486 weston_log("no memory to allocate shell seat\n");
2487 return NULL;
2488 }
2489
2490 shseat->seat = seat;
2491 wl_list_init(&shseat->popup_grab.surfaces_list);
2492
2493 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2494 wl_signal_add(&seat->destroy_signal,
2495 &shseat->seat_destroy_listener);
2496
2497 return shseat;
2498}
2499
2500static struct shell_seat *
2501get_shell_seat(struct weston_seat *seat)
2502{
2503 struct wl_listener *listener;
2504
2505 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2506 if (listener == NULL)
2507 return create_shell_seat(seat);
2508
2509 return container_of(listener,
2510 struct shell_seat, seat_destroy_listener);
2511}
2512
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002513static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002514popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002515{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002516 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002517 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002518 struct shell_seat *shseat =
2519 container_of(grab, struct shell_seat, popup_grab.grab);
2520 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002521 wl_fixed_t sx, sy;
2522
Jason Ekstranda7af7042013-10-12 22:38:11 -05002523 view = weston_compositor_pick_view(pointer->seat->compositor,
2524 pointer->x, pointer->y,
2525 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002526
Jason Ekstranda7af7042013-10-12 22:38:11 -05002527 if (view && view->surface->resource &&
2528 wl_resource_get_client(view->surface->resource) == client) {
2529 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002530 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002531 weston_pointer_set_focus(pointer, NULL,
2532 wl_fixed_from_int(0),
2533 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002534 }
2535}
2536
2537static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002538popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2539 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002540{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002541 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002542 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002543 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002544
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002545 weston_pointer_move(pointer, x, y);
2546
Neil Roberts96d790e2013-09-19 17:32:00 +01002547 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002548 weston_view_from_global_fixed(pointer->focus,
2549 pointer->x, pointer->y,
2550 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002551 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002552 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002553}
2554
2555static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002556popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002557 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002558{
2559 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002560 struct shell_seat *shseat =
2561 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002562 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002563 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002564 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002565 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002566
Neil Roberts96d790e2013-09-19 17:32:00 +01002567 resource_list = &grab->pointer->focus_resource_list;
2568 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002569 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002570 wl_resource_for_each(resource, resource_list) {
2571 wl_pointer_send_button(resource, serial,
2572 time, button, state);
2573 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002574 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002575 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002576 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002577 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002578 }
2579
Daniel Stone4dbadb12012-05-30 16:31:51 +01002580 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002581 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002582}
2583
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002584static void
2585popup_grab_cancel(struct weston_pointer_grab *grab)
2586{
2587 popup_grab_end(grab->pointer);
2588}
2589
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002590static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002591 popup_grab_focus,
2592 popup_grab_motion,
2593 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002594 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002595};
2596
2597static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002598popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002599{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002600 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002601 struct shell_seat *shseat =
2602 container_of(grab, struct shell_seat, popup_grab.grab);
2603 struct shell_surface *shsurf;
2604 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002605
2606 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002607 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002608 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002609 shseat->popup_grab.grab.interface = NULL;
2610 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002611 /* Send the popup_done event to all the popups open */
2612 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002613 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002614 shsurf->popup.shseat = NULL;
2615 if (prev) {
2616 wl_list_init(&prev->popup.grab_link);
2617 }
2618 prev = shsurf;
2619 }
2620 wl_list_init(&prev->popup.grab_link);
2621 wl_list_init(&shseat->popup_grab.surfaces_list);
2622 }
2623}
2624
2625static void
2626add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2627{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002628 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002629
2630 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002631 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002632 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002633 /* We must make sure here that this popup was opened after
2634 * a mouse press, and not just by moving around with other
2635 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002636 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002637 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002638
Rob Bradforddfe31052013-06-26 19:49:11 +01002639 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002640 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002641 } else {
2642 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002643 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002644}
2645
2646static void
2647remove_popup_grab(struct shell_surface *shsurf)
2648{
2649 struct shell_seat *shseat = shsurf->popup.shseat;
2650
2651 wl_list_remove(&shsurf->popup.grab_link);
2652 wl_list_init(&shsurf->popup.grab_link);
2653 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002654 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002655 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002656 }
2657}
2658
2659static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002660shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002661{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002662 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002663 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002664
Jason Ekstranda7af7042013-10-12 22:38:11 -05002665 shsurf->surface->output = parent_view->output;
2666 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002667
Jason Ekstranda7af7042013-10-12 22:38:11 -05002668 weston_view_set_transform_parent(shsurf->view, parent_view);
2669 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2670 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002671
Kristian Høgsberge3148752013-05-06 23:19:49 -04002672 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002673 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002674 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002675 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002676 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002677 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002678}
2679
2680static void
2681shell_surface_set_popup(struct wl_client *client,
2682 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002683 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002684 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002685 struct wl_resource *parent_resource,
2686 int32_t x, int32_t y, uint32_t flags)
2687{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002688 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002689
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002690 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002691 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002692 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002693 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002694 shsurf->popup.x = x;
2695 shsurf->popup.y = y;
2696}
2697
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002698static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002699 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002700 shell_surface_move,
2701 shell_surface_resize,
2702 shell_surface_set_toplevel,
2703 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002704 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002705 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002706 shell_surface_set_maximized,
2707 shell_surface_set_title,
2708 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002709};
2710
2711static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002712destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002713{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002714 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2715
Giulio Camuffo5085a752013-03-25 21:42:45 +01002716 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2717 remove_popup_grab(shsurf);
2718 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002719
Alex Wubd3354b2012-04-17 17:20:49 +08002720 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002721 shell_surface_is_top_fullscreen(shsurf))
2722 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002723
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 if (shsurf->fullscreen.black_view)
2725 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002726
Alex Wubd3354b2012-04-17 17:20:49 +08002727 /* As destroy_resource() use wl_list_for_each_safe(),
2728 * we can always remove the listener.
2729 */
2730 wl_list_remove(&shsurf->surface_destroy_listener.link);
2731 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002732 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002733 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002734
Jason Ekstranda7af7042013-10-12 22:38:11 -05002735 weston_view_destroy(shsurf->view);
2736
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002737 wl_list_remove(&shsurf->link);
2738 free(shsurf);
2739}
2740
2741static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002742shell_destroy_shell_surface(struct wl_resource *resource)
2743{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002744 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002745
2746 destroy_shell_surface(shsurf);
2747}
2748
2749static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002750shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002751{
2752 struct shell_surface *shsurf = container_of(listener,
2753 struct shell_surface,
2754 surface_destroy_listener);
2755
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002756 if (shsurf->resource)
2757 wl_resource_destroy(shsurf->resource);
2758 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002759 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002760}
2761
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002762static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002763shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002764
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002765static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002766get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002767{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002768 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002769 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002770 else
2771 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002772}
2773
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002774static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002775create_shell_surface(void *shell, struct weston_surface *surface,
2776 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002777{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002778 struct shell_surface *shsurf;
2779
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002780 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002781 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002782 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002783 }
2784
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002785 shsurf = calloc(1, sizeof *shsurf);
2786 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002787 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002788 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002789 }
2790
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 shsurf->view = weston_view_create(surface);
2792 if (!shsurf->view) {
2793 weston_log("no memory to allocate shell surface\n");
2794 free(shsurf);
2795 return NULL;
2796 }
2797
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002798 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002799 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002800
Tiago Vignattibc052c92012-04-19 16:18:18 +03002801 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002802 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002803 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002804 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002805 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002806 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2807 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002808 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002809 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002810 wl_list_init(&shsurf->fullscreen.transform.link);
2811
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002812 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002813 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002814 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002815 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002816
2817 /* init link so its safe to always remove it in destroy_shell_surface */
2818 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002819 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002820
Pekka Paalanen460099f2012-01-20 16:48:25 +02002821 /* empty when not in use */
2822 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002823 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002824
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002825 wl_list_init(&shsurf->workspace_transform.link);
2826
Pekka Paalanen98262232011-12-01 10:42:22 +02002827 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002828 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002829
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002830 shsurf->client = client;
2831
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002832 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002833}
2834
Jason Ekstranda7af7042013-10-12 22:38:11 -05002835static struct weston_view *
2836get_primary_view(void *shell, struct shell_surface *shsurf)
2837{
2838 return shsurf->view;
2839}
2840
Tiago Vignattibc052c92012-04-19 16:18:18 +03002841static void
2842shell_get_shell_surface(struct wl_client *client,
2843 struct wl_resource *resource,
2844 uint32_t id,
2845 struct wl_resource *surface_resource)
2846{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002847 struct weston_surface *surface =
2848 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002849 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002850 struct shell_surface *shsurf;
2851
2852 if (get_shell_surface(surface)) {
2853 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002854 WL_DISPLAY_ERROR_INVALID_OBJECT,
2855 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002856 return;
2857 }
2858
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002859 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002860 if (!shsurf) {
2861 wl_resource_post_error(surface_resource,
2862 WL_DISPLAY_ERROR_INVALID_OBJECT,
2863 "surface->configure already set");
2864 return;
2865 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002866
Jason Ekstranda85118c2013-06-27 20:17:02 -05002867 shsurf->resource =
2868 wl_resource_create(client,
2869 &wl_shell_surface_interface, 1, id);
2870 wl_resource_set_implementation(shsurf->resource,
2871 &shell_surface_implementation,
2872 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002873}
2874
2875static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002876 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002877};
2878
Kristian Høgsberg07937562011-04-12 17:25:42 -04002879static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002880shell_fade(struct desktop_shell *shell, enum fade_type type);
2881
2882static int
2883screensaver_timeout(void *data)
2884{
2885 struct desktop_shell *shell = data;
2886
2887 shell_fade(shell, FADE_OUT);
2888
2889 return 1;
2890}
2891
2892static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002893handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002894{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002895 struct desktop_shell *shell =
2896 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002897
Pekka Paalanen18027e52011-12-02 16:31:49 +02002898 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002899
2900 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002901 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002902}
2903
2904static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002905launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002906{
2907 if (shell->screensaver.binding)
2908 return;
2909
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002910 if (!shell->screensaver.path) {
2911 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002912 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002913 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002914
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002915 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002916 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002917 return;
2918 }
2919
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002920 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002921 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002922 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002923 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002924}
2925
2926static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002927terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002928{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002929 if (shell->screensaver.process.pid == 0)
2930 return;
2931
2932 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002933}
2934
2935static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002937{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002938 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002939
Giulio Camuffo184df502013-02-21 11:29:21 +01002940 if (width == 0)
2941 return;
2942
Jason Ekstranda7af7042013-10-12 22:38:11 -05002943 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2944 if (v->output == ev->output && v != ev) {
2945 weston_view_unmap(v);
2946 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002947 }
2948 }
2949
Jason Ekstranda7af7042013-10-12 22:38:11 -05002950 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002951
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 if (wl_list_empty(&ev->layer_link)) {
2953 wl_list_insert(&layer->view_list, &ev->layer_link);
2954 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002955 }
2956}
2957
2958static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002959background_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 -04002960{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002961 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002962 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002963
Jason Ekstranda7af7042013-10-12 22:38:11 -05002964 view = container_of(es->views.next, struct weston_view, surface_link);
2965
2966 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002967}
2968
2969static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002970desktop_shell_set_background(struct wl_client *client,
2971 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002972 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002973 struct wl_resource *surface_resource)
2974{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002975 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002976 struct weston_surface *surface =
2977 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002978 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002979
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002980 if (surface->configure) {
2981 wl_resource_post_error(surface_resource,
2982 WL_DISPLAY_ERROR_INVALID_OBJECT,
2983 "surface role already assigned");
2984 return;
2985 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002986
Jason Ekstranda7af7042013-10-12 22:38:11 -05002987 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2988 weston_view_destroy(view);
2989 view = weston_view_create(surface);
2990
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002991 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002992 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002993 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002994 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002995 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002996 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002997 surface->output->width,
2998 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002999}
3000
3001static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003002panel_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 -04003003{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003004 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003005 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003006
Jason Ekstranda7af7042013-10-12 22:38:11 -05003007 view = container_of(es->views.next, struct weston_view, surface_link);
3008
3009 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003010}
3011
3012static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003013desktop_shell_set_panel(struct wl_client *client,
3014 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003015 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003016 struct wl_resource *surface_resource)
3017{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003018 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003019 struct weston_surface *surface =
3020 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003021 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003022
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003023 if (surface->configure) {
3024 wl_resource_post_error(surface_resource,
3025 WL_DISPLAY_ERROR_INVALID_OBJECT,
3026 "surface role already assigned");
3027 return;
3028 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003029
Jason Ekstranda7af7042013-10-12 22:38:11 -05003030 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3031 weston_view_destroy(view);
3032 view = weston_view_create(surface);
3033
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003034 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003035 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003036 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003037 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003038 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003039 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003040 surface->output->width,
3041 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003042}
3043
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003044static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003045lock_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 -04003046{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003047 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003048 struct weston_view *view;
3049
3050 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003051
Giulio Camuffo184df502013-02-21 11:29:21 +01003052 if (width == 0)
3053 return;
3054
Jason Ekstranda7af7042013-10-12 22:38:11 -05003055 surface->width = width;
3056 surface->height = height;
3057 view->geometry.width = width;
3058 view->geometry.height = height;
3059 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003060
3061 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003062 wl_list_insert(&shell->lock_layer.view_list,
3063 &view->layer_link);
3064 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003065 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003066 }
3067}
3068
3069static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003070handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003071{
Tiago Vignattibe143262012-04-16 17:31:41 +03003072 struct desktop_shell *shell =
3073 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003074
Martin Minarik6d118362012-06-07 18:01:59 +02003075 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003076 shell->lock_surface = NULL;
3077}
3078
3079static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003080desktop_shell_set_lock_surface(struct wl_client *client,
3081 struct wl_resource *resource,
3082 struct wl_resource *surface_resource)
3083{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003084 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003085 struct weston_surface *surface =
3086 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003087
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003088 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003089
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003090 if (!shell->locked)
3091 return;
3092
Pekka Paalanen98262232011-12-01 10:42:22 +02003093 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003094
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003095 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003096 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003097 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003098
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003099 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003100 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003101 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003102}
3103
3104static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003105resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003106{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003107 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003108
Pekka Paalanen77346a62011-11-30 16:26:35 +02003109 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003110
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003111 wl_list_remove(&shell->lock_layer.link);
3112 wl_list_insert(&shell->compositor->cursor_layer.link,
3113 &shell->fullscreen_layer.link);
3114 wl_list_insert(&shell->fullscreen_layer.link,
3115 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003116 if (shell->showing_input_panels) {
3117 wl_list_insert(&shell->panel_layer.link,
3118 &shell->input_panel_layer.link);
3119 wl_list_insert(&shell->input_panel_layer.link,
3120 &ws->layer.link);
3121 } else {
3122 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3123 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003124
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003125 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003126
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003127 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003128 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003129 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003130}
3131
3132static void
3133desktop_shell_unlock(struct wl_client *client,
3134 struct wl_resource *resource)
3135{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003136 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003137
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003138 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003139
3140 if (shell->locked)
3141 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003142}
3143
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003144static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003145desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003146 struct wl_resource *resource,
3147 struct wl_resource *surface_resource)
3148{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003149 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003150
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003151 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003152 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003153}
3154
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003155static void
3156desktop_shell_desktop_ready(struct wl_client *client,
3157 struct wl_resource *resource)
3158{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003159 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003160
3161 shell_fade_startup(shell);
3162}
3163
Kristian Høgsberg75840622011-09-06 13:48:16 -04003164static const struct desktop_shell_interface desktop_shell_implementation = {
3165 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003166 desktop_shell_set_panel,
3167 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003168 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003169 desktop_shell_set_grab_surface,
3170 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003171};
3172
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003173static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003174get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003175{
3176 struct shell_surface *shsurf;
3177
3178 shsurf = get_shell_surface(surface);
3179 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003180 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003181 return shsurf->type;
3182}
3183
Kristian Høgsberg75840622011-09-06 13:48:16 -04003184static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003185move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003186{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003187 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003188 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003189 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003190
Pekka Paalanen01388e22013-04-25 13:57:44 +03003191 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003192 if (surface == NULL)
3193 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003194
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003195 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003196 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3197 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003198 return;
3199
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003200 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003201}
3202
3203static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003204touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3205{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003206 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003207 struct weston_surface *surface;
3208 struct shell_surface *shsurf;
3209
3210 surface = weston_surface_get_main_surface(focus);
3211 if (surface == NULL)
3212 return;
3213
3214 shsurf = get_shell_surface(surface);
3215 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3216 shsurf->type == SHELL_SURFACE_MAXIMIZED)
3217 return;
3218
3219 surface_touch_move(shsurf, (struct weston_seat *) seat);
3220}
3221
3222static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003223resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003224{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003225 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003226 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003227 uint32_t edges = 0;
3228 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003229 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003230
Pekka Paalanen01388e22013-04-25 13:57:44 +03003231 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003232 if (surface == NULL)
3233 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003234
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003235 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003236 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
3237 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003238 return;
3239
Jason Ekstranda7af7042013-10-12 22:38:11 -05003240 weston_view_from_global(shsurf->view,
3241 wl_fixed_to_int(seat->pointer->grab_x),
3242 wl_fixed_to_int(seat->pointer->grab_y),
3243 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003244
Jason Ekstranda7af7042013-10-12 22:38:11 -05003245 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003246 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003247 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003248 edges |= 0;
3249 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003250 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003251
Jason Ekstranda7af7042013-10-12 22:38:11 -05003252 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003253 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003254 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003255 edges |= 0;
3256 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003257 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003258
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003259 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003260}
3261
3262static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003263surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003264 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003265{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003266 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003267 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003268 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003269 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003270
Pekka Paalanen01388e22013-04-25 13:57:44 +03003271 /* XXX: broken for windows containing sub-surfaces */
3272 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003273 if (surface == NULL)
3274 return;
3275
3276 shsurf = get_shell_surface(surface);
3277 if (!shsurf)
3278 return;
3279
Jason Ekstranda7af7042013-10-12 22:38:11 -05003280 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003281
Jason Ekstranda7af7042013-10-12 22:38:11 -05003282 if (shsurf->view->alpha > 1.0)
3283 shsurf->view->alpha = 1.0;
3284 if (shsurf->view->alpha < step)
3285 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003286
Jason Ekstranda7af7042013-10-12 22:38:11 -05003287 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003288 weston_surface_damage(surface);
3289}
3290
3291static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003292do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003293 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003294{
Daniel Stone37816df2012-05-16 18:45:18 +01003295 struct weston_seat *ws = (struct weston_seat *) seat;
3296 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003297 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003298 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003299
3300 wl_list_for_each(output, &compositor->output_list, link) {
3301 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003302 wl_fixed_to_double(seat->pointer->x),
3303 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003304 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003305 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003306 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003307 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003308 increment = -output->zoom.increment;
3309 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003310 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003311 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003312 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003313 else
3314 increment = 0;
3315
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003316 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003317
Scott Moreaue6603982012-06-11 13:07:51 -06003318 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003319 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003320 else if (output->zoom.level > output->zoom.max_level)
3321 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003322 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003323 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003324 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003325
Scott Moreaue6603982012-06-11 13:07:51 -06003326 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003327
Jason Ekstranda7af7042013-10-12 22:38:11 -05003328 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003329 }
3330 }
3331}
3332
Scott Moreauccbf29d2012-02-22 14:21:41 -07003333static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003334zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003335 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003336{
3337 do_zoom(seat, time, 0, axis, value);
3338}
3339
3340static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003341zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003342 void *data)
3343{
3344 do_zoom(seat, time, key, 0, 0);
3345}
3346
3347static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003348terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003349 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003350{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003351 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003352
Daniel Stone325fc2d2012-05-30 16:31:58 +01003353 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003354}
3355
3356static void
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003357lower_fullscreen_layer(struct desktop_shell *shell);
3358
3359struct alt_tab {
3360 struct desktop_shell *shell;
3361 struct weston_keyboard_grab grab;
3362
3363 struct wl_list *current;
3364
3365 struct wl_list preview_list;
3366};
3367
3368struct alt_tab_preview {
3369 struct alt_tab *alt_tab;
3370
3371 struct weston_view *view;
3372 struct weston_transform transform;
3373
3374 struct wl_listener listener;
3375
3376 struct wl_list link;
3377};
3378
3379static void
3380alt_tab_next(struct alt_tab *alt_tab)
3381{
3382 alt_tab->current = alt_tab->current->next;
3383
3384 /* Make sure we're not pointing to the list header e.g. after
3385 * cycling through the whole list. */
3386 if (alt_tab->current->next == alt_tab->preview_list.next)
3387 alt_tab->current = alt_tab->current->next;
3388}
3389
3390static void
3391alt_tab_destroy(struct alt_tab *alt_tab)
3392{
3393 struct alt_tab_preview *preview, *next;
3394 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
3395
3396 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
3397 preview = wl_container_of(alt_tab->current, preview, link);
3398
3399 activate(alt_tab->shell, preview->view->surface,
3400 (struct weston_seat *) keyboard->seat);
3401 }
3402
3403 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
3404 wl_list_remove(&preview->link);
3405 wl_list_remove(&preview->listener.link);
3406 weston_view_destroy(preview->view);
3407 free(preview);
3408 }
3409
3410 weston_keyboard_end_grab(keyboard);
3411 if (keyboard->input_method_resource)
3412 keyboard->grab = &keyboard->input_method_grab;
3413
3414 free(alt_tab);
3415}
3416
3417static void
3418alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
3419{
3420 struct alt_tab_preview *preview =
3421 container_of(listener, struct alt_tab_preview, listener);
3422 struct alt_tab *alt_tab = preview->alt_tab;
3423 int advance = 0;
3424
3425 /* If the preview that we're removing is the currently selected one,
3426 * we want to move to the next one. So we move to ->prev and then
3427 * call _next() after removing the preview. */
3428 if (alt_tab->current == &preview->link) {
3429 alt_tab->current = alt_tab->current->prev;
3430 advance = 1;
3431 }
3432
3433 wl_list_remove(&preview->listener.link);
3434 wl_list_remove(&preview->link);
3435
3436 free(preview);
3437
3438 if (advance)
3439 alt_tab_next(alt_tab);
3440
3441 /* If the last preview goes away, stop the alt-tab */
3442 if (wl_list_empty(alt_tab->current))
3443 alt_tab_destroy(alt_tab);
3444}
3445
3446static void
3447alt_tab_key(struct weston_keyboard_grab *grab,
3448 uint32_t time, uint32_t key, uint32_t state_w)
3449{
3450 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3451 enum wl_keyboard_key_state state = state_w;
3452
3453 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3454 alt_tab_next(alt_tab);
3455}
3456
3457static void
3458alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
3459 uint32_t mods_depressed, uint32_t mods_latched,
3460 uint32_t mods_locked, uint32_t group)
3461{
3462 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3463 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3464
3465 if ((seat->modifier_state & MODIFIER_ALT) == 0)
3466 alt_tab_destroy(alt_tab);
3467}
3468
3469static void
3470alt_tab_cancel(struct weston_keyboard_grab *grab)
3471{
3472 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
3473
3474 alt_tab_destroy(alt_tab);
3475}
3476
3477static const struct weston_keyboard_grab_interface alt_tab_grab = {
3478 alt_tab_key,
3479 alt_tab_modifier,
3480 alt_tab_cancel,
3481};
3482
3483static int
3484view_for_alt_tab(struct weston_view *view)
3485{
3486 if (!get_shell_surface(view->surface))
3487 return 0;
3488
3489 if (get_shell_surface_type(view->surface) == SHELL_SURFACE_TRANSIENT)
3490 return 0;
3491
3492 if (view != get_default_view(view->surface))
3493 return 0;
3494
3495 return 1;
3496}
3497
3498static void
3499alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
3500 void *data)
3501{
3502 struct alt_tab *alt_tab;
3503 struct desktop_shell *shell = data;
3504 struct weston_output *output = get_default_output(shell->compositor);
3505 struct workspace *ws;
3506 struct weston_view *view;
3507 int num_surfaces = 0;
3508 int x, y, side, margin;
3509
3510 wl_list_for_each(view, &shell->compositor->view_list, link) {
3511
3512 if (view_for_alt_tab(view))
3513 num_surfaces++;
3514 }
3515
3516 if (!num_surfaces)
3517 return;
3518
3519 alt_tab = malloc(sizeof *alt_tab);
3520 if (!alt_tab)
3521 return;
3522
3523 alt_tab->shell = shell;
3524 wl_list_init(&alt_tab->preview_list);
3525 alt_tab->current = &alt_tab->preview_list;
3526
3527 restore_all_output_modes(shell->compositor);
3528 lower_fullscreen_layer(alt_tab->shell);
3529
3530 alt_tab->grab.interface = &alt_tab_grab;
3531 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
3532 weston_keyboard_set_focus(seat->keyboard, NULL);
3533
3534 ws = get_current_workspace(shell);
3535
3536 /* FIXME: add some visual candy e.g. prelight the selected view
3537 * and/or add a black surrounding background à la gnome-shell */
3538
3539 /* Determine the size for each preview */
3540 side = output->width / num_surfaces;
3541 if (side > 200)
3542 side = 200;
3543 margin = side / 4;
3544 side -= margin;
3545
3546 x = margin;
3547 y = (output->height - side) / 2;
3548
3549 /* Create a view for each surface */
3550 wl_list_for_each(view, &shell->compositor->view_list, link) {
3551 struct alt_tab_preview *preview;
3552 struct weston_view *v;
3553 float scale;
3554
3555 if (!view_for_alt_tab(view))
3556 continue;
3557
3558 preview = malloc(sizeof *preview);
3559 if (!preview) {
3560 alt_tab_destroy(alt_tab);
3561 return;
3562 }
3563
3564 preview->alt_tab = alt_tab;
3565
3566 preview->view = v = weston_view_create(view->surface);
3567 v->output = view->output;
3568 weston_view_restack(v, &ws->layer.view_list);
3569 weston_view_configure(v, x, y, view->geometry.width, view->geometry.height);
3570
3571 preview->listener.notify = alt_tab_handle_surface_destroy;
3572 wl_signal_add(&v->destroy_signal, &preview->listener);
3573
3574 if (view->geometry.width > view->geometry.height)
3575 scale = side / (float) view->geometry.width;
3576 else
3577 scale = side / (float) view->geometry.height;
3578
3579 wl_list_insert(&v->geometry.transformation_list,
3580 &preview->transform.link);
3581 weston_matrix_init(&preview->transform.matrix);
3582 weston_matrix_scale(&preview->transform.matrix,
3583 scale, scale, 1.0f);
3584
3585 weston_view_geometry_dirty(v);
3586 weston_compositor_schedule_repaint(v->surface->compositor);
3587
3588 /* Insert at the end of the list */
3589 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
3590
3591 x += side + margin;
3592 }
3593
3594 /* Start at the second preview so a simple <alt>tab changes window.
3595 * We set `current' to the first preview and not the second because
3596 * we're going to receive a key press callback for the initial
3597 * <alt>tab which will make `current' point to the second element. */
3598 alt_tab_next(alt_tab);
3599}
3600
3601static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003602rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3603 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003604{
3605 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003606 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003607 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003608 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003609 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003610
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003611 weston_pointer_move(pointer, x, y);
3612
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003613 if (!shsurf)
3614 return;
3615
Jason Ekstranda7af7042013-10-12 22:38:11 -05003616 cx = 0.5f * shsurf->view->geometry.width;
3617 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003618
Daniel Stone37816df2012-05-16 18:45:18 +01003619 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3620 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003621 r = sqrtf(dx * dx + dy * dy);
3622
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003623 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003624 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003625
3626 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003627 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003628 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003629
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003630 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003631 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003632
3633 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003634 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003635 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003636 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003637 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003638
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003639 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003640 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003641 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003642 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003643 wl_list_init(&shsurf->rotation.transform.link);
3644 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003645 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003646 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003647
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003648 /* We need to adjust the position of the surface
3649 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003650 cposx = shsurf->view->geometry.x + cx;
3651 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003652 dposx = rotate->center.x - cposx;
3653 dposy = rotate->center.y - cposy;
3654 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655 weston_view_set_position(shsurf->view,
3656 shsurf->view->geometry.x + dposx,
3657 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003658 }
3659
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003660 /* Repaint implies weston_surface_update_transform(), which
3661 * lazily applies the damage due to rotation update.
3662 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003663 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003664}
3665
3666static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003667rotate_grab_button(struct weston_pointer_grab *grab,
3668 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003669{
3670 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003671 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003672 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003673 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003674 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003675
Daniel Stone4dbadb12012-05-30 16:31:51 +01003676 if (pointer->button_count == 0 &&
3677 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003678 if (shsurf)
3679 weston_matrix_multiply(&shsurf->rotation.rotation,
3680 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003681 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003682 free(rotate);
3683 }
3684}
3685
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003686static void
3687rotate_grab_cancel(struct weston_pointer_grab *grab)
3688{
3689 struct rotate_grab *rotate =
3690 container_of(grab, struct rotate_grab, base.grab);
3691
3692 shell_grab_end(&rotate->base);
3693 free(rotate);
3694}
3695
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003696static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003697 noop_grab_focus,
3698 rotate_grab_motion,
3699 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003700 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003701};
3702
3703static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003704surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003705{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003706 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003707 float dx, dy;
3708 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003709
Pekka Paalanen460099f2012-01-20 16:48:25 +02003710 rotate = malloc(sizeof *rotate);
3711 if (!rotate)
3712 return;
3713
Jason Ekstranda7af7042013-10-12 22:38:11 -05003714 weston_view_to_global_float(surface->view,
3715 surface->view->geometry.width * 0.5f,
3716 surface->view->geometry.height * 0.5f,
3717 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003718
Daniel Stone37816df2012-05-16 18:45:18 +01003719 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3720 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003721 r = sqrtf(dx * dx + dy * dy);
3722 if (r > 20.0f) {
3723 struct weston_matrix inverse;
3724
3725 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003726 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003727 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003728
3729 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003730 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003731 } else {
3732 weston_matrix_init(&surface->rotation.rotation);
3733 weston_matrix_init(&rotate->rotation);
3734 }
3735
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003736 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3737 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003738}
3739
3740static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003741rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003742 void *data)
3743{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003744 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003745 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003746 struct shell_surface *surface;
3747
Pekka Paalanen01388e22013-04-25 13:57:44 +03003748 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003749 if (base_surface == NULL)
3750 return;
3751
3752 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003753 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3754 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003755 return;
3756
3757 surface_rotate(surface, seat);
3758}
3759
3760static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003761lower_fullscreen_layer(struct desktop_shell *shell)
3762{
3763 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003764 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003765
3766 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003767 wl_list_for_each_reverse_safe(view, prev,
3768 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003769 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003770 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003771}
3772
3773static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003774activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003775 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003776{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003777 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003778 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003779 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003780 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003781 struct weston_surface *old_es;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003782
Pekka Paalanen01388e22013-04-25 13:57:44 +03003783 main_surface = weston_surface_get_main_surface(es);
3784
Daniel Stone37816df2012-05-16 18:45:18 +01003785 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003786
Jonas Ådahl8538b222012-08-29 22:13:03 +02003787 state = ensure_focus_state(shell, seat);
3788 if (state == NULL)
3789 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003790
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003791 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003792 state->keyboard_focus = es;
3793 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003794 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003795
Pekka Paalanen01388e22013-04-25 13:57:44 +03003796 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003797 case SHELL_SURFACE_FULLSCREEN:
3798 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003799 shell_stack_fullscreen(get_shell_surface(main_surface));
3800 shell_configure_fullscreen(get_shell_surface(main_surface));
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003801 return;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003802 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003803 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003804 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003805 main_view = get_default_view(main_surface);
3806 if (main_view)
3807 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003808 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003809 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003810
3811 if (shell->focus_animation_type != ANIMATION_NONE)
3812 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Kristian Høgsberg75840622011-09-06 13:48:16 -04003813}
3814
Alex Wu21858432012-04-01 20:13:08 +08003815/* no-op func for checking black surface */
3816static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003817black_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 +08003818{
3819}
3820
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003821static bool
Alex Wu21858432012-04-01 20:13:08 +08003822is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3823{
3824 if (es->configure == black_surface_configure) {
3825 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003826 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003827 return true;
3828 }
3829 return false;
3830}
3831
Kristian Høgsberg75840622011-09-06 13:48:16 -04003832static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003833activate_binding(struct weston_seat *seat,
3834 struct desktop_shell *shell,
3835 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003836{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003837 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003838
Alex Wu9c35e6b2012-03-05 14:13:13 +08003839 if (!focus)
3840 return;
3841
Pekka Paalanen01388e22013-04-25 13:57:44 +03003842 if (is_black_surface(focus, &main_surface))
3843 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003844
Pekka Paalanen01388e22013-04-25 13:57:44 +03003845 main_surface = weston_surface_get_main_surface(focus);
3846 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003847 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003848
Neil Robertsa28c6932013-10-03 16:43:04 +01003849 activate(shell, focus, seat);
3850}
3851
3852static void
3853click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3854 void *data)
3855{
3856 if (seat->pointer->grab != &seat->pointer->default_grab)
3857 return;
3858
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003859 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003860}
3861
3862static void
3863touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3864{
3865 if (seat->touch->grab != &seat->touch->default_grab)
3866 return;
3867
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003868 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003869}
3870
3871static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003872lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003873{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003874 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003875
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003876 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003877 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003878 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003879 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003880
3881 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003882
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003883 /* Hide all surfaces by removing the fullscreen, panel and
3884 * toplevel layers. This way nothing else can show or receive
3885 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003886
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003887 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003888 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003889 if (shell->showing_input_panels)
3890 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003891 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003892 wl_list_insert(&shell->compositor->cursor_layer.link,
3893 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003894
Pekka Paalanen77346a62011-11-30 16:26:35 +02003895 launch_screensaver(shell);
3896
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003897 /* TODO: disable bindings that should not work while locked. */
3898
3899 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003900}
3901
3902static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003903unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003904{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003905 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003906 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003907 return;
3908 }
3909
3910 /* If desktop-shell client has gone away, unlock immediately. */
3911 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003912 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003913 return;
3914 }
3915
3916 if (shell->prepare_event_sent)
3917 return;
3918
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003919 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003920 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003921}
3922
3923static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003924shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003925{
3926 struct desktop_shell *shell = data;
3927
3928 shell->fade.animation = NULL;
3929
3930 switch (shell->fade.type) {
3931 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003932 weston_surface_destroy(shell->fade.view->surface);
3933 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003934 break;
3935 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003936 lock(shell);
3937 break;
3938 }
3939}
3940
Jason Ekstranda7af7042013-10-12 22:38:11 -05003941static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003942shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003943{
3944 struct weston_compositor *compositor = shell->compositor;
3945 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003946 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003947
3948 surface = weston_surface_create(compositor);
3949 if (!surface)
3950 return NULL;
3951
Jason Ekstranda7af7042013-10-12 22:38:11 -05003952 view = weston_view_create(surface);
3953 if (!view) {
3954 weston_surface_destroy(surface);
3955 return NULL;
3956 }
3957
3958 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003959 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960 wl_list_insert(&compositor->fade_layer.view_list,
3961 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003962 pixman_region32_init(&surface->input);
3963
Jason Ekstranda7af7042013-10-12 22:38:11 -05003964 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003965}
3966
3967static void
3968shell_fade(struct desktop_shell *shell, enum fade_type type)
3969{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003970 float tint;
3971
3972 switch (type) {
3973 case FADE_IN:
3974 tint = 0.0;
3975 break;
3976 case FADE_OUT:
3977 tint = 1.0;
3978 break;
3979 default:
3980 weston_log("shell: invalid fade type\n");
3981 return;
3982 }
3983
3984 shell->fade.type = type;
3985
Jason Ekstranda7af7042013-10-12 22:38:11 -05003986 if (shell->fade.view == NULL) {
3987 shell->fade.view = shell_fade_create_surface(shell);
3988 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003989 return;
3990
Jason Ekstranda7af7042013-10-12 22:38:11 -05003991 shell->fade.view->alpha = 1.0 - tint;
3992 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003993 }
3994
3995 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003996 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003997 else
3998 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003999 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004000 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004001 shell_fade_done, shell);
4002}
4003
4004static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004005do_shell_fade_startup(void *data)
4006{
4007 struct desktop_shell *shell = data;
4008
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004009 if (shell->startup_animation_type == ANIMATION_FADE)
4010 shell_fade(shell, FADE_IN);
4011 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004012 weston_surface_destroy(shell->fade.view->surface);
4013 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004014 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004015}
4016
4017static void
4018shell_fade_startup(struct desktop_shell *shell)
4019{
4020 struct wl_event_loop *loop;
4021
4022 if (!shell->fade.startup_timer)
4023 return;
4024
4025 wl_event_source_remove(shell->fade.startup_timer);
4026 shell->fade.startup_timer = NULL;
4027
4028 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4029 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4030}
4031
4032static int
4033fade_startup_timeout(void *data)
4034{
4035 struct desktop_shell *shell = data;
4036
4037 shell_fade_startup(shell);
4038 return 0;
4039}
4040
4041static void
4042shell_fade_init(struct desktop_shell *shell)
4043{
4044 /* Make compositor output all black, and wait for the desktop-shell
4045 * client to signal it is ready, then fade in. The timer triggers a
4046 * fade-in, in case the desktop-shell client takes too long.
4047 */
4048
4049 struct wl_event_loop *loop;
4050
Jason Ekstranda7af7042013-10-12 22:38:11 -05004051 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004052 weston_log("%s: warning: fade surface already exists\n",
4053 __func__);
4054 return;
4055 }
4056
Jason Ekstranda7af7042013-10-12 22:38:11 -05004057 shell->fade.view = shell_fade_create_surface(shell);
4058 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004059 return;
4060
Jason Ekstranda7af7042013-10-12 22:38:11 -05004061 weston_view_update_transform(shell->fade.view);
4062 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004063
4064 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4065 shell->fade.startup_timer =
4066 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4067 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4068}
4069
4070static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004071idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004072{
4073 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004074 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004075
4076 shell_fade(shell, FADE_OUT);
4077 /* lock() is called from shell_fade_done() */
4078}
4079
4080static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004081wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004082{
4083 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004084 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004085
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004086 unlock(shell);
4087}
4088
4089static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004090show_input_panels(struct wl_listener *listener, void *data)
4091{
4092 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004093 container_of(listener, struct desktop_shell,
4094 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004095 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004096
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004097 shell->text_input.surface = (struct weston_surface*)data;
4098
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004099 if (shell->showing_input_panels)
4100 return;
4101
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004102 shell->showing_input_panels = true;
4103
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004104 if (!shell->locked)
4105 wl_list_insert(&shell->panel_layer.link,
4106 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004107
Jason Ekstranda7af7042013-10-12 22:38:11 -05004108 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004109 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004110 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004111 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004112 wl_list_insert(&shell->input_panel_layer.view_list,
4113 &ipsurf->view->layer_link);
4114 weston_view_geometry_dirty(ipsurf->view);
4115 weston_view_update_transform(ipsurf->view);
4116 weston_surface_damage(ipsurf->surface);
4117 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
4118 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004119 }
4120}
4121
4122static void
4123hide_input_panels(struct wl_listener *listener, void *data)
4124{
4125 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004126 container_of(listener, struct desktop_shell,
4127 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004128 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004129
Jan Arne Petersen61381972013-01-31 15:52:21 +01004130 if (!shell->showing_input_panels)
4131 return;
4132
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004133 shell->showing_input_panels = false;
4134
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004135 if (!shell->locked)
4136 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004137
Jason Ekstranda7af7042013-10-12 22:38:11 -05004138 wl_list_for_each_safe(view, next,
4139 &shell->input_panel_layer.view_list, layer_link)
4140 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004141}
4142
4143static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004144update_input_panels(struct wl_listener *listener, void *data)
4145{
4146 struct desktop_shell *shell =
4147 container_of(listener, struct desktop_shell,
4148 update_input_panel_listener);
4149
4150 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4151}
4152
4153static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004154center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004155{
Giulio Camuffob8366642013-04-25 13:57:46 +03004156 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004157 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004158
Jason Ekstranda7af7042013-10-12 22:38:11 -05004159 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004160
4161 x = output->x + (output->width - width) / 2 - surf_x / 2;
4162 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004163
Jason Ekstranda7af7042013-10-12 22:38:11 -05004164 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004165}
4166
4167static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004168weston_view_set_initial_position(struct weston_view *view,
4169 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004170{
4171 struct weston_compositor *compositor = shell->compositor;
4172 int ix = 0, iy = 0;
4173 int range_x, range_y;
4174 int dx, dy, x, y, panel_height;
4175 struct weston_output *output, *target_output = NULL;
4176 struct weston_seat *seat;
4177
4178 /* As a heuristic place the new window on the same output as the
4179 * pointer. Falling back to the output containing 0, 0.
4180 *
4181 * TODO: Do something clever for touch too?
4182 */
4183 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004184 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004185 ix = wl_fixed_to_int(seat->pointer->x);
4186 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004187 break;
4188 }
4189 }
4190
4191 wl_list_for_each(output, &compositor->output_list, link) {
4192 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4193 target_output = output;
4194 break;
4195 }
4196 }
4197
4198 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004199 weston_view_set_position(view, 10 + random() % 400,
4200 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004201 return;
4202 }
4203
4204 /* Valid range within output where the surface will still be onscreen.
4205 * If this is negative it means that the surface is bigger than
4206 * output.
4207 */
4208 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004209 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004210 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05004211 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004212
Rob Bradford4cb88c72012-08-13 15:18:44 +01004213 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004214 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004215 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004216 dx = 0;
4217
4218 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004219 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004220 else
4221 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004222
4223 x = target_output->x + dx;
4224 y = target_output->y + dy;
4225
Jason Ekstranda7af7042013-10-12 22:38:11 -05004226 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004227}
4228
4229static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004230map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004231 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004232{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004233 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004234 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01004235 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004236 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08004237 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004238 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004239
Jason Ekstranda7af7042013-10-12 22:38:11 -05004240 shsurf->view->geometry.width = width;
4241 shsurf->view->geometry.height = height;
4242 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004243
4244 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02004246 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004247 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004248 break;
Alex Wu4539b082012-03-01 12:57:46 +08004249 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004250 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08004251 shell_map_fullscreen(shsurf);
4252 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004253 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004254 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004255 panel_height = get_output_panel_height(shell, shsurf->output);
4256 surface_subsurfaces_boundingbox(shsurf->surface,
4257 &surf_x, &surf_y, NULL, NULL);
4258 weston_view_set_position(shsurf->view,
4259 shsurf->output->x - surf_x,
4260 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08004261 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004262 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004263 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004264 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004265 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004266 weston_view_set_position(shsurf->view,
4267 shsurf->view->geometry.x + sx,
4268 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004269 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004270 default:
4271 ;
4272 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004273
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02004274 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004275 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004276 case SHELL_SURFACE_POPUP:
4277 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004278 /* TODO: Handle a parent with multiple views */
4279 parent = get_default_view(shsurf->parent);
4280 if (parent) {
4281 wl_list_remove(&shsurf->view->layer_link);
4282 wl_list_insert(parent->layer_link.prev,
4283 &shsurf->view->layer_link);
4284 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05004285 break;
Alex Wu4539b082012-03-01 12:57:46 +08004286 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02004287 case SHELL_SURFACE_NONE:
4288 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004289 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004290 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004291 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004292 wl_list_remove(&shsurf->view->layer_link);
4293 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004294 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004295 }
4296
Jason Ekstranda7af7042013-10-12 22:38:11 -05004297 if (shsurf->type != SHELL_SURFACE_NONE) {
4298 weston_view_update_transform(shsurf->view);
4299 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
4300 shsurf->surface->output = shsurf->output;
4301 shsurf->view->output = shsurf->output;
4302 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004303 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004304
Jason Ekstranda7af7042013-10-12 22:38:11 -05004305 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004306 /* XXX: xwayland's using the same fields for transient type */
4307 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004308 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004309 if (shsurf->transient.flags ==
4310 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4311 break;
4312 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004313 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08004314 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01004315 if (!shell->locked) {
4316 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004317 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01004318 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05004319 break;
4320 default:
4321 break;
4322 }
4323
Jason Ekstranda7af7042013-10-12 22:38:11 -05004324 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004325 {
4326 switch (shell->win_animation_type) {
4327 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004328 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004329 break;
4330 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004331 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004332 break;
4333 default:
4334 break;
4335 }
4336 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004337}
4338
4339static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004340configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004341 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004342{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004343 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
4344 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004345 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03004346 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004347
Pekka Paalanen77346a62011-11-30 16:26:35 +02004348 shsurf = get_shell_surface(surface);
4349 if (shsurf)
4350 surface_type = shsurf->type;
4351
Jason Ekstranda7af7042013-10-12 22:38:11 -05004352 /* TODO:
4353 * This should probably be changed to be more shell_surface
4354 * dependent
4355 */
4356 wl_list_for_each(view, &surface->views, surface_link)
4357 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004358
4359 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08004360 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08004361 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004362 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08004363 break;
Juan Zhao96879df2012-02-07 08:45:41 +08004364 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08004365 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004366 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4367 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004368 shsurf->view->geometry.x = shsurf->output->x - surf_x;
4369 shsurf->view->geometry.y = shsurf->output->y +
4370 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08004371 break;
Alex Wu4539b082012-03-01 12:57:46 +08004372 case SHELL_SURFACE_TOPLEVEL:
4373 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004374 default:
4375 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004376 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004377
Alex Wu4539b082012-03-01 12:57:46 +08004378 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004379 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004380 wl_list_for_each(view, &surface->views, surface_link)
4381 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004382
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004383 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08004384 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004385 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004386}
4387
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004388static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004389shell_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 +03004390{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004391 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004392 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004393
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004394 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004395
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004396 if (!weston_surface_is_mapped(es) &&
4397 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004398 remove_popup_grab(shsurf);
4399 }
4400
Giulio Camuffo184df502013-02-21 11:29:21 +01004401 if (width == 0)
4402 return;
4403
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004404 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004405 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004406 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004407 type_changed = 1;
4408 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004409
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004410 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004412 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05004413 shsurf->view->geometry.width != width ||
4414 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004415 float from_x, from_y;
4416 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004417
Jason Ekstranda7af7042013-10-12 22:38:11 -05004418 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4419 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004420 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004421 shsurf->view->geometry.x + to_x - from_x,
4422 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004423 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004424 }
4425}
4426
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004427static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004428
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004429static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004430desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004431{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004432 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004433 struct desktop_shell *shell =
4434 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004435
4436 shell->child.process.pid = 0;
4437 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004438
4439 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4440 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004441 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004442 shell->child.deathstamp = time;
4443 shell->child.deathcount = 0;
4444 }
4445
4446 shell->child.deathcount++;
4447 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004448 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004449 return;
4450 }
4451
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004452 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004453 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004454 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004455}
4456
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004457static void
4458launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004459{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004460 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004461
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004462 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004463 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004464 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004465 desktop_shell_sigchld);
4466
4467 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004468 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004469}
4470
4471static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004472bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4473{
Tiago Vignattibe143262012-04-16 17:31:41 +03004474 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004475 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004476
Jason Ekstranda85118c2013-06-27 20:17:02 -05004477 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
4478 if (resource)
4479 wl_resource_set_implementation(resource, &shell_implementation,
4480 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004481}
4482
Kristian Høgsberg75840622011-09-06 13:48:16 -04004483static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004484unbind_desktop_shell(struct wl_resource *resource)
4485{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004486 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004487
4488 if (shell->locked)
4489 resume_desktop(shell);
4490
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004491 shell->child.desktop_shell = NULL;
4492 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004493}
4494
4495static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004496bind_desktop_shell(struct wl_client *client,
4497 void *data, uint32_t version, uint32_t id)
4498{
Tiago Vignattibe143262012-04-16 17:31:41 +03004499 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004500 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004501
Jason Ekstranda85118c2013-06-27 20:17:02 -05004502 resource = wl_resource_create(client, &desktop_shell_interface,
4503 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004504
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004505 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004506 wl_resource_set_implementation(resource,
4507 &desktop_shell_implementation,
4508 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004509 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004510
4511 if (version < 2)
4512 shell_fade_startup(shell);
4513
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004514 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004515 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004516
4517 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4518 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004519 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004520}
4521
Pekka Paalanen6e168112011-11-24 11:34:05 +02004522static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004523screensaver_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 -04004524{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004525 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004526 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004527
Giulio Camuffo184df502013-02-21 11:29:21 +01004528 if (width == 0)
4529 return;
4530
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004531 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004532 if (!shell->locked)
4533 return;
4534
Jason Ekstranda7af7042013-10-12 22:38:11 -05004535 view = container_of(surface->views.next, struct weston_view, surface_link);
4536 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004537
Jason Ekstranda7af7042013-10-12 22:38:11 -05004538 if (wl_list_empty(&view->layer_link)) {
4539 wl_list_insert(shell->lock_layer.view_list.prev,
4540 &view->layer_link);
4541 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004542 wl_event_source_timer_update(shell->screensaver.timer,
4543 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004544 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004545 }
4546}
4547
4548static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004549screensaver_set_surface(struct wl_client *client,
4550 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004551 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004552 struct wl_resource *output_resource)
4553{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004554 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004555 struct weston_surface *surface =
4556 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004557 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004558 struct weston_view *view, *next;
4559
4560 /* Make sure we only have one view */
4561 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4562 weston_view_destroy(view);
4563 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004564
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004565 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004566 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004567 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004568}
4569
4570static const struct screensaver_interface screensaver_implementation = {
4571 screensaver_set_surface
4572};
4573
4574static void
4575unbind_screensaver(struct wl_resource *resource)
4576{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004577 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004578
Pekka Paalanen77346a62011-11-30 16:26:35 +02004579 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004580}
4581
4582static void
4583bind_screensaver(struct wl_client *client,
4584 void *data, uint32_t version, uint32_t id)
4585{
Tiago Vignattibe143262012-04-16 17:31:41 +03004586 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004587 struct wl_resource *resource;
4588
Jason Ekstranda85118c2013-06-27 20:17:02 -05004589 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004590
Pekka Paalanen77346a62011-11-30 16:26:35 +02004591 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004592 wl_resource_set_implementation(resource,
4593 &screensaver_implementation,
4594 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004595 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004596 return;
4597 }
4598
4599 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4600 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004601 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004602}
4603
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004604static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004605input_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 -04004606{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004607 struct input_panel_surface *ip_surface = surface->configure_private;
4608 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004609 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004610 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004611
Giulio Camuffo184df502013-02-21 11:29:21 +01004612 if (width == 0)
4613 return;
4614
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004615 if (!weston_surface_is_mapped(surface)) {
4616 if (!shell->showing_input_panels)
4617 return;
4618
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004619 show_surface = 1;
4620 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004621
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004622 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004623
4624 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004625 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4626 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004627 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004628 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4629 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004630 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004631
Jason Ekstranda7af7042013-10-12 22:38:11 -05004632 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004633
4634 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004635 wl_list_insert(&shell->input_panel_layer.view_list,
4636 &ip_surface->view->layer_link);
4637 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004638 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004639 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004640 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004641}
4642
4643static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004644destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004645{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004646 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4647
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004648 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004649 wl_list_remove(&input_panel_surface->link);
4650
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004651 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004652 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004653
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004654 free(input_panel_surface);
4655}
4656
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004657static struct input_panel_surface *
4658get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004659{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004660 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004661 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004662 } else {
4663 return NULL;
4664 }
4665}
4666
4667static void
4668input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4669{
4670 struct input_panel_surface *ipsurface = container_of(listener,
4671 struct input_panel_surface,
4672 surface_destroy_listener);
4673
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004674 if (ipsurface->resource) {
4675 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004676 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004677 destroy_input_panel_surface(ipsurface);
4678 }
4679}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004680
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004681static struct input_panel_surface *
4682create_input_panel_surface(struct desktop_shell *shell,
4683 struct weston_surface *surface)
4684{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004685 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004686
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004687 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4688 if (!input_panel_surface)
4689 return NULL;
4690
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004691 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004692 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004693
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004694 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004695
4696 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004697 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004698
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004699 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004700 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004701 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004702 &input_panel_surface->surface_destroy_listener);
4703
4704 wl_list_init(&input_panel_surface->link);
4705
4706 return input_panel_surface;
4707}
4708
4709static void
4710input_panel_surface_set_toplevel(struct wl_client *client,
4711 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004712 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004713 uint32_t position)
4714{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004715 struct input_panel_surface *input_panel_surface =
4716 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004717 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004718
4719 wl_list_insert(&shell->input_panel.surfaces,
4720 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004721
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004722 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004723 input_panel_surface->panel = 0;
4724}
4725
4726static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004727input_panel_surface_set_overlay_panel(struct wl_client *client,
4728 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004729{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004730 struct input_panel_surface *input_panel_surface =
4731 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004732 struct desktop_shell *shell = input_panel_surface->shell;
4733
4734 wl_list_insert(&shell->input_panel.surfaces,
4735 &input_panel_surface->link);
4736
4737 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004738}
4739
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004740static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004741 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004742 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004743};
4744
4745static void
4746destroy_input_panel_surface_resource(struct wl_resource *resource)
4747{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004748 struct input_panel_surface *ipsurf =
4749 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004750
4751 destroy_input_panel_surface(ipsurf);
4752}
4753
4754static void
4755input_panel_get_input_panel_surface(struct wl_client *client,
4756 struct wl_resource *resource,
4757 uint32_t id,
4758 struct wl_resource *surface_resource)
4759{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004760 struct weston_surface *surface =
4761 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004762 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004763 struct input_panel_surface *ipsurf;
4764
4765 if (get_input_panel_surface(surface)) {
4766 wl_resource_post_error(surface_resource,
4767 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004768 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004769 return;
4770 }
4771
4772 ipsurf = create_input_panel_surface(shell, surface);
4773 if (!ipsurf) {
4774 wl_resource_post_error(surface_resource,
4775 WL_DISPLAY_ERROR_INVALID_OBJECT,
4776 "surface->configure already set");
4777 return;
4778 }
4779
Jason Ekstranda85118c2013-06-27 20:17:02 -05004780 ipsurf->resource =
4781 wl_resource_create(client,
4782 &wl_input_panel_surface_interface, 1, id);
4783 wl_resource_set_implementation(ipsurf->resource,
4784 &input_panel_surface_implementation,
4785 ipsurf,
4786 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004787}
4788
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004789static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004790 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004791};
4792
4793static void
4794unbind_input_panel(struct wl_resource *resource)
4795{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004796 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004797
4798 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004799}
4800
4801static void
4802bind_input_panel(struct wl_client *client,
4803 void *data, uint32_t version, uint32_t id)
4804{
4805 struct desktop_shell *shell = data;
4806 struct wl_resource *resource;
4807
Jason Ekstranda85118c2013-06-27 20:17:02 -05004808 resource = wl_resource_create(client,
4809 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004810
4811 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004812 wl_resource_set_implementation(resource,
4813 &input_panel_implementation,
4814 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004815 shell->input_panel.binding = resource;
4816 return;
4817 }
4818
4819 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4820 "interface object already bound");
4821 wl_resource_destroy(resource);
4822}
4823
Kristian Høgsberg07045392012-02-19 18:52:44 -05004824struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004825 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004826 struct weston_surface *current;
4827 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004828 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004829};
4830
4831static void
4832switcher_next(struct switcher *switcher)
4833{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004834 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004835 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004836 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004837 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004838
Jason Ekstranda7af7042013-10-12 22:38:11 -05004839 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4840 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004841 case SHELL_SURFACE_TOPLEVEL:
4842 case SHELL_SURFACE_FULLSCREEN:
4843 case SHELL_SURFACE_MAXIMIZED:
4844 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004845 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004846 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004847 next = view->surface;
4848 prev = view->surface;
4849 view->alpha = 0.25;
4850 weston_view_geometry_dirty(view);
4851 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004852 break;
4853 default:
4854 break;
4855 }
Alex Wu1659daa2012-04-01 20:13:09 +08004856
Jason Ekstranda7af7042013-10-12 22:38:11 -05004857 if (is_black_surface(view->surface, NULL)) {
4858 view->alpha = 0.25;
4859 weston_view_geometry_dirty(view);
4860 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004861 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004862 }
4863
4864 if (next == NULL)
4865 next = first;
4866
Alex Wu07b26062012-03-12 16:06:01 +08004867 if (next == NULL)
4868 return;
4869
Kristian Høgsberg07045392012-02-19 18:52:44 -05004870 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004871 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004872
4873 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004874 wl_list_for_each(view, &next->views, surface_link)
4875 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004876
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004877 shsurf = get_shell_surface(switcher->current);
4878 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004879 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004880}
4881
4882static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004883switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004884{
4885 struct switcher *switcher =
4886 container_of(listener, struct switcher, listener);
4887
4888 switcher_next(switcher);
4889}
4890
4891static void
Daniel Stone351eb612012-05-31 15:27:47 -04004892switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004893{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004894 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004895 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004896 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004897
Jason Ekstranda7af7042013-10-12 22:38:11 -05004898 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004899 if (is_focus_view(view))
4900 continue;
4901
Jason Ekstranda7af7042013-10-12 22:38:11 -05004902 view->alpha = 1.0;
4903 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004904 }
4905
Alex Wu07b26062012-03-12 16:06:01 +08004906 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004907 activate(switcher->shell, switcher->current,
4908 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004909 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004910 weston_keyboard_end_grab(keyboard);
4911 if (keyboard->input_method_resource)
4912 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004913 free(switcher);
4914}
4915
4916static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004917switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004918 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004919{
4920 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004921 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004922
Daniel Stonec9785ea2012-05-30 16:31:52 +01004923 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004924 switcher_next(switcher);
4925}
4926
4927static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004928switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004929 uint32_t mods_depressed, uint32_t mods_latched,
4930 uint32_t mods_locked, uint32_t group)
4931{
4932 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004933 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004934
Daniel Stone351eb612012-05-31 15:27:47 -04004935 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4936 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004937}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004938
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004939static void
4940switcher_cancel(struct weston_keyboard_grab *grab)
4941{
4942 struct switcher *switcher = container_of(grab, struct switcher, grab);
4943
4944 switcher_destroy(switcher);
4945}
4946
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004947static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004948 switcher_key,
4949 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004950 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004951};
4952
4953static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004954switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004955 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004956{
Tiago Vignattibe143262012-04-16 17:31:41 +03004957 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004958 struct switcher *switcher;
4959
4960 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004961 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004962 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004963 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004964 wl_list_init(&switcher->listener.link);
4965
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004966 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004967 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004968 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004969 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4970 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004971 switcher_next(switcher);
4972}
4973
Daniel Stonedf8133b2013-11-19 11:37:14 +01004974struct exposay_surface {
4975 struct desktop_shell *shell;
4976 struct weston_surface *surface;
4977 struct weston_view *view;
4978 struct wl_list link;
4979
4980 int x;
4981 int y;
4982 int width;
4983 int height;
4984 double scale;
4985
4986 int row;
4987 int column;
4988
4989 /* The animations only apply a transformation for their own lifetime,
4990 * and don't have an option to indefinitely maintain the
4991 * transformation in a steady state - so, we apply our own once the
4992 * animation has finished. */
4993 struct weston_transform transform;
4994};
4995
4996static void exposay_set_state(struct desktop_shell *shell,
4997 enum exposay_target_state state,
4998 struct weston_seat *seat);
4999static void exposay_check_state(struct desktop_shell *shell);
5000
5001static void
5002exposay_in_flight_inc(struct desktop_shell *shell)
5003{
5004 shell->exposay.in_flight++;
5005}
5006
5007static void
5008exposay_in_flight_dec(struct desktop_shell *shell)
5009{
5010 if (--shell->exposay.in_flight > 0)
5011 return;
5012
5013 exposay_check_state(shell);
5014}
5015
5016static void
5017exposay_animate_in_done(struct weston_view_animation *animation, void *data)
5018{
5019 struct exposay_surface *esurface = data;
5020
5021 wl_list_insert(&esurface->view->geometry.transformation_list,
5022 &esurface->transform.link);
5023 weston_matrix_init(&esurface->transform.matrix);
5024 weston_matrix_scale(&esurface->transform.matrix,
5025 esurface->scale, esurface->scale, 1.0f);
5026 weston_matrix_translate(&esurface->transform.matrix,
5027 esurface->x - esurface->view->geometry.x,
5028 esurface->y - esurface->view->geometry.y,
5029 0);
5030
5031 weston_view_geometry_dirty(esurface->view);
5032 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
5033
5034 exposay_in_flight_dec(esurface->shell);
5035}
5036
5037static void
5038exposay_animate_in(struct exposay_surface *esurface)
5039{
5040 exposay_in_flight_inc(esurface->shell);
5041
5042 weston_move_scale_run(esurface->view,
5043 esurface->x - esurface->view->geometry.x,
5044 esurface->y - esurface->view->geometry.y,
5045 1.0, esurface->scale, 0,
5046 exposay_animate_in_done, esurface);
5047}
5048
5049static void
5050exposay_animate_out_done(struct weston_view_animation *animation, void *data)
5051{
5052 struct exposay_surface *esurface = data;
5053 struct desktop_shell *shell = esurface->shell;
5054
5055 wl_list_remove(&esurface->link);
5056 free(esurface);
5057
5058 exposay_in_flight_dec(shell);
5059}
5060
5061static void
5062exposay_animate_out(struct exposay_surface *esurface)
5063{
5064 exposay_in_flight_inc(esurface->shell);
5065
5066 /* Remove the static transformation set up by
5067 * exposay_transform_in_done(). */
5068 wl_list_remove(&esurface->transform.link);
5069 weston_view_geometry_dirty(esurface->view);
5070
5071 weston_move_scale_run(esurface->view,
5072 esurface->x - esurface->view->geometry.x,
5073 esurface->y - esurface->view->geometry.y,
5074 1.0, esurface->scale, 1,
5075 exposay_animate_out_done, esurface);
5076}
5077
5078static void
5079exposay_highlight_surface(struct desktop_shell *shell,
5080 struct exposay_surface *esurface)
5081{
5082 struct weston_view *view = NULL;
5083
5084 if (esurface) {
5085 shell->exposay.row_current = esurface->row;
5086 shell->exposay.column_current = esurface->column;
5087 view = esurface->view;
5088 }
5089
Emilio Pozuelo Monfort5c22ce62013-11-19 11:37:18 +01005090 activate(shell, view->surface, shell->exposay.seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005091 shell->exposay.focus_current = view;
5092}
5093
5094static int
5095exposay_is_animating(struct desktop_shell *shell)
5096{
5097 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
5098 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
5099 return 0;
5100
5101 return (shell->exposay.in_flight > 0);
5102}
5103
5104static void
5105exposay_pick(struct desktop_shell *shell, int x, int y)
5106{
5107 struct exposay_surface *esurface;
5108
5109 if (exposay_is_animating(shell))
5110 return;
5111
5112 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5113 if (x < esurface->x || x > esurface->x + esurface->width)
5114 continue;
5115 if (y < esurface->y || y > esurface->y + esurface->height)
5116 continue;
5117
5118 exposay_highlight_surface(shell, esurface);
5119 return;
5120 }
5121}
5122
5123/* Pretty lame layout for now; just tries to make a square. Should take
5124 * aspect ratio into account really. Also needs to be notified of surface
5125 * addition and removal and adjust layout/animate accordingly. */
5126static enum exposay_layout_state
5127exposay_layout(struct desktop_shell *shell)
5128{
5129 struct workspace *workspace = shell->exposay.workspace;
5130 struct weston_compositor *compositor = shell->compositor;
5131 struct weston_output *output = get_default_output(compositor);
5132 struct weston_view *view;
5133 struct exposay_surface *esurface;
5134 int w, h;
5135 int i;
5136 int last_row_removed = 0;
5137
5138 wl_list_init(&shell->exposay.surface_list);
5139
5140 shell->exposay.num_surfaces = 0;
5141 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5142 if (!get_shell_surface(view->surface))
5143 continue;
5144 shell->exposay.num_surfaces++;
5145 }
5146
5147 if (shell->exposay.num_surfaces == 0) {
5148 shell->exposay.grid_size = 0;
5149 shell->exposay.hpadding_outer = 0;
5150 shell->exposay.vpadding_outer = 0;
5151 shell->exposay.padding_inner = 0;
5152 shell->exposay.surface_size = 0;
5153 return EXPOSAY_LAYOUT_OVERVIEW;
5154 }
5155
5156 /* Lay the grid out as square as possible, losing surfaces from the
5157 * bottom row if required. Start with fixed padding of a 10% margin
5158 * around the outside and 80px internal padding between surfaces, and
5159 * maximise the area made available to surfaces after this, but only
5160 * to a maximum of 1/3rd the total output size.
5161 *
5162 * If we can't make a square grid, add one extra row at the bottom
5163 * which will have a smaller number of columns.
5164 *
5165 * XXX: Surely there has to be a better way to express this maths,
5166 * right?!
5167 */
5168 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
5169 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
5170 shell->exposay.grid_size++;
5171 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
5172
5173 shell->exposay.hpadding_outer = (output->width / 10);
5174 shell->exposay.vpadding_outer = (output->height / 10);
5175 shell->exposay.padding_inner = 80;
5176
5177 w = output->width - (shell->exposay.hpadding_outer * 2);
5178 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5179 w /= shell->exposay.grid_size;
5180
5181 h = output->height - (shell->exposay.vpadding_outer * 2);
5182 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
5183 h /= shell->exposay.grid_size;
5184
5185 shell->exposay.surface_size = (w < h) ? w : h;
5186 if (shell->exposay.surface_size > (output->width / 2))
5187 shell->exposay.surface_size = output->width / 2;
5188 if (shell->exposay.surface_size > (output->height / 2))
5189 shell->exposay.surface_size = output->height / 2;
5190
5191 i = 0;
5192 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
5193 int pad;
5194
5195 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
5196
5197 if (!get_shell_surface(view->surface))
5198 continue;
5199
5200 esurface = malloc(sizeof(*esurface));
5201 if (!esurface) {
5202 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
5203 shell->exposay.seat);
5204 break;
5205 }
5206
5207 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
5208 esurface->shell = shell;
5209 esurface->view = view;
5210
5211 esurface->row = i / shell->exposay.grid_size;
5212 esurface->column = i % shell->exposay.grid_size;
5213
5214 esurface->x = shell->exposay.hpadding_outer;
5215 esurface->x += pad * esurface->column;
5216 esurface->y = shell->exposay.vpadding_outer;
5217 esurface->y += pad * esurface->row;
5218
5219 if (esurface->row == shell->exposay.grid_size - 1)
5220 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
5221
5222 if (view->geometry.width > view->geometry.height)
5223 esurface->scale = shell->exposay.surface_size / (float) view->geometry.width;
5224 else
5225 esurface->scale = shell->exposay.surface_size / (float) view->geometry.height;
5226 esurface->width = view->geometry.width * esurface->scale;
5227 esurface->height = view->geometry.height * esurface->scale;
5228
5229 if (shell->exposay.focus_current == esurface->view)
5230 exposay_highlight_surface(shell, esurface);
5231
5232 exposay_animate_in(esurface);
5233
5234 i++;
5235 }
5236
5237 weston_compositor_schedule_repaint(shell->compositor);
5238
5239 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
5240}
5241
5242static void
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005243exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
5244 wl_fixed_t x, wl_fixed_t y)
Daniel Stonedf8133b2013-11-19 11:37:14 +01005245{
5246 struct desktop_shell *shell =
5247 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5248
Emilio Pozuelo Monfort17467d62013-11-19 12:14:53 +01005249 weston_pointer_move(grab->pointer, x, y);
5250
Daniel Stonedf8133b2013-11-19 11:37:14 +01005251 exposay_pick(shell,
5252 wl_fixed_to_int(grab->pointer->x),
5253 wl_fixed_to_int(grab->pointer->y));
5254}
5255
5256static void
5257exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
5258 uint32_t state_w)
5259{
5260 struct desktop_shell *shell =
5261 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5262 struct weston_seat *seat = grab->pointer->seat;
5263 enum wl_pointer_button_state state = state_w;
5264
5265 if (button != BTN_LEFT)
5266 return;
5267
5268 /* Store the surface we clicked on, and don't do anything if we end up
5269 * releasing on a different surface. */
5270 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
5271 shell->exposay.clicked = shell->exposay.focus_current;
5272 return;
5273 }
5274
5275 if (shell->exposay.focus_current == shell->exposay.clicked)
5276 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5277 else
5278 shell->exposay.clicked = NULL;
5279}
5280
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005281static void
5282exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
5283{
5284 struct desktop_shell *shell =
5285 container_of(grab, struct desktop_shell, exposay.grab_ptr);
5286
5287 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5288}
5289
Daniel Stonedf8133b2013-11-19 11:37:14 +01005290static const struct weston_pointer_grab_interface exposay_ptr_grab = {
5291 noop_grab_focus,
5292 exposay_motion,
5293 exposay_button,
Emilio Pozuelo Monfort234c5242013-11-26 13:32:08 +01005294 exposay_pointer_grab_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005295};
5296
5297static int
5298exposay_maybe_move(struct desktop_shell *shell, int row, int column)
5299{
5300 struct exposay_surface *esurface;
5301
5302 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
5303 if (esurface->row != row || esurface->column != column)
5304 continue;
5305
5306 exposay_highlight_surface(shell, esurface);
5307 return 1;
5308 }
5309
5310 return 0;
5311}
5312
5313static void
5314exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
5315 uint32_t state_w)
5316{
5317 struct weston_seat *seat = grab->keyboard->seat;
5318 struct desktop_shell *shell =
5319 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5320 enum wl_keyboard_key_state state = state_w;
5321
5322 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
5323 return;
5324
5325 switch (key) {
5326 case KEY_ESC:
5327 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5328 break;
5329 case KEY_ENTER:
5330 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
5331 break;
5332 case KEY_UP:
5333 exposay_maybe_move(shell, shell->exposay.row_current - 1,
5334 shell->exposay.column_current);
5335 break;
5336 case KEY_DOWN:
5337 /* Special case for trying to move to the bottom row when it
5338 * has fewer items than all the others. */
5339 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
5340 shell->exposay.column_current) &&
5341 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
5342 exposay_maybe_move(shell, shell->exposay.row_current + 1,
5343 (shell->exposay.num_surfaces %
5344 shell->exposay.grid_size) - 1);
5345 }
5346 break;
5347 case KEY_LEFT:
5348 exposay_maybe_move(shell, shell->exposay.row_current,
5349 shell->exposay.column_current - 1);
5350 break;
5351 case KEY_RIGHT:
5352 exposay_maybe_move(shell, shell->exposay.row_current,
5353 shell->exposay.column_current + 1);
5354 break;
5355 case KEY_TAB:
5356 /* Try to move right, then down (and to the leftmost column),
5357 * then if all else fails, to the top left. */
5358 if (!exposay_maybe_move(shell, shell->exposay.row_current,
5359 shell->exposay.column_current + 1) &&
5360 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
5361 exposay_maybe_move(shell, 0, 0);
5362 break;
5363 default:
5364 break;
5365 }
5366}
5367
5368static void
5369exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5370 uint32_t mods_depressed, uint32_t mods_latched,
5371 uint32_t mods_locked, uint32_t group)
5372{
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005373 struct desktop_shell *shell =
5374 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5375 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5376
5377 /* We want to know when mod has been pressed and released.
5378 * FIXME: There is a problem here: if mod is pressed, then a key
5379 * is pressed and released, then mod is released, we will treat that
5380 * as if only mod had been pressed and released. */
5381 if (seat->modifier_state) {
5382 if (seat->modifier_state == shell->binding_modifier) {
5383 shell->exposay.mod_pressed = true;
5384 } else {
5385 shell->exposay.mod_invalid = true;
5386 }
5387 } else {
5388 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
5389 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
5390
5391 shell->exposay.mod_invalid = false;
5392 shell->exposay.mod_pressed = false;
5393 }
5394
5395 return;
Daniel Stonedf8133b2013-11-19 11:37:14 +01005396}
5397
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005398static void
5399exposay_cancel(struct weston_keyboard_grab *grab)
5400{
5401 struct desktop_shell *shell =
5402 container_of(grab, struct desktop_shell, exposay.grab_kbd);
5403
5404 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
5405}
5406
Daniel Stonedf8133b2013-11-19 11:37:14 +01005407static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
5408 exposay_key,
5409 exposay_modifier,
Emilio Pozuelo Monfort82243092013-11-19 11:37:17 +01005410 exposay_cancel,
Daniel Stonedf8133b2013-11-19 11:37:14 +01005411};
5412
5413/**
5414 * Called when the transition from overview -> inactive has completed.
5415 */
5416static enum exposay_layout_state
5417exposay_set_inactive(struct desktop_shell *shell)
5418{
5419 struct weston_seat *seat = shell->exposay.seat;
5420
5421 weston_keyboard_end_grab(seat->keyboard);
5422 weston_pointer_end_grab(seat->pointer);
5423 if (seat->keyboard->input_method_resource)
5424 seat->keyboard->grab = &seat->keyboard->input_method_grab;
5425
5426 return EXPOSAY_LAYOUT_INACTIVE;
5427}
5428
5429/**
5430 * Begins the transition from overview to inactive. */
5431static enum exposay_layout_state
5432exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
5433{
5434 struct exposay_surface *esurface;
5435
5436 /* Call activate() before we start the animations to avoid
5437 * animating back the old state and then immediately transitioning
5438 * to the new. */
5439 if (switch_focus && shell->exposay.focus_current)
5440 activate(shell, shell->exposay.focus_current->surface,
5441 shell->exposay.seat);
5442 else if (shell->exposay.focus_prev)
5443 activate(shell, shell->exposay.focus_prev->surface,
5444 shell->exposay.seat);
5445
5446 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
5447 exposay_animate_out(esurface);
5448 weston_compositor_schedule_repaint(shell->compositor);
5449
5450 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
5451}
5452
5453static enum exposay_layout_state
5454exposay_transition_active(struct desktop_shell *shell)
5455{
5456 struct weston_seat *seat = shell->exposay.seat;
5457
5458 shell->exposay.workspace = get_current_workspace(shell);
5459 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
5460 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
5461 shell->exposay.clicked = NULL;
5462 wl_list_init(&shell->exposay.surface_list);
5463
5464 lower_fullscreen_layer(shell);
5465 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
5466 weston_keyboard_start_grab(seat->keyboard,
5467 &shell->exposay.grab_kbd);
5468 weston_keyboard_set_focus(seat->keyboard, NULL);
5469
5470 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
5471 weston_pointer_start_grab(seat->pointer,
5472 &shell->exposay.grab_ptr);
5473 weston_pointer_set_focus(seat->pointer, NULL,
5474 seat->pointer->x, seat->pointer->y);
5475
5476 return exposay_layout(shell);
5477}
5478
5479static void
5480exposay_check_state(struct desktop_shell *shell)
5481{
5482 enum exposay_layout_state state_new = shell->exposay.state_cur;
5483 int do_switch = 0;
5484
5485 /* Don't do anything whilst animations are running, just store up
5486 * target state changes and only act on them when the animations have
5487 * completed. */
5488 if (exposay_is_animating(shell))
5489 return;
5490
5491 switch (shell->exposay.state_target) {
5492 case EXPOSAY_TARGET_OVERVIEW:
5493 switch (shell->exposay.state_cur) {
5494 case EXPOSAY_LAYOUT_OVERVIEW:
5495 goto out;
5496 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
5497 state_new = EXPOSAY_LAYOUT_OVERVIEW;
5498 break;
5499 default:
5500 state_new = exposay_transition_active(shell);
5501 break;
5502 }
5503 break;
5504
5505 case EXPOSAY_TARGET_SWITCH:
5506 do_switch = 1; /* fallthrough */
5507 case EXPOSAY_TARGET_CANCEL:
5508 switch (shell->exposay.state_cur) {
5509 case EXPOSAY_LAYOUT_INACTIVE:
5510 goto out;
5511 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
5512 state_new = exposay_set_inactive(shell);
5513 break;
5514 default:
5515 state_new = exposay_transition_inactive(shell, do_switch);
5516 break;
5517 }
5518
5519 break;
5520 }
5521
5522out:
5523 shell->exposay.state_cur = state_new;
5524}
5525
5526static void
5527exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
5528 struct weston_seat *seat)
5529{
5530 shell->exposay.state_target = state;
5531 shell->exposay.seat = seat;
5532 exposay_check_state(shell);
5533}
5534
5535static void
5536exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
5537 void *data)
5538{
5539 struct desktop_shell *shell = data;
5540
Emilio Pozuelo Monforteed93442013-11-27 10:49:08 +01005541 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005542}
5543
Pekka Paalanen3c647232011-12-22 13:43:43 +02005544static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005545backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005546 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005547{
5548 struct weston_compositor *compositor = data;
5549 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005550 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005551
5552 /* TODO: we're limiting to simple use cases, where we assume just
5553 * control on the primary display. We'd have to extend later if we
5554 * ever get support for setting backlights on random desktop LCD
5555 * panels though */
5556 output = get_default_output(compositor);
5557 if (!output)
5558 return;
5559
5560 if (!output->set_backlight)
5561 return;
5562
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005563 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5564 backlight_new = output->backlight_current - 25;
5565 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5566 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005567
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005568 if (backlight_new < 5)
5569 backlight_new = 5;
5570 if (backlight_new > 255)
5571 backlight_new = 255;
5572
5573 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005574 output->set_backlight(output, output->backlight_current);
5575}
5576
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005577struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005578 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005579 struct weston_seat *seat;
5580 uint32_t key[2];
5581 int key_released[2];
5582};
5583
5584static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005585debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005586 uint32_t key, uint32_t state)
5587{
5588 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005589 struct weston_compositor *ec = db->seat->compositor;
5590 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005591 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005592 uint32_t serial;
5593 int send = 0, terminate = 0;
5594 int check_binding = 1;
5595 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005596 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005597
5598 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5599 /* Do not run bindings on key releases */
5600 check_binding = 0;
5601
5602 for (i = 0; i < 2; i++)
5603 if (key == db->key[i])
5604 db->key_released[i] = 1;
5605
5606 if (db->key_released[0] && db->key_released[1]) {
5607 /* All key releases been swalled so end the grab */
5608 terminate = 1;
5609 } else if (key != db->key[0] && key != db->key[1]) {
5610 /* Should not swallow release of other keys */
5611 send = 1;
5612 }
5613 } else if (key == db->key[0] && !db->key_released[0]) {
5614 /* Do not check bindings for the first press of the binding
5615 * key. This allows it to be used as a debug shortcut.
5616 * We still need to swallow this event. */
5617 check_binding = 0;
5618 } else if (db->key[1]) {
5619 /* If we already ran a binding don't process another one since
5620 * we can't keep track of all the binding keys that were
5621 * pressed in order to swallow the release events. */
5622 send = 1;
5623 check_binding = 0;
5624 }
5625
5626 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005627 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5628 key, state)) {
5629 /* We ran a binding so swallow the press and keep the
5630 * grab to swallow the released too. */
5631 send = 0;
5632 terminate = 0;
5633 db->key[1] = key;
5634 } else {
5635 /* Terminate the grab since the key pressed is not a
5636 * debug binding key. */
5637 send = 1;
5638 terminate = 1;
5639 }
5640 }
5641
5642 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005643 serial = wl_display_next_serial(display);
5644 resource_list = &grab->keyboard->focus_resource_list;
5645 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005646 wl_keyboard_send_key(resource, serial, time, key, state);
5647 }
5648 }
5649
5650 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005651 weston_keyboard_end_grab(grab->keyboard);
5652 if (grab->keyboard->input_method_resource)
5653 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005654 free(db);
5655 }
5656}
5657
5658static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005659debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005660 uint32_t mods_depressed, uint32_t mods_latched,
5661 uint32_t mods_locked, uint32_t group)
5662{
5663 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005664 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005665
Neil Roberts96d790e2013-09-19 17:32:00 +01005666 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005667
Neil Roberts96d790e2013-09-19 17:32:00 +01005668 wl_resource_for_each(resource, resource_list) {
5669 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5670 mods_latched, mods_locked, group);
5671 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005672}
5673
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005674static void
5675debug_binding_cancel(struct weston_keyboard_grab *grab)
5676{
5677 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5678
5679 weston_keyboard_end_grab(grab->keyboard);
5680 free(db);
5681}
5682
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005683struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005684 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005685 debug_binding_modifiers,
5686 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005687};
5688
5689static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005690debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005691{
5692 struct debug_binding_grab *grab;
5693
5694 grab = calloc(1, sizeof *grab);
5695 if (!grab)
5696 return;
5697
5698 grab->seat = (struct weston_seat *) seat;
5699 grab->key[0] = key;
5700 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005701 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005702}
5703
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005704static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005705force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005706 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005707{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005708 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005709 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005710 struct desktop_shell *shell = data;
5711 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005712 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005713
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005714 focus_surface = seat->keyboard->focus;
5715 if (!focus_surface)
5716 return;
5717
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005718 wl_signal_emit(&compositor->kill_signal, focus_surface);
5719
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005720 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005721 wl_client_get_credentials(client, &pid, NULL, NULL);
5722
5723 /* Skip clients that we launched ourselves (the credentials of
5724 * the socketpair is ours) */
5725 if (pid == getpid())
5726 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005727
Daniel Stone325fc2d2012-05-30 16:31:58 +01005728 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005729}
5730
5731static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005732workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005733 uint32_t key, void *data)
5734{
5735 struct desktop_shell *shell = data;
5736 unsigned int new_index = shell->workspaces.current;
5737
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005738 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005739 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005740 if (new_index != 0)
5741 new_index--;
5742
5743 change_workspace(shell, new_index);
5744}
5745
5746static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005747workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005748 uint32_t key, void *data)
5749{
5750 struct desktop_shell *shell = data;
5751 unsigned int new_index = shell->workspaces.current;
5752
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005753 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005754 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005755 if (new_index < shell->workspaces.num - 1)
5756 new_index++;
5757
5758 change_workspace(shell, new_index);
5759}
5760
5761static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005762workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005763 uint32_t key, void *data)
5764{
5765 struct desktop_shell *shell = data;
5766 unsigned int new_index;
5767
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005768 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005769 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005770 new_index = key - KEY_F1;
5771 if (new_index >= shell->workspaces.num)
5772 new_index = shell->workspaces.num - 1;
5773
5774 change_workspace(shell, new_index);
5775}
5776
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005777static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005778workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005779 uint32_t key, void *data)
5780{
5781 struct desktop_shell *shell = data;
5782 unsigned int new_index = shell->workspaces.current;
5783
5784 if (shell->locked)
5785 return;
5786
5787 if (new_index != 0)
5788 new_index--;
5789
5790 take_surface_to_workspace_by_seat(shell, seat, new_index);
5791}
5792
5793static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005794workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005795 uint32_t key, void *data)
5796{
5797 struct desktop_shell *shell = data;
5798 unsigned int new_index = shell->workspaces.current;
5799
5800 if (shell->locked)
5801 return;
5802
5803 if (new_index < shell->workspaces.num - 1)
5804 new_index++;
5805
5806 take_surface_to_workspace_by_seat(shell, seat, new_index);
5807}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005808
5809static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005810handle_output_destroy(struct wl_listener *listener, void *data)
5811{
5812 struct shell_output *output_listener =
5813 container_of(listener, struct shell_output, destroy_listener);
5814
5815 wl_list_remove(&output_listener->destroy_listener.link);
5816 wl_list_remove(&output_listener->link);
5817 free(output_listener);
5818}
5819
5820static void
5821create_shell_output(struct desktop_shell *shell,
5822 struct weston_output *output)
5823{
5824 struct shell_output *shell_output;
5825
5826 shell_output = zalloc(sizeof *shell_output);
5827 if (shell_output == NULL)
5828 return;
5829
5830 shell_output->output = output;
5831 shell_output->shell = shell;
5832 shell_output->destroy_listener.notify = handle_output_destroy;
5833 wl_signal_add(&output->destroy_signal,
5834 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005835 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005836}
5837
5838static void
5839handle_output_create(struct wl_listener *listener, void *data)
5840{
5841 struct desktop_shell *shell =
5842 container_of(listener, struct desktop_shell, output_create_listener);
5843 struct weston_output *output = (struct weston_output *)data;
5844
5845 create_shell_output(shell, output);
5846}
5847
5848static void
5849setup_output_destroy_handler(struct weston_compositor *ec,
5850 struct desktop_shell *shell)
5851{
5852 struct weston_output *output;
5853
5854 wl_list_init(&shell->output_list);
5855 wl_list_for_each(output, &ec->output_list, link)
5856 create_shell_output(shell, output);
5857
5858 shell->output_create_listener.notify = handle_output_create;
5859 wl_signal_add(&ec->output_created_signal,
5860 &shell->output_create_listener);
5861}
5862
5863static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005864shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005865{
Tiago Vignattibe143262012-04-16 17:31:41 +03005866 struct desktop_shell *shell =
5867 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005868 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005869 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005870
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005871 if (shell->child.client)
5872 wl_client_destroy(shell->child.client);
5873
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005874 wl_list_remove(&shell->idle_listener.link);
5875 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005876 wl_list_remove(&shell->show_input_panel_listener.link);
5877 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005878
Xiong Zhang6b481422013-10-23 13:58:32 +08005879 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5880 wl_list_remove(&shell_output->destroy_listener.link);
5881 wl_list_remove(&shell_output->link);
5882 free(shell_output);
5883 }
5884
5885 wl_list_remove(&shell->output_create_listener.link);
5886
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005887 wl_array_for_each(ws, &shell->workspaces.array)
5888 workspace_destroy(*ws);
5889 wl_array_release(&shell->workspaces.array);
5890
Pekka Paalanen3c647232011-12-22 13:43:43 +02005891 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005892 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005893 free(shell);
5894}
5895
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005896static void
5897shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5898{
5899 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005900 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005901
5902 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005903 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5904 MODIFIER_CTRL | MODIFIER_ALT,
5905 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005906 weston_compositor_add_key_binding(ec, KEY_TAB,
5907 MODIFIER_ALT,
5908 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005909 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5910 click_to_activate_binding,
5911 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005912 weston_compositor_add_touch_binding(ec, 0,
5913 touch_to_activate_binding,
5914 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005915 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5916 MODIFIER_SUPER | MODIFIER_ALT,
5917 surface_opacity_binding, NULL);
5918 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5919 MODIFIER_SUPER, zoom_axis_binding,
5920 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005921
5922 /* configurable bindings */
5923 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005924 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5925 zoom_key_binding, NULL);
5926 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5927 zoom_key_binding, NULL);
5928 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5929 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005930 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005931 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5932 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005933
5934 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5935 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5936 rotate_binding, NULL);
5937
Daniel Stone325fc2d2012-05-30 16:31:58 +01005938 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5939 shell);
5940 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5941 ec);
5942 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5943 backlight_binding, ec);
5944 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5945 ec);
5946 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5947 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005948 weston_compositor_add_key_binding(ec, KEY_K, mod,
5949 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005950 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5951 workspace_up_binding, shell);
5952 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5953 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005954 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5955 workspace_move_surface_up_binding,
5956 shell);
5957 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5958 workspace_move_surface_down_binding,
5959 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005960
Daniel Stonedf8133b2013-11-19 11:37:14 +01005961 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5962
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005963 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5964 if (shell->workspaces.num > 1) {
5965 num_workspace_bindings = shell->workspaces.num;
5966 if (num_workspace_bindings > 6)
5967 num_workspace_bindings = 6;
5968 for (i = 0; i < num_workspace_bindings; i++)
5969 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5970 workspace_f_binding,
5971 shell);
5972 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005973
5974 /* Debug bindings */
5975 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5976 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005977}
5978
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005979WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005980module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005981 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005982{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005983 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005984 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005985 struct workspace **pws;
5986 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005987 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005988
Peter Huttererf3d62272013-08-08 11:57:05 +10005989 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005990 if (shell == NULL)
5991 return -1;
5992
Kristian Høgsberg75840622011-09-06 13:48:16 -04005993 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005994
5995 shell->destroy_listener.notify = shell_destroy;
5996 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005997 shell->idle_listener.notify = idle_handler;
5998 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5999 shell->wake_listener.notify = wake_handler;
6000 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006001 shell->show_input_panel_listener.notify = show_input_panels;
6002 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6003 shell->hide_input_panel_listener.notify = hide_input_panels;
6004 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006005 shell->update_input_panel_listener.notify = update_input_panels;
6006 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006007 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006008 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006009 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006010 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006011 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006012 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006013 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006014 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006015 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006016 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006017 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006018
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006019 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006020
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006021 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6022 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006023 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6024 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006025 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006026
6027 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006028 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006029
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006030 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006031
Daniel Stonedf8133b2013-11-19 11:37:14 +01006032 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6033 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6034
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006035 for (i = 0; i < shell->workspaces.num; i++) {
6036 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6037 if (pws == NULL)
6038 return -1;
6039
6040 *pws = workspace_create();
6041 if (*pws == NULL)
6042 return -1;
6043 }
6044 activate_workspace(shell, 0);
6045
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006046 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006047 wl_list_init(&shell->workspaces.animation.link);
6048 shell->workspaces.animation.frame = animate_workspace_change_frame;
6049
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006050 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006051 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006052 return -1;
6053
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006054 if (wl_global_create(ec->wl_display,
6055 &desktop_shell_interface, 2,
6056 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006057 return -1;
6058
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006059 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6060 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006061 return -1;
6062
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006063 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006064 shell, bind_input_panel) == NULL)
6065 return -1;
6066
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006067 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6068 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006069 return -1;
6070
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006071 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006072
Xiong Zhang6b481422013-10-23 13:58:32 +08006073 setup_output_destroy_handler(ec, shell);
6074
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006075 loop = wl_display_get_event_loop(ec->wl_display);
6076 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006077
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006078 shell->screensaver.timer =
6079 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6080
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006081 wl_list_for_each(seat, &ec->seat_list, link)
6082 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006083
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006084 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006085
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006086 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006087
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006088 return 0;
6089}