blob: 3d10eef64ec61864a4d7a8b247d5a175f8935f82 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050026#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040027#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020028#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
51 ANIMATION_FADE
52};
53
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020054enum fade_type {
55 FADE_IN,
56 FADE_OUT
57};
58
Jonas Ådahl04769742012-06-13 00:01:24 +020059struct focus_state {
60 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040061 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020062 struct weston_surface *keyboard_focus;
63 struct wl_list link;
64 struct wl_listener seat_destroy_listener;
65 struct wl_listener surface_destroy_listener;
66};
67
Jonas Ådahle3cddce2012-06-13 00:01:22 +020068struct workspace {
69 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020070
71 struct wl_list focus_list;
72 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020073};
74
Philipp Brüschweiler88013572012-08-06 13:44:42 +020075struct input_panel_surface {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010076 struct wl_resource resource;
77
78 struct desktop_shell *shell;
79
Philipp Brüschweiler88013572012-08-06 13:44:42 +020080 struct wl_list link;
81 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010082 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020083
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020084 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020085 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020086};
87
Tiago Vignattibe143262012-04-16 17:31:41 +030088struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050089 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040090
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020091 struct wl_listener idle_listener;
92 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040093 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020094 struct wl_listener show_input_panel_listener;
95 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020096 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020097
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050098 struct weston_layer fullscreen_layer;
99 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500100 struct weston_layer background_layer;
101 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200102 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500103
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400104 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300105 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400106
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200107 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200109 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200110 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200111
112 unsigned deathcount;
113 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200114 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200115
116 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200117 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200118 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200119
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200120 struct {
121 struct weston_surface *surface;
122 pixman_box32_t cursor_rectangle;
123 } text_input;
124
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400125 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500126 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100127
Pekka Paalanen77346a62011-11-30 16:26:35 +0200128 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200129 struct wl_array array;
130 unsigned int current;
131 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200132
Jonas Ådahle9d22502012-08-29 22:13:01 +0200133 struct wl_list client_list;
134
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200135 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200136 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200137 int anim_dir;
138 uint32_t anim_timestamp;
139 double anim_current;
140 struct workspace *anim_from;
141 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200142 } workspaces;
143
144 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200145 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200146 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200147 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200149 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200150 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500151
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200152 struct {
153 struct wl_resource *binding;
154 struct wl_list surfaces;
155 } input_panel;
156
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200157 struct {
158 struct weston_surface *surface;
159 struct weston_surface_animation *animation;
160 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300161 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200162 } fade;
163
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300164 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800165 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400166};
167
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500168enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200169 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500170 SHELL_SURFACE_TOPLEVEL,
171 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500172 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800173 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500174 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200175};
176
Scott Moreauff1db4a2012-04-17 19:06:18 -0600177struct ping_timer {
178 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600179 uint32_t serial;
180};
181
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200182struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200183 struct wl_resource resource;
184
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500185 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200186 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400187 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300188 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200189
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400190 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400191 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500192 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800193 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800194 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600195 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100196
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500197 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200198 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500199 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200 } rotation;
201
202 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100203 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500204 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100205 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400206 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500207 } popup;
208
Alex Wu4539b082012-03-01 12:57:46 +0800209 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300210 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400211 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300212 } transient;
213
214 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800215 enum wl_shell_surface_fullscreen_method type;
216 struct weston_transform transform; /* matrix from x, y */
217 uint32_t framerate;
218 struct weston_surface *black_surface;
219 } fullscreen;
220
Scott Moreauff1db4a2012-04-17 19:06:18 -0600221 struct ping_timer *ping_timer;
222
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200223 struct weston_transform workspace_transform;
224
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500225 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500226 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100227 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400228
229 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200230};
231
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300232struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400233 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300234 struct shell_surface *shsurf;
235 struct wl_listener shsurf_destroy_listener;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400236 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300237};
238
239struct weston_move_grab {
240 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100241 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242};
243
Pekka Paalanen460099f2012-01-20 16:48:25 +0200244struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300245 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500246 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200247 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200248 float x;
249 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200250 } center;
251};
252
Giulio Camuffo5085a752013-03-25 21:42:45 +0100253struct shell_seat {
254 struct weston_seat *seat;
255 struct wl_listener seat_destroy_listener;
256
257 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400258 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100259 struct wl_list surfaces_list;
260 struct wl_client *client;
261 int32_t initial_up;
262 } popup_grab;
263};
264
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400265static void
266activate(struct desktop_shell *shell, struct weston_surface *es,
267 struct weston_seat *seat);
268
269static struct workspace *
270get_current_workspace(struct desktop_shell *shell);
271
Alex Wubd3354b2012-04-17 17:20:49 +0800272static struct shell_surface *
273get_shell_surface(struct weston_surface *surface);
274
275static struct desktop_shell *
276shell_surface_get_shell(struct shell_surface *shsurf);
277
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500278static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400279surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500280
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300281static void
282shell_fade_startup(struct desktop_shell *shell);
283
Alex Wubd3354b2012-04-17 17:20:49 +0800284static bool
285shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
286{
287 struct desktop_shell *shell;
288 struct weston_surface *top_fs_es;
289
290 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100291
Alex Wubd3354b2012-04-17 17:20:49 +0800292 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
293 return false;
294
295 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100296 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800297 layer_link);
298 return (shsurf == get_shell_surface(top_fs_es));
299}
300
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500301static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400302destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300303{
304 struct shell_grab *grab;
305
306 grab = container_of(listener, struct shell_grab,
307 shsurf_destroy_listener);
308
309 grab->shsurf = NULL;
310}
311
312static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400313popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400314
315static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300316shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400317 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300318 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400319 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300320 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300321{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300322 struct desktop_shell *shell = shsurf->shell;
323
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400324 popup_grab_end(pointer);
325
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300326 grab->grab.interface = interface;
327 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400328 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
329 wl_signal_add(&shsurf->resource.destroy_signal,
330 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300331
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300332 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300333
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400334 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300335 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400336 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400337 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300338}
339
340static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300341shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400343 if (grab->shsurf)
344 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300345
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400346 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300347}
348
349static void
Alex Wu4539b082012-03-01 12:57:46 +0800350center_on_output(struct weston_surface *surface,
351 struct weston_output *output);
352
Daniel Stone496ca172012-05-30 16:31:42 +0100353static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300354get_modifier(char *modifier)
355{
356 if (!modifier)
357 return MODIFIER_SUPER;
358
359 if (!strcmp("ctrl", modifier))
360 return MODIFIER_CTRL;
361 else if (!strcmp("alt", modifier))
362 return MODIFIER_ALT;
363 else if (!strcmp("super", modifier))
364 return MODIFIER_SUPER;
365 else
366 return MODIFIER_SUPER;
367}
368
Juan Zhaoe10d2792012-04-25 19:09:52 +0800369static enum animation_type
370get_animation_type(char *animation)
371{
372 if (!animation)
373 return ANIMATION_NONE;
374
375 if (!strcmp("zoom", animation))
376 return ANIMATION_ZOOM;
377 else if (!strcmp("fade", animation))
378 return ANIMATION_FADE;
379 else
380 return ANIMATION_NONE;
381}
382
Alex Wu4539b082012-03-01 12:57:46 +0800383static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400384shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200385{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400386 struct weston_config_section *section;
387 int duration;
388 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200389
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400390 section = weston_config_get_section(shell->compositor->config,
391 "screensaver", NULL, NULL);
392 weston_config_section_get_string(section,
393 "path", &shell->screensaver.path, NULL);
394 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200395 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400396
397 section = weston_config_get_section(shell->compositor->config,
398 "shell", NULL, NULL);
399 weston_config_section_get_string(section,
400 "binding-modifier", &s, "super");
401 shell->binding_modifier = get_modifier(s);
402 weston_config_section_get_string(section, "animation", &s, "none");
403 shell->win_animation_type = get_animation_type(s);
404 weston_config_section_get_uint(section, "num-workspaces",
405 &shell->workspaces.num,
406 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200407}
408
409static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200410focus_state_destroy(struct focus_state *state)
411{
412 wl_list_remove(&state->seat_destroy_listener.link);
413 wl_list_remove(&state->surface_destroy_listener.link);
414 free(state);
415}
416
417static void
418focus_state_seat_destroy(struct wl_listener *listener, void *data)
419{
420 struct focus_state *state = container_of(listener,
421 struct focus_state,
422 seat_destroy_listener);
423
424 wl_list_remove(&state->link);
425 focus_state_destroy(state);
426}
427
428static void
429focus_state_surface_destroy(struct wl_listener *listener, void *data)
430{
431 struct focus_state *state = container_of(listener,
432 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400433 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400434 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300435 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400436 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200437
Pekka Paalanen01388e22013-04-25 13:57:44 +0300438 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
439
Kristian Høgsberge3778222012-07-31 17:29:30 -0400440 next = NULL;
441 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300442 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400443 continue;
444
445 next = surface;
446 break;
447 }
448
Pekka Paalanen01388e22013-04-25 13:57:44 +0300449 /* if the focus was a sub-surface, activate its main surface */
450 if (main_surface != state->keyboard_focus)
451 next = main_surface;
452
Kristian Høgsberge3778222012-07-31 17:29:30 -0400453 if (next) {
454 shell = state->seat->compositor->shell_interface.shell;
455 activate(shell, next, state->seat);
456 } else {
457 wl_list_remove(&state->link);
458 focus_state_destroy(state);
459 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200460}
461
462static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400463focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200464{
Jonas Ådahl04769742012-06-13 00:01:24 +0200465 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200466
467 state = malloc(sizeof *state);
468 if (state == NULL)
469 return NULL;
470
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400471 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200472 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400473 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200474
475 state->seat_destroy_listener.notify = focus_state_seat_destroy;
476 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400477 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200478 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400479 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200480
481 return state;
482}
483
Jonas Ådahl8538b222012-08-29 22:13:03 +0200484static struct focus_state *
485ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
486{
487 struct workspace *ws = get_current_workspace(shell);
488 struct focus_state *state;
489
490 wl_list_for_each(state, &ws->focus_list, link)
491 if (state->seat == seat)
492 break;
493
494 if (&state->link == &ws->focus_list)
495 state = focus_state_create(seat, ws);
496
497 return state;
498}
499
Jonas Ådahl04769742012-06-13 00:01:24 +0200500static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400501restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200502{
503 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400504 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200505
506 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400507 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200508
Kristian Høgsberge3148752013-05-06 23:19:49 -0400509 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200510 }
511}
512
513static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200514replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
515 struct weston_seat *seat)
516{
517 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400518 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200519
520 wl_list_for_each(state, &ws->focus_list, link) {
521 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400522 surface = seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200523 state->keyboard_focus =
524 (struct weston_surface *) surface;
525 return;
526 }
527 }
528}
529
530static void
531drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
532 struct weston_surface *surface)
533{
534 struct focus_state *state;
535
536 wl_list_for_each(state, &ws->focus_list, link)
537 if (state->keyboard_focus == surface)
538 state->keyboard_focus = NULL;
539}
540
541static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200542workspace_destroy(struct workspace *ws)
543{
Jonas Ådahl04769742012-06-13 00:01:24 +0200544 struct focus_state *state, *next;
545
546 wl_list_for_each_safe(state, next, &ws->focus_list, link)
547 focus_state_destroy(state);
548
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200549 free(ws);
550}
551
Jonas Ådahl04769742012-06-13 00:01:24 +0200552static void
553seat_destroyed(struct wl_listener *listener, void *data)
554{
555 struct weston_seat *seat = data;
556 struct focus_state *state, *next;
557 struct workspace *ws = container_of(listener,
558 struct workspace,
559 seat_destroyed_listener);
560
561 wl_list_for_each_safe(state, next, &ws->focus_list, link)
562 if (state->seat == seat)
563 wl_list_remove(&state->link);
564}
565
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200566static struct workspace *
567workspace_create(void)
568{
569 struct workspace *ws = malloc(sizeof *ws);
570 if (ws == NULL)
571 return NULL;
572
573 weston_layer_init(&ws->layer, NULL);
574
Jonas Ådahl04769742012-06-13 00:01:24 +0200575 wl_list_init(&ws->focus_list);
576 wl_list_init(&ws->seat_destroyed_listener.link);
577 ws->seat_destroyed_listener.notify = seat_destroyed;
578
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200579 return ws;
580}
581
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200582static int
583workspace_is_empty(struct workspace *ws)
584{
585 return wl_list_empty(&ws->layer.surface_list);
586}
587
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200588static struct workspace *
589get_workspace(struct desktop_shell *shell, unsigned int index)
590{
591 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200592 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200593 pws += index;
594 return *pws;
595}
596
597static struct workspace *
598get_current_workspace(struct desktop_shell *shell)
599{
600 return get_workspace(shell, shell->workspaces.current);
601}
602
603static void
604activate_workspace(struct desktop_shell *shell, unsigned int index)
605{
606 struct workspace *ws;
607
608 ws = get_workspace(shell, index);
609 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
610
611 shell->workspaces.current = index;
612}
613
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200614static unsigned int
615get_output_height(struct weston_output *output)
616{
617 return abs(output->region.extents.y1 - output->region.extents.y2);
618}
619
620static void
621surface_translate(struct weston_surface *surface, double d)
622{
623 struct shell_surface *shsurf = get_shell_surface(surface);
624 struct weston_transform *transform;
625
626 transform = &shsurf->workspace_transform;
627 if (wl_list_empty(&transform->link))
628 wl_list_insert(surface->geometry.transformation_list.prev,
629 &shsurf->workspace_transform.link);
630
631 weston_matrix_init(&shsurf->workspace_transform.matrix);
632 weston_matrix_translate(&shsurf->workspace_transform.matrix,
633 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200634 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200635}
636
637static void
638workspace_translate_out(struct workspace *ws, double fraction)
639{
640 struct weston_surface *surface;
641 unsigned int height;
642 double d;
643
644 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
645 height = get_output_height(surface->output);
646 d = height * fraction;
647
648 surface_translate(surface, d);
649 }
650}
651
652static void
653workspace_translate_in(struct workspace *ws, double fraction)
654{
655 struct weston_surface *surface;
656 unsigned int height;
657 double d;
658
659 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
660 height = get_output_height(surface->output);
661
662 if (fraction > 0)
663 d = -(height - height * fraction);
664 else
665 d = height + height * fraction;
666
667 surface_translate(surface, d);
668 }
669}
670
671static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200672broadcast_current_workspace_state(struct desktop_shell *shell)
673{
674 struct wl_resource *resource;
675
676 wl_list_for_each(resource, &shell->workspaces.client_list, link)
677 workspace_manager_send_state(resource,
678 shell->workspaces.current,
679 shell->workspaces.num);
680}
681
682static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200683reverse_workspace_change_animation(struct desktop_shell *shell,
684 unsigned int index,
685 struct workspace *from,
686 struct workspace *to)
687{
688 shell->workspaces.current = index;
689
690 shell->workspaces.anim_to = to;
691 shell->workspaces.anim_from = from;
692 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
693 shell->workspaces.anim_timestamp = 0;
694
Scott Moreau4272e632012-08-13 09:58:41 -0600695 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200696}
697
698static void
699workspace_deactivate_transforms(struct workspace *ws)
700{
701 struct weston_surface *surface;
702 struct shell_surface *shsurf;
703
704 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
705 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200706 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
707 wl_list_remove(&shsurf->workspace_transform.link);
708 wl_list_init(&shsurf->workspace_transform.link);
709 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200710 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200711 }
712}
713
714static void
715finish_workspace_change_animation(struct desktop_shell *shell,
716 struct workspace *from,
717 struct workspace *to)
718{
Scott Moreau4272e632012-08-13 09:58:41 -0600719 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200720
721 wl_list_remove(&shell->workspaces.animation.link);
722 workspace_deactivate_transforms(from);
723 workspace_deactivate_transforms(to);
724 shell->workspaces.anim_to = NULL;
725
726 wl_list_remove(&shell->workspaces.anim_from->layer.link);
727}
728
729static void
730animate_workspace_change_frame(struct weston_animation *animation,
731 struct weston_output *output, uint32_t msecs)
732{
733 struct desktop_shell *shell =
734 container_of(animation, struct desktop_shell,
735 workspaces.animation);
736 struct workspace *from = shell->workspaces.anim_from;
737 struct workspace *to = shell->workspaces.anim_to;
738 uint32_t t;
739 double x, y;
740
741 if (workspace_is_empty(from) && workspace_is_empty(to)) {
742 finish_workspace_change_animation(shell, from, to);
743 return;
744 }
745
746 if (shell->workspaces.anim_timestamp == 0) {
747 if (shell->workspaces.anim_current == 0.0)
748 shell->workspaces.anim_timestamp = msecs;
749 else
750 shell->workspaces.anim_timestamp =
751 msecs -
752 /* Invers of movement function 'y' below. */
753 (asin(1.0 - shell->workspaces.anim_current) *
754 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
755 M_2_PI);
756 }
757
758 t = msecs - shell->workspaces.anim_timestamp;
759
760 /*
761 * x = [0, π/2]
762 * y(x) = sin(x)
763 */
764 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
765 y = sin(x);
766
767 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600768 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200769
770 workspace_translate_out(from, shell->workspaces.anim_dir * y);
771 workspace_translate_in(to, shell->workspaces.anim_dir * y);
772 shell->workspaces.anim_current = y;
773
Scott Moreau4272e632012-08-13 09:58:41 -0600774 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200775 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200776 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200777 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200778}
779
780static void
781animate_workspace_change(struct desktop_shell *shell,
782 unsigned int index,
783 struct workspace *from,
784 struct workspace *to)
785{
786 struct weston_output *output;
787
788 int dir;
789
790 if (index > shell->workspaces.current)
791 dir = -1;
792 else
793 dir = 1;
794
795 shell->workspaces.current = index;
796
797 shell->workspaces.anim_dir = dir;
798 shell->workspaces.anim_from = from;
799 shell->workspaces.anim_to = to;
800 shell->workspaces.anim_current = 0.0;
801 shell->workspaces.anim_timestamp = 0;
802
803 output = container_of(shell->compositor->output_list.next,
804 struct weston_output, link);
805 wl_list_insert(&output->animation_list,
806 &shell->workspaces.animation.link);
807
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200808 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200809
810 workspace_translate_in(to, 0);
811
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400812 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200813
Scott Moreau4272e632012-08-13 09:58:41 -0600814 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200815}
816
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200817static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200818update_workspace(struct desktop_shell *shell, unsigned int index,
819 struct workspace *from, struct workspace *to)
820{
821 shell->workspaces.current = index;
822 wl_list_insert(&from->layer.link, &to->layer.link);
823 wl_list_remove(&from->layer.link);
824}
825
826static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200827change_workspace(struct desktop_shell *shell, unsigned int index)
828{
829 struct workspace *from;
830 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200831
832 if (index == shell->workspaces.current)
833 return;
834
835 /* Don't change workspace when there is any fullscreen surfaces. */
836 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
837 return;
838
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200839 from = get_current_workspace(shell);
840 to = get_workspace(shell, index);
841
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200842 if (shell->workspaces.anim_from == to &&
843 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200844 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200845 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200846 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200847 return;
848 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200849
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200850 if (shell->workspaces.anim_to != NULL)
851 finish_workspace_change_animation(shell,
852 shell->workspaces.anim_from,
853 shell->workspaces.anim_to);
854
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200855 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200856
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200857 if (workspace_is_empty(to) && workspace_is_empty(from))
858 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200859 else
860 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200861
862 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200863}
864
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200865static bool
866workspace_has_only(struct workspace *ws, struct weston_surface *surface)
867{
868 struct wl_list *list = &ws->layer.surface_list;
869 struct wl_list *e;
870
871 if (wl_list_empty(list))
872 return false;
873
874 e = list->next;
875
876 if (e->next != list)
877 return false;
878
879 return container_of(e, struct weston_surface, layer_link) == surface;
880}
881
882static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200883move_surface_to_workspace(struct desktop_shell *shell,
884 struct weston_surface *surface,
885 uint32_t workspace)
886{
887 struct workspace *from;
888 struct workspace *to;
889 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300890 struct weston_surface *focus;
891
892 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200893
894 if (workspace == shell->workspaces.current)
895 return;
896
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200897 if (workspace >= shell->workspaces.num)
898 workspace = shell->workspaces.num - 1;
899
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900 from = get_current_workspace(shell);
901 to = get_workspace(shell, workspace);
902
903 wl_list_remove(&surface->layer_link);
904 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
905
906 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300907 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
908 if (!seat->keyboard)
909 continue;
910
911 focus = weston_surface_get_main_surface(seat->keyboard->focus);
912 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400913 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300914 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200915
916 weston_surface_damage_below(surface);
917}
918
919static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200920take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400921 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200922 unsigned int index)
923{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300924 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200925 struct shell_surface *shsurf;
926 struct workspace *from;
927 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200928 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929
Pekka Paalanen01388e22013-04-25 13:57:44 +0300930 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200931 if (surface == NULL ||
932 index == shell->workspaces.current)
933 return;
934
935 from = get_current_workspace(shell);
936 to = get_workspace(shell, index);
937
938 wl_list_remove(&surface->layer_link);
939 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
940
Jonas Ådahle9d22502012-08-29 22:13:01 +0200941 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200942 drop_focus_state(shell, from, surface);
943
944 if (shell->workspaces.anim_from == to &&
945 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200946 wl_list_remove(&to->layer.link);
947 wl_list_insert(from->layer.link.prev, &to->layer.link);
948
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200949 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200950 broadcast_current_workspace_state(shell);
951
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200952 return;
953 }
954
955 if (shell->workspaces.anim_to != NULL)
956 finish_workspace_change_animation(shell,
957 shell->workspaces.anim_from,
958 shell->workspaces.anim_to);
959
960 if (workspace_is_empty(from) &&
961 workspace_has_only(to, surface))
962 update_workspace(shell, index, from, to);
963 else {
964 shsurf = get_shell_surface(surface);
965 if (wl_list_empty(&shsurf->workspace_transform.link))
966 wl_list_insert(&shell->workspaces.anim_sticky_list,
967 &shsurf->workspace_transform.link);
968
969 animate_workspace_change(shell, index, from, to);
970 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200971
972 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200973
974 state = ensure_focus_state(shell, seat);
975 if (state != NULL)
976 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200977}
978
979static void
980workspace_manager_move_surface(struct wl_client *client,
981 struct wl_resource *resource,
982 struct wl_resource *surface_resource,
983 uint32_t workspace)
984{
985 struct desktop_shell *shell = resource->data;
986 struct weston_surface *surface =
987 (struct weston_surface *) surface_resource;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300988 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200989
Pekka Paalanen01388e22013-04-25 13:57:44 +0300990 main_surface = weston_surface_get_main_surface(surface);
991 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200992}
993
994static const struct workspace_manager_interface workspace_manager_implementation = {
995 workspace_manager_move_surface,
996};
997
998static void
999unbind_resource(struct wl_resource *resource)
1000{
1001 wl_list_remove(&resource->link);
1002 free(resource);
1003}
1004
1005static void
1006bind_workspace_manager(struct wl_client *client,
1007 void *data, uint32_t version, uint32_t id)
1008{
1009 struct desktop_shell *shell = data;
1010 struct wl_resource *resource;
1011
1012 resource = wl_client_add_object(client, &workspace_manager_interface,
1013 &workspace_manager_implementation,
1014 id, shell);
1015
1016 if (resource == NULL) {
1017 weston_log("couldn't add workspace manager object");
1018 return;
1019 }
1020
1021 resource->destroy = unbind_resource;
1022 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1023
1024 workspace_manager_send_state(resource,
1025 shell->workspaces.current,
1026 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001027}
1028
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001029static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001030noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001031{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001032}
1033
1034static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001035move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001036{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001037 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001038 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001039 struct shell_surface *shsurf = move->base.shsurf;
1040 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001041 int dx = wl_fixed_to_int(pointer->x + move->dx);
1042 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001043
1044 if (!shsurf)
1045 return;
1046
1047 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001048
Daniel Stone103db7f2012-05-08 17:17:55 +01001049 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001050 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001051
1052 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001053}
1054
1055static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001056move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001057 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001058{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001059 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1060 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001061 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001062 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001063
Daniel Stone4dbadb12012-05-30 16:31:51 +01001064 if (pointer->button_count == 0 &&
1065 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001066 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001067 free(grab);
1068 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001069}
1070
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001071static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001072 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001073 move_grab_motion,
1074 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001075};
1076
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001077static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001078surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001079{
1080 struct weston_move_grab *move;
1081
1082 if (!shsurf)
1083 return -1;
1084
1085 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1086 return 0;
1087
1088 move = malloc(sizeof *move);
1089 if (!move)
1090 return -1;
1091
1092 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001093 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001094 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001095 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001096
1097 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001098 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001099
1100 return 0;
1101}
1102
1103static void
1104shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1105 struct wl_resource *seat_resource, uint32_t serial)
1106{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001107 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001108 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001109 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001110
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001111 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001112 if (seat->pointer->button_count == 0 ||
1113 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001114 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001115 return;
1116
Kristian Høgsberge3148752013-05-06 23:19:49 -04001117 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001118 wl_resource_post_no_memory(resource);
1119}
1120
1121struct weston_resize_grab {
1122 struct shell_grab base;
1123 uint32_t edges;
1124 int32_t width, height;
1125};
1126
1127static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001128resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001129{
1130 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001131 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001132 struct shell_surface *shsurf = resize->base.shsurf;
1133 int32_t width, height;
1134 wl_fixed_t from_x, from_y;
1135 wl_fixed_t to_x, to_y;
1136
1137 if (!shsurf)
1138 return;
1139
1140 weston_surface_from_global_fixed(shsurf->surface,
1141 pointer->grab_x, pointer->grab_y,
1142 &from_x, &from_y);
1143 weston_surface_from_global_fixed(shsurf->surface,
1144 pointer->x, pointer->y, &to_x, &to_y);
1145
1146 width = resize->width;
1147 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1148 width += wl_fixed_to_int(from_x - to_x);
1149 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1150 width += wl_fixed_to_int(to_x - from_x);
1151 }
1152
1153 height = resize->height;
1154 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1155 height += wl_fixed_to_int(from_y - to_y);
1156 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1157 height += wl_fixed_to_int(to_y - from_y);
1158 }
1159
1160 shsurf->client->send_configure(shsurf->surface,
1161 resize->edges, width, height);
1162}
1163
1164static void
1165send_configure(struct weston_surface *surface,
1166 uint32_t edges, int32_t width, int32_t height)
1167{
1168 struct shell_surface *shsurf = get_shell_surface(surface);
1169
1170 wl_shell_surface_send_configure(&shsurf->resource,
1171 edges, width, height);
1172}
1173
1174static const struct weston_shell_client shell_client = {
1175 send_configure
1176};
1177
1178static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001179resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001180 uint32_t time, uint32_t button, uint32_t state_w)
1181{
1182 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001183 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001184 enum wl_pointer_button_state state = state_w;
1185
1186 if (pointer->button_count == 0 &&
1187 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1188 shell_grab_end(&resize->base);
1189 free(grab);
1190 }
1191}
1192
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001193static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001194 noop_grab_focus,
1195 resize_grab_motion,
1196 resize_grab_button,
1197};
1198
Giulio Camuffob8366642013-04-25 13:57:46 +03001199/*
1200 * Returns the bounding box of a surface and all its sub-surfaces,
1201 * in the surface coordinates system. */
1202static void
1203surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1204 int32_t *y, int32_t *w, int32_t *h) {
1205 pixman_region32_t region;
1206 pixman_box32_t *box;
1207 struct weston_subsurface *subsurface;
1208
1209 pixman_region32_init_rect(&region, 0, 0,
1210 surface->geometry.width,
1211 surface->geometry.height);
1212
1213 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1214 pixman_region32_union_rect(&region, &region,
1215 subsurface->position.x,
1216 subsurface->position.y,
1217 subsurface->surface->geometry.width,
1218 subsurface->surface->geometry.height);
1219 }
1220
1221 box = pixman_region32_extents(&region);
1222 if (x)
1223 *x = box->x1;
1224 if (y)
1225 *y = box->y1;
1226 if (w)
1227 *w = box->x2 - box->x1;
1228 if (h)
1229 *h = box->y2 - box->y1;
1230
1231 pixman_region32_fini(&region);
1232}
1233
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001234static int
1235surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001236 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001237{
1238 struct weston_resize_grab *resize;
1239
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001240 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1241 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001242 return 0;
1243
1244 if (edges == 0 || edges > 15 ||
1245 (edges & 3) == 3 || (edges & 12) == 12)
1246 return 0;
1247
1248 resize = malloc(sizeof *resize);
1249 if (!resize)
1250 return -1;
1251
1252 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001253 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1254 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001255
1256 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001257 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001258
1259 return 0;
1260}
1261
1262static void
1263shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1264 struct wl_resource *seat_resource, uint32_t serial,
1265 uint32_t edges)
1266{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001267 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001268 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001269 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001270
1271 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1272 return;
1273
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001274 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001275 if (seat->pointer->button_count == 0 ||
1276 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001277 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001278 return;
1279
Kristian Høgsberge3148752013-05-06 23:19:49 -04001280 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001281 wl_resource_post_no_memory(resource);
1282}
1283
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001284static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001285busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001286{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001287 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001288 struct weston_pointer *pointer = base->pointer;
1289 struct weston_surface *surface;
1290 wl_fixed_t sx, sy;
1291
1292 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1293 pointer->x, pointer->y,
1294 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001295
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001296 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001297 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001298 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001299 }
1300}
1301
1302static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001303busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001304{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001305}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001306
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001307static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001308busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001309 uint32_t time, uint32_t button, uint32_t state)
1310{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001311 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001312 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001313 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001314
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001315 if (shsurf && button == BTN_LEFT && state) {
1316 activate(shsurf->shell, shsurf->surface, seat);
1317 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001318 } else if (shsurf && button == BTN_RIGHT && state) {
1319 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001320 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001321 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001322}
1323
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001324static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001325 busy_cursor_grab_focus,
1326 busy_cursor_grab_motion,
1327 busy_cursor_grab_button,
1328};
1329
1330static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001331set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001332{
1333 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001334
1335 grab = malloc(sizeof *grab);
1336 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001337 return;
1338
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001339 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1340 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001341}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001342
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001343static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001344end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001345{
1346 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1347
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001348 if (grab->grab.interface == &busy_cursor_grab_interface &&
1349 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001350 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001351 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001352 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001353}
1354
Scott Moreau9521d5e2012-04-19 13:06:17 -06001355static void
1356ping_timer_destroy(struct shell_surface *shsurf)
1357{
1358 if (!shsurf || !shsurf->ping_timer)
1359 return;
1360
1361 if (shsurf->ping_timer->source)
1362 wl_event_source_remove(shsurf->ping_timer->source);
1363
1364 free(shsurf->ping_timer);
1365 shsurf->ping_timer = NULL;
1366}
1367
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001368static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001369ping_timeout_handler(void *data)
1370{
1371 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001372 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001373
Scott Moreau9521d5e2012-04-19 13:06:17 -06001374 /* Client is not responding */
1375 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001376
1377 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001378 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001379 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001380
1381 return 1;
1382}
1383
1384static void
1385ping_handler(struct weston_surface *surface, uint32_t serial)
1386{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001387 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001388 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001389 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001390
1391 if (!shsurf)
1392 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001393 if (!shsurf->resource.client)
1394 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001395
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001396 if (shsurf->surface == shsurf->shell->grab_surface)
1397 return;
1398
Scott Moreauff1db4a2012-04-17 19:06:18 -06001399 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001400 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001401 if (!shsurf->ping_timer)
1402 return;
1403
1404 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001405 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1406 shsurf->ping_timer->source =
1407 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1408 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1409
1410 wl_shell_surface_send_ping(&shsurf->resource, serial);
1411 }
1412}
1413
1414static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001415handle_pointer_focus(struct wl_listener *listener, void *data)
1416{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001417 struct weston_pointer *pointer = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001418 struct weston_surface *surface =
1419 (struct weston_surface *) pointer->focus;
1420 struct weston_compositor *compositor;
1421 struct shell_surface *shsurf;
1422 uint32_t serial;
1423
1424 if (!surface)
1425 return;
1426
1427 compositor = surface->compositor;
1428 shsurf = get_shell_surface(surface);
1429
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001430 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001431 set_busy_cursor(shsurf, pointer);
1432 } else {
1433 serial = wl_display_next_serial(compositor->wl_display);
1434 ping_handler(surface, serial);
1435 }
1436}
1437
1438static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001439create_pointer_focus_listener(struct weston_seat *seat)
1440{
1441 struct wl_listener *listener;
1442
Kristian Høgsberge3148752013-05-06 23:19:49 -04001443 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001444 return;
1445
1446 listener = malloc(sizeof *listener);
1447 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001448 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001449}
1450
1451static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001452shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1453 uint32_t serial)
1454{
1455 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001456 struct weston_seat *seat;
1457 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001458
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001459 if (shsurf->ping_timer == NULL)
1460 /* Just ignore unsolicited pong. */
1461 return;
1462
Scott Moreauff1db4a2012-04-17 19:06:18 -06001463 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001464 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001465 wl_list_for_each(seat, &ec->seat_list, link) {
1466 if(seat->pointer)
1467 end_busy_cursor(shsurf, seat->pointer);
1468 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001469 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001470 }
1471}
1472
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001473static void
1474shell_surface_set_title(struct wl_client *client,
1475 struct wl_resource *resource, const char *title)
1476{
1477 struct shell_surface *shsurf = resource->data;
1478
1479 free(shsurf->title);
1480 shsurf->title = strdup(title);
1481}
1482
1483static void
1484shell_surface_set_class(struct wl_client *client,
1485 struct wl_resource *resource, const char *class)
1486{
1487 struct shell_surface *shsurf = resource->data;
1488
1489 free(shsurf->class);
1490 shsurf->class = strdup(class);
1491}
1492
Juan Zhao96879df2012-02-07 08:45:41 +08001493static struct weston_output *
1494get_default_output(struct weston_compositor *compositor)
1495{
1496 return container_of(compositor->output_list.next,
1497 struct weston_output, link);
1498}
1499
Alex Wu4539b082012-03-01 12:57:46 +08001500static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001501restore_output_mode(struct weston_output *output)
1502{
1503 if (output->current != output->origin ||
1504 (int32_t)output->scale != output->origin_scale)
1505 weston_output_switch_mode(output,
1506 output->origin,
1507 output->origin_scale);
1508}
1509
1510static void
1511restore_all_output_modes(struct weston_compositor *compositor)
1512{
1513 struct weston_output *output;
1514
1515 wl_list_for_each(output, &compositor->output_list, link)
1516 restore_output_mode(output);
1517}
1518
1519static void
Alex Wu4539b082012-03-01 12:57:46 +08001520shell_unset_fullscreen(struct shell_surface *shsurf)
1521{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001522 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001523 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001524 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1525 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001526 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001527 }
Alex Wu4539b082012-03-01 12:57:46 +08001528 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1529 shsurf->fullscreen.framerate = 0;
1530 wl_list_remove(&shsurf->fullscreen.transform.link);
1531 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001532 if (shsurf->fullscreen.black_surface)
1533 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001534 shsurf->fullscreen.black_surface = NULL;
1535 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001536 weston_surface_set_position(shsurf->surface,
1537 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001538 if (shsurf->saved_rotation_valid) {
1539 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1540 &shsurf->rotation.transform.link);
1541 shsurf->saved_rotation_valid = false;
1542 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001543
1544 ws = get_current_workspace(shsurf->shell);
1545 wl_list_remove(&shsurf->surface->layer_link);
1546 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001547}
1548
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001549static void
1550shell_unset_maximized(struct shell_surface *shsurf)
1551{
1552 struct workspace *ws;
1553 /* undo all maximized things here */
1554 shsurf->output = get_default_output(shsurf->surface->compositor);
1555 weston_surface_set_position(shsurf->surface,
1556 shsurf->saved_x,
1557 shsurf->saved_y);
1558
1559 if (shsurf->saved_rotation_valid) {
1560 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1561 &shsurf->rotation.transform.link);
1562 shsurf->saved_rotation_valid = false;
1563 }
1564
1565 ws = get_current_workspace(shsurf->shell);
1566 wl_list_remove(&shsurf->surface->layer_link);
1567 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1568}
1569
Pekka Paalanen98262232011-12-01 10:42:22 +02001570static int
1571reset_shell_surface_type(struct shell_surface *surface)
1572{
1573 switch (surface->type) {
1574 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001575 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001576 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001577 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001578 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001579 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001580 case SHELL_SURFACE_NONE:
1581 case SHELL_SURFACE_TOPLEVEL:
1582 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001583 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001584 break;
1585 }
1586
1587 surface->type = SHELL_SURFACE_NONE;
1588 return 0;
1589}
1590
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001591static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001592set_surface_type(struct shell_surface *shsurf)
1593{
1594 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001595 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001596
1597 reset_shell_surface_type(shsurf);
1598
1599 shsurf->type = shsurf->next_type;
1600 shsurf->next_type = SHELL_SURFACE_NONE;
1601
1602 switch (shsurf->type) {
1603 case SHELL_SURFACE_TOPLEVEL:
1604 break;
1605 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001606 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001607 pes->geometry.x + shsurf->transient.x,
1608 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001609 break;
1610
1611 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001612 case SHELL_SURFACE_FULLSCREEN:
1613 shsurf->saved_x = surface->geometry.x;
1614 shsurf->saved_y = surface->geometry.y;
1615 shsurf->saved_position_valid = true;
1616
1617 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1618 wl_list_remove(&shsurf->rotation.transform.link);
1619 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001620 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001621 shsurf->saved_rotation_valid = true;
1622 }
1623 break;
1624
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001625 default:
1626 break;
1627 }
1628}
1629
1630static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001631set_toplevel(struct shell_surface *shsurf)
1632{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001633 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001634}
1635
1636static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001637shell_surface_set_toplevel(struct wl_client *client,
1638 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001639{
Pekka Paalanen98262232011-12-01 10:42:22 +02001640 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001641
Tiago Vignattibc052c92012-04-19 16:18:18 +03001642 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001643}
1644
1645static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001646set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001647 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001648{
1649 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001650 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001651 shsurf->transient.x = x;
1652 shsurf->transient.y = y;
1653 shsurf->transient.flags = flags;
1654 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1655}
1656
1657static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001658shell_surface_set_transient(struct wl_client *client,
1659 struct wl_resource *resource,
1660 struct wl_resource *parent_resource,
1661 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001662{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001663 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001664 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001665
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001666 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001667}
1668
Tiago Vignattibe143262012-04-16 17:31:41 +03001669static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001670shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001671{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001672 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001673}
1674
1675static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001676get_output_panel_height(struct desktop_shell *shell,
1677 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001678{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001679 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001680 int panel_height = 0;
1681
1682 if (!output)
1683 return 0;
1684
Juan Zhao4ab94682012-07-09 22:24:09 -07001685 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001686 if (surface->output == output) {
1687 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001688 break;
1689 }
1690 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001691
Juan Zhao96879df2012-02-07 08:45:41 +08001692 return panel_height;
1693}
1694
1695static void
1696shell_surface_set_maximized(struct wl_client *client,
1697 struct wl_resource *resource,
1698 struct wl_resource *output_resource )
1699{
1700 struct shell_surface *shsurf = resource->data;
1701 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001702 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001703 uint32_t edges = 0, panel_height = 0;
1704
1705 /* get the default output, if the client set it as NULL
1706 check whether the ouput is available */
1707 if (output_resource)
1708 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001709 else if (es->output)
1710 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001711 else
1712 shsurf->output = get_default_output(es->compositor);
1713
Tiago Vignattibe143262012-04-16 17:31:41 +03001714 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001715 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001716 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001717
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001718 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001719 shsurf->output->width,
1720 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001721
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001722 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001723}
1724
Alex Wu21858432012-04-01 20:13:08 +08001725static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001726black_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 +08001727
Alex Wu4539b082012-03-01 12:57:46 +08001728static struct weston_surface *
1729create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001730 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001731 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001732{
1733 struct weston_surface *surface = NULL;
1734
1735 surface = weston_surface_create(ec);
1736 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001737 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001738 return NULL;
1739 }
1740
Alex Wu21858432012-04-01 20:13:08 +08001741 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001742 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001743 weston_surface_configure(surface, x, y, w, h);
1744 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001745 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001746 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001747 pixman_region32_fini(&surface->input);
1748 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001749
Alex Wu4539b082012-03-01 12:57:46 +08001750 return surface;
1751}
1752
1753/* Create black surface and append it to the associated fullscreen surface.
1754 * Handle size dismatch and positioning according to the method. */
1755static void
1756shell_configure_fullscreen(struct shell_surface *shsurf)
1757{
1758 struct weston_output *output = shsurf->fullscreen_output;
1759 struct weston_surface *surface = shsurf->surface;
1760 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001761 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001762 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001763
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001764 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1765 restore_output_mode(output);
1766
Alex Wu4539b082012-03-01 12:57:46 +08001767 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001768 shsurf->fullscreen.black_surface =
1769 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001770 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001771 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001772 output->width,
1773 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001774
1775 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1776 wl_list_insert(&surface->layer_link,
1777 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001778 shsurf->fullscreen.black_surface->output = output;
1779
Giulio Camuffob8366642013-04-25 13:57:46 +03001780 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1781 &surf_width, &surf_height);
1782
Alex Wu4539b082012-03-01 12:57:46 +08001783 switch (shsurf->fullscreen.type) {
1784 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001785 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001786 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001787 break;
1788 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001789 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001790 if (output->width == surf_width &&
1791 output->height == surf_height) {
1792 weston_surface_set_position(surface, output->x - surf_x,
1793 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001794 break;
1795 }
1796
Alex Wu4539b082012-03-01 12:57:46 +08001797 matrix = &shsurf->fullscreen.transform.matrix;
1798 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001799
Scott Moreau1bad5db2012-08-18 01:04:05 -06001800 output_aspect = (float) output->width /
1801 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001802 surface_aspect = (float) surface->geometry.width /
1803 (float) surface->geometry.height;
1804 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001805 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001806 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001807 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001808 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001809 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001810
Alex Wu4539b082012-03-01 12:57:46 +08001811 weston_matrix_scale(matrix, scale, scale, 1);
1812 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001813 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001814 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001815 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1816 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001817 weston_surface_set_position(surface, x, y);
1818
Alex Wu4539b082012-03-01 12:57:46 +08001819 break;
1820 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001821 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001822 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001823 surf_width * surface->buffer_scale,
1824 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001825 shsurf->fullscreen.framerate};
1826
Alexander Larsson355748e2013-05-28 16:23:38 +02001827 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001828 weston_surface_set_position(surface,
1829 output->x - surf_x,
1830 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001831 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001832 output->x - surf_x,
1833 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001834 output->width,
1835 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001836 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001837 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001838 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001839 center_on_output(surface, output);
1840 }
Alex Wubd3354b2012-04-17 17:20:49 +08001841 }
Alex Wu4539b082012-03-01 12:57:46 +08001842 break;
1843 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001844 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001845 break;
1846 default:
1847 break;
1848 }
1849}
1850
1851/* make the fullscreen and black surface at the top */
1852static void
1853shell_stack_fullscreen(struct shell_surface *shsurf)
1854{
Alex Wubd3354b2012-04-17 17:20:49 +08001855 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001856 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001857 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001858
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001859 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001860 wl_list_insert(&shell->fullscreen_layer.surface_list,
1861 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001862 weston_surface_damage(surface);
1863
1864 if (!shsurf->fullscreen.black_surface)
1865 shsurf->fullscreen.black_surface =
1866 create_black_surface(surface->compositor,
1867 surface,
1868 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001869 output->width,
1870 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001871
1872 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001873 wl_list_insert(&surface->layer_link,
1874 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001875 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001876}
1877
1878static void
1879shell_map_fullscreen(struct shell_surface *shsurf)
1880{
Alex Wu4539b082012-03-01 12:57:46 +08001881 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001882 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001883}
1884
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001885static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001886set_fullscreen(struct shell_surface *shsurf,
1887 uint32_t method,
1888 uint32_t framerate,
1889 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001890{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001891 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001892
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001893 if (output)
1894 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001895 else if (es->output)
1896 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001897 else
1898 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001899
Alex Wu4539b082012-03-01 12:57:46 +08001900 shsurf->fullscreen_output = shsurf->output;
1901 shsurf->fullscreen.type = method;
1902 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001903 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001904
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001905 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001906 shsurf->output->width,
1907 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001908}
1909
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001910static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001911shell_surface_set_fullscreen(struct wl_client *client,
1912 struct wl_resource *resource,
1913 uint32_t method,
1914 uint32_t framerate,
1915 struct wl_resource *output_resource)
1916{
1917 struct shell_surface *shsurf = resource->data;
1918 struct weston_output *output;
1919
1920 if (output_resource)
1921 output = output_resource->data;
1922 else
1923 output = NULL;
1924
1925 set_fullscreen(shsurf, method, framerate, output);
1926}
1927
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001928static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001929
1930static void
1931destroy_shell_seat(struct wl_listener *listener, void *data)
1932{
1933 struct shell_seat *shseat =
1934 container_of(listener,
1935 struct shell_seat, seat_destroy_listener);
1936 struct shell_surface *shsurf, *prev = NULL;
1937
1938 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001939 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001940 shseat->popup_grab.client = NULL;
1941
1942 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1943 shsurf->popup.shseat = NULL;
1944 if (prev) {
1945 wl_list_init(&prev->popup.grab_link);
1946 }
1947 prev = shsurf;
1948 }
1949 wl_list_init(&prev->popup.grab_link);
1950 }
1951
1952 wl_list_remove(&shseat->seat_destroy_listener.link);
1953 free(shseat);
1954}
1955
1956static struct shell_seat *
1957create_shell_seat(struct weston_seat *seat)
1958{
1959 struct shell_seat *shseat;
1960
1961 shseat = calloc(1, sizeof *shseat);
1962 if (!shseat) {
1963 weston_log("no memory to allocate shell seat\n");
1964 return NULL;
1965 }
1966
1967 shseat->seat = seat;
1968 wl_list_init(&shseat->popup_grab.surfaces_list);
1969
1970 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1971 wl_signal_add(&seat->destroy_signal,
1972 &shseat->seat_destroy_listener);
1973
1974 return shseat;
1975}
1976
1977static struct shell_seat *
1978get_shell_seat(struct weston_seat *seat)
1979{
1980 struct wl_listener *listener;
1981
1982 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1983 if (listener == NULL)
1984 return create_shell_seat(seat);
1985
1986 return container_of(listener,
1987 struct shell_seat, seat_destroy_listener);
1988}
1989
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001990static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001991popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001992{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001993 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001994 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001995 struct shell_seat *shseat =
1996 container_of(grab, struct shell_seat, popup_grab.grab);
1997 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001998 wl_fixed_t sx, sy;
1999
2000 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2001 pointer->x, pointer->y,
2002 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002003
Pekka Paalanencb108432012-01-19 16:25:40 +02002004 if (surface && surface->resource.client == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002005 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002006 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002007 weston_pointer_set_focus(pointer, NULL,
2008 wl_fixed_from_int(0),
2009 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002010 }
2011}
2012
2013static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002014popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002015{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002016 struct weston_pointer *pointer = grab->pointer;
2017 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002018
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002019 if (pointer->focus_resource) {
2020 weston_surface_from_global_fixed(pointer->focus,
2021 pointer->x, pointer->y,
2022 &sx, &sy);
2023 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2024 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002025}
2026
2027static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002028popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002029 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002030{
2031 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002032 struct shell_seat *shseat =
2033 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002034 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002035 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002036 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002037
Daniel Stone37816df2012-05-16 18:45:18 +01002038 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002039 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002040 display = wl_client_get_display(resource->client);
2041 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002042 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002043 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002044 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002045 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002046 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002047 }
2048
Daniel Stone4dbadb12012-05-30 16:31:51 +01002049 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002050 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002051}
2052
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002053static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002054 popup_grab_focus,
2055 popup_grab_motion,
2056 popup_grab_button,
2057};
2058
2059static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002060popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002061{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002062 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002063 struct shell_seat *shseat =
2064 container_of(grab, struct shell_seat, popup_grab.grab);
2065 struct shell_surface *shsurf;
2066 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002067
2068 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002069 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002070 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002071 shseat->popup_grab.grab.interface = NULL;
2072 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002073 /* Send the popup_done event to all the popups open */
2074 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2075 wl_shell_surface_send_popup_done(&shsurf->resource);
2076 shsurf->popup.shseat = NULL;
2077 if (prev) {
2078 wl_list_init(&prev->popup.grab_link);
2079 }
2080 prev = shsurf;
2081 }
2082 wl_list_init(&prev->popup.grab_link);
2083 wl_list_init(&shseat->popup_grab.surfaces_list);
2084 }
2085}
2086
2087static void
2088add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2089{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002090 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002091
2092 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002093 shseat->popup_grab.client = shsurf->resource.client;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002094 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002095 /* We must make sure here that this popup was opened after
2096 * a mouse press, and not just by moving around with other
2097 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002098 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002099 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002100
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002101 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002102 }
2103 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2104}
2105
2106static void
2107remove_popup_grab(struct shell_surface *shsurf)
2108{
2109 struct shell_seat *shseat = shsurf->popup.shseat;
2110
2111 wl_list_remove(&shsurf->popup.grab_link);
2112 wl_list_init(&shsurf->popup.grab_link);
2113 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002114 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002115 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002116 }
2117}
2118
2119static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002120shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002121{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002122 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002123 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002124 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002125
2126 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002127
Pekka Paalanen483243f2013-03-08 14:56:50 +02002128 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002129 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2130 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002131
Kristian Høgsberge3148752013-05-06 23:19:49 -04002132 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002133 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002134 } else {
2135 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002136 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002137 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002138}
2139
2140static void
2141shell_surface_set_popup(struct wl_client *client,
2142 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002143 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002144 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002145 struct wl_resource *parent_resource,
2146 int32_t x, int32_t y, uint32_t flags)
2147{
2148 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002149
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002150 shsurf->type = SHELL_SURFACE_POPUP;
2151 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002152 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002153 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002154 shsurf->popup.x = x;
2155 shsurf->popup.y = y;
2156}
2157
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002158static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002159 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002160 shell_surface_move,
2161 shell_surface_resize,
2162 shell_surface_set_toplevel,
2163 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002164 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002165 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002166 shell_surface_set_maximized,
2167 shell_surface_set_title,
2168 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002169};
2170
2171static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002172destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002173{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002174 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2175 remove_popup_grab(shsurf);
2176 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002177
Alex Wubd3354b2012-04-17 17:20:49 +08002178 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002179 shell_surface_is_top_fullscreen(shsurf))
2180 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002181
Alex Wuaa08e2d2012-03-05 11:01:40 +08002182 if (shsurf->fullscreen.black_surface)
2183 weston_surface_destroy(shsurf->fullscreen.black_surface);
2184
Alex Wubd3354b2012-04-17 17:20:49 +08002185 /* As destroy_resource() use wl_list_for_each_safe(),
2186 * we can always remove the listener.
2187 */
2188 wl_list_remove(&shsurf->surface_destroy_listener.link);
2189 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002190 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002191 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002192
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002193 wl_list_remove(&shsurf->link);
2194 free(shsurf);
2195}
2196
2197static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002198shell_destroy_shell_surface(struct wl_resource *resource)
2199{
2200 struct shell_surface *shsurf = resource->data;
2201
2202 destroy_shell_surface(shsurf);
2203}
2204
2205static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002206shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002207{
2208 struct shell_surface *shsurf = container_of(listener,
2209 struct shell_surface,
2210 surface_destroy_listener);
2211
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002212 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002213 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002214 } else {
2215 wl_signal_emit(&shsurf->resource.destroy_signal,
2216 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002217 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002218 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002219}
2220
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002221static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002222shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002223
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002224static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002225get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002226{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002227 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002228 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002229 else
2230 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002231}
2232
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002233static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002234create_shell_surface(void *shell, struct weston_surface *surface,
2235 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002236{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002237 struct shell_surface *shsurf;
2238
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002239 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002240 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002241 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002242 }
2243
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002244 shsurf = calloc(1, sizeof *shsurf);
2245 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002246 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002247 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002248 }
2249
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002250 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002251 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002252
Tiago Vignattibc052c92012-04-19 16:18:18 +03002253 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002254 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002255 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002256 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002257 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002258 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2259 shsurf->fullscreen.framerate = 0;
2260 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002261 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002262 wl_list_init(&shsurf->fullscreen.transform.link);
2263
Tiago Vignattibc052c92012-04-19 16:18:18 +03002264 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002265 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002266 wl_signal_add(&surface->resource.destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002267 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002268
2269 /* init link so its safe to always remove it in destroy_shell_surface */
2270 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002271 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002272
Pekka Paalanen460099f2012-01-20 16:48:25 +02002273 /* empty when not in use */
2274 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002275 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002276
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002277 wl_list_init(&shsurf->workspace_transform.link);
2278
Pekka Paalanen98262232011-12-01 10:42:22 +02002279 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002280 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002281
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002282 shsurf->client = client;
2283
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002284 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002285}
2286
2287static void
2288shell_get_shell_surface(struct wl_client *client,
2289 struct wl_resource *resource,
2290 uint32_t id,
2291 struct wl_resource *surface_resource)
2292{
2293 struct weston_surface *surface = surface_resource->data;
2294 struct desktop_shell *shell = resource->data;
2295 struct shell_surface *shsurf;
2296
2297 if (get_shell_surface(surface)) {
2298 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002299 WL_DISPLAY_ERROR_INVALID_OBJECT,
2300 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002301 return;
2302 }
2303
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002304 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002305 if (!shsurf) {
2306 wl_resource_post_error(surface_resource,
2307 WL_DISPLAY_ERROR_INVALID_OBJECT,
2308 "surface->configure already set");
2309 return;
2310 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002311
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002312 shsurf->resource.destroy = shell_destroy_shell_surface;
2313 shsurf->resource.object.id = id;
2314 shsurf->resource.object.interface = &wl_shell_surface_interface;
2315 shsurf->resource.object.implementation =
2316 (void (**)(void)) &shell_surface_implementation;
2317 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002318
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002319 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002320}
2321
2322static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002323 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002324};
2325
Kristian Høgsberg07937562011-04-12 17:25:42 -04002326static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002327shell_fade(struct desktop_shell *shell, enum fade_type type);
2328
2329static int
2330screensaver_timeout(void *data)
2331{
2332 struct desktop_shell *shell = data;
2333
2334 shell_fade(shell, FADE_OUT);
2335
2336 return 1;
2337}
2338
2339static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002340handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002341{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002342 struct desktop_shell *shell =
2343 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002344
Pekka Paalanen18027e52011-12-02 16:31:49 +02002345 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002346
2347 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002348 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002349}
2350
2351static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002352launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002353{
2354 if (shell->screensaver.binding)
2355 return;
2356
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002357 if (!shell->screensaver.path) {
2358 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002359 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002360 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002361
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002362 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002363 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002364 return;
2365 }
2366
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002367 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002368 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002369 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002370 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002371}
2372
2373static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002374terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002375{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002376 if (shell->screensaver.process.pid == 0)
2377 return;
2378
2379 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002380}
2381
2382static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002383configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002384{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002385 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002386
Giulio Camuffo184df502013-02-21 11:29:21 +01002387 if (width == 0)
2388 return;
2389
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002390 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2391 if (s->output == es->output && s != es) {
2392 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002393 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002394 }
2395 }
2396
Giulio Camuffo184df502013-02-21 11:29:21 +01002397 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002398
2399 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002400 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002401 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002402 }
2403}
2404
2405static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002406background_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 -04002407{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002408 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002409
Giulio Camuffo184df502013-02-21 11:29:21 +01002410 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002411}
2412
2413static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002414desktop_shell_set_background(struct wl_client *client,
2415 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002416 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002417 struct wl_resource *surface_resource)
2418{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002419 struct desktop_shell *shell = resource->data;
2420 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002421
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002422 if (surface->configure) {
2423 wl_resource_post_error(surface_resource,
2424 WL_DISPLAY_ERROR_INVALID_OBJECT,
2425 "surface role already assigned");
2426 return;
2427 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002428
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002429 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002430 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002431 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002432 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002433 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002434 surface->output->width,
2435 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002436}
2437
2438static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002439panel_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 -04002440{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002441 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002442
Giulio Camuffo184df502013-02-21 11:29:21 +01002443 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002444}
2445
2446static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002447desktop_shell_set_panel(struct wl_client *client,
2448 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002449 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002450 struct wl_resource *surface_resource)
2451{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002452 struct desktop_shell *shell = resource->data;
2453 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002454
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002455 if (surface->configure) {
2456 wl_resource_post_error(surface_resource,
2457 WL_DISPLAY_ERROR_INVALID_OBJECT,
2458 "surface role already assigned");
2459 return;
2460 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002461
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002462 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002463 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002464 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002465 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002466 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002467 surface->output->width,
2468 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002469}
2470
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002471static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002472lock_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 -04002473{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002474 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002475
Giulio Camuffo184df502013-02-21 11:29:21 +01002476 if (width == 0)
2477 return;
2478
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002479 surface->geometry.width = width;
2480 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002481 center_on_output(surface, get_default_output(shell->compositor));
2482
2483 if (!weston_surface_is_mapped(surface)) {
2484 wl_list_insert(&shell->lock_layer.surface_list,
2485 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002486 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002487 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002488 }
2489}
2490
2491static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002492handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002493{
Tiago Vignattibe143262012-04-16 17:31:41 +03002494 struct desktop_shell *shell =
2495 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002496
Martin Minarik6d118362012-06-07 18:01:59 +02002497 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002498 shell->lock_surface = NULL;
2499}
2500
2501static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002502desktop_shell_set_lock_surface(struct wl_client *client,
2503 struct wl_resource *resource,
2504 struct wl_resource *surface_resource)
2505{
Tiago Vignattibe143262012-04-16 17:31:41 +03002506 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002507 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002508
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002509 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002510
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002511 if (!shell->locked)
2512 return;
2513
Pekka Paalanen98262232011-12-01 10:42:22 +02002514 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002515
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002516 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2517 wl_signal_add(&surface_resource->destroy_signal,
2518 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002519
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002520 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002521 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002522}
2523
2524static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002525resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002526{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002527 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002528
Pekka Paalanen77346a62011-11-30 16:26:35 +02002529 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002530
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002531 wl_list_remove(&shell->lock_layer.link);
2532 wl_list_insert(&shell->compositor->cursor_layer.link,
2533 &shell->fullscreen_layer.link);
2534 wl_list_insert(&shell->fullscreen_layer.link,
2535 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002536 if (shell->showing_input_panels) {
2537 wl_list_insert(&shell->panel_layer.link,
2538 &shell->input_panel_layer.link);
2539 wl_list_insert(&shell->input_panel_layer.link,
2540 &ws->layer.link);
2541 } else {
2542 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2543 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002544
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002545 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002546
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002547 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002548 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002549 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002550}
2551
2552static void
2553desktop_shell_unlock(struct wl_client *client,
2554 struct wl_resource *resource)
2555{
Tiago Vignattibe143262012-04-16 17:31:41 +03002556 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002557
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002558 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002559
2560 if (shell->locked)
2561 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002562}
2563
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002564static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002565desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002566 struct wl_resource *resource,
2567 struct wl_resource *surface_resource)
2568{
2569 struct desktop_shell *shell = resource->data;
2570
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002571 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002572}
2573
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002574static void
2575desktop_shell_desktop_ready(struct wl_client *client,
2576 struct wl_resource *resource)
2577{
2578 struct desktop_shell *shell = resource->data;
2579
2580 shell_fade_startup(shell);
2581}
2582
Kristian Høgsberg75840622011-09-06 13:48:16 -04002583static const struct desktop_shell_interface desktop_shell_implementation = {
2584 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002585 desktop_shell_set_panel,
2586 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002587 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002588 desktop_shell_set_grab_surface,
2589 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002590};
2591
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002592static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002593get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002594{
2595 struct shell_surface *shsurf;
2596
2597 shsurf = get_shell_surface(surface);
2598 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002599 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002600 return shsurf->type;
2601}
2602
Kristian Høgsberg75840622011-09-06 13:48:16 -04002603static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002604move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002605{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002606 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002607 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002608 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002609 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002610
Pekka Paalanen01388e22013-04-25 13:57:44 +03002611 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002612 if (surface == NULL)
2613 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002614
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002615 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002616 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2617 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002618 return;
2619
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002620 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002621}
2622
2623static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002624resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002625{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002626 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002627 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002628 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002629 uint32_t edges = 0;
2630 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002631 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002632
Pekka Paalanen01388e22013-04-25 13:57:44 +03002633 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002634 if (surface == NULL)
2635 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002636
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002637 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002638 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2639 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002640 return;
2641
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002642 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002643 wl_fixed_to_int(seat->pointer->grab_x),
2644 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002645 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002646
Pekka Paalanen60921e52012-01-25 15:55:43 +02002647 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002648 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002649 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002650 edges |= 0;
2651 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002652 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002653
Pekka Paalanen60921e52012-01-25 15:55:43 +02002654 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002655 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002656 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002657 edges |= 0;
2658 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002659 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002660
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002661 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002662}
2663
2664static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002665surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002666 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002667{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002668 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002669 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002670 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002671 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002672 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002673
Pekka Paalanen01388e22013-04-25 13:57:44 +03002674 /* XXX: broken for windows containing sub-surfaces */
2675 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002676 if (surface == NULL)
2677 return;
2678
2679 shsurf = get_shell_surface(surface);
2680 if (!shsurf)
2681 return;
2682
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002683 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002684
Scott Moreau02709af2012-05-22 01:54:10 -06002685 if (surface->alpha > 1.0)
2686 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002687 if (surface->alpha < step)
2688 surface->alpha = step;
2689
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002690 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002691 weston_surface_damage(surface);
2692}
2693
2694static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002695do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002696 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002697{
Daniel Stone37816df2012-05-16 18:45:18 +01002698 struct weston_seat *ws = (struct weston_seat *) seat;
2699 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002700 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002701 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002702
2703 wl_list_for_each(output, &compositor->output_list, link) {
2704 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002705 wl_fixed_to_double(seat->pointer->x),
2706 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002707 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002708 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002709 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002710 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002711 increment = -output->zoom.increment;
2712 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002713 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002714 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002715 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002716 else
2717 increment = 0;
2718
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002719 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002720
Scott Moreaue6603982012-06-11 13:07:51 -06002721 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002722 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002723 else if (output->zoom.level > output->zoom.max_level)
2724 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002725 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002726 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002727 output->disable_planes++;
2728 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002729
Scott Moreaue6603982012-06-11 13:07:51 -06002730 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002731
Scott Moreau8dacaab2012-06-17 18:10:58 -06002732 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002733 }
2734 }
2735}
2736
Scott Moreauccbf29d2012-02-22 14:21:41 -07002737static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002738zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002739 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002740{
2741 do_zoom(seat, time, 0, axis, value);
2742}
2743
2744static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002745zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002746 void *data)
2747{
2748 do_zoom(seat, time, key, 0, 0);
2749}
2750
2751static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002752terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002753 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002754{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002755 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002756
Daniel Stone325fc2d2012-05-30 16:31:58 +01002757 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002758}
2759
2760static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002761rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002762{
2763 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002764 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002765 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002766 struct shell_surface *shsurf = rotate->base.shsurf;
2767 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002768 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002769
2770 if (!shsurf)
2771 return;
2772
2773 surface = shsurf->surface;
2774
2775 cx = 0.5f * surface->geometry.width;
2776 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002777
Daniel Stone37816df2012-05-16 18:45:18 +01002778 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2779 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002780 r = sqrtf(dx * dx + dy * dy);
2781
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002782 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002783 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002784
2785 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002786 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002787 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002788
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002789 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002790 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002791
2792 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002793 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002794 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002795 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002796 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002797
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002798 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002799 &shsurf->surface->geometry.transformation_list,
2800 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002801 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002802 wl_list_init(&shsurf->rotation.transform.link);
2803 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002804 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002805 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002806
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002807 /* We need to adjust the position of the surface
2808 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002809 cposx = surface->geometry.x + cx;
2810 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002811 dposx = rotate->center.x - cposx;
2812 dposy = rotate->center.y - cposy;
2813 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002814 weston_surface_set_position(surface,
2815 surface->geometry.x + dposx,
2816 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002817 }
2818
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002819 /* Repaint implies weston_surface_update_transform(), which
2820 * lazily applies the damage due to rotation update.
2821 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002822 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002823}
2824
2825static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002826rotate_grab_button(struct weston_pointer_grab *grab,
2827 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002828{
2829 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002830 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002831 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002832 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002833 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002834
Daniel Stone4dbadb12012-05-30 16:31:51 +01002835 if (pointer->button_count == 0 &&
2836 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002837 if (shsurf)
2838 weston_matrix_multiply(&shsurf->rotation.rotation,
2839 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002840 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002841 free(rotate);
2842 }
2843}
2844
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002845static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002846 noop_grab_focus,
2847 rotate_grab_motion,
2848 rotate_grab_button,
2849};
2850
2851static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002852surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002853{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002854 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002855 float dx, dy;
2856 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002857
Pekka Paalanen460099f2012-01-20 16:48:25 +02002858 rotate = malloc(sizeof *rotate);
2859 if (!rotate)
2860 return;
2861
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002862 weston_surface_to_global_float(surface->surface,
2863 surface->surface->geometry.width / 2,
2864 surface->surface->geometry.height / 2,
2865 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002866
Daniel Stone37816df2012-05-16 18:45:18 +01002867 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2868 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002869 r = sqrtf(dx * dx + dy * dy);
2870 if (r > 20.0f) {
2871 struct weston_matrix inverse;
2872
2873 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002874 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002875 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002876
2877 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002878 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002879 } else {
2880 weston_matrix_init(&surface->rotation.rotation);
2881 weston_matrix_init(&rotate->rotation);
2882 }
2883
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002884 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2885 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002886}
2887
2888static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002889rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002890 void *data)
2891{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002892 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002893 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002894 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002895 struct shell_surface *surface;
2896
Pekka Paalanen01388e22013-04-25 13:57:44 +03002897 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002898 if (base_surface == NULL)
2899 return;
2900
2901 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002902 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2903 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002904 return;
2905
2906 surface_rotate(surface, seat);
2907}
2908
2909static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002910lower_fullscreen_layer(struct desktop_shell *shell)
2911{
2912 struct workspace *ws;
2913 struct weston_surface *surface, *prev;
2914
2915 ws = get_current_workspace(shell);
2916 wl_list_for_each_reverse_safe(surface, prev,
2917 &shell->fullscreen_layer.surface_list,
2918 layer_link)
2919 weston_surface_restack(surface, &ws->layer.surface_list);
2920}
2921
2922static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002923activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002924 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002925{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002926 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002927 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002928 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002929
Pekka Paalanen01388e22013-04-25 13:57:44 +03002930 main_surface = weston_surface_get_main_surface(es);
2931
Daniel Stone37816df2012-05-16 18:45:18 +01002932 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002933
Jonas Ådahl8538b222012-08-29 22:13:03 +02002934 state = ensure_focus_state(shell, seat);
2935 if (state == NULL)
2936 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002937
2938 state->keyboard_focus = es;
2939 wl_list_remove(&state->surface_destroy_listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002940 wl_signal_add(&es->resource.destroy_signal,
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002941 &state->surface_destroy_listener);
2942
Pekka Paalanen01388e22013-04-25 13:57:44 +03002943 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002944 case SHELL_SURFACE_FULLSCREEN:
2945 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002946 shell_stack_fullscreen(get_shell_surface(main_surface));
2947 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002948 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002949 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002950 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002951 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002952 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002953 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002954 }
2955}
2956
Alex Wu21858432012-04-01 20:13:08 +08002957/* no-op func for checking black surface */
2958static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002959black_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 +08002960{
2961}
2962
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002963static bool
Alex Wu21858432012-04-01 20:13:08 +08002964is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2965{
2966 if (es->configure == black_surface_configure) {
2967 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002968 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002969 return true;
2970 }
2971 return false;
2972}
2973
Kristian Høgsberg75840622011-09-06 13:48:16 -04002974static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002975click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002976 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002977{
Daniel Stone37816df2012-05-16 18:45:18 +01002978 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002979 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002980 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002981 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002982
Daniel Stone37816df2012-05-16 18:45:18 +01002983 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002984 if (!focus)
2985 return;
2986
Pekka Paalanen01388e22013-04-25 13:57:44 +03002987 if (is_black_surface(focus, &main_surface))
2988 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002989
Pekka Paalanen01388e22013-04-25 13:57:44 +03002990 main_surface = weston_surface_get_main_surface(focus);
2991 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002992 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002993
Daniel Stone325fc2d2012-05-30 16:31:58 +01002994 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002995 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002996}
2997
2998static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002999lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003000{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003001 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003002
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003003 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003004 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003005 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003006 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003007
3008 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003009
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003010 /* Hide all surfaces by removing the fullscreen, panel and
3011 * toplevel layers. This way nothing else can show or receive
3012 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003013
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003014 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003015 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003016 if (shell->showing_input_panels)
3017 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003018 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003019 wl_list_insert(&shell->compositor->cursor_layer.link,
3020 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003021
Pekka Paalanen77346a62011-11-30 16:26:35 +02003022 launch_screensaver(shell);
3023
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003024 /* TODO: disable bindings that should not work while locked. */
3025
3026 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003027}
3028
3029static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003030unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003031{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003032 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003033 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003034 return;
3035 }
3036
3037 /* If desktop-shell client has gone away, unlock immediately. */
3038 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003039 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003040 return;
3041 }
3042
3043 if (shell->prepare_event_sent)
3044 return;
3045
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003046 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003047 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003048}
3049
3050static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003051shell_fade_done(struct weston_surface_animation *animation, void *data)
3052{
3053 struct desktop_shell *shell = data;
3054
3055 shell->fade.animation = NULL;
3056
3057 switch (shell->fade.type) {
3058 case FADE_IN:
3059 weston_surface_destroy(shell->fade.surface);
3060 shell->fade.surface = NULL;
3061 break;
3062 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003063 lock(shell);
3064 break;
3065 }
3066}
3067
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003068static struct weston_surface *
3069shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003070{
3071 struct weston_compositor *compositor = shell->compositor;
3072 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003073
3074 surface = weston_surface_create(compositor);
3075 if (!surface)
3076 return NULL;
3077
3078 weston_surface_configure(surface, 0, 0, 8192, 8192);
3079 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3080 wl_list_insert(&compositor->fade_layer.surface_list,
3081 &surface->layer_link);
3082 pixman_region32_init(&surface->input);
3083
3084 return surface;
3085}
3086
3087static void
3088shell_fade(struct desktop_shell *shell, enum fade_type type)
3089{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003090 float tint;
3091
3092 switch (type) {
3093 case FADE_IN:
3094 tint = 0.0;
3095 break;
3096 case FADE_OUT:
3097 tint = 1.0;
3098 break;
3099 default:
3100 weston_log("shell: invalid fade type\n");
3101 return;
3102 }
3103
3104 shell->fade.type = type;
3105
3106 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003107 shell->fade.surface = shell_fade_create_surface(shell);
3108 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003109 return;
3110
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003111 shell->fade.surface->alpha = 1.0 - tint;
3112 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003113 }
3114
3115 if (shell->fade.animation)
3116 weston_fade_update(shell->fade.animation,
3117 shell->fade.surface->alpha, tint, 30.0);
3118 else
3119 shell->fade.animation =
3120 weston_fade_run(shell->fade.surface,
3121 1.0 - tint, tint, 30.0,
3122 shell_fade_done, shell);
3123}
3124
3125static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003126do_shell_fade_startup(void *data)
3127{
3128 struct desktop_shell *shell = data;
3129
3130 shell_fade(shell, FADE_IN);
3131}
3132
3133static void
3134shell_fade_startup(struct desktop_shell *shell)
3135{
3136 struct wl_event_loop *loop;
3137
3138 if (!shell->fade.startup_timer)
3139 return;
3140
3141 wl_event_source_remove(shell->fade.startup_timer);
3142 shell->fade.startup_timer = NULL;
3143
3144 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3145 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3146}
3147
3148static int
3149fade_startup_timeout(void *data)
3150{
3151 struct desktop_shell *shell = data;
3152
3153 shell_fade_startup(shell);
3154 return 0;
3155}
3156
3157static void
3158shell_fade_init(struct desktop_shell *shell)
3159{
3160 /* Make compositor output all black, and wait for the desktop-shell
3161 * client to signal it is ready, then fade in. The timer triggers a
3162 * fade-in, in case the desktop-shell client takes too long.
3163 */
3164
3165 struct wl_event_loop *loop;
3166
3167 if (shell->fade.surface != NULL) {
3168 weston_log("%s: warning: fade surface already exists\n",
3169 __func__);
3170 return;
3171 }
3172
3173 shell->fade.surface = shell_fade_create_surface(shell);
3174 if (!shell->fade.surface)
3175 return;
3176
3177 weston_surface_update_transform(shell->fade.surface);
3178 weston_surface_damage(shell->fade.surface);
3179
3180 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3181 shell->fade.startup_timer =
3182 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3183 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3184}
3185
3186static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003187idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003188{
3189 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003190 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003191
3192 shell_fade(shell, FADE_OUT);
3193 /* lock() is called from shell_fade_done() */
3194}
3195
3196static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003197wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003198{
3199 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003200 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003201
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003202 unlock(shell);
3203}
3204
3205static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003206show_input_panels(struct wl_listener *listener, void *data)
3207{
3208 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003209 container_of(listener, struct desktop_shell,
3210 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003211 struct input_panel_surface *surface, *next;
3212 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003213
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003214 shell->text_input.surface = (struct weston_surface*)data;
3215
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003216 if (shell->showing_input_panels)
3217 return;
3218
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003219 shell->showing_input_panels = true;
3220
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003221 if (!shell->locked)
3222 wl_list_insert(&shell->panel_layer.link,
3223 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003224
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003225 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003226 &shell->input_panel.surfaces, link) {
3227 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003228 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003229 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003230 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003231 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003232 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003233 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003234 weston_surface_damage(ws);
3235 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003236 }
3237}
3238
3239static void
3240hide_input_panels(struct wl_listener *listener, void *data)
3241{
3242 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003243 container_of(listener, struct desktop_shell,
3244 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003245 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003246
Jan Arne Petersen61381972013-01-31 15:52:21 +01003247 if (!shell->showing_input_panels)
3248 return;
3249
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003250 shell->showing_input_panels = false;
3251
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003252 if (!shell->locked)
3253 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003254
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003255 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003256 &shell->input_panel_layer.surface_list, layer_link)
3257 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003258}
3259
3260static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003261update_input_panels(struct wl_listener *listener, void *data)
3262{
3263 struct desktop_shell *shell =
3264 container_of(listener, struct desktop_shell,
3265 update_input_panel_listener);
3266
3267 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3268}
3269
3270static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003271center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003272{
Giulio Camuffob8366642013-04-25 13:57:46 +03003273 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003274 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003275
Giulio Camuffob8366642013-04-25 13:57:46 +03003276 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3277
3278 x = output->x + (output->width - width) / 2 - surf_x / 2;
3279 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003280
3281 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003282}
3283
3284static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003285weston_surface_set_initial_position (struct weston_surface *surface,
3286 struct desktop_shell *shell)
3287{
3288 struct weston_compositor *compositor = shell->compositor;
3289 int ix = 0, iy = 0;
3290 int range_x, range_y;
3291 int dx, dy, x, y, panel_height;
3292 struct weston_output *output, *target_output = NULL;
3293 struct weston_seat *seat;
3294
3295 /* As a heuristic place the new window on the same output as the
3296 * pointer. Falling back to the output containing 0, 0.
3297 *
3298 * TODO: Do something clever for touch too?
3299 */
3300 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003301 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003302 ix = wl_fixed_to_int(seat->pointer->x);
3303 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003304 break;
3305 }
3306 }
3307
3308 wl_list_for_each(output, &compositor->output_list, link) {
3309 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3310 target_output = output;
3311 break;
3312 }
3313 }
3314
3315 if (!target_output) {
3316 weston_surface_set_position(surface, 10 + random() % 400,
3317 10 + random() % 400);
3318 return;
3319 }
3320
3321 /* Valid range within output where the surface will still be onscreen.
3322 * If this is negative it means that the surface is bigger than
3323 * output.
3324 */
3325 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003326 range_x = target_output->width - surface->geometry.width;
3327 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003328 surface->geometry.height;
3329
Rob Bradford4cb88c72012-08-13 15:18:44 +01003330 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003331 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003332 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003333 dx = 0;
3334
3335 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003336 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003337 else
3338 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003339
3340 x = target_output->x + dx;
3341 y = target_output->y + dy;
3342
3343 weston_surface_set_position (surface, x, y);
3344}
3345
3346static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003347map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003348 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003349{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003350 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003351 struct shell_surface *shsurf = get_shell_surface(surface);
3352 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003353 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003354 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003355 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003356 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003357 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003358
Pekka Paalanen60921e52012-01-25 15:55:43 +02003359 surface->geometry.width = width;
3360 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003361 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003362
3363 /* initial positioning, see also configure() */
3364 switch (surface_type) {
3365 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003366 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003367 break;
Alex Wu4539b082012-03-01 12:57:46 +08003368 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003369 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003370 shell_map_fullscreen(shsurf);
3371 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003372 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003373 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003374 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003375 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3376 NULL, NULL);
3377 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3378 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003379 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003380 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003381 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003382 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003383 case SHELL_SURFACE_NONE:
3384 weston_surface_set_position(surface,
3385 surface->geometry.x + sx,
3386 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003387 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003388 default:
3389 ;
3390 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003391
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003392 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003393 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003394 case SHELL_SURFACE_POPUP:
3395 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003396 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003397 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3398 break;
Alex Wu4539b082012-03-01 12:57:46 +08003399 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003400 case SHELL_SURFACE_NONE:
3401 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003402 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003403 ws = get_current_workspace(shell);
3404 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003405 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003406 }
3407
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003408 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003409 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003410 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3411 surface->output = shsurf->output;
3412 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003413
Juan Zhao7bb92f02011-12-15 11:31:51 -05003414 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003415 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003416 if (shsurf->transient.flags ==
3417 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3418 break;
3419 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003420 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003421 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003422 if (!shell->locked) {
3423 wl_list_for_each(seat, &compositor->seat_list, link)
3424 activate(shell, surface, seat);
3425 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003426 break;
3427 default:
3428 break;
3429 }
3430
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003431 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003432 {
3433 switch (shell->win_animation_type) {
3434 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003435 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003436 break;
3437 case ANIMATION_ZOOM:
3438 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3439 break;
3440 default:
3441 break;
3442 }
3443 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003444}
3445
3446static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003447configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003448 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003449{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003450 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3451 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003452 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003453
Pekka Paalanen77346a62011-11-30 16:26:35 +02003454 shsurf = get_shell_surface(surface);
3455 if (shsurf)
3456 surface_type = shsurf->type;
3457
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003458 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003459
3460 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003461 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003462 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003463 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003464 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003465 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003466 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003467 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3468 NULL, NULL);
3469 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003470 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003471 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003472 break;
Alex Wu4539b082012-03-01 12:57:46 +08003473 case SHELL_SURFACE_TOPLEVEL:
3474 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003475 default:
3476 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003477 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003478
Alex Wu4539b082012-03-01 12:57:46 +08003479 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003480 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003481 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003482
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003483 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003484 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003485 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003486}
3487
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003488static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003489shell_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 +03003490{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003491 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003492 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003493
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003494 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003495
Giulio Camuffo5085a752013-03-25 21:42:45 +01003496 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3497 remove_popup_grab(shsurf);
3498 }
3499
Giulio Camuffo184df502013-02-21 11:29:21 +01003500 if (width == 0)
3501 return;
3502
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003503 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003504 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003505 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003506 type_changed = 1;
3507 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003508
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003509 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003510 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003511 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003512 es->geometry.width != width ||
3513 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003514 float from_x, from_y;
3515 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003516
3517 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3518 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3519 configure(shell, es,
3520 es->geometry.x + to_x - from_x,
3521 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003522 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003523 }
3524}
3525
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003526static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003527
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003528static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003529desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003530{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003531 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003532 struct desktop_shell *shell =
3533 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003534
3535 shell->child.process.pid = 0;
3536 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003537
3538 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3539 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003540 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003541 shell->child.deathstamp = time;
3542 shell->child.deathcount = 0;
3543 }
3544
3545 shell->child.deathcount++;
3546 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003547 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003548 return;
3549 }
3550
Martin Minarik6d118362012-06-07 18:01:59 +02003551 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003552 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003553 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003554}
3555
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003556static void
3557launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003558{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003559 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003560 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003561
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003562 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003563 &shell->child.process,
3564 shell_exe,
3565 desktop_shell_sigchld);
3566
3567 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003568 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003569}
3570
3571static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003572bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3573{
Tiago Vignattibe143262012-04-16 17:31:41 +03003574 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003575
3576 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003577 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003578}
3579
Kristian Høgsberg75840622011-09-06 13:48:16 -04003580static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003581unbind_desktop_shell(struct wl_resource *resource)
3582{
Tiago Vignattibe143262012-04-16 17:31:41 +03003583 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003584
3585 if (shell->locked)
3586 resume_desktop(shell);
3587
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003588 shell->child.desktop_shell = NULL;
3589 shell->prepare_event_sent = false;
3590 free(resource);
3591}
3592
3593static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003594bind_desktop_shell(struct wl_client *client,
3595 void *data, uint32_t version, uint32_t id)
3596{
Tiago Vignattibe143262012-04-16 17:31:41 +03003597 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003598 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003599
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003600 resource = wl_client_add_object(client, &desktop_shell_interface,
3601 &desktop_shell_implementation,
3602 id, shell);
3603
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003604 if (client == shell->child.client) {
3605 resource->destroy = unbind_desktop_shell;
3606 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003607
3608 if (version < 2)
3609 shell_fade_startup(shell);
3610
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003611 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003612 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003613
3614 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3615 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003616 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003617}
3618
Pekka Paalanen6e168112011-11-24 11:34:05 +02003619static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003620screensaver_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 -04003621{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003622 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003623
Giulio Camuffo184df502013-02-21 11:29:21 +01003624 if (width == 0)
3625 return;
3626
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003627 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003628 if (!shell->locked)
3629 return;
3630
3631 center_on_output(surface, surface->output);
3632
3633 if (wl_list_empty(&surface->layer_link)) {
3634 wl_list_insert(shell->lock_layer.surface_list.prev,
3635 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003636 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003637 wl_event_source_timer_update(shell->screensaver.timer,
3638 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003639 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003640 }
3641}
3642
3643static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003644screensaver_set_surface(struct wl_client *client,
3645 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003646 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003647 struct wl_resource *output_resource)
3648{
Tiago Vignattibe143262012-04-16 17:31:41 +03003649 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003650 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003651 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003652
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003653 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003654 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003655 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003656}
3657
3658static const struct screensaver_interface screensaver_implementation = {
3659 screensaver_set_surface
3660};
3661
3662static void
3663unbind_screensaver(struct wl_resource *resource)
3664{
Tiago Vignattibe143262012-04-16 17:31:41 +03003665 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003666
Pekka Paalanen77346a62011-11-30 16:26:35 +02003667 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003668 free(resource);
3669}
3670
3671static void
3672bind_screensaver(struct wl_client *client,
3673 void *data, uint32_t version, uint32_t id)
3674{
Tiago Vignattibe143262012-04-16 17:31:41 +03003675 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003676 struct wl_resource *resource;
3677
3678 resource = wl_client_add_object(client, &screensaver_interface,
3679 &screensaver_implementation,
3680 id, shell);
3681
Pekka Paalanen77346a62011-11-30 16:26:35 +02003682 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003683 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003684 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003685 return;
3686 }
3687
3688 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3689 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003690 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003691}
3692
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003693static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003694input_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 -04003695{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003696 struct input_panel_surface *ip_surface = surface->configure_private;
3697 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003698 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003699 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003700 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003701
Giulio Camuffo184df502013-02-21 11:29:21 +01003702 if (width == 0)
3703 return;
3704
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003705 if (!weston_surface_is_mapped(surface)) {
3706 if (!shell->showing_input_panels)
3707 return;
3708
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003709 show_surface = 1;
3710 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003711
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003712 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003713
3714 if (ip_surface->panel) {
3715 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3716 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3717 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003718 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003719
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003720 x = ip_surface->output->x + (mode->width - width) / 2;
3721 y = ip_surface->output->y + mode->height - height;
3722 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003723
3724 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003725 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003726 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003727
3728 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003729 wl_list_insert(&shell->input_panel_layer.surface_list,
3730 &surface->layer_link);
3731 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003732 weston_surface_damage(surface);
3733 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3734 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003735}
3736
3737static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003738destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003739{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003740 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003741 wl_list_remove(&input_panel_surface->link);
3742
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003743 input_panel_surface->surface->configure = NULL;
3744
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003745 free(input_panel_surface);
3746}
3747
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003748static struct input_panel_surface *
3749get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003750{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003751 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003752 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003753 } else {
3754 return NULL;
3755 }
3756}
3757
3758static void
3759input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3760{
3761 struct input_panel_surface *ipsurface = container_of(listener,
3762 struct input_panel_surface,
3763 surface_destroy_listener);
3764
3765 if (ipsurface->resource.client) {
3766 wl_resource_destroy(&ipsurface->resource);
3767 } else {
3768 wl_signal_emit(&ipsurface->resource.destroy_signal,
3769 &ipsurface->resource);
3770 destroy_input_panel_surface(ipsurface);
3771 }
3772}
3773static struct input_panel_surface *
3774create_input_panel_surface(struct desktop_shell *shell,
3775 struct weston_surface *surface)
3776{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003777 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003778
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003779 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3780 if (!input_panel_surface)
3781 return NULL;
3782
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003783 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003784 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003785
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003786 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003787
3788 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003789
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003790 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3791 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003792 wl_signal_add(&surface->resource.destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003793 &input_panel_surface->surface_destroy_listener);
3794
3795 wl_list_init(&input_panel_surface->link);
3796
3797 return input_panel_surface;
3798}
3799
3800static void
3801input_panel_surface_set_toplevel(struct wl_client *client,
3802 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003803 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003804 uint32_t position)
3805{
3806 struct input_panel_surface *input_panel_surface = resource->data;
3807 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003808
3809 wl_list_insert(&shell->input_panel.surfaces,
3810 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003811
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003812 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003813 input_panel_surface->panel = 0;
3814}
3815
3816static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003817input_panel_surface_set_overlay_panel(struct wl_client *client,
3818 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003819{
3820 struct input_panel_surface *input_panel_surface = resource->data;
3821 struct desktop_shell *shell = input_panel_surface->shell;
3822
3823 wl_list_insert(&shell->input_panel.surfaces,
3824 &input_panel_surface->link);
3825
3826 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003827}
3828
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003829static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003830 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003831 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003832};
3833
3834static void
3835destroy_input_panel_surface_resource(struct wl_resource *resource)
3836{
3837 struct input_panel_surface *ipsurf = resource->data;
3838
3839 destroy_input_panel_surface(ipsurf);
3840}
3841
3842static void
3843input_panel_get_input_panel_surface(struct wl_client *client,
3844 struct wl_resource *resource,
3845 uint32_t id,
3846 struct wl_resource *surface_resource)
3847{
3848 struct weston_surface *surface = surface_resource->data;
3849 struct desktop_shell *shell = resource->data;
3850 struct input_panel_surface *ipsurf;
3851
3852 if (get_input_panel_surface(surface)) {
3853 wl_resource_post_error(surface_resource,
3854 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003855 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003856 return;
3857 }
3858
3859 ipsurf = create_input_panel_surface(shell, surface);
3860 if (!ipsurf) {
3861 wl_resource_post_error(surface_resource,
3862 WL_DISPLAY_ERROR_INVALID_OBJECT,
3863 "surface->configure already set");
3864 return;
3865 }
3866
3867 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3868 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003869 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003870 ipsurf->resource.object.implementation =
3871 (void (**)(void)) &input_panel_surface_implementation;
3872 ipsurf->resource.data = ipsurf;
3873
3874 wl_client_add_resource(client, &ipsurf->resource);
3875}
3876
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003877static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003878 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003879};
3880
3881static void
3882unbind_input_panel(struct wl_resource *resource)
3883{
3884 struct desktop_shell *shell = resource->data;
3885
3886 shell->input_panel.binding = NULL;
3887 free(resource);
3888}
3889
3890static void
3891bind_input_panel(struct wl_client *client,
3892 void *data, uint32_t version, uint32_t id)
3893{
3894 struct desktop_shell *shell = data;
3895 struct wl_resource *resource;
3896
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003897 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003898 &input_panel_implementation,
3899 id, shell);
3900
3901 if (shell->input_panel.binding == NULL) {
3902 resource->destroy = unbind_input_panel;
3903 shell->input_panel.binding = resource;
3904 return;
3905 }
3906
3907 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3908 "interface object already bound");
3909 wl_resource_destroy(resource);
3910}
3911
Kristian Høgsberg07045392012-02-19 18:52:44 -05003912struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003913 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003914 struct weston_surface *current;
3915 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003916 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003917};
3918
3919static void
3920switcher_next(struct switcher *switcher)
3921{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003922 struct weston_surface *surface;
3923 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003924 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003925 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003926
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003927 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003928 switch (get_shell_surface_type(surface)) {
3929 case SHELL_SURFACE_TOPLEVEL:
3930 case SHELL_SURFACE_FULLSCREEN:
3931 case SHELL_SURFACE_MAXIMIZED:
3932 if (first == NULL)
3933 first = surface;
3934 if (prev == switcher->current)
3935 next = surface;
3936 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003937 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003938 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003939 weston_surface_damage(surface);
3940 break;
3941 default:
3942 break;
3943 }
Alex Wu1659daa2012-04-01 20:13:09 +08003944
3945 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003946 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003947 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003948 weston_surface_damage(surface);
3949 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003950 }
3951
3952 if (next == NULL)
3953 next = first;
3954
Alex Wu07b26062012-03-12 16:06:01 +08003955 if (next == NULL)
3956 return;
3957
Kristian Høgsberg07045392012-02-19 18:52:44 -05003958 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003959 wl_signal_add(&next->resource.destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003960
3961 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003962 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003963
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003964 shsurf = get_shell_surface(switcher->current);
3965 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003966 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003967}
3968
3969static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003970switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003971{
3972 struct switcher *switcher =
3973 container_of(listener, struct switcher, listener);
3974
3975 switcher_next(switcher);
3976}
3977
3978static void
Daniel Stone351eb612012-05-31 15:27:47 -04003979switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003980{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003981 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003982 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003983 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003984
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003985 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003986 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003987 weston_surface_damage(surface);
3988 }
3989
Alex Wu07b26062012-03-12 16:06:01 +08003990 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003991 activate(switcher->shell, switcher->current,
3992 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003993 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003994 weston_keyboard_end_grab(keyboard);
3995 if (keyboard->input_method_resource)
3996 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003997 free(switcher);
3998}
3999
4000static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004001switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004002 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004003{
4004 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004005 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004006
Daniel Stonec9785ea2012-05-30 16:31:52 +01004007 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004008 switcher_next(switcher);
4009}
4010
4011static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004012switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004013 uint32_t mods_depressed, uint32_t mods_latched,
4014 uint32_t mods_locked, uint32_t group)
4015{
4016 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004017 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004018
Daniel Stone351eb612012-05-31 15:27:47 -04004019 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4020 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004021}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004022
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004023static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004024 switcher_key,
4025 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004026};
4027
4028static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004029switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004030 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004031{
Tiago Vignattibe143262012-04-16 17:31:41 +03004032 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004033 struct switcher *switcher;
4034
4035 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004036 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004037 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004038 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004039 wl_list_init(&switcher->listener.link);
4040
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004041 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004042 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004043 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004044 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4045 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004046 switcher_next(switcher);
4047}
4048
Pekka Paalanen3c647232011-12-22 13:43:43 +02004049static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004050backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004051 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004052{
4053 struct weston_compositor *compositor = data;
4054 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004055 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004056
4057 /* TODO: we're limiting to simple use cases, where we assume just
4058 * control on the primary display. We'd have to extend later if we
4059 * ever get support for setting backlights on random desktop LCD
4060 * panels though */
4061 output = get_default_output(compositor);
4062 if (!output)
4063 return;
4064
4065 if (!output->set_backlight)
4066 return;
4067
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004068 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4069 backlight_new = output->backlight_current - 25;
4070 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4071 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004072
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004073 if (backlight_new < 5)
4074 backlight_new = 5;
4075 if (backlight_new > 255)
4076 backlight_new = 255;
4077
4078 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004079 output->set_backlight(output, output->backlight_current);
4080}
4081
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004082struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004083 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004084 struct weston_seat *seat;
4085 uint32_t key[2];
4086 int key_released[2];
4087};
4088
4089static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004090debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004091 uint32_t key, uint32_t state)
4092{
4093 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4094 struct wl_resource *resource;
4095 struct wl_display *display;
4096 uint32_t serial;
4097 int send = 0, terminate = 0;
4098 int check_binding = 1;
4099 int i;
4100
4101 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4102 /* Do not run bindings on key releases */
4103 check_binding = 0;
4104
4105 for (i = 0; i < 2; i++)
4106 if (key == db->key[i])
4107 db->key_released[i] = 1;
4108
4109 if (db->key_released[0] && db->key_released[1]) {
4110 /* All key releases been swalled so end the grab */
4111 terminate = 1;
4112 } else if (key != db->key[0] && key != db->key[1]) {
4113 /* Should not swallow release of other keys */
4114 send = 1;
4115 }
4116 } else if (key == db->key[0] && !db->key_released[0]) {
4117 /* Do not check bindings for the first press of the binding
4118 * key. This allows it to be used as a debug shortcut.
4119 * We still need to swallow this event. */
4120 check_binding = 0;
4121 } else if (db->key[1]) {
4122 /* If we already ran a binding don't process another one since
4123 * we can't keep track of all the binding keys that were
4124 * pressed in order to swallow the release events. */
4125 send = 1;
4126 check_binding = 0;
4127 }
4128
4129 if (check_binding) {
4130 struct weston_compositor *ec = db->seat->compositor;
4131
4132 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4133 key, state)) {
4134 /* We ran a binding so swallow the press and keep the
4135 * grab to swallow the released too. */
4136 send = 0;
4137 terminate = 0;
4138 db->key[1] = key;
4139 } else {
4140 /* Terminate the grab since the key pressed is not a
4141 * debug binding key. */
4142 send = 1;
4143 terminate = 1;
4144 }
4145 }
4146
4147 if (send) {
4148 resource = grab->keyboard->focus_resource;
4149
4150 if (resource) {
4151 display = wl_client_get_display(resource->client);
4152 serial = wl_display_next_serial(display);
4153 wl_keyboard_send_key(resource, serial, time, key, state);
4154 }
4155 }
4156
4157 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004158 weston_keyboard_end_grab(grab->keyboard);
4159 if (grab->keyboard->input_method_resource)
4160 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004161 free(db);
4162 }
4163}
4164
4165static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004166debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004167 uint32_t mods_depressed, uint32_t mods_latched,
4168 uint32_t mods_locked, uint32_t group)
4169{
4170 struct wl_resource *resource;
4171
4172 resource = grab->keyboard->focus_resource;
4173 if (!resource)
4174 return;
4175
4176 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4177 mods_latched, mods_locked, group);
4178}
4179
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004180struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004181 debug_binding_key,
4182 debug_binding_modifiers
4183};
4184
4185static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004186debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004187{
4188 struct debug_binding_grab *grab;
4189
4190 grab = calloc(1, sizeof *grab);
4191 if (!grab)
4192 return;
4193
4194 grab->seat = (struct weston_seat *) seat;
4195 grab->key[0] = key;
4196 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004197 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004198}
4199
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004200static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004201force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004202 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004203{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004204 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004205 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004206 struct desktop_shell *shell = data;
4207 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004208 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004209
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004210 focus_surface = seat->keyboard->focus;
4211 if (!focus_surface)
4212 return;
4213
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004214 wl_signal_emit(&compositor->kill_signal, focus_surface);
4215
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004216 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03004217 wl_client_get_credentials(client, &pid, NULL, NULL);
4218
4219 /* Skip clients that we launched ourselves (the credentials of
4220 * the socketpair is ours) */
4221 if (pid == getpid())
4222 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004223
Daniel Stone325fc2d2012-05-30 16:31:58 +01004224 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004225}
4226
4227static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004228workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004229 uint32_t key, void *data)
4230{
4231 struct desktop_shell *shell = data;
4232 unsigned int new_index = shell->workspaces.current;
4233
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004234 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004235 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004236 if (new_index != 0)
4237 new_index--;
4238
4239 change_workspace(shell, new_index);
4240}
4241
4242static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004243workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004244 uint32_t key, void *data)
4245{
4246 struct desktop_shell *shell = data;
4247 unsigned int new_index = shell->workspaces.current;
4248
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004249 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004250 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004251 if (new_index < shell->workspaces.num - 1)
4252 new_index++;
4253
4254 change_workspace(shell, new_index);
4255}
4256
4257static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004258workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004259 uint32_t key, void *data)
4260{
4261 struct desktop_shell *shell = data;
4262 unsigned int new_index;
4263
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004264 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004265 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004266 new_index = key - KEY_F1;
4267 if (new_index >= shell->workspaces.num)
4268 new_index = shell->workspaces.num - 1;
4269
4270 change_workspace(shell, new_index);
4271}
4272
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004273static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004274workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004275 uint32_t key, void *data)
4276{
4277 struct desktop_shell *shell = data;
4278 unsigned int new_index = shell->workspaces.current;
4279
4280 if (shell->locked)
4281 return;
4282
4283 if (new_index != 0)
4284 new_index--;
4285
4286 take_surface_to_workspace_by_seat(shell, seat, new_index);
4287}
4288
4289static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004290workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004291 uint32_t key, void *data)
4292{
4293 struct desktop_shell *shell = data;
4294 unsigned int new_index = shell->workspaces.current;
4295
4296 if (shell->locked)
4297 return;
4298
4299 if (new_index < shell->workspaces.num - 1)
4300 new_index++;
4301
4302 take_surface_to_workspace_by_seat(shell, seat, new_index);
4303}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004304
4305static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004306shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004307{
Tiago Vignattibe143262012-04-16 17:31:41 +03004308 struct desktop_shell *shell =
4309 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004310 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004311
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004312 if (shell->child.client)
4313 wl_client_destroy(shell->child.client);
4314
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004315 wl_list_remove(&shell->idle_listener.link);
4316 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004317 wl_list_remove(&shell->show_input_panel_listener.link);
4318 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004319
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004320 wl_array_for_each(ws, &shell->workspaces.array)
4321 workspace_destroy(*ws);
4322 wl_array_release(&shell->workspaces.array);
4323
Pekka Paalanen3c647232011-12-22 13:43:43 +02004324 free(shell->screensaver.path);
4325 free(shell);
4326}
4327
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004328static void
4329shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4330{
4331 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004332 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004333
4334 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004335 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4336 MODIFIER_CTRL | MODIFIER_ALT,
4337 terminate_binding, ec);
4338 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4339 click_to_activate_binding,
4340 shell);
4341 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4342 MODIFIER_SUPER | MODIFIER_ALT,
4343 surface_opacity_binding, NULL);
4344 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4345 MODIFIER_SUPER, zoom_axis_binding,
4346 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004347
4348 /* configurable bindings */
4349 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004350 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4351 zoom_key_binding, NULL);
4352 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4353 zoom_key_binding, NULL);
4354 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4355 shell);
4356 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4357 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004358
4359 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4360 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4361 rotate_binding, NULL);
4362
Daniel Stone325fc2d2012-05-30 16:31:58 +01004363 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4364 shell);
4365 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4366 ec);
4367 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4368 backlight_binding, ec);
4369 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4370 ec);
4371 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4372 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004373 weston_compositor_add_key_binding(ec, KEY_K, mod,
4374 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004375 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4376 workspace_up_binding, shell);
4377 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4378 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004379 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4380 workspace_move_surface_up_binding,
4381 shell);
4382 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4383 workspace_move_surface_down_binding,
4384 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004385
4386 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4387 if (shell->workspaces.num > 1) {
4388 num_workspace_bindings = shell->workspaces.num;
4389 if (num_workspace_bindings > 6)
4390 num_workspace_bindings = 6;
4391 for (i = 0; i < num_workspace_bindings; i++)
4392 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4393 workspace_f_binding,
4394 shell);
4395 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004396
4397 /* Debug bindings */
4398 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4399 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004400}
4401
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004402WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004403module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004404 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004405{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004406 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004407 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004408 struct workspace **pws;
4409 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004410 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004411
4412 shell = malloc(sizeof *shell);
4413 if (shell == NULL)
4414 return -1;
4415
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004416 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004417 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004418
4419 shell->destroy_listener.notify = shell_destroy;
4420 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004421 shell->idle_listener.notify = idle_handler;
4422 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4423 shell->wake_listener.notify = wake_handler;
4424 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004425 shell->show_input_panel_listener.notify = show_input_panels;
4426 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4427 shell->hide_input_panel_listener.notify = hide_input_panels;
4428 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004429 shell->update_input_panel_listener.notify = update_input_panels;
4430 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004431 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004432 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004433 ec->shell_interface.create_shell_surface = create_shell_surface;
4434 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004435 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004436 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004437 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004438 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004439
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004440 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004441
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004442 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4443 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004444 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4445 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004446 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004447
4448 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004449 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004450
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004451 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004452
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004453 for (i = 0; i < shell->workspaces.num; i++) {
4454 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4455 if (pws == NULL)
4456 return -1;
4457
4458 *pws = workspace_create();
4459 if (*pws == NULL)
4460 return -1;
4461 }
4462 activate_workspace(shell, 0);
4463
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004464 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004465 wl_list_init(&shell->workspaces.animation.link);
4466 shell->workspaces.animation.frame = animate_workspace_change_frame;
4467
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004468 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4469 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004470 return -1;
4471
Kristian Høgsberg75840622011-09-06 13:48:16 -04004472 if (wl_display_add_global(ec->wl_display,
4473 &desktop_shell_interface,
4474 shell, bind_desktop_shell) == NULL)
4475 return -1;
4476
Pekka Paalanen6e168112011-11-24 11:34:05 +02004477 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4478 shell, bind_screensaver) == NULL)
4479 return -1;
4480
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004481 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004482 shell, bind_input_panel) == NULL)
4483 return -1;
4484
Jonas Ådahle9d22502012-08-29 22:13:01 +02004485 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4486 shell, bind_workspace_manager) == NULL)
4487 return -1;
4488
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004489 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004490
4491 loop = wl_display_get_event_loop(ec->wl_display);
4492 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004493
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004494 shell->screensaver.timer =
4495 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4496
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004497 wl_list_for_each(seat, &ec->seat_list, link)
4498 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004499
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004500 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004501
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004502 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004503
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004504 return 0;
4505}