blob: 3b236c928faf3147ca9e9a6aa019c09fc8d868ee [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.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050026#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040027#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020028#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-server.h>
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,
51 ANIMATION_FADE
52};
53
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020054enum fade_type {
55 FADE_IN,
56 FADE_OUT
57};
58
Jonas Ådahl04769742012-06-13 00:01:24 +020059struct focus_state {
60 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040061 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020062 struct weston_surface *keyboard_focus;
63 struct wl_list link;
64 struct wl_listener seat_destroy_listener;
65 struct wl_listener surface_destroy_listener;
66};
67
Jonas Ådahle3cddce2012-06-13 00:01:22 +020068struct workspace {
69 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020070
71 struct wl_list focus_list;
72 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020073};
74
Philipp Brüschweiler88013572012-08-06 13:44:42 +020075struct input_panel_surface {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010076 struct wl_resource resource;
77
78 struct desktop_shell *shell;
79
Philipp Brüschweiler88013572012-08-06 13:44:42 +020080 struct wl_list link;
81 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010082 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020083
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020084 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020085 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020086};
87
Tiago Vignattibe143262012-04-16 17:31:41 +030088struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050089 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040090
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020091 struct wl_listener idle_listener;
92 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040093 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020094 struct wl_listener show_input_panel_listener;
95 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020096 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020097
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050098 struct weston_layer fullscreen_layer;
99 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500100 struct weston_layer background_layer;
101 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200102 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500103
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400104 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300105 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400106
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200107 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200109 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200110 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200111
112 unsigned deathcount;
113 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200114 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200115
116 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200117 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200118 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200119
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200120 struct {
121 struct weston_surface *surface;
122 pixman_box32_t cursor_rectangle;
123 } text_input;
124
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400125 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500126 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100127
Pekka Paalanen77346a62011-11-30 16:26:35 +0200128 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200129 struct wl_array array;
130 unsigned int current;
131 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200132
Jonas Ådahle9d22502012-08-29 22:13:01 +0200133 struct wl_list client_list;
134
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200135 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200136 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200137 int anim_dir;
138 uint32_t anim_timestamp;
139 double anim_current;
140 struct workspace *anim_from;
141 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200142 } workspaces;
143
144 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200145 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200146 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200147 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200149 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200150 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500151
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200152 struct {
153 struct wl_resource *binding;
154 struct wl_list surfaces;
155 } input_panel;
156
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200157 struct {
158 struct weston_surface *surface;
159 struct weston_surface_animation *animation;
160 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300161 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200162 } fade;
163
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300164 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800165 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400166};
167
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500168enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200169 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500170 SHELL_SURFACE_TOPLEVEL,
171 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500172 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800173 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300174 SHELL_SURFACE_POPUP,
175 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200176};
177
Scott Moreauff1db4a2012-04-17 19:06:18 -0600178struct ping_timer {
179 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600180 uint32_t serial;
181};
182
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200183struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500184 struct wl_resource *resource;
185 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200186
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500187 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200188 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400189 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300190 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200191
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400192 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400193 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500194 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800195 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800196 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600197 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100198
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500199 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500201 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200202 } rotation;
203
204 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100205 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500206 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100207 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400208 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500209 } popup;
210
Alex Wu4539b082012-03-01 12:57:46 +0800211 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300212 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400213 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300214 } transient;
215
216 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800217 enum wl_shell_surface_fullscreen_method type;
218 struct weston_transform transform; /* matrix from x, y */
219 uint32_t framerate;
220 struct weston_surface *black_surface;
221 } fullscreen;
222
Scott Moreauff1db4a2012-04-17 19:06:18 -0600223 struct ping_timer *ping_timer;
224
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200225 struct weston_transform workspace_transform;
226
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500227 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100229 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400230
231 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200232};
233
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300234struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400235 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300236 struct shell_surface *shsurf;
237 struct wl_listener shsurf_destroy_listener;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400238 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300239};
240
241struct weston_move_grab {
242 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100243 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500244};
245
Pekka Paalanen460099f2012-01-20 16:48:25 +0200246struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300247 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500248 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200249 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200250 float x;
251 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200252 } center;
253};
254
Giulio Camuffo5085a752013-03-25 21:42:45 +0100255struct shell_seat {
256 struct weston_seat *seat;
257 struct wl_listener seat_destroy_listener;
258
259 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400260 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100261 struct wl_list surfaces_list;
262 struct wl_client *client;
263 int32_t initial_up;
264 } popup_grab;
265};
266
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400267static void
268activate(struct desktop_shell *shell, struct weston_surface *es,
269 struct weston_seat *seat);
270
271static struct workspace *
272get_current_workspace(struct desktop_shell *shell);
273
Alex Wubd3354b2012-04-17 17:20:49 +0800274static struct shell_surface *
275get_shell_surface(struct weston_surface *surface);
276
277static struct desktop_shell *
278shell_surface_get_shell(struct shell_surface *shsurf);
279
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500280static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400281surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500282
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300283static void
284shell_fade_startup(struct desktop_shell *shell);
285
Alex Wubd3354b2012-04-17 17:20:49 +0800286static bool
287shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
288{
289 struct desktop_shell *shell;
290 struct weston_surface *top_fs_es;
291
292 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100293
Alex Wubd3354b2012-04-17 17:20:49 +0800294 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
295 return false;
296
297 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100298 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800299 layer_link);
300 return (shsurf == get_shell_surface(top_fs_es));
301}
302
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500303static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400304destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300305{
306 struct shell_grab *grab;
307
308 grab = container_of(listener, struct shell_grab,
309 shsurf_destroy_listener);
310
311 grab->shsurf = NULL;
312}
313
314static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400315popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400316
317static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300318shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400319 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300320 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400321 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300322 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300324 struct desktop_shell *shell = shsurf->shell;
325
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400326 popup_grab_end(pointer);
327
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300328 grab->grab.interface = interface;
329 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400330 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500331 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400332 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300333
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300334 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300335
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400336 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300337 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400338 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400339 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300340}
341
342static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300343shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300344{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400345 if (grab->shsurf)
346 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300347
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400348 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349}
350
351static void
Alex Wu4539b082012-03-01 12:57:46 +0800352center_on_output(struct weston_surface *surface,
353 struct weston_output *output);
354
Daniel Stone496ca172012-05-30 16:31:42 +0100355static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300356get_modifier(char *modifier)
357{
358 if (!modifier)
359 return MODIFIER_SUPER;
360
361 if (!strcmp("ctrl", modifier))
362 return MODIFIER_CTRL;
363 else if (!strcmp("alt", modifier))
364 return MODIFIER_ALT;
365 else if (!strcmp("super", modifier))
366 return MODIFIER_SUPER;
367 else
368 return MODIFIER_SUPER;
369}
370
Juan Zhaoe10d2792012-04-25 19:09:52 +0800371static enum animation_type
372get_animation_type(char *animation)
373{
374 if (!animation)
375 return ANIMATION_NONE;
376
377 if (!strcmp("zoom", animation))
378 return ANIMATION_ZOOM;
379 else if (!strcmp("fade", animation))
380 return ANIMATION_FADE;
381 else
382 return ANIMATION_NONE;
383}
384
Alex Wu4539b082012-03-01 12:57:46 +0800385static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400386shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200387{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400388 struct weston_config_section *section;
389 int duration;
390 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200391
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400392 section = weston_config_get_section(shell->compositor->config,
393 "screensaver", NULL, NULL);
394 weston_config_section_get_string(section,
395 "path", &shell->screensaver.path, NULL);
396 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200397 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400398
399 section = weston_config_get_section(shell->compositor->config,
400 "shell", NULL, NULL);
401 weston_config_section_get_string(section,
402 "binding-modifier", &s, "super");
403 shell->binding_modifier = get_modifier(s);
404 weston_config_section_get_string(section, "animation", &s, "none");
405 shell->win_animation_type = get_animation_type(s);
406 weston_config_section_get_uint(section, "num-workspaces",
407 &shell->workspaces.num,
408 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200409}
410
411static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200412focus_state_destroy(struct focus_state *state)
413{
414 wl_list_remove(&state->seat_destroy_listener.link);
415 wl_list_remove(&state->surface_destroy_listener.link);
416 free(state);
417}
418
419static void
420focus_state_seat_destroy(struct wl_listener *listener, void *data)
421{
422 struct focus_state *state = container_of(listener,
423 struct focus_state,
424 seat_destroy_listener);
425
426 wl_list_remove(&state->link);
427 focus_state_destroy(state);
428}
429
430static void
431focus_state_surface_destroy(struct wl_listener *listener, void *data)
432{
433 struct focus_state *state = container_of(listener,
434 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400435 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400436 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300437 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400438 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200439
Pekka Paalanen01388e22013-04-25 13:57:44 +0300440 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
441
Kristian Høgsberge3778222012-07-31 17:29:30 -0400442 next = NULL;
443 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300444 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400445 continue;
446
447 next = surface;
448 break;
449 }
450
Pekka Paalanen01388e22013-04-25 13:57:44 +0300451 /* if the focus was a sub-surface, activate its main surface */
452 if (main_surface != state->keyboard_focus)
453 next = main_surface;
454
Kristian Høgsberge3778222012-07-31 17:29:30 -0400455 if (next) {
456 shell = state->seat->compositor->shell_interface.shell;
457 activate(shell, next, state->seat);
458 } else {
459 wl_list_remove(&state->link);
460 focus_state_destroy(state);
461 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200462}
463
464static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400465focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200466{
Jonas Ådahl04769742012-06-13 00:01:24 +0200467 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200468
469 state = malloc(sizeof *state);
470 if (state == NULL)
471 return NULL;
472
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400473 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200474 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400475 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200476
477 state->seat_destroy_listener.notify = focus_state_seat_destroy;
478 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400479 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200480 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400481 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200482
483 return state;
484}
485
Jonas Ådahl8538b222012-08-29 22:13:03 +0200486static struct focus_state *
487ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
488{
489 struct workspace *ws = get_current_workspace(shell);
490 struct focus_state *state;
491
492 wl_list_for_each(state, &ws->focus_list, link)
493 if (state->seat == seat)
494 break;
495
496 if (&state->link == &ws->focus_list)
497 state = focus_state_create(seat, ws);
498
499 return state;
500}
501
Jonas Ådahl04769742012-06-13 00:01:24 +0200502static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400503restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200504{
505 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400506 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200507
508 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400509 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200510
Kristian Høgsberge3148752013-05-06 23:19:49 -0400511 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200512 }
513}
514
515static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200516replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
517 struct weston_seat *seat)
518{
519 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400520 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200521
522 wl_list_for_each(state, &ws->focus_list, link) {
523 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400524 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500525 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200526 return;
527 }
528 }
529}
530
531static void
532drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
533 struct weston_surface *surface)
534{
535 struct focus_state *state;
536
537 wl_list_for_each(state, &ws->focus_list, link)
538 if (state->keyboard_focus == surface)
539 state->keyboard_focus = NULL;
540}
541
542static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200543workspace_destroy(struct workspace *ws)
544{
Jonas Ådahl04769742012-06-13 00:01:24 +0200545 struct focus_state *state, *next;
546
547 wl_list_for_each_safe(state, next, &ws->focus_list, link)
548 focus_state_destroy(state);
549
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200550 free(ws);
551}
552
Jonas Ådahl04769742012-06-13 00:01:24 +0200553static void
554seat_destroyed(struct wl_listener *listener, void *data)
555{
556 struct weston_seat *seat = data;
557 struct focus_state *state, *next;
558 struct workspace *ws = container_of(listener,
559 struct workspace,
560 seat_destroyed_listener);
561
562 wl_list_for_each_safe(state, next, &ws->focus_list, link)
563 if (state->seat == seat)
564 wl_list_remove(&state->link);
565}
566
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200567static struct workspace *
568workspace_create(void)
569{
570 struct workspace *ws = malloc(sizeof *ws);
571 if (ws == NULL)
572 return NULL;
573
574 weston_layer_init(&ws->layer, NULL);
575
Jonas Ådahl04769742012-06-13 00:01:24 +0200576 wl_list_init(&ws->focus_list);
577 wl_list_init(&ws->seat_destroyed_listener.link);
578 ws->seat_destroyed_listener.notify = seat_destroyed;
579
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200580 return ws;
581}
582
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200583static int
584workspace_is_empty(struct workspace *ws)
585{
586 return wl_list_empty(&ws->layer.surface_list);
587}
588
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200589static struct workspace *
590get_workspace(struct desktop_shell *shell, unsigned int index)
591{
592 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200593 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200594 pws += index;
595 return *pws;
596}
597
598static struct workspace *
599get_current_workspace(struct desktop_shell *shell)
600{
601 return get_workspace(shell, shell->workspaces.current);
602}
603
604static void
605activate_workspace(struct desktop_shell *shell, unsigned int index)
606{
607 struct workspace *ws;
608
609 ws = get_workspace(shell, index);
610 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
611
612 shell->workspaces.current = index;
613}
614
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200615static unsigned int
616get_output_height(struct weston_output *output)
617{
618 return abs(output->region.extents.y1 - output->region.extents.y2);
619}
620
621static void
622surface_translate(struct weston_surface *surface, double d)
623{
624 struct shell_surface *shsurf = get_shell_surface(surface);
625 struct weston_transform *transform;
626
627 transform = &shsurf->workspace_transform;
628 if (wl_list_empty(&transform->link))
629 wl_list_insert(surface->geometry.transformation_list.prev,
630 &shsurf->workspace_transform.link);
631
632 weston_matrix_init(&shsurf->workspace_transform.matrix);
633 weston_matrix_translate(&shsurf->workspace_transform.matrix,
634 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200635 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200636}
637
638static void
639workspace_translate_out(struct workspace *ws, double fraction)
640{
641 struct weston_surface *surface;
642 unsigned int height;
643 double d;
644
645 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
646 height = get_output_height(surface->output);
647 d = height * fraction;
648
649 surface_translate(surface, d);
650 }
651}
652
653static void
654workspace_translate_in(struct workspace *ws, double fraction)
655{
656 struct weston_surface *surface;
657 unsigned int height;
658 double d;
659
660 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
661 height = get_output_height(surface->output);
662
663 if (fraction > 0)
664 d = -(height - height * fraction);
665 else
666 d = height + height * fraction;
667
668 surface_translate(surface, d);
669 }
670}
671
672static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200673broadcast_current_workspace_state(struct desktop_shell *shell)
674{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500675 struct wl_list *link;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200676
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500677 for (link = shell->workspaces.client_list.next;
678 link != &shell->workspaces.client_list;
679 link = link->next) {
680 workspace_manager_send_state(wl_resource_from_link(link),
Jonas Ådahle9d22502012-08-29 22:13:01 +0200681 shell->workspaces.current,
682 shell->workspaces.num);
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500683 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200684}
685
686static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200687reverse_workspace_change_animation(struct desktop_shell *shell,
688 unsigned int index,
689 struct workspace *from,
690 struct workspace *to)
691{
692 shell->workspaces.current = index;
693
694 shell->workspaces.anim_to = to;
695 shell->workspaces.anim_from = from;
696 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
697 shell->workspaces.anim_timestamp = 0;
698
Scott Moreau4272e632012-08-13 09:58:41 -0600699 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200700}
701
702static void
703workspace_deactivate_transforms(struct workspace *ws)
704{
705 struct weston_surface *surface;
706 struct shell_surface *shsurf;
707
708 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
709 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200710 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
711 wl_list_remove(&shsurf->workspace_transform.link);
712 wl_list_init(&shsurf->workspace_transform.link);
713 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200714 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200715 }
716}
717
718static void
719finish_workspace_change_animation(struct desktop_shell *shell,
720 struct workspace *from,
721 struct workspace *to)
722{
Scott Moreau4272e632012-08-13 09:58:41 -0600723 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200724
725 wl_list_remove(&shell->workspaces.animation.link);
726 workspace_deactivate_transforms(from);
727 workspace_deactivate_transforms(to);
728 shell->workspaces.anim_to = NULL;
729
730 wl_list_remove(&shell->workspaces.anim_from->layer.link);
731}
732
733static void
734animate_workspace_change_frame(struct weston_animation *animation,
735 struct weston_output *output, uint32_t msecs)
736{
737 struct desktop_shell *shell =
738 container_of(animation, struct desktop_shell,
739 workspaces.animation);
740 struct workspace *from = shell->workspaces.anim_from;
741 struct workspace *to = shell->workspaces.anim_to;
742 uint32_t t;
743 double x, y;
744
745 if (workspace_is_empty(from) && workspace_is_empty(to)) {
746 finish_workspace_change_animation(shell, from, to);
747 return;
748 }
749
750 if (shell->workspaces.anim_timestamp == 0) {
751 if (shell->workspaces.anim_current == 0.0)
752 shell->workspaces.anim_timestamp = msecs;
753 else
754 shell->workspaces.anim_timestamp =
755 msecs -
756 /* Invers of movement function 'y' below. */
757 (asin(1.0 - shell->workspaces.anim_current) *
758 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
759 M_2_PI);
760 }
761
762 t = msecs - shell->workspaces.anim_timestamp;
763
764 /*
765 * x = [0, π/2]
766 * y(x) = sin(x)
767 */
768 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
769 y = sin(x);
770
771 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600772 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200773
774 workspace_translate_out(from, shell->workspaces.anim_dir * y);
775 workspace_translate_in(to, shell->workspaces.anim_dir * y);
776 shell->workspaces.anim_current = y;
777
Scott Moreau4272e632012-08-13 09:58:41 -0600778 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200779 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200780 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200781 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200782}
783
784static void
785animate_workspace_change(struct desktop_shell *shell,
786 unsigned int index,
787 struct workspace *from,
788 struct workspace *to)
789{
790 struct weston_output *output;
791
792 int dir;
793
794 if (index > shell->workspaces.current)
795 dir = -1;
796 else
797 dir = 1;
798
799 shell->workspaces.current = index;
800
801 shell->workspaces.anim_dir = dir;
802 shell->workspaces.anim_from = from;
803 shell->workspaces.anim_to = to;
804 shell->workspaces.anim_current = 0.0;
805 shell->workspaces.anim_timestamp = 0;
806
807 output = container_of(shell->compositor->output_list.next,
808 struct weston_output, link);
809 wl_list_insert(&output->animation_list,
810 &shell->workspaces.animation.link);
811
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200812 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200813
814 workspace_translate_in(to, 0);
815
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400816 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200817
Scott Moreau4272e632012-08-13 09:58:41 -0600818 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200819}
820
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200821static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200822update_workspace(struct desktop_shell *shell, unsigned int index,
823 struct workspace *from, struct workspace *to)
824{
825 shell->workspaces.current = index;
826 wl_list_insert(&from->layer.link, &to->layer.link);
827 wl_list_remove(&from->layer.link);
828}
829
830static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200831change_workspace(struct desktop_shell *shell, unsigned int index)
832{
833 struct workspace *from;
834 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200835
836 if (index == shell->workspaces.current)
837 return;
838
839 /* Don't change workspace when there is any fullscreen surfaces. */
840 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
841 return;
842
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200843 from = get_current_workspace(shell);
844 to = get_workspace(shell, index);
845
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200846 if (shell->workspaces.anim_from == to &&
847 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200848 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200849 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200850 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851 return;
852 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200853
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200854 if (shell->workspaces.anim_to != NULL)
855 finish_workspace_change_animation(shell,
856 shell->workspaces.anim_from,
857 shell->workspaces.anim_to);
858
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200859 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200860
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200861 if (workspace_is_empty(to) && workspace_is_empty(from))
862 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200863 else
864 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200865
866 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200867}
868
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200869static bool
870workspace_has_only(struct workspace *ws, struct weston_surface *surface)
871{
872 struct wl_list *list = &ws->layer.surface_list;
873 struct wl_list *e;
874
875 if (wl_list_empty(list))
876 return false;
877
878 e = list->next;
879
880 if (e->next != list)
881 return false;
882
883 return container_of(e, struct weston_surface, layer_link) == surface;
884}
885
886static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200887move_surface_to_workspace(struct desktop_shell *shell,
888 struct weston_surface *surface,
889 uint32_t workspace)
890{
891 struct workspace *from;
892 struct workspace *to;
893 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300894 struct weston_surface *focus;
895
896 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200897
898 if (workspace == shell->workspaces.current)
899 return;
900
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200901 if (workspace >= shell->workspaces.num)
902 workspace = shell->workspaces.num - 1;
903
Jonas Ådahle9d22502012-08-29 22:13:01 +0200904 from = get_current_workspace(shell);
905 to = get_workspace(shell, workspace);
906
907 wl_list_remove(&surface->layer_link);
908 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
909
910 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300911 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
912 if (!seat->keyboard)
913 continue;
914
915 focus = weston_surface_get_main_surface(seat->keyboard->focus);
916 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400917 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300918 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200919
920 weston_surface_damage_below(surface);
921}
922
923static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200924take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400925 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200926 unsigned int index)
927{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300928 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929 struct shell_surface *shsurf;
930 struct workspace *from;
931 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200932 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200933
Pekka Paalanen01388e22013-04-25 13:57:44 +0300934 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200935 if (surface == NULL ||
936 index == shell->workspaces.current)
937 return;
938
939 from = get_current_workspace(shell);
940 to = get_workspace(shell, index);
941
942 wl_list_remove(&surface->layer_link);
943 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
944
Jonas Ådahle9d22502012-08-29 22:13:01 +0200945 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200946 drop_focus_state(shell, from, surface);
947
948 if (shell->workspaces.anim_from == to &&
949 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200950 wl_list_remove(&to->layer.link);
951 wl_list_insert(from->layer.link.prev, &to->layer.link);
952
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200953 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200954 broadcast_current_workspace_state(shell);
955
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200956 return;
957 }
958
959 if (shell->workspaces.anim_to != NULL)
960 finish_workspace_change_animation(shell,
961 shell->workspaces.anim_from,
962 shell->workspaces.anim_to);
963
964 if (workspace_is_empty(from) &&
965 workspace_has_only(to, surface))
966 update_workspace(shell, index, from, to);
967 else {
968 shsurf = get_shell_surface(surface);
969 if (wl_list_empty(&shsurf->workspace_transform.link))
970 wl_list_insert(&shell->workspaces.anim_sticky_list,
971 &shsurf->workspace_transform.link);
972
973 animate_workspace_change(shell, index, from, to);
974 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200975
976 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200977
978 state = ensure_focus_state(shell, seat);
979 if (state != NULL)
980 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200981}
982
983static void
984workspace_manager_move_surface(struct wl_client *client,
985 struct wl_resource *resource,
986 struct wl_resource *surface_resource,
987 uint32_t workspace)
988{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500989 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200990 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500991 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300992 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200993
Pekka Paalanen01388e22013-04-25 13:57:44 +0300994 main_surface = weston_surface_get_main_surface(surface);
995 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200996}
997
998static const struct workspace_manager_interface workspace_manager_implementation = {
999 workspace_manager_move_surface,
1000};
1001
1002static void
1003unbind_resource(struct wl_resource *resource)
1004{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001005 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001006 free(resource);
1007}
1008
1009static void
1010bind_workspace_manager(struct wl_client *client,
1011 void *data, uint32_t version, uint32_t id)
1012{
1013 struct desktop_shell *shell = data;
1014 struct wl_resource *resource;
1015
1016 resource = wl_client_add_object(client, &workspace_manager_interface,
1017 &workspace_manager_implementation,
1018 id, shell);
1019
1020 if (resource == NULL) {
1021 weston_log("couldn't add workspace manager object");
1022 return;
1023 }
1024
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001025 wl_resource_set_destructor(resource, unbind_resource);
1026 wl_list_insert(&shell->workspaces.client_list,
1027 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001028
1029 workspace_manager_send_state(resource,
1030 shell->workspaces.current,
1031 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001032}
1033
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001034static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001035noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001036{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001037}
1038
1039static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001040move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001041{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001042 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001043 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001044 struct shell_surface *shsurf = move->base.shsurf;
1045 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001046 int dx = wl_fixed_to_int(pointer->x + move->dx);
1047 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001048
1049 if (!shsurf)
1050 return;
1051
1052 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001053
Daniel Stone103db7f2012-05-08 17:17:55 +01001054 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001055 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001056
1057 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001058}
1059
1060static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001061move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001062 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001063{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001064 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1065 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001066 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001067 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001068
Daniel Stone4dbadb12012-05-30 16:31:51 +01001069 if (pointer->button_count == 0 &&
1070 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001071 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001072 free(grab);
1073 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001074}
1075
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001076static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001077 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001078 move_grab_motion,
1079 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001080};
1081
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001082static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001083surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001084{
1085 struct weston_move_grab *move;
1086
1087 if (!shsurf)
1088 return -1;
1089
1090 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1091 return 0;
1092
1093 move = malloc(sizeof *move);
1094 if (!move)
1095 return -1;
1096
1097 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001098 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001099 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001100 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001101
1102 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001103 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001104
1105 return 0;
1106}
1107
1108static void
1109shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1110 struct wl_resource *seat_resource, uint32_t serial)
1111{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001112 struct weston_seat *seat = seat_resource->data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001113 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001114 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001115
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001116 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001117 if (seat->pointer->button_count == 0 ||
1118 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001119 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001120 return;
1121
Kristian Høgsberge3148752013-05-06 23:19:49 -04001122 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001123 wl_resource_post_no_memory(resource);
1124}
1125
1126struct weston_resize_grab {
1127 struct shell_grab base;
1128 uint32_t edges;
1129 int32_t width, height;
1130};
1131
1132static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001133resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001134{
1135 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001136 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001137 struct shell_surface *shsurf = resize->base.shsurf;
1138 int32_t width, height;
1139 wl_fixed_t from_x, from_y;
1140 wl_fixed_t to_x, to_y;
1141
1142 if (!shsurf)
1143 return;
1144
1145 weston_surface_from_global_fixed(shsurf->surface,
1146 pointer->grab_x, pointer->grab_y,
1147 &from_x, &from_y);
1148 weston_surface_from_global_fixed(shsurf->surface,
1149 pointer->x, pointer->y, &to_x, &to_y);
1150
1151 width = resize->width;
1152 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1153 width += wl_fixed_to_int(from_x - to_x);
1154 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1155 width += wl_fixed_to_int(to_x - from_x);
1156 }
1157
1158 height = resize->height;
1159 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1160 height += wl_fixed_to_int(from_y - to_y);
1161 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1162 height += wl_fixed_to_int(to_y - from_y);
1163 }
1164
1165 shsurf->client->send_configure(shsurf->surface,
1166 resize->edges, width, height);
1167}
1168
1169static void
1170send_configure(struct weston_surface *surface,
1171 uint32_t edges, int32_t width, int32_t height)
1172{
1173 struct shell_surface *shsurf = get_shell_surface(surface);
1174
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001175 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001176 edges, width, height);
1177}
1178
1179static const struct weston_shell_client shell_client = {
1180 send_configure
1181};
1182
1183static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001184resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001185 uint32_t time, uint32_t button, uint32_t state_w)
1186{
1187 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001188 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001189 enum wl_pointer_button_state state = state_w;
1190
1191 if (pointer->button_count == 0 &&
1192 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1193 shell_grab_end(&resize->base);
1194 free(grab);
1195 }
1196}
1197
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001198static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001199 noop_grab_focus,
1200 resize_grab_motion,
1201 resize_grab_button,
1202};
1203
Giulio Camuffob8366642013-04-25 13:57:46 +03001204/*
1205 * Returns the bounding box of a surface and all its sub-surfaces,
1206 * in the surface coordinates system. */
1207static void
1208surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1209 int32_t *y, int32_t *w, int32_t *h) {
1210 pixman_region32_t region;
1211 pixman_box32_t *box;
1212 struct weston_subsurface *subsurface;
1213
1214 pixman_region32_init_rect(&region, 0, 0,
1215 surface->geometry.width,
1216 surface->geometry.height);
1217
1218 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1219 pixman_region32_union_rect(&region, &region,
1220 subsurface->position.x,
1221 subsurface->position.y,
1222 subsurface->surface->geometry.width,
1223 subsurface->surface->geometry.height);
1224 }
1225
1226 box = pixman_region32_extents(&region);
1227 if (x)
1228 *x = box->x1;
1229 if (y)
1230 *y = box->y1;
1231 if (w)
1232 *w = box->x2 - box->x1;
1233 if (h)
1234 *h = box->y2 - box->y1;
1235
1236 pixman_region32_fini(&region);
1237}
1238
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001239static int
1240surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001241 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001242{
1243 struct weston_resize_grab *resize;
1244
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001245 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1246 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001247 return 0;
1248
1249 if (edges == 0 || edges > 15 ||
1250 (edges & 3) == 3 || (edges & 12) == 12)
1251 return 0;
1252
1253 resize = malloc(sizeof *resize);
1254 if (!resize)
1255 return -1;
1256
1257 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001258 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1259 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001260
1261 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001262 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001263
1264 return 0;
1265}
1266
1267static void
1268shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1269 struct wl_resource *seat_resource, uint32_t serial,
1270 uint32_t edges)
1271{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001272 struct weston_seat *seat = seat_resource->data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001273 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001274 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001275
1276 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1277 return;
1278
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001279 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001280 if (seat->pointer->button_count == 0 ||
1281 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001282 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001283 return;
1284
Kristian Høgsberge3148752013-05-06 23:19:49 -04001285 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001286 wl_resource_post_no_memory(resource);
1287}
1288
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001289static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001290busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001291{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001292 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001293 struct weston_pointer *pointer = base->pointer;
1294 struct weston_surface *surface;
1295 wl_fixed_t sx, sy;
1296
1297 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1298 pointer->x, pointer->y,
1299 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001300
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001301 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001302 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001303 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001304 }
1305}
1306
1307static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001308busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001309{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001310}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001311
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001312static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001313busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001314 uint32_t time, uint32_t button, uint32_t state)
1315{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001316 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001317 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001318 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001319
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001320 if (shsurf && button == BTN_LEFT && state) {
1321 activate(shsurf->shell, shsurf->surface, seat);
1322 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001323 } else if (shsurf && button == BTN_RIGHT && state) {
1324 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001325 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001326 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001327}
1328
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001329static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001330 busy_cursor_grab_focus,
1331 busy_cursor_grab_motion,
1332 busy_cursor_grab_button,
1333};
1334
1335static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001336set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001337{
1338 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001339
1340 grab = malloc(sizeof *grab);
1341 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001342 return;
1343
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001344 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1345 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001346}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001347
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001348static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001349end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001350{
1351 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1352
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001353 if (grab->grab.interface == &busy_cursor_grab_interface &&
1354 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001355 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001356 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001357 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001358}
1359
Scott Moreau9521d5e2012-04-19 13:06:17 -06001360static void
1361ping_timer_destroy(struct shell_surface *shsurf)
1362{
1363 if (!shsurf || !shsurf->ping_timer)
1364 return;
1365
1366 if (shsurf->ping_timer->source)
1367 wl_event_source_remove(shsurf->ping_timer->source);
1368
1369 free(shsurf->ping_timer);
1370 shsurf->ping_timer = NULL;
1371}
1372
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001373static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001374ping_timeout_handler(void *data)
1375{
1376 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001377 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001378
Scott Moreau9521d5e2012-04-19 13:06:17 -06001379 /* Client is not responding */
1380 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001381
1382 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001383 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001384 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001385
1386 return 1;
1387}
1388
1389static void
1390ping_handler(struct weston_surface *surface, uint32_t serial)
1391{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001392 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001393 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001394 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001395
1396 if (!shsurf)
1397 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001398 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001399 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001400
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001401 if (shsurf->surface == shsurf->shell->grab_surface)
1402 return;
1403
Scott Moreauff1db4a2012-04-17 19:06:18 -06001404 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001405 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001406 if (!shsurf->ping_timer)
1407 return;
1408
1409 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001410 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1411 shsurf->ping_timer->source =
1412 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1413 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1414
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001415 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001416 }
1417}
1418
1419static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001420handle_pointer_focus(struct wl_listener *listener, void *data)
1421{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001422 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001423 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001424 struct weston_compositor *compositor;
1425 struct shell_surface *shsurf;
1426 uint32_t serial;
1427
1428 if (!surface)
1429 return;
1430
1431 compositor = surface->compositor;
1432 shsurf = get_shell_surface(surface);
1433
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001434 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001435 set_busy_cursor(shsurf, pointer);
1436 } else {
1437 serial = wl_display_next_serial(compositor->wl_display);
1438 ping_handler(surface, serial);
1439 }
1440}
1441
1442static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001443create_pointer_focus_listener(struct weston_seat *seat)
1444{
1445 struct wl_listener *listener;
1446
Kristian Høgsberge3148752013-05-06 23:19:49 -04001447 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001448 return;
1449
1450 listener = malloc(sizeof *listener);
1451 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001452 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001453}
1454
1455static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001456shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1457 uint32_t serial)
1458{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001459 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001460 struct weston_seat *seat;
1461 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001462
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001463 if (shsurf->ping_timer == NULL)
1464 /* Just ignore unsolicited pong. */
1465 return;
1466
Scott Moreauff1db4a2012-04-17 19:06:18 -06001467 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001468 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001469 wl_list_for_each(seat, &ec->seat_list, link) {
1470 if(seat->pointer)
1471 end_busy_cursor(shsurf, seat->pointer);
1472 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001473 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001474 }
1475}
1476
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001477static void
1478shell_surface_set_title(struct wl_client *client,
1479 struct wl_resource *resource, const char *title)
1480{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001481 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001482
1483 free(shsurf->title);
1484 shsurf->title = strdup(title);
1485}
1486
1487static void
1488shell_surface_set_class(struct wl_client *client,
1489 struct wl_resource *resource, const char *class)
1490{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001491 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001492
1493 free(shsurf->class);
1494 shsurf->class = strdup(class);
1495}
1496
Juan Zhao96879df2012-02-07 08:45:41 +08001497static struct weston_output *
1498get_default_output(struct weston_compositor *compositor)
1499{
1500 return container_of(compositor->output_list.next,
1501 struct weston_output, link);
1502}
1503
Alex Wu4539b082012-03-01 12:57:46 +08001504static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001505restore_output_mode(struct weston_output *output)
1506{
1507 if (output->current != output->origin ||
1508 (int32_t)output->scale != output->origin_scale)
1509 weston_output_switch_mode(output,
1510 output->origin,
1511 output->origin_scale);
1512}
1513
1514static void
1515restore_all_output_modes(struct weston_compositor *compositor)
1516{
1517 struct weston_output *output;
1518
1519 wl_list_for_each(output, &compositor->output_list, link)
1520 restore_output_mode(output);
1521}
1522
1523static void
Alex Wu4539b082012-03-01 12:57:46 +08001524shell_unset_fullscreen(struct shell_surface *shsurf)
1525{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001526 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001527 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001528 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1529 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001530 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001531 }
Alex Wu4539b082012-03-01 12:57:46 +08001532 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1533 shsurf->fullscreen.framerate = 0;
1534 wl_list_remove(&shsurf->fullscreen.transform.link);
1535 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001536 if (shsurf->fullscreen.black_surface)
1537 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001538 shsurf->fullscreen.black_surface = NULL;
1539 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001540 weston_surface_set_position(shsurf->surface,
1541 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001542 if (shsurf->saved_rotation_valid) {
1543 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1544 &shsurf->rotation.transform.link);
1545 shsurf->saved_rotation_valid = false;
1546 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001547
1548 ws = get_current_workspace(shsurf->shell);
1549 wl_list_remove(&shsurf->surface->layer_link);
1550 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001551}
1552
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001553static void
1554shell_unset_maximized(struct shell_surface *shsurf)
1555{
1556 struct workspace *ws;
1557 /* undo all maximized things here */
1558 shsurf->output = get_default_output(shsurf->surface->compositor);
1559 weston_surface_set_position(shsurf->surface,
1560 shsurf->saved_x,
1561 shsurf->saved_y);
1562
1563 if (shsurf->saved_rotation_valid) {
1564 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1565 &shsurf->rotation.transform.link);
1566 shsurf->saved_rotation_valid = false;
1567 }
1568
1569 ws = get_current_workspace(shsurf->shell);
1570 wl_list_remove(&shsurf->surface->layer_link);
1571 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1572}
1573
Pekka Paalanen98262232011-12-01 10:42:22 +02001574static int
1575reset_shell_surface_type(struct shell_surface *surface)
1576{
1577 switch (surface->type) {
1578 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001579 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001580 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001581 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001582 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001583 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001584 case SHELL_SURFACE_NONE:
1585 case SHELL_SURFACE_TOPLEVEL:
1586 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001587 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001588 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001589 break;
1590 }
1591
1592 surface->type = SHELL_SURFACE_NONE;
1593 return 0;
1594}
1595
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001596static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001597set_surface_type(struct shell_surface *shsurf)
1598{
1599 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001600 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001601
1602 reset_shell_surface_type(shsurf);
1603
1604 shsurf->type = shsurf->next_type;
1605 shsurf->next_type = SHELL_SURFACE_NONE;
1606
1607 switch (shsurf->type) {
1608 case SHELL_SURFACE_TOPLEVEL:
1609 break;
1610 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001611 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001612 pes->geometry.x + shsurf->transient.x,
1613 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001614 break;
1615
1616 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001617 case SHELL_SURFACE_FULLSCREEN:
1618 shsurf->saved_x = surface->geometry.x;
1619 shsurf->saved_y = surface->geometry.y;
1620 shsurf->saved_position_valid = true;
1621
1622 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1623 wl_list_remove(&shsurf->rotation.transform.link);
1624 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001625 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001626 shsurf->saved_rotation_valid = true;
1627 }
1628 break;
1629
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001630 case SHELL_SURFACE_XWAYLAND:
1631 weston_surface_set_position(surface, shsurf->transient.x,
1632 shsurf->transient.y);
1633 break;
1634
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001635 default:
1636 break;
1637 }
1638}
1639
1640static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001641set_toplevel(struct shell_surface *shsurf)
1642{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001643 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001644}
1645
1646static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001647shell_surface_set_toplevel(struct wl_client *client,
1648 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001649{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001650 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001651
Tiago Vignattibc052c92012-04-19 16:18:18 +03001652 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001653}
1654
1655static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001656set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001657 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001658{
1659 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001660 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001661 shsurf->transient.x = x;
1662 shsurf->transient.y = y;
1663 shsurf->transient.flags = flags;
1664 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1665}
1666
1667static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001668shell_surface_set_transient(struct wl_client *client,
1669 struct wl_resource *resource,
1670 struct wl_resource *parent_resource,
1671 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001672{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001673 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001674 struct weston_surface *parent =
1675 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001676
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001677 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001678}
1679
Tiago Vignattibe143262012-04-16 17:31:41 +03001680static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001681shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001682{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001683 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001684}
1685
1686static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001687get_output_panel_height(struct desktop_shell *shell,
1688 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001689{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001690 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001691 int panel_height = 0;
1692
1693 if (!output)
1694 return 0;
1695
Juan Zhao4ab94682012-07-09 22:24:09 -07001696 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001697 if (surface->output == output) {
1698 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001699 break;
1700 }
1701 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001702
Juan Zhao96879df2012-02-07 08:45:41 +08001703 return panel_height;
1704}
1705
1706static void
1707shell_surface_set_maximized(struct wl_client *client,
1708 struct wl_resource *resource,
1709 struct wl_resource *output_resource )
1710{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001711 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001712 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001713 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001714 uint32_t edges = 0, panel_height = 0;
1715
1716 /* get the default output, if the client set it as NULL
1717 check whether the ouput is available */
1718 if (output_resource)
1719 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001720 else if (es->output)
1721 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001722 else
1723 shsurf->output = get_default_output(es->compositor);
1724
Tiago Vignattibe143262012-04-16 17:31:41 +03001725 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001726 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001727 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001728
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001729 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001730 shsurf->output->width,
1731 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001732
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001733 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001734}
1735
Alex Wu21858432012-04-01 20:13:08 +08001736static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001737black_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 +08001738
Alex Wu4539b082012-03-01 12:57:46 +08001739static struct weston_surface *
1740create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001741 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001742 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001743{
1744 struct weston_surface *surface = NULL;
1745
1746 surface = weston_surface_create(ec);
1747 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001748 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001749 return NULL;
1750 }
1751
Alex Wu21858432012-04-01 20:13:08 +08001752 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001753 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001754 weston_surface_configure(surface, x, y, w, h);
1755 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001756 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001757 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001758 pixman_region32_fini(&surface->input);
1759 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001760
Alex Wu4539b082012-03-01 12:57:46 +08001761 return surface;
1762}
1763
1764/* Create black surface and append it to the associated fullscreen surface.
1765 * Handle size dismatch and positioning according to the method. */
1766static void
1767shell_configure_fullscreen(struct shell_surface *shsurf)
1768{
1769 struct weston_output *output = shsurf->fullscreen_output;
1770 struct weston_surface *surface = shsurf->surface;
1771 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001772 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001773 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001774
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001775 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1776 restore_output_mode(output);
1777
Alex Wu4539b082012-03-01 12:57:46 +08001778 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001779 shsurf->fullscreen.black_surface =
1780 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001781 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001782 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001783 output->width,
1784 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001785
1786 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1787 wl_list_insert(&surface->layer_link,
1788 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001789 shsurf->fullscreen.black_surface->output = output;
1790
Giulio Camuffob8366642013-04-25 13:57:46 +03001791 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1792 &surf_width, &surf_height);
1793
Alex Wu4539b082012-03-01 12:57:46 +08001794 switch (shsurf->fullscreen.type) {
1795 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001796 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001797 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001798 break;
1799 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001800 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001801 if (output->width == surf_width &&
1802 output->height == surf_height) {
1803 weston_surface_set_position(surface, output->x - surf_x,
1804 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001805 break;
1806 }
1807
Alex Wu4539b082012-03-01 12:57:46 +08001808 matrix = &shsurf->fullscreen.transform.matrix;
1809 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001810
Scott Moreau1bad5db2012-08-18 01:04:05 -06001811 output_aspect = (float) output->width /
1812 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001813 surface_aspect = (float) surface->geometry.width /
1814 (float) surface->geometry.height;
1815 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001816 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001817 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001818 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001819 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001820 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001821
Alex Wu4539b082012-03-01 12:57:46 +08001822 weston_matrix_scale(matrix, scale, scale, 1);
1823 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001824 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001825 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001826 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1827 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001828 weston_surface_set_position(surface, x, y);
1829
Alex Wu4539b082012-03-01 12:57:46 +08001830 break;
1831 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001832 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001833 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001834 surf_width * surface->buffer_scale,
1835 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001836 shsurf->fullscreen.framerate};
1837
Alexander Larsson355748e2013-05-28 16:23:38 +02001838 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001839 weston_surface_set_position(surface,
1840 output->x - surf_x,
1841 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001842 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001843 output->x - surf_x,
1844 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001845 output->width,
1846 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001847 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001848 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001849 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001850 center_on_output(surface, output);
1851 }
Alex Wubd3354b2012-04-17 17:20:49 +08001852 }
Alex Wu4539b082012-03-01 12:57:46 +08001853 break;
1854 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001855 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001856 break;
1857 default:
1858 break;
1859 }
1860}
1861
1862/* make the fullscreen and black surface at the top */
1863static void
1864shell_stack_fullscreen(struct shell_surface *shsurf)
1865{
Alex Wubd3354b2012-04-17 17:20:49 +08001866 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001867 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001868 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001869
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001870 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001871 wl_list_insert(&shell->fullscreen_layer.surface_list,
1872 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001873 weston_surface_damage(surface);
1874
1875 if (!shsurf->fullscreen.black_surface)
1876 shsurf->fullscreen.black_surface =
1877 create_black_surface(surface->compositor,
1878 surface,
1879 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001880 output->width,
1881 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001882
1883 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001884 wl_list_insert(&surface->layer_link,
1885 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001886 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001887}
1888
1889static void
1890shell_map_fullscreen(struct shell_surface *shsurf)
1891{
Alex Wu4539b082012-03-01 12:57:46 +08001892 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001893 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001894}
1895
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001896static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001897set_fullscreen(struct shell_surface *shsurf,
1898 uint32_t method,
1899 uint32_t framerate,
1900 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001901{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001902 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001903
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001904 if (output)
1905 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001906 else if (es->output)
1907 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001908 else
1909 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001910
Alex Wu4539b082012-03-01 12:57:46 +08001911 shsurf->fullscreen_output = shsurf->output;
1912 shsurf->fullscreen.type = method;
1913 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001914 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001915
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001916 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001917 shsurf->output->width,
1918 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001919}
1920
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001921static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001922shell_surface_set_fullscreen(struct wl_client *client,
1923 struct wl_resource *resource,
1924 uint32_t method,
1925 uint32_t framerate,
1926 struct wl_resource *output_resource)
1927{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001928 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001929 struct weston_output *output;
1930
1931 if (output_resource)
1932 output = output_resource->data;
1933 else
1934 output = NULL;
1935
1936 set_fullscreen(shsurf, method, framerate, output);
1937}
1938
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001939static void
1940set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1941{
1942 /* XXX: using the same fields for transient type */
1943 shsurf->transient.x = x;
1944 shsurf->transient.y = y;
1945 shsurf->transient.flags = flags;
1946 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1947}
1948
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001949static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001950
1951static void
1952destroy_shell_seat(struct wl_listener *listener, void *data)
1953{
1954 struct shell_seat *shseat =
1955 container_of(listener,
1956 struct shell_seat, seat_destroy_listener);
1957 struct shell_surface *shsurf, *prev = NULL;
1958
1959 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001960 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001961 shseat->popup_grab.client = NULL;
1962
1963 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1964 shsurf->popup.shseat = NULL;
1965 if (prev) {
1966 wl_list_init(&prev->popup.grab_link);
1967 }
1968 prev = shsurf;
1969 }
1970 wl_list_init(&prev->popup.grab_link);
1971 }
1972
1973 wl_list_remove(&shseat->seat_destroy_listener.link);
1974 free(shseat);
1975}
1976
1977static struct shell_seat *
1978create_shell_seat(struct weston_seat *seat)
1979{
1980 struct shell_seat *shseat;
1981
1982 shseat = calloc(1, sizeof *shseat);
1983 if (!shseat) {
1984 weston_log("no memory to allocate shell seat\n");
1985 return NULL;
1986 }
1987
1988 shseat->seat = seat;
1989 wl_list_init(&shseat->popup_grab.surfaces_list);
1990
1991 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1992 wl_signal_add(&seat->destroy_signal,
1993 &shseat->seat_destroy_listener);
1994
1995 return shseat;
1996}
1997
1998static struct shell_seat *
1999get_shell_seat(struct weston_seat *seat)
2000{
2001 struct wl_listener *listener;
2002
2003 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2004 if (listener == NULL)
2005 return create_shell_seat(seat);
2006
2007 return container_of(listener,
2008 struct shell_seat, seat_destroy_listener);
2009}
2010
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002011static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002012popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002013{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002014 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002015 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002016 struct shell_seat *shseat =
2017 container_of(grab, struct shell_seat, popup_grab.grab);
2018 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002019 wl_fixed_t sx, sy;
2020
2021 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2022 pointer->x, pointer->y,
2023 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002024
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002025 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002026 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002027 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002028 weston_pointer_set_focus(pointer, NULL,
2029 wl_fixed_from_int(0),
2030 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002031 }
2032}
2033
2034static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002035popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002036{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002037 struct weston_pointer *pointer = grab->pointer;
2038 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002039
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002040 if (pointer->focus_resource) {
2041 weston_surface_from_global_fixed(pointer->focus,
2042 pointer->x, pointer->y,
2043 &sx, &sy);
2044 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2045 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002046}
2047
2048static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002049popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002050 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002051{
2052 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002053 struct shell_seat *shseat =
2054 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002055 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002056 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002057 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002058
Daniel Stone37816df2012-05-16 18:45:18 +01002059 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002060 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002061 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002062 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002063 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002064 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002065 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002066 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002067 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002068 }
2069
Daniel Stone4dbadb12012-05-30 16:31:51 +01002070 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002071 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002072}
2073
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002074static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002075 popup_grab_focus,
2076 popup_grab_motion,
2077 popup_grab_button,
2078};
2079
2080static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002081popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002082{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002083 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002084 struct shell_seat *shseat =
2085 container_of(grab, struct shell_seat, popup_grab.grab);
2086 struct shell_surface *shsurf;
2087 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002088
2089 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002090 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002091 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002092 shseat->popup_grab.grab.interface = NULL;
2093 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002094 /* Send the popup_done event to all the popups open */
2095 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002096 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002097 shsurf->popup.shseat = NULL;
2098 if (prev) {
2099 wl_list_init(&prev->popup.grab_link);
2100 }
2101 prev = shsurf;
2102 }
2103 wl_list_init(&prev->popup.grab_link);
2104 wl_list_init(&shseat->popup_grab.surfaces_list);
2105 }
2106}
2107
2108static void
2109add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2110{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002111 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002112
2113 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002114 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002115 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002116 /* We must make sure here that this popup was opened after
2117 * a mouse press, and not just by moving around with other
2118 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002119 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002120 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002121
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002122 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002123 }
2124 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2125}
2126
2127static void
2128remove_popup_grab(struct shell_surface *shsurf)
2129{
2130 struct shell_seat *shseat = shsurf->popup.shseat;
2131
2132 wl_list_remove(&shsurf->popup.grab_link);
2133 wl_list_init(&shsurf->popup.grab_link);
2134 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002135 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002136 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002137 }
2138}
2139
2140static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002141shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002142{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002143 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002144 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002145 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002146
2147 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002148
Pekka Paalanen483243f2013-03-08 14:56:50 +02002149 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002150 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2151 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002152
Kristian Høgsberge3148752013-05-06 23:19:49 -04002153 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002154 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002155 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002156 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002157 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002158 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002159}
2160
2161static void
2162shell_surface_set_popup(struct wl_client *client,
2163 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002164 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002165 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002166 struct wl_resource *parent_resource,
2167 int32_t x, int32_t y, uint32_t flags)
2168{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002169 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002170
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002171 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002172 shsurf->parent = wl_resource_get_user_data(parent_resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002173 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002174 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002175 shsurf->popup.x = x;
2176 shsurf->popup.y = y;
2177}
2178
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002179static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002180 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002181 shell_surface_move,
2182 shell_surface_resize,
2183 shell_surface_set_toplevel,
2184 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002185 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002186 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002187 shell_surface_set_maximized,
2188 shell_surface_set_title,
2189 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002190};
2191
2192static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002193destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002194{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002195 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2196
Giulio Camuffo5085a752013-03-25 21:42:45 +01002197 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2198 remove_popup_grab(shsurf);
2199 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002200
Alex Wubd3354b2012-04-17 17:20:49 +08002201 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002202 shell_surface_is_top_fullscreen(shsurf))
2203 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002204
Alex Wuaa08e2d2012-03-05 11:01:40 +08002205 if (shsurf->fullscreen.black_surface)
2206 weston_surface_destroy(shsurf->fullscreen.black_surface);
2207
Alex Wubd3354b2012-04-17 17:20:49 +08002208 /* As destroy_resource() use wl_list_for_each_safe(),
2209 * we can always remove the listener.
2210 */
2211 wl_list_remove(&shsurf->surface_destroy_listener.link);
2212 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002213 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002214 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002215
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002216 wl_list_remove(&shsurf->link);
2217 free(shsurf);
2218}
2219
2220static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002221shell_destroy_shell_surface(struct wl_resource *resource)
2222{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002223 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002224
2225 destroy_shell_surface(shsurf);
2226}
2227
2228static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002229shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002230{
2231 struct shell_surface *shsurf = container_of(listener,
2232 struct shell_surface,
2233 surface_destroy_listener);
2234
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002235 if (shsurf->resource)
2236 wl_resource_destroy(shsurf->resource);
2237 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002238 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002239}
2240
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002241static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002242shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002243
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002244static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002245get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002246{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002247 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002248 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002249 else
2250 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002251}
2252
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002253static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002254create_shell_surface(void *shell, struct weston_surface *surface,
2255 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002256{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002257 struct shell_surface *shsurf;
2258
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002259 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002260 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002261 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002262 }
2263
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002264 shsurf = calloc(1, sizeof *shsurf);
2265 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002266 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002267 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002268 }
2269
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002270 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002271 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002272
Tiago Vignattibc052c92012-04-19 16:18:18 +03002273 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002274 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002275 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002276 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002277 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002278 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2279 shsurf->fullscreen.framerate = 0;
2280 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002281 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002282 wl_list_init(&shsurf->fullscreen.transform.link);
2283
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002284 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002285 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002286 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002287 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002288
2289 /* init link so its safe to always remove it in destroy_shell_surface */
2290 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002291 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002292
Pekka Paalanen460099f2012-01-20 16:48:25 +02002293 /* empty when not in use */
2294 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002295 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002296
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002297 wl_list_init(&shsurf->workspace_transform.link);
2298
Pekka Paalanen98262232011-12-01 10:42:22 +02002299 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002300 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002301
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002302 shsurf->client = client;
2303
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002304 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002305}
2306
2307static void
2308shell_get_shell_surface(struct wl_client *client,
2309 struct wl_resource *resource,
2310 uint32_t id,
2311 struct wl_resource *surface_resource)
2312{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002313 struct weston_surface *surface =
2314 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002315 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002316 struct shell_surface *shsurf;
2317
2318 if (get_shell_surface(surface)) {
2319 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002320 WL_DISPLAY_ERROR_INVALID_OBJECT,
2321 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002322 return;
2323 }
2324
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002325 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002326 if (!shsurf) {
2327 wl_resource_post_error(surface_resource,
2328 WL_DISPLAY_ERROR_INVALID_OBJECT,
2329 "surface->configure already set");
2330 return;
2331 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002332
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002333 shsurf->resource = wl_client_add_object(client,
2334 &wl_shell_surface_interface,
2335 &shell_surface_implementation,
2336 id, shsurf);
2337 wl_resource_set_destructor(shsurf->resource,
2338 shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002339}
2340
2341static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002342 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002343};
2344
Kristian Høgsberg07937562011-04-12 17:25:42 -04002345static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002346shell_fade(struct desktop_shell *shell, enum fade_type type);
2347
2348static int
2349screensaver_timeout(void *data)
2350{
2351 struct desktop_shell *shell = data;
2352
2353 shell_fade(shell, FADE_OUT);
2354
2355 return 1;
2356}
2357
2358static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002359handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002360{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002361 struct desktop_shell *shell =
2362 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002363
Pekka Paalanen18027e52011-12-02 16:31:49 +02002364 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002365
2366 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002367 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002368}
2369
2370static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002371launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002372{
2373 if (shell->screensaver.binding)
2374 return;
2375
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002376 if (!shell->screensaver.path) {
2377 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002378 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002379 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002380
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002381 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002382 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002383 return;
2384 }
2385
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002386 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002387 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002388 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002389 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002390}
2391
2392static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002393terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002394{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002395 if (shell->screensaver.process.pid == 0)
2396 return;
2397
2398 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002399}
2400
2401static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002402configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002403{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002404 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002405
Giulio Camuffo184df502013-02-21 11:29:21 +01002406 if (width == 0)
2407 return;
2408
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002409 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2410 if (s->output == es->output && s != es) {
2411 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002412 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002413 }
2414 }
2415
Giulio Camuffo184df502013-02-21 11:29:21 +01002416 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002417
2418 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002419 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002420 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002421 }
2422}
2423
2424static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002425background_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 -04002426{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002427 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002428
Giulio Camuffo184df502013-02-21 11:29:21 +01002429 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002430}
2431
2432static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002433desktop_shell_set_background(struct wl_client *client,
2434 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002435 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002436 struct wl_resource *surface_resource)
2437{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002438 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002439 struct weston_surface *surface =
2440 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002441
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002442 if (surface->configure) {
2443 wl_resource_post_error(surface_resource,
2444 WL_DISPLAY_ERROR_INVALID_OBJECT,
2445 "surface role already assigned");
2446 return;
2447 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002448
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002449 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002450 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002451 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002452 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002453 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002454 surface->output->width,
2455 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002456}
2457
2458static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002459panel_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 -04002460{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002461 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002462
Giulio Camuffo184df502013-02-21 11:29:21 +01002463 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002464}
2465
2466static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002467desktop_shell_set_panel(struct wl_client *client,
2468 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002469 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002470 struct wl_resource *surface_resource)
2471{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002472 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002473 struct weston_surface *surface =
2474 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002475
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002476 if (surface->configure) {
2477 wl_resource_post_error(surface_resource,
2478 WL_DISPLAY_ERROR_INVALID_OBJECT,
2479 "surface role already assigned");
2480 return;
2481 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002482
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002483 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002484 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002485 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002486 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002487 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002488 surface->output->width,
2489 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002490}
2491
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002492static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002493lock_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 -04002494{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002495 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002496
Giulio Camuffo184df502013-02-21 11:29:21 +01002497 if (width == 0)
2498 return;
2499
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002500 surface->geometry.width = width;
2501 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002502 center_on_output(surface, get_default_output(shell->compositor));
2503
2504 if (!weston_surface_is_mapped(surface)) {
2505 wl_list_insert(&shell->lock_layer.surface_list,
2506 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002507 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002508 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002509 }
2510}
2511
2512static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002513handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002514{
Tiago Vignattibe143262012-04-16 17:31:41 +03002515 struct desktop_shell *shell =
2516 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002517
Martin Minarik6d118362012-06-07 18:01:59 +02002518 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002519 shell->lock_surface = NULL;
2520}
2521
2522static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002523desktop_shell_set_lock_surface(struct wl_client *client,
2524 struct wl_resource *resource,
2525 struct wl_resource *surface_resource)
2526{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002527 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002528 struct weston_surface *surface =
2529 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002530
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002531 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002532
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002533 if (!shell->locked)
2534 return;
2535
Pekka Paalanen98262232011-12-01 10:42:22 +02002536 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002537
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002538 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002539 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002540 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002541
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002542 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002543 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002544}
2545
2546static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002547resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002548{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002549 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002550
Pekka Paalanen77346a62011-11-30 16:26:35 +02002551 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002552
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002553 wl_list_remove(&shell->lock_layer.link);
2554 wl_list_insert(&shell->compositor->cursor_layer.link,
2555 &shell->fullscreen_layer.link);
2556 wl_list_insert(&shell->fullscreen_layer.link,
2557 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002558 if (shell->showing_input_panels) {
2559 wl_list_insert(&shell->panel_layer.link,
2560 &shell->input_panel_layer.link);
2561 wl_list_insert(&shell->input_panel_layer.link,
2562 &ws->layer.link);
2563 } else {
2564 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2565 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002566
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002567 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002568
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002569 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002570 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002571 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002572}
2573
2574static void
2575desktop_shell_unlock(struct wl_client *client,
2576 struct wl_resource *resource)
2577{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002578 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002579
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002580 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002581
2582 if (shell->locked)
2583 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002584}
2585
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002586static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002587desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002588 struct wl_resource *resource,
2589 struct wl_resource *surface_resource)
2590{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002591 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002592
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002593 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002594}
2595
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002596static void
2597desktop_shell_desktop_ready(struct wl_client *client,
2598 struct wl_resource *resource)
2599{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002600 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002601
2602 shell_fade_startup(shell);
2603}
2604
Kristian Høgsberg75840622011-09-06 13:48:16 -04002605static const struct desktop_shell_interface desktop_shell_implementation = {
2606 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002607 desktop_shell_set_panel,
2608 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002609 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002610 desktop_shell_set_grab_surface,
2611 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002612};
2613
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002614static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002615get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002616{
2617 struct shell_surface *shsurf;
2618
2619 shsurf = get_shell_surface(surface);
2620 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002621 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002622 return shsurf->type;
2623}
2624
Kristian Høgsberg75840622011-09-06 13:48:16 -04002625static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002626move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002627{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002628 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002629 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002630 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002631 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002632
Pekka Paalanen01388e22013-04-25 13:57:44 +03002633 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002634 if (surface == NULL)
2635 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002636
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002637 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002638 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2639 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002640 return;
2641
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002642 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002643}
2644
2645static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002646resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002647{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002648 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002649 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002650 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002651 uint32_t edges = 0;
2652 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002653 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002654
Pekka Paalanen01388e22013-04-25 13:57:44 +03002655 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002656 if (surface == NULL)
2657 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002658
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002659 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002660 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2661 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002662 return;
2663
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002664 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002665 wl_fixed_to_int(seat->pointer->grab_x),
2666 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002667 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002668
Pekka Paalanen60921e52012-01-25 15:55:43 +02002669 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002670 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002671 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002672 edges |= 0;
2673 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002674 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002675
Pekka Paalanen60921e52012-01-25 15:55:43 +02002676 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002677 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002678 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002679 edges |= 0;
2680 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002681 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002682
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002683 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002684}
2685
2686static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002687surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002688 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002689{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002690 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002691 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002692 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002693 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002694 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002695
Pekka Paalanen01388e22013-04-25 13:57:44 +03002696 /* XXX: broken for windows containing sub-surfaces */
2697 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002698 if (surface == NULL)
2699 return;
2700
2701 shsurf = get_shell_surface(surface);
2702 if (!shsurf)
2703 return;
2704
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002705 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002706
Scott Moreau02709af2012-05-22 01:54:10 -06002707 if (surface->alpha > 1.0)
2708 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002709 if (surface->alpha < step)
2710 surface->alpha = step;
2711
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002712 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002713 weston_surface_damage(surface);
2714}
2715
2716static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002717do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002718 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002719{
Daniel Stone37816df2012-05-16 18:45:18 +01002720 struct weston_seat *ws = (struct weston_seat *) seat;
2721 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002722 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002723 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002724
2725 wl_list_for_each(output, &compositor->output_list, link) {
2726 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002727 wl_fixed_to_double(seat->pointer->x),
2728 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002729 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002730 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002731 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002732 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002733 increment = -output->zoom.increment;
2734 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002735 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002736 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002737 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002738 else
2739 increment = 0;
2740
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002741 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002742
Scott Moreaue6603982012-06-11 13:07:51 -06002743 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002744 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002745 else if (output->zoom.level > output->zoom.max_level)
2746 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002747 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002748 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002749 output->disable_planes++;
2750 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002751
Scott Moreaue6603982012-06-11 13:07:51 -06002752 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002753
Scott Moreau8dacaab2012-06-17 18:10:58 -06002754 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002755 }
2756 }
2757}
2758
Scott Moreauccbf29d2012-02-22 14:21:41 -07002759static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002760zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002761 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002762{
2763 do_zoom(seat, time, 0, axis, value);
2764}
2765
2766static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002767zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002768 void *data)
2769{
2770 do_zoom(seat, time, key, 0, 0);
2771}
2772
2773static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002774terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002775 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002776{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002777 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002778
Daniel Stone325fc2d2012-05-30 16:31:58 +01002779 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002780}
2781
2782static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002783rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002784{
2785 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002786 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002787 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002788 struct shell_surface *shsurf = rotate->base.shsurf;
2789 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002790 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002791
2792 if (!shsurf)
2793 return;
2794
2795 surface = shsurf->surface;
2796
2797 cx = 0.5f * surface->geometry.width;
2798 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002799
Daniel Stone37816df2012-05-16 18:45:18 +01002800 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2801 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002802 r = sqrtf(dx * dx + dy * dy);
2803
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002804 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002805 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002806
2807 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002808 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002809 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002810
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002811 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002812 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002813
2814 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002815 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002816 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002817 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002818 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002819
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002820 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002821 &shsurf->surface->geometry.transformation_list,
2822 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002823 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002824 wl_list_init(&shsurf->rotation.transform.link);
2825 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002826 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002827 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002828
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002829 /* We need to adjust the position of the surface
2830 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002831 cposx = surface->geometry.x + cx;
2832 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002833 dposx = rotate->center.x - cposx;
2834 dposy = rotate->center.y - cposy;
2835 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002836 weston_surface_set_position(surface,
2837 surface->geometry.x + dposx,
2838 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002839 }
2840
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002841 /* Repaint implies weston_surface_update_transform(), which
2842 * lazily applies the damage due to rotation update.
2843 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002844 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002845}
2846
2847static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002848rotate_grab_button(struct weston_pointer_grab *grab,
2849 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002850{
2851 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002852 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002853 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002854 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002855 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002856
Daniel Stone4dbadb12012-05-30 16:31:51 +01002857 if (pointer->button_count == 0 &&
2858 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002859 if (shsurf)
2860 weston_matrix_multiply(&shsurf->rotation.rotation,
2861 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002862 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002863 free(rotate);
2864 }
2865}
2866
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002867static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002868 noop_grab_focus,
2869 rotate_grab_motion,
2870 rotate_grab_button,
2871};
2872
2873static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002874surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002875{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002876 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002877 float dx, dy;
2878 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002879
Pekka Paalanen460099f2012-01-20 16:48:25 +02002880 rotate = malloc(sizeof *rotate);
2881 if (!rotate)
2882 return;
2883
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002884 weston_surface_to_global_float(surface->surface,
2885 surface->surface->geometry.width / 2,
2886 surface->surface->geometry.height / 2,
2887 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002888
Daniel Stone37816df2012-05-16 18:45:18 +01002889 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2890 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002891 r = sqrtf(dx * dx + dy * dy);
2892 if (r > 20.0f) {
2893 struct weston_matrix inverse;
2894
2895 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002896 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002897 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002898
2899 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002900 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002901 } else {
2902 weston_matrix_init(&surface->rotation.rotation);
2903 weston_matrix_init(&rotate->rotation);
2904 }
2905
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002906 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2907 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002908}
2909
2910static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002911rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002912 void *data)
2913{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002914 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002915 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002916 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002917 struct shell_surface *surface;
2918
Pekka Paalanen01388e22013-04-25 13:57:44 +03002919 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002920 if (base_surface == NULL)
2921 return;
2922
2923 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002924 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2925 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002926 return;
2927
2928 surface_rotate(surface, seat);
2929}
2930
2931static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002932lower_fullscreen_layer(struct desktop_shell *shell)
2933{
2934 struct workspace *ws;
2935 struct weston_surface *surface, *prev;
2936
2937 ws = get_current_workspace(shell);
2938 wl_list_for_each_reverse_safe(surface, prev,
2939 &shell->fullscreen_layer.surface_list,
2940 layer_link)
2941 weston_surface_restack(surface, &ws->layer.surface_list);
2942}
2943
2944static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002945activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002946 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002947{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002948 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002949 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002950 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002951
Pekka Paalanen01388e22013-04-25 13:57:44 +03002952 main_surface = weston_surface_get_main_surface(es);
2953
Daniel Stone37816df2012-05-16 18:45:18 +01002954 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002955
Jonas Ådahl8538b222012-08-29 22:13:03 +02002956 state = ensure_focus_state(shell, seat);
2957 if (state == NULL)
2958 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002959
2960 state->keyboard_focus = es;
2961 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002962 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002963
Pekka Paalanen01388e22013-04-25 13:57:44 +03002964 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002965 case SHELL_SURFACE_FULLSCREEN:
2966 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002967 shell_stack_fullscreen(get_shell_surface(main_surface));
2968 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002969 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002970 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002971 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002972 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002973 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002974 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002975 }
2976}
2977
Alex Wu21858432012-04-01 20:13:08 +08002978/* no-op func for checking black surface */
2979static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002980black_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 +08002981{
2982}
2983
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002984static bool
Alex Wu21858432012-04-01 20:13:08 +08002985is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2986{
2987 if (es->configure == black_surface_configure) {
2988 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002989 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002990 return true;
2991 }
2992 return false;
2993}
2994
Kristian Høgsberg75840622011-09-06 13:48:16 -04002995static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002996click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002997 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002998{
Daniel Stone37816df2012-05-16 18:45:18 +01002999 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003000 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003001 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003002 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003003
Daniel Stone37816df2012-05-16 18:45:18 +01003004 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003005 if (!focus)
3006 return;
3007
Pekka Paalanen01388e22013-04-25 13:57:44 +03003008 if (is_black_surface(focus, &main_surface))
3009 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003010
Pekka Paalanen01388e22013-04-25 13:57:44 +03003011 main_surface = weston_surface_get_main_surface(focus);
3012 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003013 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003014
Daniel Stone325fc2d2012-05-30 16:31:58 +01003015 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003016 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003017}
3018
3019static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003020lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003021{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003022 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003023
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003024 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003025 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003026 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003027 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003028
3029 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003030
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003031 /* Hide all surfaces by removing the fullscreen, panel and
3032 * toplevel layers. This way nothing else can show or receive
3033 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003034
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003035 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003036 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003037 if (shell->showing_input_panels)
3038 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003039 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003040 wl_list_insert(&shell->compositor->cursor_layer.link,
3041 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003042
Pekka Paalanen77346a62011-11-30 16:26:35 +02003043 launch_screensaver(shell);
3044
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003045 /* TODO: disable bindings that should not work while locked. */
3046
3047 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003048}
3049
3050static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003051unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003052{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003053 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003054 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003055 return;
3056 }
3057
3058 /* If desktop-shell client has gone away, unlock immediately. */
3059 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003060 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003061 return;
3062 }
3063
3064 if (shell->prepare_event_sent)
3065 return;
3066
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003067 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003068 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003069}
3070
3071static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003072shell_fade_done(struct weston_surface_animation *animation, void *data)
3073{
3074 struct desktop_shell *shell = data;
3075
3076 shell->fade.animation = NULL;
3077
3078 switch (shell->fade.type) {
3079 case FADE_IN:
3080 weston_surface_destroy(shell->fade.surface);
3081 shell->fade.surface = NULL;
3082 break;
3083 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003084 lock(shell);
3085 break;
3086 }
3087}
3088
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003089static struct weston_surface *
3090shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003091{
3092 struct weston_compositor *compositor = shell->compositor;
3093 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003094
3095 surface = weston_surface_create(compositor);
3096 if (!surface)
3097 return NULL;
3098
3099 weston_surface_configure(surface, 0, 0, 8192, 8192);
3100 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3101 wl_list_insert(&compositor->fade_layer.surface_list,
3102 &surface->layer_link);
3103 pixman_region32_init(&surface->input);
3104
3105 return surface;
3106}
3107
3108static void
3109shell_fade(struct desktop_shell *shell, enum fade_type type)
3110{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003111 float tint;
3112
3113 switch (type) {
3114 case FADE_IN:
3115 tint = 0.0;
3116 break;
3117 case FADE_OUT:
3118 tint = 1.0;
3119 break;
3120 default:
3121 weston_log("shell: invalid fade type\n");
3122 return;
3123 }
3124
3125 shell->fade.type = type;
3126
3127 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003128 shell->fade.surface = shell_fade_create_surface(shell);
3129 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003130 return;
3131
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003132 shell->fade.surface->alpha = 1.0 - tint;
3133 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003134 }
3135
3136 if (shell->fade.animation)
3137 weston_fade_update(shell->fade.animation,
3138 shell->fade.surface->alpha, tint, 30.0);
3139 else
3140 shell->fade.animation =
3141 weston_fade_run(shell->fade.surface,
3142 1.0 - tint, tint, 30.0,
3143 shell_fade_done, shell);
3144}
3145
3146static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003147do_shell_fade_startup(void *data)
3148{
3149 struct desktop_shell *shell = data;
3150
3151 shell_fade(shell, FADE_IN);
3152}
3153
3154static void
3155shell_fade_startup(struct desktop_shell *shell)
3156{
3157 struct wl_event_loop *loop;
3158
3159 if (!shell->fade.startup_timer)
3160 return;
3161
3162 wl_event_source_remove(shell->fade.startup_timer);
3163 shell->fade.startup_timer = NULL;
3164
3165 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3166 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3167}
3168
3169static int
3170fade_startup_timeout(void *data)
3171{
3172 struct desktop_shell *shell = data;
3173
3174 shell_fade_startup(shell);
3175 return 0;
3176}
3177
3178static void
3179shell_fade_init(struct desktop_shell *shell)
3180{
3181 /* Make compositor output all black, and wait for the desktop-shell
3182 * client to signal it is ready, then fade in. The timer triggers a
3183 * fade-in, in case the desktop-shell client takes too long.
3184 */
3185
3186 struct wl_event_loop *loop;
3187
3188 if (shell->fade.surface != NULL) {
3189 weston_log("%s: warning: fade surface already exists\n",
3190 __func__);
3191 return;
3192 }
3193
3194 shell->fade.surface = shell_fade_create_surface(shell);
3195 if (!shell->fade.surface)
3196 return;
3197
3198 weston_surface_update_transform(shell->fade.surface);
3199 weston_surface_damage(shell->fade.surface);
3200
3201 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3202 shell->fade.startup_timer =
3203 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3204 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3205}
3206
3207static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003208idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003209{
3210 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003211 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003212
3213 shell_fade(shell, FADE_OUT);
3214 /* lock() is called from shell_fade_done() */
3215}
3216
3217static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003218wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003219{
3220 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003221 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003222
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003223 unlock(shell);
3224}
3225
3226static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003227show_input_panels(struct wl_listener *listener, void *data)
3228{
3229 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003230 container_of(listener, struct desktop_shell,
3231 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003232 struct input_panel_surface *surface, *next;
3233 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003234
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003235 shell->text_input.surface = (struct weston_surface*)data;
3236
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003237 if (shell->showing_input_panels)
3238 return;
3239
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003240 shell->showing_input_panels = true;
3241
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003242 if (!shell->locked)
3243 wl_list_insert(&shell->panel_layer.link,
3244 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003245
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003246 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003247 &shell->input_panel.surfaces, link) {
3248 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003249 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003250 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003251 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003252 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003253 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003254 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003255 weston_surface_damage(ws);
3256 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003257 }
3258}
3259
3260static void
3261hide_input_panels(struct wl_listener *listener, void *data)
3262{
3263 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003264 container_of(listener, struct desktop_shell,
3265 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003266 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003267
Jan Arne Petersen61381972013-01-31 15:52:21 +01003268 if (!shell->showing_input_panels)
3269 return;
3270
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003271 shell->showing_input_panels = false;
3272
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003273 if (!shell->locked)
3274 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003275
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003276 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003277 &shell->input_panel_layer.surface_list, layer_link)
3278 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003279}
3280
3281static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003282update_input_panels(struct wl_listener *listener, void *data)
3283{
3284 struct desktop_shell *shell =
3285 container_of(listener, struct desktop_shell,
3286 update_input_panel_listener);
3287
3288 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3289}
3290
3291static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003292center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003293{
Giulio Camuffob8366642013-04-25 13:57:46 +03003294 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003295 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003296
Giulio Camuffob8366642013-04-25 13:57:46 +03003297 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3298
3299 x = output->x + (output->width - width) / 2 - surf_x / 2;
3300 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003301
3302 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003303}
3304
3305static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003306weston_surface_set_initial_position (struct weston_surface *surface,
3307 struct desktop_shell *shell)
3308{
3309 struct weston_compositor *compositor = shell->compositor;
3310 int ix = 0, iy = 0;
3311 int range_x, range_y;
3312 int dx, dy, x, y, panel_height;
3313 struct weston_output *output, *target_output = NULL;
3314 struct weston_seat *seat;
3315
3316 /* As a heuristic place the new window on the same output as the
3317 * pointer. Falling back to the output containing 0, 0.
3318 *
3319 * TODO: Do something clever for touch too?
3320 */
3321 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003322 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003323 ix = wl_fixed_to_int(seat->pointer->x);
3324 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003325 break;
3326 }
3327 }
3328
3329 wl_list_for_each(output, &compositor->output_list, link) {
3330 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3331 target_output = output;
3332 break;
3333 }
3334 }
3335
3336 if (!target_output) {
3337 weston_surface_set_position(surface, 10 + random() % 400,
3338 10 + random() % 400);
3339 return;
3340 }
3341
3342 /* Valid range within output where the surface will still be onscreen.
3343 * If this is negative it means that the surface is bigger than
3344 * output.
3345 */
3346 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003347 range_x = target_output->width - surface->geometry.width;
3348 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003349 surface->geometry.height;
3350
Rob Bradford4cb88c72012-08-13 15:18:44 +01003351 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003352 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003353 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003354 dx = 0;
3355
3356 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003357 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003358 else
3359 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003360
3361 x = target_output->x + dx;
3362 y = target_output->y + dy;
3363
3364 weston_surface_set_position (surface, x, y);
3365}
3366
3367static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003368map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003369 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003370{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003371 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003372 struct shell_surface *shsurf = get_shell_surface(surface);
3373 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003374 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003375 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003376 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003377 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003378 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003379
Pekka Paalanen60921e52012-01-25 15:55:43 +02003380 surface->geometry.width = width;
3381 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003382 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003383
3384 /* initial positioning, see also configure() */
3385 switch (surface_type) {
3386 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003387 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003388 break;
Alex Wu4539b082012-03-01 12:57:46 +08003389 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003390 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003391 shell_map_fullscreen(shsurf);
3392 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003393 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003394 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003395 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003396 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3397 NULL, NULL);
3398 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3399 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003400 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003401 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003402 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003403 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003404 case SHELL_SURFACE_NONE:
3405 weston_surface_set_position(surface,
3406 surface->geometry.x + sx,
3407 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003408 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003409 default:
3410 ;
3411 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003412
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003413 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003414 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003415 case SHELL_SURFACE_POPUP:
3416 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003417 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003418 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3419 break;
Alex Wu4539b082012-03-01 12:57:46 +08003420 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003421 case SHELL_SURFACE_NONE:
3422 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003423 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003424 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003425 ws = get_current_workspace(shell);
3426 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003427 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003428 }
3429
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003430 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003431 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003432 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3433 surface->output = shsurf->output;
3434 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003435
Juan Zhao7bb92f02011-12-15 11:31:51 -05003436 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003437 /* XXX: xwayland's using the same fields for transient type */
3438 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003439 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003440 if (shsurf->transient.flags ==
3441 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3442 break;
3443 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003444 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003445 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003446 if (!shell->locked) {
3447 wl_list_for_each(seat, &compositor->seat_list, link)
3448 activate(shell, surface, seat);
3449 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003450 break;
3451 default:
3452 break;
3453 }
3454
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003455 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003456 {
3457 switch (shell->win_animation_type) {
3458 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003459 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003460 break;
3461 case ANIMATION_ZOOM:
3462 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3463 break;
3464 default:
3465 break;
3466 }
3467 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003468}
3469
3470static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003471configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003472 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003473{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003474 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3475 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003476 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003477
Pekka Paalanen77346a62011-11-30 16:26:35 +02003478 shsurf = get_shell_surface(surface);
3479 if (shsurf)
3480 surface_type = shsurf->type;
3481
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003482 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003483
3484 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003485 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003486 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003487 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003488 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003489 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003490 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003491 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3492 NULL, NULL);
3493 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003494 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003495 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003496 break;
Alex Wu4539b082012-03-01 12:57:46 +08003497 case SHELL_SURFACE_TOPLEVEL:
3498 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003499 default:
3500 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003501 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003502
Alex Wu4539b082012-03-01 12:57:46 +08003503 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003504 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003505 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003506
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003507 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003508 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003509 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003510}
3511
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003512static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003513shell_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 +03003514{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003515 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003516 struct desktop_shell *shell = shsurf->shell;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003517 struct weston_compositor *compositor = shsurf->surface->compositor;
Giulio Camuffo184df502013-02-21 11:29:21 +01003518
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003519 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003520
Giulio Camuffo5085a752013-03-25 21:42:45 +01003521 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3522 remove_popup_grab(shsurf);
3523 }
3524
Giulio Camuffo184df502013-02-21 11:29:21 +01003525 if (width == 0)
3526 return;
3527
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003528 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003529 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003530 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003531 type_changed = 1;
3532 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003533
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003534 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003535 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003536 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003537 es->geometry.width != width ||
3538 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003539 float from_x, from_y;
3540 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003541
3542 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3543 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3544 configure(shell, es,
3545 es->geometry.x + to_x - from_x,
3546 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003547 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003548 }
3549}
3550
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003551static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003552
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003553static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003554desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003555{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003556 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003557 struct desktop_shell *shell =
3558 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003559
3560 shell->child.process.pid = 0;
3561 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003562
3563 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3564 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003565 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003566 shell->child.deathstamp = time;
3567 shell->child.deathcount = 0;
3568 }
3569
3570 shell->child.deathcount++;
3571 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003572 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003573 return;
3574 }
3575
Martin Minarik6d118362012-06-07 18:01:59 +02003576 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003577 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003578 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003579}
3580
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003581static void
3582launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003583{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003584 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003585 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003586
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003587 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003588 &shell->child.process,
3589 shell_exe,
3590 desktop_shell_sigchld);
3591
3592 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003593 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003594}
3595
3596static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003597bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3598{
Tiago Vignattibe143262012-04-16 17:31:41 +03003599 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003600
3601 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003602 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003603}
3604
Kristian Høgsberg75840622011-09-06 13:48:16 -04003605static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003606unbind_desktop_shell(struct wl_resource *resource)
3607{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003608 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003609
3610 if (shell->locked)
3611 resume_desktop(shell);
3612
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003613 shell->child.desktop_shell = NULL;
3614 shell->prepare_event_sent = false;
3615 free(resource);
3616}
3617
3618static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003619bind_desktop_shell(struct wl_client *client,
3620 void *data, uint32_t version, uint32_t id)
3621{
Tiago Vignattibe143262012-04-16 17:31:41 +03003622 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003623 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003624
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003625 resource = wl_client_add_object(client, &desktop_shell_interface,
3626 &desktop_shell_implementation,
3627 id, shell);
3628
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003629 if (client == shell->child.client) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003630 wl_resource_set_destructor(resource, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003631 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003632
3633 if (version < 2)
3634 shell_fade_startup(shell);
3635
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003636 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003637 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003638
3639 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3640 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003641 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003642}
3643
Pekka Paalanen6e168112011-11-24 11:34:05 +02003644static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003645screensaver_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 -04003646{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003647 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003648
Giulio Camuffo184df502013-02-21 11:29:21 +01003649 if (width == 0)
3650 return;
3651
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003652 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003653 if (!shell->locked)
3654 return;
3655
3656 center_on_output(surface, surface->output);
3657
3658 if (wl_list_empty(&surface->layer_link)) {
3659 wl_list_insert(shell->lock_layer.surface_list.prev,
3660 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003661 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003662 wl_event_source_timer_update(shell->screensaver.timer,
3663 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003664 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003665 }
3666}
3667
3668static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003669screensaver_set_surface(struct wl_client *client,
3670 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003671 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003672 struct wl_resource *output_resource)
3673{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003674 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003675 struct weston_surface *surface =
3676 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003677 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003678
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003679 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003680 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003681 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003682}
3683
3684static const struct screensaver_interface screensaver_implementation = {
3685 screensaver_set_surface
3686};
3687
3688static void
3689unbind_screensaver(struct wl_resource *resource)
3690{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003691 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003692
Pekka Paalanen77346a62011-11-30 16:26:35 +02003693 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003694 free(resource);
3695}
3696
3697static void
3698bind_screensaver(struct wl_client *client,
3699 void *data, uint32_t version, uint32_t id)
3700{
Tiago Vignattibe143262012-04-16 17:31:41 +03003701 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003702 struct wl_resource *resource;
3703
3704 resource = wl_client_add_object(client, &screensaver_interface,
3705 &screensaver_implementation,
3706 id, shell);
3707
Pekka Paalanen77346a62011-11-30 16:26:35 +02003708 if (shell->screensaver.binding == NULL) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003709 wl_resource_set_destructor(resource, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003710 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003711 return;
3712 }
3713
3714 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3715 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003716 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003717}
3718
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003719static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003720input_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 -04003721{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003722 struct input_panel_surface *ip_surface = surface->configure_private;
3723 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003724 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003725 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003726 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003727
Giulio Camuffo184df502013-02-21 11:29:21 +01003728 if (width == 0)
3729 return;
3730
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003731 if (!weston_surface_is_mapped(surface)) {
3732 if (!shell->showing_input_panels)
3733 return;
3734
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003735 show_surface = 1;
3736 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003737
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003738 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003739
3740 if (ip_surface->panel) {
3741 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3742 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3743 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003744 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003745
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003746 x = ip_surface->output->x + (mode->width - width) / 2;
3747 y = ip_surface->output->y + mode->height - height;
3748 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003749
3750 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003751 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003752 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003753
3754 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003755 wl_list_insert(&shell->input_panel_layer.surface_list,
3756 &surface->layer_link);
3757 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003758 weston_surface_damage(surface);
3759 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3760 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003761}
3762
3763static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003764destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003765{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003766 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003767 wl_list_remove(&input_panel_surface->link);
3768
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003769 input_panel_surface->surface->configure = NULL;
3770
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003771 free(input_panel_surface);
3772}
3773
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003774static struct input_panel_surface *
3775get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003776{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003777 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003778 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003779 } else {
3780 return NULL;
3781 }
3782}
3783
3784static void
3785input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3786{
3787 struct input_panel_surface *ipsurface = container_of(listener,
3788 struct input_panel_surface,
3789 surface_destroy_listener);
3790
3791 if (ipsurface->resource.client) {
3792 wl_resource_destroy(&ipsurface->resource);
3793 } else {
3794 wl_signal_emit(&ipsurface->resource.destroy_signal,
3795 &ipsurface->resource);
3796 destroy_input_panel_surface(ipsurface);
3797 }
3798}
3799static struct input_panel_surface *
3800create_input_panel_surface(struct desktop_shell *shell,
3801 struct weston_surface *surface)
3802{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003803 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003804
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003805 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3806 if (!input_panel_surface)
3807 return NULL;
3808
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003809 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003810 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003811
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003812 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003813
3814 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003815
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003816 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3817 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003818 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003819 &input_panel_surface->surface_destroy_listener);
3820
3821 wl_list_init(&input_panel_surface->link);
3822
3823 return input_panel_surface;
3824}
3825
3826static void
3827input_panel_surface_set_toplevel(struct wl_client *client,
3828 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003829 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003830 uint32_t position)
3831{
3832 struct input_panel_surface *input_panel_surface = resource->data;
3833 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003834
3835 wl_list_insert(&shell->input_panel.surfaces,
3836 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003837
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003838 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003839 input_panel_surface->panel = 0;
3840}
3841
3842static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003843input_panel_surface_set_overlay_panel(struct wl_client *client,
3844 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003845{
3846 struct input_panel_surface *input_panel_surface = resource->data;
3847 struct desktop_shell *shell = input_panel_surface->shell;
3848
3849 wl_list_insert(&shell->input_panel.surfaces,
3850 &input_panel_surface->link);
3851
3852 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003853}
3854
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003855static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003856 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003857 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003858};
3859
3860static void
3861destroy_input_panel_surface_resource(struct wl_resource *resource)
3862{
3863 struct input_panel_surface *ipsurf = resource->data;
3864
3865 destroy_input_panel_surface(ipsurf);
3866}
3867
3868static void
3869input_panel_get_input_panel_surface(struct wl_client *client,
3870 struct wl_resource *resource,
3871 uint32_t id,
3872 struct wl_resource *surface_resource)
3873{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003874 struct weston_surface *surface =
3875 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003876 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003877 struct input_panel_surface *ipsurf;
3878
3879 if (get_input_panel_surface(surface)) {
3880 wl_resource_post_error(surface_resource,
3881 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003882 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003883 return;
3884 }
3885
3886 ipsurf = create_input_panel_surface(shell, surface);
3887 if (!ipsurf) {
3888 wl_resource_post_error(surface_resource,
3889 WL_DISPLAY_ERROR_INVALID_OBJECT,
3890 "surface->configure already set");
3891 return;
3892 }
3893
3894 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3895 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003896 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003897 ipsurf->resource.object.implementation =
3898 (void (**)(void)) &input_panel_surface_implementation;
3899 ipsurf->resource.data = ipsurf;
3900
3901 wl_client_add_resource(client, &ipsurf->resource);
3902}
3903
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003904static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003905 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003906};
3907
3908static void
3909unbind_input_panel(struct wl_resource *resource)
3910{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003911 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003912
3913 shell->input_panel.binding = NULL;
3914 free(resource);
3915}
3916
3917static void
3918bind_input_panel(struct wl_client *client,
3919 void *data, uint32_t version, uint32_t id)
3920{
3921 struct desktop_shell *shell = data;
3922 struct wl_resource *resource;
3923
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003924 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003925 &input_panel_implementation,
3926 id, shell);
3927
3928 if (shell->input_panel.binding == NULL) {
3929 resource->destroy = unbind_input_panel;
3930 shell->input_panel.binding = resource;
3931 return;
3932 }
3933
3934 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3935 "interface object already bound");
3936 wl_resource_destroy(resource);
3937}
3938
Kristian Høgsberg07045392012-02-19 18:52:44 -05003939struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003940 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003941 struct weston_surface *current;
3942 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003943 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003944};
3945
3946static void
3947switcher_next(struct switcher *switcher)
3948{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003949 struct weston_surface *surface;
3950 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003951 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003952 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003953
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003954 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003955 switch (get_shell_surface_type(surface)) {
3956 case SHELL_SURFACE_TOPLEVEL:
3957 case SHELL_SURFACE_FULLSCREEN:
3958 case SHELL_SURFACE_MAXIMIZED:
3959 if (first == NULL)
3960 first = surface;
3961 if (prev == switcher->current)
3962 next = surface;
3963 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003964 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003965 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003966 weston_surface_damage(surface);
3967 break;
3968 default:
3969 break;
3970 }
Alex Wu1659daa2012-04-01 20:13:09 +08003971
3972 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003973 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003974 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003975 weston_surface_damage(surface);
3976 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003977 }
3978
3979 if (next == NULL)
3980 next = first;
3981
Alex Wu07b26062012-03-12 16:06:01 +08003982 if (next == NULL)
3983 return;
3984
Kristian Høgsberg07045392012-02-19 18:52:44 -05003985 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003986 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003987
3988 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003989 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003990
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003991 shsurf = get_shell_surface(switcher->current);
3992 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003993 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003994}
3995
3996static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003997switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003998{
3999 struct switcher *switcher =
4000 container_of(listener, struct switcher, listener);
4001
4002 switcher_next(switcher);
4003}
4004
4005static void
Daniel Stone351eb612012-05-31 15:27:47 -04004006switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004007{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004008 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004009 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004010 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004011
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004012 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004013 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004014 weston_surface_damage(surface);
4015 }
4016
Alex Wu07b26062012-03-12 16:06:01 +08004017 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004018 activate(switcher->shell, switcher->current,
4019 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004020 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004021 weston_keyboard_end_grab(keyboard);
4022 if (keyboard->input_method_resource)
4023 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004024 free(switcher);
4025}
4026
4027static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004028switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004029 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004030{
4031 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004032 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004033
Daniel Stonec9785ea2012-05-30 16:31:52 +01004034 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004035 switcher_next(switcher);
4036}
4037
4038static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004039switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004040 uint32_t mods_depressed, uint32_t mods_latched,
4041 uint32_t mods_locked, uint32_t group)
4042{
4043 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004044 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004045
Daniel Stone351eb612012-05-31 15:27:47 -04004046 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4047 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004048}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004049
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004050static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004051 switcher_key,
4052 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004053};
4054
4055static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004056switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004057 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004058{
Tiago Vignattibe143262012-04-16 17:31:41 +03004059 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004060 struct switcher *switcher;
4061
4062 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004063 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004064 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004065 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004066 wl_list_init(&switcher->listener.link);
4067
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004068 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004069 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004070 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004071 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4072 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004073 switcher_next(switcher);
4074}
4075
Pekka Paalanen3c647232011-12-22 13:43:43 +02004076static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004077backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004078 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004079{
4080 struct weston_compositor *compositor = data;
4081 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004082 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004083
4084 /* TODO: we're limiting to simple use cases, where we assume just
4085 * control on the primary display. We'd have to extend later if we
4086 * ever get support for setting backlights on random desktop LCD
4087 * panels though */
4088 output = get_default_output(compositor);
4089 if (!output)
4090 return;
4091
4092 if (!output->set_backlight)
4093 return;
4094
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004095 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4096 backlight_new = output->backlight_current - 25;
4097 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4098 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004099
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004100 if (backlight_new < 5)
4101 backlight_new = 5;
4102 if (backlight_new > 255)
4103 backlight_new = 255;
4104
4105 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004106 output->set_backlight(output, output->backlight_current);
4107}
4108
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004109struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004110 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004111 struct weston_seat *seat;
4112 uint32_t key[2];
4113 int key_released[2];
4114};
4115
4116static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004117debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004118 uint32_t key, uint32_t state)
4119{
4120 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4121 struct wl_resource *resource;
4122 struct wl_display *display;
4123 uint32_t serial;
4124 int send = 0, terminate = 0;
4125 int check_binding = 1;
4126 int i;
4127
4128 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4129 /* Do not run bindings on key releases */
4130 check_binding = 0;
4131
4132 for (i = 0; i < 2; i++)
4133 if (key == db->key[i])
4134 db->key_released[i] = 1;
4135
4136 if (db->key_released[0] && db->key_released[1]) {
4137 /* All key releases been swalled so end the grab */
4138 terminate = 1;
4139 } else if (key != db->key[0] && key != db->key[1]) {
4140 /* Should not swallow release of other keys */
4141 send = 1;
4142 }
4143 } else if (key == db->key[0] && !db->key_released[0]) {
4144 /* Do not check bindings for the first press of the binding
4145 * key. This allows it to be used as a debug shortcut.
4146 * We still need to swallow this event. */
4147 check_binding = 0;
4148 } else if (db->key[1]) {
4149 /* If we already ran a binding don't process another one since
4150 * we can't keep track of all the binding keys that were
4151 * pressed in order to swallow the release events. */
4152 send = 1;
4153 check_binding = 0;
4154 }
4155
4156 if (check_binding) {
4157 struct weston_compositor *ec = db->seat->compositor;
4158
4159 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4160 key, state)) {
4161 /* We ran a binding so swallow the press and keep the
4162 * grab to swallow the released too. */
4163 send = 0;
4164 terminate = 0;
4165 db->key[1] = key;
4166 } else {
4167 /* Terminate the grab since the key pressed is not a
4168 * debug binding key. */
4169 send = 1;
4170 terminate = 1;
4171 }
4172 }
4173
4174 if (send) {
4175 resource = grab->keyboard->focus_resource;
4176
4177 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004178 display = wl_client_get_display(wl_resource_get_client(resource));
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004179 serial = wl_display_next_serial(display);
4180 wl_keyboard_send_key(resource, serial, time, key, state);
4181 }
4182 }
4183
4184 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004185 weston_keyboard_end_grab(grab->keyboard);
4186 if (grab->keyboard->input_method_resource)
4187 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004188 free(db);
4189 }
4190}
4191
4192static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004193debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004194 uint32_t mods_depressed, uint32_t mods_latched,
4195 uint32_t mods_locked, uint32_t group)
4196{
4197 struct wl_resource *resource;
4198
4199 resource = grab->keyboard->focus_resource;
4200 if (!resource)
4201 return;
4202
4203 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4204 mods_latched, mods_locked, group);
4205}
4206
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004207struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004208 debug_binding_key,
4209 debug_binding_modifiers
4210};
4211
4212static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004213debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004214{
4215 struct debug_binding_grab *grab;
4216
4217 grab = calloc(1, sizeof *grab);
4218 if (!grab)
4219 return;
4220
4221 grab->seat = (struct weston_seat *) seat;
4222 grab->key[0] = key;
4223 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004224 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004225}
4226
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004227static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004228force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004229 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004230{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004231 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004232 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004233 struct desktop_shell *shell = data;
4234 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004235 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004236
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004237 focus_surface = seat->keyboard->focus;
4238 if (!focus_surface)
4239 return;
4240
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004241 wl_signal_emit(&compositor->kill_signal, focus_surface);
4242
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004243 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004244 wl_client_get_credentials(client, &pid, NULL, NULL);
4245
4246 /* Skip clients that we launched ourselves (the credentials of
4247 * the socketpair is ours) */
4248 if (pid == getpid())
4249 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004250
Daniel Stone325fc2d2012-05-30 16:31:58 +01004251 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004252}
4253
4254static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004255workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004256 uint32_t key, void *data)
4257{
4258 struct desktop_shell *shell = data;
4259 unsigned int new_index = shell->workspaces.current;
4260
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004261 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004262 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004263 if (new_index != 0)
4264 new_index--;
4265
4266 change_workspace(shell, new_index);
4267}
4268
4269static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004270workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004271 uint32_t key, void *data)
4272{
4273 struct desktop_shell *shell = data;
4274 unsigned int new_index = shell->workspaces.current;
4275
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004276 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004277 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004278 if (new_index < shell->workspaces.num - 1)
4279 new_index++;
4280
4281 change_workspace(shell, new_index);
4282}
4283
4284static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004285workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004286 uint32_t key, void *data)
4287{
4288 struct desktop_shell *shell = data;
4289 unsigned int new_index;
4290
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004291 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004292 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004293 new_index = key - KEY_F1;
4294 if (new_index >= shell->workspaces.num)
4295 new_index = shell->workspaces.num - 1;
4296
4297 change_workspace(shell, new_index);
4298}
4299
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004300static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004301workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004302 uint32_t key, void *data)
4303{
4304 struct desktop_shell *shell = data;
4305 unsigned int new_index = shell->workspaces.current;
4306
4307 if (shell->locked)
4308 return;
4309
4310 if (new_index != 0)
4311 new_index--;
4312
4313 take_surface_to_workspace_by_seat(shell, seat, new_index);
4314}
4315
4316static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004317workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004318 uint32_t key, void *data)
4319{
4320 struct desktop_shell *shell = data;
4321 unsigned int new_index = shell->workspaces.current;
4322
4323 if (shell->locked)
4324 return;
4325
4326 if (new_index < shell->workspaces.num - 1)
4327 new_index++;
4328
4329 take_surface_to_workspace_by_seat(shell, seat, new_index);
4330}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004331
4332static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004333shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004334{
Tiago Vignattibe143262012-04-16 17:31:41 +03004335 struct desktop_shell *shell =
4336 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004337 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004338
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004339 if (shell->child.client)
4340 wl_client_destroy(shell->child.client);
4341
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004342 wl_list_remove(&shell->idle_listener.link);
4343 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004344 wl_list_remove(&shell->show_input_panel_listener.link);
4345 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004346
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004347 wl_array_for_each(ws, &shell->workspaces.array)
4348 workspace_destroy(*ws);
4349 wl_array_release(&shell->workspaces.array);
4350
Pekka Paalanen3c647232011-12-22 13:43:43 +02004351 free(shell->screensaver.path);
4352 free(shell);
4353}
4354
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004355static void
4356shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4357{
4358 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004359 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004360
4361 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004362 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4363 MODIFIER_CTRL | MODIFIER_ALT,
4364 terminate_binding, ec);
4365 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4366 click_to_activate_binding,
4367 shell);
4368 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4369 MODIFIER_SUPER | MODIFIER_ALT,
4370 surface_opacity_binding, NULL);
4371 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4372 MODIFIER_SUPER, zoom_axis_binding,
4373 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004374
4375 /* configurable bindings */
4376 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004377 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4378 zoom_key_binding, NULL);
4379 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4380 zoom_key_binding, NULL);
4381 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4382 shell);
4383 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4384 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004385
4386 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4387 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4388 rotate_binding, NULL);
4389
Daniel Stone325fc2d2012-05-30 16:31:58 +01004390 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4391 shell);
4392 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4393 ec);
4394 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4395 backlight_binding, ec);
4396 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4397 ec);
4398 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4399 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004400 weston_compositor_add_key_binding(ec, KEY_K, mod,
4401 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004402 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4403 workspace_up_binding, shell);
4404 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4405 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004406 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4407 workspace_move_surface_up_binding,
4408 shell);
4409 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4410 workspace_move_surface_down_binding,
4411 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004412
4413 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4414 if (shell->workspaces.num > 1) {
4415 num_workspace_bindings = shell->workspaces.num;
4416 if (num_workspace_bindings > 6)
4417 num_workspace_bindings = 6;
4418 for (i = 0; i < num_workspace_bindings; i++)
4419 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4420 workspace_f_binding,
4421 shell);
4422 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004423
4424 /* Debug bindings */
4425 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4426 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004427}
4428
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004429WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004430module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004431 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004432{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004433 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004434 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004435 struct workspace **pws;
4436 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004437 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004438
4439 shell = malloc(sizeof *shell);
4440 if (shell == NULL)
4441 return -1;
4442
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004443 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004444 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004445
4446 shell->destroy_listener.notify = shell_destroy;
4447 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004448 shell->idle_listener.notify = idle_handler;
4449 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4450 shell->wake_listener.notify = wake_handler;
4451 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004452 shell->show_input_panel_listener.notify = show_input_panels;
4453 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4454 shell->hide_input_panel_listener.notify = hide_input_panels;
4455 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004456 shell->update_input_panel_listener.notify = update_input_panels;
4457 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004458 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004459 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004460 ec->shell_interface.create_shell_surface = create_shell_surface;
4461 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004462 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004463 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004464 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004465 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004466 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004467
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004468 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004469
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004470 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4471 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004472 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4473 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004474 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004475
4476 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004477 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004478
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004479 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004480
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004481 for (i = 0; i < shell->workspaces.num; i++) {
4482 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4483 if (pws == NULL)
4484 return -1;
4485
4486 *pws = workspace_create();
4487 if (*pws == NULL)
4488 return -1;
4489 }
4490 activate_workspace(shell, 0);
4491
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004492 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004493 wl_list_init(&shell->workspaces.animation.link);
4494 shell->workspaces.animation.frame = animate_workspace_change_frame;
4495
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004496 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4497 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004498 return -1;
4499
Kristian Høgsberg75840622011-09-06 13:48:16 -04004500 if (wl_display_add_global(ec->wl_display,
4501 &desktop_shell_interface,
4502 shell, bind_desktop_shell) == NULL)
4503 return -1;
4504
Pekka Paalanen6e168112011-11-24 11:34:05 +02004505 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4506 shell, bind_screensaver) == NULL)
4507 return -1;
4508
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004509 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004510 shell, bind_input_panel) == NULL)
4511 return -1;
4512
Jonas Ådahle9d22502012-08-29 22:13:01 +02004513 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4514 shell, bind_workspace_manager) == NULL)
4515 return -1;
4516
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004517 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004518
4519 loop = wl_display_get_event_loop(ec->wl_display);
4520 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004521
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004522 shell->screensaver.timer =
4523 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4524
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004525 wl_list_for_each(seat, &ec->seat_list, link)
4526 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004527
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004528 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004529
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004530 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004531
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004532 return 0;
4533}