blob: e0c352724d99ce92e76db0a3cbc9374dda64c703 [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
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010039#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020041#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Juan Zhaoe10d2792012-04-25 19:09:52 +080046enum animation_type {
47 ANIMATION_NONE,
48
49 ANIMATION_ZOOM,
50 ANIMATION_FADE
51};
52
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020053enum fade_type {
54 FADE_IN,
55 FADE_OUT
56};
57
Jonas Ådahl04769742012-06-13 00:01:24 +020058struct focus_state {
59 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040060 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020061 struct weston_surface *keyboard_focus;
62 struct wl_list link;
63 struct wl_listener seat_destroy_listener;
64 struct wl_listener surface_destroy_listener;
65};
66
Jonas Ådahle3cddce2012-06-13 00:01:22 +020067struct workspace {
68 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020069
70 struct wl_list focus_list;
71 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020072};
73
Philipp Brüschweiler88013572012-08-06 13:44:42 +020074struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -050075 struct wl_resource *resource;
76 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010077
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øgsberg724c8d92013-10-16 11:38:24 -0700166 enum animation_type startup_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400167};
168
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500169enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200170 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500171 SHELL_SURFACE_TOPLEVEL,
172 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800174 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300175 SHELL_SURFACE_POPUP,
176 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200177};
178
Scott Moreauff1db4a2012-04-17 19:06:18 -0600179struct ping_timer {
180 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600181 uint32_t serial;
182};
183
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200184struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500185 struct wl_resource *resource;
186 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200187
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500188 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200189 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400190 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300191 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200192
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400193 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400194 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500195 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800196 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800197 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600198 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100199
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500200 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200201 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500202 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 } rotation;
204
205 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100206 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500207 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100208 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400209 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500210 } popup;
211
Alex Wu4539b082012-03-01 12:57:46 +0800212 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300213 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400214 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300215 } transient;
216
217 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800218 enum wl_shell_surface_fullscreen_method type;
219 struct weston_transform transform; /* matrix from x, y */
220 uint32_t framerate;
221 struct weston_surface *black_surface;
222 } fullscreen;
223
Scott Moreauff1db4a2012-04-17 19:06:18 -0600224 struct ping_timer *ping_timer;
225
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200226 struct weston_transform workspace_transform;
227
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500228 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100230 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400231
232 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200233};
234
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400236 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300237 struct shell_surface *shsurf;
238 struct wl_listener shsurf_destroy_listener;
239};
240
Rusty Lynch1084da52013-08-15 09:10:08 -0700241struct shell_touch_grab {
242 struct weston_touch_grab grab;
243 struct shell_surface *shsurf;
244 struct wl_listener shsurf_destroy_listener;
245 struct weston_touch *touch;
246};
247
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300248struct weston_move_grab {
249 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100250 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500251};
252
Rusty Lynch1084da52013-08-15 09:10:08 -0700253struct weston_touch_move_grab {
254 struct shell_touch_grab base;
255 wl_fixed_t dx, dy;
256};
257
Pekka Paalanen460099f2012-01-20 16:48:25 +0200258struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300259 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500260 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200261 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200262 float x;
263 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200264 } center;
265};
266
Giulio Camuffo5085a752013-03-25 21:42:45 +0100267struct shell_seat {
268 struct weston_seat *seat;
269 struct wl_listener seat_destroy_listener;
270
271 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400272 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100273 struct wl_list surfaces_list;
274 struct wl_client *client;
275 int32_t initial_up;
276 } popup_grab;
277};
278
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400279static void
280activate(struct desktop_shell *shell, struct weston_surface *es,
281 struct weston_seat *seat);
282
283static struct workspace *
284get_current_workspace(struct desktop_shell *shell);
285
Alex Wubd3354b2012-04-17 17:20:49 +0800286static struct shell_surface *
287get_shell_surface(struct weston_surface *surface);
288
289static struct desktop_shell *
290shell_surface_get_shell(struct shell_surface *shsurf);
291
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500292static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400293surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500294
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300295static void
296shell_fade_startup(struct desktop_shell *shell);
297
Alex Wubd3354b2012-04-17 17:20:49 +0800298static bool
299shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
300{
301 struct desktop_shell *shell;
302 struct weston_surface *top_fs_es;
303
304 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100305
Alex Wubd3354b2012-04-17 17:20:49 +0800306 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
307 return false;
308
309 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100310 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800311 layer_link);
312 return (shsurf == get_shell_surface(top_fs_es));
313}
314
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500315static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400316destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300317{
318 struct shell_grab *grab;
319
320 grab = container_of(listener, struct shell_grab,
321 shsurf_destroy_listener);
322
323 grab->shsurf = NULL;
324}
325
326static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400327popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400328
329static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300330shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400331 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300332 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400333 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300334 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300335{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300336 struct desktop_shell *shell = shsurf->shell;
337
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400338 popup_grab_end(pointer);
339
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300340 grab->grab.interface = interface;
341 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400342 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500343 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400344 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400346 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400347 if (shell->child.desktop_shell) {
348 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
349 cursor);
350 weston_pointer_set_focus(pointer, shell->grab_surface,
351 wl_fixed_from_int(0),
352 wl_fixed_from_int(0));
353 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300354}
355
356static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300357shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300358{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400359 if (grab->shsurf)
360 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300361
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700362 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300363}
364
365static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700366shell_touch_grab_start(struct shell_touch_grab *grab,
367 const struct weston_touch_grab_interface *interface,
368 struct shell_surface *shsurf,
369 struct weston_touch *touch)
370{
371 struct desktop_shell *shell = shsurf->shell;
372
373 grab->grab.interface = interface;
374 grab->shsurf = shsurf;
375 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
376 wl_signal_add(&shsurf->destroy_signal,
377 &grab->shsurf_destroy_listener);
378
379 grab->touch = touch;
380
381 weston_touch_start_grab(touch, &grab->grab);
382 if (shell->child.desktop_shell)
383 weston_touch_set_focus(touch->seat, shell->grab_surface);
384}
385
386static void
387shell_touch_grab_end(struct shell_touch_grab *grab)
388{
389 if (grab->shsurf)
390 wl_list_remove(&grab->shsurf_destroy_listener.link);
391
392 weston_touch_end_grab(grab->touch);
393}
394
395static void
Alex Wu4539b082012-03-01 12:57:46 +0800396center_on_output(struct weston_surface *surface,
397 struct weston_output *output);
398
Daniel Stone496ca172012-05-30 16:31:42 +0100399static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300400get_modifier(char *modifier)
401{
402 if (!modifier)
403 return MODIFIER_SUPER;
404
405 if (!strcmp("ctrl", modifier))
406 return MODIFIER_CTRL;
407 else if (!strcmp("alt", modifier))
408 return MODIFIER_ALT;
409 else if (!strcmp("super", modifier))
410 return MODIFIER_SUPER;
411 else
412 return MODIFIER_SUPER;
413}
414
Juan Zhaoe10d2792012-04-25 19:09:52 +0800415static enum animation_type
416get_animation_type(char *animation)
417{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800418 if (!strcmp("zoom", animation))
419 return ANIMATION_ZOOM;
420 else if (!strcmp("fade", animation))
421 return ANIMATION_FADE;
422 else
423 return ANIMATION_NONE;
424}
425
Alex Wu4539b082012-03-01 12:57:46 +0800426static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400427shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200428{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400429 struct weston_config_section *section;
430 int duration;
431 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200432
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400433 section = weston_config_get_section(shell->compositor->config,
434 "screensaver", NULL, NULL);
435 weston_config_section_get_string(section,
436 "path", &shell->screensaver.path, NULL);
437 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200438 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400439
440 section = weston_config_get_section(shell->compositor->config,
441 "shell", NULL, NULL);
442 weston_config_section_get_string(section,
443 "binding-modifier", &s, "super");
444 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200445 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400446 weston_config_section_get_string(section, "animation", &s, "none");
447 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200448 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700449 weston_config_section_get_string(section,
450 "startup-animation", &s, "fade");
451 shell->startup_animation_type = get_animation_type(s);
452 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400453 weston_config_section_get_uint(section, "num-workspaces",
454 &shell->workspaces.num,
455 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200456}
457
458static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200459focus_state_destroy(struct focus_state *state)
460{
461 wl_list_remove(&state->seat_destroy_listener.link);
462 wl_list_remove(&state->surface_destroy_listener.link);
463 free(state);
464}
465
466static void
467focus_state_seat_destroy(struct wl_listener *listener, void *data)
468{
469 struct focus_state *state = container_of(listener,
470 struct focus_state,
471 seat_destroy_listener);
472
473 wl_list_remove(&state->link);
474 focus_state_destroy(state);
475}
476
477static void
478focus_state_surface_destroy(struct wl_listener *listener, void *data)
479{
480 struct focus_state *state = container_of(listener,
481 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400482 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400483 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300484 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400485 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200486
Pekka Paalanen01388e22013-04-25 13:57:44 +0300487 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
488
Kristian Høgsberge3778222012-07-31 17:29:30 -0400489 next = NULL;
490 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300491 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400492 continue;
493
494 next = surface;
495 break;
496 }
497
Pekka Paalanen01388e22013-04-25 13:57:44 +0300498 /* if the focus was a sub-surface, activate its main surface */
499 if (main_surface != state->keyboard_focus)
500 next = main_surface;
501
Kristian Høgsberge3778222012-07-31 17:29:30 -0400502 if (next) {
503 shell = state->seat->compositor->shell_interface.shell;
504 activate(shell, next, state->seat);
505 } else {
506 wl_list_remove(&state->link);
507 focus_state_destroy(state);
508 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200509}
510
511static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400512focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200513{
Jonas Ådahl04769742012-06-13 00:01:24 +0200514 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200515
516 state = malloc(sizeof *state);
517 if (state == NULL)
518 return NULL;
519
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400520 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200521 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400522 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200523
524 state->seat_destroy_listener.notify = focus_state_seat_destroy;
525 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400526 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200527 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400528 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200529
530 return state;
531}
532
Jonas Ådahl8538b222012-08-29 22:13:03 +0200533static struct focus_state *
534ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
535{
536 struct workspace *ws = get_current_workspace(shell);
537 struct focus_state *state;
538
539 wl_list_for_each(state, &ws->focus_list, link)
540 if (state->seat == seat)
541 break;
542
543 if (&state->link == &ws->focus_list)
544 state = focus_state_create(seat, ws);
545
546 return state;
547}
548
Jonas Ådahl04769742012-06-13 00:01:24 +0200549static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400550restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200551{
552 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400553 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200554
555 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400556 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200557
Kristian Høgsberge3148752013-05-06 23:19:49 -0400558 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200559 }
560}
561
562static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200563replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
564 struct weston_seat *seat)
565{
566 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400567 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200568
569 wl_list_for_each(state, &ws->focus_list, link) {
570 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400571 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500572 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200573 return;
574 }
575 }
576}
577
578static void
579drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
580 struct weston_surface *surface)
581{
582 struct focus_state *state;
583
584 wl_list_for_each(state, &ws->focus_list, link)
585 if (state->keyboard_focus == surface)
586 state->keyboard_focus = NULL;
587}
588
589static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200590workspace_destroy(struct workspace *ws)
591{
Jonas Ådahl04769742012-06-13 00:01:24 +0200592 struct focus_state *state, *next;
593
594 wl_list_for_each_safe(state, next, &ws->focus_list, link)
595 focus_state_destroy(state);
596
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200597 free(ws);
598}
599
Jonas Ådahl04769742012-06-13 00:01:24 +0200600static void
601seat_destroyed(struct wl_listener *listener, void *data)
602{
603 struct weston_seat *seat = data;
604 struct focus_state *state, *next;
605 struct workspace *ws = container_of(listener,
606 struct workspace,
607 seat_destroyed_listener);
608
609 wl_list_for_each_safe(state, next, &ws->focus_list, link)
610 if (state->seat == seat)
611 wl_list_remove(&state->link);
612}
613
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200614static struct workspace *
615workspace_create(void)
616{
617 struct workspace *ws = malloc(sizeof *ws);
618 if (ws == NULL)
619 return NULL;
620
621 weston_layer_init(&ws->layer, NULL);
622
Jonas Ådahl04769742012-06-13 00:01:24 +0200623 wl_list_init(&ws->focus_list);
624 wl_list_init(&ws->seat_destroyed_listener.link);
625 ws->seat_destroyed_listener.notify = seat_destroyed;
626
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200627 return ws;
628}
629
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200630static int
631workspace_is_empty(struct workspace *ws)
632{
633 return wl_list_empty(&ws->layer.surface_list);
634}
635
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200636static struct workspace *
637get_workspace(struct desktop_shell *shell, unsigned int index)
638{
639 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200640 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200641 pws += index;
642 return *pws;
643}
644
645static struct workspace *
646get_current_workspace(struct desktop_shell *shell)
647{
648 return get_workspace(shell, shell->workspaces.current);
649}
650
651static void
652activate_workspace(struct desktop_shell *shell, unsigned int index)
653{
654 struct workspace *ws;
655
656 ws = get_workspace(shell, index);
657 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
658
659 shell->workspaces.current = index;
660}
661
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200662static unsigned int
663get_output_height(struct weston_output *output)
664{
665 return abs(output->region.extents.y1 - output->region.extents.y2);
666}
667
668static void
669surface_translate(struct weston_surface *surface, double d)
670{
671 struct shell_surface *shsurf = get_shell_surface(surface);
672 struct weston_transform *transform;
673
674 transform = &shsurf->workspace_transform;
675 if (wl_list_empty(&transform->link))
676 wl_list_insert(surface->geometry.transformation_list.prev,
677 &shsurf->workspace_transform.link);
678
679 weston_matrix_init(&shsurf->workspace_transform.matrix);
680 weston_matrix_translate(&shsurf->workspace_transform.matrix,
681 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200682 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200683}
684
685static void
686workspace_translate_out(struct workspace *ws, double fraction)
687{
688 struct weston_surface *surface;
689 unsigned int height;
690 double d;
691
692 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
693 height = get_output_height(surface->output);
694 d = height * fraction;
695
696 surface_translate(surface, d);
697 }
698}
699
700static void
701workspace_translate_in(struct workspace *ws, double fraction)
702{
703 struct weston_surface *surface;
704 unsigned int height;
705 double d;
706
707 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
708 height = get_output_height(surface->output);
709
710 if (fraction > 0)
711 d = -(height - height * fraction);
712 else
713 d = height + height * fraction;
714
715 surface_translate(surface, d);
716 }
717}
718
719static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200720broadcast_current_workspace_state(struct desktop_shell *shell)
721{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700722 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200723
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700724 wl_resource_for_each(resource, &shell->workspaces.client_list)
725 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200726 shell->workspaces.current,
727 shell->workspaces.num);
728}
729
730static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200731reverse_workspace_change_animation(struct desktop_shell *shell,
732 unsigned int index,
733 struct workspace *from,
734 struct workspace *to)
735{
736 shell->workspaces.current = index;
737
738 shell->workspaces.anim_to = to;
739 shell->workspaces.anim_from = from;
740 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
741 shell->workspaces.anim_timestamp = 0;
742
Scott Moreau4272e632012-08-13 09:58:41 -0600743 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200744}
745
746static void
747workspace_deactivate_transforms(struct workspace *ws)
748{
749 struct weston_surface *surface;
750 struct shell_surface *shsurf;
751
752 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
753 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200754 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
755 wl_list_remove(&shsurf->workspace_transform.link);
756 wl_list_init(&shsurf->workspace_transform.link);
757 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200758 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200759 }
760}
761
762static void
763finish_workspace_change_animation(struct desktop_shell *shell,
764 struct workspace *from,
765 struct workspace *to)
766{
Scott Moreau4272e632012-08-13 09:58:41 -0600767 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200768
769 wl_list_remove(&shell->workspaces.animation.link);
770 workspace_deactivate_transforms(from);
771 workspace_deactivate_transforms(to);
772 shell->workspaces.anim_to = NULL;
773
774 wl_list_remove(&shell->workspaces.anim_from->layer.link);
775}
776
777static void
778animate_workspace_change_frame(struct weston_animation *animation,
779 struct weston_output *output, uint32_t msecs)
780{
781 struct desktop_shell *shell =
782 container_of(animation, struct desktop_shell,
783 workspaces.animation);
784 struct workspace *from = shell->workspaces.anim_from;
785 struct workspace *to = shell->workspaces.anim_to;
786 uint32_t t;
787 double x, y;
788
789 if (workspace_is_empty(from) && workspace_is_empty(to)) {
790 finish_workspace_change_animation(shell, from, to);
791 return;
792 }
793
794 if (shell->workspaces.anim_timestamp == 0) {
795 if (shell->workspaces.anim_current == 0.0)
796 shell->workspaces.anim_timestamp = msecs;
797 else
798 shell->workspaces.anim_timestamp =
799 msecs -
800 /* Invers of movement function 'y' below. */
801 (asin(1.0 - shell->workspaces.anim_current) *
802 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
803 M_2_PI);
804 }
805
806 t = msecs - shell->workspaces.anim_timestamp;
807
808 /*
809 * x = [0, π/2]
810 * y(x) = sin(x)
811 */
812 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
813 y = sin(x);
814
815 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600816 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200817
818 workspace_translate_out(from, shell->workspaces.anim_dir * y);
819 workspace_translate_in(to, shell->workspaces.anim_dir * y);
820 shell->workspaces.anim_current = y;
821
Scott Moreau4272e632012-08-13 09:58:41 -0600822 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200823 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200824 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200825 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200826}
827
828static void
829animate_workspace_change(struct desktop_shell *shell,
830 unsigned int index,
831 struct workspace *from,
832 struct workspace *to)
833{
834 struct weston_output *output;
835
836 int dir;
837
838 if (index > shell->workspaces.current)
839 dir = -1;
840 else
841 dir = 1;
842
843 shell->workspaces.current = index;
844
845 shell->workspaces.anim_dir = dir;
846 shell->workspaces.anim_from = from;
847 shell->workspaces.anim_to = to;
848 shell->workspaces.anim_current = 0.0;
849 shell->workspaces.anim_timestamp = 0;
850
851 output = container_of(shell->compositor->output_list.next,
852 struct weston_output, link);
853 wl_list_insert(&output->animation_list,
854 &shell->workspaces.animation.link);
855
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200856 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857
858 workspace_translate_in(to, 0);
859
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400860 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200861
Scott Moreau4272e632012-08-13 09:58:41 -0600862 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200863}
864
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200865static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200866update_workspace(struct desktop_shell *shell, unsigned int index,
867 struct workspace *from, struct workspace *to)
868{
869 shell->workspaces.current = index;
870 wl_list_insert(&from->layer.link, &to->layer.link);
871 wl_list_remove(&from->layer.link);
872}
873
874static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200875change_workspace(struct desktop_shell *shell, unsigned int index)
876{
877 struct workspace *from;
878 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200879
880 if (index == shell->workspaces.current)
881 return;
882
883 /* Don't change workspace when there is any fullscreen surfaces. */
884 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
885 return;
886
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200887 from = get_current_workspace(shell);
888 to = get_workspace(shell, index);
889
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200890 if (shell->workspaces.anim_from == to &&
891 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200892 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200893 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200894 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200895 return;
896 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200897
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200898 if (shell->workspaces.anim_to != NULL)
899 finish_workspace_change_animation(shell,
900 shell->workspaces.anim_from,
901 shell->workspaces.anim_to);
902
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200903 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200904
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200905 if (workspace_is_empty(to) && workspace_is_empty(from))
906 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200907 else
908 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200909
910 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200911}
912
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200913static bool
914workspace_has_only(struct workspace *ws, struct weston_surface *surface)
915{
916 struct wl_list *list = &ws->layer.surface_list;
917 struct wl_list *e;
918
919 if (wl_list_empty(list))
920 return false;
921
922 e = list->next;
923
924 if (e->next != list)
925 return false;
926
927 return container_of(e, struct weston_surface, layer_link) == surface;
928}
929
930static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200931move_surface_to_workspace(struct desktop_shell *shell,
932 struct weston_surface *surface,
933 uint32_t workspace)
934{
935 struct workspace *from;
936 struct workspace *to;
937 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300938 struct weston_surface *focus;
939
940 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200941
942 if (workspace == shell->workspaces.current)
943 return;
944
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200945 if (workspace >= shell->workspaces.num)
946 workspace = shell->workspaces.num - 1;
947
Jonas Ådahle9d22502012-08-29 22:13:01 +0200948 from = get_current_workspace(shell);
949 to = get_workspace(shell, workspace);
950
951 wl_list_remove(&surface->layer_link);
952 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
953
954 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300955 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
956 if (!seat->keyboard)
957 continue;
958
959 focus = weston_surface_get_main_surface(seat->keyboard->focus);
960 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400961 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300962 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200963
964 weston_surface_damage_below(surface);
965}
966
967static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200968take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400969 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200970 unsigned int index)
971{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300972 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200973 struct shell_surface *shsurf;
974 struct workspace *from;
975 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200976 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200977
Pekka Paalanen01388e22013-04-25 13:57:44 +0300978 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200979 if (surface == NULL ||
980 index == shell->workspaces.current)
981 return;
982
983 from = get_current_workspace(shell);
984 to = get_workspace(shell, index);
985
986 wl_list_remove(&surface->layer_link);
987 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
988
Jonas Ådahle9d22502012-08-29 22:13:01 +0200989 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200990 drop_focus_state(shell, from, surface);
991
992 if (shell->workspaces.anim_from == to &&
993 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200994 wl_list_remove(&to->layer.link);
995 wl_list_insert(from->layer.link.prev, &to->layer.link);
996
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200997 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200998 broadcast_current_workspace_state(shell);
999
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001000 return;
1001 }
1002
1003 if (shell->workspaces.anim_to != NULL)
1004 finish_workspace_change_animation(shell,
1005 shell->workspaces.anim_from,
1006 shell->workspaces.anim_to);
1007
1008 if (workspace_is_empty(from) &&
1009 workspace_has_only(to, surface))
1010 update_workspace(shell, index, from, to);
1011 else {
1012 shsurf = get_shell_surface(surface);
1013 if (wl_list_empty(&shsurf->workspace_transform.link))
1014 wl_list_insert(&shell->workspaces.anim_sticky_list,
1015 &shsurf->workspace_transform.link);
1016
1017 animate_workspace_change(shell, index, from, to);
1018 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001019
1020 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001021
1022 state = ensure_focus_state(shell, seat);
1023 if (state != NULL)
1024 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001025}
1026
1027static void
1028workspace_manager_move_surface(struct wl_client *client,
1029 struct wl_resource *resource,
1030 struct wl_resource *surface_resource,
1031 uint32_t workspace)
1032{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001033 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001034 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001035 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001036 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001037
Pekka Paalanen01388e22013-04-25 13:57:44 +03001038 main_surface = weston_surface_get_main_surface(surface);
1039 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001040}
1041
1042static const struct workspace_manager_interface workspace_manager_implementation = {
1043 workspace_manager_move_surface,
1044};
1045
1046static void
1047unbind_resource(struct wl_resource *resource)
1048{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001049 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001050}
1051
1052static void
1053bind_workspace_manager(struct wl_client *client,
1054 void *data, uint32_t version, uint32_t id)
1055{
1056 struct desktop_shell *shell = data;
1057 struct wl_resource *resource;
1058
Jason Ekstranda85118c2013-06-27 20:17:02 -05001059 resource = wl_resource_create(client,
1060 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001061
1062 if (resource == NULL) {
1063 weston_log("couldn't add workspace manager object");
1064 return;
1065 }
1066
Jason Ekstranda85118c2013-06-27 20:17:02 -05001067 wl_resource_set_implementation(resource,
1068 &workspace_manager_implementation,
1069 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001070 wl_list_insert(&shell->workspaces.client_list,
1071 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001072
1073 workspace_manager_send_state(resource,
1074 shell->workspaces.current,
1075 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001076}
1077
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001078static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001079touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1080 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1081{
1082}
1083
1084static void
1085touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1086{
1087 struct shell_touch_grab *shell_grab = container_of(grab,
1088 struct shell_touch_grab,
1089 grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001090
1091 if (grab->touch->seat->num_tp == 0)
1092 shell_touch_grab_end(shell_grab);
Rusty Lynch1084da52013-08-15 09:10:08 -07001093}
1094
1095static void
1096touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1097 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1098{
1099 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1100 struct shell_surface *shsurf = move->base.shsurf;
1101 struct weston_surface *es;
1102 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1103 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1104
1105 if (!shsurf)
1106 return;
1107
1108 es = shsurf->surface;
1109
1110 weston_surface_configure(es, dx, dy,
1111 es->geometry.width, es->geometry.height);
1112
1113 weston_compositor_schedule_repaint(es->compositor);
1114}
1115
1116static const struct weston_touch_grab_interface touch_move_grab_interface = {
1117 touch_move_grab_down,
1118 touch_move_grab_up,
1119 touch_move_grab_motion,
1120};
1121
1122static int
1123surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1124{
1125 struct weston_touch_move_grab *move;
1126
1127 if (!shsurf)
1128 return -1;
1129
1130 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1131 return 0;
1132
1133 move = malloc(sizeof *move);
1134 if (!move)
1135 return -1;
1136
1137 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1138 seat->touch->grab_x;
1139 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1140 seat->touch->grab_y;
1141
1142 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1143 seat->touch);
1144
1145 return 0;
1146}
1147
1148static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001149noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001150{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001151}
1152
1153static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001154move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001155{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001156 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001157 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001158 struct shell_surface *shsurf = move->base.shsurf;
1159 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001160 int dx = wl_fixed_to_int(pointer->x + move->dx);
1161 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001162
1163 if (!shsurf)
1164 return;
1165
1166 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001167
Daniel Stone103db7f2012-05-08 17:17:55 +01001168 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001169 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001170
1171 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001172}
1173
1174static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001175move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001176 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001177{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001178 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1179 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001180 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001181 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001182
Daniel Stone4dbadb12012-05-30 16:31:51 +01001183 if (pointer->button_count == 0 &&
1184 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001185 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001186 free(grab);
1187 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001188}
1189
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001190static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001191 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001192 move_grab_motion,
1193 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001194};
1195
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001196static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001197surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001198{
1199 struct weston_move_grab *move;
1200
1201 if (!shsurf)
1202 return -1;
1203
1204 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1205 return 0;
1206
1207 move = malloc(sizeof *move);
1208 if (!move)
1209 return -1;
1210
1211 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001212 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001213 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001214 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001215
1216 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001217 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001218
1219 return 0;
1220}
1221
1222static void
1223shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1224 struct wl_resource *seat_resource, uint32_t serial)
1225{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001226 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001227 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001228 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001229
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001230 if (seat->pointer &&
1231 seat->pointer->button_count > 0 &&
1232 seat->pointer->grab_serial == serial) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001233 surface = weston_surface_get_main_surface(seat->pointer->focus);
1234 if ((surface == shsurf->surface) &&
1235 (surface_move(shsurf, seat) < 0))
1236 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001237 } else if (seat->touch &&
1238 seat->touch->grab_serial == serial) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001239 surface = weston_surface_get_main_surface(seat->touch->focus);
1240 if ((surface == shsurf->surface) &&
1241 (surface_touch_move(shsurf, seat) < 0))
1242 wl_resource_post_no_memory(resource);
1243 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001244}
1245
1246struct weston_resize_grab {
1247 struct shell_grab base;
1248 uint32_t edges;
1249 int32_t width, height;
1250};
1251
1252static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001253resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001254{
1255 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001256 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001257 struct shell_surface *shsurf = resize->base.shsurf;
1258 int32_t width, height;
1259 wl_fixed_t from_x, from_y;
1260 wl_fixed_t to_x, to_y;
1261
1262 if (!shsurf)
1263 return;
1264
1265 weston_surface_from_global_fixed(shsurf->surface,
1266 pointer->grab_x, pointer->grab_y,
1267 &from_x, &from_y);
1268 weston_surface_from_global_fixed(shsurf->surface,
1269 pointer->x, pointer->y, &to_x, &to_y);
1270
1271 width = resize->width;
1272 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1273 width += wl_fixed_to_int(from_x - to_x);
1274 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1275 width += wl_fixed_to_int(to_x - from_x);
1276 }
1277
1278 height = resize->height;
1279 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1280 height += wl_fixed_to_int(from_y - to_y);
1281 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1282 height += wl_fixed_to_int(to_y - from_y);
1283 }
1284
1285 shsurf->client->send_configure(shsurf->surface,
1286 resize->edges, width, height);
1287}
1288
1289static void
1290send_configure(struct weston_surface *surface,
1291 uint32_t edges, int32_t width, int32_t height)
1292{
1293 struct shell_surface *shsurf = get_shell_surface(surface);
1294
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001295 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001296 edges, width, height);
1297}
1298
1299static const struct weston_shell_client shell_client = {
1300 send_configure
1301};
1302
1303static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001304resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001305 uint32_t time, uint32_t button, uint32_t state_w)
1306{
1307 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001308 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001309 enum wl_pointer_button_state state = state_w;
1310
1311 if (pointer->button_count == 0 &&
1312 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1313 shell_grab_end(&resize->base);
1314 free(grab);
1315 }
1316}
1317
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001318static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001319 noop_grab_focus,
1320 resize_grab_motion,
1321 resize_grab_button,
1322};
1323
Giulio Camuffob8366642013-04-25 13:57:46 +03001324/*
1325 * Returns the bounding box of a surface and all its sub-surfaces,
1326 * in the surface coordinates system. */
1327static void
1328surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1329 int32_t *y, int32_t *w, int32_t *h) {
1330 pixman_region32_t region;
1331 pixman_box32_t *box;
1332 struct weston_subsurface *subsurface;
1333
1334 pixman_region32_init_rect(&region, 0, 0,
1335 surface->geometry.width,
1336 surface->geometry.height);
1337
1338 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1339 pixman_region32_union_rect(&region, &region,
1340 subsurface->position.x,
1341 subsurface->position.y,
1342 subsurface->surface->geometry.width,
1343 subsurface->surface->geometry.height);
1344 }
1345
1346 box = pixman_region32_extents(&region);
1347 if (x)
1348 *x = box->x1;
1349 if (y)
1350 *y = box->y1;
1351 if (w)
1352 *w = box->x2 - box->x1;
1353 if (h)
1354 *h = box->y2 - box->y1;
1355
1356 pixman_region32_fini(&region);
1357}
1358
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001359static int
1360surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001361 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001362{
1363 struct weston_resize_grab *resize;
1364
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001365 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1366 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001367 return 0;
1368
1369 if (edges == 0 || edges > 15 ||
1370 (edges & 3) == 3 || (edges & 12) == 12)
1371 return 0;
1372
1373 resize = malloc(sizeof *resize);
1374 if (!resize)
1375 return -1;
1376
1377 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001378 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1379 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001380
1381 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001382 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001383
1384 return 0;
1385}
1386
1387static void
1388shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1389 struct wl_resource *seat_resource, uint32_t serial,
1390 uint32_t edges)
1391{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001392 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001393 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001394 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001395
1396 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1397 return;
1398
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001399 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001400 if (seat->pointer->button_count == 0 ||
1401 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001402 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001403 return;
1404
Kristian Høgsberge3148752013-05-06 23:19:49 -04001405 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001406 wl_resource_post_no_memory(resource);
1407}
1408
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001409static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001410end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1411
1412static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001413busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001414{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001415 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001416 struct weston_pointer *pointer = base->pointer;
1417 struct weston_surface *surface;
1418 wl_fixed_t sx, sy;
1419
1420 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1421 pointer->x, pointer->y,
1422 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001423
Kristian Høgsberg67800732013-07-04 01:12:17 -04001424 if (!grab->shsurf || grab->shsurf->surface != surface)
1425 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001426}
1427
1428static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001429busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001430{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001431}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001432
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001433static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001434busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001435 uint32_t time, uint32_t button, uint32_t state)
1436{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001437 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001438 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001439 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001440
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001441 if (shsurf && button == BTN_LEFT && state) {
1442 activate(shsurf->shell, shsurf->surface, seat);
1443 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001444 } else if (shsurf && button == BTN_RIGHT && state) {
1445 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001446 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001447 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001448}
1449
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001450static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001451 busy_cursor_grab_focus,
1452 busy_cursor_grab_motion,
1453 busy_cursor_grab_button,
1454};
1455
1456static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001457set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001458{
1459 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001460
1461 grab = malloc(sizeof *grab);
1462 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001463 return;
1464
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001465 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1466 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001467}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001468
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001469static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001470end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001471{
1472 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1473
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001474 if (grab->grab.interface == &busy_cursor_grab_interface &&
1475 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001476 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001477 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001478 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001479}
1480
Scott Moreau9521d5e2012-04-19 13:06:17 -06001481static void
1482ping_timer_destroy(struct shell_surface *shsurf)
1483{
1484 if (!shsurf || !shsurf->ping_timer)
1485 return;
1486
1487 if (shsurf->ping_timer->source)
1488 wl_event_source_remove(shsurf->ping_timer->source);
1489
1490 free(shsurf->ping_timer);
1491 shsurf->ping_timer = NULL;
1492}
1493
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001494static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001495ping_timeout_handler(void *data)
1496{
1497 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001498 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001499
Scott Moreau9521d5e2012-04-19 13:06:17 -06001500 /* Client is not responding */
1501 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001502
1503 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001504 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001505 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001506
1507 return 1;
1508}
1509
1510static void
1511ping_handler(struct weston_surface *surface, uint32_t serial)
1512{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001513 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001514 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001515 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001516
1517 if (!shsurf)
1518 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001519 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001520 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001521
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001522 if (shsurf->surface == shsurf->shell->grab_surface)
1523 return;
1524
Scott Moreauff1db4a2012-04-17 19:06:18 -06001525 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001526 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001527 if (!shsurf->ping_timer)
1528 return;
1529
1530 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001531 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1532 shsurf->ping_timer->source =
1533 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1534 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1535
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001536 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001537 }
1538}
1539
1540static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001541handle_pointer_focus(struct wl_listener *listener, void *data)
1542{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001543 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001544 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001545 struct weston_compositor *compositor;
1546 struct shell_surface *shsurf;
1547 uint32_t serial;
1548
1549 if (!surface)
1550 return;
1551
1552 compositor = surface->compositor;
1553 shsurf = get_shell_surface(surface);
1554
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001555 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001556 set_busy_cursor(shsurf, pointer);
1557 } else {
1558 serial = wl_display_next_serial(compositor->wl_display);
1559 ping_handler(surface, serial);
1560 }
1561}
1562
1563static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001564create_pointer_focus_listener(struct weston_seat *seat)
1565{
1566 struct wl_listener *listener;
1567
Kristian Høgsberge3148752013-05-06 23:19:49 -04001568 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001569 return;
1570
1571 listener = malloc(sizeof *listener);
1572 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001573 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001574}
1575
1576static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001577shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1578 uint32_t serial)
1579{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001580 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001581 struct weston_seat *seat;
1582 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001583
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001584 if (shsurf->ping_timer == NULL)
1585 /* Just ignore unsolicited pong. */
1586 return;
1587
Scott Moreauff1db4a2012-04-17 19:06:18 -06001588 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001589 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001590 wl_list_for_each(seat, &ec->seat_list, link) {
1591 if(seat->pointer)
1592 end_busy_cursor(shsurf, seat->pointer);
1593 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001594 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001595 }
1596}
1597
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001598static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001599set_title(struct shell_surface *shsurf, const char *title)
1600{
1601 free(shsurf->title);
1602 shsurf->title = strdup(title);
1603}
1604
1605static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001606shell_surface_set_title(struct wl_client *client,
1607 struct wl_resource *resource, const char *title)
1608{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001609 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001610
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001611 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001612}
1613
1614static void
1615shell_surface_set_class(struct wl_client *client,
1616 struct wl_resource *resource, const char *class)
1617{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001618 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001619
1620 free(shsurf->class);
1621 shsurf->class = strdup(class);
1622}
1623
Juan Zhao96879df2012-02-07 08:45:41 +08001624static struct weston_output *
1625get_default_output(struct weston_compositor *compositor)
1626{
1627 return container_of(compositor->output_list.next,
1628 struct weston_output, link);
1629}
1630
Alex Wu4539b082012-03-01 12:57:46 +08001631static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001632restore_output_mode(struct weston_output *output)
1633{
Hardening57388e42013-09-18 23:56:36 +02001634 if (output->current_mode != output->original_mode ||
1635 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001636 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001637 output->original_mode,
1638 output->original_scale,
1639 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001640}
1641
1642static void
1643restore_all_output_modes(struct weston_compositor *compositor)
1644{
1645 struct weston_output *output;
1646
1647 wl_list_for_each(output, &compositor->output_list, link)
1648 restore_output_mode(output);
1649}
1650
1651static void
Alex Wu4539b082012-03-01 12:57:46 +08001652shell_unset_fullscreen(struct shell_surface *shsurf)
1653{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001654 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001655 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001656 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1657 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001658 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001659 }
Alex Wu4539b082012-03-01 12:57:46 +08001660 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1661 shsurf->fullscreen.framerate = 0;
1662 wl_list_remove(&shsurf->fullscreen.transform.link);
1663 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001664 if (shsurf->fullscreen.black_surface)
1665 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001666 shsurf->fullscreen.black_surface = NULL;
1667 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001668 weston_surface_set_position(shsurf->surface,
1669 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001670 if (shsurf->saved_rotation_valid) {
1671 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1672 &shsurf->rotation.transform.link);
1673 shsurf->saved_rotation_valid = false;
1674 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001675
1676 ws = get_current_workspace(shsurf->shell);
1677 wl_list_remove(&shsurf->surface->layer_link);
1678 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001679}
1680
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001681static void
1682shell_unset_maximized(struct shell_surface *shsurf)
1683{
1684 struct workspace *ws;
1685 /* undo all maximized things here */
1686 shsurf->output = get_default_output(shsurf->surface->compositor);
1687 weston_surface_set_position(shsurf->surface,
1688 shsurf->saved_x,
1689 shsurf->saved_y);
1690
1691 if (shsurf->saved_rotation_valid) {
1692 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1693 &shsurf->rotation.transform.link);
1694 shsurf->saved_rotation_valid = false;
1695 }
1696
1697 ws = get_current_workspace(shsurf->shell);
1698 wl_list_remove(&shsurf->surface->layer_link);
1699 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1700}
1701
Pekka Paalanen98262232011-12-01 10:42:22 +02001702static int
1703reset_shell_surface_type(struct shell_surface *surface)
1704{
1705 switch (surface->type) {
1706 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001707 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001708 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001709 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001710 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001711 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001712 case SHELL_SURFACE_NONE:
1713 case SHELL_SURFACE_TOPLEVEL:
1714 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001715 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001716 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001717 break;
1718 }
1719
1720 surface->type = SHELL_SURFACE_NONE;
1721 return 0;
1722}
1723
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001724static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001725set_surface_type(struct shell_surface *shsurf)
1726{
1727 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001728 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001729
1730 reset_shell_surface_type(shsurf);
1731
1732 shsurf->type = shsurf->next_type;
1733 shsurf->next_type = SHELL_SURFACE_NONE;
1734
1735 switch (shsurf->type) {
1736 case SHELL_SURFACE_TOPLEVEL:
1737 break;
1738 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001739 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001740 pes->geometry.x + shsurf->transient.x,
1741 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001742 break;
1743
1744 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001745 case SHELL_SURFACE_FULLSCREEN:
1746 shsurf->saved_x = surface->geometry.x;
1747 shsurf->saved_y = surface->geometry.y;
1748 shsurf->saved_position_valid = true;
1749
1750 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1751 wl_list_remove(&shsurf->rotation.transform.link);
1752 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001753 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001754 shsurf->saved_rotation_valid = true;
1755 }
1756 break;
1757
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001758 case SHELL_SURFACE_XWAYLAND:
1759 weston_surface_set_position(surface, shsurf->transient.x,
1760 shsurf->transient.y);
1761 break;
1762
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001763 default:
1764 break;
1765 }
1766}
1767
1768static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001769set_toplevel(struct shell_surface *shsurf)
1770{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001771 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001772}
1773
1774static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001775shell_surface_set_toplevel(struct wl_client *client,
1776 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001777{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001778 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001779
Tiago Vignattibc052c92012-04-19 16:18:18 +03001780 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001781}
1782
1783static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001784set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001785 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001786{
1787 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001788 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001789 shsurf->transient.x = x;
1790 shsurf->transient.y = y;
1791 shsurf->transient.flags = flags;
1792 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1793}
1794
1795static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001796shell_surface_set_transient(struct wl_client *client,
1797 struct wl_resource *resource,
1798 struct wl_resource *parent_resource,
1799 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001800{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001801 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001802 struct weston_surface *parent =
1803 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001804
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001805 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001806}
1807
Tiago Vignattibe143262012-04-16 17:31:41 +03001808static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001809shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001810{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001811 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001812}
1813
1814static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001815get_output_panel_height(struct desktop_shell *shell,
1816 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001817{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001818 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001819 int panel_height = 0;
1820
1821 if (!output)
1822 return 0;
1823
Juan Zhao4ab94682012-07-09 22:24:09 -07001824 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001825 if (surface->output == output) {
1826 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001827 break;
1828 }
1829 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001830
Juan Zhao96879df2012-02-07 08:45:41 +08001831 return panel_height;
1832}
1833
1834static void
1835shell_surface_set_maximized(struct wl_client *client,
1836 struct wl_resource *resource,
1837 struct wl_resource *output_resource )
1838{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001839 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001840 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001841 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001842 uint32_t edges = 0, panel_height = 0;
1843
1844 /* get the default output, if the client set it as NULL
1845 check whether the ouput is available */
1846 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001847 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001848 else if (es->output)
1849 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001850 else
1851 shsurf->output = get_default_output(es->compositor);
1852
Tiago Vignattibe143262012-04-16 17:31:41 +03001853 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001854 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001855 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001856
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001857 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001858 shsurf->output->width,
1859 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001860
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001861 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001862}
1863
Alex Wu21858432012-04-01 20:13:08 +08001864static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001865black_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 +08001866
Alex Wu4539b082012-03-01 12:57:46 +08001867static struct weston_surface *
1868create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001869 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001870 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001871{
1872 struct weston_surface *surface = NULL;
1873
1874 surface = weston_surface_create(ec);
1875 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001876 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001877 return NULL;
1878 }
1879
Alex Wu21858432012-04-01 20:13:08 +08001880 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001881 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001882 weston_surface_configure(surface, x, y, w, h);
1883 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001884 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001885 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001886 pixman_region32_fini(&surface->input);
1887 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001888
Alex Wu4539b082012-03-01 12:57:46 +08001889 return surface;
1890}
1891
1892/* Create black surface and append it to the associated fullscreen surface.
1893 * Handle size dismatch and positioning according to the method. */
1894static void
1895shell_configure_fullscreen(struct shell_surface *shsurf)
1896{
1897 struct weston_output *output = shsurf->fullscreen_output;
1898 struct weston_surface *surface = shsurf->surface;
1899 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001900 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001901 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001902
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001903 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1904 restore_output_mode(output);
1905
Alex Wu4539b082012-03-01 12:57:46 +08001906 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001907 shsurf->fullscreen.black_surface =
1908 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001909 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001910 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001911 output->width,
1912 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001913
1914 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1915 wl_list_insert(&surface->layer_link,
1916 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001917 shsurf->fullscreen.black_surface->output = output;
1918
Giulio Camuffob8366642013-04-25 13:57:46 +03001919 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1920 &surf_width, &surf_height);
1921
Alex Wu4539b082012-03-01 12:57:46 +08001922 switch (shsurf->fullscreen.type) {
1923 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001924 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001925 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001926 break;
1927 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001928 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001929 if (output->width == surf_width &&
1930 output->height == surf_height) {
1931 weston_surface_set_position(surface, output->x - surf_x,
1932 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001933 break;
1934 }
1935
Alex Wu4539b082012-03-01 12:57:46 +08001936 matrix = &shsurf->fullscreen.transform.matrix;
1937 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001938
Scott Moreau1bad5db2012-08-18 01:04:05 -06001939 output_aspect = (float) output->width /
1940 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001941 surface_aspect = (float) surface->geometry.width /
1942 (float) surface->geometry.height;
1943 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001944 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001945 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001946 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001947 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001948 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001949
Alex Wu4539b082012-03-01 12:57:46 +08001950 weston_matrix_scale(matrix, scale, scale, 1);
1951 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001952 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001953 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001954 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1955 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001956 weston_surface_set_position(surface, x, y);
1957
Alex Wu4539b082012-03-01 12:57:46 +08001958 break;
1959 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001960 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001961 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001962 surf_width * surface->buffer_scale,
1963 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001964 shsurf->fullscreen.framerate};
1965
Hardening57388e42013-09-18 23:56:36 +02001966 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
1967 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001968 weston_surface_set_position(surface,
1969 output->x - surf_x,
1970 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001971 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001972 output->x - surf_x,
1973 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001974 output->width,
1975 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001976 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001977 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001978 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001979 center_on_output(surface, output);
1980 }
Alex Wubd3354b2012-04-17 17:20:49 +08001981 }
Alex Wu4539b082012-03-01 12:57:46 +08001982 break;
1983 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001984 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001985 break;
1986 default:
1987 break;
1988 }
1989}
1990
1991/* make the fullscreen and black surface at the top */
1992static void
1993shell_stack_fullscreen(struct shell_surface *shsurf)
1994{
Alex Wubd3354b2012-04-17 17:20:49 +08001995 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001996 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001997 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001998
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001999 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002000 wl_list_insert(&shell->fullscreen_layer.surface_list,
2001 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08002002 weston_surface_damage(surface);
2003
2004 if (!shsurf->fullscreen.black_surface)
2005 shsurf->fullscreen.black_surface =
2006 create_black_surface(surface->compositor,
2007 surface,
2008 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002009 output->width,
2010 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002011
2012 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002013 wl_list_insert(&surface->layer_link,
2014 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002015 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08002016}
2017
2018static void
2019shell_map_fullscreen(struct shell_surface *shsurf)
2020{
Alex Wu4539b082012-03-01 12:57:46 +08002021 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002022 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002023}
2024
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002025static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002026set_fullscreen(struct shell_surface *shsurf,
2027 uint32_t method,
2028 uint32_t framerate,
2029 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002030{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002031 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002032
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002033 if (output)
2034 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002035 else if (es->output)
2036 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002037 else
2038 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002039
Alex Wu4539b082012-03-01 12:57:46 +08002040 shsurf->fullscreen_output = shsurf->output;
2041 shsurf->fullscreen.type = method;
2042 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002043 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002044
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002045 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002046 shsurf->output->width,
2047 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002048}
2049
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002050static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002051shell_surface_set_fullscreen(struct wl_client *client,
2052 struct wl_resource *resource,
2053 uint32_t method,
2054 uint32_t framerate,
2055 struct wl_resource *output_resource)
2056{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002057 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002058 struct weston_output *output;
2059
2060 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002061 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002062 else
2063 output = NULL;
2064
2065 set_fullscreen(shsurf, method, framerate, output);
2066}
2067
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002068static void
2069set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2070{
2071 /* XXX: using the same fields for transient type */
2072 shsurf->transient.x = x;
2073 shsurf->transient.y = y;
2074 shsurf->transient.flags = flags;
2075 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2076}
2077
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002078static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002079
2080static void
2081destroy_shell_seat(struct wl_listener *listener, void *data)
2082{
2083 struct shell_seat *shseat =
2084 container_of(listener,
2085 struct shell_seat, seat_destroy_listener);
2086 struct shell_surface *shsurf, *prev = NULL;
2087
2088 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002089 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002090 shseat->popup_grab.client = NULL;
2091
2092 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2093 shsurf->popup.shseat = NULL;
2094 if (prev) {
2095 wl_list_init(&prev->popup.grab_link);
2096 }
2097 prev = shsurf;
2098 }
2099 wl_list_init(&prev->popup.grab_link);
2100 }
2101
2102 wl_list_remove(&shseat->seat_destroy_listener.link);
2103 free(shseat);
2104}
2105
2106static struct shell_seat *
2107create_shell_seat(struct weston_seat *seat)
2108{
2109 struct shell_seat *shseat;
2110
2111 shseat = calloc(1, sizeof *shseat);
2112 if (!shseat) {
2113 weston_log("no memory to allocate shell seat\n");
2114 return NULL;
2115 }
2116
2117 shseat->seat = seat;
2118 wl_list_init(&shseat->popup_grab.surfaces_list);
2119
2120 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2121 wl_signal_add(&seat->destroy_signal,
2122 &shseat->seat_destroy_listener);
2123
2124 return shseat;
2125}
2126
2127static struct shell_seat *
2128get_shell_seat(struct weston_seat *seat)
2129{
2130 struct wl_listener *listener;
2131
2132 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2133 if (listener == NULL)
2134 return create_shell_seat(seat);
2135
2136 return container_of(listener,
2137 struct shell_seat, seat_destroy_listener);
2138}
2139
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002140static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002141popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002142{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002143 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002144 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002145 struct shell_seat *shseat =
2146 container_of(grab, struct shell_seat, popup_grab.grab);
2147 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002148 wl_fixed_t sx, sy;
2149
2150 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2151 pointer->x, pointer->y,
2152 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002153
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002154 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002155 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002156 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002157 weston_pointer_set_focus(pointer, NULL,
2158 wl_fixed_from_int(0),
2159 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002160 }
2161}
2162
2163static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002164popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002165{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002166 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002167 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002168 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002169
Neil Roberts96d790e2013-09-19 17:32:00 +01002170 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002171 weston_surface_from_global_fixed(pointer->focus,
2172 pointer->x, pointer->y,
2173 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002174 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002175 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002176}
2177
2178static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002179popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002180 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002181{
2182 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002183 struct shell_seat *shseat =
2184 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002185 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002186 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002187 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002188 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002189
Neil Roberts96d790e2013-09-19 17:32:00 +01002190 resource_list = &grab->pointer->focus_resource_list;
2191 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002192 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002193 wl_resource_for_each(resource, resource_list) {
2194 wl_pointer_send_button(resource, serial,
2195 time, button, state);
2196 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002197 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002198 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002199 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002200 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002201 }
2202
Daniel Stone4dbadb12012-05-30 16:31:51 +01002203 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002204 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002205}
2206
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002207static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002208 popup_grab_focus,
2209 popup_grab_motion,
2210 popup_grab_button,
2211};
2212
2213static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002214popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002215{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002216 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002217 struct shell_seat *shseat =
2218 container_of(grab, struct shell_seat, popup_grab.grab);
2219 struct shell_surface *shsurf;
2220 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002221
2222 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002223 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002224 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002225 shseat->popup_grab.grab.interface = NULL;
2226 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002227 /* Send the popup_done event to all the popups open */
2228 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002229 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002230 shsurf->popup.shseat = NULL;
2231 if (prev) {
2232 wl_list_init(&prev->popup.grab_link);
2233 }
2234 prev = shsurf;
2235 }
2236 wl_list_init(&prev->popup.grab_link);
2237 wl_list_init(&shseat->popup_grab.surfaces_list);
2238 }
2239}
2240
2241static void
2242add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2243{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002244 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002245
2246 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002247 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002248 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002249 /* We must make sure here that this popup was opened after
2250 * a mouse press, and not just by moving around with other
2251 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002252 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002253 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002254
Rob Bradforddfe31052013-06-26 19:49:11 +01002255 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002256 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002257 } else {
2258 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002259 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002260}
2261
2262static void
2263remove_popup_grab(struct shell_surface *shsurf)
2264{
2265 struct shell_seat *shseat = shsurf->popup.shseat;
2266
2267 wl_list_remove(&shsurf->popup.grab_link);
2268 wl_list_init(&shsurf->popup.grab_link);
2269 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002270 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002271 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002272 }
2273}
2274
2275static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002276shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002277{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002278 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002279 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002280 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002281
2282 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002283
Pekka Paalanen483243f2013-03-08 14:56:50 +02002284 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002285 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2286 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002287
Kristian Høgsberge3148752013-05-06 23:19:49 -04002288 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002289 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002290 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002291 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002292 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002293 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002294}
2295
2296static void
2297shell_surface_set_popup(struct wl_client *client,
2298 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002299 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002300 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002301 struct wl_resource *parent_resource,
2302 int32_t x, int32_t y, uint32_t flags)
2303{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002304 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002305
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002306 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002307 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002308 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002309 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002310 shsurf->popup.x = x;
2311 shsurf->popup.y = y;
2312}
2313
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002314static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002315 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002316 shell_surface_move,
2317 shell_surface_resize,
2318 shell_surface_set_toplevel,
2319 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002320 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002321 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002322 shell_surface_set_maximized,
2323 shell_surface_set_title,
2324 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002325};
2326
2327static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002328destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002329{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002330 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2331
Giulio Camuffo5085a752013-03-25 21:42:45 +01002332 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2333 remove_popup_grab(shsurf);
2334 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002335
Alex Wubd3354b2012-04-17 17:20:49 +08002336 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002337 shell_surface_is_top_fullscreen(shsurf))
2338 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002339
Alex Wuaa08e2d2012-03-05 11:01:40 +08002340 if (shsurf->fullscreen.black_surface)
2341 weston_surface_destroy(shsurf->fullscreen.black_surface);
2342
Alex Wubd3354b2012-04-17 17:20:49 +08002343 /* As destroy_resource() use wl_list_for_each_safe(),
2344 * we can always remove the listener.
2345 */
2346 wl_list_remove(&shsurf->surface_destroy_listener.link);
2347 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002348 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002349 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002350
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002351 wl_list_remove(&shsurf->link);
2352 free(shsurf);
2353}
2354
2355static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002356shell_destroy_shell_surface(struct wl_resource *resource)
2357{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002358 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002359
2360 destroy_shell_surface(shsurf);
2361}
2362
2363static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002364shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002365{
2366 struct shell_surface *shsurf = container_of(listener,
2367 struct shell_surface,
2368 surface_destroy_listener);
2369
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002370 if (shsurf->resource)
2371 wl_resource_destroy(shsurf->resource);
2372 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002373 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002374}
2375
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002376static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002377shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002378
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002379static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002380get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002381{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002382 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002383 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002384 else
2385 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002386}
2387
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002388static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002389create_shell_surface(void *shell, struct weston_surface *surface,
2390 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002391{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002392 struct shell_surface *shsurf;
2393
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002394 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002395 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002396 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002397 }
2398
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002399 shsurf = calloc(1, sizeof *shsurf);
2400 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002401 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002402 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002403 }
2404
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002405 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002406 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002407
Tiago Vignattibc052c92012-04-19 16:18:18 +03002408 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002409 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002410 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002411 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002412 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002413 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2414 shsurf->fullscreen.framerate = 0;
2415 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002416 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002417 wl_list_init(&shsurf->fullscreen.transform.link);
2418
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002419 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002420 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002421 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002422 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002423
2424 /* init link so its safe to always remove it in destroy_shell_surface */
2425 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002426 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002427
Pekka Paalanen460099f2012-01-20 16:48:25 +02002428 /* empty when not in use */
2429 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002430 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002431
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002432 wl_list_init(&shsurf->workspace_transform.link);
2433
Pekka Paalanen98262232011-12-01 10:42:22 +02002434 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002435 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002436
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002437 shsurf->client = client;
2438
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002439 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002440}
2441
2442static void
2443shell_get_shell_surface(struct wl_client *client,
2444 struct wl_resource *resource,
2445 uint32_t id,
2446 struct wl_resource *surface_resource)
2447{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002448 struct weston_surface *surface =
2449 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002450 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002451 struct shell_surface *shsurf;
2452
2453 if (get_shell_surface(surface)) {
2454 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002455 WL_DISPLAY_ERROR_INVALID_OBJECT,
2456 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002457 return;
2458 }
2459
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002460 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002461 if (!shsurf) {
2462 wl_resource_post_error(surface_resource,
2463 WL_DISPLAY_ERROR_INVALID_OBJECT,
2464 "surface->configure already set");
2465 return;
2466 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002467
Jason Ekstranda85118c2013-06-27 20:17:02 -05002468 shsurf->resource =
2469 wl_resource_create(client,
2470 &wl_shell_surface_interface, 1, id);
2471 wl_resource_set_implementation(shsurf->resource,
2472 &shell_surface_implementation,
2473 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002474}
2475
2476static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002477 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002478};
2479
Kristian Høgsberg07937562011-04-12 17:25:42 -04002480static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002481shell_fade(struct desktop_shell *shell, enum fade_type type);
2482
2483static int
2484screensaver_timeout(void *data)
2485{
2486 struct desktop_shell *shell = data;
2487
2488 shell_fade(shell, FADE_OUT);
2489
2490 return 1;
2491}
2492
2493static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002494handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002495{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002496 struct desktop_shell *shell =
2497 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002498
Pekka Paalanen18027e52011-12-02 16:31:49 +02002499 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002500
2501 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002502 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002503}
2504
2505static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002506launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002507{
2508 if (shell->screensaver.binding)
2509 return;
2510
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002511 if (!shell->screensaver.path) {
2512 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002513 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002514 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002515
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002516 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002517 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002518 return;
2519 }
2520
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002521 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002522 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002523 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002524 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002525}
2526
2527static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002528terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002529{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002530 if (shell->screensaver.process.pid == 0)
2531 return;
2532
2533 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002534}
2535
2536static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002537configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002538{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002539 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002540
Giulio Camuffo184df502013-02-21 11:29:21 +01002541 if (width == 0)
2542 return;
2543
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002544 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2545 if (s->output == es->output && s != es) {
2546 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002547 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002548 }
2549 }
2550
Giulio Camuffo184df502013-02-21 11:29:21 +01002551 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002552
2553 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002554 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002555 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002556 }
2557}
2558
2559static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002560background_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 -04002561{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002562 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002563
Giulio Camuffo184df502013-02-21 11:29:21 +01002564 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002565}
2566
2567static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002568desktop_shell_set_background(struct wl_client *client,
2569 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002570 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002571 struct wl_resource *surface_resource)
2572{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002573 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002574 struct weston_surface *surface =
2575 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002576
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002577 if (surface->configure) {
2578 wl_resource_post_error(surface_resource,
2579 WL_DISPLAY_ERROR_INVALID_OBJECT,
2580 "surface role already assigned");
2581 return;
2582 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002583
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002584 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002585 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002586 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002587 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002588 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002589 surface->output->width,
2590 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002591}
2592
2593static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002594panel_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 -04002595{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002596 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002597
Giulio Camuffo184df502013-02-21 11:29:21 +01002598 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002599}
2600
2601static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002602desktop_shell_set_panel(struct wl_client *client,
2603 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002604 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002605 struct wl_resource *surface_resource)
2606{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002607 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002608 struct weston_surface *surface =
2609 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002610
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002611 if (surface->configure) {
2612 wl_resource_post_error(surface_resource,
2613 WL_DISPLAY_ERROR_INVALID_OBJECT,
2614 "surface role already assigned");
2615 return;
2616 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002617
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002618 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002619 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002620 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002621 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002622 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002623 surface->output->width,
2624 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002625}
2626
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002627static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002628lock_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 -04002629{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002630 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002631
Giulio Camuffo184df502013-02-21 11:29:21 +01002632 if (width == 0)
2633 return;
2634
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002635 surface->geometry.width = width;
2636 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002637 center_on_output(surface, get_default_output(shell->compositor));
2638
2639 if (!weston_surface_is_mapped(surface)) {
2640 wl_list_insert(&shell->lock_layer.surface_list,
2641 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002642 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002643 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002644 }
2645}
2646
2647static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002648handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002649{
Tiago Vignattibe143262012-04-16 17:31:41 +03002650 struct desktop_shell *shell =
2651 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002652
Martin Minarik6d118362012-06-07 18:01:59 +02002653 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002654 shell->lock_surface = NULL;
2655}
2656
2657static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002658desktop_shell_set_lock_surface(struct wl_client *client,
2659 struct wl_resource *resource,
2660 struct wl_resource *surface_resource)
2661{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002662 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002663 struct weston_surface *surface =
2664 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002665
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002666 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002667
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002668 if (!shell->locked)
2669 return;
2670
Pekka Paalanen98262232011-12-01 10:42:22 +02002671 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002672
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002673 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002674 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002675 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002676
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002677 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002678 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002679}
2680
2681static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002682resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002683{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002684 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002685
Pekka Paalanen77346a62011-11-30 16:26:35 +02002686 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002687
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002688 wl_list_remove(&shell->lock_layer.link);
2689 wl_list_insert(&shell->compositor->cursor_layer.link,
2690 &shell->fullscreen_layer.link);
2691 wl_list_insert(&shell->fullscreen_layer.link,
2692 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002693 if (shell->showing_input_panels) {
2694 wl_list_insert(&shell->panel_layer.link,
2695 &shell->input_panel_layer.link);
2696 wl_list_insert(&shell->input_panel_layer.link,
2697 &ws->layer.link);
2698 } else {
2699 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2700 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002701
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002702 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002703
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002704 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002705 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002706 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002707}
2708
2709static void
2710desktop_shell_unlock(struct wl_client *client,
2711 struct wl_resource *resource)
2712{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002713 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002714
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002715 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002716
2717 if (shell->locked)
2718 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002719}
2720
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002721static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002722desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002723 struct wl_resource *resource,
2724 struct wl_resource *surface_resource)
2725{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002726 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002727
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002728 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002729}
2730
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002731static void
2732desktop_shell_desktop_ready(struct wl_client *client,
2733 struct wl_resource *resource)
2734{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002735 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002736
2737 shell_fade_startup(shell);
2738}
2739
Kristian Høgsberg75840622011-09-06 13:48:16 -04002740static const struct desktop_shell_interface desktop_shell_implementation = {
2741 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002742 desktop_shell_set_panel,
2743 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002744 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002745 desktop_shell_set_grab_surface,
2746 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002747};
2748
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002749static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002750get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002751{
2752 struct shell_surface *shsurf;
2753
2754 shsurf = get_shell_surface(surface);
2755 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002756 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002757 return shsurf->type;
2758}
2759
Kristian Høgsberg75840622011-09-06 13:48:16 -04002760static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002761move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002762{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002763 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002764 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002765 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002766 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002767
Pekka Paalanen01388e22013-04-25 13:57:44 +03002768 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002769 if (surface == NULL)
2770 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002771
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002772 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002773 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2774 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002775 return;
2776
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002777 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002778}
2779
2780static void
Neil Robertsaba0f252013-10-03 16:43:05 +01002781touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
2782{
2783 struct weston_surface *focus =
2784 (struct weston_surface *) seat->touch->focus;
2785 struct weston_surface *surface;
2786 struct shell_surface *shsurf;
2787
2788 surface = weston_surface_get_main_surface(focus);
2789 if (surface == NULL)
2790 return;
2791
2792 shsurf = get_shell_surface(surface);
2793 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2794 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2795 return;
2796
2797 surface_touch_move(shsurf, (struct weston_seat *) seat);
2798}
2799
2800static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002801resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002802{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002803 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002804 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002805 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002806 uint32_t edges = 0;
2807 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002808 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002809
Pekka Paalanen01388e22013-04-25 13:57:44 +03002810 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002811 if (surface == NULL)
2812 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002813
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002814 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002815 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2816 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002817 return;
2818
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002819 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002820 wl_fixed_to_int(seat->pointer->grab_x),
2821 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002822 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002823
Pekka Paalanen60921e52012-01-25 15:55:43 +02002824 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002825 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002826 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002827 edges |= 0;
2828 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002829 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002830
Pekka Paalanen60921e52012-01-25 15:55:43 +02002831 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002832 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002833 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002834 edges |= 0;
2835 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002836 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002837
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002838 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002839}
2840
2841static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002842surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002843 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002844{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002845 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002846 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002847 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002848 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002849 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002850
Pekka Paalanen01388e22013-04-25 13:57:44 +03002851 /* XXX: broken for windows containing sub-surfaces */
2852 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002853 if (surface == NULL)
2854 return;
2855
2856 shsurf = get_shell_surface(surface);
2857 if (!shsurf)
2858 return;
2859
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002860 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002861
Scott Moreau02709af2012-05-22 01:54:10 -06002862 if (surface->alpha > 1.0)
2863 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002864 if (surface->alpha < step)
2865 surface->alpha = step;
2866
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002867 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002868 weston_surface_damage(surface);
2869}
2870
2871static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002872do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002873 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002874{
Daniel Stone37816df2012-05-16 18:45:18 +01002875 struct weston_seat *ws = (struct weston_seat *) seat;
2876 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002877 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002878 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002879
2880 wl_list_for_each(output, &compositor->output_list, link) {
2881 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002882 wl_fixed_to_double(seat->pointer->x),
2883 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002884 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002885 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002886 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002887 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002888 increment = -output->zoom.increment;
2889 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002890 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002891 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002892 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002893 else
2894 increment = 0;
2895
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002896 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002897
Scott Moreaue6603982012-06-11 13:07:51 -06002898 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002899 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002900 else if (output->zoom.level > output->zoom.max_level)
2901 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002902 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002903 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002904 output->disable_planes++;
2905 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002906
Scott Moreaue6603982012-06-11 13:07:51 -06002907 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002908
Scott Moreau8dacaab2012-06-17 18:10:58 -06002909 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002910 }
2911 }
2912}
2913
Scott Moreauccbf29d2012-02-22 14:21:41 -07002914static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002915zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002916 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002917{
2918 do_zoom(seat, time, 0, axis, value);
2919}
2920
2921static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002922zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002923 void *data)
2924{
2925 do_zoom(seat, time, key, 0, 0);
2926}
2927
2928static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002929terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002930 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002931{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002932 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002933
Daniel Stone325fc2d2012-05-30 16:31:58 +01002934 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002935}
2936
2937static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002938rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002939{
2940 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002941 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002942 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002943 struct shell_surface *shsurf = rotate->base.shsurf;
2944 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002945 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002946
2947 if (!shsurf)
2948 return;
2949
2950 surface = shsurf->surface;
2951
2952 cx = 0.5f * surface->geometry.width;
2953 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002954
Daniel Stone37816df2012-05-16 18:45:18 +01002955 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2956 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002957 r = sqrtf(dx * dx + dy * dy);
2958
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002959 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002960 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002961
2962 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002963 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002964 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002965
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002966 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002967 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002968
2969 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002970 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002971 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002972 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002973 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002974
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002975 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002976 &shsurf->surface->geometry.transformation_list,
2977 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002978 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002979 wl_list_init(&shsurf->rotation.transform.link);
2980 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002981 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002982 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002983
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002984 /* We need to adjust the position of the surface
2985 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002986 cposx = surface->geometry.x + cx;
2987 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002988 dposx = rotate->center.x - cposx;
2989 dposy = rotate->center.y - cposy;
2990 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002991 weston_surface_set_position(surface,
2992 surface->geometry.x + dposx,
2993 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002994 }
2995
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002996 /* Repaint implies weston_surface_update_transform(), which
2997 * lazily applies the damage due to rotation update.
2998 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002999 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003000}
3001
3002static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003003rotate_grab_button(struct weston_pointer_grab *grab,
3004 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003005{
3006 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003007 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003008 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003009 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003010 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003011
Daniel Stone4dbadb12012-05-30 16:31:51 +01003012 if (pointer->button_count == 0 &&
3013 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003014 if (shsurf)
3015 weston_matrix_multiply(&shsurf->rotation.rotation,
3016 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003017 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003018 free(rotate);
3019 }
3020}
3021
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003022static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003023 noop_grab_focus,
3024 rotate_grab_motion,
3025 rotate_grab_button,
3026};
3027
3028static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003029surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003030{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003031 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003032 float dx, dy;
3033 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003034
Pekka Paalanen460099f2012-01-20 16:48:25 +02003035 rotate = malloc(sizeof *rotate);
3036 if (!rotate)
3037 return;
3038
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04003039 weston_surface_to_global_float(surface->surface,
Louis-Francis Ratté-Boulianne9a4f10f2013-07-03 15:58:22 +02003040 surface->surface->geometry.width * 0.5f,
3041 surface->surface->geometry.height * 0.5f,
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04003042 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003043
Daniel Stone37816df2012-05-16 18:45:18 +01003044 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3045 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003046 r = sqrtf(dx * dx + dy * dy);
3047 if (r > 20.0f) {
3048 struct weston_matrix inverse;
3049
3050 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003051 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003052 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003053
3054 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003055 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003056 } else {
3057 weston_matrix_init(&surface->rotation.rotation);
3058 weston_matrix_init(&rotate->rotation);
3059 }
3060
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003061 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3062 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003063}
3064
3065static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003066rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003067 void *data)
3068{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003069 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003070 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003071 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003072 struct shell_surface *surface;
3073
Pekka Paalanen01388e22013-04-25 13:57:44 +03003074 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003075 if (base_surface == NULL)
3076 return;
3077
3078 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003079 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3080 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003081 return;
3082
3083 surface_rotate(surface, seat);
3084}
3085
3086static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003087lower_fullscreen_layer(struct desktop_shell *shell)
3088{
3089 struct workspace *ws;
3090 struct weston_surface *surface, *prev;
3091
3092 ws = get_current_workspace(shell);
3093 wl_list_for_each_reverse_safe(surface, prev,
3094 &shell->fullscreen_layer.surface_list,
3095 layer_link)
3096 weston_surface_restack(surface, &ws->layer.surface_list);
3097}
3098
3099static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003100activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003101 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003102{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003103 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003104 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003105 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003106
Pekka Paalanen01388e22013-04-25 13:57:44 +03003107 main_surface = weston_surface_get_main_surface(es);
3108
Daniel Stone37816df2012-05-16 18:45:18 +01003109 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003110
Jonas Ådahl8538b222012-08-29 22:13:03 +02003111 state = ensure_focus_state(shell, seat);
3112 if (state == NULL)
3113 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003114
3115 state->keyboard_focus = es;
3116 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003117 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003118
Pekka Paalanen01388e22013-04-25 13:57:44 +03003119 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003120 case SHELL_SURFACE_FULLSCREEN:
3121 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003122 shell_stack_fullscreen(get_shell_surface(main_surface));
3123 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08003124 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003125 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003126 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003127 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003128 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003129 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003130 }
3131}
3132
Alex Wu21858432012-04-01 20:13:08 +08003133/* no-op func for checking black surface */
3134static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003135black_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 +08003136{
3137}
3138
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003139static bool
Alex Wu21858432012-04-01 20:13:08 +08003140is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3141{
3142 if (es->configure == black_surface_configure) {
3143 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003144 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003145 return true;
3146 }
3147 return false;
3148}
3149
Kristian Høgsberg75840622011-09-06 13:48:16 -04003150static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003151activate_binding(struct weston_seat *seat,
3152 struct desktop_shell *shell,
3153 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003154{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003155 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003156
Alex Wu9c35e6b2012-03-05 14:13:13 +08003157 if (!focus)
3158 return;
3159
Pekka Paalanen01388e22013-04-25 13:57:44 +03003160 if (is_black_surface(focus, &main_surface))
3161 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003162
Pekka Paalanen01388e22013-04-25 13:57:44 +03003163 main_surface = weston_surface_get_main_surface(focus);
3164 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003165 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003166
Neil Robertsa28c6932013-10-03 16:43:04 +01003167 activate(shell, focus, seat);
3168}
3169
3170static void
3171click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3172 void *data)
3173{
3174 if (seat->pointer->grab != &seat->pointer->default_grab)
3175 return;
3176
3177 activate_binding(seat, data,
3178 (struct weston_surface *) seat->pointer->focus);
3179}
3180
3181static void
3182touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3183{
3184 if (seat->touch->grab != &seat->touch->default_grab)
3185 return;
3186
3187 activate_binding(seat, data,
3188 (struct weston_surface *) seat->touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003189}
3190
3191static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003192lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003193{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003194 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003195
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003196 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003197 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003198 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003199 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003200
3201 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003202
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003203 /* Hide all surfaces by removing the fullscreen, panel and
3204 * toplevel layers. This way nothing else can show or receive
3205 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003206
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003207 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003208 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003209 if (shell->showing_input_panels)
3210 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003211 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003212 wl_list_insert(&shell->compositor->cursor_layer.link,
3213 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003214
Pekka Paalanen77346a62011-11-30 16:26:35 +02003215 launch_screensaver(shell);
3216
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003217 /* TODO: disable bindings that should not work while locked. */
3218
3219 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003220}
3221
3222static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003223unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003224{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003225 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003226 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003227 return;
3228 }
3229
3230 /* If desktop-shell client has gone away, unlock immediately. */
3231 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003232 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003233 return;
3234 }
3235
3236 if (shell->prepare_event_sent)
3237 return;
3238
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003239 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003240 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003241}
3242
3243static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003244shell_fade_done(struct weston_surface_animation *animation, void *data)
3245{
3246 struct desktop_shell *shell = data;
3247
3248 shell->fade.animation = NULL;
3249
3250 switch (shell->fade.type) {
3251 case FADE_IN:
3252 weston_surface_destroy(shell->fade.surface);
3253 shell->fade.surface = NULL;
3254 break;
3255 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003256 lock(shell);
3257 break;
3258 }
3259}
3260
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003261static struct weston_surface *
3262shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003263{
3264 struct weston_compositor *compositor = shell->compositor;
3265 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003266
3267 surface = weston_surface_create(compositor);
3268 if (!surface)
3269 return NULL;
3270
3271 weston_surface_configure(surface, 0, 0, 8192, 8192);
3272 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3273 wl_list_insert(&compositor->fade_layer.surface_list,
3274 &surface->layer_link);
3275 pixman_region32_init(&surface->input);
3276
3277 return surface;
3278}
3279
3280static void
3281shell_fade(struct desktop_shell *shell, enum fade_type type)
3282{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003283 float tint;
3284
3285 switch (type) {
3286 case FADE_IN:
3287 tint = 0.0;
3288 break;
3289 case FADE_OUT:
3290 tint = 1.0;
3291 break;
3292 default:
3293 weston_log("shell: invalid fade type\n");
3294 return;
3295 }
3296
3297 shell->fade.type = type;
3298
3299 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003300 shell->fade.surface = shell_fade_create_surface(shell);
3301 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003302 return;
3303
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003304 shell->fade.surface->alpha = 1.0 - tint;
3305 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003306 }
3307
3308 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003309 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003310 else
3311 shell->fade.animation =
3312 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003313 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003314 shell_fade_done, shell);
3315}
3316
3317static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003318do_shell_fade_startup(void *data)
3319{
3320 struct desktop_shell *shell = data;
3321
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003322 if (shell->startup_animation_type == ANIMATION_FADE)
3323 shell_fade(shell, FADE_IN);
3324 else if (shell->startup_animation_type == ANIMATION_NONE) {
3325 weston_surface_destroy(shell->fade.surface);
3326 shell->fade.surface = NULL;
3327 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003328}
3329
3330static void
3331shell_fade_startup(struct desktop_shell *shell)
3332{
3333 struct wl_event_loop *loop;
3334
3335 if (!shell->fade.startup_timer)
3336 return;
3337
3338 wl_event_source_remove(shell->fade.startup_timer);
3339 shell->fade.startup_timer = NULL;
3340
3341 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3342 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3343}
3344
3345static int
3346fade_startup_timeout(void *data)
3347{
3348 struct desktop_shell *shell = data;
3349
3350 shell_fade_startup(shell);
3351 return 0;
3352}
3353
3354static void
3355shell_fade_init(struct desktop_shell *shell)
3356{
3357 /* Make compositor output all black, and wait for the desktop-shell
3358 * client to signal it is ready, then fade in. The timer triggers a
3359 * fade-in, in case the desktop-shell client takes too long.
3360 */
3361
3362 struct wl_event_loop *loop;
3363
3364 if (shell->fade.surface != NULL) {
3365 weston_log("%s: warning: fade surface already exists\n",
3366 __func__);
3367 return;
3368 }
3369
3370 shell->fade.surface = shell_fade_create_surface(shell);
3371 if (!shell->fade.surface)
3372 return;
3373
3374 weston_surface_update_transform(shell->fade.surface);
3375 weston_surface_damage(shell->fade.surface);
3376
3377 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3378 shell->fade.startup_timer =
3379 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3380 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3381}
3382
3383static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003384idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003385{
3386 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003387 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003388
3389 shell_fade(shell, FADE_OUT);
3390 /* lock() is called from shell_fade_done() */
3391}
3392
3393static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003394wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003395{
3396 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003397 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003398
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003399 unlock(shell);
3400}
3401
3402static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003403show_input_panels(struct wl_listener *listener, void *data)
3404{
3405 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003406 container_of(listener, struct desktop_shell,
3407 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003408 struct input_panel_surface *surface, *next;
3409 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003410
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003411 shell->text_input.surface = (struct weston_surface*)data;
3412
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003413 if (shell->showing_input_panels)
3414 return;
3415
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003416 shell->showing_input_panels = true;
3417
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003418 if (!shell->locked)
3419 wl_list_insert(&shell->panel_layer.link,
3420 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003421
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003422 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003423 &shell->input_panel.surfaces, link) {
3424 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003425 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003426 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003427 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003428 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003429 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003430 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003431 weston_surface_damage(ws);
3432 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003433 }
3434}
3435
3436static void
3437hide_input_panels(struct wl_listener *listener, void *data)
3438{
3439 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003440 container_of(listener, struct desktop_shell,
3441 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003442 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003443
Jan Arne Petersen61381972013-01-31 15:52:21 +01003444 if (!shell->showing_input_panels)
3445 return;
3446
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003447 shell->showing_input_panels = false;
3448
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003449 if (!shell->locked)
3450 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003451
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003452 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003453 &shell->input_panel_layer.surface_list, layer_link)
3454 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003455}
3456
3457static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003458update_input_panels(struct wl_listener *listener, void *data)
3459{
3460 struct desktop_shell *shell =
3461 container_of(listener, struct desktop_shell,
3462 update_input_panel_listener);
3463
3464 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3465}
3466
3467static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003468center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003469{
Giulio Camuffob8366642013-04-25 13:57:46 +03003470 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003471 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003472
Giulio Camuffob8366642013-04-25 13:57:46 +03003473 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3474
3475 x = output->x + (output->width - width) / 2 - surf_x / 2;
3476 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003477
3478 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003479}
3480
3481static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003482weston_surface_set_initial_position (struct weston_surface *surface,
3483 struct desktop_shell *shell)
3484{
3485 struct weston_compositor *compositor = shell->compositor;
3486 int ix = 0, iy = 0;
3487 int range_x, range_y;
3488 int dx, dy, x, y, panel_height;
3489 struct weston_output *output, *target_output = NULL;
3490 struct weston_seat *seat;
3491
3492 /* As a heuristic place the new window on the same output as the
3493 * pointer. Falling back to the output containing 0, 0.
3494 *
3495 * TODO: Do something clever for touch too?
3496 */
3497 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003498 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003499 ix = wl_fixed_to_int(seat->pointer->x);
3500 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003501 break;
3502 }
3503 }
3504
3505 wl_list_for_each(output, &compositor->output_list, link) {
3506 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3507 target_output = output;
3508 break;
3509 }
3510 }
3511
3512 if (!target_output) {
3513 weston_surface_set_position(surface, 10 + random() % 400,
3514 10 + random() % 400);
3515 return;
3516 }
3517
3518 /* Valid range within output where the surface will still be onscreen.
3519 * If this is negative it means that the surface is bigger than
3520 * output.
3521 */
3522 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003523 range_x = target_output->width - surface->geometry.width;
3524 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003525 surface->geometry.height;
3526
Rob Bradford4cb88c72012-08-13 15:18:44 +01003527 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003528 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003529 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003530 dx = 0;
3531
3532 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003533 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003534 else
3535 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003536
3537 x = target_output->x + dx;
3538 y = target_output->y + dy;
3539
3540 weston_surface_set_position (surface, x, y);
3541}
3542
3543static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003544map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003545 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003546{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003547 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003548 struct shell_surface *shsurf = get_shell_surface(surface);
3549 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003550 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003551 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003552 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003553 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003554 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003555
Pekka Paalanen60921e52012-01-25 15:55:43 +02003556 surface->geometry.width = width;
3557 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003558 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003559
3560 /* initial positioning, see also configure() */
3561 switch (surface_type) {
3562 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003563 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003564 break;
Alex Wu4539b082012-03-01 12:57:46 +08003565 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003566 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003567 shell_map_fullscreen(shsurf);
3568 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003569 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003570 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003571 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003572 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3573 NULL, NULL);
3574 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3575 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003576 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003577 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003578 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003579 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003580 case SHELL_SURFACE_NONE:
3581 weston_surface_set_position(surface,
3582 surface->geometry.x + sx,
3583 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003584 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003585 default:
3586 ;
3587 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003588
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003589 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003590 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003591 case SHELL_SURFACE_POPUP:
3592 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003593 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003594 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3595 break;
Alex Wu4539b082012-03-01 12:57:46 +08003596 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003597 case SHELL_SURFACE_NONE:
3598 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003599 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003600 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003601 ws = get_current_workspace(shell);
3602 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003603 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003604 }
3605
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003606 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003607 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003608 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3609 surface->output = shsurf->output;
3610 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003611
Juan Zhao7bb92f02011-12-15 11:31:51 -05003612 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003613 /* XXX: xwayland's using the same fields for transient type */
3614 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003615 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003616 if (shsurf->transient.flags ==
3617 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3618 break;
3619 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003620 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003621 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003622 if (!shell->locked) {
3623 wl_list_for_each(seat, &compositor->seat_list, link)
3624 activate(shell, surface, seat);
3625 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003626 break;
3627 default:
3628 break;
3629 }
3630
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003631 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003632 {
3633 switch (shell->win_animation_type) {
3634 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003635 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003636 break;
3637 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003638 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003639 break;
3640 default:
3641 break;
3642 }
3643 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003644}
3645
3646static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003647configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003648 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003649{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003650 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3651 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003652 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003653
Pekka Paalanen77346a62011-11-30 16:26:35 +02003654 shsurf = get_shell_surface(surface);
3655 if (shsurf)
3656 surface_type = shsurf->type;
3657
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003658 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003659
3660 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003661 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003662 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003663 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003664 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003665 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003666 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003667 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3668 NULL, NULL);
3669 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003670 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003671 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003672 break;
Alex Wu4539b082012-03-01 12:57:46 +08003673 case SHELL_SURFACE_TOPLEVEL:
3674 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003675 default:
3676 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003677 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003678
Alex Wu4539b082012-03-01 12:57:46 +08003679 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003680 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003681 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003682
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003683 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003684 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003685 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003686}
3687
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003688static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003689shell_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 +03003690{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003691 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003692 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003693
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003694 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003695
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003696 if (!weston_surface_is_mapped(es) &&
3697 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003698 remove_popup_grab(shsurf);
3699 }
3700
Giulio Camuffo184df502013-02-21 11:29:21 +01003701 if (width == 0)
3702 return;
3703
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003704 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003705 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003706 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003707 type_changed = 1;
3708 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003709
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003710 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003711 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003712 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003713 es->geometry.width != width ||
3714 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003715 float from_x, from_y;
3716 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003717
3718 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3719 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3720 configure(shell, es,
3721 es->geometry.x + to_x - from_x,
3722 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003723 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003724 }
3725}
3726
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003727static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003728
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003729static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003730desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003731{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003732 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003733 struct desktop_shell *shell =
3734 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003735
3736 shell->child.process.pid = 0;
3737 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003738
3739 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3740 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003741 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003742 shell->child.deathstamp = time;
3743 shell->child.deathcount = 0;
3744 }
3745
3746 shell->child.deathcount++;
3747 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003748 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003749 return;
3750 }
3751
Martin Minarik6d118362012-06-07 18:01:59 +02003752 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003753 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003754 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003755}
3756
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003757static void
3758launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003759{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003760 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003761 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003762
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003763 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003764 &shell->child.process,
3765 shell_exe,
3766 desktop_shell_sigchld);
3767
3768 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003769 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003770}
3771
3772static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003773bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3774{
Tiago Vignattibe143262012-04-16 17:31:41 +03003775 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003776 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003777
Jason Ekstranda85118c2013-06-27 20:17:02 -05003778 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3779 if (resource)
3780 wl_resource_set_implementation(resource, &shell_implementation,
3781 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003782}
3783
Kristian Høgsberg75840622011-09-06 13:48:16 -04003784static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003785unbind_desktop_shell(struct wl_resource *resource)
3786{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003787 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003788
3789 if (shell->locked)
3790 resume_desktop(shell);
3791
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003792 shell->child.desktop_shell = NULL;
3793 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003794}
3795
3796static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003797bind_desktop_shell(struct wl_client *client,
3798 void *data, uint32_t version, uint32_t id)
3799{
Tiago Vignattibe143262012-04-16 17:31:41 +03003800 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003801 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003802
Jason Ekstranda85118c2013-06-27 20:17:02 -05003803 resource = wl_resource_create(client, &desktop_shell_interface,
3804 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003805
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003806 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003807 wl_resource_set_implementation(resource,
3808 &desktop_shell_implementation,
3809 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003810 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003811
3812 if (version < 2)
3813 shell_fade_startup(shell);
3814
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003815 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003816 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003817
3818 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3819 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003820 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003821}
3822
Pekka Paalanen6e168112011-11-24 11:34:05 +02003823static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003824screensaver_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 -04003825{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003826 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003827
Giulio Camuffo184df502013-02-21 11:29:21 +01003828 if (width == 0)
3829 return;
3830
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003831 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003832 if (!shell->locked)
3833 return;
3834
3835 center_on_output(surface, surface->output);
3836
3837 if (wl_list_empty(&surface->layer_link)) {
3838 wl_list_insert(shell->lock_layer.surface_list.prev,
3839 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003840 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003841 wl_event_source_timer_update(shell->screensaver.timer,
3842 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003843 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003844 }
3845}
3846
3847static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003848screensaver_set_surface(struct wl_client *client,
3849 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003850 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003851 struct wl_resource *output_resource)
3852{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003853 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003854 struct weston_surface *surface =
3855 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003856 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003857
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003858 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003859 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003860 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003861}
3862
3863static const struct screensaver_interface screensaver_implementation = {
3864 screensaver_set_surface
3865};
3866
3867static void
3868unbind_screensaver(struct wl_resource *resource)
3869{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003870 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003871
Pekka Paalanen77346a62011-11-30 16:26:35 +02003872 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003873}
3874
3875static void
3876bind_screensaver(struct wl_client *client,
3877 void *data, uint32_t version, uint32_t id)
3878{
Tiago Vignattibe143262012-04-16 17:31:41 +03003879 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003880 struct wl_resource *resource;
3881
Jason Ekstranda85118c2013-06-27 20:17:02 -05003882 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003883
Pekka Paalanen77346a62011-11-30 16:26:35 +02003884 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003885 wl_resource_set_implementation(resource,
3886 &screensaver_implementation,
3887 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003888 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003889 return;
3890 }
3891
3892 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3893 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003894 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003895}
3896
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003897static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003898input_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 -04003899{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003900 struct input_panel_surface *ip_surface = surface->configure_private;
3901 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003902 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003903 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003904
Giulio Camuffo184df502013-02-21 11:29:21 +01003905 if (width == 0)
3906 return;
3907
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003908 if (!weston_surface_is_mapped(surface)) {
3909 if (!shell->showing_input_panels)
3910 return;
3911
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003912 show_surface = 1;
3913 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003914
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003915 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003916
3917 if (ip_surface->panel) {
3918 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3919 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3920 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01003921 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
3922 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003923 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003924
3925 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003926 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003927 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003928
3929 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003930 wl_list_insert(&shell->input_panel_layer.surface_list,
3931 &surface->layer_link);
3932 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003933 weston_surface_damage(surface);
3934 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3935 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003936}
3937
3938static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003939destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003940{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003941 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3942
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003943 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003944 wl_list_remove(&input_panel_surface->link);
3945
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003946 input_panel_surface->surface->configure = NULL;
3947
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003948 free(input_panel_surface);
3949}
3950
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003951static struct input_panel_surface *
3952get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003953{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003954 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003955 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003956 } else {
3957 return NULL;
3958 }
3959}
3960
3961static void
3962input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3963{
3964 struct input_panel_surface *ipsurface = container_of(listener,
3965 struct input_panel_surface,
3966 surface_destroy_listener);
3967
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003968 if (ipsurface->resource) {
3969 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003970 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003971 destroy_input_panel_surface(ipsurface);
3972 }
3973}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003974
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003975static struct input_panel_surface *
3976create_input_panel_surface(struct desktop_shell *shell,
3977 struct weston_surface *surface)
3978{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003979 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003980
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003981 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3982 if (!input_panel_surface)
3983 return NULL;
3984
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003985 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003986 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003987
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003988 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003989
3990 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003991
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003992 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003993 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003994 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003995 &input_panel_surface->surface_destroy_listener);
3996
3997 wl_list_init(&input_panel_surface->link);
3998
3999 return input_panel_surface;
4000}
4001
4002static void
4003input_panel_surface_set_toplevel(struct wl_client *client,
4004 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004005 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004006 uint32_t position)
4007{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004008 struct input_panel_surface *input_panel_surface =
4009 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004010 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004011
4012 wl_list_insert(&shell->input_panel.surfaces,
4013 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004014
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004015 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004016 input_panel_surface->panel = 0;
4017}
4018
4019static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004020input_panel_surface_set_overlay_panel(struct wl_client *client,
4021 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004022{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004023 struct input_panel_surface *input_panel_surface =
4024 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004025 struct desktop_shell *shell = input_panel_surface->shell;
4026
4027 wl_list_insert(&shell->input_panel.surfaces,
4028 &input_panel_surface->link);
4029
4030 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004031}
4032
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004033static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004034 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004035 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004036};
4037
4038static void
4039destroy_input_panel_surface_resource(struct wl_resource *resource)
4040{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004041 struct input_panel_surface *ipsurf =
4042 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004043
4044 destroy_input_panel_surface(ipsurf);
4045}
4046
4047static void
4048input_panel_get_input_panel_surface(struct wl_client *client,
4049 struct wl_resource *resource,
4050 uint32_t id,
4051 struct wl_resource *surface_resource)
4052{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004053 struct weston_surface *surface =
4054 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004055 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004056 struct input_panel_surface *ipsurf;
4057
4058 if (get_input_panel_surface(surface)) {
4059 wl_resource_post_error(surface_resource,
4060 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004061 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004062 return;
4063 }
4064
4065 ipsurf = create_input_panel_surface(shell, surface);
4066 if (!ipsurf) {
4067 wl_resource_post_error(surface_resource,
4068 WL_DISPLAY_ERROR_INVALID_OBJECT,
4069 "surface->configure already set");
4070 return;
4071 }
4072
Jason Ekstranda85118c2013-06-27 20:17:02 -05004073 ipsurf->resource =
4074 wl_resource_create(client,
4075 &wl_input_panel_surface_interface, 1, id);
4076 wl_resource_set_implementation(ipsurf->resource,
4077 &input_panel_surface_implementation,
4078 ipsurf,
4079 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004080}
4081
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004082static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004083 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004084};
4085
4086static void
4087unbind_input_panel(struct wl_resource *resource)
4088{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004089 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004090
4091 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004092}
4093
4094static void
4095bind_input_panel(struct wl_client *client,
4096 void *data, uint32_t version, uint32_t id)
4097{
4098 struct desktop_shell *shell = data;
4099 struct wl_resource *resource;
4100
Jason Ekstranda85118c2013-06-27 20:17:02 -05004101 resource = wl_resource_create(client,
4102 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004103
4104 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004105 wl_resource_set_implementation(resource,
4106 &input_panel_implementation,
4107 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004108 shell->input_panel.binding = resource;
4109 return;
4110 }
4111
4112 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4113 "interface object already bound");
4114 wl_resource_destroy(resource);
4115}
4116
Kristian Høgsberg07045392012-02-19 18:52:44 -05004117struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004118 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004119 struct weston_surface *current;
4120 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004121 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004122};
4123
4124static void
4125switcher_next(struct switcher *switcher)
4126{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004127 struct weston_surface *surface;
4128 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004129 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004130 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004131
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004132 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004133 switch (get_shell_surface_type(surface)) {
4134 case SHELL_SURFACE_TOPLEVEL:
4135 case SHELL_SURFACE_FULLSCREEN:
4136 case SHELL_SURFACE_MAXIMIZED:
4137 if (first == NULL)
4138 first = surface;
4139 if (prev == switcher->current)
4140 next = surface;
4141 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06004142 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02004143 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004144 weston_surface_damage(surface);
4145 break;
4146 default:
4147 break;
4148 }
Alex Wu1659daa2012-04-01 20:13:09 +08004149
4150 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06004151 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02004152 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004153 weston_surface_damage(surface);
4154 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004155 }
4156
4157 if (next == NULL)
4158 next = first;
4159
Alex Wu07b26062012-03-12 16:06:01 +08004160 if (next == NULL)
4161 return;
4162
Kristian Høgsberg07045392012-02-19 18:52:44 -05004163 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004164 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004165
4166 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004167 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004168
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004169 shsurf = get_shell_surface(switcher->current);
4170 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004171 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004172}
4173
4174static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004175switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004176{
4177 struct switcher *switcher =
4178 container_of(listener, struct switcher, listener);
4179
4180 switcher_next(switcher);
4181}
4182
4183static void
Daniel Stone351eb612012-05-31 15:27:47 -04004184switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004185{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004186 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004187 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004188 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004189
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004190 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004191 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004192 weston_surface_damage(surface);
4193 }
4194
Alex Wu07b26062012-03-12 16:06:01 +08004195 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004196 activate(switcher->shell, switcher->current,
4197 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004198 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004199 weston_keyboard_end_grab(keyboard);
4200 if (keyboard->input_method_resource)
4201 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004202 free(switcher);
4203}
4204
4205static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004206switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004207 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004208{
4209 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004210 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004211
Daniel Stonec9785ea2012-05-30 16:31:52 +01004212 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004213 switcher_next(switcher);
4214}
4215
4216static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004217switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004218 uint32_t mods_depressed, uint32_t mods_latched,
4219 uint32_t mods_locked, uint32_t group)
4220{
4221 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004222 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004223
Daniel Stone351eb612012-05-31 15:27:47 -04004224 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4225 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004226}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004227
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004228static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004229 switcher_key,
4230 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004231};
4232
4233static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004234switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004235 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004236{
Tiago Vignattibe143262012-04-16 17:31:41 +03004237 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004238 struct switcher *switcher;
4239
4240 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004241 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004242 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004243 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004244 wl_list_init(&switcher->listener.link);
4245
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004246 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004247 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004248 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004249 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4250 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004251 switcher_next(switcher);
4252}
4253
Pekka Paalanen3c647232011-12-22 13:43:43 +02004254static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004255backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004256 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004257{
4258 struct weston_compositor *compositor = data;
4259 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004260 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004261
4262 /* TODO: we're limiting to simple use cases, where we assume just
4263 * control on the primary display. We'd have to extend later if we
4264 * ever get support for setting backlights on random desktop LCD
4265 * panels though */
4266 output = get_default_output(compositor);
4267 if (!output)
4268 return;
4269
4270 if (!output->set_backlight)
4271 return;
4272
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004273 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4274 backlight_new = output->backlight_current - 25;
4275 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4276 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004277
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004278 if (backlight_new < 5)
4279 backlight_new = 5;
4280 if (backlight_new > 255)
4281 backlight_new = 255;
4282
4283 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004284 output->set_backlight(output, output->backlight_current);
4285}
4286
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004287struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004288 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004289 struct weston_seat *seat;
4290 uint32_t key[2];
4291 int key_released[2];
4292};
4293
4294static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004295debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004296 uint32_t key, uint32_t state)
4297{
4298 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004299 struct weston_compositor *ec = db->seat->compositor;
4300 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004301 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004302 uint32_t serial;
4303 int send = 0, terminate = 0;
4304 int check_binding = 1;
4305 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01004306 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004307
4308 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4309 /* Do not run bindings on key releases */
4310 check_binding = 0;
4311
4312 for (i = 0; i < 2; i++)
4313 if (key == db->key[i])
4314 db->key_released[i] = 1;
4315
4316 if (db->key_released[0] && db->key_released[1]) {
4317 /* All key releases been swalled so end the grab */
4318 terminate = 1;
4319 } else if (key != db->key[0] && key != db->key[1]) {
4320 /* Should not swallow release of other keys */
4321 send = 1;
4322 }
4323 } else if (key == db->key[0] && !db->key_released[0]) {
4324 /* Do not check bindings for the first press of the binding
4325 * key. This allows it to be used as a debug shortcut.
4326 * We still need to swallow this event. */
4327 check_binding = 0;
4328 } else if (db->key[1]) {
4329 /* If we already ran a binding don't process another one since
4330 * we can't keep track of all the binding keys that were
4331 * pressed in order to swallow the release events. */
4332 send = 1;
4333 check_binding = 0;
4334 }
4335
4336 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004337 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4338 key, state)) {
4339 /* We ran a binding so swallow the press and keep the
4340 * grab to swallow the released too. */
4341 send = 0;
4342 terminate = 0;
4343 db->key[1] = key;
4344 } else {
4345 /* Terminate the grab since the key pressed is not a
4346 * debug binding key. */
4347 send = 1;
4348 terminate = 1;
4349 }
4350 }
4351
4352 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01004353 serial = wl_display_next_serial(display);
4354 resource_list = &grab->keyboard->focus_resource_list;
4355 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004356 wl_keyboard_send_key(resource, serial, time, key, state);
4357 }
4358 }
4359
4360 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004361 weston_keyboard_end_grab(grab->keyboard);
4362 if (grab->keyboard->input_method_resource)
4363 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004364 free(db);
4365 }
4366}
4367
4368static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004369debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004370 uint32_t mods_depressed, uint32_t mods_latched,
4371 uint32_t mods_locked, uint32_t group)
4372{
4373 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01004374 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004375
Neil Roberts96d790e2013-09-19 17:32:00 +01004376 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004377
Neil Roberts96d790e2013-09-19 17:32:00 +01004378 wl_resource_for_each(resource, resource_list) {
4379 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4380 mods_latched, mods_locked, group);
4381 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004382}
4383
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004384struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004385 debug_binding_key,
4386 debug_binding_modifiers
4387};
4388
4389static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004390debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004391{
4392 struct debug_binding_grab *grab;
4393
4394 grab = calloc(1, sizeof *grab);
4395 if (!grab)
4396 return;
4397
4398 grab->seat = (struct weston_seat *) seat;
4399 grab->key[0] = key;
4400 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004401 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004402}
4403
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004404static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004405force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004406 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004407{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004408 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004409 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004410 struct desktop_shell *shell = data;
4411 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004412 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004413
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004414 focus_surface = seat->keyboard->focus;
4415 if (!focus_surface)
4416 return;
4417
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004418 wl_signal_emit(&compositor->kill_signal, focus_surface);
4419
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004420 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004421 wl_client_get_credentials(client, &pid, NULL, NULL);
4422
4423 /* Skip clients that we launched ourselves (the credentials of
4424 * the socketpair is ours) */
4425 if (pid == getpid())
4426 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004427
Daniel Stone325fc2d2012-05-30 16:31:58 +01004428 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004429}
4430
4431static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004432workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004433 uint32_t key, void *data)
4434{
4435 struct desktop_shell *shell = data;
4436 unsigned int new_index = shell->workspaces.current;
4437
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004438 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004439 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004440 if (new_index != 0)
4441 new_index--;
4442
4443 change_workspace(shell, new_index);
4444}
4445
4446static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004447workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004448 uint32_t key, void *data)
4449{
4450 struct desktop_shell *shell = data;
4451 unsigned int new_index = shell->workspaces.current;
4452
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004453 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004454 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004455 if (new_index < shell->workspaces.num - 1)
4456 new_index++;
4457
4458 change_workspace(shell, new_index);
4459}
4460
4461static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004462workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004463 uint32_t key, void *data)
4464{
4465 struct desktop_shell *shell = data;
4466 unsigned int new_index;
4467
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004468 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004469 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004470 new_index = key - KEY_F1;
4471 if (new_index >= shell->workspaces.num)
4472 new_index = shell->workspaces.num - 1;
4473
4474 change_workspace(shell, new_index);
4475}
4476
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004477static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004478workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004479 uint32_t key, void *data)
4480{
4481 struct desktop_shell *shell = data;
4482 unsigned int new_index = shell->workspaces.current;
4483
4484 if (shell->locked)
4485 return;
4486
4487 if (new_index != 0)
4488 new_index--;
4489
4490 take_surface_to_workspace_by_seat(shell, seat, new_index);
4491}
4492
4493static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004494workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004495 uint32_t key, void *data)
4496{
4497 struct desktop_shell *shell = data;
4498 unsigned int new_index = shell->workspaces.current;
4499
4500 if (shell->locked)
4501 return;
4502
4503 if (new_index < shell->workspaces.num - 1)
4504 new_index++;
4505
4506 take_surface_to_workspace_by_seat(shell, seat, new_index);
4507}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004508
4509static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004510shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004511{
Tiago Vignattibe143262012-04-16 17:31:41 +03004512 struct desktop_shell *shell =
4513 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004514 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004515
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004516 if (shell->child.client)
4517 wl_client_destroy(shell->child.client);
4518
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004519 wl_list_remove(&shell->idle_listener.link);
4520 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004521 wl_list_remove(&shell->show_input_panel_listener.link);
4522 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004523
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004524 wl_array_for_each(ws, &shell->workspaces.array)
4525 workspace_destroy(*ws);
4526 wl_array_release(&shell->workspaces.array);
4527
Pekka Paalanen3c647232011-12-22 13:43:43 +02004528 free(shell->screensaver.path);
4529 free(shell);
4530}
4531
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004532static void
4533shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4534{
4535 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004536 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004537
4538 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004539 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4540 MODIFIER_CTRL | MODIFIER_ALT,
4541 terminate_binding, ec);
4542 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4543 click_to_activate_binding,
4544 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004545 weston_compositor_add_touch_binding(ec, 0,
4546 touch_to_activate_binding,
4547 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004548 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4549 MODIFIER_SUPER | MODIFIER_ALT,
4550 surface_opacity_binding, NULL);
4551 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4552 MODIFIER_SUPER, zoom_axis_binding,
4553 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004554
4555 /* configurable bindings */
4556 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004557 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4558 zoom_key_binding, NULL);
4559 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4560 zoom_key_binding, NULL);
4561 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4562 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004563 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004564 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4565 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004566
4567 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4568 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4569 rotate_binding, NULL);
4570
Daniel Stone325fc2d2012-05-30 16:31:58 +01004571 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4572 shell);
4573 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4574 ec);
4575 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4576 backlight_binding, ec);
4577 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4578 ec);
4579 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4580 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004581 weston_compositor_add_key_binding(ec, KEY_K, mod,
4582 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004583 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4584 workspace_up_binding, shell);
4585 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4586 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004587 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4588 workspace_move_surface_up_binding,
4589 shell);
4590 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4591 workspace_move_surface_down_binding,
4592 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004593
4594 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4595 if (shell->workspaces.num > 1) {
4596 num_workspace_bindings = shell->workspaces.num;
4597 if (num_workspace_bindings > 6)
4598 num_workspace_bindings = 6;
4599 for (i = 0; i < num_workspace_bindings; i++)
4600 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4601 workspace_f_binding,
4602 shell);
4603 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004604
4605 /* Debug bindings */
4606 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4607 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004608}
4609
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004610WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004611module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004612 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004613{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004614 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004615 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004616 struct workspace **pws;
4617 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004618 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004619
Peter Huttererf3d62272013-08-08 11:57:05 +10004620 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004621 if (shell == NULL)
4622 return -1;
4623
Kristian Høgsberg75840622011-09-06 13:48:16 -04004624 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004625
4626 shell->destroy_listener.notify = shell_destroy;
4627 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004628 shell->idle_listener.notify = idle_handler;
4629 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4630 shell->wake_listener.notify = wake_handler;
4631 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004632 shell->show_input_panel_listener.notify = show_input_panels;
4633 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4634 shell->hide_input_panel_listener.notify = hide_input_panels;
4635 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004636 shell->update_input_panel_listener.notify = update_input_panels;
4637 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004638 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004639 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004640 ec->shell_interface.create_shell_surface = create_shell_surface;
4641 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004642 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004643 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004644 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004645 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004646 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02004647 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004648
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004649 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004650
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004651 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4652 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004653 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4654 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004655 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004656
4657 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004658 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004659
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004660 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004661
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004662 for (i = 0; i < shell->workspaces.num; i++) {
4663 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4664 if (pws == NULL)
4665 return -1;
4666
4667 *pws = workspace_create();
4668 if (*pws == NULL)
4669 return -1;
4670 }
4671 activate_workspace(shell, 0);
4672
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004673 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004674 wl_list_init(&shell->workspaces.animation.link);
4675 shell->workspaces.animation.frame = animate_workspace_change_frame;
4676
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004677 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004678 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004679 return -1;
4680
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004681 if (wl_global_create(ec->wl_display,
4682 &desktop_shell_interface, 2,
4683 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004684 return -1;
4685
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004686 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4687 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004688 return -1;
4689
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004690 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004691 shell, bind_input_panel) == NULL)
4692 return -1;
4693
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004694 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4695 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004696 return -1;
4697
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004698 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004699
4700 loop = wl_display_get_event_loop(ec->wl_display);
4701 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004702
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004703 shell->screensaver.timer =
4704 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4705
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004706 wl_list_for_each(seat, &ec->seat_list, link)
4707 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004708
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004709 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004710
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004711 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004712
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004713 return 0;
4714}