blob: cd94aa5a5025c1ac2c1caaccc8abcd2156687ef0 [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øgsberg02ec0a52011-04-23 13:04:11 -0400166};
167
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500168enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200169 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500170 SHELL_SURFACE_TOPLEVEL,
171 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500172 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800173 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300174 SHELL_SURFACE_POPUP,
175 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200176};
177
Scott Moreauff1db4a2012-04-17 19:06:18 -0600178struct ping_timer {
179 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600180 uint32_t serial;
181};
182
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200183struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500184 struct wl_resource *resource;
185 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200186
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500187 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200188 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400189 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300190 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200191
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400192 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400193 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500194 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800195 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800196 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600197 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100198
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500199 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500201 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200202 } rotation;
203
204 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100205 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500206 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100207 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400208 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500209 } popup;
210
Alex Wu4539b082012-03-01 12:57:46 +0800211 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300212 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400213 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300214 } transient;
215
216 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800217 enum wl_shell_surface_fullscreen_method type;
218 struct weston_transform transform; /* matrix from x, y */
219 uint32_t framerate;
220 struct weston_surface *black_surface;
221 } fullscreen;
222
Scott Moreauff1db4a2012-04-17 19:06:18 -0600223 struct ping_timer *ping_timer;
224
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200225 struct weston_transform workspace_transform;
226
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500227 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100229 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400230
231 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200232};
233
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300234struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400235 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300236 struct shell_surface *shsurf;
237 struct wl_listener shsurf_destroy_listener;
238};
239
Rusty Lynch1084da52013-08-15 09:10:08 -0700240struct shell_touch_grab {
241 struct weston_touch_grab grab;
242 struct shell_surface *shsurf;
243 struct wl_listener shsurf_destroy_listener;
244 struct weston_touch *touch;
245};
246
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300247struct weston_move_grab {
248 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100249 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500250};
251
Rusty Lynch1084da52013-08-15 09:10:08 -0700252struct weston_touch_move_grab {
253 struct shell_touch_grab base;
254 wl_fixed_t dx, dy;
255};
256
Pekka Paalanen460099f2012-01-20 16:48:25 +0200257struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300258 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500259 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200260 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200261 float x;
262 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200263 } center;
264};
265
Giulio Camuffo5085a752013-03-25 21:42:45 +0100266struct shell_seat {
267 struct weston_seat *seat;
268 struct wl_listener seat_destroy_listener;
269
270 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400271 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100272 struct wl_list surfaces_list;
273 struct wl_client *client;
274 int32_t initial_up;
275 } popup_grab;
276};
277
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400278static void
279activate(struct desktop_shell *shell, struct weston_surface *es,
280 struct weston_seat *seat);
281
282static struct workspace *
283get_current_workspace(struct desktop_shell *shell);
284
Alex Wubd3354b2012-04-17 17:20:49 +0800285static struct shell_surface *
286get_shell_surface(struct weston_surface *surface);
287
288static struct desktop_shell *
289shell_surface_get_shell(struct shell_surface *shsurf);
290
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500291static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400292surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500293
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300294static void
295shell_fade_startup(struct desktop_shell *shell);
296
Alex Wubd3354b2012-04-17 17:20:49 +0800297static bool
298shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
299{
300 struct desktop_shell *shell;
301 struct weston_surface *top_fs_es;
302
303 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100304
Alex Wubd3354b2012-04-17 17:20:49 +0800305 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
306 return false;
307
308 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100309 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800310 layer_link);
311 return (shsurf == get_shell_surface(top_fs_es));
312}
313
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500314static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400315destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300316{
317 struct shell_grab *grab;
318
319 grab = container_of(listener, struct shell_grab,
320 shsurf_destroy_listener);
321
322 grab->shsurf = NULL;
323}
324
325static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400326popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400327
328static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300329shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400330 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300331 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400332 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300333 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300334{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300335 struct desktop_shell *shell = shsurf->shell;
336
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400337 popup_grab_end(pointer);
338
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300339 grab->grab.interface = interface;
340 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400341 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500342 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400343 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300344
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400345 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400346 if (shell->child.desktop_shell) {
347 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
348 cursor);
349 weston_pointer_set_focus(pointer, shell->grab_surface,
350 wl_fixed_from_int(0),
351 wl_fixed_from_int(0));
352 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300353}
354
355static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300356shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400358 if (grab->shsurf)
359 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300360
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700361 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300362}
363
364static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700365shell_touch_grab_start(struct shell_touch_grab *grab,
366 const struct weston_touch_grab_interface *interface,
367 struct shell_surface *shsurf,
368 struct weston_touch *touch)
369{
370 struct desktop_shell *shell = shsurf->shell;
371
372 grab->grab.interface = interface;
373 grab->shsurf = shsurf;
374 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
375 wl_signal_add(&shsurf->destroy_signal,
376 &grab->shsurf_destroy_listener);
377
378 grab->touch = touch;
379
380 weston_touch_start_grab(touch, &grab->grab);
381 if (shell->child.desktop_shell)
382 weston_touch_set_focus(touch->seat, shell->grab_surface);
383}
384
385static void
386shell_touch_grab_end(struct shell_touch_grab *grab)
387{
388 if (grab->shsurf)
389 wl_list_remove(&grab->shsurf_destroy_listener.link);
390
391 weston_touch_end_grab(grab->touch);
392}
393
394static void
Alex Wu4539b082012-03-01 12:57:46 +0800395center_on_output(struct weston_surface *surface,
396 struct weston_output *output);
397
Daniel Stone496ca172012-05-30 16:31:42 +0100398static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300399get_modifier(char *modifier)
400{
401 if (!modifier)
402 return MODIFIER_SUPER;
403
404 if (!strcmp("ctrl", modifier))
405 return MODIFIER_CTRL;
406 else if (!strcmp("alt", modifier))
407 return MODIFIER_ALT;
408 else if (!strcmp("super", modifier))
409 return MODIFIER_SUPER;
410 else
411 return MODIFIER_SUPER;
412}
413
Juan Zhaoe10d2792012-04-25 19:09:52 +0800414static enum animation_type
415get_animation_type(char *animation)
416{
417 if (!animation)
418 return ANIMATION_NONE;
419
420 if (!strcmp("zoom", animation))
421 return ANIMATION_ZOOM;
422 else if (!strcmp("fade", animation))
423 return ANIMATION_FADE;
424 else
425 return ANIMATION_NONE;
426}
427
Alex Wu4539b082012-03-01 12:57:46 +0800428static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400429shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200430{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400431 struct weston_config_section *section;
432 int duration;
433 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200434
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400435 section = weston_config_get_section(shell->compositor->config,
436 "screensaver", NULL, NULL);
437 weston_config_section_get_string(section,
438 "path", &shell->screensaver.path, NULL);
439 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200440 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400441
442 section = weston_config_get_section(shell->compositor->config,
443 "shell", NULL, NULL);
444 weston_config_section_get_string(section,
445 "binding-modifier", &s, "super");
446 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200447 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400448 weston_config_section_get_string(section, "animation", &s, "none");
449 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200450 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400451 weston_config_section_get_uint(section, "num-workspaces",
452 &shell->workspaces.num,
453 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200454}
455
456static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200457focus_state_destroy(struct focus_state *state)
458{
459 wl_list_remove(&state->seat_destroy_listener.link);
460 wl_list_remove(&state->surface_destroy_listener.link);
461 free(state);
462}
463
464static void
465focus_state_seat_destroy(struct wl_listener *listener, void *data)
466{
467 struct focus_state *state = container_of(listener,
468 struct focus_state,
469 seat_destroy_listener);
470
471 wl_list_remove(&state->link);
472 focus_state_destroy(state);
473}
474
475static void
476focus_state_surface_destroy(struct wl_listener *listener, void *data)
477{
478 struct focus_state *state = container_of(listener,
479 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400480 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400481 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300482 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400483 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200484
Pekka Paalanen01388e22013-04-25 13:57:44 +0300485 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
486
Kristian Høgsberge3778222012-07-31 17:29:30 -0400487 next = NULL;
488 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300489 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400490 continue;
491
492 next = surface;
493 break;
494 }
495
Pekka Paalanen01388e22013-04-25 13:57:44 +0300496 /* if the focus was a sub-surface, activate its main surface */
497 if (main_surface != state->keyboard_focus)
498 next = main_surface;
499
Kristian Høgsberge3778222012-07-31 17:29:30 -0400500 if (next) {
501 shell = state->seat->compositor->shell_interface.shell;
502 activate(shell, next, state->seat);
503 } else {
504 wl_list_remove(&state->link);
505 focus_state_destroy(state);
506 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200507}
508
509static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400510focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200511{
Jonas Ådahl04769742012-06-13 00:01:24 +0200512 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200513
514 state = malloc(sizeof *state);
515 if (state == NULL)
516 return NULL;
517
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400518 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200519 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400520 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200521
522 state->seat_destroy_listener.notify = focus_state_seat_destroy;
523 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400524 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200525 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400526 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200527
528 return state;
529}
530
Jonas Ådahl8538b222012-08-29 22:13:03 +0200531static struct focus_state *
532ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
533{
534 struct workspace *ws = get_current_workspace(shell);
535 struct focus_state *state;
536
537 wl_list_for_each(state, &ws->focus_list, link)
538 if (state->seat == seat)
539 break;
540
541 if (&state->link == &ws->focus_list)
542 state = focus_state_create(seat, ws);
543
544 return state;
545}
546
Jonas Ådahl04769742012-06-13 00:01:24 +0200547static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400548restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200549{
550 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400551 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200552
553 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400554 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200555
Kristian Høgsberge3148752013-05-06 23:19:49 -0400556 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200557 }
558}
559
560static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200561replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
562 struct weston_seat *seat)
563{
564 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400565 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200566
567 wl_list_for_each(state, &ws->focus_list, link) {
568 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400569 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500570 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200571 return;
572 }
573 }
574}
575
576static void
577drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
578 struct weston_surface *surface)
579{
580 struct focus_state *state;
581
582 wl_list_for_each(state, &ws->focus_list, link)
583 if (state->keyboard_focus == surface)
584 state->keyboard_focus = NULL;
585}
586
587static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200588workspace_destroy(struct workspace *ws)
589{
Jonas Ådahl04769742012-06-13 00:01:24 +0200590 struct focus_state *state, *next;
591
592 wl_list_for_each_safe(state, next, &ws->focus_list, link)
593 focus_state_destroy(state);
594
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200595 free(ws);
596}
597
Jonas Ådahl04769742012-06-13 00:01:24 +0200598static void
599seat_destroyed(struct wl_listener *listener, void *data)
600{
601 struct weston_seat *seat = data;
602 struct focus_state *state, *next;
603 struct workspace *ws = container_of(listener,
604 struct workspace,
605 seat_destroyed_listener);
606
607 wl_list_for_each_safe(state, next, &ws->focus_list, link)
608 if (state->seat == seat)
609 wl_list_remove(&state->link);
610}
611
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200612static struct workspace *
613workspace_create(void)
614{
615 struct workspace *ws = malloc(sizeof *ws);
616 if (ws == NULL)
617 return NULL;
618
619 weston_layer_init(&ws->layer, NULL);
620
Jonas Ådahl04769742012-06-13 00:01:24 +0200621 wl_list_init(&ws->focus_list);
622 wl_list_init(&ws->seat_destroyed_listener.link);
623 ws->seat_destroyed_listener.notify = seat_destroyed;
624
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200625 return ws;
626}
627
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200628static int
629workspace_is_empty(struct workspace *ws)
630{
631 return wl_list_empty(&ws->layer.surface_list);
632}
633
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200634static struct workspace *
635get_workspace(struct desktop_shell *shell, unsigned int index)
636{
637 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200638 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200639 pws += index;
640 return *pws;
641}
642
643static struct workspace *
644get_current_workspace(struct desktop_shell *shell)
645{
646 return get_workspace(shell, shell->workspaces.current);
647}
648
649static void
650activate_workspace(struct desktop_shell *shell, unsigned int index)
651{
652 struct workspace *ws;
653
654 ws = get_workspace(shell, index);
655 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
656
657 shell->workspaces.current = index;
658}
659
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200660static unsigned int
661get_output_height(struct weston_output *output)
662{
663 return abs(output->region.extents.y1 - output->region.extents.y2);
664}
665
666static void
667surface_translate(struct weston_surface *surface, double d)
668{
669 struct shell_surface *shsurf = get_shell_surface(surface);
670 struct weston_transform *transform;
671
672 transform = &shsurf->workspace_transform;
673 if (wl_list_empty(&transform->link))
674 wl_list_insert(surface->geometry.transformation_list.prev,
675 &shsurf->workspace_transform.link);
676
677 weston_matrix_init(&shsurf->workspace_transform.matrix);
678 weston_matrix_translate(&shsurf->workspace_transform.matrix,
679 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200680 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200681}
682
683static void
684workspace_translate_out(struct workspace *ws, double fraction)
685{
686 struct weston_surface *surface;
687 unsigned int height;
688 double d;
689
690 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
691 height = get_output_height(surface->output);
692 d = height * fraction;
693
694 surface_translate(surface, d);
695 }
696}
697
698static void
699workspace_translate_in(struct workspace *ws, double fraction)
700{
701 struct weston_surface *surface;
702 unsigned int height;
703 double d;
704
705 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
706 height = get_output_height(surface->output);
707
708 if (fraction > 0)
709 d = -(height - height * fraction);
710 else
711 d = height + height * fraction;
712
713 surface_translate(surface, d);
714 }
715}
716
717static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200718broadcast_current_workspace_state(struct desktop_shell *shell)
719{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500720 struct wl_list *link;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200721
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500722 for (link = shell->workspaces.client_list.next;
723 link != &shell->workspaces.client_list;
724 link = link->next) {
725 workspace_manager_send_state(wl_resource_from_link(link),
Jonas Ådahle9d22502012-08-29 22:13:01 +0200726 shell->workspaces.current,
727 shell->workspaces.num);
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500728 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200729}
730
731static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200732reverse_workspace_change_animation(struct desktop_shell *shell,
733 unsigned int index,
734 struct workspace *from,
735 struct workspace *to)
736{
737 shell->workspaces.current = index;
738
739 shell->workspaces.anim_to = to;
740 shell->workspaces.anim_from = from;
741 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
742 shell->workspaces.anim_timestamp = 0;
743
Scott Moreau4272e632012-08-13 09:58:41 -0600744 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200745}
746
747static void
748workspace_deactivate_transforms(struct workspace *ws)
749{
750 struct weston_surface *surface;
751 struct shell_surface *shsurf;
752
753 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
754 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200755 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
756 wl_list_remove(&shsurf->workspace_transform.link);
757 wl_list_init(&shsurf->workspace_transform.link);
758 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200759 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200760 }
761}
762
763static void
764finish_workspace_change_animation(struct desktop_shell *shell,
765 struct workspace *from,
766 struct workspace *to)
767{
Scott Moreau4272e632012-08-13 09:58:41 -0600768 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200769
770 wl_list_remove(&shell->workspaces.animation.link);
771 workspace_deactivate_transforms(from);
772 workspace_deactivate_transforms(to);
773 shell->workspaces.anim_to = NULL;
774
775 wl_list_remove(&shell->workspaces.anim_from->layer.link);
776}
777
778static void
779animate_workspace_change_frame(struct weston_animation *animation,
780 struct weston_output *output, uint32_t msecs)
781{
782 struct desktop_shell *shell =
783 container_of(animation, struct desktop_shell,
784 workspaces.animation);
785 struct workspace *from = shell->workspaces.anim_from;
786 struct workspace *to = shell->workspaces.anim_to;
787 uint32_t t;
788 double x, y;
789
790 if (workspace_is_empty(from) && workspace_is_empty(to)) {
791 finish_workspace_change_animation(shell, from, to);
792 return;
793 }
794
795 if (shell->workspaces.anim_timestamp == 0) {
796 if (shell->workspaces.anim_current == 0.0)
797 shell->workspaces.anim_timestamp = msecs;
798 else
799 shell->workspaces.anim_timestamp =
800 msecs -
801 /* Invers of movement function 'y' below. */
802 (asin(1.0 - shell->workspaces.anim_current) *
803 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
804 M_2_PI);
805 }
806
807 t = msecs - shell->workspaces.anim_timestamp;
808
809 /*
810 * x = [0, π/2]
811 * y(x) = sin(x)
812 */
813 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
814 y = sin(x);
815
816 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600817 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200818
819 workspace_translate_out(from, shell->workspaces.anim_dir * y);
820 workspace_translate_in(to, shell->workspaces.anim_dir * y);
821 shell->workspaces.anim_current = y;
822
Scott Moreau4272e632012-08-13 09:58:41 -0600823 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200824 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200825 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200826 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200827}
828
829static void
830animate_workspace_change(struct desktop_shell *shell,
831 unsigned int index,
832 struct workspace *from,
833 struct workspace *to)
834{
835 struct weston_output *output;
836
837 int dir;
838
839 if (index > shell->workspaces.current)
840 dir = -1;
841 else
842 dir = 1;
843
844 shell->workspaces.current = index;
845
846 shell->workspaces.anim_dir = dir;
847 shell->workspaces.anim_from = from;
848 shell->workspaces.anim_to = to;
849 shell->workspaces.anim_current = 0.0;
850 shell->workspaces.anim_timestamp = 0;
851
852 output = container_of(shell->compositor->output_list.next,
853 struct weston_output, link);
854 wl_list_insert(&output->animation_list,
855 &shell->workspaces.animation.link);
856
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200857 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200858
859 workspace_translate_in(to, 0);
860
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400861 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200862
Scott Moreau4272e632012-08-13 09:58:41 -0600863 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200864}
865
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200866static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200867update_workspace(struct desktop_shell *shell, unsigned int index,
868 struct workspace *from, struct workspace *to)
869{
870 shell->workspaces.current = index;
871 wl_list_insert(&from->layer.link, &to->layer.link);
872 wl_list_remove(&from->layer.link);
873}
874
875static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200876change_workspace(struct desktop_shell *shell, unsigned int index)
877{
878 struct workspace *from;
879 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200880
881 if (index == shell->workspaces.current)
882 return;
883
884 /* Don't change workspace when there is any fullscreen surfaces. */
885 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
886 return;
887
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200888 from = get_current_workspace(shell);
889 to = get_workspace(shell, index);
890
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200891 if (shell->workspaces.anim_from == to &&
892 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200893 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200894 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200895 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200896 return;
897 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200898
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200899 if (shell->workspaces.anim_to != NULL)
900 finish_workspace_change_animation(shell,
901 shell->workspaces.anim_from,
902 shell->workspaces.anim_to);
903
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200904 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200905
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200906 if (workspace_is_empty(to) && workspace_is_empty(from))
907 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200908 else
909 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200910
911 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200912}
913
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200914static bool
915workspace_has_only(struct workspace *ws, struct weston_surface *surface)
916{
917 struct wl_list *list = &ws->layer.surface_list;
918 struct wl_list *e;
919
920 if (wl_list_empty(list))
921 return false;
922
923 e = list->next;
924
925 if (e->next != list)
926 return false;
927
928 return container_of(e, struct weston_surface, layer_link) == surface;
929}
930
931static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200932move_surface_to_workspace(struct desktop_shell *shell,
933 struct weston_surface *surface,
934 uint32_t workspace)
935{
936 struct workspace *from;
937 struct workspace *to;
938 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300939 struct weston_surface *focus;
940
941 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200942
943 if (workspace == shell->workspaces.current)
944 return;
945
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200946 if (workspace >= shell->workspaces.num)
947 workspace = shell->workspaces.num - 1;
948
Jonas Ådahle9d22502012-08-29 22:13:01 +0200949 from = get_current_workspace(shell);
950 to = get_workspace(shell, workspace);
951
952 wl_list_remove(&surface->layer_link);
953 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
954
955 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300956 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
957 if (!seat->keyboard)
958 continue;
959
960 focus = weston_surface_get_main_surface(seat->keyboard->focus);
961 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400962 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300963 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200964
965 weston_surface_damage_below(surface);
966}
967
968static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200969take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400970 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200971 unsigned int index)
972{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300973 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200974 struct shell_surface *shsurf;
975 struct workspace *from;
976 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200977 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200978
Pekka Paalanen01388e22013-04-25 13:57:44 +0300979 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200980 if (surface == NULL ||
981 index == shell->workspaces.current)
982 return;
983
984 from = get_current_workspace(shell);
985 to = get_workspace(shell, index);
986
987 wl_list_remove(&surface->layer_link);
988 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
989
Jonas Ådahle9d22502012-08-29 22:13:01 +0200990 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200991 drop_focus_state(shell, from, surface);
992
993 if (shell->workspaces.anim_from == to &&
994 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200995 wl_list_remove(&to->layer.link);
996 wl_list_insert(from->layer.link.prev, &to->layer.link);
997
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200998 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200999 broadcast_current_workspace_state(shell);
1000
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001001 return;
1002 }
1003
1004 if (shell->workspaces.anim_to != NULL)
1005 finish_workspace_change_animation(shell,
1006 shell->workspaces.anim_from,
1007 shell->workspaces.anim_to);
1008
1009 if (workspace_is_empty(from) &&
1010 workspace_has_only(to, surface))
1011 update_workspace(shell, index, from, to);
1012 else {
1013 shsurf = get_shell_surface(surface);
1014 if (wl_list_empty(&shsurf->workspace_transform.link))
1015 wl_list_insert(&shell->workspaces.anim_sticky_list,
1016 &shsurf->workspace_transform.link);
1017
1018 animate_workspace_change(shell, index, from, to);
1019 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001020
1021 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001022
1023 state = ensure_focus_state(shell, seat);
1024 if (state != NULL)
1025 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001026}
1027
1028static void
1029workspace_manager_move_surface(struct wl_client *client,
1030 struct wl_resource *resource,
1031 struct wl_resource *surface_resource,
1032 uint32_t workspace)
1033{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001034 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001035 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001036 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001037 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001038
Pekka Paalanen01388e22013-04-25 13:57:44 +03001039 main_surface = weston_surface_get_main_surface(surface);
1040 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001041}
1042
1043static const struct workspace_manager_interface workspace_manager_implementation = {
1044 workspace_manager_move_surface,
1045};
1046
1047static void
1048unbind_resource(struct wl_resource *resource)
1049{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001050 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001051}
1052
1053static void
1054bind_workspace_manager(struct wl_client *client,
1055 void *data, uint32_t version, uint32_t id)
1056{
1057 struct desktop_shell *shell = data;
1058 struct wl_resource *resource;
1059
Jason Ekstranda85118c2013-06-27 20:17:02 -05001060 resource = wl_resource_create(client,
1061 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001062
1063 if (resource == NULL) {
1064 weston_log("couldn't add workspace manager object");
1065 return;
1066 }
1067
Jason Ekstranda85118c2013-06-27 20:17:02 -05001068 wl_resource_set_implementation(resource,
1069 &workspace_manager_implementation,
1070 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001071 wl_list_insert(&shell->workspaces.client_list,
1072 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001073
1074 workspace_manager_send_state(resource,
1075 shell->workspaces.current,
1076 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001077}
1078
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001079static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001080touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1081 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1082{
1083}
1084
1085static void
1086touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1087{
1088 struct shell_touch_grab *shell_grab = container_of(grab,
1089 struct shell_touch_grab,
1090 grab);
1091 shell_touch_grab_end(shell_grab);
1092}
1093
1094static void
1095touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1096 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1097{
1098 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1099 struct shell_surface *shsurf = move->base.shsurf;
1100 struct weston_surface *es;
1101 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1102 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1103
1104 if (!shsurf)
1105 return;
1106
1107 es = shsurf->surface;
1108
1109 weston_surface_configure(es, dx, dy,
1110 es->geometry.width, es->geometry.height);
1111
1112 weston_compositor_schedule_repaint(es->compositor);
1113}
1114
1115static const struct weston_touch_grab_interface touch_move_grab_interface = {
1116 touch_move_grab_down,
1117 touch_move_grab_up,
1118 touch_move_grab_motion,
1119};
1120
1121static int
1122surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1123{
1124 struct weston_touch_move_grab *move;
1125
1126 if (!shsurf)
1127 return -1;
1128
1129 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1130 return 0;
1131
1132 move = malloc(sizeof *move);
1133 if (!move)
1134 return -1;
1135
1136 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1137 seat->touch->grab_x;
1138 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1139 seat->touch->grab_y;
1140
1141 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1142 seat->touch);
1143
1144 return 0;
1145}
1146
1147static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001148noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001149{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001150}
1151
1152static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001153move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001154{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001155 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001156 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001157 struct shell_surface *shsurf = move->base.shsurf;
1158 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001159 int dx = wl_fixed_to_int(pointer->x + move->dx);
1160 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001161
1162 if (!shsurf)
1163 return;
1164
1165 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001166
Daniel Stone103db7f2012-05-08 17:17:55 +01001167 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001168 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001169
1170 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001171}
1172
1173static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001174move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001175 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001176{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001177 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1178 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001179 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001180 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001181
Daniel Stone4dbadb12012-05-30 16:31:51 +01001182 if (pointer->button_count == 0 &&
1183 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001184 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001185 free(grab);
1186 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001187}
1188
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001189static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001190 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001191 move_grab_motion,
1192 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001193};
1194
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001195static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001196surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001197{
1198 struct weston_move_grab *move;
1199
1200 if (!shsurf)
1201 return -1;
1202
1203 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1204 return 0;
1205
1206 move = malloc(sizeof *move);
1207 if (!move)
1208 return -1;
1209
1210 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001211 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001212 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001213 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001214
1215 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001216 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001217
1218 return 0;
1219}
1220
1221static void
1222shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1223 struct wl_resource *seat_resource, uint32_t serial)
1224{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001225 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001226 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001227 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001228
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001229 if (seat->pointer &&
1230 seat->pointer->button_count > 0 &&
1231 seat->pointer->grab_serial == serial) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001232 surface = weston_surface_get_main_surface(seat->pointer->focus);
1233 if ((surface == shsurf->surface) &&
1234 (surface_move(shsurf, seat) < 0))
1235 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001236 } else if (seat->touch &&
1237 seat->touch->grab_serial == serial) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001238 surface = weston_surface_get_main_surface(seat->touch->focus);
1239 if ((surface == shsurf->surface) &&
1240 (surface_touch_move(shsurf, seat) < 0))
1241 wl_resource_post_no_memory(resource);
1242 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001243}
1244
1245struct weston_resize_grab {
1246 struct shell_grab base;
1247 uint32_t edges;
1248 int32_t width, height;
1249};
1250
1251static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001252resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001253{
1254 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001255 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001256 struct shell_surface *shsurf = resize->base.shsurf;
1257 int32_t width, height;
1258 wl_fixed_t from_x, from_y;
1259 wl_fixed_t to_x, to_y;
1260
1261 if (!shsurf)
1262 return;
1263
1264 weston_surface_from_global_fixed(shsurf->surface,
1265 pointer->grab_x, pointer->grab_y,
1266 &from_x, &from_y);
1267 weston_surface_from_global_fixed(shsurf->surface,
1268 pointer->x, pointer->y, &to_x, &to_y);
1269
1270 width = resize->width;
1271 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1272 width += wl_fixed_to_int(from_x - to_x);
1273 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1274 width += wl_fixed_to_int(to_x - from_x);
1275 }
1276
1277 height = resize->height;
1278 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1279 height += wl_fixed_to_int(from_y - to_y);
1280 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1281 height += wl_fixed_to_int(to_y - from_y);
1282 }
1283
1284 shsurf->client->send_configure(shsurf->surface,
1285 resize->edges, width, height);
1286}
1287
1288static void
1289send_configure(struct weston_surface *surface,
1290 uint32_t edges, int32_t width, int32_t height)
1291{
1292 struct shell_surface *shsurf = get_shell_surface(surface);
1293
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001294 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001295 edges, width, height);
1296}
1297
1298static const struct weston_shell_client shell_client = {
1299 send_configure
1300};
1301
1302static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001303resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001304 uint32_t time, uint32_t button, uint32_t state_w)
1305{
1306 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001307 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001308 enum wl_pointer_button_state state = state_w;
1309
1310 if (pointer->button_count == 0 &&
1311 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1312 shell_grab_end(&resize->base);
1313 free(grab);
1314 }
1315}
1316
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001317static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001318 noop_grab_focus,
1319 resize_grab_motion,
1320 resize_grab_button,
1321};
1322
Giulio Camuffob8366642013-04-25 13:57:46 +03001323/*
1324 * Returns the bounding box of a surface and all its sub-surfaces,
1325 * in the surface coordinates system. */
1326static void
1327surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1328 int32_t *y, int32_t *w, int32_t *h) {
1329 pixman_region32_t region;
1330 pixman_box32_t *box;
1331 struct weston_subsurface *subsurface;
1332
1333 pixman_region32_init_rect(&region, 0, 0,
1334 surface->geometry.width,
1335 surface->geometry.height);
1336
1337 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1338 pixman_region32_union_rect(&region, &region,
1339 subsurface->position.x,
1340 subsurface->position.y,
1341 subsurface->surface->geometry.width,
1342 subsurface->surface->geometry.height);
1343 }
1344
1345 box = pixman_region32_extents(&region);
1346 if (x)
1347 *x = box->x1;
1348 if (y)
1349 *y = box->y1;
1350 if (w)
1351 *w = box->x2 - box->x1;
1352 if (h)
1353 *h = box->y2 - box->y1;
1354
1355 pixman_region32_fini(&region);
1356}
1357
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001358static int
1359surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001360 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001361{
1362 struct weston_resize_grab *resize;
1363
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001364 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1365 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001366 return 0;
1367
1368 if (edges == 0 || edges > 15 ||
1369 (edges & 3) == 3 || (edges & 12) == 12)
1370 return 0;
1371
1372 resize = malloc(sizeof *resize);
1373 if (!resize)
1374 return -1;
1375
1376 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001377 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1378 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001379
1380 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001381 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001382
1383 return 0;
1384}
1385
1386static void
1387shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1388 struct wl_resource *seat_resource, uint32_t serial,
1389 uint32_t edges)
1390{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001391 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001392 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001393 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001394
1395 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1396 return;
1397
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001398 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001399 if (seat->pointer->button_count == 0 ||
1400 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001401 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001402 return;
1403
Kristian Høgsberge3148752013-05-06 23:19:49 -04001404 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001405 wl_resource_post_no_memory(resource);
1406}
1407
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001408static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001409end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1410
1411static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001412busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001413{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001414 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001415 struct weston_pointer *pointer = base->pointer;
1416 struct weston_surface *surface;
1417 wl_fixed_t sx, sy;
1418
1419 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1420 pointer->x, pointer->y,
1421 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001422
Kristian Høgsberg67800732013-07-04 01:12:17 -04001423 if (!grab->shsurf || grab->shsurf->surface != surface)
1424 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001425}
1426
1427static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001428busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001429{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001430}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001431
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001432static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001433busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001434 uint32_t time, uint32_t button, uint32_t state)
1435{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001436 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001437 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001438 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001439
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001440 if (shsurf && button == BTN_LEFT && state) {
1441 activate(shsurf->shell, shsurf->surface, seat);
1442 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001443 } else if (shsurf && button == BTN_RIGHT && state) {
1444 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001445 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001446 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001447}
1448
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001449static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001450 busy_cursor_grab_focus,
1451 busy_cursor_grab_motion,
1452 busy_cursor_grab_button,
1453};
1454
1455static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001456set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001457{
1458 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001459
1460 grab = malloc(sizeof *grab);
1461 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001462 return;
1463
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001464 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1465 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001466}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001467
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001468static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001469end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001470{
1471 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1472
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001473 if (grab->grab.interface == &busy_cursor_grab_interface &&
1474 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001475 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001476 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001477 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001478}
1479
Scott Moreau9521d5e2012-04-19 13:06:17 -06001480static void
1481ping_timer_destroy(struct shell_surface *shsurf)
1482{
1483 if (!shsurf || !shsurf->ping_timer)
1484 return;
1485
1486 if (shsurf->ping_timer->source)
1487 wl_event_source_remove(shsurf->ping_timer->source);
1488
1489 free(shsurf->ping_timer);
1490 shsurf->ping_timer = NULL;
1491}
1492
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001493static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001494ping_timeout_handler(void *data)
1495{
1496 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001497 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001498
Scott Moreau9521d5e2012-04-19 13:06:17 -06001499 /* Client is not responding */
1500 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001501
1502 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001503 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001504 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001505
1506 return 1;
1507}
1508
1509static void
1510ping_handler(struct weston_surface *surface, uint32_t serial)
1511{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001512 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001513 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001514 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001515
1516 if (!shsurf)
1517 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001518 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001519 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001520
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001521 if (shsurf->surface == shsurf->shell->grab_surface)
1522 return;
1523
Scott Moreauff1db4a2012-04-17 19:06:18 -06001524 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001525 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001526 if (!shsurf->ping_timer)
1527 return;
1528
1529 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001530 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1531 shsurf->ping_timer->source =
1532 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1533 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1534
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001535 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001536 }
1537}
1538
1539static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001540handle_pointer_focus(struct wl_listener *listener, void *data)
1541{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001542 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001543 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001544 struct weston_compositor *compositor;
1545 struct shell_surface *shsurf;
1546 uint32_t serial;
1547
1548 if (!surface)
1549 return;
1550
1551 compositor = surface->compositor;
1552 shsurf = get_shell_surface(surface);
1553
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001554 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001555 set_busy_cursor(shsurf, pointer);
1556 } else {
1557 serial = wl_display_next_serial(compositor->wl_display);
1558 ping_handler(surface, serial);
1559 }
1560}
1561
1562static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001563create_pointer_focus_listener(struct weston_seat *seat)
1564{
1565 struct wl_listener *listener;
1566
Kristian Høgsberge3148752013-05-06 23:19:49 -04001567 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001568 return;
1569
1570 listener = malloc(sizeof *listener);
1571 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001572 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001573}
1574
1575static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001576shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1577 uint32_t serial)
1578{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001579 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001580 struct weston_seat *seat;
1581 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001582
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001583 if (shsurf->ping_timer == NULL)
1584 /* Just ignore unsolicited pong. */
1585 return;
1586
Scott Moreauff1db4a2012-04-17 19:06:18 -06001587 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001588 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001589 wl_list_for_each(seat, &ec->seat_list, link) {
1590 if(seat->pointer)
1591 end_busy_cursor(shsurf, seat->pointer);
1592 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001593 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001594 }
1595}
1596
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001597static void
1598shell_surface_set_title(struct wl_client *client,
1599 struct wl_resource *resource, const char *title)
1600{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001601 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001602
1603 free(shsurf->title);
1604 shsurf->title = strdup(title);
1605}
1606
1607static void
1608shell_surface_set_class(struct wl_client *client,
1609 struct wl_resource *resource, const char *class)
1610{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001611 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001612
1613 free(shsurf->class);
1614 shsurf->class = strdup(class);
1615}
1616
Juan Zhao96879df2012-02-07 08:45:41 +08001617static struct weston_output *
1618get_default_output(struct weston_compositor *compositor)
1619{
1620 return container_of(compositor->output_list.next,
1621 struct weston_output, link);
1622}
1623
Alex Wu4539b082012-03-01 12:57:46 +08001624static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001625restore_output_mode(struct weston_output *output)
1626{
1627 if (output->current != output->origin ||
1628 (int32_t)output->scale != output->origin_scale)
1629 weston_output_switch_mode(output,
1630 output->origin,
1631 output->origin_scale);
1632}
1633
1634static void
1635restore_all_output_modes(struct weston_compositor *compositor)
1636{
1637 struct weston_output *output;
1638
1639 wl_list_for_each(output, &compositor->output_list, link)
1640 restore_output_mode(output);
1641}
1642
1643static void
Alex Wu4539b082012-03-01 12:57:46 +08001644shell_unset_fullscreen(struct shell_surface *shsurf)
1645{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001646 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001647 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001648 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1649 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001650 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001651 }
Alex Wu4539b082012-03-01 12:57:46 +08001652 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1653 shsurf->fullscreen.framerate = 0;
1654 wl_list_remove(&shsurf->fullscreen.transform.link);
1655 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001656 if (shsurf->fullscreen.black_surface)
1657 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001658 shsurf->fullscreen.black_surface = NULL;
1659 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001660 weston_surface_set_position(shsurf->surface,
1661 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001662 if (shsurf->saved_rotation_valid) {
1663 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1664 &shsurf->rotation.transform.link);
1665 shsurf->saved_rotation_valid = false;
1666 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001667
1668 ws = get_current_workspace(shsurf->shell);
1669 wl_list_remove(&shsurf->surface->layer_link);
1670 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001671}
1672
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001673static void
1674shell_unset_maximized(struct shell_surface *shsurf)
1675{
1676 struct workspace *ws;
1677 /* undo all maximized things here */
1678 shsurf->output = get_default_output(shsurf->surface->compositor);
1679 weston_surface_set_position(shsurf->surface,
1680 shsurf->saved_x,
1681 shsurf->saved_y);
1682
1683 if (shsurf->saved_rotation_valid) {
1684 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1685 &shsurf->rotation.transform.link);
1686 shsurf->saved_rotation_valid = false;
1687 }
1688
1689 ws = get_current_workspace(shsurf->shell);
1690 wl_list_remove(&shsurf->surface->layer_link);
1691 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1692}
1693
Pekka Paalanen98262232011-12-01 10:42:22 +02001694static int
1695reset_shell_surface_type(struct shell_surface *surface)
1696{
1697 switch (surface->type) {
1698 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001699 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001700 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001701 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001702 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001703 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001704 case SHELL_SURFACE_NONE:
1705 case SHELL_SURFACE_TOPLEVEL:
1706 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001707 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001708 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001709 break;
1710 }
1711
1712 surface->type = SHELL_SURFACE_NONE;
1713 return 0;
1714}
1715
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001716static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001717set_surface_type(struct shell_surface *shsurf)
1718{
1719 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001720 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001721
1722 reset_shell_surface_type(shsurf);
1723
1724 shsurf->type = shsurf->next_type;
1725 shsurf->next_type = SHELL_SURFACE_NONE;
1726
1727 switch (shsurf->type) {
1728 case SHELL_SURFACE_TOPLEVEL:
1729 break;
1730 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001731 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001732 pes->geometry.x + shsurf->transient.x,
1733 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001734 break;
1735
1736 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001737 case SHELL_SURFACE_FULLSCREEN:
1738 shsurf->saved_x = surface->geometry.x;
1739 shsurf->saved_y = surface->geometry.y;
1740 shsurf->saved_position_valid = true;
1741
1742 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1743 wl_list_remove(&shsurf->rotation.transform.link);
1744 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001745 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001746 shsurf->saved_rotation_valid = true;
1747 }
1748 break;
1749
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001750 case SHELL_SURFACE_XWAYLAND:
1751 weston_surface_set_position(surface, shsurf->transient.x,
1752 shsurf->transient.y);
1753 break;
1754
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001755 default:
1756 break;
1757 }
1758}
1759
1760static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001761set_toplevel(struct shell_surface *shsurf)
1762{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001763 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001764}
1765
1766static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001767shell_surface_set_toplevel(struct wl_client *client,
1768 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001769{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001770 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001771
Tiago Vignattibc052c92012-04-19 16:18:18 +03001772 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001773}
1774
1775static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001776set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001777 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001778{
1779 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001780 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001781 shsurf->transient.x = x;
1782 shsurf->transient.y = y;
1783 shsurf->transient.flags = flags;
1784 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1785}
1786
1787static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001788shell_surface_set_transient(struct wl_client *client,
1789 struct wl_resource *resource,
1790 struct wl_resource *parent_resource,
1791 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001792{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001793 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001794 struct weston_surface *parent =
1795 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001796
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001797 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001798}
1799
Tiago Vignattibe143262012-04-16 17:31:41 +03001800static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001801shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001802{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001803 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001804}
1805
1806static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001807get_output_panel_height(struct desktop_shell *shell,
1808 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001809{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001810 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001811 int panel_height = 0;
1812
1813 if (!output)
1814 return 0;
1815
Juan Zhao4ab94682012-07-09 22:24:09 -07001816 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001817 if (surface->output == output) {
1818 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001819 break;
1820 }
1821 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001822
Juan Zhao96879df2012-02-07 08:45:41 +08001823 return panel_height;
1824}
1825
1826static void
1827shell_surface_set_maximized(struct wl_client *client,
1828 struct wl_resource *resource,
1829 struct wl_resource *output_resource )
1830{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001831 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001832 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001833 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001834 uint32_t edges = 0, panel_height = 0;
1835
1836 /* get the default output, if the client set it as NULL
1837 check whether the ouput is available */
1838 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001839 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001840 else if (es->output)
1841 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001842 else
1843 shsurf->output = get_default_output(es->compositor);
1844
Tiago Vignattibe143262012-04-16 17:31:41 +03001845 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001846 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001847 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001848
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001849 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001850 shsurf->output->width,
1851 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001852
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001853 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001854}
1855
Alex Wu21858432012-04-01 20:13:08 +08001856static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001857black_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 +08001858
Alex Wu4539b082012-03-01 12:57:46 +08001859static struct weston_surface *
1860create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001861 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001862 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001863{
1864 struct weston_surface *surface = NULL;
1865
1866 surface = weston_surface_create(ec);
1867 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001868 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001869 return NULL;
1870 }
1871
Alex Wu21858432012-04-01 20:13:08 +08001872 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001873 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001874 weston_surface_configure(surface, x, y, w, h);
1875 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001876 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001877 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001878 pixman_region32_fini(&surface->input);
1879 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001880
Alex Wu4539b082012-03-01 12:57:46 +08001881 return surface;
1882}
1883
1884/* Create black surface and append it to the associated fullscreen surface.
1885 * Handle size dismatch and positioning according to the method. */
1886static void
1887shell_configure_fullscreen(struct shell_surface *shsurf)
1888{
1889 struct weston_output *output = shsurf->fullscreen_output;
1890 struct weston_surface *surface = shsurf->surface;
1891 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001892 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001893 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001894
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001895 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1896 restore_output_mode(output);
1897
Alex Wu4539b082012-03-01 12:57:46 +08001898 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001899 shsurf->fullscreen.black_surface =
1900 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001901 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001902 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001903 output->width,
1904 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001905
1906 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1907 wl_list_insert(&surface->layer_link,
1908 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001909 shsurf->fullscreen.black_surface->output = output;
1910
Giulio Camuffob8366642013-04-25 13:57:46 +03001911 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1912 &surf_width, &surf_height);
1913
Alex Wu4539b082012-03-01 12:57:46 +08001914 switch (shsurf->fullscreen.type) {
1915 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001916 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001917 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001918 break;
1919 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001920 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001921 if (output->width == surf_width &&
1922 output->height == surf_height) {
1923 weston_surface_set_position(surface, output->x - surf_x,
1924 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001925 break;
1926 }
1927
Alex Wu4539b082012-03-01 12:57:46 +08001928 matrix = &shsurf->fullscreen.transform.matrix;
1929 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001930
Scott Moreau1bad5db2012-08-18 01:04:05 -06001931 output_aspect = (float) output->width /
1932 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001933 surface_aspect = (float) surface->geometry.width /
1934 (float) surface->geometry.height;
1935 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001936 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001937 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001938 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001939 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001940 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001941
Alex Wu4539b082012-03-01 12:57:46 +08001942 weston_matrix_scale(matrix, scale, scale, 1);
1943 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001944 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001945 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001946 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1947 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001948 weston_surface_set_position(surface, x, y);
1949
Alex Wu4539b082012-03-01 12:57:46 +08001950 break;
1951 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001952 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001953 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001954 surf_width * surface->buffer_scale,
1955 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001956 shsurf->fullscreen.framerate};
1957
Alexander Larsson355748e2013-05-28 16:23:38 +02001958 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001959 weston_surface_set_position(surface,
1960 output->x - surf_x,
1961 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001962 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001963 output->x - surf_x,
1964 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001965 output->width,
1966 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001967 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001968 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001969 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001970 center_on_output(surface, output);
1971 }
Alex Wubd3354b2012-04-17 17:20:49 +08001972 }
Alex Wu4539b082012-03-01 12:57:46 +08001973 break;
1974 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001975 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001976 break;
1977 default:
1978 break;
1979 }
1980}
1981
1982/* make the fullscreen and black surface at the top */
1983static void
1984shell_stack_fullscreen(struct shell_surface *shsurf)
1985{
Alex Wubd3354b2012-04-17 17:20:49 +08001986 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001987 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001988 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001989
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001990 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001991 wl_list_insert(&shell->fullscreen_layer.surface_list,
1992 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001993 weston_surface_damage(surface);
1994
1995 if (!shsurf->fullscreen.black_surface)
1996 shsurf->fullscreen.black_surface =
1997 create_black_surface(surface->compositor,
1998 surface,
1999 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002000 output->width,
2001 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002002
2003 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002004 wl_list_insert(&surface->layer_link,
2005 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002006 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08002007}
2008
2009static void
2010shell_map_fullscreen(struct shell_surface *shsurf)
2011{
Alex Wu4539b082012-03-01 12:57:46 +08002012 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002013 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002014}
2015
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002016static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002017set_fullscreen(struct shell_surface *shsurf,
2018 uint32_t method,
2019 uint32_t framerate,
2020 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002021{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002022 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002023
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002024 if (output)
2025 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002026 else if (es->output)
2027 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002028 else
2029 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002030
Alex Wu4539b082012-03-01 12:57:46 +08002031 shsurf->fullscreen_output = shsurf->output;
2032 shsurf->fullscreen.type = method;
2033 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002034 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002035
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002036 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002037 shsurf->output->width,
2038 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002039}
2040
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002041static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002042shell_surface_set_fullscreen(struct wl_client *client,
2043 struct wl_resource *resource,
2044 uint32_t method,
2045 uint32_t framerate,
2046 struct wl_resource *output_resource)
2047{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002048 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002049 struct weston_output *output;
2050
2051 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002052 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002053 else
2054 output = NULL;
2055
2056 set_fullscreen(shsurf, method, framerate, output);
2057}
2058
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002059static void
2060set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2061{
2062 /* XXX: using the same fields for transient type */
2063 shsurf->transient.x = x;
2064 shsurf->transient.y = y;
2065 shsurf->transient.flags = flags;
2066 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2067}
2068
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002069static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002070
2071static void
2072destroy_shell_seat(struct wl_listener *listener, void *data)
2073{
2074 struct shell_seat *shseat =
2075 container_of(listener,
2076 struct shell_seat, seat_destroy_listener);
2077 struct shell_surface *shsurf, *prev = NULL;
2078
2079 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002080 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002081 shseat->popup_grab.client = NULL;
2082
2083 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2084 shsurf->popup.shseat = NULL;
2085 if (prev) {
2086 wl_list_init(&prev->popup.grab_link);
2087 }
2088 prev = shsurf;
2089 }
2090 wl_list_init(&prev->popup.grab_link);
2091 }
2092
2093 wl_list_remove(&shseat->seat_destroy_listener.link);
2094 free(shseat);
2095}
2096
2097static struct shell_seat *
2098create_shell_seat(struct weston_seat *seat)
2099{
2100 struct shell_seat *shseat;
2101
2102 shseat = calloc(1, sizeof *shseat);
2103 if (!shseat) {
2104 weston_log("no memory to allocate shell seat\n");
2105 return NULL;
2106 }
2107
2108 shseat->seat = seat;
2109 wl_list_init(&shseat->popup_grab.surfaces_list);
2110
2111 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2112 wl_signal_add(&seat->destroy_signal,
2113 &shseat->seat_destroy_listener);
2114
2115 return shseat;
2116}
2117
2118static struct shell_seat *
2119get_shell_seat(struct weston_seat *seat)
2120{
2121 struct wl_listener *listener;
2122
2123 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2124 if (listener == NULL)
2125 return create_shell_seat(seat);
2126
2127 return container_of(listener,
2128 struct shell_seat, seat_destroy_listener);
2129}
2130
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002131static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002132popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002133{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002134 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002135 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002136 struct shell_seat *shseat =
2137 container_of(grab, struct shell_seat, popup_grab.grab);
2138 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002139 wl_fixed_t sx, sy;
2140
2141 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2142 pointer->x, pointer->y,
2143 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002144
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002145 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002146 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002147 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002148 weston_pointer_set_focus(pointer, NULL,
2149 wl_fixed_from_int(0),
2150 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002151 }
2152}
2153
2154static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002155popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002156{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002157 struct weston_pointer *pointer = grab->pointer;
2158 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002159
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002160 if (pointer->focus_resource) {
2161 weston_surface_from_global_fixed(pointer->focus,
2162 pointer->x, pointer->y,
2163 &sx, &sy);
2164 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2165 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002166}
2167
2168static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002169popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002170 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002171{
2172 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002173 struct shell_seat *shseat =
2174 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002175 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002176 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002177 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002178
Daniel Stone37816df2012-05-16 18:45:18 +01002179 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002180 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002181 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002182 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002183 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002184 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002185 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002186 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002187 }
2188
Daniel Stone4dbadb12012-05-30 16:31:51 +01002189 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002190 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002191}
2192
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002193static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002194 popup_grab_focus,
2195 popup_grab_motion,
2196 popup_grab_button,
2197};
2198
2199static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002200popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002201{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002202 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002203 struct shell_seat *shseat =
2204 container_of(grab, struct shell_seat, popup_grab.grab);
2205 struct shell_surface *shsurf;
2206 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002207
2208 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002209 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002210 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002211 shseat->popup_grab.grab.interface = NULL;
2212 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002213 /* Send the popup_done event to all the popups open */
2214 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002215 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002216 shsurf->popup.shseat = NULL;
2217 if (prev) {
2218 wl_list_init(&prev->popup.grab_link);
2219 }
2220 prev = shsurf;
2221 }
2222 wl_list_init(&prev->popup.grab_link);
2223 wl_list_init(&shseat->popup_grab.surfaces_list);
2224 }
2225}
2226
2227static void
2228add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2229{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002230 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002231
2232 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002233 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002234 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002235 /* We must make sure here that this popup was opened after
2236 * a mouse press, and not just by moving around with other
2237 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002238 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002239 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002240
Rob Bradforddfe31052013-06-26 19:49:11 +01002241 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002242 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002243 } else {
2244 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002245 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002246}
2247
2248static void
2249remove_popup_grab(struct shell_surface *shsurf)
2250{
2251 struct shell_seat *shseat = shsurf->popup.shseat;
2252
2253 wl_list_remove(&shsurf->popup.grab_link);
2254 wl_list_init(&shsurf->popup.grab_link);
2255 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002256 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002257 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002258 }
2259}
2260
2261static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002262shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002263{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002264 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002265 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002266 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002267
2268 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002269
Pekka Paalanen483243f2013-03-08 14:56:50 +02002270 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002271 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2272 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002273
Kristian Høgsberge3148752013-05-06 23:19:49 -04002274 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002275 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002276 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002277 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002278 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002279 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002280}
2281
2282static void
2283shell_surface_set_popup(struct wl_client *client,
2284 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002285 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002286 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002287 struct wl_resource *parent_resource,
2288 int32_t x, int32_t y, uint32_t flags)
2289{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002290 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002291
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002292 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002293 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002294 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002295 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002296 shsurf->popup.x = x;
2297 shsurf->popup.y = y;
2298}
2299
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002300static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002301 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002302 shell_surface_move,
2303 shell_surface_resize,
2304 shell_surface_set_toplevel,
2305 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002306 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002307 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002308 shell_surface_set_maximized,
2309 shell_surface_set_title,
2310 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002311};
2312
2313static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002314destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002315{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002316 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2317
Giulio Camuffo5085a752013-03-25 21:42:45 +01002318 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2319 remove_popup_grab(shsurf);
2320 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002321
Alex Wubd3354b2012-04-17 17:20:49 +08002322 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002323 shell_surface_is_top_fullscreen(shsurf))
2324 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002325
Alex Wuaa08e2d2012-03-05 11:01:40 +08002326 if (shsurf->fullscreen.black_surface)
2327 weston_surface_destroy(shsurf->fullscreen.black_surface);
2328
Alex Wubd3354b2012-04-17 17:20:49 +08002329 /* As destroy_resource() use wl_list_for_each_safe(),
2330 * we can always remove the listener.
2331 */
2332 wl_list_remove(&shsurf->surface_destroy_listener.link);
2333 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002334 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002335 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002336
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002337 wl_list_remove(&shsurf->link);
2338 free(shsurf);
2339}
2340
2341static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002342shell_destroy_shell_surface(struct wl_resource *resource)
2343{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002344 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002345
2346 destroy_shell_surface(shsurf);
2347}
2348
2349static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002350shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002351{
2352 struct shell_surface *shsurf = container_of(listener,
2353 struct shell_surface,
2354 surface_destroy_listener);
2355
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002356 if (shsurf->resource)
2357 wl_resource_destroy(shsurf->resource);
2358 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002359 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002360}
2361
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002362static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002363shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002364
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002365static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002366get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002367{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002368 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002369 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002370 else
2371 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002372}
2373
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002374static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002375create_shell_surface(void *shell, struct weston_surface *surface,
2376 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002377{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002378 struct shell_surface *shsurf;
2379
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002380 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002381 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002382 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002383 }
2384
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002385 shsurf = calloc(1, sizeof *shsurf);
2386 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002387 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002388 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002389 }
2390
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002391 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002392 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002393
Tiago Vignattibc052c92012-04-19 16:18:18 +03002394 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002395 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002396 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002397 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002398 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002399 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2400 shsurf->fullscreen.framerate = 0;
2401 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002402 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002403 wl_list_init(&shsurf->fullscreen.transform.link);
2404
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002405 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002406 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002407 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002408 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002409
2410 /* init link so its safe to always remove it in destroy_shell_surface */
2411 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002412 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002413
Pekka Paalanen460099f2012-01-20 16:48:25 +02002414 /* empty when not in use */
2415 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002416 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002417
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002418 wl_list_init(&shsurf->workspace_transform.link);
2419
Pekka Paalanen98262232011-12-01 10:42:22 +02002420 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002421 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002422
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002423 shsurf->client = client;
2424
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002425 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002426}
2427
2428static void
2429shell_get_shell_surface(struct wl_client *client,
2430 struct wl_resource *resource,
2431 uint32_t id,
2432 struct wl_resource *surface_resource)
2433{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002434 struct weston_surface *surface =
2435 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002436 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002437 struct shell_surface *shsurf;
2438
2439 if (get_shell_surface(surface)) {
2440 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002441 WL_DISPLAY_ERROR_INVALID_OBJECT,
2442 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002443 return;
2444 }
2445
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002446 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002447 if (!shsurf) {
2448 wl_resource_post_error(surface_resource,
2449 WL_DISPLAY_ERROR_INVALID_OBJECT,
2450 "surface->configure already set");
2451 return;
2452 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002453
Jason Ekstranda85118c2013-06-27 20:17:02 -05002454 shsurf->resource =
2455 wl_resource_create(client,
2456 &wl_shell_surface_interface, 1, id);
2457 wl_resource_set_implementation(shsurf->resource,
2458 &shell_surface_implementation,
2459 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002460}
2461
2462static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002463 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002464};
2465
Kristian Høgsberg07937562011-04-12 17:25:42 -04002466static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002467shell_fade(struct desktop_shell *shell, enum fade_type type);
2468
2469static int
2470screensaver_timeout(void *data)
2471{
2472 struct desktop_shell *shell = data;
2473
2474 shell_fade(shell, FADE_OUT);
2475
2476 return 1;
2477}
2478
2479static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002480handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002481{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002482 struct desktop_shell *shell =
2483 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002484
Pekka Paalanen18027e52011-12-02 16:31:49 +02002485 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002486
2487 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002488 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002489}
2490
2491static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002492launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002493{
2494 if (shell->screensaver.binding)
2495 return;
2496
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002497 if (!shell->screensaver.path) {
2498 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002499 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002500 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002501
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002502 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002503 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002504 return;
2505 }
2506
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002507 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002508 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002509 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002510 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002511}
2512
2513static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002514terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002515{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002516 if (shell->screensaver.process.pid == 0)
2517 return;
2518
2519 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002520}
2521
2522static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002523configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002524{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002525 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002526
Giulio Camuffo184df502013-02-21 11:29:21 +01002527 if (width == 0)
2528 return;
2529
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002530 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2531 if (s->output == es->output && s != es) {
2532 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002533 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002534 }
2535 }
2536
Giulio Camuffo184df502013-02-21 11:29:21 +01002537 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002538
2539 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002540 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002541 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002542 }
2543}
2544
2545static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002546background_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 -04002547{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002548 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002549
Giulio Camuffo184df502013-02-21 11:29:21 +01002550 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002551}
2552
2553static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002554desktop_shell_set_background(struct wl_client *client,
2555 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002556 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002557 struct wl_resource *surface_resource)
2558{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002559 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002560 struct weston_surface *surface =
2561 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002562
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002563 if (surface->configure) {
2564 wl_resource_post_error(surface_resource,
2565 WL_DISPLAY_ERROR_INVALID_OBJECT,
2566 "surface role already assigned");
2567 return;
2568 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002569
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002570 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002571 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002572 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002573 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002574 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002575 surface->output->width,
2576 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002577}
2578
2579static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002580panel_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 -04002581{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002582 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002583
Giulio Camuffo184df502013-02-21 11:29:21 +01002584 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002585}
2586
2587static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002588desktop_shell_set_panel(struct wl_client *client,
2589 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002590 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002591 struct wl_resource *surface_resource)
2592{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002593 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002594 struct weston_surface *surface =
2595 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002596
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002597 if (surface->configure) {
2598 wl_resource_post_error(surface_resource,
2599 WL_DISPLAY_ERROR_INVALID_OBJECT,
2600 "surface role already assigned");
2601 return;
2602 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002603
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002604 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002605 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002606 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002607 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002608 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002609 surface->output->width,
2610 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002611}
2612
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002613static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002614lock_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 -04002615{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002616 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002617
Giulio Camuffo184df502013-02-21 11:29:21 +01002618 if (width == 0)
2619 return;
2620
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002621 surface->geometry.width = width;
2622 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002623 center_on_output(surface, get_default_output(shell->compositor));
2624
2625 if (!weston_surface_is_mapped(surface)) {
2626 wl_list_insert(&shell->lock_layer.surface_list,
2627 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002628 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002629 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002630 }
2631}
2632
2633static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002634handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002635{
Tiago Vignattibe143262012-04-16 17:31:41 +03002636 struct desktop_shell *shell =
2637 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002638
Martin Minarik6d118362012-06-07 18:01:59 +02002639 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002640 shell->lock_surface = NULL;
2641}
2642
2643static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002644desktop_shell_set_lock_surface(struct wl_client *client,
2645 struct wl_resource *resource,
2646 struct wl_resource *surface_resource)
2647{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002648 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002649 struct weston_surface *surface =
2650 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002651
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002652 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002653
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002654 if (!shell->locked)
2655 return;
2656
Pekka Paalanen98262232011-12-01 10:42:22 +02002657 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002658
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002659 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002660 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002661 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002662
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002663 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002664 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002665}
2666
2667static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002668resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002669{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002670 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002671
Pekka Paalanen77346a62011-11-30 16:26:35 +02002672 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002673
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002674 wl_list_remove(&shell->lock_layer.link);
2675 wl_list_insert(&shell->compositor->cursor_layer.link,
2676 &shell->fullscreen_layer.link);
2677 wl_list_insert(&shell->fullscreen_layer.link,
2678 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002679 if (shell->showing_input_panels) {
2680 wl_list_insert(&shell->panel_layer.link,
2681 &shell->input_panel_layer.link);
2682 wl_list_insert(&shell->input_panel_layer.link,
2683 &ws->layer.link);
2684 } else {
2685 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2686 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002687
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002688 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002689
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002690 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002691 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002692 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002693}
2694
2695static void
2696desktop_shell_unlock(struct wl_client *client,
2697 struct wl_resource *resource)
2698{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002699 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002700
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002701 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002702
2703 if (shell->locked)
2704 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002705}
2706
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002707static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002708desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002709 struct wl_resource *resource,
2710 struct wl_resource *surface_resource)
2711{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002712 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002713
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002714 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002715}
2716
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002717static void
2718desktop_shell_desktop_ready(struct wl_client *client,
2719 struct wl_resource *resource)
2720{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002721 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002722
2723 shell_fade_startup(shell);
2724}
2725
Kristian Høgsberg75840622011-09-06 13:48:16 -04002726static const struct desktop_shell_interface desktop_shell_implementation = {
2727 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002728 desktop_shell_set_panel,
2729 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002730 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002731 desktop_shell_set_grab_surface,
2732 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002733};
2734
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002735static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002736get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002737{
2738 struct shell_surface *shsurf;
2739
2740 shsurf = get_shell_surface(surface);
2741 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002742 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002743 return shsurf->type;
2744}
2745
Kristian Høgsberg75840622011-09-06 13:48:16 -04002746static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002747move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002748{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002749 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002750 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002751 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002752 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002753
Pekka Paalanen01388e22013-04-25 13:57:44 +03002754 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002755 if (surface == NULL)
2756 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002757
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002758 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002759 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2760 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002761 return;
2762
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002763 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002764}
2765
2766static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002767resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002768{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002769 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002770 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002771 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002772 uint32_t edges = 0;
2773 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002774 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002775
Pekka Paalanen01388e22013-04-25 13:57:44 +03002776 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002777 if (surface == NULL)
2778 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002779
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002780 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002781 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2782 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002783 return;
2784
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002785 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002786 wl_fixed_to_int(seat->pointer->grab_x),
2787 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002788 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002789
Pekka Paalanen60921e52012-01-25 15:55:43 +02002790 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002791 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002792 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002793 edges |= 0;
2794 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002795 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002796
Pekka Paalanen60921e52012-01-25 15:55:43 +02002797 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002798 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002799 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002800 edges |= 0;
2801 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002802 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002803
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002804 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002805}
2806
2807static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002808surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002809 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002810{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002811 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002812 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002813 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002814 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002815 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002816
Pekka Paalanen01388e22013-04-25 13:57:44 +03002817 /* XXX: broken for windows containing sub-surfaces */
2818 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002819 if (surface == NULL)
2820 return;
2821
2822 shsurf = get_shell_surface(surface);
2823 if (!shsurf)
2824 return;
2825
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002826 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002827
Scott Moreau02709af2012-05-22 01:54:10 -06002828 if (surface->alpha > 1.0)
2829 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002830 if (surface->alpha < step)
2831 surface->alpha = step;
2832
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002833 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002834 weston_surface_damage(surface);
2835}
2836
2837static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002838do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002839 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002840{
Daniel Stone37816df2012-05-16 18:45:18 +01002841 struct weston_seat *ws = (struct weston_seat *) seat;
2842 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002843 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002844 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002845
2846 wl_list_for_each(output, &compositor->output_list, link) {
2847 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002848 wl_fixed_to_double(seat->pointer->x),
2849 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002850 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002851 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002852 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002853 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002854 increment = -output->zoom.increment;
2855 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002856 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002857 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002858 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002859 else
2860 increment = 0;
2861
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002862 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002863
Scott Moreaue6603982012-06-11 13:07:51 -06002864 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002865 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002866 else if (output->zoom.level > output->zoom.max_level)
2867 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002868 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002869 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002870 output->disable_planes++;
2871 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002872
Scott Moreaue6603982012-06-11 13:07:51 -06002873 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002874
Scott Moreau8dacaab2012-06-17 18:10:58 -06002875 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002876 }
2877 }
2878}
2879
Scott Moreauccbf29d2012-02-22 14:21:41 -07002880static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002881zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002882 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002883{
2884 do_zoom(seat, time, 0, axis, value);
2885}
2886
2887static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002888zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002889 void *data)
2890{
2891 do_zoom(seat, time, key, 0, 0);
2892}
2893
2894static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002895terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002896 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002897{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002898 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002899
Daniel Stone325fc2d2012-05-30 16:31:58 +01002900 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002901}
2902
2903static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002904rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002905{
2906 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002907 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002908 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002909 struct shell_surface *shsurf = rotate->base.shsurf;
2910 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002911 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002912
2913 if (!shsurf)
2914 return;
2915
2916 surface = shsurf->surface;
2917
2918 cx = 0.5f * surface->geometry.width;
2919 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002920
Daniel Stone37816df2012-05-16 18:45:18 +01002921 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2922 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002923 r = sqrtf(dx * dx + dy * dy);
2924
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002925 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002926 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002927
2928 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002929 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002930 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002931
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002932 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002933 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002934
2935 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002936 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002937 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002938 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002939 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002940
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002941 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002942 &shsurf->surface->geometry.transformation_list,
2943 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002944 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002945 wl_list_init(&shsurf->rotation.transform.link);
2946 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002947 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002948 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002949
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002950 /* We need to adjust the position of the surface
2951 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002952 cposx = surface->geometry.x + cx;
2953 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002954 dposx = rotate->center.x - cposx;
2955 dposy = rotate->center.y - cposy;
2956 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002957 weston_surface_set_position(surface,
2958 surface->geometry.x + dposx,
2959 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002960 }
2961
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002962 /* Repaint implies weston_surface_update_transform(), which
2963 * lazily applies the damage due to rotation update.
2964 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002965 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002966}
2967
2968static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002969rotate_grab_button(struct weston_pointer_grab *grab,
2970 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002971{
2972 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002973 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002974 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002975 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002976 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002977
Daniel Stone4dbadb12012-05-30 16:31:51 +01002978 if (pointer->button_count == 0 &&
2979 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002980 if (shsurf)
2981 weston_matrix_multiply(&shsurf->rotation.rotation,
2982 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002983 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002984 free(rotate);
2985 }
2986}
2987
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002988static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002989 noop_grab_focus,
2990 rotate_grab_motion,
2991 rotate_grab_button,
2992};
2993
2994static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002995surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002996{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002997 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002998 float dx, dy;
2999 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003000
Pekka Paalanen460099f2012-01-20 16:48:25 +02003001 rotate = malloc(sizeof *rotate);
3002 if (!rotate)
3003 return;
3004
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04003005 weston_surface_to_global_float(surface->surface,
Louis-Francis Ratté-Boulianne9a4f10f2013-07-03 15:58:22 +02003006 surface->surface->geometry.width * 0.5f,
3007 surface->surface->geometry.height * 0.5f,
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04003008 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003009
Daniel Stone37816df2012-05-16 18:45:18 +01003010 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3011 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003012 r = sqrtf(dx * dx + dy * dy);
3013 if (r > 20.0f) {
3014 struct weston_matrix inverse;
3015
3016 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003017 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003018 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003019
3020 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003021 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003022 } else {
3023 weston_matrix_init(&surface->rotation.rotation);
3024 weston_matrix_init(&rotate->rotation);
3025 }
3026
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003027 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3028 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003029}
3030
3031static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003032rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003033 void *data)
3034{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003035 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003036 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003037 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003038 struct shell_surface *surface;
3039
Pekka Paalanen01388e22013-04-25 13:57:44 +03003040 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003041 if (base_surface == NULL)
3042 return;
3043
3044 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003045 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3046 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003047 return;
3048
3049 surface_rotate(surface, seat);
3050}
3051
3052static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003053lower_fullscreen_layer(struct desktop_shell *shell)
3054{
3055 struct workspace *ws;
3056 struct weston_surface *surface, *prev;
3057
3058 ws = get_current_workspace(shell);
3059 wl_list_for_each_reverse_safe(surface, prev,
3060 &shell->fullscreen_layer.surface_list,
3061 layer_link)
3062 weston_surface_restack(surface, &ws->layer.surface_list);
3063}
3064
3065static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003066activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003067 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003068{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003069 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003070 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003071 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003072
Pekka Paalanen01388e22013-04-25 13:57:44 +03003073 main_surface = weston_surface_get_main_surface(es);
3074
Daniel Stone37816df2012-05-16 18:45:18 +01003075 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003076
Jonas Ådahl8538b222012-08-29 22:13:03 +02003077 state = ensure_focus_state(shell, seat);
3078 if (state == NULL)
3079 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003080
3081 state->keyboard_focus = es;
3082 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003083 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003084
Pekka Paalanen01388e22013-04-25 13:57:44 +03003085 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003086 case SHELL_SURFACE_FULLSCREEN:
3087 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003088 shell_stack_fullscreen(get_shell_surface(main_surface));
3089 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08003090 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003091 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003092 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003093 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003094 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003095 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003096 }
3097}
3098
Alex Wu21858432012-04-01 20:13:08 +08003099/* no-op func for checking black surface */
3100static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003101black_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 +08003102{
3103}
3104
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003105static bool
Alex Wu21858432012-04-01 20:13:08 +08003106is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3107{
3108 if (es->configure == black_surface_configure) {
3109 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003110 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003111 return true;
3112 }
3113 return false;
3114}
3115
Kristian Høgsberg75840622011-09-06 13:48:16 -04003116static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003117click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003118 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003119{
Daniel Stone37816df2012-05-16 18:45:18 +01003120 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003121 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003122 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003123 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003124
Daniel Stone37816df2012-05-16 18:45:18 +01003125 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003126 if (!focus)
3127 return;
3128
Pekka Paalanen01388e22013-04-25 13:57:44 +03003129 if (is_black_surface(focus, &main_surface))
3130 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003131
Pekka Paalanen01388e22013-04-25 13:57:44 +03003132 main_surface = weston_surface_get_main_surface(focus);
3133 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003134 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003135
Daniel Stone325fc2d2012-05-30 16:31:58 +01003136 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003137 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003138}
3139
3140static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003141lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003142{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003143 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003144
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003145 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003146 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003147 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003148 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003149
3150 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003151
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003152 /* Hide all surfaces by removing the fullscreen, panel and
3153 * toplevel layers. This way nothing else can show or receive
3154 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003155
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003156 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003157 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003158 if (shell->showing_input_panels)
3159 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003160 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003161 wl_list_insert(&shell->compositor->cursor_layer.link,
3162 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003163
Pekka Paalanen77346a62011-11-30 16:26:35 +02003164 launch_screensaver(shell);
3165
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003166 /* TODO: disable bindings that should not work while locked. */
3167
3168 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003169}
3170
3171static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003172unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003173{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003174 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003175 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003176 return;
3177 }
3178
3179 /* If desktop-shell client has gone away, unlock immediately. */
3180 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003181 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003182 return;
3183 }
3184
3185 if (shell->prepare_event_sent)
3186 return;
3187
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003188 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003189 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003190}
3191
3192static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003193shell_fade_done(struct weston_surface_animation *animation, void *data)
3194{
3195 struct desktop_shell *shell = data;
3196
3197 shell->fade.animation = NULL;
3198
3199 switch (shell->fade.type) {
3200 case FADE_IN:
3201 weston_surface_destroy(shell->fade.surface);
3202 shell->fade.surface = NULL;
3203 break;
3204 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003205 lock(shell);
3206 break;
3207 }
3208}
3209
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003210static struct weston_surface *
3211shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003212{
3213 struct weston_compositor *compositor = shell->compositor;
3214 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003215
3216 surface = weston_surface_create(compositor);
3217 if (!surface)
3218 return NULL;
3219
3220 weston_surface_configure(surface, 0, 0, 8192, 8192);
3221 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3222 wl_list_insert(&compositor->fade_layer.surface_list,
3223 &surface->layer_link);
3224 pixman_region32_init(&surface->input);
3225
3226 return surface;
3227}
3228
3229static void
3230shell_fade(struct desktop_shell *shell, enum fade_type type)
3231{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003232 float tint;
3233
3234 switch (type) {
3235 case FADE_IN:
3236 tint = 0.0;
3237 break;
3238 case FADE_OUT:
3239 tint = 1.0;
3240 break;
3241 default:
3242 weston_log("shell: invalid fade type\n");
3243 return;
3244 }
3245
3246 shell->fade.type = type;
3247
3248 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003249 shell->fade.surface = shell_fade_create_surface(shell);
3250 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003251 return;
3252
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003253 shell->fade.surface->alpha = 1.0 - tint;
3254 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003255 }
3256
3257 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003258 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003259 else
3260 shell->fade.animation =
3261 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003262 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003263 shell_fade_done, shell);
3264}
3265
3266static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003267do_shell_fade_startup(void *data)
3268{
3269 struct desktop_shell *shell = data;
3270
3271 shell_fade(shell, FADE_IN);
3272}
3273
3274static void
3275shell_fade_startup(struct desktop_shell *shell)
3276{
3277 struct wl_event_loop *loop;
3278
3279 if (!shell->fade.startup_timer)
3280 return;
3281
3282 wl_event_source_remove(shell->fade.startup_timer);
3283 shell->fade.startup_timer = NULL;
3284
3285 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3286 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3287}
3288
3289static int
3290fade_startup_timeout(void *data)
3291{
3292 struct desktop_shell *shell = data;
3293
3294 shell_fade_startup(shell);
3295 return 0;
3296}
3297
3298static void
3299shell_fade_init(struct desktop_shell *shell)
3300{
3301 /* Make compositor output all black, and wait for the desktop-shell
3302 * client to signal it is ready, then fade in. The timer triggers a
3303 * fade-in, in case the desktop-shell client takes too long.
3304 */
3305
3306 struct wl_event_loop *loop;
3307
3308 if (shell->fade.surface != NULL) {
3309 weston_log("%s: warning: fade surface already exists\n",
3310 __func__);
3311 return;
3312 }
3313
3314 shell->fade.surface = shell_fade_create_surface(shell);
3315 if (!shell->fade.surface)
3316 return;
3317
3318 weston_surface_update_transform(shell->fade.surface);
3319 weston_surface_damage(shell->fade.surface);
3320
3321 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3322 shell->fade.startup_timer =
3323 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3324 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3325}
3326
3327static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003328idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003329{
3330 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003331 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003332
3333 shell_fade(shell, FADE_OUT);
3334 /* lock() is called from shell_fade_done() */
3335}
3336
3337static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003338wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003339{
3340 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003341 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003342
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003343 unlock(shell);
3344}
3345
3346static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003347show_input_panels(struct wl_listener *listener, void *data)
3348{
3349 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003350 container_of(listener, struct desktop_shell,
3351 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003352 struct input_panel_surface *surface, *next;
3353 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003354
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003355 shell->text_input.surface = (struct weston_surface*)data;
3356
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003357 if (shell->showing_input_panels)
3358 return;
3359
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003360 shell->showing_input_panels = true;
3361
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003362 if (!shell->locked)
3363 wl_list_insert(&shell->panel_layer.link,
3364 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003365
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003366 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003367 &shell->input_panel.surfaces, link) {
3368 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003369 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003370 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003371 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003372 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003373 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003374 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003375 weston_surface_damage(ws);
3376 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003377 }
3378}
3379
3380static void
3381hide_input_panels(struct wl_listener *listener, void *data)
3382{
3383 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003384 container_of(listener, struct desktop_shell,
3385 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003386 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003387
Jan Arne Petersen61381972013-01-31 15:52:21 +01003388 if (!shell->showing_input_panels)
3389 return;
3390
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003391 shell->showing_input_panels = false;
3392
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003393 if (!shell->locked)
3394 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003395
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003396 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003397 &shell->input_panel_layer.surface_list, layer_link)
3398 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003399}
3400
3401static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003402update_input_panels(struct wl_listener *listener, void *data)
3403{
3404 struct desktop_shell *shell =
3405 container_of(listener, struct desktop_shell,
3406 update_input_panel_listener);
3407
3408 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3409}
3410
3411static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003412center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003413{
Giulio Camuffob8366642013-04-25 13:57:46 +03003414 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003415 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003416
Giulio Camuffob8366642013-04-25 13:57:46 +03003417 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3418
3419 x = output->x + (output->width - width) / 2 - surf_x / 2;
3420 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003421
3422 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003423}
3424
3425static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003426weston_surface_set_initial_position (struct weston_surface *surface,
3427 struct desktop_shell *shell)
3428{
3429 struct weston_compositor *compositor = shell->compositor;
3430 int ix = 0, iy = 0;
3431 int range_x, range_y;
3432 int dx, dy, x, y, panel_height;
3433 struct weston_output *output, *target_output = NULL;
3434 struct weston_seat *seat;
3435
3436 /* As a heuristic place the new window on the same output as the
3437 * pointer. Falling back to the output containing 0, 0.
3438 *
3439 * TODO: Do something clever for touch too?
3440 */
3441 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003442 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003443 ix = wl_fixed_to_int(seat->pointer->x);
3444 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003445 break;
3446 }
3447 }
3448
3449 wl_list_for_each(output, &compositor->output_list, link) {
3450 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3451 target_output = output;
3452 break;
3453 }
3454 }
3455
3456 if (!target_output) {
3457 weston_surface_set_position(surface, 10 + random() % 400,
3458 10 + random() % 400);
3459 return;
3460 }
3461
3462 /* Valid range within output where the surface will still be onscreen.
3463 * If this is negative it means that the surface is bigger than
3464 * output.
3465 */
3466 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003467 range_x = target_output->width - surface->geometry.width;
3468 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003469 surface->geometry.height;
3470
Rob Bradford4cb88c72012-08-13 15:18:44 +01003471 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003472 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003473 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003474 dx = 0;
3475
3476 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003477 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003478 else
3479 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003480
3481 x = target_output->x + dx;
3482 y = target_output->y + dy;
3483
3484 weston_surface_set_position (surface, x, y);
3485}
3486
3487static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003488map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003489 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003490{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003491 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003492 struct shell_surface *shsurf = get_shell_surface(surface);
3493 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003494 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003495 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003496 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003497 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003498 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003499
Pekka Paalanen60921e52012-01-25 15:55:43 +02003500 surface->geometry.width = width;
3501 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003502 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003503
3504 /* initial positioning, see also configure() */
3505 switch (surface_type) {
3506 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003507 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003508 break;
Alex Wu4539b082012-03-01 12:57:46 +08003509 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003510 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003511 shell_map_fullscreen(shsurf);
3512 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003513 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003514 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003515 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003516 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3517 NULL, NULL);
3518 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3519 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003520 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003521 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003522 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003523 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003524 case SHELL_SURFACE_NONE:
3525 weston_surface_set_position(surface,
3526 surface->geometry.x + sx,
3527 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003528 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003529 default:
3530 ;
3531 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003532
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003533 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003534 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003535 case SHELL_SURFACE_POPUP:
3536 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003537 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003538 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3539 break;
Alex Wu4539b082012-03-01 12:57:46 +08003540 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003541 case SHELL_SURFACE_NONE:
3542 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003543 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003544 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003545 ws = get_current_workspace(shell);
3546 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003547 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003548 }
3549
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003550 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003551 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003552 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3553 surface->output = shsurf->output;
3554 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003555
Juan Zhao7bb92f02011-12-15 11:31:51 -05003556 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003557 /* XXX: xwayland's using the same fields for transient type */
3558 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003559 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003560 if (shsurf->transient.flags ==
3561 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3562 break;
3563 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003564 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003565 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003566 if (!shell->locked) {
3567 wl_list_for_each(seat, &compositor->seat_list, link)
3568 activate(shell, surface, seat);
3569 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003570 break;
3571 default:
3572 break;
3573 }
3574
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003575 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003576 {
3577 switch (shell->win_animation_type) {
3578 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003579 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003580 break;
3581 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003582 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003583 break;
3584 default:
3585 break;
3586 }
3587 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003588}
3589
3590static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003591configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003592 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003593{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003594 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3595 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003596 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003597
Pekka Paalanen77346a62011-11-30 16:26:35 +02003598 shsurf = get_shell_surface(surface);
3599 if (shsurf)
3600 surface_type = shsurf->type;
3601
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003602 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003603
3604 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003605 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003606 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003607 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003608 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003609 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003610 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003611 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3612 NULL, NULL);
3613 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003614 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003615 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003616 break;
Alex Wu4539b082012-03-01 12:57:46 +08003617 case SHELL_SURFACE_TOPLEVEL:
3618 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003619 default:
3620 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003621 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003622
Alex Wu4539b082012-03-01 12:57:46 +08003623 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003624 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003625 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003626
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003627 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003628 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003629 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003630}
3631
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003632static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003633shell_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 +03003634{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003635 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003636 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003637
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003638 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003639
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003640 if (!weston_surface_is_mapped(es) &&
3641 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003642 remove_popup_grab(shsurf);
3643 }
3644
Giulio Camuffo184df502013-02-21 11:29:21 +01003645 if (width == 0)
3646 return;
3647
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003648 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003649 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003650 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003651 type_changed = 1;
3652 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003653
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003654 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003655 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003656 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003657 es->geometry.width != width ||
3658 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003659 float from_x, from_y;
3660 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003661
3662 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3663 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3664 configure(shell, es,
3665 es->geometry.x + to_x - from_x,
3666 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003667 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003668 }
3669}
3670
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003671static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003672
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003673static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003674desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003675{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003676 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003677 struct desktop_shell *shell =
3678 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003679
3680 shell->child.process.pid = 0;
3681 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003682
3683 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3684 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003685 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003686 shell->child.deathstamp = time;
3687 shell->child.deathcount = 0;
3688 }
3689
3690 shell->child.deathcount++;
3691 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003692 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003693 return;
3694 }
3695
Martin Minarik6d118362012-06-07 18:01:59 +02003696 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003697 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003698 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003699}
3700
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003701static void
3702launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003703{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003704 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003705 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003706
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003707 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003708 &shell->child.process,
3709 shell_exe,
3710 desktop_shell_sigchld);
3711
3712 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003713 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003714}
3715
3716static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003717bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3718{
Tiago Vignattibe143262012-04-16 17:31:41 +03003719 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003720 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003721
Jason Ekstranda85118c2013-06-27 20:17:02 -05003722 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3723 if (resource)
3724 wl_resource_set_implementation(resource, &shell_implementation,
3725 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003726}
3727
Kristian Høgsberg75840622011-09-06 13:48:16 -04003728static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003729unbind_desktop_shell(struct wl_resource *resource)
3730{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003731 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003732
3733 if (shell->locked)
3734 resume_desktop(shell);
3735
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003736 shell->child.desktop_shell = NULL;
3737 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003738}
3739
3740static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003741bind_desktop_shell(struct wl_client *client,
3742 void *data, uint32_t version, uint32_t id)
3743{
Tiago Vignattibe143262012-04-16 17:31:41 +03003744 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003745 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003746
Jason Ekstranda85118c2013-06-27 20:17:02 -05003747 resource = wl_resource_create(client, &desktop_shell_interface,
3748 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003749
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003750 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003751 wl_resource_set_implementation(resource,
3752 &desktop_shell_implementation,
3753 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003754 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003755
3756 if (version < 2)
3757 shell_fade_startup(shell);
3758
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003759 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003760 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003761
3762 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3763 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003764 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003765}
3766
Pekka Paalanen6e168112011-11-24 11:34:05 +02003767static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003768screensaver_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 -04003769{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003770 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003771
Giulio Camuffo184df502013-02-21 11:29:21 +01003772 if (width == 0)
3773 return;
3774
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003775 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003776 if (!shell->locked)
3777 return;
3778
3779 center_on_output(surface, surface->output);
3780
3781 if (wl_list_empty(&surface->layer_link)) {
3782 wl_list_insert(shell->lock_layer.surface_list.prev,
3783 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003784 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003785 wl_event_source_timer_update(shell->screensaver.timer,
3786 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003787 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003788 }
3789}
3790
3791static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003792screensaver_set_surface(struct wl_client *client,
3793 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003794 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003795 struct wl_resource *output_resource)
3796{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003797 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003798 struct weston_surface *surface =
3799 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003800 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003801
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003802 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003803 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003804 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003805}
3806
3807static const struct screensaver_interface screensaver_implementation = {
3808 screensaver_set_surface
3809};
3810
3811static void
3812unbind_screensaver(struct wl_resource *resource)
3813{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003814 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003815
Pekka Paalanen77346a62011-11-30 16:26:35 +02003816 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003817}
3818
3819static void
3820bind_screensaver(struct wl_client *client,
3821 void *data, uint32_t version, uint32_t id)
3822{
Tiago Vignattibe143262012-04-16 17:31:41 +03003823 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003824 struct wl_resource *resource;
3825
Jason Ekstranda85118c2013-06-27 20:17:02 -05003826 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003827
Pekka Paalanen77346a62011-11-30 16:26:35 +02003828 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003829 wl_resource_set_implementation(resource,
3830 &screensaver_implementation,
3831 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003832 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003833 return;
3834 }
3835
3836 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3837 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003838 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003839}
3840
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003841static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003842input_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 -04003843{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003844 struct input_panel_surface *ip_surface = surface->configure_private;
3845 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003846 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003847 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003848
Giulio Camuffo184df502013-02-21 11:29:21 +01003849 if (width == 0)
3850 return;
3851
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003852 if (!weston_surface_is_mapped(surface)) {
3853 if (!shell->showing_input_panels)
3854 return;
3855
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003856 show_surface = 1;
3857 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003858
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003859 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003860
3861 if (ip_surface->panel) {
3862 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3863 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3864 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01003865 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
3866 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003867 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003868
3869 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003870 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003871 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003872
3873 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003874 wl_list_insert(&shell->input_panel_layer.surface_list,
3875 &surface->layer_link);
3876 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003877 weston_surface_damage(surface);
3878 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3879 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003880}
3881
3882static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003883destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003884{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003885 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3886
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003887 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003888 wl_list_remove(&input_panel_surface->link);
3889
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003890 input_panel_surface->surface->configure = NULL;
3891
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003892 free(input_panel_surface);
3893}
3894
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003895static struct input_panel_surface *
3896get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003897{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003898 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003899 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003900 } else {
3901 return NULL;
3902 }
3903}
3904
3905static void
3906input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3907{
3908 struct input_panel_surface *ipsurface = container_of(listener,
3909 struct input_panel_surface,
3910 surface_destroy_listener);
3911
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003912 if (ipsurface->resource) {
3913 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003914 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003915 destroy_input_panel_surface(ipsurface);
3916 }
3917}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003918
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003919static struct input_panel_surface *
3920create_input_panel_surface(struct desktop_shell *shell,
3921 struct weston_surface *surface)
3922{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003923 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003924
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003925 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3926 if (!input_panel_surface)
3927 return NULL;
3928
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003929 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003930 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003931
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003932 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003933
3934 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003935
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003936 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003937 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003938 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003939 &input_panel_surface->surface_destroy_listener);
3940
3941 wl_list_init(&input_panel_surface->link);
3942
3943 return input_panel_surface;
3944}
3945
3946static void
3947input_panel_surface_set_toplevel(struct wl_client *client,
3948 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003949 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003950 uint32_t position)
3951{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003952 struct input_panel_surface *input_panel_surface =
3953 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003954 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003955
3956 wl_list_insert(&shell->input_panel.surfaces,
3957 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003958
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003959 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003960 input_panel_surface->panel = 0;
3961}
3962
3963static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003964input_panel_surface_set_overlay_panel(struct wl_client *client,
3965 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003966{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003967 struct input_panel_surface *input_panel_surface =
3968 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003969 struct desktop_shell *shell = input_panel_surface->shell;
3970
3971 wl_list_insert(&shell->input_panel.surfaces,
3972 &input_panel_surface->link);
3973
3974 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003975}
3976
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003977static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003978 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003979 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003980};
3981
3982static void
3983destroy_input_panel_surface_resource(struct wl_resource *resource)
3984{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003985 struct input_panel_surface *ipsurf =
3986 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003987
3988 destroy_input_panel_surface(ipsurf);
3989}
3990
3991static void
3992input_panel_get_input_panel_surface(struct wl_client *client,
3993 struct wl_resource *resource,
3994 uint32_t id,
3995 struct wl_resource *surface_resource)
3996{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003997 struct weston_surface *surface =
3998 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003999 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004000 struct input_panel_surface *ipsurf;
4001
4002 if (get_input_panel_surface(surface)) {
4003 wl_resource_post_error(surface_resource,
4004 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004005 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004006 return;
4007 }
4008
4009 ipsurf = create_input_panel_surface(shell, surface);
4010 if (!ipsurf) {
4011 wl_resource_post_error(surface_resource,
4012 WL_DISPLAY_ERROR_INVALID_OBJECT,
4013 "surface->configure already set");
4014 return;
4015 }
4016
Jason Ekstranda85118c2013-06-27 20:17:02 -05004017 ipsurf->resource =
4018 wl_resource_create(client,
4019 &wl_input_panel_surface_interface, 1, id);
4020 wl_resource_set_implementation(ipsurf->resource,
4021 &input_panel_surface_implementation,
4022 ipsurf,
4023 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004024}
4025
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004026static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004027 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004028};
4029
4030static void
4031unbind_input_panel(struct wl_resource *resource)
4032{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004033 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004034
4035 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004036}
4037
4038static void
4039bind_input_panel(struct wl_client *client,
4040 void *data, uint32_t version, uint32_t id)
4041{
4042 struct desktop_shell *shell = data;
4043 struct wl_resource *resource;
4044
Jason Ekstranda85118c2013-06-27 20:17:02 -05004045 resource = wl_resource_create(client,
4046 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004047
4048 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004049 wl_resource_set_implementation(resource,
4050 &input_panel_implementation,
4051 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004052 shell->input_panel.binding = resource;
4053 return;
4054 }
4055
4056 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4057 "interface object already bound");
4058 wl_resource_destroy(resource);
4059}
4060
Kristian Høgsberg07045392012-02-19 18:52:44 -05004061struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004062 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004063 struct weston_surface *current;
4064 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004065 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004066};
4067
4068static void
4069switcher_next(struct switcher *switcher)
4070{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004071 struct weston_surface *surface;
4072 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004073 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004074 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004075
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004076 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004077 switch (get_shell_surface_type(surface)) {
4078 case SHELL_SURFACE_TOPLEVEL:
4079 case SHELL_SURFACE_FULLSCREEN:
4080 case SHELL_SURFACE_MAXIMIZED:
4081 if (first == NULL)
4082 first = surface;
4083 if (prev == switcher->current)
4084 next = surface;
4085 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06004086 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02004087 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004088 weston_surface_damage(surface);
4089 break;
4090 default:
4091 break;
4092 }
Alex Wu1659daa2012-04-01 20:13:09 +08004093
4094 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06004095 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02004096 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004097 weston_surface_damage(surface);
4098 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004099 }
4100
4101 if (next == NULL)
4102 next = first;
4103
Alex Wu07b26062012-03-12 16:06:01 +08004104 if (next == NULL)
4105 return;
4106
Kristian Høgsberg07045392012-02-19 18:52:44 -05004107 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004108 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004109
4110 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004111 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004112
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004113 shsurf = get_shell_surface(switcher->current);
4114 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004115 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004116}
4117
4118static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004119switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004120{
4121 struct switcher *switcher =
4122 container_of(listener, struct switcher, listener);
4123
4124 switcher_next(switcher);
4125}
4126
4127static void
Daniel Stone351eb612012-05-31 15:27:47 -04004128switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004129{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004130 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004131 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004132 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004133
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004134 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004135 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004136 weston_surface_damage(surface);
4137 }
4138
Alex Wu07b26062012-03-12 16:06:01 +08004139 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004140 activate(switcher->shell, switcher->current,
4141 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004142 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004143 weston_keyboard_end_grab(keyboard);
4144 if (keyboard->input_method_resource)
4145 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004146 free(switcher);
4147}
4148
4149static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004150switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004151 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004152{
4153 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004154 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004155
Daniel Stonec9785ea2012-05-30 16:31:52 +01004156 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004157 switcher_next(switcher);
4158}
4159
4160static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004161switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004162 uint32_t mods_depressed, uint32_t mods_latched,
4163 uint32_t mods_locked, uint32_t group)
4164{
4165 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004166 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004167
Daniel Stone351eb612012-05-31 15:27:47 -04004168 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4169 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004170}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004171
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004172static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004173 switcher_key,
4174 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004175};
4176
4177static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004178switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004179 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004180{
Tiago Vignattibe143262012-04-16 17:31:41 +03004181 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004182 struct switcher *switcher;
4183
4184 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004185 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004186 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004187 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004188 wl_list_init(&switcher->listener.link);
4189
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004190 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004191 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004192 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004193 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4194 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004195 switcher_next(switcher);
4196}
4197
Pekka Paalanen3c647232011-12-22 13:43:43 +02004198static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004199backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004200 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004201{
4202 struct weston_compositor *compositor = data;
4203 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004204 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004205
4206 /* TODO: we're limiting to simple use cases, where we assume just
4207 * control on the primary display. We'd have to extend later if we
4208 * ever get support for setting backlights on random desktop LCD
4209 * panels though */
4210 output = get_default_output(compositor);
4211 if (!output)
4212 return;
4213
4214 if (!output->set_backlight)
4215 return;
4216
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004217 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4218 backlight_new = output->backlight_current - 25;
4219 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4220 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004221
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004222 if (backlight_new < 5)
4223 backlight_new = 5;
4224 if (backlight_new > 255)
4225 backlight_new = 255;
4226
4227 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004228 output->set_backlight(output, output->backlight_current);
4229}
4230
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004231struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004232 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004233 struct weston_seat *seat;
4234 uint32_t key[2];
4235 int key_released[2];
4236};
4237
4238static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004239debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004240 uint32_t key, uint32_t state)
4241{
4242 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004243 struct weston_compositor *ec = db->seat->compositor;
4244 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004245 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004246 uint32_t serial;
4247 int send = 0, terminate = 0;
4248 int check_binding = 1;
4249 int i;
4250
4251 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4252 /* Do not run bindings on key releases */
4253 check_binding = 0;
4254
4255 for (i = 0; i < 2; i++)
4256 if (key == db->key[i])
4257 db->key_released[i] = 1;
4258
4259 if (db->key_released[0] && db->key_released[1]) {
4260 /* All key releases been swalled so end the grab */
4261 terminate = 1;
4262 } else if (key != db->key[0] && key != db->key[1]) {
4263 /* Should not swallow release of other keys */
4264 send = 1;
4265 }
4266 } else if (key == db->key[0] && !db->key_released[0]) {
4267 /* Do not check bindings for the first press of the binding
4268 * key. This allows it to be used as a debug shortcut.
4269 * We still need to swallow this event. */
4270 check_binding = 0;
4271 } else if (db->key[1]) {
4272 /* If we already ran a binding don't process another one since
4273 * we can't keep track of all the binding keys that were
4274 * pressed in order to swallow the release events. */
4275 send = 1;
4276 check_binding = 0;
4277 }
4278
4279 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004280 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4281 key, state)) {
4282 /* We ran a binding so swallow the press and keep the
4283 * grab to swallow the released too. */
4284 send = 0;
4285 terminate = 0;
4286 db->key[1] = key;
4287 } else {
4288 /* Terminate the grab since the key pressed is not a
4289 * debug binding key. */
4290 send = 1;
4291 terminate = 1;
4292 }
4293 }
4294
4295 if (send) {
4296 resource = grab->keyboard->focus_resource;
4297
4298 if (resource) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004299 serial = wl_display_next_serial(display);
4300 wl_keyboard_send_key(resource, serial, time, key, state);
4301 }
4302 }
4303
4304 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004305 weston_keyboard_end_grab(grab->keyboard);
4306 if (grab->keyboard->input_method_resource)
4307 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004308 free(db);
4309 }
4310}
4311
4312static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004313debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004314 uint32_t mods_depressed, uint32_t mods_latched,
4315 uint32_t mods_locked, uint32_t group)
4316{
4317 struct wl_resource *resource;
4318
4319 resource = grab->keyboard->focus_resource;
4320 if (!resource)
4321 return;
4322
4323 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4324 mods_latched, mods_locked, group);
4325}
4326
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004327struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004328 debug_binding_key,
4329 debug_binding_modifiers
4330};
4331
4332static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004333debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004334{
4335 struct debug_binding_grab *grab;
4336
4337 grab = calloc(1, sizeof *grab);
4338 if (!grab)
4339 return;
4340
4341 grab->seat = (struct weston_seat *) seat;
4342 grab->key[0] = key;
4343 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004344 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004345}
4346
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004347static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004348force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004349 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004350{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004351 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004352 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004353 struct desktop_shell *shell = data;
4354 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004355 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004356
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004357 focus_surface = seat->keyboard->focus;
4358 if (!focus_surface)
4359 return;
4360
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004361 wl_signal_emit(&compositor->kill_signal, focus_surface);
4362
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004363 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004364 wl_client_get_credentials(client, &pid, NULL, NULL);
4365
4366 /* Skip clients that we launched ourselves (the credentials of
4367 * the socketpair is ours) */
4368 if (pid == getpid())
4369 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004370
Daniel Stone325fc2d2012-05-30 16:31:58 +01004371 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004372}
4373
4374static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004375workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004376 uint32_t key, void *data)
4377{
4378 struct desktop_shell *shell = data;
4379 unsigned int new_index = shell->workspaces.current;
4380
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004381 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004382 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004383 if (new_index != 0)
4384 new_index--;
4385
4386 change_workspace(shell, new_index);
4387}
4388
4389static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004390workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004391 uint32_t key, void *data)
4392{
4393 struct desktop_shell *shell = data;
4394 unsigned int new_index = shell->workspaces.current;
4395
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004396 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004397 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004398 if (new_index < shell->workspaces.num - 1)
4399 new_index++;
4400
4401 change_workspace(shell, new_index);
4402}
4403
4404static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004405workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004406 uint32_t key, void *data)
4407{
4408 struct desktop_shell *shell = data;
4409 unsigned int new_index;
4410
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004411 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004412 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004413 new_index = key - KEY_F1;
4414 if (new_index >= shell->workspaces.num)
4415 new_index = shell->workspaces.num - 1;
4416
4417 change_workspace(shell, new_index);
4418}
4419
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004420static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004421workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004422 uint32_t key, void *data)
4423{
4424 struct desktop_shell *shell = data;
4425 unsigned int new_index = shell->workspaces.current;
4426
4427 if (shell->locked)
4428 return;
4429
4430 if (new_index != 0)
4431 new_index--;
4432
4433 take_surface_to_workspace_by_seat(shell, seat, new_index);
4434}
4435
4436static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004437workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004438 uint32_t key, void *data)
4439{
4440 struct desktop_shell *shell = data;
4441 unsigned int new_index = shell->workspaces.current;
4442
4443 if (shell->locked)
4444 return;
4445
4446 if (new_index < shell->workspaces.num - 1)
4447 new_index++;
4448
4449 take_surface_to_workspace_by_seat(shell, seat, new_index);
4450}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004451
4452static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004453shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004454{
Tiago Vignattibe143262012-04-16 17:31:41 +03004455 struct desktop_shell *shell =
4456 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004457 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004458
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004459 if (shell->child.client)
4460 wl_client_destroy(shell->child.client);
4461
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004462 wl_list_remove(&shell->idle_listener.link);
4463 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004464 wl_list_remove(&shell->show_input_panel_listener.link);
4465 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004466
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004467 wl_array_for_each(ws, &shell->workspaces.array)
4468 workspace_destroy(*ws);
4469 wl_array_release(&shell->workspaces.array);
4470
Pekka Paalanen3c647232011-12-22 13:43:43 +02004471 free(shell->screensaver.path);
4472 free(shell);
4473}
4474
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004475static void
4476shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4477{
4478 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004479 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004480
4481 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004482 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4483 MODIFIER_CTRL | MODIFIER_ALT,
4484 terminate_binding, ec);
4485 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4486 click_to_activate_binding,
4487 shell);
4488 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4489 MODIFIER_SUPER | MODIFIER_ALT,
4490 surface_opacity_binding, NULL);
4491 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4492 MODIFIER_SUPER, zoom_axis_binding,
4493 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004494
4495 /* configurable bindings */
4496 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004497 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4498 zoom_key_binding, NULL);
4499 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4500 zoom_key_binding, NULL);
4501 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4502 shell);
4503 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4504 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004505
4506 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4507 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4508 rotate_binding, NULL);
4509
Daniel Stone325fc2d2012-05-30 16:31:58 +01004510 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4511 shell);
4512 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4513 ec);
4514 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4515 backlight_binding, ec);
4516 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4517 ec);
4518 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4519 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004520 weston_compositor_add_key_binding(ec, KEY_K, mod,
4521 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004522 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4523 workspace_up_binding, shell);
4524 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4525 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004526 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4527 workspace_move_surface_up_binding,
4528 shell);
4529 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4530 workspace_move_surface_down_binding,
4531 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004532
4533 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4534 if (shell->workspaces.num > 1) {
4535 num_workspace_bindings = shell->workspaces.num;
4536 if (num_workspace_bindings > 6)
4537 num_workspace_bindings = 6;
4538 for (i = 0; i < num_workspace_bindings; i++)
4539 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4540 workspace_f_binding,
4541 shell);
4542 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004543
4544 /* Debug bindings */
4545 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4546 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004547}
4548
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004549WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004550module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004551 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004552{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004553 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004554 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004555 struct workspace **pws;
4556 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004557 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004558
Peter Huttererf3d62272013-08-08 11:57:05 +10004559 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004560 if (shell == NULL)
4561 return -1;
4562
Kristian Høgsberg75840622011-09-06 13:48:16 -04004563 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004564
4565 shell->destroy_listener.notify = shell_destroy;
4566 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004567 shell->idle_listener.notify = idle_handler;
4568 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4569 shell->wake_listener.notify = wake_handler;
4570 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004571 shell->show_input_panel_listener.notify = show_input_panels;
4572 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4573 shell->hide_input_panel_listener.notify = hide_input_panels;
4574 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004575 shell->update_input_panel_listener.notify = update_input_panels;
4576 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004577 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004578 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004579 ec->shell_interface.create_shell_surface = create_shell_surface;
4580 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004581 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004582 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004583 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004584 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004585 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004586
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004587 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004588
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004589 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4590 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004591 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4592 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004593 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004594
4595 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004596 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004597
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004598 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004599
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004600 for (i = 0; i < shell->workspaces.num; i++) {
4601 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4602 if (pws == NULL)
4603 return -1;
4604
4605 *pws = workspace_create();
4606 if (*pws == NULL)
4607 return -1;
4608 }
4609 activate_workspace(shell, 0);
4610
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004611 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004612 wl_list_init(&shell->workspaces.animation.link);
4613 shell->workspaces.animation.frame = animate_workspace_change_frame;
4614
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004615 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004616 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004617 return -1;
4618
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004619 if (wl_global_create(ec->wl_display,
4620 &desktop_shell_interface, 2,
4621 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004622 return -1;
4623
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004624 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4625 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004626 return -1;
4627
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004628 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004629 shell, bind_input_panel) == NULL)
4630 return -1;
4631
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004632 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4633 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004634 return -1;
4635
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004636 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004637
4638 loop = wl_display_get_event_loop(ec->wl_display);
4639 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004640
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004641 shell->screensaver.timer =
4642 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4643
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004644 wl_list_for_each(seat, &ec->seat_list, link)
4645 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004646
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004647 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004648
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004649 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004650
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004651 return 0;
4652}