blob: bcde7895dfe612841b521957857e03bb78d5731a [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 {
Jason Ekstrand51e5b142013-06-14 10:07:58 -050076 struct wl_resource *resource;
77 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010078
79 struct desktop_shell *shell;
80
Philipp Brüschweiler88013572012-08-06 13:44:42 +020081 struct wl_list link;
82 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010083 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020084
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020085 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020086 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020087};
88
Tiago Vignattibe143262012-04-16 17:31:41 +030089struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050090 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040091
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020092 struct wl_listener idle_listener;
93 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040094 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020095 struct wl_listener show_input_panel_listener;
96 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020097 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020098
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050099 struct weston_layer fullscreen_layer;
100 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500101 struct weston_layer background_layer;
102 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200103 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500104
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400105 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300106 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400107
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200108 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500109 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200110 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200111 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200112
113 unsigned deathcount;
114 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200115 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200116
117 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200118 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200119 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200120
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200121 struct {
122 struct weston_surface *surface;
123 pixman_box32_t cursor_rectangle;
124 } text_input;
125
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400126 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500127 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100128
Pekka Paalanen77346a62011-11-30 16:26:35 +0200129 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200130 struct wl_array array;
131 unsigned int current;
132 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200133
Jonas Ådahle9d22502012-08-29 22:13:01 +0200134 struct wl_list client_list;
135
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200136 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200137 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200138 int anim_dir;
139 uint32_t anim_timestamp;
140 double anim_current;
141 struct workspace *anim_from;
142 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200143 } workspaces;
144
145 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200146 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200147 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200148 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500149 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200150 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200151 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500152
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200153 struct {
154 struct wl_resource *binding;
155 struct wl_list surfaces;
156 } input_panel;
157
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200158 struct {
159 struct weston_surface *surface;
160 struct weston_surface_animation *animation;
161 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300162 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200163 } fade;
164
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300165 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800166 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400167};
168
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500169enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200170 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500171 SHELL_SURFACE_TOPLEVEL,
172 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800174 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300175 SHELL_SURFACE_POPUP,
176 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200177};
178
Scott Moreauff1db4a2012-04-17 19:06:18 -0600179struct ping_timer {
180 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600181 uint32_t serial;
182};
183
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200184struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500185 struct wl_resource *resource;
186 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200187
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500188 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200189 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400190 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300191 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200192
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400193 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400194 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500195 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800196 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800197 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600198 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100199
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500200 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200201 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500202 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 } rotation;
204
205 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100206 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500207 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100208 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400209 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500210 } popup;
211
Alex Wu4539b082012-03-01 12:57:46 +0800212 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300213 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400214 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300215 } transient;
216
217 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800218 enum wl_shell_surface_fullscreen_method type;
219 struct weston_transform transform; /* matrix from x, y */
220 uint32_t framerate;
221 struct weston_surface *black_surface;
222 } fullscreen;
223
Scott Moreauff1db4a2012-04-17 19:06:18 -0600224 struct ping_timer *ping_timer;
225
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200226 struct weston_transform workspace_transform;
227
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500228 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100230 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400231
232 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200233};
234
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400236 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300237 struct shell_surface *shsurf;
238 struct wl_listener shsurf_destroy_listener;
239};
240
241struct weston_move_grab {
242 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100243 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500244};
245
Pekka Paalanen460099f2012-01-20 16:48:25 +0200246struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300247 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500248 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200249 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200250 float x;
251 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200252 } center;
253};
254
Giulio Camuffo5085a752013-03-25 21:42:45 +0100255struct shell_seat {
256 struct weston_seat *seat;
257 struct wl_listener seat_destroy_listener;
258
259 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400260 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100261 struct wl_list surfaces_list;
262 struct wl_client *client;
263 int32_t initial_up;
264 } popup_grab;
265};
266
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400267static void
268activate(struct desktop_shell *shell, struct weston_surface *es,
269 struct weston_seat *seat);
270
271static struct workspace *
272get_current_workspace(struct desktop_shell *shell);
273
Alex Wubd3354b2012-04-17 17:20:49 +0800274static struct shell_surface *
275get_shell_surface(struct weston_surface *surface);
276
277static struct desktop_shell *
278shell_surface_get_shell(struct shell_surface *shsurf);
279
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500280static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400281surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500282
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300283static void
284shell_fade_startup(struct desktop_shell *shell);
285
Alex Wubd3354b2012-04-17 17:20:49 +0800286static bool
287shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
288{
289 struct desktop_shell *shell;
290 struct weston_surface *top_fs_es;
291
292 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100293
Alex Wubd3354b2012-04-17 17:20:49 +0800294 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
295 return false;
296
297 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100298 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800299 layer_link);
300 return (shsurf == get_shell_surface(top_fs_es));
301}
302
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500303static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400304destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300305{
306 struct shell_grab *grab;
307
308 grab = container_of(listener, struct shell_grab,
309 shsurf_destroy_listener);
310
311 grab->shsurf = NULL;
312}
313
314static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400315popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400316
317static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300318shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400319 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300320 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400321 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300322 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300324 struct desktop_shell *shell = shsurf->shell;
325
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400326 popup_grab_end(pointer);
327
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300328 grab->grab.interface = interface;
329 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400330 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500331 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400332 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300333
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400334 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400335 if (shell->child.desktop_shell) {
336 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
337 cursor);
338 weston_pointer_set_focus(pointer, shell->grab_surface,
339 wl_fixed_from_int(0),
340 wl_fixed_from_int(0));
341 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342}
343
344static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300345shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300346{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400347 if (grab->shsurf)
348 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300349
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700350 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300351}
352
353static void
Alex Wu4539b082012-03-01 12:57:46 +0800354center_on_output(struct weston_surface *surface,
355 struct weston_output *output);
356
Daniel Stone496ca172012-05-30 16:31:42 +0100357static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300358get_modifier(char *modifier)
359{
360 if (!modifier)
361 return MODIFIER_SUPER;
362
363 if (!strcmp("ctrl", modifier))
364 return MODIFIER_CTRL;
365 else if (!strcmp("alt", modifier))
366 return MODIFIER_ALT;
367 else if (!strcmp("super", modifier))
368 return MODIFIER_SUPER;
369 else
370 return MODIFIER_SUPER;
371}
372
Juan Zhaoe10d2792012-04-25 19:09:52 +0800373static enum animation_type
374get_animation_type(char *animation)
375{
376 if (!animation)
377 return ANIMATION_NONE;
378
379 if (!strcmp("zoom", animation))
380 return ANIMATION_ZOOM;
381 else if (!strcmp("fade", animation))
382 return ANIMATION_FADE;
383 else
384 return ANIMATION_NONE;
385}
386
Alex Wu4539b082012-03-01 12:57:46 +0800387static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400388shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200389{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400390 struct weston_config_section *section;
391 int duration;
392 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200393
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400394 section = weston_config_get_section(shell->compositor->config,
395 "screensaver", NULL, NULL);
396 weston_config_section_get_string(section,
397 "path", &shell->screensaver.path, NULL);
398 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200399 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400400
401 section = weston_config_get_section(shell->compositor->config,
402 "shell", NULL, NULL);
403 weston_config_section_get_string(section,
404 "binding-modifier", &s, "super");
405 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200406 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400407 weston_config_section_get_string(section, "animation", &s, "none");
408 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200409 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400410 weston_config_section_get_uint(section, "num-workspaces",
411 &shell->workspaces.num,
412 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200413}
414
415static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200416focus_state_destroy(struct focus_state *state)
417{
418 wl_list_remove(&state->seat_destroy_listener.link);
419 wl_list_remove(&state->surface_destroy_listener.link);
420 free(state);
421}
422
423static void
424focus_state_seat_destroy(struct wl_listener *listener, void *data)
425{
426 struct focus_state *state = container_of(listener,
427 struct focus_state,
428 seat_destroy_listener);
429
430 wl_list_remove(&state->link);
431 focus_state_destroy(state);
432}
433
434static void
435focus_state_surface_destroy(struct wl_listener *listener, void *data)
436{
437 struct focus_state *state = container_of(listener,
438 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400439 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400440 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300441 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400442 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200443
Pekka Paalanen01388e22013-04-25 13:57:44 +0300444 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
445
Kristian Høgsberge3778222012-07-31 17:29:30 -0400446 next = NULL;
447 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300448 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400449 continue;
450
451 next = surface;
452 break;
453 }
454
Pekka Paalanen01388e22013-04-25 13:57:44 +0300455 /* if the focus was a sub-surface, activate its main surface */
456 if (main_surface != state->keyboard_focus)
457 next = main_surface;
458
Kristian Høgsberge3778222012-07-31 17:29:30 -0400459 if (next) {
460 shell = state->seat->compositor->shell_interface.shell;
461 activate(shell, next, state->seat);
462 } else {
463 wl_list_remove(&state->link);
464 focus_state_destroy(state);
465 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200466}
467
468static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400469focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200470{
Jonas Ådahl04769742012-06-13 00:01:24 +0200471 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200472
473 state = malloc(sizeof *state);
474 if (state == NULL)
475 return NULL;
476
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400477 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200478 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400479 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200480
481 state->seat_destroy_listener.notify = focus_state_seat_destroy;
482 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400483 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200484 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400485 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200486
487 return state;
488}
489
Jonas Ådahl8538b222012-08-29 22:13:03 +0200490static struct focus_state *
491ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
492{
493 struct workspace *ws = get_current_workspace(shell);
494 struct focus_state *state;
495
496 wl_list_for_each(state, &ws->focus_list, link)
497 if (state->seat == seat)
498 break;
499
500 if (&state->link == &ws->focus_list)
501 state = focus_state_create(seat, ws);
502
503 return state;
504}
505
Jonas Ådahl04769742012-06-13 00:01:24 +0200506static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400507restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200508{
509 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400510 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200511
512 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400513 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200514
Kristian Høgsberge3148752013-05-06 23:19:49 -0400515 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200516 }
517}
518
519static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200520replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
521 struct weston_seat *seat)
522{
523 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400524 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200525
526 wl_list_for_each(state, &ws->focus_list, link) {
527 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400528 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500529 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200530 return;
531 }
532 }
533}
534
535static void
536drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
537 struct weston_surface *surface)
538{
539 struct focus_state *state;
540
541 wl_list_for_each(state, &ws->focus_list, link)
542 if (state->keyboard_focus == surface)
543 state->keyboard_focus = NULL;
544}
545
546static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200547workspace_destroy(struct workspace *ws)
548{
Jonas Ådahl04769742012-06-13 00:01:24 +0200549 struct focus_state *state, *next;
550
551 wl_list_for_each_safe(state, next, &ws->focus_list, link)
552 focus_state_destroy(state);
553
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200554 free(ws);
555}
556
Jonas Ådahl04769742012-06-13 00:01:24 +0200557static void
558seat_destroyed(struct wl_listener *listener, void *data)
559{
560 struct weston_seat *seat = data;
561 struct focus_state *state, *next;
562 struct workspace *ws = container_of(listener,
563 struct workspace,
564 seat_destroyed_listener);
565
566 wl_list_for_each_safe(state, next, &ws->focus_list, link)
567 if (state->seat == seat)
568 wl_list_remove(&state->link);
569}
570
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200571static struct workspace *
572workspace_create(void)
573{
574 struct workspace *ws = malloc(sizeof *ws);
575 if (ws == NULL)
576 return NULL;
577
578 weston_layer_init(&ws->layer, NULL);
579
Jonas Ådahl04769742012-06-13 00:01:24 +0200580 wl_list_init(&ws->focus_list);
581 wl_list_init(&ws->seat_destroyed_listener.link);
582 ws->seat_destroyed_listener.notify = seat_destroyed;
583
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200584 return ws;
585}
586
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200587static int
588workspace_is_empty(struct workspace *ws)
589{
590 return wl_list_empty(&ws->layer.surface_list);
591}
592
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200593static struct workspace *
594get_workspace(struct desktop_shell *shell, unsigned int index)
595{
596 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200597 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200598 pws += index;
599 return *pws;
600}
601
602static struct workspace *
603get_current_workspace(struct desktop_shell *shell)
604{
605 return get_workspace(shell, shell->workspaces.current);
606}
607
608static void
609activate_workspace(struct desktop_shell *shell, unsigned int index)
610{
611 struct workspace *ws;
612
613 ws = get_workspace(shell, index);
614 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
615
616 shell->workspaces.current = index;
617}
618
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200619static unsigned int
620get_output_height(struct weston_output *output)
621{
622 return abs(output->region.extents.y1 - output->region.extents.y2);
623}
624
625static void
626surface_translate(struct weston_surface *surface, double d)
627{
628 struct shell_surface *shsurf = get_shell_surface(surface);
629 struct weston_transform *transform;
630
631 transform = &shsurf->workspace_transform;
632 if (wl_list_empty(&transform->link))
633 wl_list_insert(surface->geometry.transformation_list.prev,
634 &shsurf->workspace_transform.link);
635
636 weston_matrix_init(&shsurf->workspace_transform.matrix);
637 weston_matrix_translate(&shsurf->workspace_transform.matrix,
638 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200639 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200640}
641
642static void
643workspace_translate_out(struct workspace *ws, double fraction)
644{
645 struct weston_surface *surface;
646 unsigned int height;
647 double d;
648
649 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
650 height = get_output_height(surface->output);
651 d = height * fraction;
652
653 surface_translate(surface, d);
654 }
655}
656
657static void
658workspace_translate_in(struct workspace *ws, double fraction)
659{
660 struct weston_surface *surface;
661 unsigned int height;
662 double d;
663
664 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
665 height = get_output_height(surface->output);
666
667 if (fraction > 0)
668 d = -(height - height * fraction);
669 else
670 d = height + height * fraction;
671
672 surface_translate(surface, d);
673 }
674}
675
676static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200677broadcast_current_workspace_state(struct desktop_shell *shell)
678{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500679 struct wl_list *link;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200680
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500681 for (link = shell->workspaces.client_list.next;
682 link != &shell->workspaces.client_list;
683 link = link->next) {
684 workspace_manager_send_state(wl_resource_from_link(link),
Jonas Ådahle9d22502012-08-29 22:13:01 +0200685 shell->workspaces.current,
686 shell->workspaces.num);
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500687 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200688}
689
690static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200691reverse_workspace_change_animation(struct desktop_shell *shell,
692 unsigned int index,
693 struct workspace *from,
694 struct workspace *to)
695{
696 shell->workspaces.current = index;
697
698 shell->workspaces.anim_to = to;
699 shell->workspaces.anim_from = from;
700 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
701 shell->workspaces.anim_timestamp = 0;
702
Scott Moreau4272e632012-08-13 09:58:41 -0600703 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200704}
705
706static void
707workspace_deactivate_transforms(struct workspace *ws)
708{
709 struct weston_surface *surface;
710 struct shell_surface *shsurf;
711
712 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
713 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200714 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
715 wl_list_remove(&shsurf->workspace_transform.link);
716 wl_list_init(&shsurf->workspace_transform.link);
717 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200718 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200719 }
720}
721
722static void
723finish_workspace_change_animation(struct desktop_shell *shell,
724 struct workspace *from,
725 struct workspace *to)
726{
Scott Moreau4272e632012-08-13 09:58:41 -0600727 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200728
729 wl_list_remove(&shell->workspaces.animation.link);
730 workspace_deactivate_transforms(from);
731 workspace_deactivate_transforms(to);
732 shell->workspaces.anim_to = NULL;
733
734 wl_list_remove(&shell->workspaces.anim_from->layer.link);
735}
736
737static void
738animate_workspace_change_frame(struct weston_animation *animation,
739 struct weston_output *output, uint32_t msecs)
740{
741 struct desktop_shell *shell =
742 container_of(animation, struct desktop_shell,
743 workspaces.animation);
744 struct workspace *from = shell->workspaces.anim_from;
745 struct workspace *to = shell->workspaces.anim_to;
746 uint32_t t;
747 double x, y;
748
749 if (workspace_is_empty(from) && workspace_is_empty(to)) {
750 finish_workspace_change_animation(shell, from, to);
751 return;
752 }
753
754 if (shell->workspaces.anim_timestamp == 0) {
755 if (shell->workspaces.anim_current == 0.0)
756 shell->workspaces.anim_timestamp = msecs;
757 else
758 shell->workspaces.anim_timestamp =
759 msecs -
760 /* Invers of movement function 'y' below. */
761 (asin(1.0 - shell->workspaces.anim_current) *
762 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
763 M_2_PI);
764 }
765
766 t = msecs - shell->workspaces.anim_timestamp;
767
768 /*
769 * x = [0, π/2]
770 * y(x) = sin(x)
771 */
772 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
773 y = sin(x);
774
775 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600776 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200777
778 workspace_translate_out(from, shell->workspaces.anim_dir * y);
779 workspace_translate_in(to, shell->workspaces.anim_dir * y);
780 shell->workspaces.anim_current = y;
781
Scott Moreau4272e632012-08-13 09:58:41 -0600782 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200783 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200784 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200785 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200786}
787
788static void
789animate_workspace_change(struct desktop_shell *shell,
790 unsigned int index,
791 struct workspace *from,
792 struct workspace *to)
793{
794 struct weston_output *output;
795
796 int dir;
797
798 if (index > shell->workspaces.current)
799 dir = -1;
800 else
801 dir = 1;
802
803 shell->workspaces.current = index;
804
805 shell->workspaces.anim_dir = dir;
806 shell->workspaces.anim_from = from;
807 shell->workspaces.anim_to = to;
808 shell->workspaces.anim_current = 0.0;
809 shell->workspaces.anim_timestamp = 0;
810
811 output = container_of(shell->compositor->output_list.next,
812 struct weston_output, link);
813 wl_list_insert(&output->animation_list,
814 &shell->workspaces.animation.link);
815
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200816 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200817
818 workspace_translate_in(to, 0);
819
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400820 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200821
Scott Moreau4272e632012-08-13 09:58:41 -0600822 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200823}
824
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200825static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200826update_workspace(struct desktop_shell *shell, unsigned int index,
827 struct workspace *from, struct workspace *to)
828{
829 shell->workspaces.current = index;
830 wl_list_insert(&from->layer.link, &to->layer.link);
831 wl_list_remove(&from->layer.link);
832}
833
834static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200835change_workspace(struct desktop_shell *shell, unsigned int index)
836{
837 struct workspace *from;
838 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200839
840 if (index == shell->workspaces.current)
841 return;
842
843 /* Don't change workspace when there is any fullscreen surfaces. */
844 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
845 return;
846
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200847 from = get_current_workspace(shell);
848 to = get_workspace(shell, index);
849
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200850 if (shell->workspaces.anim_from == to &&
851 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200852 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200853 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200854 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855 return;
856 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200857
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200858 if (shell->workspaces.anim_to != NULL)
859 finish_workspace_change_animation(shell,
860 shell->workspaces.anim_from,
861 shell->workspaces.anim_to);
862
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200863 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200864
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200865 if (workspace_is_empty(to) && workspace_is_empty(from))
866 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200867 else
868 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200869
870 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200871}
872
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200873static bool
874workspace_has_only(struct workspace *ws, struct weston_surface *surface)
875{
876 struct wl_list *list = &ws->layer.surface_list;
877 struct wl_list *e;
878
879 if (wl_list_empty(list))
880 return false;
881
882 e = list->next;
883
884 if (e->next != list)
885 return false;
886
887 return container_of(e, struct weston_surface, layer_link) == surface;
888}
889
890static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200891move_surface_to_workspace(struct desktop_shell *shell,
892 struct weston_surface *surface,
893 uint32_t workspace)
894{
895 struct workspace *from;
896 struct workspace *to;
897 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300898 struct weston_surface *focus;
899
900 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200901
902 if (workspace == shell->workspaces.current)
903 return;
904
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200905 if (workspace >= shell->workspaces.num)
906 workspace = shell->workspaces.num - 1;
907
Jonas Ådahle9d22502012-08-29 22:13:01 +0200908 from = get_current_workspace(shell);
909 to = get_workspace(shell, workspace);
910
911 wl_list_remove(&surface->layer_link);
912 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
913
914 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300915 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
916 if (!seat->keyboard)
917 continue;
918
919 focus = weston_surface_get_main_surface(seat->keyboard->focus);
920 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400921 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300922 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200923
924 weston_surface_damage_below(surface);
925}
926
927static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200928take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400929 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200930 unsigned int index)
931{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300932 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200933 struct shell_surface *shsurf;
934 struct workspace *from;
935 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200936 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200937
Pekka Paalanen01388e22013-04-25 13:57:44 +0300938 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200939 if (surface == NULL ||
940 index == shell->workspaces.current)
941 return;
942
943 from = get_current_workspace(shell);
944 to = get_workspace(shell, index);
945
946 wl_list_remove(&surface->layer_link);
947 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
948
Jonas Ådahle9d22502012-08-29 22:13:01 +0200949 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200950 drop_focus_state(shell, from, surface);
951
952 if (shell->workspaces.anim_from == to &&
953 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200954 wl_list_remove(&to->layer.link);
955 wl_list_insert(from->layer.link.prev, &to->layer.link);
956
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200957 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200958 broadcast_current_workspace_state(shell);
959
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200960 return;
961 }
962
963 if (shell->workspaces.anim_to != NULL)
964 finish_workspace_change_animation(shell,
965 shell->workspaces.anim_from,
966 shell->workspaces.anim_to);
967
968 if (workspace_is_empty(from) &&
969 workspace_has_only(to, surface))
970 update_workspace(shell, index, from, to);
971 else {
972 shsurf = get_shell_surface(surface);
973 if (wl_list_empty(&shsurf->workspace_transform.link))
974 wl_list_insert(&shell->workspaces.anim_sticky_list,
975 &shsurf->workspace_transform.link);
976
977 animate_workspace_change(shell, index, from, to);
978 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200979
980 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200981
982 state = ensure_focus_state(shell, seat);
983 if (state != NULL)
984 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200985}
986
987static void
988workspace_manager_move_surface(struct wl_client *client,
989 struct wl_resource *resource,
990 struct wl_resource *surface_resource,
991 uint32_t workspace)
992{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500993 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200994 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500995 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300996 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200997
Pekka Paalanen01388e22013-04-25 13:57:44 +0300998 main_surface = weston_surface_get_main_surface(surface);
999 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001000}
1001
1002static const struct workspace_manager_interface workspace_manager_implementation = {
1003 workspace_manager_move_surface,
1004};
1005
1006static void
1007unbind_resource(struct wl_resource *resource)
1008{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001009 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001010}
1011
1012static void
1013bind_workspace_manager(struct wl_client *client,
1014 void *data, uint32_t version, uint32_t id)
1015{
1016 struct desktop_shell *shell = data;
1017 struct wl_resource *resource;
1018
Jason Ekstranda85118c2013-06-27 20:17:02 -05001019 resource = wl_resource_create(client,
1020 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001021
1022 if (resource == NULL) {
1023 weston_log("couldn't add workspace manager object");
1024 return;
1025 }
1026
Jason Ekstranda85118c2013-06-27 20:17:02 -05001027 wl_resource_set_implementation(resource,
1028 &workspace_manager_implementation,
1029 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001030 wl_list_insert(&shell->workspaces.client_list,
1031 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001032
1033 workspace_manager_send_state(resource,
1034 shell->workspaces.current,
1035 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001036}
1037
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001038static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001039noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001040{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001041}
1042
1043static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001044move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001045{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001046 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001047 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001048 struct shell_surface *shsurf = move->base.shsurf;
1049 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001050 int dx = wl_fixed_to_int(pointer->x + move->dx);
1051 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001052
1053 if (!shsurf)
1054 return;
1055
1056 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001057
Daniel Stone103db7f2012-05-08 17:17:55 +01001058 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001059 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001060
1061 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001062}
1063
1064static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001065move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001066 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001067{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001068 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1069 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001070 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001071 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001072
Daniel Stone4dbadb12012-05-30 16:31:51 +01001073 if (pointer->button_count == 0 &&
1074 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001075 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001076 free(grab);
1077 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001078}
1079
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001080static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001081 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001082 move_grab_motion,
1083 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001084};
1085
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001086static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001087surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001088{
1089 struct weston_move_grab *move;
1090
1091 if (!shsurf)
1092 return -1;
1093
1094 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1095 return 0;
1096
1097 move = malloc(sizeof *move);
1098 if (!move)
1099 return -1;
1100
1101 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001102 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001103 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001104 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001105
1106 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001107 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001108
1109 return 0;
1110}
1111
1112static void
1113shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1114 struct wl_resource *seat_resource, uint32_t serial)
1115{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001116 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001117 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001118 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001119
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001120 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001121 if (seat->pointer->button_count == 0 ||
1122 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001123 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001124 return;
1125
Kristian Høgsberge3148752013-05-06 23:19:49 -04001126 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001127 wl_resource_post_no_memory(resource);
1128}
1129
1130struct weston_resize_grab {
1131 struct shell_grab base;
1132 uint32_t edges;
1133 int32_t width, height;
1134};
1135
1136static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001137resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001138{
1139 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001140 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001141 struct shell_surface *shsurf = resize->base.shsurf;
1142 int32_t width, height;
1143 wl_fixed_t from_x, from_y;
1144 wl_fixed_t to_x, to_y;
1145
1146 if (!shsurf)
1147 return;
1148
1149 weston_surface_from_global_fixed(shsurf->surface,
1150 pointer->grab_x, pointer->grab_y,
1151 &from_x, &from_y);
1152 weston_surface_from_global_fixed(shsurf->surface,
1153 pointer->x, pointer->y, &to_x, &to_y);
1154
1155 width = resize->width;
1156 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1157 width += wl_fixed_to_int(from_x - to_x);
1158 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1159 width += wl_fixed_to_int(to_x - from_x);
1160 }
1161
1162 height = resize->height;
1163 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1164 height += wl_fixed_to_int(from_y - to_y);
1165 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1166 height += wl_fixed_to_int(to_y - from_y);
1167 }
1168
1169 shsurf->client->send_configure(shsurf->surface,
1170 resize->edges, width, height);
1171}
1172
1173static void
1174send_configure(struct weston_surface *surface,
1175 uint32_t edges, int32_t width, int32_t height)
1176{
1177 struct shell_surface *shsurf = get_shell_surface(surface);
1178
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001179 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001180 edges, width, height);
1181}
1182
1183static const struct weston_shell_client shell_client = {
1184 send_configure
1185};
1186
1187static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001188resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001189 uint32_t time, uint32_t button, uint32_t state_w)
1190{
1191 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001192 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001193 enum wl_pointer_button_state state = state_w;
1194
1195 if (pointer->button_count == 0 &&
1196 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1197 shell_grab_end(&resize->base);
1198 free(grab);
1199 }
1200}
1201
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001202static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001203 noop_grab_focus,
1204 resize_grab_motion,
1205 resize_grab_button,
1206};
1207
Giulio Camuffob8366642013-04-25 13:57:46 +03001208/*
1209 * Returns the bounding box of a surface and all its sub-surfaces,
1210 * in the surface coordinates system. */
1211static void
1212surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1213 int32_t *y, int32_t *w, int32_t *h) {
1214 pixman_region32_t region;
1215 pixman_box32_t *box;
1216 struct weston_subsurface *subsurface;
1217
1218 pixman_region32_init_rect(&region, 0, 0,
1219 surface->geometry.width,
1220 surface->geometry.height);
1221
1222 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1223 pixman_region32_union_rect(&region, &region,
1224 subsurface->position.x,
1225 subsurface->position.y,
1226 subsurface->surface->geometry.width,
1227 subsurface->surface->geometry.height);
1228 }
1229
1230 box = pixman_region32_extents(&region);
1231 if (x)
1232 *x = box->x1;
1233 if (y)
1234 *y = box->y1;
1235 if (w)
1236 *w = box->x2 - box->x1;
1237 if (h)
1238 *h = box->y2 - box->y1;
1239
1240 pixman_region32_fini(&region);
1241}
1242
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001243static int
1244surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001245 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001246{
1247 struct weston_resize_grab *resize;
1248
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001249 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1250 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001251 return 0;
1252
1253 if (edges == 0 || edges > 15 ||
1254 (edges & 3) == 3 || (edges & 12) == 12)
1255 return 0;
1256
1257 resize = malloc(sizeof *resize);
1258 if (!resize)
1259 return -1;
1260
1261 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001262 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1263 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001264
1265 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001266 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001267
1268 return 0;
1269}
1270
1271static void
1272shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1273 struct wl_resource *seat_resource, uint32_t serial,
1274 uint32_t edges)
1275{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001276 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001277 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001278 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001279
1280 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1281 return;
1282
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001283 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001284 if (seat->pointer->button_count == 0 ||
1285 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001286 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001287 return;
1288
Kristian Høgsberge3148752013-05-06 23:19:49 -04001289 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001290 wl_resource_post_no_memory(resource);
1291}
1292
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001293static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001294end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1295
1296static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001297busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001298{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001299 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001300 struct weston_pointer *pointer = base->pointer;
1301 struct weston_surface *surface;
1302 wl_fixed_t sx, sy;
1303
1304 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1305 pointer->x, pointer->y,
1306 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001307
Kristian Høgsberg67800732013-07-04 01:12:17 -04001308 if (!grab->shsurf || grab->shsurf->surface != surface)
1309 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001310}
1311
1312static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001313busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001314{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001315}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001316
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001317static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001318busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001319 uint32_t time, uint32_t button, uint32_t state)
1320{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001321 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001322 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001323 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001324
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001325 if (shsurf && button == BTN_LEFT && state) {
1326 activate(shsurf->shell, shsurf->surface, seat);
1327 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001328 } else if (shsurf && button == BTN_RIGHT && state) {
1329 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001330 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001331 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001332}
1333
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001334static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001335 busy_cursor_grab_focus,
1336 busy_cursor_grab_motion,
1337 busy_cursor_grab_button,
1338};
1339
1340static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001341set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001342{
1343 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001344
1345 grab = malloc(sizeof *grab);
1346 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001347 return;
1348
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001349 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1350 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001351}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001352
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001353static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001354end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001355{
1356 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1357
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001358 if (grab->grab.interface == &busy_cursor_grab_interface &&
1359 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001360 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001361 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001362 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001363}
1364
Scott Moreau9521d5e2012-04-19 13:06:17 -06001365static void
1366ping_timer_destroy(struct shell_surface *shsurf)
1367{
1368 if (!shsurf || !shsurf->ping_timer)
1369 return;
1370
1371 if (shsurf->ping_timer->source)
1372 wl_event_source_remove(shsurf->ping_timer->source);
1373
1374 free(shsurf->ping_timer);
1375 shsurf->ping_timer = NULL;
1376}
1377
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001378static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001379ping_timeout_handler(void *data)
1380{
1381 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001382 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001383
Scott Moreau9521d5e2012-04-19 13:06:17 -06001384 /* Client is not responding */
1385 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001386
1387 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001388 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001389 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001390
1391 return 1;
1392}
1393
1394static void
1395ping_handler(struct weston_surface *surface, uint32_t serial)
1396{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001397 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001398 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001399 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001400
1401 if (!shsurf)
1402 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001403 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001404 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001405
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001406 if (shsurf->surface == shsurf->shell->grab_surface)
1407 return;
1408
Scott Moreauff1db4a2012-04-17 19:06:18 -06001409 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001410 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001411 if (!shsurf->ping_timer)
1412 return;
1413
1414 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001415 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1416 shsurf->ping_timer->source =
1417 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1418 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1419
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001420 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001421 }
1422}
1423
1424static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001425handle_pointer_focus(struct wl_listener *listener, void *data)
1426{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001427 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001428 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001429 struct weston_compositor *compositor;
1430 struct shell_surface *shsurf;
1431 uint32_t serial;
1432
1433 if (!surface)
1434 return;
1435
1436 compositor = surface->compositor;
1437 shsurf = get_shell_surface(surface);
1438
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001439 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001440 set_busy_cursor(shsurf, pointer);
1441 } else {
1442 serial = wl_display_next_serial(compositor->wl_display);
1443 ping_handler(surface, serial);
1444 }
1445}
1446
1447static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001448create_pointer_focus_listener(struct weston_seat *seat)
1449{
1450 struct wl_listener *listener;
1451
Kristian Høgsberge3148752013-05-06 23:19:49 -04001452 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001453 return;
1454
1455 listener = malloc(sizeof *listener);
1456 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001457 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001458}
1459
1460static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001461shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1462 uint32_t serial)
1463{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001464 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001465 struct weston_seat *seat;
1466 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001467
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001468 if (shsurf->ping_timer == NULL)
1469 /* Just ignore unsolicited pong. */
1470 return;
1471
Scott Moreauff1db4a2012-04-17 19:06:18 -06001472 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001473 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001474 wl_list_for_each(seat, &ec->seat_list, link) {
1475 if(seat->pointer)
1476 end_busy_cursor(shsurf, seat->pointer);
1477 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001478 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001479 }
1480}
1481
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001482static void
1483shell_surface_set_title(struct wl_client *client,
1484 struct wl_resource *resource, const char *title)
1485{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001486 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001487
1488 free(shsurf->title);
1489 shsurf->title = strdup(title);
1490}
1491
1492static void
1493shell_surface_set_class(struct wl_client *client,
1494 struct wl_resource *resource, const char *class)
1495{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001496 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001497
1498 free(shsurf->class);
1499 shsurf->class = strdup(class);
1500}
1501
Juan Zhao96879df2012-02-07 08:45:41 +08001502static struct weston_output *
1503get_default_output(struct weston_compositor *compositor)
1504{
1505 return container_of(compositor->output_list.next,
1506 struct weston_output, link);
1507}
1508
Alex Wu4539b082012-03-01 12:57:46 +08001509static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001510restore_output_mode(struct weston_output *output)
1511{
1512 if (output->current != output->origin ||
1513 (int32_t)output->scale != output->origin_scale)
1514 weston_output_switch_mode(output,
1515 output->origin,
1516 output->origin_scale);
1517}
1518
1519static void
1520restore_all_output_modes(struct weston_compositor *compositor)
1521{
1522 struct weston_output *output;
1523
1524 wl_list_for_each(output, &compositor->output_list, link)
1525 restore_output_mode(output);
1526}
1527
1528static void
Alex Wu4539b082012-03-01 12:57:46 +08001529shell_unset_fullscreen(struct shell_surface *shsurf)
1530{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001531 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001532 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001533 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1534 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001535 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001536 }
Alex Wu4539b082012-03-01 12:57:46 +08001537 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1538 shsurf->fullscreen.framerate = 0;
1539 wl_list_remove(&shsurf->fullscreen.transform.link);
1540 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001541 if (shsurf->fullscreen.black_surface)
1542 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001543 shsurf->fullscreen.black_surface = NULL;
1544 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001545 weston_surface_set_position(shsurf->surface,
1546 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001547 if (shsurf->saved_rotation_valid) {
1548 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1549 &shsurf->rotation.transform.link);
1550 shsurf->saved_rotation_valid = false;
1551 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001552
1553 ws = get_current_workspace(shsurf->shell);
1554 wl_list_remove(&shsurf->surface->layer_link);
1555 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001556}
1557
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001558static void
1559shell_unset_maximized(struct shell_surface *shsurf)
1560{
1561 struct workspace *ws;
1562 /* undo all maximized things here */
1563 shsurf->output = get_default_output(shsurf->surface->compositor);
1564 weston_surface_set_position(shsurf->surface,
1565 shsurf->saved_x,
1566 shsurf->saved_y);
1567
1568 if (shsurf->saved_rotation_valid) {
1569 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1570 &shsurf->rotation.transform.link);
1571 shsurf->saved_rotation_valid = false;
1572 }
1573
1574 ws = get_current_workspace(shsurf->shell);
1575 wl_list_remove(&shsurf->surface->layer_link);
1576 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1577}
1578
Pekka Paalanen98262232011-12-01 10:42:22 +02001579static int
1580reset_shell_surface_type(struct shell_surface *surface)
1581{
1582 switch (surface->type) {
1583 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001584 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001585 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001586 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001587 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001588 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001589 case SHELL_SURFACE_NONE:
1590 case SHELL_SURFACE_TOPLEVEL:
1591 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001592 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001593 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001594 break;
1595 }
1596
1597 surface->type = SHELL_SURFACE_NONE;
1598 return 0;
1599}
1600
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001601static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001602set_surface_type(struct shell_surface *shsurf)
1603{
1604 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001605 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001606
1607 reset_shell_surface_type(shsurf);
1608
1609 shsurf->type = shsurf->next_type;
1610 shsurf->next_type = SHELL_SURFACE_NONE;
1611
1612 switch (shsurf->type) {
1613 case SHELL_SURFACE_TOPLEVEL:
1614 break;
1615 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001616 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001617 pes->geometry.x + shsurf->transient.x,
1618 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001619 break;
1620
1621 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001622 case SHELL_SURFACE_FULLSCREEN:
1623 shsurf->saved_x = surface->geometry.x;
1624 shsurf->saved_y = surface->geometry.y;
1625 shsurf->saved_position_valid = true;
1626
1627 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1628 wl_list_remove(&shsurf->rotation.transform.link);
1629 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001630 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001631 shsurf->saved_rotation_valid = true;
1632 }
1633 break;
1634
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001635 case SHELL_SURFACE_XWAYLAND:
1636 weston_surface_set_position(surface, shsurf->transient.x,
1637 shsurf->transient.y);
1638 break;
1639
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001640 default:
1641 break;
1642 }
1643}
1644
1645static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001646set_toplevel(struct shell_surface *shsurf)
1647{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001648 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001649}
1650
1651static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001652shell_surface_set_toplevel(struct wl_client *client,
1653 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001654{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001655 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001656
Tiago Vignattibc052c92012-04-19 16:18:18 +03001657 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001658}
1659
1660static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001661set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001662 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001663{
1664 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001665 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001666 shsurf->transient.x = x;
1667 shsurf->transient.y = y;
1668 shsurf->transient.flags = flags;
1669 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1670}
1671
1672static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001673shell_surface_set_transient(struct wl_client *client,
1674 struct wl_resource *resource,
1675 struct wl_resource *parent_resource,
1676 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001677{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001678 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001679 struct weston_surface *parent =
1680 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001681
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001682 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001683}
1684
Tiago Vignattibe143262012-04-16 17:31:41 +03001685static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001686shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001687{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001688 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001689}
1690
1691static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001692get_output_panel_height(struct desktop_shell *shell,
1693 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001694{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001695 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001696 int panel_height = 0;
1697
1698 if (!output)
1699 return 0;
1700
Juan Zhao4ab94682012-07-09 22:24:09 -07001701 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001702 if (surface->output == output) {
1703 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001704 break;
1705 }
1706 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001707
Juan Zhao96879df2012-02-07 08:45:41 +08001708 return panel_height;
1709}
1710
1711static void
1712shell_surface_set_maximized(struct wl_client *client,
1713 struct wl_resource *resource,
1714 struct wl_resource *output_resource )
1715{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001716 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001717 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001718 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001719 uint32_t edges = 0, panel_height = 0;
1720
1721 /* get the default output, if the client set it as NULL
1722 check whether the ouput is available */
1723 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001724 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001725 else if (es->output)
1726 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001727 else
1728 shsurf->output = get_default_output(es->compositor);
1729
Tiago Vignattibe143262012-04-16 17:31:41 +03001730 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001731 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001732 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001733
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001734 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001735 shsurf->output->width,
1736 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001737
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001738 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001739}
1740
Alex Wu21858432012-04-01 20:13:08 +08001741static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001742black_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 +08001743
Alex Wu4539b082012-03-01 12:57:46 +08001744static struct weston_surface *
1745create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001746 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001747 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001748{
1749 struct weston_surface *surface = NULL;
1750
1751 surface = weston_surface_create(ec);
1752 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001753 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001754 return NULL;
1755 }
1756
Alex Wu21858432012-04-01 20:13:08 +08001757 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001758 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001759 weston_surface_configure(surface, x, y, w, h);
1760 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001761 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001762 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001763 pixman_region32_fini(&surface->input);
1764 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001765
Alex Wu4539b082012-03-01 12:57:46 +08001766 return surface;
1767}
1768
1769/* Create black surface and append it to the associated fullscreen surface.
1770 * Handle size dismatch and positioning according to the method. */
1771static void
1772shell_configure_fullscreen(struct shell_surface *shsurf)
1773{
1774 struct weston_output *output = shsurf->fullscreen_output;
1775 struct weston_surface *surface = shsurf->surface;
1776 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001777 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001778 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001779
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001780 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1781 restore_output_mode(output);
1782
Alex Wu4539b082012-03-01 12:57:46 +08001783 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001784 shsurf->fullscreen.black_surface =
1785 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001786 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001787 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001788 output->width,
1789 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001790
1791 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1792 wl_list_insert(&surface->layer_link,
1793 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001794 shsurf->fullscreen.black_surface->output = output;
1795
Giulio Camuffob8366642013-04-25 13:57:46 +03001796 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1797 &surf_width, &surf_height);
1798
Alex Wu4539b082012-03-01 12:57:46 +08001799 switch (shsurf->fullscreen.type) {
1800 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001801 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001802 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001803 break;
1804 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001805 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001806 if (output->width == surf_width &&
1807 output->height == surf_height) {
1808 weston_surface_set_position(surface, output->x - surf_x,
1809 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001810 break;
1811 }
1812
Alex Wu4539b082012-03-01 12:57:46 +08001813 matrix = &shsurf->fullscreen.transform.matrix;
1814 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001815
Scott Moreau1bad5db2012-08-18 01:04:05 -06001816 output_aspect = (float) output->width /
1817 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001818 surface_aspect = (float) surface->geometry.width /
1819 (float) surface->geometry.height;
1820 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001821 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001822 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001823 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001824 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001825 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001826
Alex Wu4539b082012-03-01 12:57:46 +08001827 weston_matrix_scale(matrix, scale, scale, 1);
1828 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001829 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001830 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001831 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1832 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001833 weston_surface_set_position(surface, x, y);
1834
Alex Wu4539b082012-03-01 12:57:46 +08001835 break;
1836 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001837 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001838 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001839 surf_width * surface->buffer_scale,
1840 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001841 shsurf->fullscreen.framerate};
1842
Alexander Larsson355748e2013-05-28 16:23:38 +02001843 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001844 weston_surface_set_position(surface,
1845 output->x - surf_x,
1846 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001847 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001848 output->x - surf_x,
1849 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001850 output->width,
1851 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001852 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001853 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001854 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001855 center_on_output(surface, output);
1856 }
Alex Wubd3354b2012-04-17 17:20:49 +08001857 }
Alex Wu4539b082012-03-01 12:57:46 +08001858 break;
1859 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001860 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001861 break;
1862 default:
1863 break;
1864 }
1865}
1866
1867/* make the fullscreen and black surface at the top */
1868static void
1869shell_stack_fullscreen(struct shell_surface *shsurf)
1870{
Alex Wubd3354b2012-04-17 17:20:49 +08001871 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001872 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001873 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001874
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001875 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001876 wl_list_insert(&shell->fullscreen_layer.surface_list,
1877 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001878 weston_surface_damage(surface);
1879
1880 if (!shsurf->fullscreen.black_surface)
1881 shsurf->fullscreen.black_surface =
1882 create_black_surface(surface->compositor,
1883 surface,
1884 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001885 output->width,
1886 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001887
1888 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001889 wl_list_insert(&surface->layer_link,
1890 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001891 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001892}
1893
1894static void
1895shell_map_fullscreen(struct shell_surface *shsurf)
1896{
Alex Wu4539b082012-03-01 12:57:46 +08001897 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001898 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001899}
1900
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001901static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001902set_fullscreen(struct shell_surface *shsurf,
1903 uint32_t method,
1904 uint32_t framerate,
1905 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001906{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001907 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001908
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001909 if (output)
1910 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001911 else if (es->output)
1912 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001913 else
1914 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001915
Alex Wu4539b082012-03-01 12:57:46 +08001916 shsurf->fullscreen_output = shsurf->output;
1917 shsurf->fullscreen.type = method;
1918 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001919 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001920
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001921 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001922 shsurf->output->width,
1923 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001924}
1925
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001926static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001927shell_surface_set_fullscreen(struct wl_client *client,
1928 struct wl_resource *resource,
1929 uint32_t method,
1930 uint32_t framerate,
1931 struct wl_resource *output_resource)
1932{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001933 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001934 struct weston_output *output;
1935
1936 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001937 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001938 else
1939 output = NULL;
1940
1941 set_fullscreen(shsurf, method, framerate, output);
1942}
1943
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001944static void
1945set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1946{
1947 /* XXX: using the same fields for transient type */
1948 shsurf->transient.x = x;
1949 shsurf->transient.y = y;
1950 shsurf->transient.flags = flags;
1951 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1952}
1953
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001954static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001955
1956static void
1957destroy_shell_seat(struct wl_listener *listener, void *data)
1958{
1959 struct shell_seat *shseat =
1960 container_of(listener,
1961 struct shell_seat, seat_destroy_listener);
1962 struct shell_surface *shsurf, *prev = NULL;
1963
1964 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001965 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001966 shseat->popup_grab.client = NULL;
1967
1968 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1969 shsurf->popup.shseat = NULL;
1970 if (prev) {
1971 wl_list_init(&prev->popup.grab_link);
1972 }
1973 prev = shsurf;
1974 }
1975 wl_list_init(&prev->popup.grab_link);
1976 }
1977
1978 wl_list_remove(&shseat->seat_destroy_listener.link);
1979 free(shseat);
1980}
1981
1982static struct shell_seat *
1983create_shell_seat(struct weston_seat *seat)
1984{
1985 struct shell_seat *shseat;
1986
1987 shseat = calloc(1, sizeof *shseat);
1988 if (!shseat) {
1989 weston_log("no memory to allocate shell seat\n");
1990 return NULL;
1991 }
1992
1993 shseat->seat = seat;
1994 wl_list_init(&shseat->popup_grab.surfaces_list);
1995
1996 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1997 wl_signal_add(&seat->destroy_signal,
1998 &shseat->seat_destroy_listener);
1999
2000 return shseat;
2001}
2002
2003static struct shell_seat *
2004get_shell_seat(struct weston_seat *seat)
2005{
2006 struct wl_listener *listener;
2007
2008 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2009 if (listener == NULL)
2010 return create_shell_seat(seat);
2011
2012 return container_of(listener,
2013 struct shell_seat, seat_destroy_listener);
2014}
2015
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002016static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002017popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002018{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002019 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002020 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002021 struct shell_seat *shseat =
2022 container_of(grab, struct shell_seat, popup_grab.grab);
2023 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002024 wl_fixed_t sx, sy;
2025
2026 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2027 pointer->x, pointer->y,
2028 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002029
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002030 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002031 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002032 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002033 weston_pointer_set_focus(pointer, NULL,
2034 wl_fixed_from_int(0),
2035 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002036 }
2037}
2038
2039static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002040popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002041{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002042 struct weston_pointer *pointer = grab->pointer;
2043 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002044
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002045 if (pointer->focus_resource) {
2046 weston_surface_from_global_fixed(pointer->focus,
2047 pointer->x, pointer->y,
2048 &sx, &sy);
2049 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2050 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002051}
2052
2053static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002054popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002055 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002056{
2057 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002058 struct shell_seat *shseat =
2059 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002060 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002061 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002062 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002063
Daniel Stone37816df2012-05-16 18:45:18 +01002064 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002065 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002066 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002067 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002068 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002069 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002070 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002071 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002072 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002073 }
2074
Daniel Stone4dbadb12012-05-30 16:31:51 +01002075 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002076 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002077}
2078
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002079static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002080 popup_grab_focus,
2081 popup_grab_motion,
2082 popup_grab_button,
2083};
2084
2085static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002086popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002087{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002088 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002089 struct shell_seat *shseat =
2090 container_of(grab, struct shell_seat, popup_grab.grab);
2091 struct shell_surface *shsurf;
2092 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002093
2094 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002095 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002096 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002097 shseat->popup_grab.grab.interface = NULL;
2098 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002099 /* Send the popup_done event to all the popups open */
2100 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002101 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002102 shsurf->popup.shseat = NULL;
2103 if (prev) {
2104 wl_list_init(&prev->popup.grab_link);
2105 }
2106 prev = shsurf;
2107 }
2108 wl_list_init(&prev->popup.grab_link);
2109 wl_list_init(&shseat->popup_grab.surfaces_list);
2110 }
2111}
2112
2113static void
2114add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2115{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002116 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002117
2118 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002119 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002120 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002121 /* We must make sure here that this popup was opened after
2122 * a mouse press, and not just by moving around with other
2123 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002124 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002125 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002126
Rob Bradforddfe31052013-06-26 19:49:11 +01002127 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002128 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002129 } else {
2130 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002131 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002132}
2133
2134static void
2135remove_popup_grab(struct shell_surface *shsurf)
2136{
2137 struct shell_seat *shseat = shsurf->popup.shseat;
2138
2139 wl_list_remove(&shsurf->popup.grab_link);
2140 wl_list_init(&shsurf->popup.grab_link);
2141 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002142 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002143 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002144 }
2145}
2146
2147static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002148shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002149{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002150 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002151 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002152 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002153
2154 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002155
Pekka Paalanen483243f2013-03-08 14:56:50 +02002156 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002157 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2158 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002159
Kristian Høgsberge3148752013-05-06 23:19:49 -04002160 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002161 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002162 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002163 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002164 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002165 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002166}
2167
2168static void
2169shell_surface_set_popup(struct wl_client *client,
2170 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002171 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002172 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002173 struct wl_resource *parent_resource,
2174 int32_t x, int32_t y, uint32_t flags)
2175{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002176 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002177
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002178 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002179 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002180 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002181 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002182 shsurf->popup.x = x;
2183 shsurf->popup.y = y;
2184}
2185
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002186static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002187 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002188 shell_surface_move,
2189 shell_surface_resize,
2190 shell_surface_set_toplevel,
2191 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002192 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002193 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002194 shell_surface_set_maximized,
2195 shell_surface_set_title,
2196 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002197};
2198
2199static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002200destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002201{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002202 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2203
Giulio Camuffo5085a752013-03-25 21:42:45 +01002204 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2205 remove_popup_grab(shsurf);
2206 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002207
Alex Wubd3354b2012-04-17 17:20:49 +08002208 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002209 shell_surface_is_top_fullscreen(shsurf))
2210 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002211
Alex Wuaa08e2d2012-03-05 11:01:40 +08002212 if (shsurf->fullscreen.black_surface)
2213 weston_surface_destroy(shsurf->fullscreen.black_surface);
2214
Alex Wubd3354b2012-04-17 17:20:49 +08002215 /* As destroy_resource() use wl_list_for_each_safe(),
2216 * we can always remove the listener.
2217 */
2218 wl_list_remove(&shsurf->surface_destroy_listener.link);
2219 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002220 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002221 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002222
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002223 wl_list_remove(&shsurf->link);
2224 free(shsurf);
2225}
2226
2227static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002228shell_destroy_shell_surface(struct wl_resource *resource)
2229{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002230 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002231
2232 destroy_shell_surface(shsurf);
2233}
2234
2235static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002236shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002237{
2238 struct shell_surface *shsurf = container_of(listener,
2239 struct shell_surface,
2240 surface_destroy_listener);
2241
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002242 if (shsurf->resource)
2243 wl_resource_destroy(shsurf->resource);
2244 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002245 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002246}
2247
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002248static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002249shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002250
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002251static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002252get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002253{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002254 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002255 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002256 else
2257 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002258}
2259
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002260static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002261create_shell_surface(void *shell, struct weston_surface *surface,
2262 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002263{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002264 struct shell_surface *shsurf;
2265
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002266 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002267 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002268 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002269 }
2270
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002271 shsurf = calloc(1, sizeof *shsurf);
2272 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002273 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002274 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002275 }
2276
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002277 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002278 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002279
Tiago Vignattibc052c92012-04-19 16:18:18 +03002280 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002281 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002282 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002283 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002284 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002285 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2286 shsurf->fullscreen.framerate = 0;
2287 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002288 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002289 wl_list_init(&shsurf->fullscreen.transform.link);
2290
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002291 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002292 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002293 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002294 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002295
2296 /* init link so its safe to always remove it in destroy_shell_surface */
2297 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002298 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002299
Pekka Paalanen460099f2012-01-20 16:48:25 +02002300 /* empty when not in use */
2301 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002302 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002303
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002304 wl_list_init(&shsurf->workspace_transform.link);
2305
Pekka Paalanen98262232011-12-01 10:42:22 +02002306 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002307 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002308
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002309 shsurf->client = client;
2310
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002311 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002312}
2313
2314static void
2315shell_get_shell_surface(struct wl_client *client,
2316 struct wl_resource *resource,
2317 uint32_t id,
2318 struct wl_resource *surface_resource)
2319{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002320 struct weston_surface *surface =
2321 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002322 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002323 struct shell_surface *shsurf;
2324
2325 if (get_shell_surface(surface)) {
2326 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002327 WL_DISPLAY_ERROR_INVALID_OBJECT,
2328 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002329 return;
2330 }
2331
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002332 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002333 if (!shsurf) {
2334 wl_resource_post_error(surface_resource,
2335 WL_DISPLAY_ERROR_INVALID_OBJECT,
2336 "surface->configure already set");
2337 return;
2338 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002339
Jason Ekstranda85118c2013-06-27 20:17:02 -05002340 shsurf->resource =
2341 wl_resource_create(client,
2342 &wl_shell_surface_interface, 1, id);
2343 wl_resource_set_implementation(shsurf->resource,
2344 &shell_surface_implementation,
2345 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002346}
2347
2348static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002349 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002350};
2351
Kristian Høgsberg07937562011-04-12 17:25:42 -04002352static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002353shell_fade(struct desktop_shell *shell, enum fade_type type);
2354
2355static int
2356screensaver_timeout(void *data)
2357{
2358 struct desktop_shell *shell = data;
2359
2360 shell_fade(shell, FADE_OUT);
2361
2362 return 1;
2363}
2364
2365static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002366handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002367{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002368 struct desktop_shell *shell =
2369 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002370
Pekka Paalanen18027e52011-12-02 16:31:49 +02002371 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002372
2373 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002374 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002375}
2376
2377static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002378launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002379{
2380 if (shell->screensaver.binding)
2381 return;
2382
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002383 if (!shell->screensaver.path) {
2384 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002385 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002386 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002387
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002388 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002389 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002390 return;
2391 }
2392
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002393 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002394 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002395 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002396 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002397}
2398
2399static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002400terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002401{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002402 if (shell->screensaver.process.pid == 0)
2403 return;
2404
2405 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002406}
2407
2408static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002409configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002410{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002411 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002412
Giulio Camuffo184df502013-02-21 11:29:21 +01002413 if (width == 0)
2414 return;
2415
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002416 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2417 if (s->output == es->output && s != es) {
2418 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002419 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002420 }
2421 }
2422
Giulio Camuffo184df502013-02-21 11:29:21 +01002423 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002424
2425 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002426 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002427 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002428 }
2429}
2430
2431static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002432background_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 -04002433{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002434 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002435
Giulio Camuffo184df502013-02-21 11:29:21 +01002436 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002437}
2438
2439static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002440desktop_shell_set_background(struct wl_client *client,
2441 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002442 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002443 struct wl_resource *surface_resource)
2444{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002445 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002446 struct weston_surface *surface =
2447 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002448
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002449 if (surface->configure) {
2450 wl_resource_post_error(surface_resource,
2451 WL_DISPLAY_ERROR_INVALID_OBJECT,
2452 "surface role already assigned");
2453 return;
2454 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002455
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002456 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002457 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002458 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002459 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002460 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002461 surface->output->width,
2462 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002463}
2464
2465static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002466panel_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 -04002467{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002468 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002469
Giulio Camuffo184df502013-02-21 11:29:21 +01002470 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002471}
2472
2473static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002474desktop_shell_set_panel(struct wl_client *client,
2475 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002476 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002477 struct wl_resource *surface_resource)
2478{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002479 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002480 struct weston_surface *surface =
2481 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002482
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002483 if (surface->configure) {
2484 wl_resource_post_error(surface_resource,
2485 WL_DISPLAY_ERROR_INVALID_OBJECT,
2486 "surface role already assigned");
2487 return;
2488 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002489
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002490 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002491 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002492 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002493 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002494 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002495 surface->output->width,
2496 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002497}
2498
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002499static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002500lock_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 -04002501{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002502 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002503
Giulio Camuffo184df502013-02-21 11:29:21 +01002504 if (width == 0)
2505 return;
2506
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002507 surface->geometry.width = width;
2508 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002509 center_on_output(surface, get_default_output(shell->compositor));
2510
2511 if (!weston_surface_is_mapped(surface)) {
2512 wl_list_insert(&shell->lock_layer.surface_list,
2513 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002514 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002515 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002516 }
2517}
2518
2519static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002520handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002521{
Tiago Vignattibe143262012-04-16 17:31:41 +03002522 struct desktop_shell *shell =
2523 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002524
Martin Minarik6d118362012-06-07 18:01:59 +02002525 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002526 shell->lock_surface = NULL;
2527}
2528
2529static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002530desktop_shell_set_lock_surface(struct wl_client *client,
2531 struct wl_resource *resource,
2532 struct wl_resource *surface_resource)
2533{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002534 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002535 struct weston_surface *surface =
2536 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002537
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002538 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002539
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002540 if (!shell->locked)
2541 return;
2542
Pekka Paalanen98262232011-12-01 10:42:22 +02002543 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002544
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002545 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002546 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002547 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002548
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002549 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002550 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002551}
2552
2553static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002554resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002555{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002556 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002557
Pekka Paalanen77346a62011-11-30 16:26:35 +02002558 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002559
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002560 wl_list_remove(&shell->lock_layer.link);
2561 wl_list_insert(&shell->compositor->cursor_layer.link,
2562 &shell->fullscreen_layer.link);
2563 wl_list_insert(&shell->fullscreen_layer.link,
2564 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002565 if (shell->showing_input_panels) {
2566 wl_list_insert(&shell->panel_layer.link,
2567 &shell->input_panel_layer.link);
2568 wl_list_insert(&shell->input_panel_layer.link,
2569 &ws->layer.link);
2570 } else {
2571 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2572 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002573
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002574 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002575
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002576 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002577 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002578 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002579}
2580
2581static void
2582desktop_shell_unlock(struct wl_client *client,
2583 struct wl_resource *resource)
2584{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002585 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002586
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002587 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002588
2589 if (shell->locked)
2590 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002591}
2592
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002593static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002594desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002595 struct wl_resource *resource,
2596 struct wl_resource *surface_resource)
2597{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002598 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002599
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002600 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002601}
2602
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002603static void
2604desktop_shell_desktop_ready(struct wl_client *client,
2605 struct wl_resource *resource)
2606{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002607 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002608
2609 shell_fade_startup(shell);
2610}
2611
Kristian Høgsberg75840622011-09-06 13:48:16 -04002612static const struct desktop_shell_interface desktop_shell_implementation = {
2613 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002614 desktop_shell_set_panel,
2615 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002616 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002617 desktop_shell_set_grab_surface,
2618 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002619};
2620
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002621static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002622get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002623{
2624 struct shell_surface *shsurf;
2625
2626 shsurf = get_shell_surface(surface);
2627 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002628 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002629 return shsurf->type;
2630}
2631
Kristian Høgsberg75840622011-09-06 13:48:16 -04002632static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002633move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002634{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002635 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002636 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002637 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002638 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002639
Pekka Paalanen01388e22013-04-25 13:57:44 +03002640 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002641 if (surface == NULL)
2642 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002643
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002644 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002645 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2646 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002647 return;
2648
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002649 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002650}
2651
2652static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002653resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002654{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002655 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002656 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002657 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002658 uint32_t edges = 0;
2659 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002660 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002661
Pekka Paalanen01388e22013-04-25 13:57:44 +03002662 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002663 if (surface == NULL)
2664 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002665
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002666 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002667 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2668 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002669 return;
2670
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002671 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002672 wl_fixed_to_int(seat->pointer->grab_x),
2673 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002674 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002675
Pekka Paalanen60921e52012-01-25 15:55:43 +02002676 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002677 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002678 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002679 edges |= 0;
2680 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002681 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002682
Pekka Paalanen60921e52012-01-25 15:55:43 +02002683 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002684 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002685 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002686 edges |= 0;
2687 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002688 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002689
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002690 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002691}
2692
2693static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002694surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002695 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002696{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002697 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002698 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002699 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002700 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002701 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002702
Pekka Paalanen01388e22013-04-25 13:57:44 +03002703 /* XXX: broken for windows containing sub-surfaces */
2704 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002705 if (surface == NULL)
2706 return;
2707
2708 shsurf = get_shell_surface(surface);
2709 if (!shsurf)
2710 return;
2711
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002712 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002713
Scott Moreau02709af2012-05-22 01:54:10 -06002714 if (surface->alpha > 1.0)
2715 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002716 if (surface->alpha < step)
2717 surface->alpha = step;
2718
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002719 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002720 weston_surface_damage(surface);
2721}
2722
2723static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002724do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002725 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002726{
Daniel Stone37816df2012-05-16 18:45:18 +01002727 struct weston_seat *ws = (struct weston_seat *) seat;
2728 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002729 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002730 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002731
2732 wl_list_for_each(output, &compositor->output_list, link) {
2733 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002734 wl_fixed_to_double(seat->pointer->x),
2735 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002736 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002737 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002738 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002739 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002740 increment = -output->zoom.increment;
2741 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002742 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002743 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002744 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002745 else
2746 increment = 0;
2747
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002748 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002749
Scott Moreaue6603982012-06-11 13:07:51 -06002750 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002751 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002752 else if (output->zoom.level > output->zoom.max_level)
2753 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002754 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002755 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002756 output->disable_planes++;
2757 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002758
Scott Moreaue6603982012-06-11 13:07:51 -06002759 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002760
Scott Moreau8dacaab2012-06-17 18:10:58 -06002761 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002762 }
2763 }
2764}
2765
Scott Moreauccbf29d2012-02-22 14:21:41 -07002766static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002767zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002768 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002769{
2770 do_zoom(seat, time, 0, axis, value);
2771}
2772
2773static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002774zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002775 void *data)
2776{
2777 do_zoom(seat, time, key, 0, 0);
2778}
2779
2780static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002781terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002782 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002783{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002784 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002785
Daniel Stone325fc2d2012-05-30 16:31:58 +01002786 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002787}
2788
2789static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002790rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002791{
2792 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002793 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002794 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002795 struct shell_surface *shsurf = rotate->base.shsurf;
2796 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002797 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002798
2799 if (!shsurf)
2800 return;
2801
2802 surface = shsurf->surface;
2803
2804 cx = 0.5f * surface->geometry.width;
2805 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002806
Daniel Stone37816df2012-05-16 18:45:18 +01002807 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2808 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002809 r = sqrtf(dx * dx + dy * dy);
2810
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002811 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002812 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002813
2814 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002815 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002816 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002817
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002818 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002819 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002820
2821 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002822 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002823 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002824 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002825 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002826
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002827 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002828 &shsurf->surface->geometry.transformation_list,
2829 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002830 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002831 wl_list_init(&shsurf->rotation.transform.link);
2832 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002833 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002834 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002835
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002836 /* We need to adjust the position of the surface
2837 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002838 cposx = surface->geometry.x + cx;
2839 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002840 dposx = rotate->center.x - cposx;
2841 dposy = rotate->center.y - cposy;
2842 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002843 weston_surface_set_position(surface,
2844 surface->geometry.x + dposx,
2845 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002846 }
2847
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002848 /* Repaint implies weston_surface_update_transform(), which
2849 * lazily applies the damage due to rotation update.
2850 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002851 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002852}
2853
2854static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002855rotate_grab_button(struct weston_pointer_grab *grab,
2856 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002857{
2858 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002859 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002860 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002861 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002862 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002863
Daniel Stone4dbadb12012-05-30 16:31:51 +01002864 if (pointer->button_count == 0 &&
2865 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002866 if (shsurf)
2867 weston_matrix_multiply(&shsurf->rotation.rotation,
2868 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002869 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002870 free(rotate);
2871 }
2872}
2873
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002874static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002875 noop_grab_focus,
2876 rotate_grab_motion,
2877 rotate_grab_button,
2878};
2879
2880static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002881surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002882{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002883 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002884 float dx, dy;
2885 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002886
Pekka Paalanen460099f2012-01-20 16:48:25 +02002887 rotate = malloc(sizeof *rotate);
2888 if (!rotate)
2889 return;
2890
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002891 weston_surface_to_global_float(surface->surface,
Louis-Francis Ratté-Boulianne9a4f10f2013-07-03 15:58:22 +02002892 surface->surface->geometry.width * 0.5f,
2893 surface->surface->geometry.height * 0.5f,
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002894 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002895
Daniel Stone37816df2012-05-16 18:45:18 +01002896 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2897 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002898 r = sqrtf(dx * dx + dy * dy);
2899 if (r > 20.0f) {
2900 struct weston_matrix inverse;
2901
2902 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002903 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002904 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002905
2906 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002907 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002908 } else {
2909 weston_matrix_init(&surface->rotation.rotation);
2910 weston_matrix_init(&rotate->rotation);
2911 }
2912
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002913 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2914 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002915}
2916
2917static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002918rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002919 void *data)
2920{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002921 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002922 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002923 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002924 struct shell_surface *surface;
2925
Pekka Paalanen01388e22013-04-25 13:57:44 +03002926 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002927 if (base_surface == NULL)
2928 return;
2929
2930 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002931 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2932 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002933 return;
2934
2935 surface_rotate(surface, seat);
2936}
2937
2938static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002939lower_fullscreen_layer(struct desktop_shell *shell)
2940{
2941 struct workspace *ws;
2942 struct weston_surface *surface, *prev;
2943
2944 ws = get_current_workspace(shell);
2945 wl_list_for_each_reverse_safe(surface, prev,
2946 &shell->fullscreen_layer.surface_list,
2947 layer_link)
2948 weston_surface_restack(surface, &ws->layer.surface_list);
2949}
2950
2951static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002952activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002953 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002954{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002955 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002956 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002957 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002958
Pekka Paalanen01388e22013-04-25 13:57:44 +03002959 main_surface = weston_surface_get_main_surface(es);
2960
Daniel Stone37816df2012-05-16 18:45:18 +01002961 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002962
Jonas Ådahl8538b222012-08-29 22:13:03 +02002963 state = ensure_focus_state(shell, seat);
2964 if (state == NULL)
2965 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002966
2967 state->keyboard_focus = es;
2968 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002969 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002970
Pekka Paalanen01388e22013-04-25 13:57:44 +03002971 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002972 case SHELL_SURFACE_FULLSCREEN:
2973 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002974 shell_stack_fullscreen(get_shell_surface(main_surface));
2975 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002976 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002977 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002978 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002979 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002980 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002981 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002982 }
2983}
2984
Alex Wu21858432012-04-01 20:13:08 +08002985/* no-op func for checking black surface */
2986static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002987black_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 +08002988{
2989}
2990
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002991static bool
Alex Wu21858432012-04-01 20:13:08 +08002992is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2993{
2994 if (es->configure == black_surface_configure) {
2995 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002996 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002997 return true;
2998 }
2999 return false;
3000}
3001
Kristian Høgsberg75840622011-09-06 13:48:16 -04003002static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003003click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003004 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003005{
Daniel Stone37816df2012-05-16 18:45:18 +01003006 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003007 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003008 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003009 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003010
Daniel Stone37816df2012-05-16 18:45:18 +01003011 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003012 if (!focus)
3013 return;
3014
Pekka Paalanen01388e22013-04-25 13:57:44 +03003015 if (is_black_surface(focus, &main_surface))
3016 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003017
Pekka Paalanen01388e22013-04-25 13:57:44 +03003018 main_surface = weston_surface_get_main_surface(focus);
3019 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003020 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003021
Daniel Stone325fc2d2012-05-30 16:31:58 +01003022 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003023 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003024}
3025
3026static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003027lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003028{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003029 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003030
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003031 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003032 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003033 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003034 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003035
3036 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003037
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003038 /* Hide all surfaces by removing the fullscreen, panel and
3039 * toplevel layers. This way nothing else can show or receive
3040 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003041
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003042 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003043 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003044 if (shell->showing_input_panels)
3045 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003046 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003047 wl_list_insert(&shell->compositor->cursor_layer.link,
3048 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003049
Pekka Paalanen77346a62011-11-30 16:26:35 +02003050 launch_screensaver(shell);
3051
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003052 /* TODO: disable bindings that should not work while locked. */
3053
3054 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003055}
3056
3057static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003058unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003059{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003060 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003061 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003062 return;
3063 }
3064
3065 /* If desktop-shell client has gone away, unlock immediately. */
3066 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003067 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003068 return;
3069 }
3070
3071 if (shell->prepare_event_sent)
3072 return;
3073
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003074 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003075 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003076}
3077
3078static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003079shell_fade_done(struct weston_surface_animation *animation, void *data)
3080{
3081 struct desktop_shell *shell = data;
3082
3083 shell->fade.animation = NULL;
3084
3085 switch (shell->fade.type) {
3086 case FADE_IN:
3087 weston_surface_destroy(shell->fade.surface);
3088 shell->fade.surface = NULL;
3089 break;
3090 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003091 lock(shell);
3092 break;
3093 }
3094}
3095
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003096static struct weston_surface *
3097shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003098{
3099 struct weston_compositor *compositor = shell->compositor;
3100 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003101
3102 surface = weston_surface_create(compositor);
3103 if (!surface)
3104 return NULL;
3105
3106 weston_surface_configure(surface, 0, 0, 8192, 8192);
3107 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3108 wl_list_insert(&compositor->fade_layer.surface_list,
3109 &surface->layer_link);
3110 pixman_region32_init(&surface->input);
3111
3112 return surface;
3113}
3114
3115static void
3116shell_fade(struct desktop_shell *shell, enum fade_type type)
3117{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003118 float tint;
3119
3120 switch (type) {
3121 case FADE_IN:
3122 tint = 0.0;
3123 break;
3124 case FADE_OUT:
3125 tint = 1.0;
3126 break;
3127 default:
3128 weston_log("shell: invalid fade type\n");
3129 return;
3130 }
3131
3132 shell->fade.type = type;
3133
3134 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003135 shell->fade.surface = shell_fade_create_surface(shell);
3136 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003137 return;
3138
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003139 shell->fade.surface->alpha = 1.0 - tint;
3140 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003141 }
3142
3143 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003144 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003145 else
3146 shell->fade.animation =
3147 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003148 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003149 shell_fade_done, shell);
3150}
3151
3152static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003153do_shell_fade_startup(void *data)
3154{
3155 struct desktop_shell *shell = data;
3156
3157 shell_fade(shell, FADE_IN);
3158}
3159
3160static void
3161shell_fade_startup(struct desktop_shell *shell)
3162{
3163 struct wl_event_loop *loop;
3164
3165 if (!shell->fade.startup_timer)
3166 return;
3167
3168 wl_event_source_remove(shell->fade.startup_timer);
3169 shell->fade.startup_timer = NULL;
3170
3171 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3172 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3173}
3174
3175static int
3176fade_startup_timeout(void *data)
3177{
3178 struct desktop_shell *shell = data;
3179
3180 shell_fade_startup(shell);
3181 return 0;
3182}
3183
3184static void
3185shell_fade_init(struct desktop_shell *shell)
3186{
3187 /* Make compositor output all black, and wait for the desktop-shell
3188 * client to signal it is ready, then fade in. The timer triggers a
3189 * fade-in, in case the desktop-shell client takes too long.
3190 */
3191
3192 struct wl_event_loop *loop;
3193
3194 if (shell->fade.surface != NULL) {
3195 weston_log("%s: warning: fade surface already exists\n",
3196 __func__);
3197 return;
3198 }
3199
3200 shell->fade.surface = shell_fade_create_surface(shell);
3201 if (!shell->fade.surface)
3202 return;
3203
3204 weston_surface_update_transform(shell->fade.surface);
3205 weston_surface_damage(shell->fade.surface);
3206
3207 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3208 shell->fade.startup_timer =
3209 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3210 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3211}
3212
3213static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003214idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003215{
3216 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003217 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003218
3219 shell_fade(shell, FADE_OUT);
3220 /* lock() is called from shell_fade_done() */
3221}
3222
3223static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003224wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003225{
3226 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003227 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003228
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003229 unlock(shell);
3230}
3231
3232static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003233show_input_panels(struct wl_listener *listener, void *data)
3234{
3235 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003236 container_of(listener, struct desktop_shell,
3237 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003238 struct input_panel_surface *surface, *next;
3239 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003240
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003241 shell->text_input.surface = (struct weston_surface*)data;
3242
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003243 if (shell->showing_input_panels)
3244 return;
3245
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003246 shell->showing_input_panels = true;
3247
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003248 if (!shell->locked)
3249 wl_list_insert(&shell->panel_layer.link,
3250 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003251
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003252 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003253 &shell->input_panel.surfaces, link) {
3254 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003255 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003256 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003257 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003258 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003259 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003260 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003261 weston_surface_damage(ws);
3262 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003263 }
3264}
3265
3266static void
3267hide_input_panels(struct wl_listener *listener, void *data)
3268{
3269 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003270 container_of(listener, struct desktop_shell,
3271 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003272 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003273
Jan Arne Petersen61381972013-01-31 15:52:21 +01003274 if (!shell->showing_input_panels)
3275 return;
3276
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003277 shell->showing_input_panels = false;
3278
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003279 if (!shell->locked)
3280 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003281
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003282 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003283 &shell->input_panel_layer.surface_list, layer_link)
3284 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003285}
3286
3287static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003288update_input_panels(struct wl_listener *listener, void *data)
3289{
3290 struct desktop_shell *shell =
3291 container_of(listener, struct desktop_shell,
3292 update_input_panel_listener);
3293
3294 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3295}
3296
3297static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003298center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003299{
Giulio Camuffob8366642013-04-25 13:57:46 +03003300 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003301 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003302
Giulio Camuffob8366642013-04-25 13:57:46 +03003303 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3304
3305 x = output->x + (output->width - width) / 2 - surf_x / 2;
3306 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003307
3308 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003309}
3310
3311static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003312weston_surface_set_initial_position (struct weston_surface *surface,
3313 struct desktop_shell *shell)
3314{
3315 struct weston_compositor *compositor = shell->compositor;
3316 int ix = 0, iy = 0;
3317 int range_x, range_y;
3318 int dx, dy, x, y, panel_height;
3319 struct weston_output *output, *target_output = NULL;
3320 struct weston_seat *seat;
3321
3322 /* As a heuristic place the new window on the same output as the
3323 * pointer. Falling back to the output containing 0, 0.
3324 *
3325 * TODO: Do something clever for touch too?
3326 */
3327 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003328 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003329 ix = wl_fixed_to_int(seat->pointer->x);
3330 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003331 break;
3332 }
3333 }
3334
3335 wl_list_for_each(output, &compositor->output_list, link) {
3336 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3337 target_output = output;
3338 break;
3339 }
3340 }
3341
3342 if (!target_output) {
3343 weston_surface_set_position(surface, 10 + random() % 400,
3344 10 + random() % 400);
3345 return;
3346 }
3347
3348 /* Valid range within output where the surface will still be onscreen.
3349 * If this is negative it means that the surface is bigger than
3350 * output.
3351 */
3352 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003353 range_x = target_output->width - surface->geometry.width;
3354 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003355 surface->geometry.height;
3356
Rob Bradford4cb88c72012-08-13 15:18:44 +01003357 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003358 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003359 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003360 dx = 0;
3361
3362 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003363 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003364 else
3365 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003366
3367 x = target_output->x + dx;
3368 y = target_output->y + dy;
3369
3370 weston_surface_set_position (surface, x, y);
3371}
3372
3373static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003374map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003375 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003376{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003377 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003378 struct shell_surface *shsurf = get_shell_surface(surface);
3379 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003380 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003381 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003382 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003383 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003384 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003385
Pekka Paalanen60921e52012-01-25 15:55:43 +02003386 surface->geometry.width = width;
3387 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003388 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003389
3390 /* initial positioning, see also configure() */
3391 switch (surface_type) {
3392 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003393 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003394 break;
Alex Wu4539b082012-03-01 12:57:46 +08003395 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003396 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003397 shell_map_fullscreen(shsurf);
3398 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003399 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003400 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003401 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003402 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3403 NULL, NULL);
3404 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3405 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003406 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003407 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003408 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003409 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003410 case SHELL_SURFACE_NONE:
3411 weston_surface_set_position(surface,
3412 surface->geometry.x + sx,
3413 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003414 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003415 default:
3416 ;
3417 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003418
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003419 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003420 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003421 case SHELL_SURFACE_POPUP:
3422 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003423 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003424 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3425 break;
Alex Wu4539b082012-03-01 12:57:46 +08003426 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003427 case SHELL_SURFACE_NONE:
3428 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003429 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003430 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003431 ws = get_current_workspace(shell);
3432 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003433 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003434 }
3435
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003436 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003437 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003438 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3439 surface->output = shsurf->output;
3440 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003441
Juan Zhao7bb92f02011-12-15 11:31:51 -05003442 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003443 /* XXX: xwayland's using the same fields for transient type */
3444 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003445 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003446 if (shsurf->transient.flags ==
3447 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3448 break;
3449 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003450 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003451 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003452 if (!shell->locked) {
3453 wl_list_for_each(seat, &compositor->seat_list, link)
3454 activate(shell, surface, seat);
3455 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003456 break;
3457 default:
3458 break;
3459 }
3460
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003461 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003462 {
3463 switch (shell->win_animation_type) {
3464 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003465 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003466 break;
3467 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003468 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003469 break;
3470 default:
3471 break;
3472 }
3473 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003474}
3475
3476static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003477configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003478 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003479{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003480 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3481 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003482 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003483
Pekka Paalanen77346a62011-11-30 16:26:35 +02003484 shsurf = get_shell_surface(surface);
3485 if (shsurf)
3486 surface_type = shsurf->type;
3487
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003488 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003489
3490 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003491 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003492 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003493 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003494 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003495 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003496 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003497 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3498 NULL, NULL);
3499 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003500 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003501 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003502 break;
Alex Wu4539b082012-03-01 12:57:46 +08003503 case SHELL_SURFACE_TOPLEVEL:
3504 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003505 default:
3506 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003507 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003508
Alex Wu4539b082012-03-01 12:57:46 +08003509 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003510 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003511 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003512
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003513 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003514 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003515 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003516}
3517
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003518static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003519shell_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 +03003520{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003521 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003522 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003523
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003524 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003525
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003526 if (!weston_surface_is_mapped(es) &&
3527 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003528 remove_popup_grab(shsurf);
3529 }
3530
Giulio Camuffo184df502013-02-21 11:29:21 +01003531 if (width == 0)
3532 return;
3533
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003534 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003535 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003536 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003537 type_changed = 1;
3538 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003539
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003540 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003541 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003542 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003543 es->geometry.width != width ||
3544 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003545 float from_x, from_y;
3546 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003547
3548 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3549 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3550 configure(shell, es,
3551 es->geometry.x + to_x - from_x,
3552 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003553 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003554 }
3555}
3556
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003557static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003558
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003559static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003560desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003561{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003562 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003563 struct desktop_shell *shell =
3564 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003565
3566 shell->child.process.pid = 0;
3567 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003568
3569 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3570 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003571 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003572 shell->child.deathstamp = time;
3573 shell->child.deathcount = 0;
3574 }
3575
3576 shell->child.deathcount++;
3577 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003578 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003579 return;
3580 }
3581
Martin Minarik6d118362012-06-07 18:01:59 +02003582 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003583 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003584 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003585}
3586
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003587static void
3588launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003589{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003590 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003591 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003592
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003593 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003594 &shell->child.process,
3595 shell_exe,
3596 desktop_shell_sigchld);
3597
3598 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003599 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003600}
3601
3602static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003603bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3604{
Tiago Vignattibe143262012-04-16 17:31:41 +03003605 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003606 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003607
Jason Ekstranda85118c2013-06-27 20:17:02 -05003608 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3609 if (resource)
3610 wl_resource_set_implementation(resource, &shell_implementation,
3611 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003612}
3613
Kristian Høgsberg75840622011-09-06 13:48:16 -04003614static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003615unbind_desktop_shell(struct wl_resource *resource)
3616{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003617 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003618
3619 if (shell->locked)
3620 resume_desktop(shell);
3621
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003622 shell->child.desktop_shell = NULL;
3623 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003624}
3625
3626static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003627bind_desktop_shell(struct wl_client *client,
3628 void *data, uint32_t version, uint32_t id)
3629{
Tiago Vignattibe143262012-04-16 17:31:41 +03003630 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003631 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003632
Jason Ekstranda85118c2013-06-27 20:17:02 -05003633 resource = wl_resource_create(client, &desktop_shell_interface,
3634 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003635
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003636 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003637 wl_resource_set_implementation(resource,
3638 &desktop_shell_implementation,
3639 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003640 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003641
3642 if (version < 2)
3643 shell_fade_startup(shell);
3644
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003645 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003646 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003647
3648 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3649 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003650 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003651}
3652
Pekka Paalanen6e168112011-11-24 11:34:05 +02003653static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003654screensaver_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 -04003655{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003656 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003657
Giulio Camuffo184df502013-02-21 11:29:21 +01003658 if (width == 0)
3659 return;
3660
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003661 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003662 if (!shell->locked)
3663 return;
3664
3665 center_on_output(surface, surface->output);
3666
3667 if (wl_list_empty(&surface->layer_link)) {
3668 wl_list_insert(shell->lock_layer.surface_list.prev,
3669 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003670 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003671 wl_event_source_timer_update(shell->screensaver.timer,
3672 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003673 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003674 }
3675}
3676
3677static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003678screensaver_set_surface(struct wl_client *client,
3679 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003680 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003681 struct wl_resource *output_resource)
3682{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003683 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003684 struct weston_surface *surface =
3685 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003686 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003687
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003688 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003689 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003690 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003691}
3692
3693static const struct screensaver_interface screensaver_implementation = {
3694 screensaver_set_surface
3695};
3696
3697static void
3698unbind_screensaver(struct wl_resource *resource)
3699{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003700 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003701
Pekka Paalanen77346a62011-11-30 16:26:35 +02003702 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003703}
3704
3705static void
3706bind_screensaver(struct wl_client *client,
3707 void *data, uint32_t version, uint32_t id)
3708{
Tiago Vignattibe143262012-04-16 17:31:41 +03003709 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003710 struct wl_resource *resource;
3711
Jason Ekstranda85118c2013-06-27 20:17:02 -05003712 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003713
Pekka Paalanen77346a62011-11-30 16:26:35 +02003714 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003715 wl_resource_set_implementation(resource,
3716 &screensaver_implementation,
3717 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003718 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003719 return;
3720 }
3721
3722 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3723 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003724 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003725}
3726
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003727static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003728input_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 -04003729{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003730 struct input_panel_surface *ip_surface = surface->configure_private;
3731 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003732 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003733 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003734
Giulio Camuffo184df502013-02-21 11:29:21 +01003735 if (width == 0)
3736 return;
3737
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003738 if (!weston_surface_is_mapped(surface)) {
3739 if (!shell->showing_input_panels)
3740 return;
3741
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003742 show_surface = 1;
3743 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003744
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003745 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003746
3747 if (ip_surface->panel) {
3748 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3749 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3750 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01003751 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
3752 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003753 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003754
3755 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003756 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003757 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003758
3759 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003760 wl_list_insert(&shell->input_panel_layer.surface_list,
3761 &surface->layer_link);
3762 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003763 weston_surface_damage(surface);
3764 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3765 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003766}
3767
3768static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003769destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003770{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003771 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3772
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003773 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003774 wl_list_remove(&input_panel_surface->link);
3775
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003776 input_panel_surface->surface->configure = NULL;
3777
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003778 free(input_panel_surface);
3779}
3780
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003781static struct input_panel_surface *
3782get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003783{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003784 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003785 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003786 } else {
3787 return NULL;
3788 }
3789}
3790
3791static void
3792input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3793{
3794 struct input_panel_surface *ipsurface = container_of(listener,
3795 struct input_panel_surface,
3796 surface_destroy_listener);
3797
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003798 if (ipsurface->resource) {
3799 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003800 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003801 destroy_input_panel_surface(ipsurface);
3802 }
3803}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003804
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003805static struct input_panel_surface *
3806create_input_panel_surface(struct desktop_shell *shell,
3807 struct weston_surface *surface)
3808{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003809 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003810
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003811 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3812 if (!input_panel_surface)
3813 return NULL;
3814
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003815 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003816 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003817
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003818 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003819
3820 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003821
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003822 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003823 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003824 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003825 &input_panel_surface->surface_destroy_listener);
3826
3827 wl_list_init(&input_panel_surface->link);
3828
3829 return input_panel_surface;
3830}
3831
3832static void
3833input_panel_surface_set_toplevel(struct wl_client *client,
3834 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003835 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003836 uint32_t position)
3837{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003838 struct input_panel_surface *input_panel_surface =
3839 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003840 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003841
3842 wl_list_insert(&shell->input_panel.surfaces,
3843 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003844
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003845 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003846 input_panel_surface->panel = 0;
3847}
3848
3849static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003850input_panel_surface_set_overlay_panel(struct wl_client *client,
3851 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003852{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003853 struct input_panel_surface *input_panel_surface =
3854 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003855 struct desktop_shell *shell = input_panel_surface->shell;
3856
3857 wl_list_insert(&shell->input_panel.surfaces,
3858 &input_panel_surface->link);
3859
3860 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003861}
3862
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003863static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003864 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003865 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003866};
3867
3868static void
3869destroy_input_panel_surface_resource(struct wl_resource *resource)
3870{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003871 struct input_panel_surface *ipsurf =
3872 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003873
3874 destroy_input_panel_surface(ipsurf);
3875}
3876
3877static void
3878input_panel_get_input_panel_surface(struct wl_client *client,
3879 struct wl_resource *resource,
3880 uint32_t id,
3881 struct wl_resource *surface_resource)
3882{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003883 struct weston_surface *surface =
3884 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003885 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003886 struct input_panel_surface *ipsurf;
3887
3888 if (get_input_panel_surface(surface)) {
3889 wl_resource_post_error(surface_resource,
3890 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003891 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003892 return;
3893 }
3894
3895 ipsurf = create_input_panel_surface(shell, surface);
3896 if (!ipsurf) {
3897 wl_resource_post_error(surface_resource,
3898 WL_DISPLAY_ERROR_INVALID_OBJECT,
3899 "surface->configure already set");
3900 return;
3901 }
3902
Jason Ekstranda85118c2013-06-27 20:17:02 -05003903 ipsurf->resource =
3904 wl_resource_create(client,
3905 &wl_input_panel_surface_interface, 1, id);
3906 wl_resource_set_implementation(ipsurf->resource,
3907 &input_panel_surface_implementation,
3908 ipsurf,
3909 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003910}
3911
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003912static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003913 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003914};
3915
3916static void
3917unbind_input_panel(struct wl_resource *resource)
3918{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003919 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003920
3921 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003922}
3923
3924static void
3925bind_input_panel(struct wl_client *client,
3926 void *data, uint32_t version, uint32_t id)
3927{
3928 struct desktop_shell *shell = data;
3929 struct wl_resource *resource;
3930
Jason Ekstranda85118c2013-06-27 20:17:02 -05003931 resource = wl_resource_create(client,
3932 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003933
3934 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003935 wl_resource_set_implementation(resource,
3936 &input_panel_implementation,
3937 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003938 shell->input_panel.binding = resource;
3939 return;
3940 }
3941
3942 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3943 "interface object already bound");
3944 wl_resource_destroy(resource);
3945}
3946
Kristian Høgsberg07045392012-02-19 18:52:44 -05003947struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003948 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003949 struct weston_surface *current;
3950 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003951 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003952};
3953
3954static void
3955switcher_next(struct switcher *switcher)
3956{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003957 struct weston_surface *surface;
3958 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003959 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003960 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003961
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003962 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003963 switch (get_shell_surface_type(surface)) {
3964 case SHELL_SURFACE_TOPLEVEL:
3965 case SHELL_SURFACE_FULLSCREEN:
3966 case SHELL_SURFACE_MAXIMIZED:
3967 if (first == NULL)
3968 first = surface;
3969 if (prev == switcher->current)
3970 next = surface;
3971 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003972 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003973 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003974 weston_surface_damage(surface);
3975 break;
3976 default:
3977 break;
3978 }
Alex Wu1659daa2012-04-01 20:13:09 +08003979
3980 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003981 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003982 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003983 weston_surface_damage(surface);
3984 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003985 }
3986
3987 if (next == NULL)
3988 next = first;
3989
Alex Wu07b26062012-03-12 16:06:01 +08003990 if (next == NULL)
3991 return;
3992
Kristian Høgsberg07045392012-02-19 18:52:44 -05003993 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003994 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003995
3996 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003997 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003998
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003999 shsurf = get_shell_surface(switcher->current);
4000 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004001 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004002}
4003
4004static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004005switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004006{
4007 struct switcher *switcher =
4008 container_of(listener, struct switcher, listener);
4009
4010 switcher_next(switcher);
4011}
4012
4013static void
Daniel Stone351eb612012-05-31 15:27:47 -04004014switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004015{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004016 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004017 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004018 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004019
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004020 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004021 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004022 weston_surface_damage(surface);
4023 }
4024
Alex Wu07b26062012-03-12 16:06:01 +08004025 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004026 activate(switcher->shell, switcher->current,
4027 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004028 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004029 weston_keyboard_end_grab(keyboard);
4030 if (keyboard->input_method_resource)
4031 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004032 free(switcher);
4033}
4034
4035static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004036switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004037 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004038{
4039 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004040 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004041
Daniel Stonec9785ea2012-05-30 16:31:52 +01004042 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004043 switcher_next(switcher);
4044}
4045
4046static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004047switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004048 uint32_t mods_depressed, uint32_t mods_latched,
4049 uint32_t mods_locked, uint32_t group)
4050{
4051 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004052 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004053
Daniel Stone351eb612012-05-31 15:27:47 -04004054 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4055 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004056}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004057
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004058static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004059 switcher_key,
4060 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004061};
4062
4063static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004064switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004065 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004066{
Tiago Vignattibe143262012-04-16 17:31:41 +03004067 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004068 struct switcher *switcher;
4069
4070 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004071 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004072 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004073 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004074 wl_list_init(&switcher->listener.link);
4075
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004076 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004077 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004078 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004079 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4080 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004081 switcher_next(switcher);
4082}
4083
Pekka Paalanen3c647232011-12-22 13:43:43 +02004084static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004085backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004086 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004087{
4088 struct weston_compositor *compositor = data;
4089 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004090 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004091
4092 /* TODO: we're limiting to simple use cases, where we assume just
4093 * control on the primary display. We'd have to extend later if we
4094 * ever get support for setting backlights on random desktop LCD
4095 * panels though */
4096 output = get_default_output(compositor);
4097 if (!output)
4098 return;
4099
4100 if (!output->set_backlight)
4101 return;
4102
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004103 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4104 backlight_new = output->backlight_current - 25;
4105 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4106 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004107
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004108 if (backlight_new < 5)
4109 backlight_new = 5;
4110 if (backlight_new > 255)
4111 backlight_new = 255;
4112
4113 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004114 output->set_backlight(output, output->backlight_current);
4115}
4116
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004117struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004118 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004119 struct weston_seat *seat;
4120 uint32_t key[2];
4121 int key_released[2];
4122};
4123
4124static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004125debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004126 uint32_t key, uint32_t state)
4127{
4128 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4129 struct wl_resource *resource;
4130 struct wl_display *display;
4131 uint32_t serial;
4132 int send = 0, terminate = 0;
4133 int check_binding = 1;
4134 int i;
4135
4136 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4137 /* Do not run bindings on key releases */
4138 check_binding = 0;
4139
4140 for (i = 0; i < 2; i++)
4141 if (key == db->key[i])
4142 db->key_released[i] = 1;
4143
4144 if (db->key_released[0] && db->key_released[1]) {
4145 /* All key releases been swalled so end the grab */
4146 terminate = 1;
4147 } else if (key != db->key[0] && key != db->key[1]) {
4148 /* Should not swallow release of other keys */
4149 send = 1;
4150 }
4151 } else if (key == db->key[0] && !db->key_released[0]) {
4152 /* Do not check bindings for the first press of the binding
4153 * key. This allows it to be used as a debug shortcut.
4154 * We still need to swallow this event. */
4155 check_binding = 0;
4156 } else if (db->key[1]) {
4157 /* If we already ran a binding don't process another one since
4158 * we can't keep track of all the binding keys that were
4159 * pressed in order to swallow the release events. */
4160 send = 1;
4161 check_binding = 0;
4162 }
4163
4164 if (check_binding) {
4165 struct weston_compositor *ec = db->seat->compositor;
4166
4167 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4168 key, state)) {
4169 /* We ran a binding so swallow the press and keep the
4170 * grab to swallow the released too. */
4171 send = 0;
4172 terminate = 0;
4173 db->key[1] = key;
4174 } else {
4175 /* Terminate the grab since the key pressed is not a
4176 * debug binding key. */
4177 send = 1;
4178 terminate = 1;
4179 }
4180 }
4181
4182 if (send) {
4183 resource = grab->keyboard->focus_resource;
4184
4185 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004186 display = wl_client_get_display(wl_resource_get_client(resource));
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004187 serial = wl_display_next_serial(display);
4188 wl_keyboard_send_key(resource, serial, time, key, state);
4189 }
4190 }
4191
4192 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004193 weston_keyboard_end_grab(grab->keyboard);
4194 if (grab->keyboard->input_method_resource)
4195 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004196 free(db);
4197 }
4198}
4199
4200static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004201debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004202 uint32_t mods_depressed, uint32_t mods_latched,
4203 uint32_t mods_locked, uint32_t group)
4204{
4205 struct wl_resource *resource;
4206
4207 resource = grab->keyboard->focus_resource;
4208 if (!resource)
4209 return;
4210
4211 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4212 mods_latched, mods_locked, group);
4213}
4214
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004215struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004216 debug_binding_key,
4217 debug_binding_modifiers
4218};
4219
4220static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004221debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004222{
4223 struct debug_binding_grab *grab;
4224
4225 grab = calloc(1, sizeof *grab);
4226 if (!grab)
4227 return;
4228
4229 grab->seat = (struct weston_seat *) seat;
4230 grab->key[0] = key;
4231 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004232 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004233}
4234
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004235static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004236force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004237 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004238{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004239 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004240 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004241 struct desktop_shell *shell = data;
4242 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004243 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004244
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004245 focus_surface = seat->keyboard->focus;
4246 if (!focus_surface)
4247 return;
4248
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004249 wl_signal_emit(&compositor->kill_signal, focus_surface);
4250
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004251 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004252 wl_client_get_credentials(client, &pid, NULL, NULL);
4253
4254 /* Skip clients that we launched ourselves (the credentials of
4255 * the socketpair is ours) */
4256 if (pid == getpid())
4257 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004258
Daniel Stone325fc2d2012-05-30 16:31:58 +01004259 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004260}
4261
4262static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004263workspace_up_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 != 0)
4272 new_index--;
4273
4274 change_workspace(shell, new_index);
4275}
4276
4277static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004278workspace_down_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 = shell->workspaces.current;
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 if (new_index < shell->workspaces.num - 1)
4287 new_index++;
4288
4289 change_workspace(shell, new_index);
4290}
4291
4292static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004293workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004294 uint32_t key, void *data)
4295{
4296 struct desktop_shell *shell = data;
4297 unsigned int new_index;
4298
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004299 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004300 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004301 new_index = key - KEY_F1;
4302 if (new_index >= shell->workspaces.num)
4303 new_index = shell->workspaces.num - 1;
4304
4305 change_workspace(shell, new_index);
4306}
4307
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004308static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004309workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004310 uint32_t key, void *data)
4311{
4312 struct desktop_shell *shell = data;
4313 unsigned int new_index = shell->workspaces.current;
4314
4315 if (shell->locked)
4316 return;
4317
4318 if (new_index != 0)
4319 new_index--;
4320
4321 take_surface_to_workspace_by_seat(shell, seat, new_index);
4322}
4323
4324static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004325workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004326 uint32_t key, void *data)
4327{
4328 struct desktop_shell *shell = data;
4329 unsigned int new_index = shell->workspaces.current;
4330
4331 if (shell->locked)
4332 return;
4333
4334 if (new_index < shell->workspaces.num - 1)
4335 new_index++;
4336
4337 take_surface_to_workspace_by_seat(shell, seat, new_index);
4338}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004339
4340static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004341shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004342{
Tiago Vignattibe143262012-04-16 17:31:41 +03004343 struct desktop_shell *shell =
4344 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004345 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004346
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004347 if (shell->child.client)
4348 wl_client_destroy(shell->child.client);
4349
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004350 wl_list_remove(&shell->idle_listener.link);
4351 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004352 wl_list_remove(&shell->show_input_panel_listener.link);
4353 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004354
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004355 wl_array_for_each(ws, &shell->workspaces.array)
4356 workspace_destroy(*ws);
4357 wl_array_release(&shell->workspaces.array);
4358
Pekka Paalanen3c647232011-12-22 13:43:43 +02004359 free(shell->screensaver.path);
4360 free(shell);
4361}
4362
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004363static void
4364shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4365{
4366 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004367 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004368
4369 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004370 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4371 MODIFIER_CTRL | MODIFIER_ALT,
4372 terminate_binding, ec);
4373 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4374 click_to_activate_binding,
4375 shell);
4376 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4377 MODIFIER_SUPER | MODIFIER_ALT,
4378 surface_opacity_binding, NULL);
4379 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4380 MODIFIER_SUPER, zoom_axis_binding,
4381 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004382
4383 /* configurable bindings */
4384 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004385 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4386 zoom_key_binding, NULL);
4387 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4388 zoom_key_binding, NULL);
4389 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4390 shell);
4391 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4392 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004393
4394 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4395 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4396 rotate_binding, NULL);
4397
Daniel Stone325fc2d2012-05-30 16:31:58 +01004398 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4399 shell);
4400 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4401 ec);
4402 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4403 backlight_binding, ec);
4404 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4405 ec);
4406 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4407 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004408 weston_compositor_add_key_binding(ec, KEY_K, mod,
4409 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004410 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4411 workspace_up_binding, shell);
4412 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4413 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004414 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4415 workspace_move_surface_up_binding,
4416 shell);
4417 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4418 workspace_move_surface_down_binding,
4419 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004420
4421 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4422 if (shell->workspaces.num > 1) {
4423 num_workspace_bindings = shell->workspaces.num;
4424 if (num_workspace_bindings > 6)
4425 num_workspace_bindings = 6;
4426 for (i = 0; i < num_workspace_bindings; i++)
4427 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4428 workspace_f_binding,
4429 shell);
4430 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004431
4432 /* Debug bindings */
4433 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4434 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004435}
4436
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004437WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004438module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004439 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004440{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004441 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004442 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004443 struct workspace **pws;
4444 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004445 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004446
4447 shell = malloc(sizeof *shell);
4448 if (shell == NULL)
4449 return -1;
4450
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004451 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004452 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004453
4454 shell->destroy_listener.notify = shell_destroy;
4455 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004456 shell->idle_listener.notify = idle_handler;
4457 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4458 shell->wake_listener.notify = wake_handler;
4459 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004460 shell->show_input_panel_listener.notify = show_input_panels;
4461 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4462 shell->hide_input_panel_listener.notify = hide_input_panels;
4463 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004464 shell->update_input_panel_listener.notify = update_input_panels;
4465 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004466 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004467 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004468 ec->shell_interface.create_shell_surface = create_shell_surface;
4469 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004470 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004471 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004472 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004473 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004474 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004475
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004476 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004477
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004478 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4479 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004480 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4481 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004482 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004483
4484 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004485 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004486
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004487 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004488
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004489 for (i = 0; i < shell->workspaces.num; i++) {
4490 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4491 if (pws == NULL)
4492 return -1;
4493
4494 *pws = workspace_create();
4495 if (*pws == NULL)
4496 return -1;
4497 }
4498 activate_workspace(shell, 0);
4499
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004500 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004501 wl_list_init(&shell->workspaces.animation.link);
4502 shell->workspaces.animation.frame = animate_workspace_change_frame;
4503
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004504 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004505 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004506 return -1;
4507
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004508 if (wl_global_create(ec->wl_display,
4509 &desktop_shell_interface, 2,
4510 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004511 return -1;
4512
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004513 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4514 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004515 return -1;
4516
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004517 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004518 shell, bind_input_panel) == NULL)
4519 return -1;
4520
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004521 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4522 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004523 return -1;
4524
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004525 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004526
4527 loop = wl_display_get_event_loop(ec->wl_display);
4528 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004529
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004530 shell->screensaver.timer =
4531 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4532
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004533 wl_list_for_each(seat, &ec->seat_list, link)
4534 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004535
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004536 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004537
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004538 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004539
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004540 return 0;
4541}