blob: d1847f53d3f3ef065e38aa61f3e5a871c7089bce [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{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700720 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200721
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700722 wl_resource_for_each(resource, &shell->workspaces.client_list)
723 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200724 shell->workspaces.current,
725 shell->workspaces.num);
726}
727
728static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200729reverse_workspace_change_animation(struct desktop_shell *shell,
730 unsigned int index,
731 struct workspace *from,
732 struct workspace *to)
733{
734 shell->workspaces.current = index;
735
736 shell->workspaces.anim_to = to;
737 shell->workspaces.anim_from = from;
738 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
739 shell->workspaces.anim_timestamp = 0;
740
Scott Moreau4272e632012-08-13 09:58:41 -0600741 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200742}
743
744static void
745workspace_deactivate_transforms(struct workspace *ws)
746{
747 struct weston_surface *surface;
748 struct shell_surface *shsurf;
749
750 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
751 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200752 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
753 wl_list_remove(&shsurf->workspace_transform.link);
754 wl_list_init(&shsurf->workspace_transform.link);
755 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200756 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200757 }
758}
759
760static void
761finish_workspace_change_animation(struct desktop_shell *shell,
762 struct workspace *from,
763 struct workspace *to)
764{
Scott Moreau4272e632012-08-13 09:58:41 -0600765 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200766
767 wl_list_remove(&shell->workspaces.animation.link);
768 workspace_deactivate_transforms(from);
769 workspace_deactivate_transforms(to);
770 shell->workspaces.anim_to = NULL;
771
772 wl_list_remove(&shell->workspaces.anim_from->layer.link);
773}
774
775static void
776animate_workspace_change_frame(struct weston_animation *animation,
777 struct weston_output *output, uint32_t msecs)
778{
779 struct desktop_shell *shell =
780 container_of(animation, struct desktop_shell,
781 workspaces.animation);
782 struct workspace *from = shell->workspaces.anim_from;
783 struct workspace *to = shell->workspaces.anim_to;
784 uint32_t t;
785 double x, y;
786
787 if (workspace_is_empty(from) && workspace_is_empty(to)) {
788 finish_workspace_change_animation(shell, from, to);
789 return;
790 }
791
792 if (shell->workspaces.anim_timestamp == 0) {
793 if (shell->workspaces.anim_current == 0.0)
794 shell->workspaces.anim_timestamp = msecs;
795 else
796 shell->workspaces.anim_timestamp =
797 msecs -
798 /* Invers of movement function 'y' below. */
799 (asin(1.0 - shell->workspaces.anim_current) *
800 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
801 M_2_PI);
802 }
803
804 t = msecs - shell->workspaces.anim_timestamp;
805
806 /*
807 * x = [0, π/2]
808 * y(x) = sin(x)
809 */
810 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
811 y = sin(x);
812
813 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600814 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200815
816 workspace_translate_out(from, shell->workspaces.anim_dir * y);
817 workspace_translate_in(to, shell->workspaces.anim_dir * y);
818 shell->workspaces.anim_current = y;
819
Scott Moreau4272e632012-08-13 09:58:41 -0600820 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200821 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200822 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200823 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200824}
825
826static void
827animate_workspace_change(struct desktop_shell *shell,
828 unsigned int index,
829 struct workspace *from,
830 struct workspace *to)
831{
832 struct weston_output *output;
833
834 int dir;
835
836 if (index > shell->workspaces.current)
837 dir = -1;
838 else
839 dir = 1;
840
841 shell->workspaces.current = index;
842
843 shell->workspaces.anim_dir = dir;
844 shell->workspaces.anim_from = from;
845 shell->workspaces.anim_to = to;
846 shell->workspaces.anim_current = 0.0;
847 shell->workspaces.anim_timestamp = 0;
848
849 output = container_of(shell->compositor->output_list.next,
850 struct weston_output, link);
851 wl_list_insert(&output->animation_list,
852 &shell->workspaces.animation.link);
853
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200854 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855
856 workspace_translate_in(to, 0);
857
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400858 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200859
Scott Moreau4272e632012-08-13 09:58:41 -0600860 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200861}
862
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200863static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200864update_workspace(struct desktop_shell *shell, unsigned int index,
865 struct workspace *from, struct workspace *to)
866{
867 shell->workspaces.current = index;
868 wl_list_insert(&from->layer.link, &to->layer.link);
869 wl_list_remove(&from->layer.link);
870}
871
872static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200873change_workspace(struct desktop_shell *shell, unsigned int index)
874{
875 struct workspace *from;
876 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200877
878 if (index == shell->workspaces.current)
879 return;
880
881 /* Don't change workspace when there is any fullscreen surfaces. */
882 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
883 return;
884
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200885 from = get_current_workspace(shell);
886 to = get_workspace(shell, index);
887
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200888 if (shell->workspaces.anim_from == to &&
889 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200890 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200891 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200892 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200893 return;
894 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200895
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200896 if (shell->workspaces.anim_to != NULL)
897 finish_workspace_change_animation(shell,
898 shell->workspaces.anim_from,
899 shell->workspaces.anim_to);
900
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200901 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200902
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200903 if (workspace_is_empty(to) && workspace_is_empty(from))
904 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200905 else
906 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200907
908 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200909}
910
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200911static bool
912workspace_has_only(struct workspace *ws, struct weston_surface *surface)
913{
914 struct wl_list *list = &ws->layer.surface_list;
915 struct wl_list *e;
916
917 if (wl_list_empty(list))
918 return false;
919
920 e = list->next;
921
922 if (e->next != list)
923 return false;
924
925 return container_of(e, struct weston_surface, layer_link) == surface;
926}
927
928static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200929move_surface_to_workspace(struct desktop_shell *shell,
930 struct weston_surface *surface,
931 uint32_t workspace)
932{
933 struct workspace *from;
934 struct workspace *to;
935 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300936 struct weston_surface *focus;
937
938 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200939
940 if (workspace == shell->workspaces.current)
941 return;
942
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200943 if (workspace >= shell->workspaces.num)
944 workspace = shell->workspaces.num - 1;
945
Jonas Ådahle9d22502012-08-29 22:13:01 +0200946 from = get_current_workspace(shell);
947 to = get_workspace(shell, workspace);
948
949 wl_list_remove(&surface->layer_link);
950 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
951
952 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300953 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
954 if (!seat->keyboard)
955 continue;
956
957 focus = weston_surface_get_main_surface(seat->keyboard->focus);
958 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400959 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300960 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200961
962 weston_surface_damage_below(surface);
963}
964
965static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200966take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400967 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200968 unsigned int index)
969{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300970 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200971 struct shell_surface *shsurf;
972 struct workspace *from;
973 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200974 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200975
Pekka Paalanen01388e22013-04-25 13:57:44 +0300976 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200977 if (surface == NULL ||
978 index == shell->workspaces.current)
979 return;
980
981 from = get_current_workspace(shell);
982 to = get_workspace(shell, index);
983
984 wl_list_remove(&surface->layer_link);
985 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
986
Jonas Ådahle9d22502012-08-29 22:13:01 +0200987 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200988 drop_focus_state(shell, from, surface);
989
990 if (shell->workspaces.anim_from == to &&
991 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200992 wl_list_remove(&to->layer.link);
993 wl_list_insert(from->layer.link.prev, &to->layer.link);
994
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200995 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200996 broadcast_current_workspace_state(shell);
997
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200998 return;
999 }
1000
1001 if (shell->workspaces.anim_to != NULL)
1002 finish_workspace_change_animation(shell,
1003 shell->workspaces.anim_from,
1004 shell->workspaces.anim_to);
1005
1006 if (workspace_is_empty(from) &&
1007 workspace_has_only(to, surface))
1008 update_workspace(shell, index, from, to);
1009 else {
1010 shsurf = get_shell_surface(surface);
1011 if (wl_list_empty(&shsurf->workspace_transform.link))
1012 wl_list_insert(&shell->workspaces.anim_sticky_list,
1013 &shsurf->workspace_transform.link);
1014
1015 animate_workspace_change(shell, index, from, to);
1016 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001017
1018 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001019
1020 state = ensure_focus_state(shell, seat);
1021 if (state != NULL)
1022 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001023}
1024
1025static void
1026workspace_manager_move_surface(struct wl_client *client,
1027 struct wl_resource *resource,
1028 struct wl_resource *surface_resource,
1029 uint32_t workspace)
1030{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001031 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001032 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001033 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001034 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001035
Pekka Paalanen01388e22013-04-25 13:57:44 +03001036 main_surface = weston_surface_get_main_surface(surface);
1037 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001038}
1039
1040static const struct workspace_manager_interface workspace_manager_implementation = {
1041 workspace_manager_move_surface,
1042};
1043
1044static void
1045unbind_resource(struct wl_resource *resource)
1046{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001047 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001048}
1049
1050static void
1051bind_workspace_manager(struct wl_client *client,
1052 void *data, uint32_t version, uint32_t id)
1053{
1054 struct desktop_shell *shell = data;
1055 struct wl_resource *resource;
1056
Jason Ekstranda85118c2013-06-27 20:17:02 -05001057 resource = wl_resource_create(client,
1058 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001059
1060 if (resource == NULL) {
1061 weston_log("couldn't add workspace manager object");
1062 return;
1063 }
1064
Jason Ekstranda85118c2013-06-27 20:17:02 -05001065 wl_resource_set_implementation(resource,
1066 &workspace_manager_implementation,
1067 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001068 wl_list_insert(&shell->workspaces.client_list,
1069 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001070
1071 workspace_manager_send_state(resource,
1072 shell->workspaces.current,
1073 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001074}
1075
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001076static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001077touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1078 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1079{
1080}
1081
1082static void
1083touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1084{
1085 struct shell_touch_grab *shell_grab = container_of(grab,
1086 struct shell_touch_grab,
1087 grab);
1088 shell_touch_grab_end(shell_grab);
1089}
1090
1091static void
1092touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1093 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1094{
1095 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1096 struct shell_surface *shsurf = move->base.shsurf;
1097 struct weston_surface *es;
1098 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1099 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1100
1101 if (!shsurf)
1102 return;
1103
1104 es = shsurf->surface;
1105
1106 weston_surface_configure(es, dx, dy,
1107 es->geometry.width, es->geometry.height);
1108
1109 weston_compositor_schedule_repaint(es->compositor);
1110}
1111
1112static const struct weston_touch_grab_interface touch_move_grab_interface = {
1113 touch_move_grab_down,
1114 touch_move_grab_up,
1115 touch_move_grab_motion,
1116};
1117
1118static int
1119surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1120{
1121 struct weston_touch_move_grab *move;
1122
1123 if (!shsurf)
1124 return -1;
1125
1126 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1127 return 0;
1128
1129 move = malloc(sizeof *move);
1130 if (!move)
1131 return -1;
1132
1133 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1134 seat->touch->grab_x;
1135 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1136 seat->touch->grab_y;
1137
1138 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1139 seat->touch);
1140
1141 return 0;
1142}
1143
1144static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001145noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001146{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001147}
1148
1149static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001150move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001151{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001152 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001153 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001154 struct shell_surface *shsurf = move->base.shsurf;
1155 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001156 int dx = wl_fixed_to_int(pointer->x + move->dx);
1157 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001158
1159 if (!shsurf)
1160 return;
1161
1162 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001163
Daniel Stone103db7f2012-05-08 17:17:55 +01001164 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001165 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001166
1167 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001168}
1169
1170static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001171move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001172 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001173{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001174 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1175 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001176 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001177 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001178
Daniel Stone4dbadb12012-05-30 16:31:51 +01001179 if (pointer->button_count == 0 &&
1180 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001181 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001182 free(grab);
1183 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001184}
1185
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001186static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001187 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001188 move_grab_motion,
1189 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001190};
1191
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001192static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001193surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001194{
1195 struct weston_move_grab *move;
1196
1197 if (!shsurf)
1198 return -1;
1199
1200 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1201 return 0;
1202
1203 move = malloc(sizeof *move);
1204 if (!move)
1205 return -1;
1206
1207 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001208 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001209 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001210 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001211
1212 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001213 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001214
1215 return 0;
1216}
1217
1218static void
1219shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1220 struct wl_resource *seat_resource, uint32_t serial)
1221{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001222 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001223 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001224 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001225
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001226 if (seat->pointer &&
1227 seat->pointer->button_count > 0 &&
1228 seat->pointer->grab_serial == serial) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001229 surface = weston_surface_get_main_surface(seat->pointer->focus);
1230 if ((surface == shsurf->surface) &&
1231 (surface_move(shsurf, seat) < 0))
1232 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001233 } else if (seat->touch &&
1234 seat->touch->grab_serial == serial) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001235 surface = weston_surface_get_main_surface(seat->touch->focus);
1236 if ((surface == shsurf->surface) &&
1237 (surface_touch_move(shsurf, seat) < 0))
1238 wl_resource_post_no_memory(resource);
1239 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001240}
1241
1242struct weston_resize_grab {
1243 struct shell_grab base;
1244 uint32_t edges;
1245 int32_t width, height;
1246};
1247
1248static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001249resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001250{
1251 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001252 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001253 struct shell_surface *shsurf = resize->base.shsurf;
1254 int32_t width, height;
1255 wl_fixed_t from_x, from_y;
1256 wl_fixed_t to_x, to_y;
1257
1258 if (!shsurf)
1259 return;
1260
1261 weston_surface_from_global_fixed(shsurf->surface,
1262 pointer->grab_x, pointer->grab_y,
1263 &from_x, &from_y);
1264 weston_surface_from_global_fixed(shsurf->surface,
1265 pointer->x, pointer->y, &to_x, &to_y);
1266
1267 width = resize->width;
1268 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1269 width += wl_fixed_to_int(from_x - to_x);
1270 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1271 width += wl_fixed_to_int(to_x - from_x);
1272 }
1273
1274 height = resize->height;
1275 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1276 height += wl_fixed_to_int(from_y - to_y);
1277 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1278 height += wl_fixed_to_int(to_y - from_y);
1279 }
1280
1281 shsurf->client->send_configure(shsurf->surface,
1282 resize->edges, width, height);
1283}
1284
1285static void
1286send_configure(struct weston_surface *surface,
1287 uint32_t edges, int32_t width, int32_t height)
1288{
1289 struct shell_surface *shsurf = get_shell_surface(surface);
1290
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001291 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001292 edges, width, height);
1293}
1294
1295static const struct weston_shell_client shell_client = {
1296 send_configure
1297};
1298
1299static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001300resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001301 uint32_t time, uint32_t button, uint32_t state_w)
1302{
1303 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001304 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001305 enum wl_pointer_button_state state = state_w;
1306
1307 if (pointer->button_count == 0 &&
1308 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1309 shell_grab_end(&resize->base);
1310 free(grab);
1311 }
1312}
1313
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001314static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001315 noop_grab_focus,
1316 resize_grab_motion,
1317 resize_grab_button,
1318};
1319
Giulio Camuffob8366642013-04-25 13:57:46 +03001320/*
1321 * Returns the bounding box of a surface and all its sub-surfaces,
1322 * in the surface coordinates system. */
1323static void
1324surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1325 int32_t *y, int32_t *w, int32_t *h) {
1326 pixman_region32_t region;
1327 pixman_box32_t *box;
1328 struct weston_subsurface *subsurface;
1329
1330 pixman_region32_init_rect(&region, 0, 0,
1331 surface->geometry.width,
1332 surface->geometry.height);
1333
1334 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1335 pixman_region32_union_rect(&region, &region,
1336 subsurface->position.x,
1337 subsurface->position.y,
1338 subsurface->surface->geometry.width,
1339 subsurface->surface->geometry.height);
1340 }
1341
1342 box = pixman_region32_extents(&region);
1343 if (x)
1344 *x = box->x1;
1345 if (y)
1346 *y = box->y1;
1347 if (w)
1348 *w = box->x2 - box->x1;
1349 if (h)
1350 *h = box->y2 - box->y1;
1351
1352 pixman_region32_fini(&region);
1353}
1354
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001355static int
1356surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001357 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001358{
1359 struct weston_resize_grab *resize;
1360
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001361 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1362 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001363 return 0;
1364
1365 if (edges == 0 || edges > 15 ||
1366 (edges & 3) == 3 || (edges & 12) == 12)
1367 return 0;
1368
1369 resize = malloc(sizeof *resize);
1370 if (!resize)
1371 return -1;
1372
1373 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001374 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1375 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001376
1377 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001378 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001379
1380 return 0;
1381}
1382
1383static void
1384shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1385 struct wl_resource *seat_resource, uint32_t serial,
1386 uint32_t edges)
1387{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001388 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001389 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001390 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001391
1392 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1393 return;
1394
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001395 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001396 if (seat->pointer->button_count == 0 ||
1397 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001398 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001399 return;
1400
Kristian Høgsberge3148752013-05-06 23:19:49 -04001401 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001402 wl_resource_post_no_memory(resource);
1403}
1404
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001405static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001406end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1407
1408static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001409busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001410{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001411 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001412 struct weston_pointer *pointer = base->pointer;
1413 struct weston_surface *surface;
1414 wl_fixed_t sx, sy;
1415
1416 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1417 pointer->x, pointer->y,
1418 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001419
Kristian Høgsberg67800732013-07-04 01:12:17 -04001420 if (!grab->shsurf || grab->shsurf->surface != surface)
1421 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001422}
1423
1424static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001425busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001426{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001427}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001428
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001429static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001430busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001431 uint32_t time, uint32_t button, uint32_t state)
1432{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001433 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001434 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001435 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001436
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001437 if (shsurf && button == BTN_LEFT && state) {
1438 activate(shsurf->shell, shsurf->surface, seat);
1439 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001440 } else if (shsurf && button == BTN_RIGHT && state) {
1441 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001442 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001443 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001444}
1445
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001446static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001447 busy_cursor_grab_focus,
1448 busy_cursor_grab_motion,
1449 busy_cursor_grab_button,
1450};
1451
1452static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001453set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001454{
1455 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001456
1457 grab = malloc(sizeof *grab);
1458 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001459 return;
1460
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001461 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1462 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001463}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001464
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001465static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001466end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001467{
1468 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1469
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001470 if (grab->grab.interface == &busy_cursor_grab_interface &&
1471 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001472 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001473 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001474 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001475}
1476
Scott Moreau9521d5e2012-04-19 13:06:17 -06001477static void
1478ping_timer_destroy(struct shell_surface *shsurf)
1479{
1480 if (!shsurf || !shsurf->ping_timer)
1481 return;
1482
1483 if (shsurf->ping_timer->source)
1484 wl_event_source_remove(shsurf->ping_timer->source);
1485
1486 free(shsurf->ping_timer);
1487 shsurf->ping_timer = NULL;
1488}
1489
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001490static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001491ping_timeout_handler(void *data)
1492{
1493 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001494 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001495
Scott Moreau9521d5e2012-04-19 13:06:17 -06001496 /* Client is not responding */
1497 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001498
1499 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001500 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001501 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001502
1503 return 1;
1504}
1505
1506static void
1507ping_handler(struct weston_surface *surface, uint32_t serial)
1508{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001509 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001510 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001511 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001512
1513 if (!shsurf)
1514 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001515 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001516 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001517
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001518 if (shsurf->surface == shsurf->shell->grab_surface)
1519 return;
1520
Scott Moreauff1db4a2012-04-17 19:06:18 -06001521 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001522 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001523 if (!shsurf->ping_timer)
1524 return;
1525
1526 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001527 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1528 shsurf->ping_timer->source =
1529 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1530 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1531
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001532 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001533 }
1534}
1535
1536static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001537handle_pointer_focus(struct wl_listener *listener, void *data)
1538{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001539 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001540 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001541 struct weston_compositor *compositor;
1542 struct shell_surface *shsurf;
1543 uint32_t serial;
1544
1545 if (!surface)
1546 return;
1547
1548 compositor = surface->compositor;
1549 shsurf = get_shell_surface(surface);
1550
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001551 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001552 set_busy_cursor(shsurf, pointer);
1553 } else {
1554 serial = wl_display_next_serial(compositor->wl_display);
1555 ping_handler(surface, serial);
1556 }
1557}
1558
1559static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001560create_pointer_focus_listener(struct weston_seat *seat)
1561{
1562 struct wl_listener *listener;
1563
Kristian Høgsberge3148752013-05-06 23:19:49 -04001564 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001565 return;
1566
1567 listener = malloc(sizeof *listener);
1568 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001569 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001570}
1571
1572static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001573shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1574 uint32_t serial)
1575{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001576 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001577 struct weston_seat *seat;
1578 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001579
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001580 if (shsurf->ping_timer == NULL)
1581 /* Just ignore unsolicited pong. */
1582 return;
1583
Scott Moreauff1db4a2012-04-17 19:06:18 -06001584 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001585 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001586 wl_list_for_each(seat, &ec->seat_list, link) {
1587 if(seat->pointer)
1588 end_busy_cursor(shsurf, seat->pointer);
1589 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001590 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001591 }
1592}
1593
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001594static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001595set_title(struct shell_surface *shsurf, const char *title)
1596{
1597 free(shsurf->title);
1598 shsurf->title = strdup(title);
1599}
1600
1601static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001602shell_surface_set_title(struct wl_client *client,
1603 struct wl_resource *resource, const char *title)
1604{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001605 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001606
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001607 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001608}
1609
1610static void
1611shell_surface_set_class(struct wl_client *client,
1612 struct wl_resource *resource, const char *class)
1613{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001614 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001615
1616 free(shsurf->class);
1617 shsurf->class = strdup(class);
1618}
1619
Juan Zhao96879df2012-02-07 08:45:41 +08001620static struct weston_output *
1621get_default_output(struct weston_compositor *compositor)
1622{
1623 return container_of(compositor->output_list.next,
1624 struct weston_output, link);
1625}
1626
Alex Wu4539b082012-03-01 12:57:46 +08001627static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001628restore_output_mode(struct weston_output *output)
1629{
Hardening57388e42013-09-18 23:56:36 +02001630 if (output->current_mode != output->original_mode ||
1631 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001632 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001633 output->original_mode,
1634 output->original_scale,
1635 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001636}
1637
1638static void
1639restore_all_output_modes(struct weston_compositor *compositor)
1640{
1641 struct weston_output *output;
1642
1643 wl_list_for_each(output, &compositor->output_list, link)
1644 restore_output_mode(output);
1645}
1646
1647static void
Alex Wu4539b082012-03-01 12:57:46 +08001648shell_unset_fullscreen(struct shell_surface *shsurf)
1649{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001650 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001651 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001652 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1653 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001654 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001655 }
Alex Wu4539b082012-03-01 12:57:46 +08001656 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1657 shsurf->fullscreen.framerate = 0;
1658 wl_list_remove(&shsurf->fullscreen.transform.link);
1659 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001660 if (shsurf->fullscreen.black_surface)
1661 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001662 shsurf->fullscreen.black_surface = NULL;
1663 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001664 weston_surface_set_position(shsurf->surface,
1665 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001666 if (shsurf->saved_rotation_valid) {
1667 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1668 &shsurf->rotation.transform.link);
1669 shsurf->saved_rotation_valid = false;
1670 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001671
1672 ws = get_current_workspace(shsurf->shell);
1673 wl_list_remove(&shsurf->surface->layer_link);
1674 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001675}
1676
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001677static void
1678shell_unset_maximized(struct shell_surface *shsurf)
1679{
1680 struct workspace *ws;
1681 /* undo all maximized things here */
1682 shsurf->output = get_default_output(shsurf->surface->compositor);
1683 weston_surface_set_position(shsurf->surface,
1684 shsurf->saved_x,
1685 shsurf->saved_y);
1686
1687 if (shsurf->saved_rotation_valid) {
1688 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1689 &shsurf->rotation.transform.link);
1690 shsurf->saved_rotation_valid = false;
1691 }
1692
1693 ws = get_current_workspace(shsurf->shell);
1694 wl_list_remove(&shsurf->surface->layer_link);
1695 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1696}
1697
Pekka Paalanen98262232011-12-01 10:42:22 +02001698static int
1699reset_shell_surface_type(struct shell_surface *surface)
1700{
1701 switch (surface->type) {
1702 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001703 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001704 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001705 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001706 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001707 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001708 case SHELL_SURFACE_NONE:
1709 case SHELL_SURFACE_TOPLEVEL:
1710 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001711 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001712 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001713 break;
1714 }
1715
1716 surface->type = SHELL_SURFACE_NONE;
1717 return 0;
1718}
1719
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001720static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001721set_surface_type(struct shell_surface *shsurf)
1722{
1723 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001724 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001725
1726 reset_shell_surface_type(shsurf);
1727
1728 shsurf->type = shsurf->next_type;
1729 shsurf->next_type = SHELL_SURFACE_NONE;
1730
1731 switch (shsurf->type) {
1732 case SHELL_SURFACE_TOPLEVEL:
1733 break;
1734 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001735 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001736 pes->geometry.x + shsurf->transient.x,
1737 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001738 break;
1739
1740 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001741 case SHELL_SURFACE_FULLSCREEN:
1742 shsurf->saved_x = surface->geometry.x;
1743 shsurf->saved_y = surface->geometry.y;
1744 shsurf->saved_position_valid = true;
1745
1746 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1747 wl_list_remove(&shsurf->rotation.transform.link);
1748 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001749 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001750 shsurf->saved_rotation_valid = true;
1751 }
1752 break;
1753
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001754 case SHELL_SURFACE_XWAYLAND:
1755 weston_surface_set_position(surface, shsurf->transient.x,
1756 shsurf->transient.y);
1757 break;
1758
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001759 default:
1760 break;
1761 }
1762}
1763
1764static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001765set_toplevel(struct shell_surface *shsurf)
1766{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001767 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001768}
1769
1770static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001771shell_surface_set_toplevel(struct wl_client *client,
1772 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001773{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001774 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001775
Tiago Vignattibc052c92012-04-19 16:18:18 +03001776 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001777}
1778
1779static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001780set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001781 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001782{
1783 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001784 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001785 shsurf->transient.x = x;
1786 shsurf->transient.y = y;
1787 shsurf->transient.flags = flags;
1788 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1789}
1790
1791static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001792shell_surface_set_transient(struct wl_client *client,
1793 struct wl_resource *resource,
1794 struct wl_resource *parent_resource,
1795 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001796{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001797 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001798 struct weston_surface *parent =
1799 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001800
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001801 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001802}
1803
Tiago Vignattibe143262012-04-16 17:31:41 +03001804static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001805shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001806{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001807 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001808}
1809
1810static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001811get_output_panel_height(struct desktop_shell *shell,
1812 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001813{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001814 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001815 int panel_height = 0;
1816
1817 if (!output)
1818 return 0;
1819
Juan Zhao4ab94682012-07-09 22:24:09 -07001820 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001821 if (surface->output == output) {
1822 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001823 break;
1824 }
1825 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001826
Juan Zhao96879df2012-02-07 08:45:41 +08001827 return panel_height;
1828}
1829
1830static void
1831shell_surface_set_maximized(struct wl_client *client,
1832 struct wl_resource *resource,
1833 struct wl_resource *output_resource )
1834{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001835 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001836 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001837 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001838 uint32_t edges = 0, panel_height = 0;
1839
1840 /* get the default output, if the client set it as NULL
1841 check whether the ouput is available */
1842 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001843 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001844 else if (es->output)
1845 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001846 else
1847 shsurf->output = get_default_output(es->compositor);
1848
Tiago Vignattibe143262012-04-16 17:31:41 +03001849 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001850 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001851 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001852
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001853 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001854 shsurf->output->width,
1855 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001856
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001857 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001858}
1859
Alex Wu21858432012-04-01 20:13:08 +08001860static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001861black_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 +08001862
Alex Wu4539b082012-03-01 12:57:46 +08001863static struct weston_surface *
1864create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001865 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001866 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001867{
1868 struct weston_surface *surface = NULL;
1869
1870 surface = weston_surface_create(ec);
1871 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001872 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001873 return NULL;
1874 }
1875
Alex Wu21858432012-04-01 20:13:08 +08001876 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001877 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001878 weston_surface_configure(surface, x, y, w, h);
1879 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001880 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001881 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001882 pixman_region32_fini(&surface->input);
1883 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001884
Alex Wu4539b082012-03-01 12:57:46 +08001885 return surface;
1886}
1887
1888/* Create black surface and append it to the associated fullscreen surface.
1889 * Handle size dismatch and positioning according to the method. */
1890static void
1891shell_configure_fullscreen(struct shell_surface *shsurf)
1892{
1893 struct weston_output *output = shsurf->fullscreen_output;
1894 struct weston_surface *surface = shsurf->surface;
1895 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001896 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001897 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001898
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001899 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1900 restore_output_mode(output);
1901
Alex Wu4539b082012-03-01 12:57:46 +08001902 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001903 shsurf->fullscreen.black_surface =
1904 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001905 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001906 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001907 output->width,
1908 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001909
1910 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1911 wl_list_insert(&surface->layer_link,
1912 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001913 shsurf->fullscreen.black_surface->output = output;
1914
Giulio Camuffob8366642013-04-25 13:57:46 +03001915 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1916 &surf_width, &surf_height);
1917
Alex Wu4539b082012-03-01 12:57:46 +08001918 switch (shsurf->fullscreen.type) {
1919 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001920 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001921 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001922 break;
1923 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001924 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001925 if (output->width == surf_width &&
1926 output->height == surf_height) {
1927 weston_surface_set_position(surface, output->x - surf_x,
1928 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001929 break;
1930 }
1931
Alex Wu4539b082012-03-01 12:57:46 +08001932 matrix = &shsurf->fullscreen.transform.matrix;
1933 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001934
Scott Moreau1bad5db2012-08-18 01:04:05 -06001935 output_aspect = (float) output->width /
1936 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001937 surface_aspect = (float) surface->geometry.width /
1938 (float) surface->geometry.height;
1939 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001940 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001941 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001942 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001943 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001944 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001945
Alex Wu4539b082012-03-01 12:57:46 +08001946 weston_matrix_scale(matrix, scale, scale, 1);
1947 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001948 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001949 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001950 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1951 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001952 weston_surface_set_position(surface, x, y);
1953
Alex Wu4539b082012-03-01 12:57:46 +08001954 break;
1955 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001956 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001957 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001958 surf_width * surface->buffer_scale,
1959 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001960 shsurf->fullscreen.framerate};
1961
Hardening57388e42013-09-18 23:56:36 +02001962 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
1963 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001964 weston_surface_set_position(surface,
1965 output->x - surf_x,
1966 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001967 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001968 output->x - surf_x,
1969 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001970 output->width,
1971 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001972 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001973 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001974 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001975 center_on_output(surface, output);
1976 }
Alex Wubd3354b2012-04-17 17:20:49 +08001977 }
Alex Wu4539b082012-03-01 12:57:46 +08001978 break;
1979 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001980 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001981 break;
1982 default:
1983 break;
1984 }
1985}
1986
1987/* make the fullscreen and black surface at the top */
1988static void
1989shell_stack_fullscreen(struct shell_surface *shsurf)
1990{
Alex Wubd3354b2012-04-17 17:20:49 +08001991 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001992 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001993 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001994
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001995 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001996 wl_list_insert(&shell->fullscreen_layer.surface_list,
1997 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001998 weston_surface_damage(surface);
1999
2000 if (!shsurf->fullscreen.black_surface)
2001 shsurf->fullscreen.black_surface =
2002 create_black_surface(surface->compositor,
2003 surface,
2004 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002005 output->width,
2006 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002007
2008 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002009 wl_list_insert(&surface->layer_link,
2010 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002011 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08002012}
2013
2014static void
2015shell_map_fullscreen(struct shell_surface *shsurf)
2016{
Alex Wu4539b082012-03-01 12:57:46 +08002017 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002018 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002019}
2020
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002021static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002022set_fullscreen(struct shell_surface *shsurf,
2023 uint32_t method,
2024 uint32_t framerate,
2025 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002026{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002027 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002028
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002029 if (output)
2030 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002031 else if (es->output)
2032 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002033 else
2034 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002035
Alex Wu4539b082012-03-01 12:57:46 +08002036 shsurf->fullscreen_output = shsurf->output;
2037 shsurf->fullscreen.type = method;
2038 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002039 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002040
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002041 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002042 shsurf->output->width,
2043 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002044}
2045
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002046static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002047shell_surface_set_fullscreen(struct wl_client *client,
2048 struct wl_resource *resource,
2049 uint32_t method,
2050 uint32_t framerate,
2051 struct wl_resource *output_resource)
2052{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002053 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002054 struct weston_output *output;
2055
2056 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002057 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002058 else
2059 output = NULL;
2060
2061 set_fullscreen(shsurf, method, framerate, output);
2062}
2063
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002064static void
2065set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2066{
2067 /* XXX: using the same fields for transient type */
2068 shsurf->transient.x = x;
2069 shsurf->transient.y = y;
2070 shsurf->transient.flags = flags;
2071 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2072}
2073
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002074static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002075
2076static void
2077destroy_shell_seat(struct wl_listener *listener, void *data)
2078{
2079 struct shell_seat *shseat =
2080 container_of(listener,
2081 struct shell_seat, seat_destroy_listener);
2082 struct shell_surface *shsurf, *prev = NULL;
2083
2084 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002085 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002086 shseat->popup_grab.client = NULL;
2087
2088 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2089 shsurf->popup.shseat = NULL;
2090 if (prev) {
2091 wl_list_init(&prev->popup.grab_link);
2092 }
2093 prev = shsurf;
2094 }
2095 wl_list_init(&prev->popup.grab_link);
2096 }
2097
2098 wl_list_remove(&shseat->seat_destroy_listener.link);
2099 free(shseat);
2100}
2101
2102static struct shell_seat *
2103create_shell_seat(struct weston_seat *seat)
2104{
2105 struct shell_seat *shseat;
2106
2107 shseat = calloc(1, sizeof *shseat);
2108 if (!shseat) {
2109 weston_log("no memory to allocate shell seat\n");
2110 return NULL;
2111 }
2112
2113 shseat->seat = seat;
2114 wl_list_init(&shseat->popup_grab.surfaces_list);
2115
2116 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2117 wl_signal_add(&seat->destroy_signal,
2118 &shseat->seat_destroy_listener);
2119
2120 return shseat;
2121}
2122
2123static struct shell_seat *
2124get_shell_seat(struct weston_seat *seat)
2125{
2126 struct wl_listener *listener;
2127
2128 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2129 if (listener == NULL)
2130 return create_shell_seat(seat);
2131
2132 return container_of(listener,
2133 struct shell_seat, seat_destroy_listener);
2134}
2135
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002136static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002137popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002138{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002139 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002140 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002141 struct shell_seat *shseat =
2142 container_of(grab, struct shell_seat, popup_grab.grab);
2143 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002144 wl_fixed_t sx, sy;
2145
2146 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2147 pointer->x, pointer->y,
2148 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002149
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002150 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002151 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002152 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002153 weston_pointer_set_focus(pointer, NULL,
2154 wl_fixed_from_int(0),
2155 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002156 }
2157}
2158
2159static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002160popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002161{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002162 struct weston_pointer *pointer = grab->pointer;
2163 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002164
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002165 if (pointer->focus_resource) {
2166 weston_surface_from_global_fixed(pointer->focus,
2167 pointer->x, pointer->y,
2168 &sx, &sy);
2169 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2170 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002171}
2172
2173static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002174popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002175 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002176{
2177 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002178 struct shell_seat *shseat =
2179 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002180 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002181 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002182 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002183
Daniel Stone37816df2012-05-16 18:45:18 +01002184 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002185 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002186 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002187 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002188 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002189 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002190 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002191 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002192 }
2193
Daniel Stone4dbadb12012-05-30 16:31:51 +01002194 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002195 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002196}
2197
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002198static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002199 popup_grab_focus,
2200 popup_grab_motion,
2201 popup_grab_button,
2202};
2203
2204static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002205popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002206{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002207 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002208 struct shell_seat *shseat =
2209 container_of(grab, struct shell_seat, popup_grab.grab);
2210 struct shell_surface *shsurf;
2211 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002212
2213 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002214 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002215 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002216 shseat->popup_grab.grab.interface = NULL;
2217 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002218 /* Send the popup_done event to all the popups open */
2219 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002220 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002221 shsurf->popup.shseat = NULL;
2222 if (prev) {
2223 wl_list_init(&prev->popup.grab_link);
2224 }
2225 prev = shsurf;
2226 }
2227 wl_list_init(&prev->popup.grab_link);
2228 wl_list_init(&shseat->popup_grab.surfaces_list);
2229 }
2230}
2231
2232static void
2233add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2234{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002235 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002236
2237 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002238 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002239 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002240 /* We must make sure here that this popup was opened after
2241 * a mouse press, and not just by moving around with other
2242 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002243 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002244 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002245
Rob Bradforddfe31052013-06-26 19:49:11 +01002246 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002247 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002248 } else {
2249 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002250 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002251}
2252
2253static void
2254remove_popup_grab(struct shell_surface *shsurf)
2255{
2256 struct shell_seat *shseat = shsurf->popup.shseat;
2257
2258 wl_list_remove(&shsurf->popup.grab_link);
2259 wl_list_init(&shsurf->popup.grab_link);
2260 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002261 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002262 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002263 }
2264}
2265
2266static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002267shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002268{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002269 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002270 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002271 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002272
2273 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002274
Pekka Paalanen483243f2013-03-08 14:56:50 +02002275 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002276 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2277 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002278
Kristian Høgsberge3148752013-05-06 23:19:49 -04002279 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002280 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002281 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002282 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002283 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002284 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002285}
2286
2287static void
2288shell_surface_set_popup(struct wl_client *client,
2289 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002290 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002291 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002292 struct wl_resource *parent_resource,
2293 int32_t x, int32_t y, uint32_t flags)
2294{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002295 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002296
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002297 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002298 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002299 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002300 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002301 shsurf->popup.x = x;
2302 shsurf->popup.y = y;
2303}
2304
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002305static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002306 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002307 shell_surface_move,
2308 shell_surface_resize,
2309 shell_surface_set_toplevel,
2310 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002311 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002312 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002313 shell_surface_set_maximized,
2314 shell_surface_set_title,
2315 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002316};
2317
2318static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002319destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002320{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002321 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2322
Giulio Camuffo5085a752013-03-25 21:42:45 +01002323 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2324 remove_popup_grab(shsurf);
2325 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002326
Alex Wubd3354b2012-04-17 17:20:49 +08002327 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002328 shell_surface_is_top_fullscreen(shsurf))
2329 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002330
Alex Wuaa08e2d2012-03-05 11:01:40 +08002331 if (shsurf->fullscreen.black_surface)
2332 weston_surface_destroy(shsurf->fullscreen.black_surface);
2333
Alex Wubd3354b2012-04-17 17:20:49 +08002334 /* As destroy_resource() use wl_list_for_each_safe(),
2335 * we can always remove the listener.
2336 */
2337 wl_list_remove(&shsurf->surface_destroy_listener.link);
2338 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002339 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002340 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002341
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002342 wl_list_remove(&shsurf->link);
2343 free(shsurf);
2344}
2345
2346static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002347shell_destroy_shell_surface(struct wl_resource *resource)
2348{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002349 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002350
2351 destroy_shell_surface(shsurf);
2352}
2353
2354static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002355shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002356{
2357 struct shell_surface *shsurf = container_of(listener,
2358 struct shell_surface,
2359 surface_destroy_listener);
2360
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002361 if (shsurf->resource)
2362 wl_resource_destroy(shsurf->resource);
2363 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002364 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002365}
2366
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002367static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002368shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002369
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002370static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002371get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002372{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002373 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002374 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002375 else
2376 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002377}
2378
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002379static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002380create_shell_surface(void *shell, struct weston_surface *surface,
2381 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002382{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002383 struct shell_surface *shsurf;
2384
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002385 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002386 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002387 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002388 }
2389
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002390 shsurf = calloc(1, sizeof *shsurf);
2391 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002392 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002393 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002394 }
2395
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002396 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002397 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002398
Tiago Vignattibc052c92012-04-19 16:18:18 +03002399 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002400 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002401 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002402 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002403 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002404 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2405 shsurf->fullscreen.framerate = 0;
2406 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002407 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002408 wl_list_init(&shsurf->fullscreen.transform.link);
2409
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002410 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002411 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002412 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002413 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002414
2415 /* init link so its safe to always remove it in destroy_shell_surface */
2416 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002417 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002418
Pekka Paalanen460099f2012-01-20 16:48:25 +02002419 /* empty when not in use */
2420 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002421 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002422
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002423 wl_list_init(&shsurf->workspace_transform.link);
2424
Pekka Paalanen98262232011-12-01 10:42:22 +02002425 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002426 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002427
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002428 shsurf->client = client;
2429
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002430 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002431}
2432
2433static void
2434shell_get_shell_surface(struct wl_client *client,
2435 struct wl_resource *resource,
2436 uint32_t id,
2437 struct wl_resource *surface_resource)
2438{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002439 struct weston_surface *surface =
2440 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002441 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002442 struct shell_surface *shsurf;
2443
2444 if (get_shell_surface(surface)) {
2445 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002446 WL_DISPLAY_ERROR_INVALID_OBJECT,
2447 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002448 return;
2449 }
2450
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002451 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002452 if (!shsurf) {
2453 wl_resource_post_error(surface_resource,
2454 WL_DISPLAY_ERROR_INVALID_OBJECT,
2455 "surface->configure already set");
2456 return;
2457 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002458
Jason Ekstranda85118c2013-06-27 20:17:02 -05002459 shsurf->resource =
2460 wl_resource_create(client,
2461 &wl_shell_surface_interface, 1, id);
2462 wl_resource_set_implementation(shsurf->resource,
2463 &shell_surface_implementation,
2464 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002465}
2466
2467static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002468 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002469};
2470
Kristian Høgsberg07937562011-04-12 17:25:42 -04002471static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002472shell_fade(struct desktop_shell *shell, enum fade_type type);
2473
2474static int
2475screensaver_timeout(void *data)
2476{
2477 struct desktop_shell *shell = data;
2478
2479 shell_fade(shell, FADE_OUT);
2480
2481 return 1;
2482}
2483
2484static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002485handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002486{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002487 struct desktop_shell *shell =
2488 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002489
Pekka Paalanen18027e52011-12-02 16:31:49 +02002490 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002491
2492 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002493 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002494}
2495
2496static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002497launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002498{
2499 if (shell->screensaver.binding)
2500 return;
2501
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002502 if (!shell->screensaver.path) {
2503 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002504 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002505 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002506
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002507 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002508 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002509 return;
2510 }
2511
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002512 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002513 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002514 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002515 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002516}
2517
2518static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002519terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002520{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002521 if (shell->screensaver.process.pid == 0)
2522 return;
2523
2524 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002525}
2526
2527static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002528configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002529{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002530 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002531
Giulio Camuffo184df502013-02-21 11:29:21 +01002532 if (width == 0)
2533 return;
2534
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002535 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2536 if (s->output == es->output && s != es) {
2537 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002538 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002539 }
2540 }
2541
Giulio Camuffo184df502013-02-21 11:29:21 +01002542 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002543
2544 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002545 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002546 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002547 }
2548}
2549
2550static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002551background_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 -04002552{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002553 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002554
Giulio Camuffo184df502013-02-21 11:29:21 +01002555 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002556}
2557
2558static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002559desktop_shell_set_background(struct wl_client *client,
2560 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002561 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002562 struct wl_resource *surface_resource)
2563{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002564 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002565 struct weston_surface *surface =
2566 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002567
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002568 if (surface->configure) {
2569 wl_resource_post_error(surface_resource,
2570 WL_DISPLAY_ERROR_INVALID_OBJECT,
2571 "surface role already assigned");
2572 return;
2573 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002574
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002575 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002576 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002577 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002578 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002579 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002580 surface->output->width,
2581 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002582}
2583
2584static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002585panel_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 -04002586{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002587 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002588
Giulio Camuffo184df502013-02-21 11:29:21 +01002589 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002590}
2591
2592static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002593desktop_shell_set_panel(struct wl_client *client,
2594 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002595 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002596 struct wl_resource *surface_resource)
2597{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002598 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002599 struct weston_surface *surface =
2600 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002601
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002602 if (surface->configure) {
2603 wl_resource_post_error(surface_resource,
2604 WL_DISPLAY_ERROR_INVALID_OBJECT,
2605 "surface role already assigned");
2606 return;
2607 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002608
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002609 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002610 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002611 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002612 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002613 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002614 surface->output->width,
2615 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002616}
2617
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002618static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002619lock_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 -04002620{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002621 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002622
Giulio Camuffo184df502013-02-21 11:29:21 +01002623 if (width == 0)
2624 return;
2625
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002626 surface->geometry.width = width;
2627 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002628 center_on_output(surface, get_default_output(shell->compositor));
2629
2630 if (!weston_surface_is_mapped(surface)) {
2631 wl_list_insert(&shell->lock_layer.surface_list,
2632 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002633 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002634 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002635 }
2636}
2637
2638static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002639handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002640{
Tiago Vignattibe143262012-04-16 17:31:41 +03002641 struct desktop_shell *shell =
2642 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002643
Martin Minarik6d118362012-06-07 18:01:59 +02002644 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002645 shell->lock_surface = NULL;
2646}
2647
2648static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002649desktop_shell_set_lock_surface(struct wl_client *client,
2650 struct wl_resource *resource,
2651 struct wl_resource *surface_resource)
2652{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002653 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002654 struct weston_surface *surface =
2655 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002656
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002657 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002658
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002659 if (!shell->locked)
2660 return;
2661
Pekka Paalanen98262232011-12-01 10:42:22 +02002662 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002663
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002664 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002665 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002666 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002667
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002668 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002669 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002670}
2671
2672static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002673resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002674{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002675 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002676
Pekka Paalanen77346a62011-11-30 16:26:35 +02002677 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002678
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002679 wl_list_remove(&shell->lock_layer.link);
2680 wl_list_insert(&shell->compositor->cursor_layer.link,
2681 &shell->fullscreen_layer.link);
2682 wl_list_insert(&shell->fullscreen_layer.link,
2683 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002684 if (shell->showing_input_panels) {
2685 wl_list_insert(&shell->panel_layer.link,
2686 &shell->input_panel_layer.link);
2687 wl_list_insert(&shell->input_panel_layer.link,
2688 &ws->layer.link);
2689 } else {
2690 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2691 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002692
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002693 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002694
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002695 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002696 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002697 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002698}
2699
2700static void
2701desktop_shell_unlock(struct wl_client *client,
2702 struct wl_resource *resource)
2703{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002704 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002705
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002706 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002707
2708 if (shell->locked)
2709 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002710}
2711
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002712static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002713desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002714 struct wl_resource *resource,
2715 struct wl_resource *surface_resource)
2716{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002717 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002718
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002719 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002720}
2721
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002722static void
2723desktop_shell_desktop_ready(struct wl_client *client,
2724 struct wl_resource *resource)
2725{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002726 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002727
2728 shell_fade_startup(shell);
2729}
2730
Kristian Høgsberg75840622011-09-06 13:48:16 -04002731static const struct desktop_shell_interface desktop_shell_implementation = {
2732 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002733 desktop_shell_set_panel,
2734 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002735 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002736 desktop_shell_set_grab_surface,
2737 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002738};
2739
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002740static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002741get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002742{
2743 struct shell_surface *shsurf;
2744
2745 shsurf = get_shell_surface(surface);
2746 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002747 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002748 return shsurf->type;
2749}
2750
Kristian Høgsberg75840622011-09-06 13:48:16 -04002751static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002752move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002753{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002754 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002755 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002756 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002757 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002758
Pekka Paalanen01388e22013-04-25 13:57:44 +03002759 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002760 if (surface == NULL)
2761 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002762
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002763 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002764 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2765 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002766 return;
2767
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002768 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002769}
2770
2771static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002772resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002773{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002774 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002775 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002776 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002777 uint32_t edges = 0;
2778 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002779 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002780
Pekka Paalanen01388e22013-04-25 13:57:44 +03002781 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002782 if (surface == NULL)
2783 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002784
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002785 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002786 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2787 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002788 return;
2789
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002790 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002791 wl_fixed_to_int(seat->pointer->grab_x),
2792 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002793 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002794
Pekka Paalanen60921e52012-01-25 15:55:43 +02002795 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002796 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002797 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002798 edges |= 0;
2799 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002800 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002801
Pekka Paalanen60921e52012-01-25 15:55:43 +02002802 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002803 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002804 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002805 edges |= 0;
2806 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002807 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002808
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002809 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002810}
2811
2812static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002813surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002814 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002815{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002816 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002817 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002818 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002819 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002820 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002821
Pekka Paalanen01388e22013-04-25 13:57:44 +03002822 /* XXX: broken for windows containing sub-surfaces */
2823 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002824 if (surface == NULL)
2825 return;
2826
2827 shsurf = get_shell_surface(surface);
2828 if (!shsurf)
2829 return;
2830
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002831 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002832
Scott Moreau02709af2012-05-22 01:54:10 -06002833 if (surface->alpha > 1.0)
2834 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002835 if (surface->alpha < step)
2836 surface->alpha = step;
2837
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002838 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002839 weston_surface_damage(surface);
2840}
2841
2842static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002843do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002844 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002845{
Daniel Stone37816df2012-05-16 18:45:18 +01002846 struct weston_seat *ws = (struct weston_seat *) seat;
2847 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002848 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002849 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002850
2851 wl_list_for_each(output, &compositor->output_list, link) {
2852 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002853 wl_fixed_to_double(seat->pointer->x),
2854 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002855 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002856 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002857 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002858 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002859 increment = -output->zoom.increment;
2860 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002861 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002862 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002863 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002864 else
2865 increment = 0;
2866
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002867 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002868
Scott Moreaue6603982012-06-11 13:07:51 -06002869 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002870 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002871 else if (output->zoom.level > output->zoom.max_level)
2872 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002873 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002874 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002875 output->disable_planes++;
2876 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002877
Scott Moreaue6603982012-06-11 13:07:51 -06002878 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002879
Scott Moreau8dacaab2012-06-17 18:10:58 -06002880 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002881 }
2882 }
2883}
2884
Scott Moreauccbf29d2012-02-22 14:21:41 -07002885static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002886zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002887 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002888{
2889 do_zoom(seat, time, 0, axis, value);
2890}
2891
2892static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002893zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002894 void *data)
2895{
2896 do_zoom(seat, time, key, 0, 0);
2897}
2898
2899static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002900terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002901 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002902{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002903 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002904
Daniel Stone325fc2d2012-05-30 16:31:58 +01002905 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002906}
2907
2908static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002909rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002910{
2911 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002912 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002913 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002914 struct shell_surface *shsurf = rotate->base.shsurf;
2915 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002916 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002917
2918 if (!shsurf)
2919 return;
2920
2921 surface = shsurf->surface;
2922
2923 cx = 0.5f * surface->geometry.width;
2924 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002925
Daniel Stone37816df2012-05-16 18:45:18 +01002926 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2927 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002928 r = sqrtf(dx * dx + dy * dy);
2929
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002930 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002931 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002932
2933 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002934 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002935 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002936
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002937 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002938 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002939
2940 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002941 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002942 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002943 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002944 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002945
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002946 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002947 &shsurf->surface->geometry.transformation_list,
2948 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002949 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002950 wl_list_init(&shsurf->rotation.transform.link);
2951 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002952 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002953 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002954
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002955 /* We need to adjust the position of the surface
2956 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002957 cposx = surface->geometry.x + cx;
2958 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002959 dposx = rotate->center.x - cposx;
2960 dposy = rotate->center.y - cposy;
2961 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002962 weston_surface_set_position(surface,
2963 surface->geometry.x + dposx,
2964 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002965 }
2966
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002967 /* Repaint implies weston_surface_update_transform(), which
2968 * lazily applies the damage due to rotation update.
2969 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002970 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002971}
2972
2973static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002974rotate_grab_button(struct weston_pointer_grab *grab,
2975 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002976{
2977 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002978 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002979 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002980 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002981 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002982
Daniel Stone4dbadb12012-05-30 16:31:51 +01002983 if (pointer->button_count == 0 &&
2984 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002985 if (shsurf)
2986 weston_matrix_multiply(&shsurf->rotation.rotation,
2987 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002988 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002989 free(rotate);
2990 }
2991}
2992
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002993static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002994 noop_grab_focus,
2995 rotate_grab_motion,
2996 rotate_grab_button,
2997};
2998
2999static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003000surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003001{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003002 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003003 float dx, dy;
3004 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003005
Pekka Paalanen460099f2012-01-20 16:48:25 +02003006 rotate = malloc(sizeof *rotate);
3007 if (!rotate)
3008 return;
3009
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04003010 weston_surface_to_global_float(surface->surface,
Louis-Francis Ratté-Boulianne9a4f10f2013-07-03 15:58:22 +02003011 surface->surface->geometry.width * 0.5f,
3012 surface->surface->geometry.height * 0.5f,
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04003013 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003014
Daniel Stone37816df2012-05-16 18:45:18 +01003015 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3016 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003017 r = sqrtf(dx * dx + dy * dy);
3018 if (r > 20.0f) {
3019 struct weston_matrix inverse;
3020
3021 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003022 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003023 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003024
3025 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003026 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003027 } else {
3028 weston_matrix_init(&surface->rotation.rotation);
3029 weston_matrix_init(&rotate->rotation);
3030 }
3031
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003032 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3033 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003034}
3035
3036static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003037rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003038 void *data)
3039{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003040 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003041 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003042 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003043 struct shell_surface *surface;
3044
Pekka Paalanen01388e22013-04-25 13:57:44 +03003045 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003046 if (base_surface == NULL)
3047 return;
3048
3049 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003050 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3051 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003052 return;
3053
3054 surface_rotate(surface, seat);
3055}
3056
3057static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003058lower_fullscreen_layer(struct desktop_shell *shell)
3059{
3060 struct workspace *ws;
3061 struct weston_surface *surface, *prev;
3062
3063 ws = get_current_workspace(shell);
3064 wl_list_for_each_reverse_safe(surface, prev,
3065 &shell->fullscreen_layer.surface_list,
3066 layer_link)
3067 weston_surface_restack(surface, &ws->layer.surface_list);
3068}
3069
3070static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003071activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003072 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003073{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003074 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003075 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003076 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003077
Pekka Paalanen01388e22013-04-25 13:57:44 +03003078 main_surface = weston_surface_get_main_surface(es);
3079
Daniel Stone37816df2012-05-16 18:45:18 +01003080 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003081
Jonas Ådahl8538b222012-08-29 22:13:03 +02003082 state = ensure_focus_state(shell, seat);
3083 if (state == NULL)
3084 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003085
3086 state->keyboard_focus = es;
3087 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003088 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003089
Pekka Paalanen01388e22013-04-25 13:57:44 +03003090 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003091 case SHELL_SURFACE_FULLSCREEN:
3092 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003093 shell_stack_fullscreen(get_shell_surface(main_surface));
3094 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08003095 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003096 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003097 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003098 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003099 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003100 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003101 }
3102}
3103
Alex Wu21858432012-04-01 20:13:08 +08003104/* no-op func for checking black surface */
3105static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003106black_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 +08003107{
3108}
3109
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003110static bool
Alex Wu21858432012-04-01 20:13:08 +08003111is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3112{
3113 if (es->configure == black_surface_configure) {
3114 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003115 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003116 return true;
3117 }
3118 return false;
3119}
3120
Kristian Høgsberg75840622011-09-06 13:48:16 -04003121static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003122click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003123 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003124{
Daniel Stone37816df2012-05-16 18:45:18 +01003125 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003126 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003127 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003128 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003129
Daniel Stone37816df2012-05-16 18:45:18 +01003130 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003131 if (!focus)
3132 return;
3133
Pekka Paalanen01388e22013-04-25 13:57:44 +03003134 if (is_black_surface(focus, &main_surface))
3135 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003136
Pekka Paalanen01388e22013-04-25 13:57:44 +03003137 main_surface = weston_surface_get_main_surface(focus);
3138 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003139 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003140
Daniel Stone325fc2d2012-05-30 16:31:58 +01003141 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003142 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003143}
3144
3145static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003146lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003147{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003148 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003149
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003150 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003151 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003152 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003153 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003154
3155 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003156
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003157 /* Hide all surfaces by removing the fullscreen, panel and
3158 * toplevel layers. This way nothing else can show or receive
3159 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003160
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003161 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003162 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003163 if (shell->showing_input_panels)
3164 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003165 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003166 wl_list_insert(&shell->compositor->cursor_layer.link,
3167 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003168
Pekka Paalanen77346a62011-11-30 16:26:35 +02003169 launch_screensaver(shell);
3170
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003171 /* TODO: disable bindings that should not work while locked. */
3172
3173 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003174}
3175
3176static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003177unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003178{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003179 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003180 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003181 return;
3182 }
3183
3184 /* If desktop-shell client has gone away, unlock immediately. */
3185 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003186 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003187 return;
3188 }
3189
3190 if (shell->prepare_event_sent)
3191 return;
3192
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003193 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003194 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003195}
3196
3197static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003198shell_fade_done(struct weston_surface_animation *animation, void *data)
3199{
3200 struct desktop_shell *shell = data;
3201
3202 shell->fade.animation = NULL;
3203
3204 switch (shell->fade.type) {
3205 case FADE_IN:
3206 weston_surface_destroy(shell->fade.surface);
3207 shell->fade.surface = NULL;
3208 break;
3209 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003210 lock(shell);
3211 break;
3212 }
3213}
3214
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003215static struct weston_surface *
3216shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003217{
3218 struct weston_compositor *compositor = shell->compositor;
3219 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003220
3221 surface = weston_surface_create(compositor);
3222 if (!surface)
3223 return NULL;
3224
3225 weston_surface_configure(surface, 0, 0, 8192, 8192);
3226 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3227 wl_list_insert(&compositor->fade_layer.surface_list,
3228 &surface->layer_link);
3229 pixman_region32_init(&surface->input);
3230
3231 return surface;
3232}
3233
3234static void
3235shell_fade(struct desktop_shell *shell, enum fade_type type)
3236{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003237 float tint;
3238
3239 switch (type) {
3240 case FADE_IN:
3241 tint = 0.0;
3242 break;
3243 case FADE_OUT:
3244 tint = 1.0;
3245 break;
3246 default:
3247 weston_log("shell: invalid fade type\n");
3248 return;
3249 }
3250
3251 shell->fade.type = type;
3252
3253 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003254 shell->fade.surface = shell_fade_create_surface(shell);
3255 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003256 return;
3257
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003258 shell->fade.surface->alpha = 1.0 - tint;
3259 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003260 }
3261
3262 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003263 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003264 else
3265 shell->fade.animation =
3266 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003267 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003268 shell_fade_done, shell);
3269}
3270
3271static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003272do_shell_fade_startup(void *data)
3273{
3274 struct desktop_shell *shell = data;
3275
3276 shell_fade(shell, FADE_IN);
3277}
3278
3279static void
3280shell_fade_startup(struct desktop_shell *shell)
3281{
3282 struct wl_event_loop *loop;
3283
3284 if (!shell->fade.startup_timer)
3285 return;
3286
3287 wl_event_source_remove(shell->fade.startup_timer);
3288 shell->fade.startup_timer = NULL;
3289
3290 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3291 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3292}
3293
3294static int
3295fade_startup_timeout(void *data)
3296{
3297 struct desktop_shell *shell = data;
3298
3299 shell_fade_startup(shell);
3300 return 0;
3301}
3302
3303static void
3304shell_fade_init(struct desktop_shell *shell)
3305{
3306 /* Make compositor output all black, and wait for the desktop-shell
3307 * client to signal it is ready, then fade in. The timer triggers a
3308 * fade-in, in case the desktop-shell client takes too long.
3309 */
3310
3311 struct wl_event_loop *loop;
3312
3313 if (shell->fade.surface != NULL) {
3314 weston_log("%s: warning: fade surface already exists\n",
3315 __func__);
3316 return;
3317 }
3318
3319 shell->fade.surface = shell_fade_create_surface(shell);
3320 if (!shell->fade.surface)
3321 return;
3322
3323 weston_surface_update_transform(shell->fade.surface);
3324 weston_surface_damage(shell->fade.surface);
3325
3326 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3327 shell->fade.startup_timer =
3328 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3329 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3330}
3331
3332static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003333idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003334{
3335 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003336 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003337
3338 shell_fade(shell, FADE_OUT);
3339 /* lock() is called from shell_fade_done() */
3340}
3341
3342static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003343wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003344{
3345 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003346 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003347
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003348 unlock(shell);
3349}
3350
3351static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003352show_input_panels(struct wl_listener *listener, void *data)
3353{
3354 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003355 container_of(listener, struct desktop_shell,
3356 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003357 struct input_panel_surface *surface, *next;
3358 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003359
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003360 shell->text_input.surface = (struct weston_surface*)data;
3361
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003362 if (shell->showing_input_panels)
3363 return;
3364
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003365 shell->showing_input_panels = true;
3366
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003367 if (!shell->locked)
3368 wl_list_insert(&shell->panel_layer.link,
3369 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003370
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003371 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003372 &shell->input_panel.surfaces, link) {
3373 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003374 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003375 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003376 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003377 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003378 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003379 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003380 weston_surface_damage(ws);
3381 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003382 }
3383}
3384
3385static void
3386hide_input_panels(struct wl_listener *listener, void *data)
3387{
3388 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003389 container_of(listener, struct desktop_shell,
3390 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003391 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003392
Jan Arne Petersen61381972013-01-31 15:52:21 +01003393 if (!shell->showing_input_panels)
3394 return;
3395
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003396 shell->showing_input_panels = false;
3397
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003398 if (!shell->locked)
3399 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003400
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003401 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003402 &shell->input_panel_layer.surface_list, layer_link)
3403 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003404}
3405
3406static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003407update_input_panels(struct wl_listener *listener, void *data)
3408{
3409 struct desktop_shell *shell =
3410 container_of(listener, struct desktop_shell,
3411 update_input_panel_listener);
3412
3413 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3414}
3415
3416static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003417center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003418{
Giulio Camuffob8366642013-04-25 13:57:46 +03003419 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003420 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003421
Giulio Camuffob8366642013-04-25 13:57:46 +03003422 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3423
3424 x = output->x + (output->width - width) / 2 - surf_x / 2;
3425 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003426
3427 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003428}
3429
3430static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003431weston_surface_set_initial_position (struct weston_surface *surface,
3432 struct desktop_shell *shell)
3433{
3434 struct weston_compositor *compositor = shell->compositor;
3435 int ix = 0, iy = 0;
3436 int range_x, range_y;
3437 int dx, dy, x, y, panel_height;
3438 struct weston_output *output, *target_output = NULL;
3439 struct weston_seat *seat;
3440
3441 /* As a heuristic place the new window on the same output as the
3442 * pointer. Falling back to the output containing 0, 0.
3443 *
3444 * TODO: Do something clever for touch too?
3445 */
3446 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003447 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003448 ix = wl_fixed_to_int(seat->pointer->x);
3449 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003450 break;
3451 }
3452 }
3453
3454 wl_list_for_each(output, &compositor->output_list, link) {
3455 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3456 target_output = output;
3457 break;
3458 }
3459 }
3460
3461 if (!target_output) {
3462 weston_surface_set_position(surface, 10 + random() % 400,
3463 10 + random() % 400);
3464 return;
3465 }
3466
3467 /* Valid range within output where the surface will still be onscreen.
3468 * If this is negative it means that the surface is bigger than
3469 * output.
3470 */
3471 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003472 range_x = target_output->width - surface->geometry.width;
3473 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003474 surface->geometry.height;
3475
Rob Bradford4cb88c72012-08-13 15:18:44 +01003476 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003477 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003478 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003479 dx = 0;
3480
3481 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003482 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003483 else
3484 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003485
3486 x = target_output->x + dx;
3487 y = target_output->y + dy;
3488
3489 weston_surface_set_position (surface, x, y);
3490}
3491
3492static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003493map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003494 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003495{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003496 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003497 struct shell_surface *shsurf = get_shell_surface(surface);
3498 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003499 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003500 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003501 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003502 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003503 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003504
Pekka Paalanen60921e52012-01-25 15:55:43 +02003505 surface->geometry.width = width;
3506 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003507 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003508
3509 /* initial positioning, see also configure() */
3510 switch (surface_type) {
3511 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003512 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003513 break;
Alex Wu4539b082012-03-01 12:57:46 +08003514 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003515 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003516 shell_map_fullscreen(shsurf);
3517 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003518 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003519 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003520 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003521 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3522 NULL, NULL);
3523 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3524 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003525 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003526 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003527 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003528 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003529 case SHELL_SURFACE_NONE:
3530 weston_surface_set_position(surface,
3531 surface->geometry.x + sx,
3532 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003533 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003534 default:
3535 ;
3536 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003537
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003538 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003539 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003540 case SHELL_SURFACE_POPUP:
3541 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003542 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003543 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3544 break;
Alex Wu4539b082012-03-01 12:57:46 +08003545 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003546 case SHELL_SURFACE_NONE:
3547 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003548 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003549 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003550 ws = get_current_workspace(shell);
3551 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003552 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003553 }
3554
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003555 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003556 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003557 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3558 surface->output = shsurf->output;
3559 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003560
Juan Zhao7bb92f02011-12-15 11:31:51 -05003561 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003562 /* XXX: xwayland's using the same fields for transient type */
3563 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003564 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003565 if (shsurf->transient.flags ==
3566 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3567 break;
3568 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003569 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003570 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003571 if (!shell->locked) {
3572 wl_list_for_each(seat, &compositor->seat_list, link)
3573 activate(shell, surface, seat);
3574 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003575 break;
3576 default:
3577 break;
3578 }
3579
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003580 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003581 {
3582 switch (shell->win_animation_type) {
3583 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003584 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003585 break;
3586 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003587 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003588 break;
3589 default:
3590 break;
3591 }
3592 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003593}
3594
3595static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003596configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003597 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003598{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003599 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3600 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003601 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003602
Pekka Paalanen77346a62011-11-30 16:26:35 +02003603 shsurf = get_shell_surface(surface);
3604 if (shsurf)
3605 surface_type = shsurf->type;
3606
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003607 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003608
3609 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003610 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003611 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003612 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003613 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003614 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003615 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003616 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3617 NULL, NULL);
3618 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003619 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003620 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003621 break;
Alex Wu4539b082012-03-01 12:57:46 +08003622 case SHELL_SURFACE_TOPLEVEL:
3623 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003624 default:
3625 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003626 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003627
Alex Wu4539b082012-03-01 12:57:46 +08003628 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003629 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003630 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003631
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003632 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003633 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003634 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003635}
3636
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003637static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003638shell_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 +03003639{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003640 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003641 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003642
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003643 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003644
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003645 if (!weston_surface_is_mapped(es) &&
3646 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003647 remove_popup_grab(shsurf);
3648 }
3649
Giulio Camuffo184df502013-02-21 11:29:21 +01003650 if (width == 0)
3651 return;
3652
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003653 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003654 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003655 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003656 type_changed = 1;
3657 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003658
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003659 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003660 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003661 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003662 es->geometry.width != width ||
3663 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003664 float from_x, from_y;
3665 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003666
3667 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3668 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3669 configure(shell, es,
3670 es->geometry.x + to_x - from_x,
3671 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003672 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003673 }
3674}
3675
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003676static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003677
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003678static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003679desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003680{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003681 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003682 struct desktop_shell *shell =
3683 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003684
3685 shell->child.process.pid = 0;
3686 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003687
3688 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3689 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003690 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003691 shell->child.deathstamp = time;
3692 shell->child.deathcount = 0;
3693 }
3694
3695 shell->child.deathcount++;
3696 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003697 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003698 return;
3699 }
3700
Martin Minarik6d118362012-06-07 18:01:59 +02003701 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003702 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003703 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003704}
3705
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003706static void
3707launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003708{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003709 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003710 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003711
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003712 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003713 &shell->child.process,
3714 shell_exe,
3715 desktop_shell_sigchld);
3716
3717 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003718 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003719}
3720
3721static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003722bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3723{
Tiago Vignattibe143262012-04-16 17:31:41 +03003724 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003725 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003726
Jason Ekstranda85118c2013-06-27 20:17:02 -05003727 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3728 if (resource)
3729 wl_resource_set_implementation(resource, &shell_implementation,
3730 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003731}
3732
Kristian Høgsberg75840622011-09-06 13:48:16 -04003733static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003734unbind_desktop_shell(struct wl_resource *resource)
3735{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003736 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003737
3738 if (shell->locked)
3739 resume_desktop(shell);
3740
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003741 shell->child.desktop_shell = NULL;
3742 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003743}
3744
3745static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003746bind_desktop_shell(struct wl_client *client,
3747 void *data, uint32_t version, uint32_t id)
3748{
Tiago Vignattibe143262012-04-16 17:31:41 +03003749 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003750 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003751
Jason Ekstranda85118c2013-06-27 20:17:02 -05003752 resource = wl_resource_create(client, &desktop_shell_interface,
3753 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003754
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003755 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003756 wl_resource_set_implementation(resource,
3757 &desktop_shell_implementation,
3758 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003759 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003760
3761 if (version < 2)
3762 shell_fade_startup(shell);
3763
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003764 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003765 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003766
3767 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3768 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003769 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003770}
3771
Pekka Paalanen6e168112011-11-24 11:34:05 +02003772static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003773screensaver_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 -04003774{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003775 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003776
Giulio Camuffo184df502013-02-21 11:29:21 +01003777 if (width == 0)
3778 return;
3779
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003780 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003781 if (!shell->locked)
3782 return;
3783
3784 center_on_output(surface, surface->output);
3785
3786 if (wl_list_empty(&surface->layer_link)) {
3787 wl_list_insert(shell->lock_layer.surface_list.prev,
3788 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003789 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003790 wl_event_source_timer_update(shell->screensaver.timer,
3791 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003792 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003793 }
3794}
3795
3796static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003797screensaver_set_surface(struct wl_client *client,
3798 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003799 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003800 struct wl_resource *output_resource)
3801{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003802 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003803 struct weston_surface *surface =
3804 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003805 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003806
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003807 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003808 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003809 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003810}
3811
3812static const struct screensaver_interface screensaver_implementation = {
3813 screensaver_set_surface
3814};
3815
3816static void
3817unbind_screensaver(struct wl_resource *resource)
3818{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003819 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003820
Pekka Paalanen77346a62011-11-30 16:26:35 +02003821 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003822}
3823
3824static void
3825bind_screensaver(struct wl_client *client,
3826 void *data, uint32_t version, uint32_t id)
3827{
Tiago Vignattibe143262012-04-16 17:31:41 +03003828 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003829 struct wl_resource *resource;
3830
Jason Ekstranda85118c2013-06-27 20:17:02 -05003831 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003832
Pekka Paalanen77346a62011-11-30 16:26:35 +02003833 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003834 wl_resource_set_implementation(resource,
3835 &screensaver_implementation,
3836 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003837 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003838 return;
3839 }
3840
3841 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3842 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003843 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003844}
3845
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003846static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003847input_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 -04003848{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003849 struct input_panel_surface *ip_surface = surface->configure_private;
3850 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003851 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003852 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003853
Giulio Camuffo184df502013-02-21 11:29:21 +01003854 if (width == 0)
3855 return;
3856
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003857 if (!weston_surface_is_mapped(surface)) {
3858 if (!shell->showing_input_panels)
3859 return;
3860
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003861 show_surface = 1;
3862 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003863
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003864 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003865
3866 if (ip_surface->panel) {
3867 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3868 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3869 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01003870 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
3871 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003872 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003873
3874 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003875 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003876 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003877
3878 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003879 wl_list_insert(&shell->input_panel_layer.surface_list,
3880 &surface->layer_link);
3881 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003882 weston_surface_damage(surface);
3883 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3884 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003885}
3886
3887static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003888destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003889{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003890 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3891
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003892 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003893 wl_list_remove(&input_panel_surface->link);
3894
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003895 input_panel_surface->surface->configure = NULL;
3896
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003897 free(input_panel_surface);
3898}
3899
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003900static struct input_panel_surface *
3901get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003902{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003903 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003904 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003905 } else {
3906 return NULL;
3907 }
3908}
3909
3910static void
3911input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3912{
3913 struct input_panel_surface *ipsurface = container_of(listener,
3914 struct input_panel_surface,
3915 surface_destroy_listener);
3916
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003917 if (ipsurface->resource) {
3918 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003919 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003920 destroy_input_panel_surface(ipsurface);
3921 }
3922}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003923
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003924static struct input_panel_surface *
3925create_input_panel_surface(struct desktop_shell *shell,
3926 struct weston_surface *surface)
3927{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003928 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003929
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003930 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3931 if (!input_panel_surface)
3932 return NULL;
3933
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003934 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003935 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003936
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003937 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003938
3939 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003940
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003941 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003942 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003943 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003944 &input_panel_surface->surface_destroy_listener);
3945
3946 wl_list_init(&input_panel_surface->link);
3947
3948 return input_panel_surface;
3949}
3950
3951static void
3952input_panel_surface_set_toplevel(struct wl_client *client,
3953 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003954 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003955 uint32_t position)
3956{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003957 struct input_panel_surface *input_panel_surface =
3958 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003959 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003960
3961 wl_list_insert(&shell->input_panel.surfaces,
3962 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003963
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003964 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003965 input_panel_surface->panel = 0;
3966}
3967
3968static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003969input_panel_surface_set_overlay_panel(struct wl_client *client,
3970 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003971{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003972 struct input_panel_surface *input_panel_surface =
3973 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003974 struct desktop_shell *shell = input_panel_surface->shell;
3975
3976 wl_list_insert(&shell->input_panel.surfaces,
3977 &input_panel_surface->link);
3978
3979 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003980}
3981
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003982static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003983 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003984 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003985};
3986
3987static void
3988destroy_input_panel_surface_resource(struct wl_resource *resource)
3989{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003990 struct input_panel_surface *ipsurf =
3991 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003992
3993 destroy_input_panel_surface(ipsurf);
3994}
3995
3996static void
3997input_panel_get_input_panel_surface(struct wl_client *client,
3998 struct wl_resource *resource,
3999 uint32_t id,
4000 struct wl_resource *surface_resource)
4001{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004002 struct weston_surface *surface =
4003 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004004 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004005 struct input_panel_surface *ipsurf;
4006
4007 if (get_input_panel_surface(surface)) {
4008 wl_resource_post_error(surface_resource,
4009 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004010 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004011 return;
4012 }
4013
4014 ipsurf = create_input_panel_surface(shell, surface);
4015 if (!ipsurf) {
4016 wl_resource_post_error(surface_resource,
4017 WL_DISPLAY_ERROR_INVALID_OBJECT,
4018 "surface->configure already set");
4019 return;
4020 }
4021
Jason Ekstranda85118c2013-06-27 20:17:02 -05004022 ipsurf->resource =
4023 wl_resource_create(client,
4024 &wl_input_panel_surface_interface, 1, id);
4025 wl_resource_set_implementation(ipsurf->resource,
4026 &input_panel_surface_implementation,
4027 ipsurf,
4028 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004029}
4030
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004031static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004032 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004033};
4034
4035static void
4036unbind_input_panel(struct wl_resource *resource)
4037{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004038 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004039
4040 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004041}
4042
4043static void
4044bind_input_panel(struct wl_client *client,
4045 void *data, uint32_t version, uint32_t id)
4046{
4047 struct desktop_shell *shell = data;
4048 struct wl_resource *resource;
4049
Jason Ekstranda85118c2013-06-27 20:17:02 -05004050 resource = wl_resource_create(client,
4051 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004052
4053 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004054 wl_resource_set_implementation(resource,
4055 &input_panel_implementation,
4056 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004057 shell->input_panel.binding = resource;
4058 return;
4059 }
4060
4061 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4062 "interface object already bound");
4063 wl_resource_destroy(resource);
4064}
4065
Kristian Høgsberg07045392012-02-19 18:52:44 -05004066struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004067 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004068 struct weston_surface *current;
4069 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004070 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004071};
4072
4073static void
4074switcher_next(struct switcher *switcher)
4075{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004076 struct weston_surface *surface;
4077 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004078 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004079 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004080
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004081 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004082 switch (get_shell_surface_type(surface)) {
4083 case SHELL_SURFACE_TOPLEVEL:
4084 case SHELL_SURFACE_FULLSCREEN:
4085 case SHELL_SURFACE_MAXIMIZED:
4086 if (first == NULL)
4087 first = surface;
4088 if (prev == switcher->current)
4089 next = surface;
4090 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06004091 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02004092 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004093 weston_surface_damage(surface);
4094 break;
4095 default:
4096 break;
4097 }
Alex Wu1659daa2012-04-01 20:13:09 +08004098
4099 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06004100 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02004101 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004102 weston_surface_damage(surface);
4103 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004104 }
4105
4106 if (next == NULL)
4107 next = first;
4108
Alex Wu07b26062012-03-12 16:06:01 +08004109 if (next == NULL)
4110 return;
4111
Kristian Høgsberg07045392012-02-19 18:52:44 -05004112 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004113 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004114
4115 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004116 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004117
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004118 shsurf = get_shell_surface(switcher->current);
4119 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004120 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004121}
4122
4123static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004124switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004125{
4126 struct switcher *switcher =
4127 container_of(listener, struct switcher, listener);
4128
4129 switcher_next(switcher);
4130}
4131
4132static void
Daniel Stone351eb612012-05-31 15:27:47 -04004133switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004134{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004135 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004136 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004137 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004138
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004139 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004140 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004141 weston_surface_damage(surface);
4142 }
4143
Alex Wu07b26062012-03-12 16:06:01 +08004144 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004145 activate(switcher->shell, switcher->current,
4146 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004147 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004148 weston_keyboard_end_grab(keyboard);
4149 if (keyboard->input_method_resource)
4150 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004151 free(switcher);
4152}
4153
4154static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004155switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004156 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004157{
4158 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004159 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004160
Daniel Stonec9785ea2012-05-30 16:31:52 +01004161 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004162 switcher_next(switcher);
4163}
4164
4165static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004166switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004167 uint32_t mods_depressed, uint32_t mods_latched,
4168 uint32_t mods_locked, uint32_t group)
4169{
4170 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004171 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004172
Daniel Stone351eb612012-05-31 15:27:47 -04004173 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4174 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004175}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004176
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004177static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004178 switcher_key,
4179 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004180};
4181
4182static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004183switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004184 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004185{
Tiago Vignattibe143262012-04-16 17:31:41 +03004186 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004187 struct switcher *switcher;
4188
4189 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004190 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004191 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004192 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004193 wl_list_init(&switcher->listener.link);
4194
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004195 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004196 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004197 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004198 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4199 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004200 switcher_next(switcher);
4201}
4202
Pekka Paalanen3c647232011-12-22 13:43:43 +02004203static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004204backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004205 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004206{
4207 struct weston_compositor *compositor = data;
4208 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004209 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004210
4211 /* TODO: we're limiting to simple use cases, where we assume just
4212 * control on the primary display. We'd have to extend later if we
4213 * ever get support for setting backlights on random desktop LCD
4214 * panels though */
4215 output = get_default_output(compositor);
4216 if (!output)
4217 return;
4218
4219 if (!output->set_backlight)
4220 return;
4221
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004222 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4223 backlight_new = output->backlight_current - 25;
4224 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4225 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004226
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004227 if (backlight_new < 5)
4228 backlight_new = 5;
4229 if (backlight_new > 255)
4230 backlight_new = 255;
4231
4232 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004233 output->set_backlight(output, output->backlight_current);
4234}
4235
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004236struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004237 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004238 struct weston_seat *seat;
4239 uint32_t key[2];
4240 int key_released[2];
4241};
4242
4243static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004244debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004245 uint32_t key, uint32_t state)
4246{
4247 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004248 struct weston_compositor *ec = db->seat->compositor;
4249 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004250 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004251 uint32_t serial;
4252 int send = 0, terminate = 0;
4253 int check_binding = 1;
4254 int i;
4255
4256 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4257 /* Do not run bindings on key releases */
4258 check_binding = 0;
4259
4260 for (i = 0; i < 2; i++)
4261 if (key == db->key[i])
4262 db->key_released[i] = 1;
4263
4264 if (db->key_released[0] && db->key_released[1]) {
4265 /* All key releases been swalled so end the grab */
4266 terminate = 1;
4267 } else if (key != db->key[0] && key != db->key[1]) {
4268 /* Should not swallow release of other keys */
4269 send = 1;
4270 }
4271 } else if (key == db->key[0] && !db->key_released[0]) {
4272 /* Do not check bindings for the first press of the binding
4273 * key. This allows it to be used as a debug shortcut.
4274 * We still need to swallow this event. */
4275 check_binding = 0;
4276 } else if (db->key[1]) {
4277 /* If we already ran a binding don't process another one since
4278 * we can't keep track of all the binding keys that were
4279 * pressed in order to swallow the release events. */
4280 send = 1;
4281 check_binding = 0;
4282 }
4283
4284 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004285 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4286 key, state)) {
4287 /* We ran a binding so swallow the press and keep the
4288 * grab to swallow the released too. */
4289 send = 0;
4290 terminate = 0;
4291 db->key[1] = key;
4292 } else {
4293 /* Terminate the grab since the key pressed is not a
4294 * debug binding key. */
4295 send = 1;
4296 terminate = 1;
4297 }
4298 }
4299
4300 if (send) {
4301 resource = grab->keyboard->focus_resource;
4302
4303 if (resource) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004304 serial = wl_display_next_serial(display);
4305 wl_keyboard_send_key(resource, serial, time, key, state);
4306 }
4307 }
4308
4309 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004310 weston_keyboard_end_grab(grab->keyboard);
4311 if (grab->keyboard->input_method_resource)
4312 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004313 free(db);
4314 }
4315}
4316
4317static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004318debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004319 uint32_t mods_depressed, uint32_t mods_latched,
4320 uint32_t mods_locked, uint32_t group)
4321{
4322 struct wl_resource *resource;
4323
4324 resource = grab->keyboard->focus_resource;
4325 if (!resource)
4326 return;
4327
4328 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4329 mods_latched, mods_locked, group);
4330}
4331
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004332struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004333 debug_binding_key,
4334 debug_binding_modifiers
4335};
4336
4337static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004338debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004339{
4340 struct debug_binding_grab *grab;
4341
4342 grab = calloc(1, sizeof *grab);
4343 if (!grab)
4344 return;
4345
4346 grab->seat = (struct weston_seat *) seat;
4347 grab->key[0] = key;
4348 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004349 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004350}
4351
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004352static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004353force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004354 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004355{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004356 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004357 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004358 struct desktop_shell *shell = data;
4359 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004360 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004361
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004362 focus_surface = seat->keyboard->focus;
4363 if (!focus_surface)
4364 return;
4365
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004366 wl_signal_emit(&compositor->kill_signal, focus_surface);
4367
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004368 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004369 wl_client_get_credentials(client, &pid, NULL, NULL);
4370
4371 /* Skip clients that we launched ourselves (the credentials of
4372 * the socketpair is ours) */
4373 if (pid == getpid())
4374 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004375
Daniel Stone325fc2d2012-05-30 16:31:58 +01004376 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004377}
4378
4379static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004380workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004381 uint32_t key, void *data)
4382{
4383 struct desktop_shell *shell = data;
4384 unsigned int new_index = shell->workspaces.current;
4385
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004386 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004387 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004388 if (new_index != 0)
4389 new_index--;
4390
4391 change_workspace(shell, new_index);
4392}
4393
4394static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004395workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004396 uint32_t key, void *data)
4397{
4398 struct desktop_shell *shell = data;
4399 unsigned int new_index = shell->workspaces.current;
4400
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004401 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004402 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004403 if (new_index < shell->workspaces.num - 1)
4404 new_index++;
4405
4406 change_workspace(shell, new_index);
4407}
4408
4409static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004410workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004411 uint32_t key, void *data)
4412{
4413 struct desktop_shell *shell = data;
4414 unsigned int new_index;
4415
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004416 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004417 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004418 new_index = key - KEY_F1;
4419 if (new_index >= shell->workspaces.num)
4420 new_index = shell->workspaces.num - 1;
4421
4422 change_workspace(shell, new_index);
4423}
4424
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004425static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004426workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004427 uint32_t key, void *data)
4428{
4429 struct desktop_shell *shell = data;
4430 unsigned int new_index = shell->workspaces.current;
4431
4432 if (shell->locked)
4433 return;
4434
4435 if (new_index != 0)
4436 new_index--;
4437
4438 take_surface_to_workspace_by_seat(shell, seat, new_index);
4439}
4440
4441static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004442workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004443 uint32_t key, void *data)
4444{
4445 struct desktop_shell *shell = data;
4446 unsigned int new_index = shell->workspaces.current;
4447
4448 if (shell->locked)
4449 return;
4450
4451 if (new_index < shell->workspaces.num - 1)
4452 new_index++;
4453
4454 take_surface_to_workspace_by_seat(shell, seat, new_index);
4455}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004456
4457static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004458shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004459{
Tiago Vignattibe143262012-04-16 17:31:41 +03004460 struct desktop_shell *shell =
4461 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004462 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004463
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004464 if (shell->child.client)
4465 wl_client_destroy(shell->child.client);
4466
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004467 wl_list_remove(&shell->idle_listener.link);
4468 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004469 wl_list_remove(&shell->show_input_panel_listener.link);
4470 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004471
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004472 wl_array_for_each(ws, &shell->workspaces.array)
4473 workspace_destroy(*ws);
4474 wl_array_release(&shell->workspaces.array);
4475
Pekka Paalanen3c647232011-12-22 13:43:43 +02004476 free(shell->screensaver.path);
4477 free(shell);
4478}
4479
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004480static void
4481shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4482{
4483 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004484 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004485
4486 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004487 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4488 MODIFIER_CTRL | MODIFIER_ALT,
4489 terminate_binding, ec);
4490 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4491 click_to_activate_binding,
4492 shell);
4493 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4494 MODIFIER_SUPER | MODIFIER_ALT,
4495 surface_opacity_binding, NULL);
4496 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4497 MODIFIER_SUPER, zoom_axis_binding,
4498 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004499
4500 /* configurable bindings */
4501 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004502 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4503 zoom_key_binding, NULL);
4504 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4505 zoom_key_binding, NULL);
4506 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4507 shell);
4508 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4509 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004510
4511 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4512 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4513 rotate_binding, NULL);
4514
Daniel Stone325fc2d2012-05-30 16:31:58 +01004515 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4516 shell);
4517 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4518 ec);
4519 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4520 backlight_binding, ec);
4521 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4522 ec);
4523 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4524 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004525 weston_compositor_add_key_binding(ec, KEY_K, mod,
4526 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004527 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4528 workspace_up_binding, shell);
4529 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4530 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004531 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4532 workspace_move_surface_up_binding,
4533 shell);
4534 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4535 workspace_move_surface_down_binding,
4536 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004537
4538 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4539 if (shell->workspaces.num > 1) {
4540 num_workspace_bindings = shell->workspaces.num;
4541 if (num_workspace_bindings > 6)
4542 num_workspace_bindings = 6;
4543 for (i = 0; i < num_workspace_bindings; i++)
4544 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4545 workspace_f_binding,
4546 shell);
4547 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004548
4549 /* Debug bindings */
4550 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4551 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004552}
4553
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004554WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004555module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004556 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004557{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004558 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004559 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004560 struct workspace **pws;
4561 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004562 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004563
Peter Huttererf3d62272013-08-08 11:57:05 +10004564 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004565 if (shell == NULL)
4566 return -1;
4567
Kristian Høgsberg75840622011-09-06 13:48:16 -04004568 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004569
4570 shell->destroy_listener.notify = shell_destroy;
4571 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004572 shell->idle_listener.notify = idle_handler;
4573 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4574 shell->wake_listener.notify = wake_handler;
4575 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004576 shell->show_input_panel_listener.notify = show_input_panels;
4577 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4578 shell->hide_input_panel_listener.notify = hide_input_panels;
4579 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004580 shell->update_input_panel_listener.notify = update_input_panels;
4581 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004582 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004583 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004584 ec->shell_interface.create_shell_surface = create_shell_surface;
4585 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004586 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004587 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004588 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004589 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004590 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02004591 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004592
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004593 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004594
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004595 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4596 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004597 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4598 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004599 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004600
4601 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004602 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004603
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004604 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004605
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004606 for (i = 0; i < shell->workspaces.num; i++) {
4607 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4608 if (pws == NULL)
4609 return -1;
4610
4611 *pws = workspace_create();
4612 if (*pws == NULL)
4613 return -1;
4614 }
4615 activate_workspace(shell, 0);
4616
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004617 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004618 wl_list_init(&shell->workspaces.animation.link);
4619 shell->workspaces.animation.frame = animate_workspace_change_frame;
4620
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004621 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004622 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004623 return -1;
4624
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004625 if (wl_global_create(ec->wl_display,
4626 &desktop_shell_interface, 2,
4627 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004628 return -1;
4629
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004630 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4631 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004632 return -1;
4633
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004634 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004635 shell, bind_input_panel) == NULL)
4636 return -1;
4637
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004638 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4639 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004640 return -1;
4641
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004642 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004643
4644 loop = wl_display_get_event_loop(ec->wl_display);
4645 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004646
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004647 shell->screensaver.timer =
4648 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4649
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004650 wl_list_for_each(seat, &ec->seat_list, link)
4651 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004652
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004653 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004654
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004655 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004656
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004657 return 0;
4658}