blob: 3b6ac42ced4dc7600820f16c90d9c0ff280c96d1 [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;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400239 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300240};
241
242struct weston_move_grab {
243 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100244 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245};
246
Pekka Paalanen460099f2012-01-20 16:48:25 +0200247struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300248 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500249 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200250 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200251 float x;
252 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200253 } center;
254};
255
Giulio Camuffo5085a752013-03-25 21:42:45 +0100256struct shell_seat {
257 struct weston_seat *seat;
258 struct wl_listener seat_destroy_listener;
259
260 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400261 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100262 struct wl_list surfaces_list;
263 struct wl_client *client;
264 int32_t initial_up;
265 } popup_grab;
266};
267
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400268static void
269activate(struct desktop_shell *shell, struct weston_surface *es,
270 struct weston_seat *seat);
271
272static struct workspace *
273get_current_workspace(struct desktop_shell *shell);
274
Alex Wubd3354b2012-04-17 17:20:49 +0800275static struct shell_surface *
276get_shell_surface(struct weston_surface *surface);
277
278static struct desktop_shell *
279shell_surface_get_shell(struct shell_surface *shsurf);
280
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500281static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400282surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500283
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300284static void
285shell_fade_startup(struct desktop_shell *shell);
286
Alex Wubd3354b2012-04-17 17:20:49 +0800287static bool
288shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
289{
290 struct desktop_shell *shell;
291 struct weston_surface *top_fs_es;
292
293 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100294
Alex Wubd3354b2012-04-17 17:20:49 +0800295 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
296 return false;
297
298 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100299 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800300 layer_link);
301 return (shsurf == get_shell_surface(top_fs_es));
302}
303
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500304static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400305destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306{
307 struct shell_grab *grab;
308
309 grab = container_of(listener, struct shell_grab,
310 shsurf_destroy_listener);
311
312 grab->shsurf = NULL;
313}
314
315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400317
318static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325 struct desktop_shell *shell = shsurf->shell;
326
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400327 popup_grab_end(pointer);
328
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329 grab->grab.interface = interface;
330 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500332 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400333 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300334
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300335 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300336
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400337 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300338 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400339 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400340 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341}
342
343static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300344shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400346 if (grab->shsurf)
347 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400349 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300350}
351
352static void
Alex Wu4539b082012-03-01 12:57:46 +0800353center_on_output(struct weston_surface *surface,
354 struct weston_output *output);
355
Daniel Stone496ca172012-05-30 16:31:42 +0100356static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300357get_modifier(char *modifier)
358{
359 if (!modifier)
360 return MODIFIER_SUPER;
361
362 if (!strcmp("ctrl", modifier))
363 return MODIFIER_CTRL;
364 else if (!strcmp("alt", modifier))
365 return MODIFIER_ALT;
366 else if (!strcmp("super", modifier))
367 return MODIFIER_SUPER;
368 else
369 return MODIFIER_SUPER;
370}
371
Juan Zhaoe10d2792012-04-25 19:09:52 +0800372static enum animation_type
373get_animation_type(char *animation)
374{
375 if (!animation)
376 return ANIMATION_NONE;
377
378 if (!strcmp("zoom", animation))
379 return ANIMATION_ZOOM;
380 else if (!strcmp("fade", animation))
381 return ANIMATION_FADE;
382 else
383 return ANIMATION_NONE;
384}
385
Alex Wu4539b082012-03-01 12:57:46 +0800386static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400387shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200388{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400389 struct weston_config_section *section;
390 int duration;
391 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200392
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400393 section = weston_config_get_section(shell->compositor->config,
394 "screensaver", NULL, NULL);
395 weston_config_section_get_string(section,
396 "path", &shell->screensaver.path, NULL);
397 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200398 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400399
400 section = weston_config_get_section(shell->compositor->config,
401 "shell", NULL, NULL);
402 weston_config_section_get_string(section,
403 "binding-modifier", &s, "super");
404 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200405 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400406 weston_config_section_get_string(section, "animation", &s, "none");
407 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200408 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400409 weston_config_section_get_uint(section, "num-workspaces",
410 &shell->workspaces.num,
411 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200412}
413
414static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200415focus_state_destroy(struct focus_state *state)
416{
417 wl_list_remove(&state->seat_destroy_listener.link);
418 wl_list_remove(&state->surface_destroy_listener.link);
419 free(state);
420}
421
422static void
423focus_state_seat_destroy(struct wl_listener *listener, void *data)
424{
425 struct focus_state *state = container_of(listener,
426 struct focus_state,
427 seat_destroy_listener);
428
429 wl_list_remove(&state->link);
430 focus_state_destroy(state);
431}
432
433static void
434focus_state_surface_destroy(struct wl_listener *listener, void *data)
435{
436 struct focus_state *state = container_of(listener,
437 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400438 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400439 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300440 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400441 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200442
Pekka Paalanen01388e22013-04-25 13:57:44 +0300443 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
444
Kristian Høgsberge3778222012-07-31 17:29:30 -0400445 next = NULL;
446 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300447 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400448 continue;
449
450 next = surface;
451 break;
452 }
453
Pekka Paalanen01388e22013-04-25 13:57:44 +0300454 /* if the focus was a sub-surface, activate its main surface */
455 if (main_surface != state->keyboard_focus)
456 next = main_surface;
457
Kristian Høgsberge3778222012-07-31 17:29:30 -0400458 if (next) {
459 shell = state->seat->compositor->shell_interface.shell;
460 activate(shell, next, state->seat);
461 } else {
462 wl_list_remove(&state->link);
463 focus_state_destroy(state);
464 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200465}
466
467static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400468focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200469{
Jonas Ådahl04769742012-06-13 00:01:24 +0200470 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200471
472 state = malloc(sizeof *state);
473 if (state == NULL)
474 return NULL;
475
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400476 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200477 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400478 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200479
480 state->seat_destroy_listener.notify = focus_state_seat_destroy;
481 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400482 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200483 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400484 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200485
486 return state;
487}
488
Jonas Ådahl8538b222012-08-29 22:13:03 +0200489static struct focus_state *
490ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
491{
492 struct workspace *ws = get_current_workspace(shell);
493 struct focus_state *state;
494
495 wl_list_for_each(state, &ws->focus_list, link)
496 if (state->seat == seat)
497 break;
498
499 if (&state->link == &ws->focus_list)
500 state = focus_state_create(seat, ws);
501
502 return state;
503}
504
Jonas Ådahl04769742012-06-13 00:01:24 +0200505static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400506restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200507{
508 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400509 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200510
511 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400512 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200513
Kristian Høgsberge3148752013-05-06 23:19:49 -0400514 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200515 }
516}
517
518static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200519replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
520 struct weston_seat *seat)
521{
522 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400523 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200524
525 wl_list_for_each(state, &ws->focus_list, link) {
526 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400527 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500528 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200529 return;
530 }
531 }
532}
533
534static void
535drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
536 struct weston_surface *surface)
537{
538 struct focus_state *state;
539
540 wl_list_for_each(state, &ws->focus_list, link)
541 if (state->keyboard_focus == surface)
542 state->keyboard_focus = NULL;
543}
544
545static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200546workspace_destroy(struct workspace *ws)
547{
Jonas Ådahl04769742012-06-13 00:01:24 +0200548 struct focus_state *state, *next;
549
550 wl_list_for_each_safe(state, next, &ws->focus_list, link)
551 focus_state_destroy(state);
552
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200553 free(ws);
554}
555
Jonas Ådahl04769742012-06-13 00:01:24 +0200556static void
557seat_destroyed(struct wl_listener *listener, void *data)
558{
559 struct weston_seat *seat = data;
560 struct focus_state *state, *next;
561 struct workspace *ws = container_of(listener,
562 struct workspace,
563 seat_destroyed_listener);
564
565 wl_list_for_each_safe(state, next, &ws->focus_list, link)
566 if (state->seat == seat)
567 wl_list_remove(&state->link);
568}
569
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200570static struct workspace *
571workspace_create(void)
572{
573 struct workspace *ws = malloc(sizeof *ws);
574 if (ws == NULL)
575 return NULL;
576
577 weston_layer_init(&ws->layer, NULL);
578
Jonas Ådahl04769742012-06-13 00:01:24 +0200579 wl_list_init(&ws->focus_list);
580 wl_list_init(&ws->seat_destroyed_listener.link);
581 ws->seat_destroyed_listener.notify = seat_destroyed;
582
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200583 return ws;
584}
585
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200586static int
587workspace_is_empty(struct workspace *ws)
588{
589 return wl_list_empty(&ws->layer.surface_list);
590}
591
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200592static struct workspace *
593get_workspace(struct desktop_shell *shell, unsigned int index)
594{
595 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200596 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200597 pws += index;
598 return *pws;
599}
600
601static struct workspace *
602get_current_workspace(struct desktop_shell *shell)
603{
604 return get_workspace(shell, shell->workspaces.current);
605}
606
607static void
608activate_workspace(struct desktop_shell *shell, unsigned int index)
609{
610 struct workspace *ws;
611
612 ws = get_workspace(shell, index);
613 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
614
615 shell->workspaces.current = index;
616}
617
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200618static unsigned int
619get_output_height(struct weston_output *output)
620{
621 return abs(output->region.extents.y1 - output->region.extents.y2);
622}
623
624static void
625surface_translate(struct weston_surface *surface, double d)
626{
627 struct shell_surface *shsurf = get_shell_surface(surface);
628 struct weston_transform *transform;
629
630 transform = &shsurf->workspace_transform;
631 if (wl_list_empty(&transform->link))
632 wl_list_insert(surface->geometry.transformation_list.prev,
633 &shsurf->workspace_transform.link);
634
635 weston_matrix_init(&shsurf->workspace_transform.matrix);
636 weston_matrix_translate(&shsurf->workspace_transform.matrix,
637 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200638 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200639}
640
641static void
642workspace_translate_out(struct workspace *ws, double fraction)
643{
644 struct weston_surface *surface;
645 unsigned int height;
646 double d;
647
648 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
649 height = get_output_height(surface->output);
650 d = height * fraction;
651
652 surface_translate(surface, d);
653 }
654}
655
656static void
657workspace_translate_in(struct workspace *ws, double fraction)
658{
659 struct weston_surface *surface;
660 unsigned int height;
661 double d;
662
663 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
664 height = get_output_height(surface->output);
665
666 if (fraction > 0)
667 d = -(height - height * fraction);
668 else
669 d = height + height * fraction;
670
671 surface_translate(surface, d);
672 }
673}
674
675static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200676broadcast_current_workspace_state(struct desktop_shell *shell)
677{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500678 struct wl_list *link;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200679
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500680 for (link = shell->workspaces.client_list.next;
681 link != &shell->workspaces.client_list;
682 link = link->next) {
683 workspace_manager_send_state(wl_resource_from_link(link),
Jonas Ådahle9d22502012-08-29 22:13:01 +0200684 shell->workspaces.current,
685 shell->workspaces.num);
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500686 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200687}
688
689static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200690reverse_workspace_change_animation(struct desktop_shell *shell,
691 unsigned int index,
692 struct workspace *from,
693 struct workspace *to)
694{
695 shell->workspaces.current = index;
696
697 shell->workspaces.anim_to = to;
698 shell->workspaces.anim_from = from;
699 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
700 shell->workspaces.anim_timestamp = 0;
701
Scott Moreau4272e632012-08-13 09:58:41 -0600702 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200703}
704
705static void
706workspace_deactivate_transforms(struct workspace *ws)
707{
708 struct weston_surface *surface;
709 struct shell_surface *shsurf;
710
711 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
712 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200713 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
714 wl_list_remove(&shsurf->workspace_transform.link);
715 wl_list_init(&shsurf->workspace_transform.link);
716 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200717 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200718 }
719}
720
721static void
722finish_workspace_change_animation(struct desktop_shell *shell,
723 struct workspace *from,
724 struct workspace *to)
725{
Scott Moreau4272e632012-08-13 09:58:41 -0600726 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200727
728 wl_list_remove(&shell->workspaces.animation.link);
729 workspace_deactivate_transforms(from);
730 workspace_deactivate_transforms(to);
731 shell->workspaces.anim_to = NULL;
732
733 wl_list_remove(&shell->workspaces.anim_from->layer.link);
734}
735
736static void
737animate_workspace_change_frame(struct weston_animation *animation,
738 struct weston_output *output, uint32_t msecs)
739{
740 struct desktop_shell *shell =
741 container_of(animation, struct desktop_shell,
742 workspaces.animation);
743 struct workspace *from = shell->workspaces.anim_from;
744 struct workspace *to = shell->workspaces.anim_to;
745 uint32_t t;
746 double x, y;
747
748 if (workspace_is_empty(from) && workspace_is_empty(to)) {
749 finish_workspace_change_animation(shell, from, to);
750 return;
751 }
752
753 if (shell->workspaces.anim_timestamp == 0) {
754 if (shell->workspaces.anim_current == 0.0)
755 shell->workspaces.anim_timestamp = msecs;
756 else
757 shell->workspaces.anim_timestamp =
758 msecs -
759 /* Invers of movement function 'y' below. */
760 (asin(1.0 - shell->workspaces.anim_current) *
761 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
762 M_2_PI);
763 }
764
765 t = msecs - shell->workspaces.anim_timestamp;
766
767 /*
768 * x = [0, π/2]
769 * y(x) = sin(x)
770 */
771 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
772 y = sin(x);
773
774 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600775 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200776
777 workspace_translate_out(from, shell->workspaces.anim_dir * y);
778 workspace_translate_in(to, shell->workspaces.anim_dir * y);
779 shell->workspaces.anim_current = y;
780
Scott Moreau4272e632012-08-13 09:58:41 -0600781 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200782 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200783 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200784 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200785}
786
787static void
788animate_workspace_change(struct desktop_shell *shell,
789 unsigned int index,
790 struct workspace *from,
791 struct workspace *to)
792{
793 struct weston_output *output;
794
795 int dir;
796
797 if (index > shell->workspaces.current)
798 dir = -1;
799 else
800 dir = 1;
801
802 shell->workspaces.current = index;
803
804 shell->workspaces.anim_dir = dir;
805 shell->workspaces.anim_from = from;
806 shell->workspaces.anim_to = to;
807 shell->workspaces.anim_current = 0.0;
808 shell->workspaces.anim_timestamp = 0;
809
810 output = container_of(shell->compositor->output_list.next,
811 struct weston_output, link);
812 wl_list_insert(&output->animation_list,
813 &shell->workspaces.animation.link);
814
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200815 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200816
817 workspace_translate_in(to, 0);
818
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400819 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200820
Scott Moreau4272e632012-08-13 09:58:41 -0600821 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200822}
823
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200824static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200825update_workspace(struct desktop_shell *shell, unsigned int index,
826 struct workspace *from, struct workspace *to)
827{
828 shell->workspaces.current = index;
829 wl_list_insert(&from->layer.link, &to->layer.link);
830 wl_list_remove(&from->layer.link);
831}
832
833static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200834change_workspace(struct desktop_shell *shell, unsigned int index)
835{
836 struct workspace *from;
837 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200838
839 if (index == shell->workspaces.current)
840 return;
841
842 /* Don't change workspace when there is any fullscreen surfaces. */
843 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
844 return;
845
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200846 from = get_current_workspace(shell);
847 to = get_workspace(shell, index);
848
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200849 if (shell->workspaces.anim_from == to &&
850 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200851 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200852 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200853 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200854 return;
855 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200856
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857 if (shell->workspaces.anim_to != NULL)
858 finish_workspace_change_animation(shell,
859 shell->workspaces.anim_from,
860 shell->workspaces.anim_to);
861
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200862 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200863
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200864 if (workspace_is_empty(to) && workspace_is_empty(from))
865 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200866 else
867 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200868
869 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200870}
871
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200872static bool
873workspace_has_only(struct workspace *ws, struct weston_surface *surface)
874{
875 struct wl_list *list = &ws->layer.surface_list;
876 struct wl_list *e;
877
878 if (wl_list_empty(list))
879 return false;
880
881 e = list->next;
882
883 if (e->next != list)
884 return false;
885
886 return container_of(e, struct weston_surface, layer_link) == surface;
887}
888
889static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200890move_surface_to_workspace(struct desktop_shell *shell,
891 struct weston_surface *surface,
892 uint32_t workspace)
893{
894 struct workspace *from;
895 struct workspace *to;
896 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300897 struct weston_surface *focus;
898
899 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900
901 if (workspace == shell->workspaces.current)
902 return;
903
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200904 if (workspace >= shell->workspaces.num)
905 workspace = shell->workspaces.num - 1;
906
Jonas Ådahle9d22502012-08-29 22:13:01 +0200907 from = get_current_workspace(shell);
908 to = get_workspace(shell, workspace);
909
910 wl_list_remove(&surface->layer_link);
911 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
912
913 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300914 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
915 if (!seat->keyboard)
916 continue;
917
918 focus = weston_surface_get_main_surface(seat->keyboard->focus);
919 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400920 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300921 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200922
923 weston_surface_damage_below(surface);
924}
925
926static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200927take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400928 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929 unsigned int index)
930{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300931 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 struct shell_surface *shsurf;
933 struct workspace *from;
934 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200935 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200936
Pekka Paalanen01388e22013-04-25 13:57:44 +0300937 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200938 if (surface == NULL ||
939 index == shell->workspaces.current)
940 return;
941
942 from = get_current_workspace(shell);
943 to = get_workspace(shell, index);
944
945 wl_list_remove(&surface->layer_link);
946 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
947
Jonas Ådahle9d22502012-08-29 22:13:01 +0200948 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200949 drop_focus_state(shell, from, surface);
950
951 if (shell->workspaces.anim_from == to &&
952 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200953 wl_list_remove(&to->layer.link);
954 wl_list_insert(from->layer.link.prev, &to->layer.link);
955
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200956 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200957 broadcast_current_workspace_state(shell);
958
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200959 return;
960 }
961
962 if (shell->workspaces.anim_to != NULL)
963 finish_workspace_change_animation(shell,
964 shell->workspaces.anim_from,
965 shell->workspaces.anim_to);
966
967 if (workspace_is_empty(from) &&
968 workspace_has_only(to, surface))
969 update_workspace(shell, index, from, to);
970 else {
971 shsurf = get_shell_surface(surface);
972 if (wl_list_empty(&shsurf->workspace_transform.link))
973 wl_list_insert(&shell->workspaces.anim_sticky_list,
974 &shsurf->workspace_transform.link);
975
976 animate_workspace_change(shell, index, from, to);
977 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200978
979 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200980
981 state = ensure_focus_state(shell, seat);
982 if (state != NULL)
983 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200984}
985
986static void
987workspace_manager_move_surface(struct wl_client *client,
988 struct wl_resource *resource,
989 struct wl_resource *surface_resource,
990 uint32_t workspace)
991{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500992 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200993 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500994 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300995 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200996
Pekka Paalanen01388e22013-04-25 13:57:44 +0300997 main_surface = weston_surface_get_main_surface(surface);
998 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200999}
1000
1001static const struct workspace_manager_interface workspace_manager_implementation = {
1002 workspace_manager_move_surface,
1003};
1004
1005static void
1006unbind_resource(struct wl_resource *resource)
1007{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001008 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001009}
1010
1011static void
1012bind_workspace_manager(struct wl_client *client,
1013 void *data, uint32_t version, uint32_t id)
1014{
1015 struct desktop_shell *shell = data;
1016 struct wl_resource *resource;
1017
Jason Ekstranda85118c2013-06-27 20:17:02 -05001018 resource = wl_resource_create(client,
1019 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001020
1021 if (resource == NULL) {
1022 weston_log("couldn't add workspace manager object");
1023 return;
1024 }
1025
Jason Ekstranda85118c2013-06-27 20:17:02 -05001026 wl_resource_set_implementation(resource,
1027 &workspace_manager_implementation,
1028 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001029 wl_list_insert(&shell->workspaces.client_list,
1030 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001031
1032 workspace_manager_send_state(resource,
1033 shell->workspaces.current,
1034 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001035}
1036
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001037static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001038noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001039{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001040}
1041
1042static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001043move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001044{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001045 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001046 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001047 struct shell_surface *shsurf = move->base.shsurf;
1048 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001049 int dx = wl_fixed_to_int(pointer->x + move->dx);
1050 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001051
1052 if (!shsurf)
1053 return;
1054
1055 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056
Daniel Stone103db7f2012-05-08 17:17:55 +01001057 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001058 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001059
1060 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001061}
1062
1063static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001064move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001065 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001066{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001067 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1068 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001069 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001070 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001071
Daniel Stone4dbadb12012-05-30 16:31:51 +01001072 if (pointer->button_count == 0 &&
1073 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001074 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001075 free(grab);
1076 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001077}
1078
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001079static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001080 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001081 move_grab_motion,
1082 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001083};
1084
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001085static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001086surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001087{
1088 struct weston_move_grab *move;
1089
1090 if (!shsurf)
1091 return -1;
1092
1093 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1094 return 0;
1095
1096 move = malloc(sizeof *move);
1097 if (!move)
1098 return -1;
1099
1100 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001101 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001102 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001103 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001104
1105 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001106 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001107
1108 return 0;
1109}
1110
1111static void
1112shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1113 struct wl_resource *seat_resource, uint32_t serial)
1114{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001115 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001116 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001117 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001118
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001119 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001120 if (seat->pointer->button_count == 0 ||
1121 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001122 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001123 return;
1124
Kristian Høgsberge3148752013-05-06 23:19:49 -04001125 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001126 wl_resource_post_no_memory(resource);
1127}
1128
1129struct weston_resize_grab {
1130 struct shell_grab base;
1131 uint32_t edges;
1132 int32_t width, height;
1133};
1134
1135static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001136resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001137{
1138 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001139 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001140 struct shell_surface *shsurf = resize->base.shsurf;
1141 int32_t width, height;
1142 wl_fixed_t from_x, from_y;
1143 wl_fixed_t to_x, to_y;
1144
1145 if (!shsurf)
1146 return;
1147
1148 weston_surface_from_global_fixed(shsurf->surface,
1149 pointer->grab_x, pointer->grab_y,
1150 &from_x, &from_y);
1151 weston_surface_from_global_fixed(shsurf->surface,
1152 pointer->x, pointer->y, &to_x, &to_y);
1153
1154 width = resize->width;
1155 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1156 width += wl_fixed_to_int(from_x - to_x);
1157 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1158 width += wl_fixed_to_int(to_x - from_x);
1159 }
1160
1161 height = resize->height;
1162 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1163 height += wl_fixed_to_int(from_y - to_y);
1164 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1165 height += wl_fixed_to_int(to_y - from_y);
1166 }
1167
1168 shsurf->client->send_configure(shsurf->surface,
1169 resize->edges, width, height);
1170}
1171
1172static void
1173send_configure(struct weston_surface *surface,
1174 uint32_t edges, int32_t width, int32_t height)
1175{
1176 struct shell_surface *shsurf = get_shell_surface(surface);
1177
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001178 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001179 edges, width, height);
1180}
1181
1182static const struct weston_shell_client shell_client = {
1183 send_configure
1184};
1185
1186static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001187resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001188 uint32_t time, uint32_t button, uint32_t state_w)
1189{
1190 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001191 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001192 enum wl_pointer_button_state state = state_w;
1193
1194 if (pointer->button_count == 0 &&
1195 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1196 shell_grab_end(&resize->base);
1197 free(grab);
1198 }
1199}
1200
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001201static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001202 noop_grab_focus,
1203 resize_grab_motion,
1204 resize_grab_button,
1205};
1206
Giulio Camuffob8366642013-04-25 13:57:46 +03001207/*
1208 * Returns the bounding box of a surface and all its sub-surfaces,
1209 * in the surface coordinates system. */
1210static void
1211surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1212 int32_t *y, int32_t *w, int32_t *h) {
1213 pixman_region32_t region;
1214 pixman_box32_t *box;
1215 struct weston_subsurface *subsurface;
1216
1217 pixman_region32_init_rect(&region, 0, 0,
1218 surface->geometry.width,
1219 surface->geometry.height);
1220
1221 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1222 pixman_region32_union_rect(&region, &region,
1223 subsurface->position.x,
1224 subsurface->position.y,
1225 subsurface->surface->geometry.width,
1226 subsurface->surface->geometry.height);
1227 }
1228
1229 box = pixman_region32_extents(&region);
1230 if (x)
1231 *x = box->x1;
1232 if (y)
1233 *y = box->y1;
1234 if (w)
1235 *w = box->x2 - box->x1;
1236 if (h)
1237 *h = box->y2 - box->y1;
1238
1239 pixman_region32_fini(&region);
1240}
1241
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001242static int
1243surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001244 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001245{
1246 struct weston_resize_grab *resize;
1247
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001248 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1249 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001250 return 0;
1251
1252 if (edges == 0 || edges > 15 ||
1253 (edges & 3) == 3 || (edges & 12) == 12)
1254 return 0;
1255
1256 resize = malloc(sizeof *resize);
1257 if (!resize)
1258 return -1;
1259
1260 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001261 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1262 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001263
1264 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001265 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001266
1267 return 0;
1268}
1269
1270static void
1271shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1272 struct wl_resource *seat_resource, uint32_t serial,
1273 uint32_t edges)
1274{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001275 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001276 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001277 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001278
1279 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1280 return;
1281
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001282 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001283 if (seat->pointer->button_count == 0 ||
1284 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001285 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001286 return;
1287
Kristian Høgsberge3148752013-05-06 23:19:49 -04001288 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001289 wl_resource_post_no_memory(resource);
1290}
1291
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001292static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001293busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001294{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001295 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001296 struct weston_pointer *pointer = base->pointer;
1297 struct weston_surface *surface;
1298 wl_fixed_t sx, sy;
1299
1300 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1301 pointer->x, pointer->y,
1302 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001303
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001304 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001305 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001306 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001307 }
1308}
1309
1310static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001311busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001312{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001313}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001314
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001316busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001317 uint32_t time, uint32_t button, uint32_t state)
1318{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001319 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001320 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001321 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001322
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001323 if (shsurf && button == BTN_LEFT && state) {
1324 activate(shsurf->shell, shsurf->surface, seat);
1325 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001326 } else if (shsurf && button == BTN_RIGHT && state) {
1327 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001328 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001329 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001330}
1331
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001332static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001333 busy_cursor_grab_focus,
1334 busy_cursor_grab_motion,
1335 busy_cursor_grab_button,
1336};
1337
1338static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001339set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001340{
1341 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001342
1343 grab = malloc(sizeof *grab);
1344 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001345 return;
1346
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001347 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1348 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001349}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001350
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001351static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001352end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001353{
1354 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1355
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001356 if (grab->grab.interface == &busy_cursor_grab_interface &&
1357 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001358 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001359 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001360 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001361}
1362
Scott Moreau9521d5e2012-04-19 13:06:17 -06001363static void
1364ping_timer_destroy(struct shell_surface *shsurf)
1365{
1366 if (!shsurf || !shsurf->ping_timer)
1367 return;
1368
1369 if (shsurf->ping_timer->source)
1370 wl_event_source_remove(shsurf->ping_timer->source);
1371
1372 free(shsurf->ping_timer);
1373 shsurf->ping_timer = NULL;
1374}
1375
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001376static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001377ping_timeout_handler(void *data)
1378{
1379 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001380 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001381
Scott Moreau9521d5e2012-04-19 13:06:17 -06001382 /* Client is not responding */
1383 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001384
1385 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001386 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001387 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001388
1389 return 1;
1390}
1391
1392static void
1393ping_handler(struct weston_surface *surface, uint32_t serial)
1394{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001395 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001396 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001397 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001398
1399 if (!shsurf)
1400 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001401 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001402 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001403
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001404 if (shsurf->surface == shsurf->shell->grab_surface)
1405 return;
1406
Scott Moreauff1db4a2012-04-17 19:06:18 -06001407 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001408 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001409 if (!shsurf->ping_timer)
1410 return;
1411
1412 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001413 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1414 shsurf->ping_timer->source =
1415 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1416 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1417
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001418 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001419 }
1420}
1421
1422static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001423handle_pointer_focus(struct wl_listener *listener, void *data)
1424{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001425 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001426 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001427 struct weston_compositor *compositor;
1428 struct shell_surface *shsurf;
1429 uint32_t serial;
1430
1431 if (!surface)
1432 return;
1433
1434 compositor = surface->compositor;
1435 shsurf = get_shell_surface(surface);
1436
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001437 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001438 set_busy_cursor(shsurf, pointer);
1439 } else {
1440 serial = wl_display_next_serial(compositor->wl_display);
1441 ping_handler(surface, serial);
1442 }
1443}
1444
1445static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001446create_pointer_focus_listener(struct weston_seat *seat)
1447{
1448 struct wl_listener *listener;
1449
Kristian Høgsberge3148752013-05-06 23:19:49 -04001450 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001451 return;
1452
1453 listener = malloc(sizeof *listener);
1454 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001455 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001456}
1457
1458static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001459shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1460 uint32_t serial)
1461{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001462 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001463 struct weston_seat *seat;
1464 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001465
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001466 if (shsurf->ping_timer == NULL)
1467 /* Just ignore unsolicited pong. */
1468 return;
1469
Scott Moreauff1db4a2012-04-17 19:06:18 -06001470 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001471 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001472 wl_list_for_each(seat, &ec->seat_list, link) {
1473 if(seat->pointer)
1474 end_busy_cursor(shsurf, seat->pointer);
1475 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001476 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001477 }
1478}
1479
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001480static void
1481shell_surface_set_title(struct wl_client *client,
1482 struct wl_resource *resource, const char *title)
1483{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001484 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001485
1486 free(shsurf->title);
1487 shsurf->title = strdup(title);
1488}
1489
1490static void
1491shell_surface_set_class(struct wl_client *client,
1492 struct wl_resource *resource, const char *class)
1493{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001494 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001495
1496 free(shsurf->class);
1497 shsurf->class = strdup(class);
1498}
1499
Juan Zhao96879df2012-02-07 08:45:41 +08001500static struct weston_output *
1501get_default_output(struct weston_compositor *compositor)
1502{
1503 return container_of(compositor->output_list.next,
1504 struct weston_output, link);
1505}
1506
Alex Wu4539b082012-03-01 12:57:46 +08001507static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001508restore_output_mode(struct weston_output *output)
1509{
1510 if (output->current != output->origin ||
1511 (int32_t)output->scale != output->origin_scale)
1512 weston_output_switch_mode(output,
1513 output->origin,
1514 output->origin_scale);
1515}
1516
1517static void
1518restore_all_output_modes(struct weston_compositor *compositor)
1519{
1520 struct weston_output *output;
1521
1522 wl_list_for_each(output, &compositor->output_list, link)
1523 restore_output_mode(output);
1524}
1525
1526static void
Alex Wu4539b082012-03-01 12:57:46 +08001527shell_unset_fullscreen(struct shell_surface *shsurf)
1528{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001529 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001530 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001531 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1532 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001533 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001534 }
Alex Wu4539b082012-03-01 12:57:46 +08001535 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1536 shsurf->fullscreen.framerate = 0;
1537 wl_list_remove(&shsurf->fullscreen.transform.link);
1538 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001539 if (shsurf->fullscreen.black_surface)
1540 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001541 shsurf->fullscreen.black_surface = NULL;
1542 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001543 weston_surface_set_position(shsurf->surface,
1544 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001545 if (shsurf->saved_rotation_valid) {
1546 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1547 &shsurf->rotation.transform.link);
1548 shsurf->saved_rotation_valid = false;
1549 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001550
1551 ws = get_current_workspace(shsurf->shell);
1552 wl_list_remove(&shsurf->surface->layer_link);
1553 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001554}
1555
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001556static void
1557shell_unset_maximized(struct shell_surface *shsurf)
1558{
1559 struct workspace *ws;
1560 /* undo all maximized things here */
1561 shsurf->output = get_default_output(shsurf->surface->compositor);
1562 weston_surface_set_position(shsurf->surface,
1563 shsurf->saved_x,
1564 shsurf->saved_y);
1565
1566 if (shsurf->saved_rotation_valid) {
1567 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1568 &shsurf->rotation.transform.link);
1569 shsurf->saved_rotation_valid = false;
1570 }
1571
1572 ws = get_current_workspace(shsurf->shell);
1573 wl_list_remove(&shsurf->surface->layer_link);
1574 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1575}
1576
Pekka Paalanen98262232011-12-01 10:42:22 +02001577static int
1578reset_shell_surface_type(struct shell_surface *surface)
1579{
1580 switch (surface->type) {
1581 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001582 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001583 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001584 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001585 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001586 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001587 case SHELL_SURFACE_NONE:
1588 case SHELL_SURFACE_TOPLEVEL:
1589 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001591 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001592 break;
1593 }
1594
1595 surface->type = SHELL_SURFACE_NONE;
1596 return 0;
1597}
1598
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001599static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001600set_surface_type(struct shell_surface *shsurf)
1601{
1602 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001603 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001604
1605 reset_shell_surface_type(shsurf);
1606
1607 shsurf->type = shsurf->next_type;
1608 shsurf->next_type = SHELL_SURFACE_NONE;
1609
1610 switch (shsurf->type) {
1611 case SHELL_SURFACE_TOPLEVEL:
1612 break;
1613 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001614 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001615 pes->geometry.x + shsurf->transient.x,
1616 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001617 break;
1618
1619 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001620 case SHELL_SURFACE_FULLSCREEN:
1621 shsurf->saved_x = surface->geometry.x;
1622 shsurf->saved_y = surface->geometry.y;
1623 shsurf->saved_position_valid = true;
1624
1625 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1626 wl_list_remove(&shsurf->rotation.transform.link);
1627 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001628 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001629 shsurf->saved_rotation_valid = true;
1630 }
1631 break;
1632
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001633 case SHELL_SURFACE_XWAYLAND:
1634 weston_surface_set_position(surface, shsurf->transient.x,
1635 shsurf->transient.y);
1636 break;
1637
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001638 default:
1639 break;
1640 }
1641}
1642
1643static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001644set_toplevel(struct shell_surface *shsurf)
1645{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001646 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001647}
1648
1649static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001650shell_surface_set_toplevel(struct wl_client *client,
1651 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001652{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001653 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001654
Tiago Vignattibc052c92012-04-19 16:18:18 +03001655 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001656}
1657
1658static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001659set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001660 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001661{
1662 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001663 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001664 shsurf->transient.x = x;
1665 shsurf->transient.y = y;
1666 shsurf->transient.flags = flags;
1667 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1668}
1669
1670static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001671shell_surface_set_transient(struct wl_client *client,
1672 struct wl_resource *resource,
1673 struct wl_resource *parent_resource,
1674 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001675{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001676 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001677 struct weston_surface *parent =
1678 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001679
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001680 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001681}
1682
Tiago Vignattibe143262012-04-16 17:31:41 +03001683static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001684shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001685{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001686 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001687}
1688
1689static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001690get_output_panel_height(struct desktop_shell *shell,
1691 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001692{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001693 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001694 int panel_height = 0;
1695
1696 if (!output)
1697 return 0;
1698
Juan Zhao4ab94682012-07-09 22:24:09 -07001699 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001700 if (surface->output == output) {
1701 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001702 break;
1703 }
1704 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001705
Juan Zhao96879df2012-02-07 08:45:41 +08001706 return panel_height;
1707}
1708
1709static void
1710shell_surface_set_maximized(struct wl_client *client,
1711 struct wl_resource *resource,
1712 struct wl_resource *output_resource )
1713{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001714 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001715 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001716 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001717 uint32_t edges = 0, panel_height = 0;
1718
1719 /* get the default output, if the client set it as NULL
1720 check whether the ouput is available */
1721 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001722 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001723 else if (es->output)
1724 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001725 else
1726 shsurf->output = get_default_output(es->compositor);
1727
Tiago Vignattibe143262012-04-16 17:31:41 +03001728 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001729 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001730 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001731
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001732 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001733 shsurf->output->width,
1734 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001735
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001736 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001737}
1738
Alex Wu21858432012-04-01 20:13:08 +08001739static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001740black_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 +08001741
Alex Wu4539b082012-03-01 12:57:46 +08001742static struct weston_surface *
1743create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001744 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001745 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001746{
1747 struct weston_surface *surface = NULL;
1748
1749 surface = weston_surface_create(ec);
1750 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001751 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001752 return NULL;
1753 }
1754
Alex Wu21858432012-04-01 20:13:08 +08001755 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001756 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001757 weston_surface_configure(surface, x, y, w, h);
1758 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001759 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001760 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001761 pixman_region32_fini(&surface->input);
1762 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001763
Alex Wu4539b082012-03-01 12:57:46 +08001764 return surface;
1765}
1766
1767/* Create black surface and append it to the associated fullscreen surface.
1768 * Handle size dismatch and positioning according to the method. */
1769static void
1770shell_configure_fullscreen(struct shell_surface *shsurf)
1771{
1772 struct weston_output *output = shsurf->fullscreen_output;
1773 struct weston_surface *surface = shsurf->surface;
1774 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001775 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001776 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001777
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001778 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1779 restore_output_mode(output);
1780
Alex Wu4539b082012-03-01 12:57:46 +08001781 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001782 shsurf->fullscreen.black_surface =
1783 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001784 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001785 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001786 output->width,
1787 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001788
1789 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1790 wl_list_insert(&surface->layer_link,
1791 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001792 shsurf->fullscreen.black_surface->output = output;
1793
Giulio Camuffob8366642013-04-25 13:57:46 +03001794 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1795 &surf_width, &surf_height);
1796
Alex Wu4539b082012-03-01 12:57:46 +08001797 switch (shsurf->fullscreen.type) {
1798 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001799 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001800 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001801 break;
1802 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001803 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001804 if (output->width == surf_width &&
1805 output->height == surf_height) {
1806 weston_surface_set_position(surface, output->x - surf_x,
1807 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001808 break;
1809 }
1810
Alex Wu4539b082012-03-01 12:57:46 +08001811 matrix = &shsurf->fullscreen.transform.matrix;
1812 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001813
Scott Moreau1bad5db2012-08-18 01:04:05 -06001814 output_aspect = (float) output->width /
1815 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001816 surface_aspect = (float) surface->geometry.width /
1817 (float) surface->geometry.height;
1818 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001819 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001820 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001821 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001822 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001823 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001824
Alex Wu4539b082012-03-01 12:57:46 +08001825 weston_matrix_scale(matrix, scale, scale, 1);
1826 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001827 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001828 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001829 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1830 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001831 weston_surface_set_position(surface, x, y);
1832
Alex Wu4539b082012-03-01 12:57:46 +08001833 break;
1834 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001835 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001836 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001837 surf_width * surface->buffer_scale,
1838 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001839 shsurf->fullscreen.framerate};
1840
Alexander Larsson355748e2013-05-28 16:23:38 +02001841 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001842 weston_surface_set_position(surface,
1843 output->x - surf_x,
1844 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001845 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001846 output->x - surf_x,
1847 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001848 output->width,
1849 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001850 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001851 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001852 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001853 center_on_output(surface, output);
1854 }
Alex Wubd3354b2012-04-17 17:20:49 +08001855 }
Alex Wu4539b082012-03-01 12:57:46 +08001856 break;
1857 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001858 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001859 break;
1860 default:
1861 break;
1862 }
1863}
1864
1865/* make the fullscreen and black surface at the top */
1866static void
1867shell_stack_fullscreen(struct shell_surface *shsurf)
1868{
Alex Wubd3354b2012-04-17 17:20:49 +08001869 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001870 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001871 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001872
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001873 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001874 wl_list_insert(&shell->fullscreen_layer.surface_list,
1875 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001876 weston_surface_damage(surface);
1877
1878 if (!shsurf->fullscreen.black_surface)
1879 shsurf->fullscreen.black_surface =
1880 create_black_surface(surface->compositor,
1881 surface,
1882 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001883 output->width,
1884 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001885
1886 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001887 wl_list_insert(&surface->layer_link,
1888 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001889 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001890}
1891
1892static void
1893shell_map_fullscreen(struct shell_surface *shsurf)
1894{
Alex Wu4539b082012-03-01 12:57:46 +08001895 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001896 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001897}
1898
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001899static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001900set_fullscreen(struct shell_surface *shsurf,
1901 uint32_t method,
1902 uint32_t framerate,
1903 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001904{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001905 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001906
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001907 if (output)
1908 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001909 else if (es->output)
1910 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001911 else
1912 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001913
Alex Wu4539b082012-03-01 12:57:46 +08001914 shsurf->fullscreen_output = shsurf->output;
1915 shsurf->fullscreen.type = method;
1916 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001917 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001918
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001919 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001920 shsurf->output->width,
1921 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001922}
1923
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001924static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001925shell_surface_set_fullscreen(struct wl_client *client,
1926 struct wl_resource *resource,
1927 uint32_t method,
1928 uint32_t framerate,
1929 struct wl_resource *output_resource)
1930{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001931 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001932 struct weston_output *output;
1933
1934 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001935 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001936 else
1937 output = NULL;
1938
1939 set_fullscreen(shsurf, method, framerate, output);
1940}
1941
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001942static void
1943set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1944{
1945 /* XXX: using the same fields for transient type */
1946 shsurf->transient.x = x;
1947 shsurf->transient.y = y;
1948 shsurf->transient.flags = flags;
1949 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1950}
1951
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001952static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001953
1954static void
1955destroy_shell_seat(struct wl_listener *listener, void *data)
1956{
1957 struct shell_seat *shseat =
1958 container_of(listener,
1959 struct shell_seat, seat_destroy_listener);
1960 struct shell_surface *shsurf, *prev = NULL;
1961
1962 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001963 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001964 shseat->popup_grab.client = NULL;
1965
1966 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1967 shsurf->popup.shseat = NULL;
1968 if (prev) {
1969 wl_list_init(&prev->popup.grab_link);
1970 }
1971 prev = shsurf;
1972 }
1973 wl_list_init(&prev->popup.grab_link);
1974 }
1975
1976 wl_list_remove(&shseat->seat_destroy_listener.link);
1977 free(shseat);
1978}
1979
1980static struct shell_seat *
1981create_shell_seat(struct weston_seat *seat)
1982{
1983 struct shell_seat *shseat;
1984
1985 shseat = calloc(1, sizeof *shseat);
1986 if (!shseat) {
1987 weston_log("no memory to allocate shell seat\n");
1988 return NULL;
1989 }
1990
1991 shseat->seat = seat;
1992 wl_list_init(&shseat->popup_grab.surfaces_list);
1993
1994 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1995 wl_signal_add(&seat->destroy_signal,
1996 &shseat->seat_destroy_listener);
1997
1998 return shseat;
1999}
2000
2001static struct shell_seat *
2002get_shell_seat(struct weston_seat *seat)
2003{
2004 struct wl_listener *listener;
2005
2006 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2007 if (listener == NULL)
2008 return create_shell_seat(seat);
2009
2010 return container_of(listener,
2011 struct shell_seat, seat_destroy_listener);
2012}
2013
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002014static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002015popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002016{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002017 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002018 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002019 struct shell_seat *shseat =
2020 container_of(grab, struct shell_seat, popup_grab.grab);
2021 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002022 wl_fixed_t sx, sy;
2023
2024 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2025 pointer->x, pointer->y,
2026 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002027
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002028 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002029 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002030 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002031 weston_pointer_set_focus(pointer, NULL,
2032 wl_fixed_from_int(0),
2033 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002034 }
2035}
2036
2037static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002038popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002039{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002040 struct weston_pointer *pointer = grab->pointer;
2041 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002042
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002043 if (pointer->focus_resource) {
2044 weston_surface_from_global_fixed(pointer->focus,
2045 pointer->x, pointer->y,
2046 &sx, &sy);
2047 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2048 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002049}
2050
2051static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002052popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002053 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002054{
2055 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002056 struct shell_seat *shseat =
2057 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002058 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002059 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002060 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002061
Daniel Stone37816df2012-05-16 18:45:18 +01002062 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002063 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002064 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002065 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002066 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002067 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002068 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002069 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002070 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002071 }
2072
Daniel Stone4dbadb12012-05-30 16:31:51 +01002073 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002074 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002075}
2076
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002077static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002078 popup_grab_focus,
2079 popup_grab_motion,
2080 popup_grab_button,
2081};
2082
2083static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002084popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002085{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002086 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002087 struct shell_seat *shseat =
2088 container_of(grab, struct shell_seat, popup_grab.grab);
2089 struct shell_surface *shsurf;
2090 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002091
2092 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002093 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002094 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002095 shseat->popup_grab.grab.interface = NULL;
2096 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002097 /* Send the popup_done event to all the popups open */
2098 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002099 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002100 shsurf->popup.shseat = NULL;
2101 if (prev) {
2102 wl_list_init(&prev->popup.grab_link);
2103 }
2104 prev = shsurf;
2105 }
2106 wl_list_init(&prev->popup.grab_link);
2107 wl_list_init(&shseat->popup_grab.surfaces_list);
2108 }
2109}
2110
2111static void
2112add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2113{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002114 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002115
2116 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002117 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002118 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002119 /* We must make sure here that this popup was opened after
2120 * a mouse press, and not just by moving around with other
2121 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002122 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002123 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002124
Rob Bradforddfe31052013-06-26 19:49:11 +01002125 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002126 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002127 } else {
2128 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002129 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002130}
2131
2132static void
2133remove_popup_grab(struct shell_surface *shsurf)
2134{
2135 struct shell_seat *shseat = shsurf->popup.shseat;
2136
2137 wl_list_remove(&shsurf->popup.grab_link);
2138 wl_list_init(&shsurf->popup.grab_link);
2139 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002140 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002141 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002142 }
2143}
2144
2145static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002146shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002147{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002148 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002149 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002150 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002151
2152 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002153
Pekka Paalanen483243f2013-03-08 14:56:50 +02002154 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002155 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2156 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002157
Kristian Høgsberge3148752013-05-06 23:19:49 -04002158 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002159 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002160 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002161 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002162 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002163 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002164}
2165
2166static void
2167shell_surface_set_popup(struct wl_client *client,
2168 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002169 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002170 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002171 struct wl_resource *parent_resource,
2172 int32_t x, int32_t y, uint32_t flags)
2173{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002174 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002175
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002176 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002177 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002178 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002179 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002180 shsurf->popup.x = x;
2181 shsurf->popup.y = y;
2182}
2183
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002184static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002185 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002186 shell_surface_move,
2187 shell_surface_resize,
2188 shell_surface_set_toplevel,
2189 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002190 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002191 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002192 shell_surface_set_maximized,
2193 shell_surface_set_title,
2194 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002195};
2196
2197static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002198destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002199{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002200 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2201
Giulio Camuffo5085a752013-03-25 21:42:45 +01002202 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2203 remove_popup_grab(shsurf);
2204 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002205
Alex Wubd3354b2012-04-17 17:20:49 +08002206 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002207 shell_surface_is_top_fullscreen(shsurf))
2208 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002209
Alex Wuaa08e2d2012-03-05 11:01:40 +08002210 if (shsurf->fullscreen.black_surface)
2211 weston_surface_destroy(shsurf->fullscreen.black_surface);
2212
Alex Wubd3354b2012-04-17 17:20:49 +08002213 /* As destroy_resource() use wl_list_for_each_safe(),
2214 * we can always remove the listener.
2215 */
2216 wl_list_remove(&shsurf->surface_destroy_listener.link);
2217 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002218 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002219 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002220
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002221 wl_list_remove(&shsurf->link);
2222 free(shsurf);
2223}
2224
2225static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002226shell_destroy_shell_surface(struct wl_resource *resource)
2227{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002228 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002229
2230 destroy_shell_surface(shsurf);
2231}
2232
2233static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002234shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002235{
2236 struct shell_surface *shsurf = container_of(listener,
2237 struct shell_surface,
2238 surface_destroy_listener);
2239
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002240 if (shsurf->resource)
2241 wl_resource_destroy(shsurf->resource);
2242 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002243 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002244}
2245
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002246static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002247shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002248
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002249static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002250get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002251{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002252 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002253 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002254 else
2255 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002256}
2257
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002258static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002259create_shell_surface(void *shell, struct weston_surface *surface,
2260 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002261{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002262 struct shell_surface *shsurf;
2263
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002264 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002265 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002266 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002267 }
2268
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002269 shsurf = calloc(1, sizeof *shsurf);
2270 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002271 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002272 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002273 }
2274
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002275 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002276 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002277
Tiago Vignattibc052c92012-04-19 16:18:18 +03002278 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002279 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002280 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002281 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002282 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002283 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2284 shsurf->fullscreen.framerate = 0;
2285 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002286 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002287 wl_list_init(&shsurf->fullscreen.transform.link);
2288
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002289 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002290 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002291 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002292 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002293
2294 /* init link so its safe to always remove it in destroy_shell_surface */
2295 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002296 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002297
Pekka Paalanen460099f2012-01-20 16:48:25 +02002298 /* empty when not in use */
2299 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002300 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002301
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002302 wl_list_init(&shsurf->workspace_transform.link);
2303
Pekka Paalanen98262232011-12-01 10:42:22 +02002304 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002305 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002306
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002307 shsurf->client = client;
2308
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002309 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002310}
2311
2312static void
2313shell_get_shell_surface(struct wl_client *client,
2314 struct wl_resource *resource,
2315 uint32_t id,
2316 struct wl_resource *surface_resource)
2317{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002318 struct weston_surface *surface =
2319 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002320 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002321 struct shell_surface *shsurf;
2322
2323 if (get_shell_surface(surface)) {
2324 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002325 WL_DISPLAY_ERROR_INVALID_OBJECT,
2326 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002327 return;
2328 }
2329
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002330 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002331 if (!shsurf) {
2332 wl_resource_post_error(surface_resource,
2333 WL_DISPLAY_ERROR_INVALID_OBJECT,
2334 "surface->configure already set");
2335 return;
2336 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002337
Jason Ekstranda85118c2013-06-27 20:17:02 -05002338 shsurf->resource =
2339 wl_resource_create(client,
2340 &wl_shell_surface_interface, 1, id);
2341 wl_resource_set_implementation(shsurf->resource,
2342 &shell_surface_implementation,
2343 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002344}
2345
2346static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002347 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002348};
2349
Kristian Høgsberg07937562011-04-12 17:25:42 -04002350static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002351shell_fade(struct desktop_shell *shell, enum fade_type type);
2352
2353static int
2354screensaver_timeout(void *data)
2355{
2356 struct desktop_shell *shell = data;
2357
2358 shell_fade(shell, FADE_OUT);
2359
2360 return 1;
2361}
2362
2363static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002364handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002365{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002366 struct desktop_shell *shell =
2367 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002368
Pekka Paalanen18027e52011-12-02 16:31:49 +02002369 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002370
2371 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002372 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002373}
2374
2375static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002376launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002377{
2378 if (shell->screensaver.binding)
2379 return;
2380
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002381 if (!shell->screensaver.path) {
2382 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002383 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002384 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002385
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002386 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002387 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002388 return;
2389 }
2390
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002391 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002392 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002393 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002394 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002395}
2396
2397static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002398terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002399{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002400 if (shell->screensaver.process.pid == 0)
2401 return;
2402
2403 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002404}
2405
2406static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002407configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002408{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002409 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002410
Giulio Camuffo184df502013-02-21 11:29:21 +01002411 if (width == 0)
2412 return;
2413
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002414 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2415 if (s->output == es->output && s != es) {
2416 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002417 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002418 }
2419 }
2420
Giulio Camuffo184df502013-02-21 11:29:21 +01002421 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002422
2423 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002424 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002425 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002426 }
2427}
2428
2429static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002430background_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 -04002431{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002432 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002433
Giulio Camuffo184df502013-02-21 11:29:21 +01002434 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002435}
2436
2437static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002438desktop_shell_set_background(struct wl_client *client,
2439 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002440 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002441 struct wl_resource *surface_resource)
2442{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002443 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002444 struct weston_surface *surface =
2445 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002446
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002447 if (surface->configure) {
2448 wl_resource_post_error(surface_resource,
2449 WL_DISPLAY_ERROR_INVALID_OBJECT,
2450 "surface role already assigned");
2451 return;
2452 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002453
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002454 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002455 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002456 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002457 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002458 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002459 surface->output->width,
2460 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002461}
2462
2463static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002464panel_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 -04002465{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002466 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002467
Giulio Camuffo184df502013-02-21 11:29:21 +01002468 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002469}
2470
2471static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002472desktop_shell_set_panel(struct wl_client *client,
2473 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002474 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002475 struct wl_resource *surface_resource)
2476{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002477 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002478 struct weston_surface *surface =
2479 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002480
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002481 if (surface->configure) {
2482 wl_resource_post_error(surface_resource,
2483 WL_DISPLAY_ERROR_INVALID_OBJECT,
2484 "surface role already assigned");
2485 return;
2486 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002487
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002488 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002489 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002490 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002491 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002492 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002493 surface->output->width,
2494 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002495}
2496
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002497static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002498lock_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 -04002499{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002500 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002501
Giulio Camuffo184df502013-02-21 11:29:21 +01002502 if (width == 0)
2503 return;
2504
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002505 surface->geometry.width = width;
2506 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002507 center_on_output(surface, get_default_output(shell->compositor));
2508
2509 if (!weston_surface_is_mapped(surface)) {
2510 wl_list_insert(&shell->lock_layer.surface_list,
2511 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002512 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002513 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002514 }
2515}
2516
2517static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002518handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002519{
Tiago Vignattibe143262012-04-16 17:31:41 +03002520 struct desktop_shell *shell =
2521 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002522
Martin Minarik6d118362012-06-07 18:01:59 +02002523 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002524 shell->lock_surface = NULL;
2525}
2526
2527static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002528desktop_shell_set_lock_surface(struct wl_client *client,
2529 struct wl_resource *resource,
2530 struct wl_resource *surface_resource)
2531{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002532 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002533 struct weston_surface *surface =
2534 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002535
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002536 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002537
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002538 if (!shell->locked)
2539 return;
2540
Pekka Paalanen98262232011-12-01 10:42:22 +02002541 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002542
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002543 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002544 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002545 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002546
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002547 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002548 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002549}
2550
2551static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002552resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002553{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002554 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002555
Pekka Paalanen77346a62011-11-30 16:26:35 +02002556 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002557
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002558 wl_list_remove(&shell->lock_layer.link);
2559 wl_list_insert(&shell->compositor->cursor_layer.link,
2560 &shell->fullscreen_layer.link);
2561 wl_list_insert(&shell->fullscreen_layer.link,
2562 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002563 if (shell->showing_input_panels) {
2564 wl_list_insert(&shell->panel_layer.link,
2565 &shell->input_panel_layer.link);
2566 wl_list_insert(&shell->input_panel_layer.link,
2567 &ws->layer.link);
2568 } else {
2569 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2570 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002571
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002572 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002573
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002574 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002575 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002576 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002577}
2578
2579static void
2580desktop_shell_unlock(struct wl_client *client,
2581 struct wl_resource *resource)
2582{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002583 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002584
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002585 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002586
2587 if (shell->locked)
2588 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002589}
2590
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002591static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002592desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002593 struct wl_resource *resource,
2594 struct wl_resource *surface_resource)
2595{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002596 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002597
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002598 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002599}
2600
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002601static void
2602desktop_shell_desktop_ready(struct wl_client *client,
2603 struct wl_resource *resource)
2604{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002605 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002606
2607 shell_fade_startup(shell);
2608}
2609
Kristian Høgsberg75840622011-09-06 13:48:16 -04002610static const struct desktop_shell_interface desktop_shell_implementation = {
2611 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002612 desktop_shell_set_panel,
2613 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002614 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002615 desktop_shell_set_grab_surface,
2616 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002617};
2618
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002619static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002620get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002621{
2622 struct shell_surface *shsurf;
2623
2624 shsurf = get_shell_surface(surface);
2625 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002626 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002627 return shsurf->type;
2628}
2629
Kristian Høgsberg75840622011-09-06 13:48:16 -04002630static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002631move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002632{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002633 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002634 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002635 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002636 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002637
Pekka Paalanen01388e22013-04-25 13:57:44 +03002638 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002639 if (surface == NULL)
2640 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002641
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002642 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002643 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2644 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002645 return;
2646
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002647 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002648}
2649
2650static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002651resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002652{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002653 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002654 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002655 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002656 uint32_t edges = 0;
2657 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002658 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002659
Pekka Paalanen01388e22013-04-25 13:57:44 +03002660 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002661 if (surface == NULL)
2662 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002663
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002664 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002665 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2666 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002667 return;
2668
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002669 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002670 wl_fixed_to_int(seat->pointer->grab_x),
2671 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002672 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002673
Pekka Paalanen60921e52012-01-25 15:55:43 +02002674 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002675 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002676 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002677 edges |= 0;
2678 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002679 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002680
Pekka Paalanen60921e52012-01-25 15:55:43 +02002681 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002682 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002683 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002684 edges |= 0;
2685 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002686 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002687
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002688 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002689}
2690
2691static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002692surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002693 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002694{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002695 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002696 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002697 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002698 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002699 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002700
Pekka Paalanen01388e22013-04-25 13:57:44 +03002701 /* XXX: broken for windows containing sub-surfaces */
2702 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002703 if (surface == NULL)
2704 return;
2705
2706 shsurf = get_shell_surface(surface);
2707 if (!shsurf)
2708 return;
2709
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002710 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002711
Scott Moreau02709af2012-05-22 01:54:10 -06002712 if (surface->alpha > 1.0)
2713 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002714 if (surface->alpha < step)
2715 surface->alpha = step;
2716
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002717 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002718 weston_surface_damage(surface);
2719}
2720
2721static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002722do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002723 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002724{
Daniel Stone37816df2012-05-16 18:45:18 +01002725 struct weston_seat *ws = (struct weston_seat *) seat;
2726 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002727 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002728 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002729
2730 wl_list_for_each(output, &compositor->output_list, link) {
2731 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002732 wl_fixed_to_double(seat->pointer->x),
2733 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002734 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002735 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002736 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002737 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002738 increment = -output->zoom.increment;
2739 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002740 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002741 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002742 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002743 else
2744 increment = 0;
2745
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002746 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002747
Scott Moreaue6603982012-06-11 13:07:51 -06002748 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002749 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002750 else if (output->zoom.level > output->zoom.max_level)
2751 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002752 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002753 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002754 output->disable_planes++;
2755 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002756
Scott Moreaue6603982012-06-11 13:07:51 -06002757 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002758
Scott Moreau8dacaab2012-06-17 18:10:58 -06002759 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002760 }
2761 }
2762}
2763
Scott Moreauccbf29d2012-02-22 14:21:41 -07002764static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002765zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002766 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002767{
2768 do_zoom(seat, time, 0, axis, value);
2769}
2770
2771static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002772zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002773 void *data)
2774{
2775 do_zoom(seat, time, key, 0, 0);
2776}
2777
2778static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002779terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002780 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002781{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002782 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002783
Daniel Stone325fc2d2012-05-30 16:31:58 +01002784 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002785}
2786
2787static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002788rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002789{
2790 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002791 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002792 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002793 struct shell_surface *shsurf = rotate->base.shsurf;
2794 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002795 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002796
2797 if (!shsurf)
2798 return;
2799
2800 surface = shsurf->surface;
2801
2802 cx = 0.5f * surface->geometry.width;
2803 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002804
Daniel Stone37816df2012-05-16 18:45:18 +01002805 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2806 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002807 r = sqrtf(dx * dx + dy * dy);
2808
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002809 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002810 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002811
2812 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002813 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002814 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002815
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002816 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002817 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002818
2819 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002820 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002821 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002822 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002823 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002824
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002825 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002826 &shsurf->surface->geometry.transformation_list,
2827 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002828 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002829 wl_list_init(&shsurf->rotation.transform.link);
2830 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002831 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002832 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002833
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002834 /* We need to adjust the position of the surface
2835 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002836 cposx = surface->geometry.x + cx;
2837 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002838 dposx = rotate->center.x - cposx;
2839 dposy = rotate->center.y - cposy;
2840 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002841 weston_surface_set_position(surface,
2842 surface->geometry.x + dposx,
2843 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002844 }
2845
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002846 /* Repaint implies weston_surface_update_transform(), which
2847 * lazily applies the damage due to rotation update.
2848 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002849 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002850}
2851
2852static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002853rotate_grab_button(struct weston_pointer_grab *grab,
2854 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002855{
2856 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002857 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002858 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002859 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002860 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002861
Daniel Stone4dbadb12012-05-30 16:31:51 +01002862 if (pointer->button_count == 0 &&
2863 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002864 if (shsurf)
2865 weston_matrix_multiply(&shsurf->rotation.rotation,
2866 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002867 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002868 free(rotate);
2869 }
2870}
2871
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002872static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002873 noop_grab_focus,
2874 rotate_grab_motion,
2875 rotate_grab_button,
2876};
2877
2878static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002879surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002880{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002881 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002882 float dx, dy;
2883 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002884
Pekka Paalanen460099f2012-01-20 16:48:25 +02002885 rotate = malloc(sizeof *rotate);
2886 if (!rotate)
2887 return;
2888
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002889 weston_surface_to_global_float(surface->surface,
Louis-Francis Ratté-Boulianne9a4f10f2013-07-03 15:58:22 +02002890 surface->surface->geometry.width * 0.5f,
2891 surface->surface->geometry.height * 0.5f,
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002892 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002893
Daniel Stone37816df2012-05-16 18:45:18 +01002894 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2895 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002896 r = sqrtf(dx * dx + dy * dy);
2897 if (r > 20.0f) {
2898 struct weston_matrix inverse;
2899
2900 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002901 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002902 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002903
2904 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002905 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002906 } else {
2907 weston_matrix_init(&surface->rotation.rotation);
2908 weston_matrix_init(&rotate->rotation);
2909 }
2910
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002911 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2912 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002913}
2914
2915static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002916rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002917 void *data)
2918{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002919 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002920 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002921 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002922 struct shell_surface *surface;
2923
Pekka Paalanen01388e22013-04-25 13:57:44 +03002924 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002925 if (base_surface == NULL)
2926 return;
2927
2928 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002929 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2930 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002931 return;
2932
2933 surface_rotate(surface, seat);
2934}
2935
2936static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002937lower_fullscreen_layer(struct desktop_shell *shell)
2938{
2939 struct workspace *ws;
2940 struct weston_surface *surface, *prev;
2941
2942 ws = get_current_workspace(shell);
2943 wl_list_for_each_reverse_safe(surface, prev,
2944 &shell->fullscreen_layer.surface_list,
2945 layer_link)
2946 weston_surface_restack(surface, &ws->layer.surface_list);
2947}
2948
2949static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002950activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002951 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002952{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002953 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002954 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002955 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002956
Pekka Paalanen01388e22013-04-25 13:57:44 +03002957 main_surface = weston_surface_get_main_surface(es);
2958
Daniel Stone37816df2012-05-16 18:45:18 +01002959 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002960
Jonas Ådahl8538b222012-08-29 22:13:03 +02002961 state = ensure_focus_state(shell, seat);
2962 if (state == NULL)
2963 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002964
2965 state->keyboard_focus = es;
2966 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002967 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002968
Pekka Paalanen01388e22013-04-25 13:57:44 +03002969 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002970 case SHELL_SURFACE_FULLSCREEN:
2971 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002972 shell_stack_fullscreen(get_shell_surface(main_surface));
2973 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002974 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002975 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002976 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002977 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002978 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002979 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002980 }
2981}
2982
Alex Wu21858432012-04-01 20:13:08 +08002983/* no-op func for checking black surface */
2984static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002985black_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 +08002986{
2987}
2988
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002989static bool
Alex Wu21858432012-04-01 20:13:08 +08002990is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2991{
2992 if (es->configure == black_surface_configure) {
2993 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002994 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002995 return true;
2996 }
2997 return false;
2998}
2999
Kristian Høgsberg75840622011-09-06 13:48:16 -04003000static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003001click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003002 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003003{
Daniel Stone37816df2012-05-16 18:45:18 +01003004 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003005 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003006 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003007 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003008
Daniel Stone37816df2012-05-16 18:45:18 +01003009 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003010 if (!focus)
3011 return;
3012
Pekka Paalanen01388e22013-04-25 13:57:44 +03003013 if (is_black_surface(focus, &main_surface))
3014 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003015
Pekka Paalanen01388e22013-04-25 13:57:44 +03003016 main_surface = weston_surface_get_main_surface(focus);
3017 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003018 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003019
Daniel Stone325fc2d2012-05-30 16:31:58 +01003020 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003021 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003022}
3023
3024static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003025lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003026{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003027 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003028
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003029 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003030 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003031 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003032 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003033
3034 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003035
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003036 /* Hide all surfaces by removing the fullscreen, panel and
3037 * toplevel layers. This way nothing else can show or receive
3038 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003039
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003040 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003041 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003042 if (shell->showing_input_panels)
3043 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003044 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003045 wl_list_insert(&shell->compositor->cursor_layer.link,
3046 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003047
Pekka Paalanen77346a62011-11-30 16:26:35 +02003048 launch_screensaver(shell);
3049
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003050 /* TODO: disable bindings that should not work while locked. */
3051
3052 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003053}
3054
3055static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003056unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003057{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003058 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003059 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003060 return;
3061 }
3062
3063 /* If desktop-shell client has gone away, unlock immediately. */
3064 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003065 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003066 return;
3067 }
3068
3069 if (shell->prepare_event_sent)
3070 return;
3071
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003072 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003073 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003074}
3075
3076static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003077shell_fade_done(struct weston_surface_animation *animation, void *data)
3078{
3079 struct desktop_shell *shell = data;
3080
3081 shell->fade.animation = NULL;
3082
3083 switch (shell->fade.type) {
3084 case FADE_IN:
3085 weston_surface_destroy(shell->fade.surface);
3086 shell->fade.surface = NULL;
3087 break;
3088 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003089 lock(shell);
3090 break;
3091 }
3092}
3093
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003094static struct weston_surface *
3095shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003096{
3097 struct weston_compositor *compositor = shell->compositor;
3098 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003099
3100 surface = weston_surface_create(compositor);
3101 if (!surface)
3102 return NULL;
3103
3104 weston_surface_configure(surface, 0, 0, 8192, 8192);
3105 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3106 wl_list_insert(&compositor->fade_layer.surface_list,
3107 &surface->layer_link);
3108 pixman_region32_init(&surface->input);
3109
3110 return surface;
3111}
3112
3113static void
3114shell_fade(struct desktop_shell *shell, enum fade_type type)
3115{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003116 float tint;
3117
3118 switch (type) {
3119 case FADE_IN:
3120 tint = 0.0;
3121 break;
3122 case FADE_OUT:
3123 tint = 1.0;
3124 break;
3125 default:
3126 weston_log("shell: invalid fade type\n");
3127 return;
3128 }
3129
3130 shell->fade.type = type;
3131
3132 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003133 shell->fade.surface = shell_fade_create_surface(shell);
3134 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003135 return;
3136
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003137 shell->fade.surface->alpha = 1.0 - tint;
3138 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003139 }
3140
3141 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003142 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003143 else
3144 shell->fade.animation =
3145 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003146 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003147 shell_fade_done, shell);
3148}
3149
3150static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003151do_shell_fade_startup(void *data)
3152{
3153 struct desktop_shell *shell = data;
3154
3155 shell_fade(shell, FADE_IN);
3156}
3157
3158static void
3159shell_fade_startup(struct desktop_shell *shell)
3160{
3161 struct wl_event_loop *loop;
3162
3163 if (!shell->fade.startup_timer)
3164 return;
3165
3166 wl_event_source_remove(shell->fade.startup_timer);
3167 shell->fade.startup_timer = NULL;
3168
3169 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3170 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3171}
3172
3173static int
3174fade_startup_timeout(void *data)
3175{
3176 struct desktop_shell *shell = data;
3177
3178 shell_fade_startup(shell);
3179 return 0;
3180}
3181
3182static void
3183shell_fade_init(struct desktop_shell *shell)
3184{
3185 /* Make compositor output all black, and wait for the desktop-shell
3186 * client to signal it is ready, then fade in. The timer triggers a
3187 * fade-in, in case the desktop-shell client takes too long.
3188 */
3189
3190 struct wl_event_loop *loop;
3191
3192 if (shell->fade.surface != NULL) {
3193 weston_log("%s: warning: fade surface already exists\n",
3194 __func__);
3195 return;
3196 }
3197
3198 shell->fade.surface = shell_fade_create_surface(shell);
3199 if (!shell->fade.surface)
3200 return;
3201
3202 weston_surface_update_transform(shell->fade.surface);
3203 weston_surface_damage(shell->fade.surface);
3204
3205 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3206 shell->fade.startup_timer =
3207 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3208 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3209}
3210
3211static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003212idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003213{
3214 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003215 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003216
3217 shell_fade(shell, FADE_OUT);
3218 /* lock() is called from shell_fade_done() */
3219}
3220
3221static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003222wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003223{
3224 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003225 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003226
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003227 unlock(shell);
3228}
3229
3230static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003231show_input_panels(struct wl_listener *listener, void *data)
3232{
3233 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003234 container_of(listener, struct desktop_shell,
3235 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003236 struct input_panel_surface *surface, *next;
3237 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003238
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003239 shell->text_input.surface = (struct weston_surface*)data;
3240
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003241 if (shell->showing_input_panels)
3242 return;
3243
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003244 shell->showing_input_panels = true;
3245
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003246 if (!shell->locked)
3247 wl_list_insert(&shell->panel_layer.link,
3248 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003249
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003250 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003251 &shell->input_panel.surfaces, link) {
3252 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003253 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003254 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003255 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003256 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003257 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003258 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003259 weston_surface_damage(ws);
3260 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003261 }
3262}
3263
3264static void
3265hide_input_panels(struct wl_listener *listener, void *data)
3266{
3267 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003268 container_of(listener, struct desktop_shell,
3269 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003270 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003271
Jan Arne Petersen61381972013-01-31 15:52:21 +01003272 if (!shell->showing_input_panels)
3273 return;
3274
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003275 shell->showing_input_panels = false;
3276
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003277 if (!shell->locked)
3278 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003279
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003280 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003281 &shell->input_panel_layer.surface_list, layer_link)
3282 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003283}
3284
3285static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003286update_input_panels(struct wl_listener *listener, void *data)
3287{
3288 struct desktop_shell *shell =
3289 container_of(listener, struct desktop_shell,
3290 update_input_panel_listener);
3291
3292 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3293}
3294
3295static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003296center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003297{
Giulio Camuffob8366642013-04-25 13:57:46 +03003298 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003299 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003300
Giulio Camuffob8366642013-04-25 13:57:46 +03003301 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3302
3303 x = output->x + (output->width - width) / 2 - surf_x / 2;
3304 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003305
3306 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003307}
3308
3309static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003310weston_surface_set_initial_position (struct weston_surface *surface,
3311 struct desktop_shell *shell)
3312{
3313 struct weston_compositor *compositor = shell->compositor;
3314 int ix = 0, iy = 0;
3315 int range_x, range_y;
3316 int dx, dy, x, y, panel_height;
3317 struct weston_output *output, *target_output = NULL;
3318 struct weston_seat *seat;
3319
3320 /* As a heuristic place the new window on the same output as the
3321 * pointer. Falling back to the output containing 0, 0.
3322 *
3323 * TODO: Do something clever for touch too?
3324 */
3325 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003326 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003327 ix = wl_fixed_to_int(seat->pointer->x);
3328 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003329 break;
3330 }
3331 }
3332
3333 wl_list_for_each(output, &compositor->output_list, link) {
3334 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3335 target_output = output;
3336 break;
3337 }
3338 }
3339
3340 if (!target_output) {
3341 weston_surface_set_position(surface, 10 + random() % 400,
3342 10 + random() % 400);
3343 return;
3344 }
3345
3346 /* Valid range within output where the surface will still be onscreen.
3347 * If this is negative it means that the surface is bigger than
3348 * output.
3349 */
3350 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003351 range_x = target_output->width - surface->geometry.width;
3352 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003353 surface->geometry.height;
3354
Rob Bradford4cb88c72012-08-13 15:18:44 +01003355 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003356 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003357 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003358 dx = 0;
3359
3360 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003361 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003362 else
3363 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003364
3365 x = target_output->x + dx;
3366 y = target_output->y + dy;
3367
3368 weston_surface_set_position (surface, x, y);
3369}
3370
3371static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003372map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003373 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003374{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003375 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003376 struct shell_surface *shsurf = get_shell_surface(surface);
3377 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003378 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003379 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003380 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003381 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003382 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003383
Pekka Paalanen60921e52012-01-25 15:55:43 +02003384 surface->geometry.width = width;
3385 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003386 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003387
3388 /* initial positioning, see also configure() */
3389 switch (surface_type) {
3390 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003391 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003392 break;
Alex Wu4539b082012-03-01 12:57:46 +08003393 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003394 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003395 shell_map_fullscreen(shsurf);
3396 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003397 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003398 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003399 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003400 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3401 NULL, NULL);
3402 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3403 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003404 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003405 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003406 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003407 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003408 case SHELL_SURFACE_NONE:
3409 weston_surface_set_position(surface,
3410 surface->geometry.x + sx,
3411 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003412 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003413 default:
3414 ;
3415 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003416
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003417 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003418 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003419 case SHELL_SURFACE_POPUP:
3420 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003421 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003422 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3423 break;
Alex Wu4539b082012-03-01 12:57:46 +08003424 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003425 case SHELL_SURFACE_NONE:
3426 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003427 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003428 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003429 ws = get_current_workspace(shell);
3430 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003431 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003432 }
3433
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003434 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003435 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003436 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3437 surface->output = shsurf->output;
3438 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003439
Juan Zhao7bb92f02011-12-15 11:31:51 -05003440 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003441 /* XXX: xwayland's using the same fields for transient type */
3442 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003443 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003444 if (shsurf->transient.flags ==
3445 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3446 break;
3447 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003448 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003449 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003450 if (!shell->locked) {
3451 wl_list_for_each(seat, &compositor->seat_list, link)
3452 activate(shell, surface, seat);
3453 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003454 break;
3455 default:
3456 break;
3457 }
3458
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003459 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003460 {
3461 switch (shell->win_animation_type) {
3462 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003463 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003464 break;
3465 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003466 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003467 break;
3468 default:
3469 break;
3470 }
3471 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003472}
3473
3474static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003475configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003476 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003477{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003478 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3479 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003480 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003481
Pekka Paalanen77346a62011-11-30 16:26:35 +02003482 shsurf = get_shell_surface(surface);
3483 if (shsurf)
3484 surface_type = shsurf->type;
3485
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003486 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003487
3488 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003489 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003490 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003491 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003492 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003493 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003494 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003495 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3496 NULL, NULL);
3497 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003498 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003499 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003500 break;
Alex Wu4539b082012-03-01 12:57:46 +08003501 case SHELL_SURFACE_TOPLEVEL:
3502 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003503 default:
3504 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003505 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003506
Alex Wu4539b082012-03-01 12:57:46 +08003507 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003508 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003509 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003510
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003511 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003512 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003513 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003514}
3515
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003516static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003517shell_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 +03003518{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003519 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003520 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003521
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003522 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003523
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003524 if (!weston_surface_is_mapped(es) &&
3525 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003526 remove_popup_grab(shsurf);
3527 }
3528
Giulio Camuffo184df502013-02-21 11:29:21 +01003529 if (width == 0)
3530 return;
3531
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003532 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003533 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003534 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003535 type_changed = 1;
3536 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003537
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003538 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003539 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003540 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003541 es->geometry.width != width ||
3542 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003543 float from_x, from_y;
3544 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003545
3546 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3547 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3548 configure(shell, es,
3549 es->geometry.x + to_x - from_x,
3550 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003551 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003552 }
3553}
3554
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003555static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003556
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003557static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003558desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003559{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003560 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003561 struct desktop_shell *shell =
3562 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003563
3564 shell->child.process.pid = 0;
3565 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003566
3567 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3568 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003569 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003570 shell->child.deathstamp = time;
3571 shell->child.deathcount = 0;
3572 }
3573
3574 shell->child.deathcount++;
3575 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003576 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003577 return;
3578 }
3579
Martin Minarik6d118362012-06-07 18:01:59 +02003580 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003581 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003582 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003583}
3584
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003585static void
3586launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003587{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003588 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003589 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003590
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003591 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003592 &shell->child.process,
3593 shell_exe,
3594 desktop_shell_sigchld);
3595
3596 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003597 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003598}
3599
3600static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003601bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3602{
Tiago Vignattibe143262012-04-16 17:31:41 +03003603 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003604 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003605
Jason Ekstranda85118c2013-06-27 20:17:02 -05003606 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3607 if (resource)
3608 wl_resource_set_implementation(resource, &shell_implementation,
3609 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003610}
3611
Kristian Høgsberg75840622011-09-06 13:48:16 -04003612static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003613unbind_desktop_shell(struct wl_resource *resource)
3614{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003615 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003616
3617 if (shell->locked)
3618 resume_desktop(shell);
3619
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003620 shell->child.desktop_shell = NULL;
3621 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003622}
3623
3624static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003625bind_desktop_shell(struct wl_client *client,
3626 void *data, uint32_t version, uint32_t id)
3627{
Tiago Vignattibe143262012-04-16 17:31:41 +03003628 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003629 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003630
Jason Ekstranda85118c2013-06-27 20:17:02 -05003631 resource = wl_resource_create(client, &desktop_shell_interface,
3632 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003633
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003634 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003635 wl_resource_set_implementation(resource,
3636 &desktop_shell_implementation,
3637 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003638 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003639
3640 if (version < 2)
3641 shell_fade_startup(shell);
3642
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003643 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003644 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003645
3646 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3647 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003648 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003649}
3650
Pekka Paalanen6e168112011-11-24 11:34:05 +02003651static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003652screensaver_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 -04003653{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003654 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003655
Giulio Camuffo184df502013-02-21 11:29:21 +01003656 if (width == 0)
3657 return;
3658
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003659 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003660 if (!shell->locked)
3661 return;
3662
3663 center_on_output(surface, surface->output);
3664
3665 if (wl_list_empty(&surface->layer_link)) {
3666 wl_list_insert(shell->lock_layer.surface_list.prev,
3667 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003668 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003669 wl_event_source_timer_update(shell->screensaver.timer,
3670 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003671 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003672 }
3673}
3674
3675static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003676screensaver_set_surface(struct wl_client *client,
3677 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003678 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003679 struct wl_resource *output_resource)
3680{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003681 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003682 struct weston_surface *surface =
3683 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003684 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003685
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003686 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003687 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003688 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003689}
3690
3691static const struct screensaver_interface screensaver_implementation = {
3692 screensaver_set_surface
3693};
3694
3695static void
3696unbind_screensaver(struct wl_resource *resource)
3697{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003698 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003699
Pekka Paalanen77346a62011-11-30 16:26:35 +02003700 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003701}
3702
3703static void
3704bind_screensaver(struct wl_client *client,
3705 void *data, uint32_t version, uint32_t id)
3706{
Tiago Vignattibe143262012-04-16 17:31:41 +03003707 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003708 struct wl_resource *resource;
3709
Jason Ekstranda85118c2013-06-27 20:17:02 -05003710 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003711
Pekka Paalanen77346a62011-11-30 16:26:35 +02003712 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003713 wl_resource_set_implementation(resource,
3714 &screensaver_implementation,
3715 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003716 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003717 return;
3718 }
3719
3720 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3721 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003722 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003723}
3724
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003725static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003726input_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 -04003727{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003728 struct input_panel_surface *ip_surface = surface->configure_private;
3729 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003730 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003731 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003732 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003733
Giulio Camuffo184df502013-02-21 11:29:21 +01003734 if (width == 0)
3735 return;
3736
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003737 if (!weston_surface_is_mapped(surface)) {
3738 if (!shell->showing_input_panels)
3739 return;
3740
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003741 show_surface = 1;
3742 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003743
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003744 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003745
3746 if (ip_surface->panel) {
3747 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3748 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3749 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003750 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003751
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003752 x = ip_surface->output->x + (mode->width - width) / 2;
3753 y = ip_surface->output->y + mode->height - height;
3754 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003755
3756 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003757 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003758 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003759
3760 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003761 wl_list_insert(&shell->input_panel_layer.surface_list,
3762 &surface->layer_link);
3763 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003764 weston_surface_damage(surface);
3765 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3766 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003767}
3768
3769static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003770destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003771{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003772 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3773
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003774 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003775 wl_list_remove(&input_panel_surface->link);
3776
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003777 input_panel_surface->surface->configure = NULL;
3778
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003779 free(input_panel_surface);
3780}
3781
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003782static struct input_panel_surface *
3783get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003784{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003785 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003786 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003787 } else {
3788 return NULL;
3789 }
3790}
3791
3792static void
3793input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3794{
3795 struct input_panel_surface *ipsurface = container_of(listener,
3796 struct input_panel_surface,
3797 surface_destroy_listener);
3798
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003799 if (ipsurface->resource) {
3800 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003801 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003802 destroy_input_panel_surface(ipsurface);
3803 }
3804}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003805
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003806static struct input_panel_surface *
3807create_input_panel_surface(struct desktop_shell *shell,
3808 struct weston_surface *surface)
3809{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003810 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003811
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003812 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3813 if (!input_panel_surface)
3814 return NULL;
3815
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003816 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003817 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003818
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003819 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003820
3821 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003822
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003823 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003824 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003825 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003826 &input_panel_surface->surface_destroy_listener);
3827
3828 wl_list_init(&input_panel_surface->link);
3829
3830 return input_panel_surface;
3831}
3832
3833static void
3834input_panel_surface_set_toplevel(struct wl_client *client,
3835 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003836 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003837 uint32_t position)
3838{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003839 struct input_panel_surface *input_panel_surface =
3840 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003841 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003842
3843 wl_list_insert(&shell->input_panel.surfaces,
3844 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003845
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003846 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003847 input_panel_surface->panel = 0;
3848}
3849
3850static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003851input_panel_surface_set_overlay_panel(struct wl_client *client,
3852 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003853{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003854 struct input_panel_surface *input_panel_surface =
3855 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003856 struct desktop_shell *shell = input_panel_surface->shell;
3857
3858 wl_list_insert(&shell->input_panel.surfaces,
3859 &input_panel_surface->link);
3860
3861 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003862}
3863
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003864static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003865 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003866 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003867};
3868
3869static void
3870destroy_input_panel_surface_resource(struct wl_resource *resource)
3871{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003872 struct input_panel_surface *ipsurf =
3873 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003874
3875 destroy_input_panel_surface(ipsurf);
3876}
3877
3878static void
3879input_panel_get_input_panel_surface(struct wl_client *client,
3880 struct wl_resource *resource,
3881 uint32_t id,
3882 struct wl_resource *surface_resource)
3883{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003884 struct weston_surface *surface =
3885 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003886 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003887 struct input_panel_surface *ipsurf;
3888
3889 if (get_input_panel_surface(surface)) {
3890 wl_resource_post_error(surface_resource,
3891 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003892 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003893 return;
3894 }
3895
3896 ipsurf = create_input_panel_surface(shell, surface);
3897 if (!ipsurf) {
3898 wl_resource_post_error(surface_resource,
3899 WL_DISPLAY_ERROR_INVALID_OBJECT,
3900 "surface->configure already set");
3901 return;
3902 }
3903
Jason Ekstranda85118c2013-06-27 20:17:02 -05003904 ipsurf->resource =
3905 wl_resource_create(client,
3906 &wl_input_panel_surface_interface, 1, id);
3907 wl_resource_set_implementation(ipsurf->resource,
3908 &input_panel_surface_implementation,
3909 ipsurf,
3910 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003911}
3912
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003913static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003914 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003915};
3916
3917static void
3918unbind_input_panel(struct wl_resource *resource)
3919{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003920 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003921
3922 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003923}
3924
3925static void
3926bind_input_panel(struct wl_client *client,
3927 void *data, uint32_t version, uint32_t id)
3928{
3929 struct desktop_shell *shell = data;
3930 struct wl_resource *resource;
3931
Jason Ekstranda85118c2013-06-27 20:17:02 -05003932 resource = wl_resource_create(client,
3933 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003934
3935 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003936 wl_resource_set_implementation(resource,
3937 &input_panel_implementation,
3938 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003939 shell->input_panel.binding = resource;
3940 return;
3941 }
3942
3943 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3944 "interface object already bound");
3945 wl_resource_destroy(resource);
3946}
3947
Kristian Høgsberg07045392012-02-19 18:52:44 -05003948struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003949 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003950 struct weston_surface *current;
3951 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003952 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003953};
3954
3955static void
3956switcher_next(struct switcher *switcher)
3957{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003958 struct weston_surface *surface;
3959 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003960 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003961 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003962
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003963 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003964 switch (get_shell_surface_type(surface)) {
3965 case SHELL_SURFACE_TOPLEVEL:
3966 case SHELL_SURFACE_FULLSCREEN:
3967 case SHELL_SURFACE_MAXIMIZED:
3968 if (first == NULL)
3969 first = surface;
3970 if (prev == switcher->current)
3971 next = surface;
3972 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003973 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003974 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003975 weston_surface_damage(surface);
3976 break;
3977 default:
3978 break;
3979 }
Alex Wu1659daa2012-04-01 20:13:09 +08003980
3981 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003982 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003983 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003984 weston_surface_damage(surface);
3985 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003986 }
3987
3988 if (next == NULL)
3989 next = first;
3990
Alex Wu07b26062012-03-12 16:06:01 +08003991 if (next == NULL)
3992 return;
3993
Kristian Høgsberg07045392012-02-19 18:52:44 -05003994 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003995 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003996
3997 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003998 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003999
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004000 shsurf = get_shell_surface(switcher->current);
4001 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004002 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004003}
4004
4005static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004006switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004007{
4008 struct switcher *switcher =
4009 container_of(listener, struct switcher, listener);
4010
4011 switcher_next(switcher);
4012}
4013
4014static void
Daniel Stone351eb612012-05-31 15:27:47 -04004015switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004016{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004017 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004018 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004019 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004020
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004021 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004022 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004023 weston_surface_damage(surface);
4024 }
4025
Alex Wu07b26062012-03-12 16:06:01 +08004026 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004027 activate(switcher->shell, switcher->current,
4028 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004029 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004030 weston_keyboard_end_grab(keyboard);
4031 if (keyboard->input_method_resource)
4032 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004033 free(switcher);
4034}
4035
4036static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004037switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004038 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004039{
4040 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004041 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004042
Daniel Stonec9785ea2012-05-30 16:31:52 +01004043 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004044 switcher_next(switcher);
4045}
4046
4047static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004048switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004049 uint32_t mods_depressed, uint32_t mods_latched,
4050 uint32_t mods_locked, uint32_t group)
4051{
4052 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004053 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004054
Daniel Stone351eb612012-05-31 15:27:47 -04004055 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4056 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004057}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004058
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004059static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004060 switcher_key,
4061 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004062};
4063
4064static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004065switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004066 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004067{
Tiago Vignattibe143262012-04-16 17:31:41 +03004068 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004069 struct switcher *switcher;
4070
4071 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004072 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004073 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004074 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004075 wl_list_init(&switcher->listener.link);
4076
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004077 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004078 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004079 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004080 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4081 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004082 switcher_next(switcher);
4083}
4084
Pekka Paalanen3c647232011-12-22 13:43:43 +02004085static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004086backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004087 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004088{
4089 struct weston_compositor *compositor = data;
4090 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004091 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004092
4093 /* TODO: we're limiting to simple use cases, where we assume just
4094 * control on the primary display. We'd have to extend later if we
4095 * ever get support for setting backlights on random desktop LCD
4096 * panels though */
4097 output = get_default_output(compositor);
4098 if (!output)
4099 return;
4100
4101 if (!output->set_backlight)
4102 return;
4103
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004104 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4105 backlight_new = output->backlight_current - 25;
4106 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4107 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004108
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004109 if (backlight_new < 5)
4110 backlight_new = 5;
4111 if (backlight_new > 255)
4112 backlight_new = 255;
4113
4114 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004115 output->set_backlight(output, output->backlight_current);
4116}
4117
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004118struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004119 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004120 struct weston_seat *seat;
4121 uint32_t key[2];
4122 int key_released[2];
4123};
4124
4125static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004126debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004127 uint32_t key, uint32_t state)
4128{
4129 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4130 struct wl_resource *resource;
4131 struct wl_display *display;
4132 uint32_t serial;
4133 int send = 0, terminate = 0;
4134 int check_binding = 1;
4135 int i;
4136
4137 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4138 /* Do not run bindings on key releases */
4139 check_binding = 0;
4140
4141 for (i = 0; i < 2; i++)
4142 if (key == db->key[i])
4143 db->key_released[i] = 1;
4144
4145 if (db->key_released[0] && db->key_released[1]) {
4146 /* All key releases been swalled so end the grab */
4147 terminate = 1;
4148 } else if (key != db->key[0] && key != db->key[1]) {
4149 /* Should not swallow release of other keys */
4150 send = 1;
4151 }
4152 } else if (key == db->key[0] && !db->key_released[0]) {
4153 /* Do not check bindings for the first press of the binding
4154 * key. This allows it to be used as a debug shortcut.
4155 * We still need to swallow this event. */
4156 check_binding = 0;
4157 } else if (db->key[1]) {
4158 /* If we already ran a binding don't process another one since
4159 * we can't keep track of all the binding keys that were
4160 * pressed in order to swallow the release events. */
4161 send = 1;
4162 check_binding = 0;
4163 }
4164
4165 if (check_binding) {
4166 struct weston_compositor *ec = db->seat->compositor;
4167
4168 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4169 key, state)) {
4170 /* We ran a binding so swallow the press and keep the
4171 * grab to swallow the released too. */
4172 send = 0;
4173 terminate = 0;
4174 db->key[1] = key;
4175 } else {
4176 /* Terminate the grab since the key pressed is not a
4177 * debug binding key. */
4178 send = 1;
4179 terminate = 1;
4180 }
4181 }
4182
4183 if (send) {
4184 resource = grab->keyboard->focus_resource;
4185
4186 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004187 display = wl_client_get_display(wl_resource_get_client(resource));
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004188 serial = wl_display_next_serial(display);
4189 wl_keyboard_send_key(resource, serial, time, key, state);
4190 }
4191 }
4192
4193 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004194 weston_keyboard_end_grab(grab->keyboard);
4195 if (grab->keyboard->input_method_resource)
4196 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004197 free(db);
4198 }
4199}
4200
4201static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004202debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004203 uint32_t mods_depressed, uint32_t mods_latched,
4204 uint32_t mods_locked, uint32_t group)
4205{
4206 struct wl_resource *resource;
4207
4208 resource = grab->keyboard->focus_resource;
4209 if (!resource)
4210 return;
4211
4212 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4213 mods_latched, mods_locked, group);
4214}
4215
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004216struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004217 debug_binding_key,
4218 debug_binding_modifiers
4219};
4220
4221static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004222debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004223{
4224 struct debug_binding_grab *grab;
4225
4226 grab = calloc(1, sizeof *grab);
4227 if (!grab)
4228 return;
4229
4230 grab->seat = (struct weston_seat *) seat;
4231 grab->key[0] = key;
4232 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004233 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004234}
4235
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004236static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004237force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004238 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004239{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004240 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004241 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004242 struct desktop_shell *shell = data;
4243 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004244 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004245
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004246 focus_surface = seat->keyboard->focus;
4247 if (!focus_surface)
4248 return;
4249
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004250 wl_signal_emit(&compositor->kill_signal, focus_surface);
4251
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004252 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004253 wl_client_get_credentials(client, &pid, NULL, NULL);
4254
4255 /* Skip clients that we launched ourselves (the credentials of
4256 * the socketpair is ours) */
4257 if (pid == getpid())
4258 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004259
Daniel Stone325fc2d2012-05-30 16:31:58 +01004260 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004261}
4262
4263static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004264workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004265 uint32_t key, void *data)
4266{
4267 struct desktop_shell *shell = data;
4268 unsigned int new_index = shell->workspaces.current;
4269
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004270 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004271 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004272 if (new_index != 0)
4273 new_index--;
4274
4275 change_workspace(shell, new_index);
4276}
4277
4278static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004279workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004280 uint32_t key, void *data)
4281{
4282 struct desktop_shell *shell = data;
4283 unsigned int new_index = shell->workspaces.current;
4284
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004285 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004286 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004287 if (new_index < shell->workspaces.num - 1)
4288 new_index++;
4289
4290 change_workspace(shell, new_index);
4291}
4292
4293static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004294workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004295 uint32_t key, void *data)
4296{
4297 struct desktop_shell *shell = data;
4298 unsigned int new_index;
4299
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004300 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004301 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004302 new_index = key - KEY_F1;
4303 if (new_index >= shell->workspaces.num)
4304 new_index = shell->workspaces.num - 1;
4305
4306 change_workspace(shell, new_index);
4307}
4308
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004309static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004310workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004311 uint32_t key, void *data)
4312{
4313 struct desktop_shell *shell = data;
4314 unsigned int new_index = shell->workspaces.current;
4315
4316 if (shell->locked)
4317 return;
4318
4319 if (new_index != 0)
4320 new_index--;
4321
4322 take_surface_to_workspace_by_seat(shell, seat, new_index);
4323}
4324
4325static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004326workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004327 uint32_t key, void *data)
4328{
4329 struct desktop_shell *shell = data;
4330 unsigned int new_index = shell->workspaces.current;
4331
4332 if (shell->locked)
4333 return;
4334
4335 if (new_index < shell->workspaces.num - 1)
4336 new_index++;
4337
4338 take_surface_to_workspace_by_seat(shell, seat, new_index);
4339}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004340
4341static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004342shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004343{
Tiago Vignattibe143262012-04-16 17:31:41 +03004344 struct desktop_shell *shell =
4345 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004346 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004347
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004348 if (shell->child.client)
4349 wl_client_destroy(shell->child.client);
4350
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004351 wl_list_remove(&shell->idle_listener.link);
4352 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004353 wl_list_remove(&shell->show_input_panel_listener.link);
4354 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004355
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004356 wl_array_for_each(ws, &shell->workspaces.array)
4357 workspace_destroy(*ws);
4358 wl_array_release(&shell->workspaces.array);
4359
Pekka Paalanen3c647232011-12-22 13:43:43 +02004360 free(shell->screensaver.path);
4361 free(shell);
4362}
4363
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004364static void
4365shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4366{
4367 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004368 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004369
4370 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004371 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4372 MODIFIER_CTRL | MODIFIER_ALT,
4373 terminate_binding, ec);
4374 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4375 click_to_activate_binding,
4376 shell);
4377 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4378 MODIFIER_SUPER | MODIFIER_ALT,
4379 surface_opacity_binding, NULL);
4380 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4381 MODIFIER_SUPER, zoom_axis_binding,
4382 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004383
4384 /* configurable bindings */
4385 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004386 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4387 zoom_key_binding, NULL);
4388 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4389 zoom_key_binding, NULL);
4390 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4391 shell);
4392 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4393 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004394
4395 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4396 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4397 rotate_binding, NULL);
4398
Daniel Stone325fc2d2012-05-30 16:31:58 +01004399 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4400 shell);
4401 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4402 ec);
4403 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4404 backlight_binding, ec);
4405 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4406 ec);
4407 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4408 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004409 weston_compositor_add_key_binding(ec, KEY_K, mod,
4410 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004411 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4412 workspace_up_binding, shell);
4413 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4414 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004415 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4416 workspace_move_surface_up_binding,
4417 shell);
4418 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4419 workspace_move_surface_down_binding,
4420 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004421
4422 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4423 if (shell->workspaces.num > 1) {
4424 num_workspace_bindings = shell->workspaces.num;
4425 if (num_workspace_bindings > 6)
4426 num_workspace_bindings = 6;
4427 for (i = 0; i < num_workspace_bindings; i++)
4428 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4429 workspace_f_binding,
4430 shell);
4431 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004432
4433 /* Debug bindings */
4434 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4435 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004436}
4437
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004438WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004439module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004440 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004441{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004442 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004443 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004444 struct workspace **pws;
4445 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004446 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004447
4448 shell = malloc(sizeof *shell);
4449 if (shell == NULL)
4450 return -1;
4451
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004452 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004453 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004454
4455 shell->destroy_listener.notify = shell_destroy;
4456 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004457 shell->idle_listener.notify = idle_handler;
4458 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4459 shell->wake_listener.notify = wake_handler;
4460 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004461 shell->show_input_panel_listener.notify = show_input_panels;
4462 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4463 shell->hide_input_panel_listener.notify = hide_input_panels;
4464 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004465 shell->update_input_panel_listener.notify = update_input_panels;
4466 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004467 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004468 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004469 ec->shell_interface.create_shell_surface = create_shell_surface;
4470 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004471 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004472 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004473 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004474 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004475 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004476
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004477 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004478
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004479 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4480 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004481 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4482 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004483 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004484
4485 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004486 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004487
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004488 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004489
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004490 for (i = 0; i < shell->workspaces.num; i++) {
4491 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4492 if (pws == NULL)
4493 return -1;
4494
4495 *pws = workspace_create();
4496 if (*pws == NULL)
4497 return -1;
4498 }
4499 activate_workspace(shell, 0);
4500
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004501 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004502 wl_list_init(&shell->workspaces.animation.link);
4503 shell->workspaces.animation.frame = animate_workspace_change_frame;
4504
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004505 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4506 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004507 return -1;
4508
Kristian Høgsberg75840622011-09-06 13:48:16 -04004509 if (wl_display_add_global(ec->wl_display,
4510 &desktop_shell_interface,
4511 shell, bind_desktop_shell) == NULL)
4512 return -1;
4513
Pekka Paalanen6e168112011-11-24 11:34:05 +02004514 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4515 shell, bind_screensaver) == NULL)
4516 return -1;
4517
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004518 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004519 shell, bind_input_panel) == NULL)
4520 return -1;
4521
Jonas Ådahle9d22502012-08-29 22:13:01 +02004522 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4523 shell, bind_workspace_manager) == NULL)
4524 return -1;
4525
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004526 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004527
4528 loop = wl_display_get_event_loop(ec->wl_display);
4529 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004530
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004531 shell->screensaver.timer =
4532 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4533
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004534 wl_list_for_each(seat, &ec->seat_list, link)
4535 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004536
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004537 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004538
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004539 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004540
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004541 return 0;
4542}