blob: cfdb19ce831ec166b7487d0a2450b0829e825691 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050026#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040027#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020028#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
51 ANIMATION_FADE
52};
53
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020054enum fade_type {
55 FADE_IN,
56 FADE_OUT
57};
58
Jonas Ådahl04769742012-06-13 00:01:24 +020059struct focus_state {
60 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040061 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020062 struct weston_surface *keyboard_focus;
63 struct wl_list link;
64 struct wl_listener seat_destroy_listener;
65 struct wl_listener surface_destroy_listener;
66};
67
Jonas Ådahle3cddce2012-06-13 00:01:22 +020068struct workspace {
69 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020070
71 struct wl_list focus_list;
72 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020073};
74
Philipp Brüschweiler88013572012-08-06 13:44:42 +020075struct input_panel_surface {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010076 struct wl_resource resource;
77
78 struct desktop_shell *shell;
79
Philipp Brüschweiler88013572012-08-06 13:44:42 +020080 struct wl_list link;
81 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010082 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020083
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020084 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020085 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020086};
87
Tiago Vignattibe143262012-04-16 17:31:41 +030088struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050089 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040090
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020091 struct wl_listener idle_listener;
92 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040093 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020094 struct wl_listener show_input_panel_listener;
95 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020096 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020097
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050098 struct weston_layer fullscreen_layer;
99 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500100 struct weston_layer background_layer;
101 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200102 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500103
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400104 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300105 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400106
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200107 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200109 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200110 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200111
112 unsigned deathcount;
113 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200114 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200115
116 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200117 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200118 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200119
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200120 struct {
121 struct weston_surface *surface;
122 pixman_box32_t cursor_rectangle;
123 } text_input;
124
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400125 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500126 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100127
Pekka Paalanen77346a62011-11-30 16:26:35 +0200128 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200129 struct wl_array array;
130 unsigned int current;
131 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200132
Jonas Ådahle9d22502012-08-29 22:13:01 +0200133 struct wl_list client_list;
134
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200135 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200136 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200137 int anim_dir;
138 uint32_t anim_timestamp;
139 double anim_current;
140 struct workspace *anim_from;
141 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200142 } workspaces;
143
144 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200145 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200146 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200147 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200149 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200150 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500151
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200152 struct {
153 struct wl_resource *binding;
154 struct wl_list surfaces;
155 } input_panel;
156
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200157 struct {
158 struct weston_surface *surface;
159 struct weston_surface_animation *animation;
160 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300161 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200162 } fade;
163
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300164 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800165 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400166};
167
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500168enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200169 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500170 SHELL_SURFACE_TOPLEVEL,
171 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500172 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800173 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300174 SHELL_SURFACE_POPUP,
175 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200176};
177
Scott Moreauff1db4a2012-04-17 19:06:18 -0600178struct ping_timer {
179 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600180 uint32_t serial;
181};
182
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200183struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200184 struct wl_resource resource;
185
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500186 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200187 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400188 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300189 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200190
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400191 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400192 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500193 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800194 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800195 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600196 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100197
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500198 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200199 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500200 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200201 } rotation;
202
203 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100204 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500205 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100206 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400207 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500208 } popup;
209
Alex Wu4539b082012-03-01 12:57:46 +0800210 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300211 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400212 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300213 } transient;
214
215 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800216 enum wl_shell_surface_fullscreen_method type;
217 struct weston_transform transform; /* matrix from x, y */
218 uint32_t framerate;
219 struct weston_surface *black_surface;
220 } fullscreen;
221
Scott Moreauff1db4a2012-04-17 19:06:18 -0600222 struct ping_timer *ping_timer;
223
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200224 struct weston_transform workspace_transform;
225
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500226 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500227 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100228 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400229
230 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200231};
232
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300233struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400234 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235 struct shell_surface *shsurf;
236 struct wl_listener shsurf_destroy_listener;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400237 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300238};
239
240struct weston_move_grab {
241 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100242 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500243};
244
Pekka Paalanen460099f2012-01-20 16:48:25 +0200245struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300246 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500247 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200248 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200249 float x;
250 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200251 } center;
252};
253
Giulio Camuffo5085a752013-03-25 21:42:45 +0100254struct shell_seat {
255 struct weston_seat *seat;
256 struct wl_listener seat_destroy_listener;
257
258 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400259 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100260 struct wl_list surfaces_list;
261 struct wl_client *client;
262 int32_t initial_up;
263 } popup_grab;
264};
265
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400266static void
267activate(struct desktop_shell *shell, struct weston_surface *es,
268 struct weston_seat *seat);
269
270static struct workspace *
271get_current_workspace(struct desktop_shell *shell);
272
Alex Wubd3354b2012-04-17 17:20:49 +0800273static struct shell_surface *
274get_shell_surface(struct weston_surface *surface);
275
276static struct desktop_shell *
277shell_surface_get_shell(struct shell_surface *shsurf);
278
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500279static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400280surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500281
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300282static void
283shell_fade_startup(struct desktop_shell *shell);
284
Alex Wubd3354b2012-04-17 17:20:49 +0800285static bool
286shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
287{
288 struct desktop_shell *shell;
289 struct weston_surface *top_fs_es;
290
291 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100292
Alex Wubd3354b2012-04-17 17:20:49 +0800293 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
294 return false;
295
296 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100297 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800298 layer_link);
299 return (shsurf == get_shell_surface(top_fs_es));
300}
301
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500302static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400303destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300304{
305 struct shell_grab *grab;
306
307 grab = container_of(listener, struct shell_grab,
308 shsurf_destroy_listener);
309
310 grab->shsurf = NULL;
311}
312
313static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400314popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400315
316static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300317shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400318 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 struct desktop_shell *shell = shsurf->shell;
324
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400325 popup_grab_end(pointer);
326
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327 grab->grab.interface = interface;
328 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400329 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
330 wl_signal_add(&shsurf->resource.destroy_signal,
331 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300333 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300334
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400335 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300336 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400337 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400338 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300339}
340
341static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300342shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300343{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400344 if (grab->shsurf)
345 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300346
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400347 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300348}
349
350static void
Alex Wu4539b082012-03-01 12:57:46 +0800351center_on_output(struct weston_surface *surface,
352 struct weston_output *output);
353
Daniel Stone496ca172012-05-30 16:31:42 +0100354static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300355get_modifier(char *modifier)
356{
357 if (!modifier)
358 return MODIFIER_SUPER;
359
360 if (!strcmp("ctrl", modifier))
361 return MODIFIER_CTRL;
362 else if (!strcmp("alt", modifier))
363 return MODIFIER_ALT;
364 else if (!strcmp("super", modifier))
365 return MODIFIER_SUPER;
366 else
367 return MODIFIER_SUPER;
368}
369
Juan Zhaoe10d2792012-04-25 19:09:52 +0800370static enum animation_type
371get_animation_type(char *animation)
372{
373 if (!animation)
374 return ANIMATION_NONE;
375
376 if (!strcmp("zoom", animation))
377 return ANIMATION_ZOOM;
378 else if (!strcmp("fade", animation))
379 return ANIMATION_FADE;
380 else
381 return ANIMATION_NONE;
382}
383
Alex Wu4539b082012-03-01 12:57:46 +0800384static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400385shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200386{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400387 struct weston_config_section *section;
388 int duration;
389 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200390
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400391 section = weston_config_get_section(shell->compositor->config,
392 "screensaver", NULL, NULL);
393 weston_config_section_get_string(section,
394 "path", &shell->screensaver.path, NULL);
395 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200396 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400397
398 section = weston_config_get_section(shell->compositor->config,
399 "shell", NULL, NULL);
400 weston_config_section_get_string(section,
401 "binding-modifier", &s, "super");
402 shell->binding_modifier = get_modifier(s);
403 weston_config_section_get_string(section, "animation", &s, "none");
404 shell->win_animation_type = get_animation_type(s);
405 weston_config_section_get_uint(section, "num-workspaces",
406 &shell->workspaces.num,
407 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200408}
409
410static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200411focus_state_destroy(struct focus_state *state)
412{
413 wl_list_remove(&state->seat_destroy_listener.link);
414 wl_list_remove(&state->surface_destroy_listener.link);
415 free(state);
416}
417
418static void
419focus_state_seat_destroy(struct wl_listener *listener, void *data)
420{
421 struct focus_state *state = container_of(listener,
422 struct focus_state,
423 seat_destroy_listener);
424
425 wl_list_remove(&state->link);
426 focus_state_destroy(state);
427}
428
429static void
430focus_state_surface_destroy(struct wl_listener *listener, void *data)
431{
432 struct focus_state *state = container_of(listener,
433 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400434 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400435 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300436 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400437 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200438
Pekka Paalanen01388e22013-04-25 13:57:44 +0300439 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
440
Kristian Høgsberge3778222012-07-31 17:29:30 -0400441 next = NULL;
442 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300443 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400444 continue;
445
446 next = surface;
447 break;
448 }
449
Pekka Paalanen01388e22013-04-25 13:57:44 +0300450 /* if the focus was a sub-surface, activate its main surface */
451 if (main_surface != state->keyboard_focus)
452 next = main_surface;
453
Kristian Høgsberge3778222012-07-31 17:29:30 -0400454 if (next) {
455 shell = state->seat->compositor->shell_interface.shell;
456 activate(shell, next, state->seat);
457 } else {
458 wl_list_remove(&state->link);
459 focus_state_destroy(state);
460 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200461}
462
463static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400464focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200465{
Jonas Ådahl04769742012-06-13 00:01:24 +0200466 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200467
468 state = malloc(sizeof *state);
469 if (state == NULL)
470 return NULL;
471
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400472 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200473 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400474 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200475
476 state->seat_destroy_listener.notify = focus_state_seat_destroy;
477 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400478 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200479 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400480 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200481
482 return state;
483}
484
Jonas Ådahl8538b222012-08-29 22:13:03 +0200485static struct focus_state *
486ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
487{
488 struct workspace *ws = get_current_workspace(shell);
489 struct focus_state *state;
490
491 wl_list_for_each(state, &ws->focus_list, link)
492 if (state->seat == seat)
493 break;
494
495 if (&state->link == &ws->focus_list)
496 state = focus_state_create(seat, ws);
497
498 return state;
499}
500
Jonas Ådahl04769742012-06-13 00:01:24 +0200501static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400502restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200503{
504 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400505 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200506
507 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400508 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200509
Kristian Høgsberge3148752013-05-06 23:19:49 -0400510 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200511 }
512}
513
514static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200515replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
516 struct weston_seat *seat)
517{
518 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400519 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200520
521 wl_list_for_each(state, &ws->focus_list, link) {
522 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400523 surface = seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200524 state->keyboard_focus =
525 (struct weston_surface *) surface;
526 return;
527 }
528 }
529}
530
531static void
532drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
533 struct weston_surface *surface)
534{
535 struct focus_state *state;
536
537 wl_list_for_each(state, &ws->focus_list, link)
538 if (state->keyboard_focus == surface)
539 state->keyboard_focus = NULL;
540}
541
542static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200543workspace_destroy(struct workspace *ws)
544{
Jonas Ådahl04769742012-06-13 00:01:24 +0200545 struct focus_state *state, *next;
546
547 wl_list_for_each_safe(state, next, &ws->focus_list, link)
548 focus_state_destroy(state);
549
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200550 free(ws);
551}
552
Jonas Ådahl04769742012-06-13 00:01:24 +0200553static void
554seat_destroyed(struct wl_listener *listener, void *data)
555{
556 struct weston_seat *seat = data;
557 struct focus_state *state, *next;
558 struct workspace *ws = container_of(listener,
559 struct workspace,
560 seat_destroyed_listener);
561
562 wl_list_for_each_safe(state, next, &ws->focus_list, link)
563 if (state->seat == seat)
564 wl_list_remove(&state->link);
565}
566
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200567static struct workspace *
568workspace_create(void)
569{
570 struct workspace *ws = malloc(sizeof *ws);
571 if (ws == NULL)
572 return NULL;
573
574 weston_layer_init(&ws->layer, NULL);
575
Jonas Ådahl04769742012-06-13 00:01:24 +0200576 wl_list_init(&ws->focus_list);
577 wl_list_init(&ws->seat_destroyed_listener.link);
578 ws->seat_destroyed_listener.notify = seat_destroyed;
579
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200580 return ws;
581}
582
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200583static int
584workspace_is_empty(struct workspace *ws)
585{
586 return wl_list_empty(&ws->layer.surface_list);
587}
588
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200589static struct workspace *
590get_workspace(struct desktop_shell *shell, unsigned int index)
591{
592 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200593 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200594 pws += index;
595 return *pws;
596}
597
598static struct workspace *
599get_current_workspace(struct desktop_shell *shell)
600{
601 return get_workspace(shell, shell->workspaces.current);
602}
603
604static void
605activate_workspace(struct desktop_shell *shell, unsigned int index)
606{
607 struct workspace *ws;
608
609 ws = get_workspace(shell, index);
610 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
611
612 shell->workspaces.current = index;
613}
614
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200615static unsigned int
616get_output_height(struct weston_output *output)
617{
618 return abs(output->region.extents.y1 - output->region.extents.y2);
619}
620
621static void
622surface_translate(struct weston_surface *surface, double d)
623{
624 struct shell_surface *shsurf = get_shell_surface(surface);
625 struct weston_transform *transform;
626
627 transform = &shsurf->workspace_transform;
628 if (wl_list_empty(&transform->link))
629 wl_list_insert(surface->geometry.transformation_list.prev,
630 &shsurf->workspace_transform.link);
631
632 weston_matrix_init(&shsurf->workspace_transform.matrix);
633 weston_matrix_translate(&shsurf->workspace_transform.matrix,
634 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200635 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200636}
637
638static void
639workspace_translate_out(struct workspace *ws, double fraction)
640{
641 struct weston_surface *surface;
642 unsigned int height;
643 double d;
644
645 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
646 height = get_output_height(surface->output);
647 d = height * fraction;
648
649 surface_translate(surface, d);
650 }
651}
652
653static void
654workspace_translate_in(struct workspace *ws, double fraction)
655{
656 struct weston_surface *surface;
657 unsigned int height;
658 double d;
659
660 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
661 height = get_output_height(surface->output);
662
663 if (fraction > 0)
664 d = -(height - height * fraction);
665 else
666 d = height + height * fraction;
667
668 surface_translate(surface, d);
669 }
670}
671
672static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200673broadcast_current_workspace_state(struct desktop_shell *shell)
674{
675 struct wl_resource *resource;
676
677 wl_list_for_each(resource, &shell->workspaces.client_list, link)
678 workspace_manager_send_state(resource,
679 shell->workspaces.current,
680 shell->workspaces.num);
681}
682
683static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200684reverse_workspace_change_animation(struct desktop_shell *shell,
685 unsigned int index,
686 struct workspace *from,
687 struct workspace *to)
688{
689 shell->workspaces.current = index;
690
691 shell->workspaces.anim_to = to;
692 shell->workspaces.anim_from = from;
693 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
694 shell->workspaces.anim_timestamp = 0;
695
Scott Moreau4272e632012-08-13 09:58:41 -0600696 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200697}
698
699static void
700workspace_deactivate_transforms(struct workspace *ws)
701{
702 struct weston_surface *surface;
703 struct shell_surface *shsurf;
704
705 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
706 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200707 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
708 wl_list_remove(&shsurf->workspace_transform.link);
709 wl_list_init(&shsurf->workspace_transform.link);
710 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200711 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200712 }
713}
714
715static void
716finish_workspace_change_animation(struct desktop_shell *shell,
717 struct workspace *from,
718 struct workspace *to)
719{
Scott Moreau4272e632012-08-13 09:58:41 -0600720 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200721
722 wl_list_remove(&shell->workspaces.animation.link);
723 workspace_deactivate_transforms(from);
724 workspace_deactivate_transforms(to);
725 shell->workspaces.anim_to = NULL;
726
727 wl_list_remove(&shell->workspaces.anim_from->layer.link);
728}
729
730static void
731animate_workspace_change_frame(struct weston_animation *animation,
732 struct weston_output *output, uint32_t msecs)
733{
734 struct desktop_shell *shell =
735 container_of(animation, struct desktop_shell,
736 workspaces.animation);
737 struct workspace *from = shell->workspaces.anim_from;
738 struct workspace *to = shell->workspaces.anim_to;
739 uint32_t t;
740 double x, y;
741
742 if (workspace_is_empty(from) && workspace_is_empty(to)) {
743 finish_workspace_change_animation(shell, from, to);
744 return;
745 }
746
747 if (shell->workspaces.anim_timestamp == 0) {
748 if (shell->workspaces.anim_current == 0.0)
749 shell->workspaces.anim_timestamp = msecs;
750 else
751 shell->workspaces.anim_timestamp =
752 msecs -
753 /* Invers of movement function 'y' below. */
754 (asin(1.0 - shell->workspaces.anim_current) *
755 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
756 M_2_PI);
757 }
758
759 t = msecs - shell->workspaces.anim_timestamp;
760
761 /*
762 * x = [0, π/2]
763 * y(x) = sin(x)
764 */
765 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
766 y = sin(x);
767
768 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600769 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200770
771 workspace_translate_out(from, shell->workspaces.anim_dir * y);
772 workspace_translate_in(to, shell->workspaces.anim_dir * y);
773 shell->workspaces.anim_current = y;
774
Scott Moreau4272e632012-08-13 09:58:41 -0600775 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200776 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200777 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200778 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200779}
780
781static void
782animate_workspace_change(struct desktop_shell *shell,
783 unsigned int index,
784 struct workspace *from,
785 struct workspace *to)
786{
787 struct weston_output *output;
788
789 int dir;
790
791 if (index > shell->workspaces.current)
792 dir = -1;
793 else
794 dir = 1;
795
796 shell->workspaces.current = index;
797
798 shell->workspaces.anim_dir = dir;
799 shell->workspaces.anim_from = from;
800 shell->workspaces.anim_to = to;
801 shell->workspaces.anim_current = 0.0;
802 shell->workspaces.anim_timestamp = 0;
803
804 output = container_of(shell->compositor->output_list.next,
805 struct weston_output, link);
806 wl_list_insert(&output->animation_list,
807 &shell->workspaces.animation.link);
808
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200809 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200810
811 workspace_translate_in(to, 0);
812
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400813 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200814
Scott Moreau4272e632012-08-13 09:58:41 -0600815 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200816}
817
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200818static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200819update_workspace(struct desktop_shell *shell, unsigned int index,
820 struct workspace *from, struct workspace *to)
821{
822 shell->workspaces.current = index;
823 wl_list_insert(&from->layer.link, &to->layer.link);
824 wl_list_remove(&from->layer.link);
825}
826
827static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828change_workspace(struct desktop_shell *shell, unsigned int index)
829{
830 struct workspace *from;
831 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200832
833 if (index == shell->workspaces.current)
834 return;
835
836 /* Don't change workspace when there is any fullscreen surfaces. */
837 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
838 return;
839
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200840 from = get_current_workspace(shell);
841 to = get_workspace(shell, index);
842
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200843 if (shell->workspaces.anim_from == to &&
844 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200845 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200846 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200847 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200848 return;
849 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200850
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851 if (shell->workspaces.anim_to != NULL)
852 finish_workspace_change_animation(shell,
853 shell->workspaces.anim_from,
854 shell->workspaces.anim_to);
855
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200856 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200857
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200858 if (workspace_is_empty(to) && workspace_is_empty(from))
859 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200860 else
861 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200862
863 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200864}
865
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200866static bool
867workspace_has_only(struct workspace *ws, struct weston_surface *surface)
868{
869 struct wl_list *list = &ws->layer.surface_list;
870 struct wl_list *e;
871
872 if (wl_list_empty(list))
873 return false;
874
875 e = list->next;
876
877 if (e->next != list)
878 return false;
879
880 return container_of(e, struct weston_surface, layer_link) == surface;
881}
882
883static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200884move_surface_to_workspace(struct desktop_shell *shell,
885 struct weston_surface *surface,
886 uint32_t workspace)
887{
888 struct workspace *from;
889 struct workspace *to;
890 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300891 struct weston_surface *focus;
892
893 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200894
895 if (workspace == shell->workspaces.current)
896 return;
897
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200898 if (workspace >= shell->workspaces.num)
899 workspace = shell->workspaces.num - 1;
900
Jonas Ådahle9d22502012-08-29 22:13:01 +0200901 from = get_current_workspace(shell);
902 to = get_workspace(shell, workspace);
903
904 wl_list_remove(&surface->layer_link);
905 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
906
907 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300908 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
909 if (!seat->keyboard)
910 continue;
911
912 focus = weston_surface_get_main_surface(seat->keyboard->focus);
913 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400914 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300915 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200916
917 weston_surface_damage_below(surface);
918}
919
920static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200921take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400922 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200923 unsigned int index)
924{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300925 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200926 struct shell_surface *shsurf;
927 struct workspace *from;
928 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200929 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200930
Pekka Paalanen01388e22013-04-25 13:57:44 +0300931 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 if (surface == NULL ||
933 index == shell->workspaces.current)
934 return;
935
936 from = get_current_workspace(shell);
937 to = get_workspace(shell, index);
938
939 wl_list_remove(&surface->layer_link);
940 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
941
Jonas Ådahle9d22502012-08-29 22:13:01 +0200942 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200943 drop_focus_state(shell, from, surface);
944
945 if (shell->workspaces.anim_from == to &&
946 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200947 wl_list_remove(&to->layer.link);
948 wl_list_insert(from->layer.link.prev, &to->layer.link);
949
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200950 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200951 broadcast_current_workspace_state(shell);
952
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200953 return;
954 }
955
956 if (shell->workspaces.anim_to != NULL)
957 finish_workspace_change_animation(shell,
958 shell->workspaces.anim_from,
959 shell->workspaces.anim_to);
960
961 if (workspace_is_empty(from) &&
962 workspace_has_only(to, surface))
963 update_workspace(shell, index, from, to);
964 else {
965 shsurf = get_shell_surface(surface);
966 if (wl_list_empty(&shsurf->workspace_transform.link))
967 wl_list_insert(&shell->workspaces.anim_sticky_list,
968 &shsurf->workspace_transform.link);
969
970 animate_workspace_change(shell, index, from, to);
971 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200972
973 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200974
975 state = ensure_focus_state(shell, seat);
976 if (state != NULL)
977 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200978}
979
980static void
981workspace_manager_move_surface(struct wl_client *client,
982 struct wl_resource *resource,
983 struct wl_resource *surface_resource,
984 uint32_t workspace)
985{
986 struct desktop_shell *shell = resource->data;
987 struct weston_surface *surface =
988 (struct weston_surface *) surface_resource;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300989 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200990
Pekka Paalanen01388e22013-04-25 13:57:44 +0300991 main_surface = weston_surface_get_main_surface(surface);
992 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200993}
994
995static const struct workspace_manager_interface workspace_manager_implementation = {
996 workspace_manager_move_surface,
997};
998
999static void
1000unbind_resource(struct wl_resource *resource)
1001{
1002 wl_list_remove(&resource->link);
1003 free(resource);
1004}
1005
1006static void
1007bind_workspace_manager(struct wl_client *client,
1008 void *data, uint32_t version, uint32_t id)
1009{
1010 struct desktop_shell *shell = data;
1011 struct wl_resource *resource;
1012
1013 resource = wl_client_add_object(client, &workspace_manager_interface,
1014 &workspace_manager_implementation,
1015 id, shell);
1016
1017 if (resource == NULL) {
1018 weston_log("couldn't add workspace manager object");
1019 return;
1020 }
1021
1022 resource->destroy = unbind_resource;
1023 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1024
1025 workspace_manager_send_state(resource,
1026 shell->workspaces.current,
1027 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001028}
1029
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001030static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001031noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001032{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001033}
1034
1035static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001036move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001037{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001038 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001039 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001040 struct shell_surface *shsurf = move->base.shsurf;
1041 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001042 int dx = wl_fixed_to_int(pointer->x + move->dx);
1043 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001044
1045 if (!shsurf)
1046 return;
1047
1048 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001049
Daniel Stone103db7f2012-05-08 17:17:55 +01001050 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001051 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001052
1053 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001054}
1055
1056static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001057move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001058 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001059{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001060 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1061 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001062 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001063 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001064
Daniel Stone4dbadb12012-05-30 16:31:51 +01001065 if (pointer->button_count == 0 &&
1066 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001067 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001068 free(grab);
1069 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001070}
1071
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001072static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001073 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001074 move_grab_motion,
1075 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001076};
1077
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001078static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001079surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001080{
1081 struct weston_move_grab *move;
1082
1083 if (!shsurf)
1084 return -1;
1085
1086 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1087 return 0;
1088
1089 move = malloc(sizeof *move);
1090 if (!move)
1091 return -1;
1092
1093 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001094 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001095 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001096 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001097
1098 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001099 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001100
1101 return 0;
1102}
1103
1104static void
1105shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1106 struct wl_resource *seat_resource, uint32_t serial)
1107{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001108 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001109 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001110 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001111
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001112 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001113 if (seat->pointer->button_count == 0 ||
1114 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001115 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001116 return;
1117
Kristian Høgsberge3148752013-05-06 23:19:49 -04001118 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001119 wl_resource_post_no_memory(resource);
1120}
1121
1122struct weston_resize_grab {
1123 struct shell_grab base;
1124 uint32_t edges;
1125 int32_t width, height;
1126};
1127
1128static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001129resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001130{
1131 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001132 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001133 struct shell_surface *shsurf = resize->base.shsurf;
1134 int32_t width, height;
1135 wl_fixed_t from_x, from_y;
1136 wl_fixed_t to_x, to_y;
1137
1138 if (!shsurf)
1139 return;
1140
1141 weston_surface_from_global_fixed(shsurf->surface,
1142 pointer->grab_x, pointer->grab_y,
1143 &from_x, &from_y);
1144 weston_surface_from_global_fixed(shsurf->surface,
1145 pointer->x, pointer->y, &to_x, &to_y);
1146
1147 width = resize->width;
1148 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1149 width += wl_fixed_to_int(from_x - to_x);
1150 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1151 width += wl_fixed_to_int(to_x - from_x);
1152 }
1153
1154 height = resize->height;
1155 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1156 height += wl_fixed_to_int(from_y - to_y);
1157 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1158 height += wl_fixed_to_int(to_y - from_y);
1159 }
1160
1161 shsurf->client->send_configure(shsurf->surface,
1162 resize->edges, width, height);
1163}
1164
1165static void
1166send_configure(struct weston_surface *surface,
1167 uint32_t edges, int32_t width, int32_t height)
1168{
1169 struct shell_surface *shsurf = get_shell_surface(surface);
1170
1171 wl_shell_surface_send_configure(&shsurf->resource,
1172 edges, width, height);
1173}
1174
1175static const struct weston_shell_client shell_client = {
1176 send_configure
1177};
1178
1179static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001180resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001181 uint32_t time, uint32_t button, uint32_t state_w)
1182{
1183 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001184 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001185 enum wl_pointer_button_state state = state_w;
1186
1187 if (pointer->button_count == 0 &&
1188 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1189 shell_grab_end(&resize->base);
1190 free(grab);
1191 }
1192}
1193
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001194static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001195 noop_grab_focus,
1196 resize_grab_motion,
1197 resize_grab_button,
1198};
1199
Giulio Camuffob8366642013-04-25 13:57:46 +03001200/*
1201 * Returns the bounding box of a surface and all its sub-surfaces,
1202 * in the surface coordinates system. */
1203static void
1204surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1205 int32_t *y, int32_t *w, int32_t *h) {
1206 pixman_region32_t region;
1207 pixman_box32_t *box;
1208 struct weston_subsurface *subsurface;
1209
1210 pixman_region32_init_rect(&region, 0, 0,
1211 surface->geometry.width,
1212 surface->geometry.height);
1213
1214 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1215 pixman_region32_union_rect(&region, &region,
1216 subsurface->position.x,
1217 subsurface->position.y,
1218 subsurface->surface->geometry.width,
1219 subsurface->surface->geometry.height);
1220 }
1221
1222 box = pixman_region32_extents(&region);
1223 if (x)
1224 *x = box->x1;
1225 if (y)
1226 *y = box->y1;
1227 if (w)
1228 *w = box->x2 - box->x1;
1229 if (h)
1230 *h = box->y2 - box->y1;
1231
1232 pixman_region32_fini(&region);
1233}
1234
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001235static int
1236surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001237 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001238{
1239 struct weston_resize_grab *resize;
1240
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001241 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1242 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001243 return 0;
1244
1245 if (edges == 0 || edges > 15 ||
1246 (edges & 3) == 3 || (edges & 12) == 12)
1247 return 0;
1248
1249 resize = malloc(sizeof *resize);
1250 if (!resize)
1251 return -1;
1252
1253 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001254 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1255 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001256
1257 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001258 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001259
1260 return 0;
1261}
1262
1263static void
1264shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1265 struct wl_resource *seat_resource, uint32_t serial,
1266 uint32_t edges)
1267{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001268 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001269 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001270 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001271
1272 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1273 return;
1274
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001275 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001276 if (seat->pointer->button_count == 0 ||
1277 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001278 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001279 return;
1280
Kristian Høgsberge3148752013-05-06 23:19:49 -04001281 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001282 wl_resource_post_no_memory(resource);
1283}
1284
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001285static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001286busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001287{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001288 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001289 struct weston_pointer *pointer = base->pointer;
1290 struct weston_surface *surface;
1291 wl_fixed_t sx, sy;
1292
1293 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1294 pointer->x, pointer->y,
1295 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001296
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001297 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001298 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001299 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001300 }
1301}
1302
1303static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001304busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001305{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001306}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001307
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001308static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001309busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001310 uint32_t time, uint32_t button, uint32_t state)
1311{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001312 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001313 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001314 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001315
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001316 if (shsurf && button == BTN_LEFT && state) {
1317 activate(shsurf->shell, shsurf->surface, seat);
1318 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001319 } else if (shsurf && button == BTN_RIGHT && state) {
1320 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001321 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001322 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001323}
1324
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001325static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001326 busy_cursor_grab_focus,
1327 busy_cursor_grab_motion,
1328 busy_cursor_grab_button,
1329};
1330
1331static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001332set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001333{
1334 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001335
1336 grab = malloc(sizeof *grab);
1337 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001338 return;
1339
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001340 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1341 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001342}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001343
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001344static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001345end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001346{
1347 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1348
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001349 if (grab->grab.interface == &busy_cursor_grab_interface &&
1350 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001351 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001352 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001353 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001354}
1355
Scott Moreau9521d5e2012-04-19 13:06:17 -06001356static void
1357ping_timer_destroy(struct shell_surface *shsurf)
1358{
1359 if (!shsurf || !shsurf->ping_timer)
1360 return;
1361
1362 if (shsurf->ping_timer->source)
1363 wl_event_source_remove(shsurf->ping_timer->source);
1364
1365 free(shsurf->ping_timer);
1366 shsurf->ping_timer = NULL;
1367}
1368
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001369static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001370ping_timeout_handler(void *data)
1371{
1372 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001373 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001374
Scott Moreau9521d5e2012-04-19 13:06:17 -06001375 /* Client is not responding */
1376 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001377
1378 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001379 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001380 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001381
1382 return 1;
1383}
1384
1385static void
1386ping_handler(struct weston_surface *surface, uint32_t serial)
1387{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001388 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001389 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001390 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001391
1392 if (!shsurf)
1393 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001394 if (!shsurf->resource.client)
1395 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001396
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001397 if (shsurf->surface == shsurf->shell->grab_surface)
1398 return;
1399
Scott Moreauff1db4a2012-04-17 19:06:18 -06001400 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001401 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001402 if (!shsurf->ping_timer)
1403 return;
1404
1405 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001406 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1407 shsurf->ping_timer->source =
1408 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1409 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1410
1411 wl_shell_surface_send_ping(&shsurf->resource, serial);
1412 }
1413}
1414
1415static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001416handle_pointer_focus(struct wl_listener *listener, void *data)
1417{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001418 struct weston_pointer *pointer = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001419 struct weston_surface *surface =
1420 (struct weston_surface *) pointer->focus;
1421 struct weston_compositor *compositor;
1422 struct shell_surface *shsurf;
1423 uint32_t serial;
1424
1425 if (!surface)
1426 return;
1427
1428 compositor = surface->compositor;
1429 shsurf = get_shell_surface(surface);
1430
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001431 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001432 set_busy_cursor(shsurf, pointer);
1433 } else {
1434 serial = wl_display_next_serial(compositor->wl_display);
1435 ping_handler(surface, serial);
1436 }
1437}
1438
1439static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001440create_pointer_focus_listener(struct weston_seat *seat)
1441{
1442 struct wl_listener *listener;
1443
Kristian Høgsberge3148752013-05-06 23:19:49 -04001444 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001445 return;
1446
1447 listener = malloc(sizeof *listener);
1448 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001449 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001450}
1451
1452static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001453shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1454 uint32_t serial)
1455{
1456 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001457 struct weston_seat *seat;
1458 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001459
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001460 if (shsurf->ping_timer == NULL)
1461 /* Just ignore unsolicited pong. */
1462 return;
1463
Scott Moreauff1db4a2012-04-17 19:06:18 -06001464 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001465 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001466 wl_list_for_each(seat, &ec->seat_list, link) {
1467 if(seat->pointer)
1468 end_busy_cursor(shsurf, seat->pointer);
1469 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001470 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001471 }
1472}
1473
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001474static void
1475shell_surface_set_title(struct wl_client *client,
1476 struct wl_resource *resource, const char *title)
1477{
1478 struct shell_surface *shsurf = resource->data;
1479
1480 free(shsurf->title);
1481 shsurf->title = strdup(title);
1482}
1483
1484static void
1485shell_surface_set_class(struct wl_client *client,
1486 struct wl_resource *resource, const char *class)
1487{
1488 struct shell_surface *shsurf = resource->data;
1489
1490 free(shsurf->class);
1491 shsurf->class = strdup(class);
1492}
1493
Juan Zhao96879df2012-02-07 08:45:41 +08001494static struct weston_output *
1495get_default_output(struct weston_compositor *compositor)
1496{
1497 return container_of(compositor->output_list.next,
1498 struct weston_output, link);
1499}
1500
Alex Wu4539b082012-03-01 12:57:46 +08001501static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001502restore_output_mode(struct weston_output *output)
1503{
1504 if (output->current != output->origin ||
1505 (int32_t)output->scale != output->origin_scale)
1506 weston_output_switch_mode(output,
1507 output->origin,
1508 output->origin_scale);
1509}
1510
1511static void
1512restore_all_output_modes(struct weston_compositor *compositor)
1513{
1514 struct weston_output *output;
1515
1516 wl_list_for_each(output, &compositor->output_list, link)
1517 restore_output_mode(output);
1518}
1519
1520static void
Alex Wu4539b082012-03-01 12:57:46 +08001521shell_unset_fullscreen(struct shell_surface *shsurf)
1522{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001523 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001524 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001525 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1526 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001527 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001528 }
Alex Wu4539b082012-03-01 12:57:46 +08001529 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1530 shsurf->fullscreen.framerate = 0;
1531 wl_list_remove(&shsurf->fullscreen.transform.link);
1532 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001533 if (shsurf->fullscreen.black_surface)
1534 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001535 shsurf->fullscreen.black_surface = NULL;
1536 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001537 weston_surface_set_position(shsurf->surface,
1538 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001539 if (shsurf->saved_rotation_valid) {
1540 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1541 &shsurf->rotation.transform.link);
1542 shsurf->saved_rotation_valid = false;
1543 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001544
1545 ws = get_current_workspace(shsurf->shell);
1546 wl_list_remove(&shsurf->surface->layer_link);
1547 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001548}
1549
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001550static void
1551shell_unset_maximized(struct shell_surface *shsurf)
1552{
1553 struct workspace *ws;
1554 /* undo all maximized things here */
1555 shsurf->output = get_default_output(shsurf->surface->compositor);
1556 weston_surface_set_position(shsurf->surface,
1557 shsurf->saved_x,
1558 shsurf->saved_y);
1559
1560 if (shsurf->saved_rotation_valid) {
1561 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1562 &shsurf->rotation.transform.link);
1563 shsurf->saved_rotation_valid = false;
1564 }
1565
1566 ws = get_current_workspace(shsurf->shell);
1567 wl_list_remove(&shsurf->surface->layer_link);
1568 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1569}
1570
Pekka Paalanen98262232011-12-01 10:42:22 +02001571static int
1572reset_shell_surface_type(struct shell_surface *surface)
1573{
1574 switch (surface->type) {
1575 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001576 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001577 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001578 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001579 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001580 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001581 case SHELL_SURFACE_NONE:
1582 case SHELL_SURFACE_TOPLEVEL:
1583 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001584 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001585 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001586 break;
1587 }
1588
1589 surface->type = SHELL_SURFACE_NONE;
1590 return 0;
1591}
1592
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001593static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001594set_surface_type(struct shell_surface *shsurf)
1595{
1596 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001597 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001598
1599 reset_shell_surface_type(shsurf);
1600
1601 shsurf->type = shsurf->next_type;
1602 shsurf->next_type = SHELL_SURFACE_NONE;
1603
1604 switch (shsurf->type) {
1605 case SHELL_SURFACE_TOPLEVEL:
1606 break;
1607 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001608 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001609 pes->geometry.x + shsurf->transient.x,
1610 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001611 break;
1612
1613 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001614 case SHELL_SURFACE_FULLSCREEN:
1615 shsurf->saved_x = surface->geometry.x;
1616 shsurf->saved_y = surface->geometry.y;
1617 shsurf->saved_position_valid = true;
1618
1619 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1620 wl_list_remove(&shsurf->rotation.transform.link);
1621 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001622 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001623 shsurf->saved_rotation_valid = true;
1624 }
1625 break;
1626
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001627 case SHELL_SURFACE_XWAYLAND:
1628 weston_surface_set_position(surface, shsurf->transient.x,
1629 shsurf->transient.y);
1630 break;
1631
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001632 default:
1633 break;
1634 }
1635}
1636
1637static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001638set_toplevel(struct shell_surface *shsurf)
1639{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001640 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001641}
1642
1643static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001644shell_surface_set_toplevel(struct wl_client *client,
1645 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001646{
Pekka Paalanen98262232011-12-01 10:42:22 +02001647 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001648
Tiago Vignattibc052c92012-04-19 16:18:18 +03001649 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001650}
1651
1652static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001653set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001654 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001655{
1656 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001657 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001658 shsurf->transient.x = x;
1659 shsurf->transient.y = y;
1660 shsurf->transient.flags = flags;
1661 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1662}
1663
1664static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001665shell_surface_set_transient(struct wl_client *client,
1666 struct wl_resource *resource,
1667 struct wl_resource *parent_resource,
1668 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001669{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001670 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001671 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001672
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001673 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001674}
1675
Tiago Vignattibe143262012-04-16 17:31:41 +03001676static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001677shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001678{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001679 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001680}
1681
1682static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001683get_output_panel_height(struct desktop_shell *shell,
1684 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001685{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001686 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001687 int panel_height = 0;
1688
1689 if (!output)
1690 return 0;
1691
Juan Zhao4ab94682012-07-09 22:24:09 -07001692 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001693 if (surface->output == output) {
1694 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001695 break;
1696 }
1697 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001698
Juan Zhao96879df2012-02-07 08:45:41 +08001699 return panel_height;
1700}
1701
1702static void
1703shell_surface_set_maximized(struct wl_client *client,
1704 struct wl_resource *resource,
1705 struct wl_resource *output_resource )
1706{
1707 struct shell_surface *shsurf = resource->data;
1708 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001709 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001710 uint32_t edges = 0, panel_height = 0;
1711
1712 /* get the default output, if the client set it as NULL
1713 check whether the ouput is available */
1714 if (output_resource)
1715 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001716 else if (es->output)
1717 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001718 else
1719 shsurf->output = get_default_output(es->compositor);
1720
Tiago Vignattibe143262012-04-16 17:31:41 +03001721 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001722 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001723 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001724
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001725 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001726 shsurf->output->width,
1727 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001728
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001729 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001730}
1731
Alex Wu21858432012-04-01 20:13:08 +08001732static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001733black_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 +08001734
Alex Wu4539b082012-03-01 12:57:46 +08001735static struct weston_surface *
1736create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001737 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001738 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001739{
1740 struct weston_surface *surface = NULL;
1741
1742 surface = weston_surface_create(ec);
1743 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001744 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001745 return NULL;
1746 }
1747
Alex Wu21858432012-04-01 20:13:08 +08001748 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001749 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001750 weston_surface_configure(surface, x, y, w, h);
1751 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001752 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001753 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001754 pixman_region32_fini(&surface->input);
1755 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001756
Alex Wu4539b082012-03-01 12:57:46 +08001757 return surface;
1758}
1759
1760/* Create black surface and append it to the associated fullscreen surface.
1761 * Handle size dismatch and positioning according to the method. */
1762static void
1763shell_configure_fullscreen(struct shell_surface *shsurf)
1764{
1765 struct weston_output *output = shsurf->fullscreen_output;
1766 struct weston_surface *surface = shsurf->surface;
1767 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001768 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001769 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001770
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001771 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1772 restore_output_mode(output);
1773
Alex Wu4539b082012-03-01 12:57:46 +08001774 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001775 shsurf->fullscreen.black_surface =
1776 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001777 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001778 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001779 output->width,
1780 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001781
1782 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1783 wl_list_insert(&surface->layer_link,
1784 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001785 shsurf->fullscreen.black_surface->output = output;
1786
Giulio Camuffob8366642013-04-25 13:57:46 +03001787 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1788 &surf_width, &surf_height);
1789
Alex Wu4539b082012-03-01 12:57:46 +08001790 switch (shsurf->fullscreen.type) {
1791 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001792 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001793 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001794 break;
1795 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001796 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001797 if (output->width == surf_width &&
1798 output->height == surf_height) {
1799 weston_surface_set_position(surface, output->x - surf_x,
1800 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001801 break;
1802 }
1803
Alex Wu4539b082012-03-01 12:57:46 +08001804 matrix = &shsurf->fullscreen.transform.matrix;
1805 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001806
Scott Moreau1bad5db2012-08-18 01:04:05 -06001807 output_aspect = (float) output->width /
1808 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001809 surface_aspect = (float) surface->geometry.width /
1810 (float) surface->geometry.height;
1811 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001812 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001813 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001814 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001815 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001816 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001817
Alex Wu4539b082012-03-01 12:57:46 +08001818 weston_matrix_scale(matrix, scale, scale, 1);
1819 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001820 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001821 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001822 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1823 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001824 weston_surface_set_position(surface, x, y);
1825
Alex Wu4539b082012-03-01 12:57:46 +08001826 break;
1827 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001828 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001829 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001830 surf_width * surface->buffer_scale,
1831 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001832 shsurf->fullscreen.framerate};
1833
Alexander Larsson355748e2013-05-28 16:23:38 +02001834 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001835 weston_surface_set_position(surface,
1836 output->x - surf_x,
1837 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001838 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001839 output->x - surf_x,
1840 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001841 output->width,
1842 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001843 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001844 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001845 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001846 center_on_output(surface, output);
1847 }
Alex Wubd3354b2012-04-17 17:20:49 +08001848 }
Alex Wu4539b082012-03-01 12:57:46 +08001849 break;
1850 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001851 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001852 break;
1853 default:
1854 break;
1855 }
1856}
1857
1858/* make the fullscreen and black surface at the top */
1859static void
1860shell_stack_fullscreen(struct shell_surface *shsurf)
1861{
Alex Wubd3354b2012-04-17 17:20:49 +08001862 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001863 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001864 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001865
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001866 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001867 wl_list_insert(&shell->fullscreen_layer.surface_list,
1868 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001869 weston_surface_damage(surface);
1870
1871 if (!shsurf->fullscreen.black_surface)
1872 shsurf->fullscreen.black_surface =
1873 create_black_surface(surface->compositor,
1874 surface,
1875 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001876 output->width,
1877 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001878
1879 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001880 wl_list_insert(&surface->layer_link,
1881 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001882 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001883}
1884
1885static void
1886shell_map_fullscreen(struct shell_surface *shsurf)
1887{
Alex Wu4539b082012-03-01 12:57:46 +08001888 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001889 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001890}
1891
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001892static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001893set_fullscreen(struct shell_surface *shsurf,
1894 uint32_t method,
1895 uint32_t framerate,
1896 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001897{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001898 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001899
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001900 if (output)
1901 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001902 else if (es->output)
1903 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001904 else
1905 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001906
Alex Wu4539b082012-03-01 12:57:46 +08001907 shsurf->fullscreen_output = shsurf->output;
1908 shsurf->fullscreen.type = method;
1909 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001910 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001911
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001912 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001913 shsurf->output->width,
1914 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001915}
1916
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001917static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001918shell_surface_set_fullscreen(struct wl_client *client,
1919 struct wl_resource *resource,
1920 uint32_t method,
1921 uint32_t framerate,
1922 struct wl_resource *output_resource)
1923{
1924 struct shell_surface *shsurf = resource->data;
1925 struct weston_output *output;
1926
1927 if (output_resource)
1928 output = output_resource->data;
1929 else
1930 output = NULL;
1931
1932 set_fullscreen(shsurf, method, framerate, output);
1933}
1934
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001935static void
1936set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1937{
1938 /* XXX: using the same fields for transient type */
1939 shsurf->transient.x = x;
1940 shsurf->transient.y = y;
1941 shsurf->transient.flags = flags;
1942 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1943}
1944
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001945static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001946
1947static void
1948destroy_shell_seat(struct wl_listener *listener, void *data)
1949{
1950 struct shell_seat *shseat =
1951 container_of(listener,
1952 struct shell_seat, seat_destroy_listener);
1953 struct shell_surface *shsurf, *prev = NULL;
1954
1955 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001956 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001957 shseat->popup_grab.client = NULL;
1958
1959 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1960 shsurf->popup.shseat = NULL;
1961 if (prev) {
1962 wl_list_init(&prev->popup.grab_link);
1963 }
1964 prev = shsurf;
1965 }
1966 wl_list_init(&prev->popup.grab_link);
1967 }
1968
1969 wl_list_remove(&shseat->seat_destroy_listener.link);
1970 free(shseat);
1971}
1972
1973static struct shell_seat *
1974create_shell_seat(struct weston_seat *seat)
1975{
1976 struct shell_seat *shseat;
1977
1978 shseat = calloc(1, sizeof *shseat);
1979 if (!shseat) {
1980 weston_log("no memory to allocate shell seat\n");
1981 return NULL;
1982 }
1983
1984 shseat->seat = seat;
1985 wl_list_init(&shseat->popup_grab.surfaces_list);
1986
1987 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1988 wl_signal_add(&seat->destroy_signal,
1989 &shseat->seat_destroy_listener);
1990
1991 return shseat;
1992}
1993
1994static struct shell_seat *
1995get_shell_seat(struct weston_seat *seat)
1996{
1997 struct wl_listener *listener;
1998
1999 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2000 if (listener == NULL)
2001 return create_shell_seat(seat);
2002
2003 return container_of(listener,
2004 struct shell_seat, seat_destroy_listener);
2005}
2006
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002007static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002008popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002009{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002010 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002011 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002012 struct shell_seat *shseat =
2013 container_of(grab, struct shell_seat, popup_grab.grab);
2014 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002015 wl_fixed_t sx, sy;
2016
2017 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2018 pointer->x, pointer->y,
2019 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002020
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002021 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002022 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002023 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002024 weston_pointer_set_focus(pointer, NULL,
2025 wl_fixed_from_int(0),
2026 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002027 }
2028}
2029
2030static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002031popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002032{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002033 struct weston_pointer *pointer = grab->pointer;
2034 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002035
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002036 if (pointer->focus_resource) {
2037 weston_surface_from_global_fixed(pointer->focus,
2038 pointer->x, pointer->y,
2039 &sx, &sy);
2040 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2041 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002042}
2043
2044static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002045popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002046 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002047{
2048 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002049 struct shell_seat *shseat =
2050 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002051 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002052 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002053 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002054
Daniel Stone37816df2012-05-16 18:45:18 +01002055 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002056 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002057 display = wl_client_get_display(resource->client);
2058 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002059 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002060 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002061 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002062 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002063 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002064 }
2065
Daniel Stone4dbadb12012-05-30 16:31:51 +01002066 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002067 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002068}
2069
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002070static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002071 popup_grab_focus,
2072 popup_grab_motion,
2073 popup_grab_button,
2074};
2075
2076static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002077popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002078{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002079 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002080 struct shell_seat *shseat =
2081 container_of(grab, struct shell_seat, popup_grab.grab);
2082 struct shell_surface *shsurf;
2083 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002084
2085 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002086 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002087 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002088 shseat->popup_grab.grab.interface = NULL;
2089 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002090 /* Send the popup_done event to all the popups open */
2091 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2092 wl_shell_surface_send_popup_done(&shsurf->resource);
2093 shsurf->popup.shseat = NULL;
2094 if (prev) {
2095 wl_list_init(&prev->popup.grab_link);
2096 }
2097 prev = shsurf;
2098 }
2099 wl_list_init(&prev->popup.grab_link);
2100 wl_list_init(&shseat->popup_grab.surfaces_list);
2101 }
2102}
2103
2104static void
2105add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2106{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002107 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002108
2109 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002110 shseat->popup_grab.client = shsurf->resource.client;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002111 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002112 /* We must make sure here that this popup was opened after
2113 * a mouse press, and not just by moving around with other
2114 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002115 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002116 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002117
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002118 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002119 }
2120 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2121}
2122
2123static void
2124remove_popup_grab(struct shell_surface *shsurf)
2125{
2126 struct shell_seat *shseat = shsurf->popup.shseat;
2127
2128 wl_list_remove(&shsurf->popup.grab_link);
2129 wl_list_init(&shsurf->popup.grab_link);
2130 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002131 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002132 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002133 }
2134}
2135
2136static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002137shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002138{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002139 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002140 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002141 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002142
2143 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002144
Pekka Paalanen483243f2013-03-08 14:56:50 +02002145 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002146 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2147 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002148
Kristian Høgsberge3148752013-05-06 23:19:49 -04002149 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002150 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002151 } else {
2152 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002153 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002154 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002155}
2156
2157static void
2158shell_surface_set_popup(struct wl_client *client,
2159 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002160 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002161 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002162 struct wl_resource *parent_resource,
2163 int32_t x, int32_t y, uint32_t flags)
2164{
2165 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002166
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002167 shsurf->type = SHELL_SURFACE_POPUP;
2168 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002169 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002170 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002171 shsurf->popup.x = x;
2172 shsurf->popup.y = y;
2173}
2174
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002175static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002176 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002177 shell_surface_move,
2178 shell_surface_resize,
2179 shell_surface_set_toplevel,
2180 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002181 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002182 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002183 shell_surface_set_maximized,
2184 shell_surface_set_title,
2185 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002186};
2187
2188static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002189destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002190{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002191 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2192 remove_popup_grab(shsurf);
2193 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002194
Alex Wubd3354b2012-04-17 17:20:49 +08002195 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002196 shell_surface_is_top_fullscreen(shsurf))
2197 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002198
Alex Wuaa08e2d2012-03-05 11:01:40 +08002199 if (shsurf->fullscreen.black_surface)
2200 weston_surface_destroy(shsurf->fullscreen.black_surface);
2201
Alex Wubd3354b2012-04-17 17:20:49 +08002202 /* As destroy_resource() use wl_list_for_each_safe(),
2203 * we can always remove the listener.
2204 */
2205 wl_list_remove(&shsurf->surface_destroy_listener.link);
2206 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002207 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002208 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002209
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002210 wl_list_remove(&shsurf->link);
2211 free(shsurf);
2212}
2213
2214static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002215shell_destroy_shell_surface(struct wl_resource *resource)
2216{
2217 struct shell_surface *shsurf = resource->data;
2218
2219 destroy_shell_surface(shsurf);
2220}
2221
2222static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002223shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002224{
2225 struct shell_surface *shsurf = container_of(listener,
2226 struct shell_surface,
2227 surface_destroy_listener);
2228
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002229 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002230 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002231 } else {
2232 wl_signal_emit(&shsurf->resource.destroy_signal,
2233 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002234 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002235 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002236}
2237
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002238static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002239shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002240
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002241static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002242get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002243{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002244 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002245 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002246 else
2247 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002248}
2249
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002250static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002251create_shell_surface(void *shell, struct weston_surface *surface,
2252 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002253{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002254 struct shell_surface *shsurf;
2255
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002256 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002257 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002258 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002259 }
2260
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002261 shsurf = calloc(1, sizeof *shsurf);
2262 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002263 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002264 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002265 }
2266
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002267 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002268 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002269
Tiago Vignattibc052c92012-04-19 16:18:18 +03002270 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002271 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002272 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002273 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002274 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002275 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2276 shsurf->fullscreen.framerate = 0;
2277 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002278 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002279 wl_list_init(&shsurf->fullscreen.transform.link);
2280
Tiago Vignattibc052c92012-04-19 16:18:18 +03002281 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002282 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002283 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002284 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002285
2286 /* init link so its safe to always remove it in destroy_shell_surface */
2287 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002288 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002289
Pekka Paalanen460099f2012-01-20 16:48:25 +02002290 /* empty when not in use */
2291 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002292 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002293
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002294 wl_list_init(&shsurf->workspace_transform.link);
2295
Pekka Paalanen98262232011-12-01 10:42:22 +02002296 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002297 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002298
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002299 shsurf->client = client;
2300
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002301 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002302}
2303
2304static void
2305shell_get_shell_surface(struct wl_client *client,
2306 struct wl_resource *resource,
2307 uint32_t id,
2308 struct wl_resource *surface_resource)
2309{
2310 struct weston_surface *surface = surface_resource->data;
2311 struct desktop_shell *shell = resource->data;
2312 struct shell_surface *shsurf;
2313
2314 if (get_shell_surface(surface)) {
2315 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002316 WL_DISPLAY_ERROR_INVALID_OBJECT,
2317 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002318 return;
2319 }
2320
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002321 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002322 if (!shsurf) {
2323 wl_resource_post_error(surface_resource,
2324 WL_DISPLAY_ERROR_INVALID_OBJECT,
2325 "surface->configure already set");
2326 return;
2327 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002328
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002329 shsurf->resource.destroy = shell_destroy_shell_surface;
2330 shsurf->resource.object.id = id;
2331 shsurf->resource.object.interface = &wl_shell_surface_interface;
2332 shsurf->resource.object.implementation =
2333 (void (**)(void)) &shell_surface_implementation;
2334 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002335
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002336 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002337}
2338
2339static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002340 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002341};
2342
Kristian Høgsberg07937562011-04-12 17:25:42 -04002343static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002344shell_fade(struct desktop_shell *shell, enum fade_type type);
2345
2346static int
2347screensaver_timeout(void *data)
2348{
2349 struct desktop_shell *shell = data;
2350
2351 shell_fade(shell, FADE_OUT);
2352
2353 return 1;
2354}
2355
2356static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002357handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002358{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002359 struct desktop_shell *shell =
2360 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002361
Pekka Paalanen18027e52011-12-02 16:31:49 +02002362 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002363
2364 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002365 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002366}
2367
2368static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002369launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002370{
2371 if (shell->screensaver.binding)
2372 return;
2373
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002374 if (!shell->screensaver.path) {
2375 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002376 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002377 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002378
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002379 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002380 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002381 return;
2382 }
2383
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002384 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002385 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002386 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002387 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002388}
2389
2390static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002391terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002392{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002393 if (shell->screensaver.process.pid == 0)
2394 return;
2395
2396 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002397}
2398
2399static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002400configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002401{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002402 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002403
Giulio Camuffo184df502013-02-21 11:29:21 +01002404 if (width == 0)
2405 return;
2406
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002407 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2408 if (s->output == es->output && s != es) {
2409 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002410 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002411 }
2412 }
2413
Giulio Camuffo184df502013-02-21 11:29:21 +01002414 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002415
2416 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002417 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002418 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002419 }
2420}
2421
2422static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002423background_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 -04002424{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002425 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002426
Giulio Camuffo184df502013-02-21 11:29:21 +01002427 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002428}
2429
2430static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002431desktop_shell_set_background(struct wl_client *client,
2432 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002433 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002434 struct wl_resource *surface_resource)
2435{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002436 struct desktop_shell *shell = resource->data;
2437 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002438
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002439 if (surface->configure) {
2440 wl_resource_post_error(surface_resource,
2441 WL_DISPLAY_ERROR_INVALID_OBJECT,
2442 "surface role already assigned");
2443 return;
2444 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002445
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002446 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002447 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002448 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002449 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002450 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002451 surface->output->width,
2452 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002453}
2454
2455static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002456panel_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 -04002457{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002458 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002459
Giulio Camuffo184df502013-02-21 11:29:21 +01002460 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002461}
2462
2463static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002464desktop_shell_set_panel(struct wl_client *client,
2465 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002466 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002467 struct wl_resource *surface_resource)
2468{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002469 struct desktop_shell *shell = resource->data;
2470 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002471
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002472 if (surface->configure) {
2473 wl_resource_post_error(surface_resource,
2474 WL_DISPLAY_ERROR_INVALID_OBJECT,
2475 "surface role already assigned");
2476 return;
2477 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002478
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002479 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002480 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002481 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002482 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002483 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002484 surface->output->width,
2485 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002486}
2487
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002488static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002489lock_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 -04002490{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002491 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002492
Giulio Camuffo184df502013-02-21 11:29:21 +01002493 if (width == 0)
2494 return;
2495
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002496 surface->geometry.width = width;
2497 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002498 center_on_output(surface, get_default_output(shell->compositor));
2499
2500 if (!weston_surface_is_mapped(surface)) {
2501 wl_list_insert(&shell->lock_layer.surface_list,
2502 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002503 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002504 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002505 }
2506}
2507
2508static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002509handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002510{
Tiago Vignattibe143262012-04-16 17:31:41 +03002511 struct desktop_shell *shell =
2512 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002513
Martin Minarik6d118362012-06-07 18:01:59 +02002514 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002515 shell->lock_surface = NULL;
2516}
2517
2518static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002519desktop_shell_set_lock_surface(struct wl_client *client,
2520 struct wl_resource *resource,
2521 struct wl_resource *surface_resource)
2522{
Tiago Vignattibe143262012-04-16 17:31:41 +03002523 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002524 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002525
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002526 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002527
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002528 if (!shell->locked)
2529 return;
2530
Pekka Paalanen98262232011-12-01 10:42:22 +02002531 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002532
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002533 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2534 wl_signal_add(&surface_resource->destroy_signal,
2535 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002536
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002537 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002538 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002539}
2540
2541static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002542resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002543{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002544 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002545
Pekka Paalanen77346a62011-11-30 16:26:35 +02002546 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002547
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002548 wl_list_remove(&shell->lock_layer.link);
2549 wl_list_insert(&shell->compositor->cursor_layer.link,
2550 &shell->fullscreen_layer.link);
2551 wl_list_insert(&shell->fullscreen_layer.link,
2552 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002553 if (shell->showing_input_panels) {
2554 wl_list_insert(&shell->panel_layer.link,
2555 &shell->input_panel_layer.link);
2556 wl_list_insert(&shell->input_panel_layer.link,
2557 &ws->layer.link);
2558 } else {
2559 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2560 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002561
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002562 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002563
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002564 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002565 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002566 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002567}
2568
2569static void
2570desktop_shell_unlock(struct wl_client *client,
2571 struct wl_resource *resource)
2572{
Tiago Vignattibe143262012-04-16 17:31:41 +03002573 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002574
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002575 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002576
2577 if (shell->locked)
2578 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002579}
2580
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002581static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002582desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002583 struct wl_resource *resource,
2584 struct wl_resource *surface_resource)
2585{
2586 struct desktop_shell *shell = resource->data;
2587
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002588 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002589}
2590
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002591static void
2592desktop_shell_desktop_ready(struct wl_client *client,
2593 struct wl_resource *resource)
2594{
2595 struct desktop_shell *shell = resource->data;
2596
2597 shell_fade_startup(shell);
2598}
2599
Kristian Høgsberg75840622011-09-06 13:48:16 -04002600static const struct desktop_shell_interface desktop_shell_implementation = {
2601 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002602 desktop_shell_set_panel,
2603 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002604 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002605 desktop_shell_set_grab_surface,
2606 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002607};
2608
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002609static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002610get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002611{
2612 struct shell_surface *shsurf;
2613
2614 shsurf = get_shell_surface(surface);
2615 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002616 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002617 return shsurf->type;
2618}
2619
Kristian Høgsberg75840622011-09-06 13:48:16 -04002620static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002621move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002622{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002623 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002624 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002625 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002626 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002627
Pekka Paalanen01388e22013-04-25 13:57:44 +03002628 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002629 if (surface == NULL)
2630 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002631
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002632 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002633 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2634 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002635 return;
2636
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002637 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002638}
2639
2640static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002641resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002642{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002643 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002644 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002645 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002646 uint32_t edges = 0;
2647 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002648 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002649
Pekka Paalanen01388e22013-04-25 13:57:44 +03002650 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002651 if (surface == NULL)
2652 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002653
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002654 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002655 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2656 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002657 return;
2658
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002659 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002660 wl_fixed_to_int(seat->pointer->grab_x),
2661 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002662 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002663
Pekka Paalanen60921e52012-01-25 15:55:43 +02002664 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002665 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002666 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002667 edges |= 0;
2668 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002669 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002670
Pekka Paalanen60921e52012-01-25 15:55:43 +02002671 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002672 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002673 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002674 edges |= 0;
2675 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002676 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002677
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002678 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002679}
2680
2681static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002682surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002683 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002684{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002685 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002686 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002687 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002688 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002689 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002690
Pekka Paalanen01388e22013-04-25 13:57:44 +03002691 /* XXX: broken for windows containing sub-surfaces */
2692 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002693 if (surface == NULL)
2694 return;
2695
2696 shsurf = get_shell_surface(surface);
2697 if (!shsurf)
2698 return;
2699
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002700 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002701
Scott Moreau02709af2012-05-22 01:54:10 -06002702 if (surface->alpha > 1.0)
2703 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002704 if (surface->alpha < step)
2705 surface->alpha = step;
2706
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002707 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002708 weston_surface_damage(surface);
2709}
2710
2711static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002712do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002713 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002714{
Daniel Stone37816df2012-05-16 18:45:18 +01002715 struct weston_seat *ws = (struct weston_seat *) seat;
2716 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002717 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002718 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002719
2720 wl_list_for_each(output, &compositor->output_list, link) {
2721 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002722 wl_fixed_to_double(seat->pointer->x),
2723 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002724 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002725 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002726 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002727 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002728 increment = -output->zoom.increment;
2729 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002730 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002731 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002732 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002733 else
2734 increment = 0;
2735
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002736 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002737
Scott Moreaue6603982012-06-11 13:07:51 -06002738 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002739 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002740 else if (output->zoom.level > output->zoom.max_level)
2741 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002742 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002743 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002744 output->disable_planes++;
2745 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002746
Scott Moreaue6603982012-06-11 13:07:51 -06002747 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002748
Scott Moreau8dacaab2012-06-17 18:10:58 -06002749 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002750 }
2751 }
2752}
2753
Scott Moreauccbf29d2012-02-22 14:21:41 -07002754static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002755zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002756 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002757{
2758 do_zoom(seat, time, 0, axis, value);
2759}
2760
2761static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002762zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002763 void *data)
2764{
2765 do_zoom(seat, time, key, 0, 0);
2766}
2767
2768static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002769terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002770 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002771{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002772 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002773
Daniel Stone325fc2d2012-05-30 16:31:58 +01002774 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002775}
2776
2777static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002778rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002779{
2780 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002781 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002782 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002783 struct shell_surface *shsurf = rotate->base.shsurf;
2784 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002785 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002786
2787 if (!shsurf)
2788 return;
2789
2790 surface = shsurf->surface;
2791
2792 cx = 0.5f * surface->geometry.width;
2793 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002794
Daniel Stone37816df2012-05-16 18:45:18 +01002795 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2796 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002797 r = sqrtf(dx * dx + dy * dy);
2798
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002799 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002800 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002801
2802 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002803 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002804 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002805
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002806 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002807 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002808
2809 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002810 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002811 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002812 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002813 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002814
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002815 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002816 &shsurf->surface->geometry.transformation_list,
2817 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002818 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002819 wl_list_init(&shsurf->rotation.transform.link);
2820 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002821 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002822 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002823
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002824 /* We need to adjust the position of the surface
2825 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002826 cposx = surface->geometry.x + cx;
2827 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002828 dposx = rotate->center.x - cposx;
2829 dposy = rotate->center.y - cposy;
2830 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002831 weston_surface_set_position(surface,
2832 surface->geometry.x + dposx,
2833 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002834 }
2835
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002836 /* Repaint implies weston_surface_update_transform(), which
2837 * lazily applies the damage due to rotation update.
2838 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002839 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002840}
2841
2842static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002843rotate_grab_button(struct weston_pointer_grab *grab,
2844 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002845{
2846 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002847 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002848 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002849 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002850 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002851
Daniel Stone4dbadb12012-05-30 16:31:51 +01002852 if (pointer->button_count == 0 &&
2853 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002854 if (shsurf)
2855 weston_matrix_multiply(&shsurf->rotation.rotation,
2856 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002857 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002858 free(rotate);
2859 }
2860}
2861
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002862static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002863 noop_grab_focus,
2864 rotate_grab_motion,
2865 rotate_grab_button,
2866};
2867
2868static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002869surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002870{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002871 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002872 float dx, dy;
2873 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002874
Pekka Paalanen460099f2012-01-20 16:48:25 +02002875 rotate = malloc(sizeof *rotate);
2876 if (!rotate)
2877 return;
2878
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002879 weston_surface_to_global_float(surface->surface,
2880 surface->surface->geometry.width / 2,
2881 surface->surface->geometry.height / 2,
2882 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002883
Daniel Stone37816df2012-05-16 18:45:18 +01002884 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2885 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002886 r = sqrtf(dx * dx + dy * dy);
2887 if (r > 20.0f) {
2888 struct weston_matrix inverse;
2889
2890 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002891 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002892 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002893
2894 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002895 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002896 } else {
2897 weston_matrix_init(&surface->rotation.rotation);
2898 weston_matrix_init(&rotate->rotation);
2899 }
2900
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002901 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2902 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002903}
2904
2905static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002906rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002907 void *data)
2908{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002909 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002910 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002911 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002912 struct shell_surface *surface;
2913
Pekka Paalanen01388e22013-04-25 13:57:44 +03002914 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002915 if (base_surface == NULL)
2916 return;
2917
2918 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002919 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2920 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002921 return;
2922
2923 surface_rotate(surface, seat);
2924}
2925
2926static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002927lower_fullscreen_layer(struct desktop_shell *shell)
2928{
2929 struct workspace *ws;
2930 struct weston_surface *surface, *prev;
2931
2932 ws = get_current_workspace(shell);
2933 wl_list_for_each_reverse_safe(surface, prev,
2934 &shell->fullscreen_layer.surface_list,
2935 layer_link)
2936 weston_surface_restack(surface, &ws->layer.surface_list);
2937}
2938
2939static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002940activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002941 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002942{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002943 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002944 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002945 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002946
Pekka Paalanen01388e22013-04-25 13:57:44 +03002947 main_surface = weston_surface_get_main_surface(es);
2948
Daniel Stone37816df2012-05-16 18:45:18 +01002949 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002950
Jonas Ådahl8538b222012-08-29 22:13:03 +02002951 state = ensure_focus_state(shell, seat);
2952 if (state == NULL)
2953 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002954
2955 state->keyboard_focus = es;
2956 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002957 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002958
Pekka Paalanen01388e22013-04-25 13:57:44 +03002959 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002960 case SHELL_SURFACE_FULLSCREEN:
2961 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002962 shell_stack_fullscreen(get_shell_surface(main_surface));
2963 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002964 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002965 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002966 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002967 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002968 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002969 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002970 }
2971}
2972
Alex Wu21858432012-04-01 20:13:08 +08002973/* no-op func for checking black surface */
2974static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002975black_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 +08002976{
2977}
2978
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002979static bool
Alex Wu21858432012-04-01 20:13:08 +08002980is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2981{
2982 if (es->configure == black_surface_configure) {
2983 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002984 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002985 return true;
2986 }
2987 return false;
2988}
2989
Kristian Høgsberg75840622011-09-06 13:48:16 -04002990static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002991click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002992 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002993{
Daniel Stone37816df2012-05-16 18:45:18 +01002994 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002995 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002996 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002997 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002998
Daniel Stone37816df2012-05-16 18:45:18 +01002999 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003000 if (!focus)
3001 return;
3002
Pekka Paalanen01388e22013-04-25 13:57:44 +03003003 if (is_black_surface(focus, &main_surface))
3004 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003005
Pekka Paalanen01388e22013-04-25 13:57:44 +03003006 main_surface = weston_surface_get_main_surface(focus);
3007 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003008 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003009
Daniel Stone325fc2d2012-05-30 16:31:58 +01003010 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003011 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003012}
3013
3014static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003015lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003016{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003017 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003018
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003019 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003020 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003021 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003022 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003023
3024 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003025
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003026 /* Hide all surfaces by removing the fullscreen, panel and
3027 * toplevel layers. This way nothing else can show or receive
3028 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003029
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003030 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003031 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003032 if (shell->showing_input_panels)
3033 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003034 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003035 wl_list_insert(&shell->compositor->cursor_layer.link,
3036 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003037
Pekka Paalanen77346a62011-11-30 16:26:35 +02003038 launch_screensaver(shell);
3039
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003040 /* TODO: disable bindings that should not work while locked. */
3041
3042 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003043}
3044
3045static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003046unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003047{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003048 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003049 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003050 return;
3051 }
3052
3053 /* If desktop-shell client has gone away, unlock immediately. */
3054 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003055 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003056 return;
3057 }
3058
3059 if (shell->prepare_event_sent)
3060 return;
3061
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003062 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003063 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003064}
3065
3066static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003067shell_fade_done(struct weston_surface_animation *animation, void *data)
3068{
3069 struct desktop_shell *shell = data;
3070
3071 shell->fade.animation = NULL;
3072
3073 switch (shell->fade.type) {
3074 case FADE_IN:
3075 weston_surface_destroy(shell->fade.surface);
3076 shell->fade.surface = NULL;
3077 break;
3078 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003079 lock(shell);
3080 break;
3081 }
3082}
3083
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003084static struct weston_surface *
3085shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003086{
3087 struct weston_compositor *compositor = shell->compositor;
3088 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003089
3090 surface = weston_surface_create(compositor);
3091 if (!surface)
3092 return NULL;
3093
3094 weston_surface_configure(surface, 0, 0, 8192, 8192);
3095 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3096 wl_list_insert(&compositor->fade_layer.surface_list,
3097 &surface->layer_link);
3098 pixman_region32_init(&surface->input);
3099
3100 return surface;
3101}
3102
3103static void
3104shell_fade(struct desktop_shell *shell, enum fade_type type)
3105{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003106 float tint;
3107
3108 switch (type) {
3109 case FADE_IN:
3110 tint = 0.0;
3111 break;
3112 case FADE_OUT:
3113 tint = 1.0;
3114 break;
3115 default:
3116 weston_log("shell: invalid fade type\n");
3117 return;
3118 }
3119
3120 shell->fade.type = type;
3121
3122 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003123 shell->fade.surface = shell_fade_create_surface(shell);
3124 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003125 return;
3126
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003127 shell->fade.surface->alpha = 1.0 - tint;
3128 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003129 }
3130
3131 if (shell->fade.animation)
3132 weston_fade_update(shell->fade.animation,
3133 shell->fade.surface->alpha, tint, 30.0);
3134 else
3135 shell->fade.animation =
3136 weston_fade_run(shell->fade.surface,
3137 1.0 - tint, tint, 30.0,
3138 shell_fade_done, shell);
3139}
3140
3141static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003142do_shell_fade_startup(void *data)
3143{
3144 struct desktop_shell *shell = data;
3145
3146 shell_fade(shell, FADE_IN);
3147}
3148
3149static void
3150shell_fade_startup(struct desktop_shell *shell)
3151{
3152 struct wl_event_loop *loop;
3153
3154 if (!shell->fade.startup_timer)
3155 return;
3156
3157 wl_event_source_remove(shell->fade.startup_timer);
3158 shell->fade.startup_timer = NULL;
3159
3160 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3161 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3162}
3163
3164static int
3165fade_startup_timeout(void *data)
3166{
3167 struct desktop_shell *shell = data;
3168
3169 shell_fade_startup(shell);
3170 return 0;
3171}
3172
3173static void
3174shell_fade_init(struct desktop_shell *shell)
3175{
3176 /* Make compositor output all black, and wait for the desktop-shell
3177 * client to signal it is ready, then fade in. The timer triggers a
3178 * fade-in, in case the desktop-shell client takes too long.
3179 */
3180
3181 struct wl_event_loop *loop;
3182
3183 if (shell->fade.surface != NULL) {
3184 weston_log("%s: warning: fade surface already exists\n",
3185 __func__);
3186 return;
3187 }
3188
3189 shell->fade.surface = shell_fade_create_surface(shell);
3190 if (!shell->fade.surface)
3191 return;
3192
3193 weston_surface_update_transform(shell->fade.surface);
3194 weston_surface_damage(shell->fade.surface);
3195
3196 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3197 shell->fade.startup_timer =
3198 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3199 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3200}
3201
3202static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003203idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003204{
3205 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003206 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003207
3208 shell_fade(shell, FADE_OUT);
3209 /* lock() is called from shell_fade_done() */
3210}
3211
3212static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003213wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003214{
3215 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003216 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003217
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003218 unlock(shell);
3219}
3220
3221static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003222show_input_panels(struct wl_listener *listener, void *data)
3223{
3224 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003225 container_of(listener, struct desktop_shell,
3226 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003227 struct input_panel_surface *surface, *next;
3228 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003229
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003230 shell->text_input.surface = (struct weston_surface*)data;
3231
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003232 if (shell->showing_input_panels)
3233 return;
3234
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003235 shell->showing_input_panels = true;
3236
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003237 if (!shell->locked)
3238 wl_list_insert(&shell->panel_layer.link,
3239 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003240
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003241 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003242 &shell->input_panel.surfaces, link) {
3243 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003244 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003245 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003246 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003247 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003248 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003249 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003250 weston_surface_damage(ws);
3251 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003252 }
3253}
3254
3255static void
3256hide_input_panels(struct wl_listener *listener, void *data)
3257{
3258 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003259 container_of(listener, struct desktop_shell,
3260 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003261 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003262
Jan Arne Petersen61381972013-01-31 15:52:21 +01003263 if (!shell->showing_input_panels)
3264 return;
3265
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003266 shell->showing_input_panels = false;
3267
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003268 if (!shell->locked)
3269 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003270
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003271 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003272 &shell->input_panel_layer.surface_list, layer_link)
3273 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003274}
3275
3276static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003277update_input_panels(struct wl_listener *listener, void *data)
3278{
3279 struct desktop_shell *shell =
3280 container_of(listener, struct desktop_shell,
3281 update_input_panel_listener);
3282
3283 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3284}
3285
3286static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003287center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003288{
Giulio Camuffob8366642013-04-25 13:57:46 +03003289 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003290 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003291
Giulio Camuffob8366642013-04-25 13:57:46 +03003292 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3293
3294 x = output->x + (output->width - width) / 2 - surf_x / 2;
3295 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003296
3297 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003298}
3299
3300static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003301weston_surface_set_initial_position (struct weston_surface *surface,
3302 struct desktop_shell *shell)
3303{
3304 struct weston_compositor *compositor = shell->compositor;
3305 int ix = 0, iy = 0;
3306 int range_x, range_y;
3307 int dx, dy, x, y, panel_height;
3308 struct weston_output *output, *target_output = NULL;
3309 struct weston_seat *seat;
3310
3311 /* As a heuristic place the new window on the same output as the
3312 * pointer. Falling back to the output containing 0, 0.
3313 *
3314 * TODO: Do something clever for touch too?
3315 */
3316 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003317 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003318 ix = wl_fixed_to_int(seat->pointer->x);
3319 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003320 break;
3321 }
3322 }
3323
3324 wl_list_for_each(output, &compositor->output_list, link) {
3325 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3326 target_output = output;
3327 break;
3328 }
3329 }
3330
3331 if (!target_output) {
3332 weston_surface_set_position(surface, 10 + random() % 400,
3333 10 + random() % 400);
3334 return;
3335 }
3336
3337 /* Valid range within output where the surface will still be onscreen.
3338 * If this is negative it means that the surface is bigger than
3339 * output.
3340 */
3341 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003342 range_x = target_output->width - surface->geometry.width;
3343 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003344 surface->geometry.height;
3345
Rob Bradford4cb88c72012-08-13 15:18:44 +01003346 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003347 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003348 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003349 dx = 0;
3350
3351 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003352 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003353 else
3354 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003355
3356 x = target_output->x + dx;
3357 y = target_output->y + dy;
3358
3359 weston_surface_set_position (surface, x, y);
3360}
3361
3362static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003363map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003364 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003365{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003366 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003367 struct shell_surface *shsurf = get_shell_surface(surface);
3368 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003369 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003370 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003371 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003372 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003373 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003374
Pekka Paalanen60921e52012-01-25 15:55:43 +02003375 surface->geometry.width = width;
3376 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003377 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003378
3379 /* initial positioning, see also configure() */
3380 switch (surface_type) {
3381 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003382 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003383 break;
Alex Wu4539b082012-03-01 12:57:46 +08003384 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003385 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003386 shell_map_fullscreen(shsurf);
3387 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003388 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003389 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003390 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003391 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3392 NULL, NULL);
3393 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3394 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003395 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003396 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003397 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003398 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003399 case SHELL_SURFACE_NONE:
3400 weston_surface_set_position(surface,
3401 surface->geometry.x + sx,
3402 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003403 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003404 default:
3405 ;
3406 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003407
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003408 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003409 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003410 case SHELL_SURFACE_POPUP:
3411 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003412 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003413 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3414 break;
Alex Wu4539b082012-03-01 12:57:46 +08003415 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003416 case SHELL_SURFACE_NONE:
3417 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003418 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003419 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003420 ws = get_current_workspace(shell);
3421 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003422 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003423 }
3424
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003425 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003426 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003427 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3428 surface->output = shsurf->output;
3429 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003430
Juan Zhao7bb92f02011-12-15 11:31:51 -05003431 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003432 /* XXX: xwayland's using the same fields for transient type */
3433 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003434 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003435 if (shsurf->transient.flags ==
3436 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3437 break;
3438 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003439 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003440 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003441 if (!shell->locked) {
3442 wl_list_for_each(seat, &compositor->seat_list, link)
3443 activate(shell, surface, seat);
3444 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003445 break;
3446 default:
3447 break;
3448 }
3449
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003450 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003451 {
3452 switch (shell->win_animation_type) {
3453 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003454 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003455 break;
3456 case ANIMATION_ZOOM:
3457 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3458 break;
3459 default:
3460 break;
3461 }
3462 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003463}
3464
3465static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003466configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003467 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003468{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003469 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3470 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003471 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003472
Pekka Paalanen77346a62011-11-30 16:26:35 +02003473 shsurf = get_shell_surface(surface);
3474 if (shsurf)
3475 surface_type = shsurf->type;
3476
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003477 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003478
3479 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003480 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003481 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003482 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003483 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003484 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003485 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003486 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3487 NULL, NULL);
3488 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003489 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003490 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003491 break;
Alex Wu4539b082012-03-01 12:57:46 +08003492 case SHELL_SURFACE_TOPLEVEL:
3493 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003494 default:
3495 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003496 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003497
Alex Wu4539b082012-03-01 12:57:46 +08003498 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003499 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003500 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003501
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003502 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003503 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003504 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003505}
3506
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003507static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003508shell_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 +03003509{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003510 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003511 struct desktop_shell *shell = shsurf->shell;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003512 struct weston_compositor *compositor = shsurf->surface->compositor;
Giulio Camuffo184df502013-02-21 11:29:21 +01003513
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003514 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003515
Giulio Camuffo5085a752013-03-25 21:42:45 +01003516 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3517 remove_popup_grab(shsurf);
3518 }
3519
Giulio Camuffo184df502013-02-21 11:29:21 +01003520 if (width == 0)
3521 return;
3522
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003523 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003524 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003525 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003526 type_changed = 1;
3527 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003528
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003529 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003530 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003531 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003532 es->geometry.width != width ||
3533 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003534 float from_x, from_y;
3535 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003536
3537 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3538 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3539 configure(shell, es,
3540 es->geometry.x + to_x - from_x,
3541 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003542 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003543 }
3544}
3545
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003546static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003547
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003548static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003549desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003550{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003551 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003552 struct desktop_shell *shell =
3553 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003554
3555 shell->child.process.pid = 0;
3556 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003557
3558 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3559 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003560 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003561 shell->child.deathstamp = time;
3562 shell->child.deathcount = 0;
3563 }
3564
3565 shell->child.deathcount++;
3566 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003567 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003568 return;
3569 }
3570
Martin Minarik6d118362012-06-07 18:01:59 +02003571 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003572 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003573 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003574}
3575
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003576static void
3577launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003578{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003579 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003580 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003581
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003582 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003583 &shell->child.process,
3584 shell_exe,
3585 desktop_shell_sigchld);
3586
3587 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003588 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003589}
3590
3591static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003592bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3593{
Tiago Vignattibe143262012-04-16 17:31:41 +03003594 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003595
3596 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003597 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003598}
3599
Kristian Høgsberg75840622011-09-06 13:48:16 -04003600static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003601unbind_desktop_shell(struct wl_resource *resource)
3602{
Tiago Vignattibe143262012-04-16 17:31:41 +03003603 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003604
3605 if (shell->locked)
3606 resume_desktop(shell);
3607
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003608 shell->child.desktop_shell = NULL;
3609 shell->prepare_event_sent = false;
3610 free(resource);
3611}
3612
3613static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003614bind_desktop_shell(struct wl_client *client,
3615 void *data, uint32_t version, uint32_t id)
3616{
Tiago Vignattibe143262012-04-16 17:31:41 +03003617 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003618 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003619
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003620 resource = wl_client_add_object(client, &desktop_shell_interface,
3621 &desktop_shell_implementation,
3622 id, shell);
3623
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003624 if (client == shell->child.client) {
3625 resource->destroy = unbind_desktop_shell;
3626 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003627
3628 if (version < 2)
3629 shell_fade_startup(shell);
3630
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003631 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003632 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003633
3634 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3635 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003636 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003637}
3638
Pekka Paalanen6e168112011-11-24 11:34:05 +02003639static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003640screensaver_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 -04003641{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003642 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003643
Giulio Camuffo184df502013-02-21 11:29:21 +01003644 if (width == 0)
3645 return;
3646
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003647 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003648 if (!shell->locked)
3649 return;
3650
3651 center_on_output(surface, surface->output);
3652
3653 if (wl_list_empty(&surface->layer_link)) {
3654 wl_list_insert(shell->lock_layer.surface_list.prev,
3655 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003656 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003657 wl_event_source_timer_update(shell->screensaver.timer,
3658 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003659 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003660 }
3661}
3662
3663static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003664screensaver_set_surface(struct wl_client *client,
3665 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003666 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003667 struct wl_resource *output_resource)
3668{
Tiago Vignattibe143262012-04-16 17:31:41 +03003669 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003670 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003671 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003672
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003673 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003674 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003675 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003676}
3677
3678static const struct screensaver_interface screensaver_implementation = {
3679 screensaver_set_surface
3680};
3681
3682static void
3683unbind_screensaver(struct wl_resource *resource)
3684{
Tiago Vignattibe143262012-04-16 17:31:41 +03003685 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003686
Pekka Paalanen77346a62011-11-30 16:26:35 +02003687 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003688 free(resource);
3689}
3690
3691static void
3692bind_screensaver(struct wl_client *client,
3693 void *data, uint32_t version, uint32_t id)
3694{
Tiago Vignattibe143262012-04-16 17:31:41 +03003695 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003696 struct wl_resource *resource;
3697
3698 resource = wl_client_add_object(client, &screensaver_interface,
3699 &screensaver_implementation,
3700 id, shell);
3701
Pekka Paalanen77346a62011-11-30 16:26:35 +02003702 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003703 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003704 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003705 return;
3706 }
3707
3708 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3709 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003710 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003711}
3712
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003713static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003714input_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 -04003715{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003716 struct input_panel_surface *ip_surface = surface->configure_private;
3717 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003718 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003719 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003720 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003721
Giulio Camuffo184df502013-02-21 11:29:21 +01003722 if (width == 0)
3723 return;
3724
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003725 if (!weston_surface_is_mapped(surface)) {
3726 if (!shell->showing_input_panels)
3727 return;
3728
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003729 show_surface = 1;
3730 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003731
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003732 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003733
3734 if (ip_surface->panel) {
3735 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3736 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3737 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003738 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003739
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003740 x = ip_surface->output->x + (mode->width - width) / 2;
3741 y = ip_surface->output->y + mode->height - height;
3742 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003743
3744 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003745 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003746 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003747
3748 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003749 wl_list_insert(&shell->input_panel_layer.surface_list,
3750 &surface->layer_link);
3751 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003752 weston_surface_damage(surface);
3753 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3754 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003755}
3756
3757static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003758destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003759{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003760 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003761 wl_list_remove(&input_panel_surface->link);
3762
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003763 input_panel_surface->surface->configure = NULL;
3764
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003765 free(input_panel_surface);
3766}
3767
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003768static struct input_panel_surface *
3769get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003770{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003771 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003772 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003773 } else {
3774 return NULL;
3775 }
3776}
3777
3778static void
3779input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3780{
3781 struct input_panel_surface *ipsurface = container_of(listener,
3782 struct input_panel_surface,
3783 surface_destroy_listener);
3784
3785 if (ipsurface->resource.client) {
3786 wl_resource_destroy(&ipsurface->resource);
3787 } else {
3788 wl_signal_emit(&ipsurface->resource.destroy_signal,
3789 &ipsurface->resource);
3790 destroy_input_panel_surface(ipsurface);
3791 }
3792}
3793static struct input_panel_surface *
3794create_input_panel_surface(struct desktop_shell *shell,
3795 struct weston_surface *surface)
3796{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003797 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003798
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003799 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3800 if (!input_panel_surface)
3801 return NULL;
3802
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003803 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003804 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003805
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003806 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003807
3808 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003809
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003810 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3811 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003812 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003813 &input_panel_surface->surface_destroy_listener);
3814
3815 wl_list_init(&input_panel_surface->link);
3816
3817 return input_panel_surface;
3818}
3819
3820static void
3821input_panel_surface_set_toplevel(struct wl_client *client,
3822 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003823 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003824 uint32_t position)
3825{
3826 struct input_panel_surface *input_panel_surface = resource->data;
3827 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003828
3829 wl_list_insert(&shell->input_panel.surfaces,
3830 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003831
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003832 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003833 input_panel_surface->panel = 0;
3834}
3835
3836static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003837input_panel_surface_set_overlay_panel(struct wl_client *client,
3838 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003839{
3840 struct input_panel_surface *input_panel_surface = resource->data;
3841 struct desktop_shell *shell = input_panel_surface->shell;
3842
3843 wl_list_insert(&shell->input_panel.surfaces,
3844 &input_panel_surface->link);
3845
3846 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003847}
3848
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003849static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003850 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003851 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003852};
3853
3854static void
3855destroy_input_panel_surface_resource(struct wl_resource *resource)
3856{
3857 struct input_panel_surface *ipsurf = resource->data;
3858
3859 destroy_input_panel_surface(ipsurf);
3860}
3861
3862static void
3863input_panel_get_input_panel_surface(struct wl_client *client,
3864 struct wl_resource *resource,
3865 uint32_t id,
3866 struct wl_resource *surface_resource)
3867{
3868 struct weston_surface *surface = surface_resource->data;
3869 struct desktop_shell *shell = resource->data;
3870 struct input_panel_surface *ipsurf;
3871
3872 if (get_input_panel_surface(surface)) {
3873 wl_resource_post_error(surface_resource,
3874 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003875 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003876 return;
3877 }
3878
3879 ipsurf = create_input_panel_surface(shell, surface);
3880 if (!ipsurf) {
3881 wl_resource_post_error(surface_resource,
3882 WL_DISPLAY_ERROR_INVALID_OBJECT,
3883 "surface->configure already set");
3884 return;
3885 }
3886
3887 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3888 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003889 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003890 ipsurf->resource.object.implementation =
3891 (void (**)(void)) &input_panel_surface_implementation;
3892 ipsurf->resource.data = ipsurf;
3893
3894 wl_client_add_resource(client, &ipsurf->resource);
3895}
3896
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003897static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003898 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003899};
3900
3901static void
3902unbind_input_panel(struct wl_resource *resource)
3903{
3904 struct desktop_shell *shell = resource->data;
3905
3906 shell->input_panel.binding = NULL;
3907 free(resource);
3908}
3909
3910static void
3911bind_input_panel(struct wl_client *client,
3912 void *data, uint32_t version, uint32_t id)
3913{
3914 struct desktop_shell *shell = data;
3915 struct wl_resource *resource;
3916
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003917 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003918 &input_panel_implementation,
3919 id, shell);
3920
3921 if (shell->input_panel.binding == NULL) {
3922 resource->destroy = unbind_input_panel;
3923 shell->input_panel.binding = resource;
3924 return;
3925 }
3926
3927 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3928 "interface object already bound");
3929 wl_resource_destroy(resource);
3930}
3931
Kristian Høgsberg07045392012-02-19 18:52:44 -05003932struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003933 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003934 struct weston_surface *current;
3935 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003936 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003937};
3938
3939static void
3940switcher_next(struct switcher *switcher)
3941{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003942 struct weston_surface *surface;
3943 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003944 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003945 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003946
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003947 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003948 switch (get_shell_surface_type(surface)) {
3949 case SHELL_SURFACE_TOPLEVEL:
3950 case SHELL_SURFACE_FULLSCREEN:
3951 case SHELL_SURFACE_MAXIMIZED:
3952 if (first == NULL)
3953 first = surface;
3954 if (prev == switcher->current)
3955 next = surface;
3956 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003957 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003958 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003959 weston_surface_damage(surface);
3960 break;
3961 default:
3962 break;
3963 }
Alex Wu1659daa2012-04-01 20:13:09 +08003964
3965 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003966 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003967 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003968 weston_surface_damage(surface);
3969 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003970 }
3971
3972 if (next == NULL)
3973 next = first;
3974
Alex Wu07b26062012-03-12 16:06:01 +08003975 if (next == NULL)
3976 return;
3977
Kristian Høgsberg07045392012-02-19 18:52:44 -05003978 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003979 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003980
3981 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003982 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003983
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003984 shsurf = get_shell_surface(switcher->current);
3985 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003986 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003987}
3988
3989static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003990switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003991{
3992 struct switcher *switcher =
3993 container_of(listener, struct switcher, listener);
3994
3995 switcher_next(switcher);
3996}
3997
3998static void
Daniel Stone351eb612012-05-31 15:27:47 -04003999switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004000{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004001 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004002 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004003 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004004
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004005 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004006 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004007 weston_surface_damage(surface);
4008 }
4009
Alex Wu07b26062012-03-12 16:06:01 +08004010 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004011 activate(switcher->shell, switcher->current,
4012 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004013 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004014 weston_keyboard_end_grab(keyboard);
4015 if (keyboard->input_method_resource)
4016 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004017 free(switcher);
4018}
4019
4020static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004021switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004022 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004023{
4024 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004025 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004026
Daniel Stonec9785ea2012-05-30 16:31:52 +01004027 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004028 switcher_next(switcher);
4029}
4030
4031static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004032switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004033 uint32_t mods_depressed, uint32_t mods_latched,
4034 uint32_t mods_locked, uint32_t group)
4035{
4036 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004037 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004038
Daniel Stone351eb612012-05-31 15:27:47 -04004039 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4040 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004041}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004042
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004043static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004044 switcher_key,
4045 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004046};
4047
4048static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004049switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004050 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004051{
Tiago Vignattibe143262012-04-16 17:31:41 +03004052 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004053 struct switcher *switcher;
4054
4055 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004056 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004057 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004058 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004059 wl_list_init(&switcher->listener.link);
4060
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004061 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004062 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004063 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004064 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4065 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004066 switcher_next(switcher);
4067}
4068
Pekka Paalanen3c647232011-12-22 13:43:43 +02004069static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004070backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004071 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004072{
4073 struct weston_compositor *compositor = data;
4074 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004075 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004076
4077 /* TODO: we're limiting to simple use cases, where we assume just
4078 * control on the primary display. We'd have to extend later if we
4079 * ever get support for setting backlights on random desktop LCD
4080 * panels though */
4081 output = get_default_output(compositor);
4082 if (!output)
4083 return;
4084
4085 if (!output->set_backlight)
4086 return;
4087
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004088 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4089 backlight_new = output->backlight_current - 25;
4090 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4091 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004092
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004093 if (backlight_new < 5)
4094 backlight_new = 5;
4095 if (backlight_new > 255)
4096 backlight_new = 255;
4097
4098 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004099 output->set_backlight(output, output->backlight_current);
4100}
4101
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004102struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004103 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004104 struct weston_seat *seat;
4105 uint32_t key[2];
4106 int key_released[2];
4107};
4108
4109static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004110debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004111 uint32_t key, uint32_t state)
4112{
4113 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4114 struct wl_resource *resource;
4115 struct wl_display *display;
4116 uint32_t serial;
4117 int send = 0, terminate = 0;
4118 int check_binding = 1;
4119 int i;
4120
4121 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4122 /* Do not run bindings on key releases */
4123 check_binding = 0;
4124
4125 for (i = 0; i < 2; i++)
4126 if (key == db->key[i])
4127 db->key_released[i] = 1;
4128
4129 if (db->key_released[0] && db->key_released[1]) {
4130 /* All key releases been swalled so end the grab */
4131 terminate = 1;
4132 } else if (key != db->key[0] && key != db->key[1]) {
4133 /* Should not swallow release of other keys */
4134 send = 1;
4135 }
4136 } else if (key == db->key[0] && !db->key_released[0]) {
4137 /* Do not check bindings for the first press of the binding
4138 * key. This allows it to be used as a debug shortcut.
4139 * We still need to swallow this event. */
4140 check_binding = 0;
4141 } else if (db->key[1]) {
4142 /* If we already ran a binding don't process another one since
4143 * we can't keep track of all the binding keys that were
4144 * pressed in order to swallow the release events. */
4145 send = 1;
4146 check_binding = 0;
4147 }
4148
4149 if (check_binding) {
4150 struct weston_compositor *ec = db->seat->compositor;
4151
4152 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4153 key, state)) {
4154 /* We ran a binding so swallow the press and keep the
4155 * grab to swallow the released too. */
4156 send = 0;
4157 terminate = 0;
4158 db->key[1] = key;
4159 } else {
4160 /* Terminate the grab since the key pressed is not a
4161 * debug binding key. */
4162 send = 1;
4163 terminate = 1;
4164 }
4165 }
4166
4167 if (send) {
4168 resource = grab->keyboard->focus_resource;
4169
4170 if (resource) {
4171 display = wl_client_get_display(resource->client);
4172 serial = wl_display_next_serial(display);
4173 wl_keyboard_send_key(resource, serial, time, key, state);
4174 }
4175 }
4176
4177 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004178 weston_keyboard_end_grab(grab->keyboard);
4179 if (grab->keyboard->input_method_resource)
4180 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004181 free(db);
4182 }
4183}
4184
4185static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004186debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004187 uint32_t mods_depressed, uint32_t mods_latched,
4188 uint32_t mods_locked, uint32_t group)
4189{
4190 struct wl_resource *resource;
4191
4192 resource = grab->keyboard->focus_resource;
4193 if (!resource)
4194 return;
4195
4196 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4197 mods_latched, mods_locked, group);
4198}
4199
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004200struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004201 debug_binding_key,
4202 debug_binding_modifiers
4203};
4204
4205static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004206debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004207{
4208 struct debug_binding_grab *grab;
4209
4210 grab = calloc(1, sizeof *grab);
4211 if (!grab)
4212 return;
4213
4214 grab->seat = (struct weston_seat *) seat;
4215 grab->key[0] = key;
4216 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004217 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004218}
4219
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004220static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004221force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004222 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004223{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004224 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004225 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004226 struct desktop_shell *shell = data;
4227 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004228 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004229
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004230 focus_surface = seat->keyboard->focus;
4231 if (!focus_surface)
4232 return;
4233
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004234 wl_signal_emit(&compositor->kill_signal, focus_surface);
4235
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004236 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004237 wl_client_get_credentials(client, &pid, NULL, NULL);
4238
4239 /* Skip clients that we launched ourselves (the credentials of
4240 * the socketpair is ours) */
4241 if (pid == getpid())
4242 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004243
Daniel Stone325fc2d2012-05-30 16:31:58 +01004244 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004245}
4246
4247static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004248workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004249 uint32_t key, void *data)
4250{
4251 struct desktop_shell *shell = data;
4252 unsigned int new_index = shell->workspaces.current;
4253
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004254 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004255 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004256 if (new_index != 0)
4257 new_index--;
4258
4259 change_workspace(shell, new_index);
4260}
4261
4262static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004263workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004264 uint32_t key, void *data)
4265{
4266 struct desktop_shell *shell = data;
4267 unsigned int new_index = shell->workspaces.current;
4268
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004269 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004270 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004271 if (new_index < shell->workspaces.num - 1)
4272 new_index++;
4273
4274 change_workspace(shell, new_index);
4275}
4276
4277static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004278workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004279 uint32_t key, void *data)
4280{
4281 struct desktop_shell *shell = data;
4282 unsigned int new_index;
4283
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004284 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004285 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004286 new_index = key - KEY_F1;
4287 if (new_index >= shell->workspaces.num)
4288 new_index = shell->workspaces.num - 1;
4289
4290 change_workspace(shell, new_index);
4291}
4292
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004293static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004294workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004295 uint32_t key, void *data)
4296{
4297 struct desktop_shell *shell = data;
4298 unsigned int new_index = shell->workspaces.current;
4299
4300 if (shell->locked)
4301 return;
4302
4303 if (new_index != 0)
4304 new_index--;
4305
4306 take_surface_to_workspace_by_seat(shell, seat, new_index);
4307}
4308
4309static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004310workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004311 uint32_t key, void *data)
4312{
4313 struct desktop_shell *shell = data;
4314 unsigned int new_index = shell->workspaces.current;
4315
4316 if (shell->locked)
4317 return;
4318
4319 if (new_index < shell->workspaces.num - 1)
4320 new_index++;
4321
4322 take_surface_to_workspace_by_seat(shell, seat, new_index);
4323}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004324
4325static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004326shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004327{
Tiago Vignattibe143262012-04-16 17:31:41 +03004328 struct desktop_shell *shell =
4329 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004330 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004331
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004332 if (shell->child.client)
4333 wl_client_destroy(shell->child.client);
4334
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004335 wl_list_remove(&shell->idle_listener.link);
4336 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004337 wl_list_remove(&shell->show_input_panel_listener.link);
4338 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004339
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004340 wl_array_for_each(ws, &shell->workspaces.array)
4341 workspace_destroy(*ws);
4342 wl_array_release(&shell->workspaces.array);
4343
Pekka Paalanen3c647232011-12-22 13:43:43 +02004344 free(shell->screensaver.path);
4345 free(shell);
4346}
4347
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004348static void
4349shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4350{
4351 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004352 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004353
4354 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004355 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4356 MODIFIER_CTRL | MODIFIER_ALT,
4357 terminate_binding, ec);
4358 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4359 click_to_activate_binding,
4360 shell);
4361 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4362 MODIFIER_SUPER | MODIFIER_ALT,
4363 surface_opacity_binding, NULL);
4364 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4365 MODIFIER_SUPER, zoom_axis_binding,
4366 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004367
4368 /* configurable bindings */
4369 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004370 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4371 zoom_key_binding, NULL);
4372 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4373 zoom_key_binding, NULL);
4374 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4375 shell);
4376 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4377 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004378
4379 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4380 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4381 rotate_binding, NULL);
4382
Daniel Stone325fc2d2012-05-30 16:31:58 +01004383 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4384 shell);
4385 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4386 ec);
4387 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4388 backlight_binding, ec);
4389 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4390 ec);
4391 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4392 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004393 weston_compositor_add_key_binding(ec, KEY_K, mod,
4394 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004395 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4396 workspace_up_binding, shell);
4397 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4398 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004399 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4400 workspace_move_surface_up_binding,
4401 shell);
4402 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4403 workspace_move_surface_down_binding,
4404 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004405
4406 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4407 if (shell->workspaces.num > 1) {
4408 num_workspace_bindings = shell->workspaces.num;
4409 if (num_workspace_bindings > 6)
4410 num_workspace_bindings = 6;
4411 for (i = 0; i < num_workspace_bindings; i++)
4412 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4413 workspace_f_binding,
4414 shell);
4415 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004416
4417 /* Debug bindings */
4418 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4419 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004420}
4421
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004422WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004423module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004424 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004425{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004426 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004427 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004428 struct workspace **pws;
4429 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004430 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004431
4432 shell = malloc(sizeof *shell);
4433 if (shell == NULL)
4434 return -1;
4435
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004436 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004437 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004438
4439 shell->destroy_listener.notify = shell_destroy;
4440 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004441 shell->idle_listener.notify = idle_handler;
4442 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4443 shell->wake_listener.notify = wake_handler;
4444 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004445 shell->show_input_panel_listener.notify = show_input_panels;
4446 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4447 shell->hide_input_panel_listener.notify = hide_input_panels;
4448 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004449 shell->update_input_panel_listener.notify = update_input_panels;
4450 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004451 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004452 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004453 ec->shell_interface.create_shell_surface = create_shell_surface;
4454 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004455 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004456 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004457 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004458 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004459 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004460
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004461 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004462
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004463 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4464 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004465 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4466 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004467 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004468
4469 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004470 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004471
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004472 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004473
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004474 for (i = 0; i < shell->workspaces.num; i++) {
4475 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4476 if (pws == NULL)
4477 return -1;
4478
4479 *pws = workspace_create();
4480 if (*pws == NULL)
4481 return -1;
4482 }
4483 activate_workspace(shell, 0);
4484
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004485 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004486 wl_list_init(&shell->workspaces.animation.link);
4487 shell->workspaces.animation.frame = animate_workspace_change_frame;
4488
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004489 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4490 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004491 return -1;
4492
Kristian Høgsberg75840622011-09-06 13:48:16 -04004493 if (wl_display_add_global(ec->wl_display,
4494 &desktop_shell_interface,
4495 shell, bind_desktop_shell) == NULL)
4496 return -1;
4497
Pekka Paalanen6e168112011-11-24 11:34:05 +02004498 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4499 shell, bind_screensaver) == NULL)
4500 return -1;
4501
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004502 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004503 shell, bind_input_panel) == NULL)
4504 return -1;
4505
Jonas Ådahle9d22502012-08-29 22:13:01 +02004506 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4507 shell, bind_workspace_manager) == NULL)
4508 return -1;
4509
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004510 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004511
4512 loop = wl_display_get_event_loop(ec->wl_display);
4513 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004514
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004515 shell->screensaver.timer =
4516 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4517
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004518 wl_list_for_each(seat, &ec->seat_list, link)
4519 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004520
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004521 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004522
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004523 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004524
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004525 return 0;
4526}