blob: 8f2be78b52685368167e722200e0f3e30123e96d [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050026#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040027#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020028#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010039#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020041#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Juan Zhaoe10d2792012-04-25 19:09:52 +080046enum animation_type {
47 ANIMATION_NONE,
48
49 ANIMATION_ZOOM,
50 ANIMATION_FADE
51};
52
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020053enum fade_type {
54 FADE_IN,
55 FADE_OUT
56};
57
Jonas Ådahl04769742012-06-13 00:01:24 +020058struct focus_state {
59 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040060 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020061 struct weston_surface *keyboard_focus;
62 struct wl_list link;
63 struct wl_listener seat_destroy_listener;
64 struct wl_listener surface_destroy_listener;
65};
66
Jonas Ådahle3cddce2012-06-13 00:01:22 +020067struct workspace {
68 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020069
70 struct wl_list focus_list;
71 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020072};
73
Philipp Brüschweiler88013572012-08-06 13:44:42 +020074struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -050075 struct wl_resource *resource;
76 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010077
78 struct desktop_shell *shell;
79
Philipp Brüschweiler88013572012-08-06 13:44:42 +020080 struct wl_list link;
81 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010082 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020083
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020084 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020085 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020086};
87
Tiago Vignattibe143262012-04-16 17:31:41 +030088struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050089 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040090
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020091 struct wl_listener idle_listener;
92 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040093 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020094 struct wl_listener show_input_panel_listener;
95 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020096 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020097
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050098 struct weston_layer fullscreen_layer;
99 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500100 struct weston_layer background_layer;
101 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200102 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500103
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400104 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300105 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400106
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200107 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200109 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200110 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200111
112 unsigned deathcount;
113 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200114 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200115
116 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200117 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200118 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200119
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200120 struct {
121 struct weston_surface *surface;
122 pixman_box32_t cursor_rectangle;
123 } text_input;
124
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400125 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500126 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100127
Pekka Paalanen77346a62011-11-30 16:26:35 +0200128 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200129 struct wl_array array;
130 unsigned int current;
131 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200132
Jonas Ådahle9d22502012-08-29 22:13:01 +0200133 struct wl_list client_list;
134
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200135 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200136 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200137 int anim_dir;
138 uint32_t anim_timestamp;
139 double anim_current;
140 struct workspace *anim_from;
141 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200142 } workspaces;
143
144 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200145 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200146 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200147 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200149 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200150 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500151
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200152 struct {
153 struct wl_resource *binding;
154 struct wl_list surfaces;
155 } input_panel;
156
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200157 struct {
158 struct weston_surface *surface;
159 struct weston_surface_animation *animation;
160 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300161 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200162 } fade;
163
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300164 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800165 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400166};
167
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500168enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200169 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500170 SHELL_SURFACE_TOPLEVEL,
171 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500172 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800173 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300174 SHELL_SURFACE_POPUP,
175 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200176};
177
Scott Moreauff1db4a2012-04-17 19:06:18 -0600178struct ping_timer {
179 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600180 uint32_t serial;
181};
182
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200183struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500184 struct wl_resource *resource;
185 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200186
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500187 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200188 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400189 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300190 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200191
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400192 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400193 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500194 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800195 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800196 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600197 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100198
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500199 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500201 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200202 } rotation;
203
204 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100205 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500206 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100207 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400208 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500209 } popup;
210
Alex Wu4539b082012-03-01 12:57:46 +0800211 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300212 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400213 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300214 } transient;
215
216 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800217 enum wl_shell_surface_fullscreen_method type;
218 struct weston_transform transform; /* matrix from x, y */
219 uint32_t framerate;
220 struct weston_surface *black_surface;
221 } fullscreen;
222
Scott Moreauff1db4a2012-04-17 19:06:18 -0600223 struct ping_timer *ping_timer;
224
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200225 struct weston_transform workspace_transform;
226
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500227 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100229 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400230
231 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200232};
233
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300234struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400235 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300236 struct shell_surface *shsurf;
237 struct wl_listener shsurf_destroy_listener;
238};
239
240struct weston_move_grab {
241 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100242 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500243};
244
Pekka Paalanen460099f2012-01-20 16:48:25 +0200245struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300246 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500247 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200248 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200249 float x;
250 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200251 } center;
252};
253
Giulio Camuffo5085a752013-03-25 21:42:45 +0100254struct shell_seat {
255 struct weston_seat *seat;
256 struct wl_listener seat_destroy_listener;
257
258 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400259 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100260 struct wl_list surfaces_list;
261 struct wl_client *client;
262 int32_t initial_up;
263 } popup_grab;
264};
265
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400266static void
267activate(struct desktop_shell *shell, struct weston_surface *es,
268 struct weston_seat *seat);
269
270static struct workspace *
271get_current_workspace(struct desktop_shell *shell);
272
Alex Wubd3354b2012-04-17 17:20:49 +0800273static struct shell_surface *
274get_shell_surface(struct weston_surface *surface);
275
276static struct desktop_shell *
277shell_surface_get_shell(struct shell_surface *shsurf);
278
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500279static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400280surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500281
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300282static void
283shell_fade_startup(struct desktop_shell *shell);
284
Alex Wubd3354b2012-04-17 17:20:49 +0800285static bool
286shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
287{
288 struct desktop_shell *shell;
289 struct weston_surface *top_fs_es;
290
291 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100292
Alex Wubd3354b2012-04-17 17:20:49 +0800293 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
294 return false;
295
296 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100297 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800298 layer_link);
299 return (shsurf == get_shell_surface(top_fs_es));
300}
301
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500302static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400303destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300304{
305 struct shell_grab *grab;
306
307 grab = container_of(listener, struct shell_grab,
308 shsurf_destroy_listener);
309
310 grab->shsurf = NULL;
311}
312
313static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400314popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400315
316static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300317shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400318 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 struct desktop_shell *shell = shsurf->shell;
324
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400325 popup_grab_end(pointer);
326
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327 grab->grab.interface = interface;
328 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400329 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500330 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400333 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400334 if (shell->child.desktop_shell) {
335 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
336 cursor);
337 weston_pointer_set_focus(pointer, shell->grab_surface,
338 wl_fixed_from_int(0),
339 wl_fixed_from_int(0));
340 }
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øgsberg9e5d7d12013-07-22 16:31:53 -0700349 weston_pointer_end_grab(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øgsberg67800732013-07-04 01:12:17 -04001293end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1294
1295static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001296busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001297{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001298 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001299 struct weston_pointer *pointer = base->pointer;
1300 struct weston_surface *surface;
1301 wl_fixed_t sx, sy;
1302
1303 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1304 pointer->x, pointer->y,
1305 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001306
Kristian Høgsberg67800732013-07-04 01:12:17 -04001307 if (!grab->shsurf || grab->shsurf->surface != surface)
1308 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001309}
1310
1311static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001312busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001313{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001314}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001315
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001316static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001317busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001318 uint32_t time, uint32_t button, uint32_t state)
1319{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001320 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001321 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001322 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001323
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001324 if (shsurf && button == BTN_LEFT && state) {
1325 activate(shsurf->shell, shsurf->surface, seat);
1326 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001327 } else if (shsurf && button == BTN_RIGHT && state) {
1328 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001329 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001330 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001331}
1332
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001333static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001334 busy_cursor_grab_focus,
1335 busy_cursor_grab_motion,
1336 busy_cursor_grab_button,
1337};
1338
1339static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001340set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001341{
1342 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001343
1344 grab = malloc(sizeof *grab);
1345 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001346 return;
1347
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001348 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1349 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001350}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001351
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001352static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001353end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001354{
1355 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1356
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001357 if (grab->grab.interface == &busy_cursor_grab_interface &&
1358 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001359 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001360 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001361 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001362}
1363
Scott Moreau9521d5e2012-04-19 13:06:17 -06001364static void
1365ping_timer_destroy(struct shell_surface *shsurf)
1366{
1367 if (!shsurf || !shsurf->ping_timer)
1368 return;
1369
1370 if (shsurf->ping_timer->source)
1371 wl_event_source_remove(shsurf->ping_timer->source);
1372
1373 free(shsurf->ping_timer);
1374 shsurf->ping_timer = NULL;
1375}
1376
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001377static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001378ping_timeout_handler(void *data)
1379{
1380 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001381 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001382
Scott Moreau9521d5e2012-04-19 13:06:17 -06001383 /* Client is not responding */
1384 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001385
1386 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001387 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001388 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001389
1390 return 1;
1391}
1392
1393static void
1394ping_handler(struct weston_surface *surface, uint32_t serial)
1395{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001396 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001397 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001398 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001399
1400 if (!shsurf)
1401 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001402 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001403 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001404
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001405 if (shsurf->surface == shsurf->shell->grab_surface)
1406 return;
1407
Scott Moreauff1db4a2012-04-17 19:06:18 -06001408 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001409 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001410 if (!shsurf->ping_timer)
1411 return;
1412
1413 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001414 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1415 shsurf->ping_timer->source =
1416 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1417 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1418
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001419 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001420 }
1421}
1422
1423static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001424handle_pointer_focus(struct wl_listener *listener, void *data)
1425{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001426 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001427 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001428 struct weston_compositor *compositor;
1429 struct shell_surface *shsurf;
1430 uint32_t serial;
1431
1432 if (!surface)
1433 return;
1434
1435 compositor = surface->compositor;
1436 shsurf = get_shell_surface(surface);
1437
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001438 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001439 set_busy_cursor(shsurf, pointer);
1440 } else {
1441 serial = wl_display_next_serial(compositor->wl_display);
1442 ping_handler(surface, serial);
1443 }
1444}
1445
1446static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001447create_pointer_focus_listener(struct weston_seat *seat)
1448{
1449 struct wl_listener *listener;
1450
Kristian Høgsberge3148752013-05-06 23:19:49 -04001451 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001452 return;
1453
1454 listener = malloc(sizeof *listener);
1455 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001456 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001457}
1458
1459static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001460shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1461 uint32_t serial)
1462{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001463 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001464 struct weston_seat *seat;
1465 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001466
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001467 if (shsurf->ping_timer == NULL)
1468 /* Just ignore unsolicited pong. */
1469 return;
1470
Scott Moreauff1db4a2012-04-17 19:06:18 -06001471 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001472 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001473 wl_list_for_each(seat, &ec->seat_list, link) {
1474 if(seat->pointer)
1475 end_busy_cursor(shsurf, seat->pointer);
1476 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001477 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001478 }
1479}
1480
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001481static void
1482shell_surface_set_title(struct wl_client *client,
1483 struct wl_resource *resource, const char *title)
1484{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001485 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001486
1487 free(shsurf->title);
1488 shsurf->title = strdup(title);
1489}
1490
1491static void
1492shell_surface_set_class(struct wl_client *client,
1493 struct wl_resource *resource, const char *class)
1494{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001495 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001496
1497 free(shsurf->class);
1498 shsurf->class = strdup(class);
1499}
1500
Juan Zhao96879df2012-02-07 08:45:41 +08001501static struct weston_output *
1502get_default_output(struct weston_compositor *compositor)
1503{
1504 return container_of(compositor->output_list.next,
1505 struct weston_output, link);
1506}
1507
Alex Wu4539b082012-03-01 12:57:46 +08001508static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001509restore_output_mode(struct weston_output *output)
1510{
1511 if (output->current != output->origin ||
1512 (int32_t)output->scale != output->origin_scale)
1513 weston_output_switch_mode(output,
1514 output->origin,
1515 output->origin_scale);
1516}
1517
1518static void
1519restore_all_output_modes(struct weston_compositor *compositor)
1520{
1521 struct weston_output *output;
1522
1523 wl_list_for_each(output, &compositor->output_list, link)
1524 restore_output_mode(output);
1525}
1526
1527static void
Alex Wu4539b082012-03-01 12:57:46 +08001528shell_unset_fullscreen(struct shell_surface *shsurf)
1529{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001530 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001531 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001532 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1533 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001534 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001535 }
Alex Wu4539b082012-03-01 12:57:46 +08001536 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1537 shsurf->fullscreen.framerate = 0;
1538 wl_list_remove(&shsurf->fullscreen.transform.link);
1539 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001540 if (shsurf->fullscreen.black_surface)
1541 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001542 shsurf->fullscreen.black_surface = NULL;
1543 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001544 weston_surface_set_position(shsurf->surface,
1545 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001546 if (shsurf->saved_rotation_valid) {
1547 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1548 &shsurf->rotation.transform.link);
1549 shsurf->saved_rotation_valid = false;
1550 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001551
1552 ws = get_current_workspace(shsurf->shell);
1553 wl_list_remove(&shsurf->surface->layer_link);
1554 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001555}
1556
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001557static void
1558shell_unset_maximized(struct shell_surface *shsurf)
1559{
1560 struct workspace *ws;
1561 /* undo all maximized things here */
1562 shsurf->output = get_default_output(shsurf->surface->compositor);
1563 weston_surface_set_position(shsurf->surface,
1564 shsurf->saved_x,
1565 shsurf->saved_y);
1566
1567 if (shsurf->saved_rotation_valid) {
1568 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1569 &shsurf->rotation.transform.link);
1570 shsurf->saved_rotation_valid = false;
1571 }
1572
1573 ws = get_current_workspace(shsurf->shell);
1574 wl_list_remove(&shsurf->surface->layer_link);
1575 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1576}
1577
Pekka Paalanen98262232011-12-01 10:42:22 +02001578static int
1579reset_shell_surface_type(struct shell_surface *surface)
1580{
1581 switch (surface->type) {
1582 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001583 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001584 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001585 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001586 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001587 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001588 case SHELL_SURFACE_NONE:
1589 case SHELL_SURFACE_TOPLEVEL:
1590 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001591 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001592 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001593 break;
1594 }
1595
1596 surface->type = SHELL_SURFACE_NONE;
1597 return 0;
1598}
1599
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001600static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001601set_surface_type(struct shell_surface *shsurf)
1602{
1603 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001604 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001605
1606 reset_shell_surface_type(shsurf);
1607
1608 shsurf->type = shsurf->next_type;
1609 shsurf->next_type = SHELL_SURFACE_NONE;
1610
1611 switch (shsurf->type) {
1612 case SHELL_SURFACE_TOPLEVEL:
1613 break;
1614 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001615 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001616 pes->geometry.x + shsurf->transient.x,
1617 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001618 break;
1619
1620 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001621 case SHELL_SURFACE_FULLSCREEN:
1622 shsurf->saved_x = surface->geometry.x;
1623 shsurf->saved_y = surface->geometry.y;
1624 shsurf->saved_position_valid = true;
1625
1626 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1627 wl_list_remove(&shsurf->rotation.transform.link);
1628 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001629 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001630 shsurf->saved_rotation_valid = true;
1631 }
1632 break;
1633
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001634 case SHELL_SURFACE_XWAYLAND:
1635 weston_surface_set_position(surface, shsurf->transient.x,
1636 shsurf->transient.y);
1637 break;
1638
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001639 default:
1640 break;
1641 }
1642}
1643
1644static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001645set_toplevel(struct shell_surface *shsurf)
1646{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001647 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001648}
1649
1650static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001651shell_surface_set_toplevel(struct wl_client *client,
1652 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001653{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001654 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001655
Tiago Vignattibc052c92012-04-19 16:18:18 +03001656 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001657}
1658
1659static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001660set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001661 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001662{
1663 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001664 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001665 shsurf->transient.x = x;
1666 shsurf->transient.y = y;
1667 shsurf->transient.flags = flags;
1668 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1669}
1670
1671static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001672shell_surface_set_transient(struct wl_client *client,
1673 struct wl_resource *resource,
1674 struct wl_resource *parent_resource,
1675 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001676{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001677 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001678 struct weston_surface *parent =
1679 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001680
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001681 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001682}
1683
Tiago Vignattibe143262012-04-16 17:31:41 +03001684static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001685shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001686{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001687 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001688}
1689
1690static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001691get_output_panel_height(struct desktop_shell *shell,
1692 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001693{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001694 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001695 int panel_height = 0;
1696
1697 if (!output)
1698 return 0;
1699
Juan Zhao4ab94682012-07-09 22:24:09 -07001700 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001701 if (surface->output == output) {
1702 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001703 break;
1704 }
1705 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001706
Juan Zhao96879df2012-02-07 08:45:41 +08001707 return panel_height;
1708}
1709
1710static void
1711shell_surface_set_maximized(struct wl_client *client,
1712 struct wl_resource *resource,
1713 struct wl_resource *output_resource )
1714{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001715 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001716 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001717 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001718 uint32_t edges = 0, panel_height = 0;
1719
1720 /* get the default output, if the client set it as NULL
1721 check whether the ouput is available */
1722 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001723 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001724 else if (es->output)
1725 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001726 else
1727 shsurf->output = get_default_output(es->compositor);
1728
Tiago Vignattibe143262012-04-16 17:31:41 +03001729 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001730 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001731 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001732
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001733 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001734 shsurf->output->width,
1735 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001736
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001737 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001738}
1739
Alex Wu21858432012-04-01 20:13:08 +08001740static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001741black_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 +08001742
Alex Wu4539b082012-03-01 12:57:46 +08001743static struct weston_surface *
1744create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001745 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001746 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001747{
1748 struct weston_surface *surface = NULL;
1749
1750 surface = weston_surface_create(ec);
1751 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001752 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001753 return NULL;
1754 }
1755
Alex Wu21858432012-04-01 20:13:08 +08001756 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001757 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001758 weston_surface_configure(surface, x, y, w, h);
1759 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001760 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001761 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001762 pixman_region32_fini(&surface->input);
1763 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001764
Alex Wu4539b082012-03-01 12:57:46 +08001765 return surface;
1766}
1767
1768/* Create black surface and append it to the associated fullscreen surface.
1769 * Handle size dismatch and positioning according to the method. */
1770static void
1771shell_configure_fullscreen(struct shell_surface *shsurf)
1772{
1773 struct weston_output *output = shsurf->fullscreen_output;
1774 struct weston_surface *surface = shsurf->surface;
1775 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001776 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001777 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001778
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001779 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1780 restore_output_mode(output);
1781
Alex Wu4539b082012-03-01 12:57:46 +08001782 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001783 shsurf->fullscreen.black_surface =
1784 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001785 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001786 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001787 output->width,
1788 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001789
1790 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1791 wl_list_insert(&surface->layer_link,
1792 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001793 shsurf->fullscreen.black_surface->output = output;
1794
Giulio Camuffob8366642013-04-25 13:57:46 +03001795 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1796 &surf_width, &surf_height);
1797
Alex Wu4539b082012-03-01 12:57:46 +08001798 switch (shsurf->fullscreen.type) {
1799 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001800 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001801 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001802 break;
1803 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001804 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001805 if (output->width == surf_width &&
1806 output->height == surf_height) {
1807 weston_surface_set_position(surface, output->x - surf_x,
1808 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001809 break;
1810 }
1811
Alex Wu4539b082012-03-01 12:57:46 +08001812 matrix = &shsurf->fullscreen.transform.matrix;
1813 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001814
Scott Moreau1bad5db2012-08-18 01:04:05 -06001815 output_aspect = (float) output->width /
1816 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001817 surface_aspect = (float) surface->geometry.width /
1818 (float) surface->geometry.height;
1819 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001820 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001821 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001822 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001823 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001824 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001825
Alex Wu4539b082012-03-01 12:57:46 +08001826 weston_matrix_scale(matrix, scale, scale, 1);
1827 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001828 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001829 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001830 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1831 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001832 weston_surface_set_position(surface, x, y);
1833
Alex Wu4539b082012-03-01 12:57:46 +08001834 break;
1835 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001836 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001837 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001838 surf_width * surface->buffer_scale,
1839 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001840 shsurf->fullscreen.framerate};
1841
Alexander Larsson355748e2013-05-28 16:23:38 +02001842 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001843 weston_surface_set_position(surface,
1844 output->x - surf_x,
1845 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001846 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001847 output->x - surf_x,
1848 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001849 output->width,
1850 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001851 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001852 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001853 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001854 center_on_output(surface, output);
1855 }
Alex Wubd3354b2012-04-17 17:20:49 +08001856 }
Alex Wu4539b082012-03-01 12:57:46 +08001857 break;
1858 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001859 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001860 break;
1861 default:
1862 break;
1863 }
1864}
1865
1866/* make the fullscreen and black surface at the top */
1867static void
1868shell_stack_fullscreen(struct shell_surface *shsurf)
1869{
Alex Wubd3354b2012-04-17 17:20:49 +08001870 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001871 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001872 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001873
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001874 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001875 wl_list_insert(&shell->fullscreen_layer.surface_list,
1876 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001877 weston_surface_damage(surface);
1878
1879 if (!shsurf->fullscreen.black_surface)
1880 shsurf->fullscreen.black_surface =
1881 create_black_surface(surface->compositor,
1882 surface,
1883 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001884 output->width,
1885 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001886
1887 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001888 wl_list_insert(&surface->layer_link,
1889 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001890 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001891}
1892
1893static void
1894shell_map_fullscreen(struct shell_surface *shsurf)
1895{
Alex Wu4539b082012-03-01 12:57:46 +08001896 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001897 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001898}
1899
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001900static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001901set_fullscreen(struct shell_surface *shsurf,
1902 uint32_t method,
1903 uint32_t framerate,
1904 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001905{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001906 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001907
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001908 if (output)
1909 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001910 else if (es->output)
1911 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001912 else
1913 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001914
Alex Wu4539b082012-03-01 12:57:46 +08001915 shsurf->fullscreen_output = shsurf->output;
1916 shsurf->fullscreen.type = method;
1917 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001918 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001919
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001920 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001921 shsurf->output->width,
1922 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001923}
1924
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001925static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001926shell_surface_set_fullscreen(struct wl_client *client,
1927 struct wl_resource *resource,
1928 uint32_t method,
1929 uint32_t framerate,
1930 struct wl_resource *output_resource)
1931{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001932 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001933 struct weston_output *output;
1934
1935 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001936 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001937 else
1938 output = NULL;
1939
1940 set_fullscreen(shsurf, method, framerate, output);
1941}
1942
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001943static void
1944set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1945{
1946 /* XXX: using the same fields for transient type */
1947 shsurf->transient.x = x;
1948 shsurf->transient.y = y;
1949 shsurf->transient.flags = flags;
1950 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1951}
1952
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001953static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001954
1955static void
1956destroy_shell_seat(struct wl_listener *listener, void *data)
1957{
1958 struct shell_seat *shseat =
1959 container_of(listener,
1960 struct shell_seat, seat_destroy_listener);
1961 struct shell_surface *shsurf, *prev = NULL;
1962
1963 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001964 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001965 shseat->popup_grab.client = NULL;
1966
1967 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1968 shsurf->popup.shseat = NULL;
1969 if (prev) {
1970 wl_list_init(&prev->popup.grab_link);
1971 }
1972 prev = shsurf;
1973 }
1974 wl_list_init(&prev->popup.grab_link);
1975 }
1976
1977 wl_list_remove(&shseat->seat_destroy_listener.link);
1978 free(shseat);
1979}
1980
1981static struct shell_seat *
1982create_shell_seat(struct weston_seat *seat)
1983{
1984 struct shell_seat *shseat;
1985
1986 shseat = calloc(1, sizeof *shseat);
1987 if (!shseat) {
1988 weston_log("no memory to allocate shell seat\n");
1989 return NULL;
1990 }
1991
1992 shseat->seat = seat;
1993 wl_list_init(&shseat->popup_grab.surfaces_list);
1994
1995 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1996 wl_signal_add(&seat->destroy_signal,
1997 &shseat->seat_destroy_listener);
1998
1999 return shseat;
2000}
2001
2002static struct shell_seat *
2003get_shell_seat(struct weston_seat *seat)
2004{
2005 struct wl_listener *listener;
2006
2007 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2008 if (listener == NULL)
2009 return create_shell_seat(seat);
2010
2011 return container_of(listener,
2012 struct shell_seat, seat_destroy_listener);
2013}
2014
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002015static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002016popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002017{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002018 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002019 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002020 struct shell_seat *shseat =
2021 container_of(grab, struct shell_seat, popup_grab.grab);
2022 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002023 wl_fixed_t sx, sy;
2024
2025 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2026 pointer->x, pointer->y,
2027 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002028
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002029 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002030 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002031 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002032 weston_pointer_set_focus(pointer, NULL,
2033 wl_fixed_from_int(0),
2034 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002035 }
2036}
2037
2038static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002039popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002040{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002041 struct weston_pointer *pointer = grab->pointer;
2042 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002043
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002044 if (pointer->focus_resource) {
2045 weston_surface_from_global_fixed(pointer->focus,
2046 pointer->x, pointer->y,
2047 &sx, &sy);
2048 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2049 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002050}
2051
2052static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002053popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002054 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002055{
2056 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002057 struct shell_seat *shseat =
2058 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002059 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002060 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002061 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002062
Daniel Stone37816df2012-05-16 18:45:18 +01002063 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002064 if (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 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003731 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003732
Giulio Camuffo184df502013-02-21 11:29:21 +01003733 if (width == 0)
3734 return;
3735
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003736 if (!weston_surface_is_mapped(surface)) {
3737 if (!shell->showing_input_panels)
3738 return;
3739
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003740 show_surface = 1;
3741 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003742
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003743 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003744
3745 if (ip_surface->panel) {
3746 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3747 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3748 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01003749 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
3750 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003751 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003752
3753 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003754 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003755 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003756
3757 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003758 wl_list_insert(&shell->input_panel_layer.surface_list,
3759 &surface->layer_link);
3760 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003761 weston_surface_damage(surface);
3762 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3763 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003764}
3765
3766static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003767destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003768{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003769 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3770
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003771 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003772 wl_list_remove(&input_panel_surface->link);
3773
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003774 input_panel_surface->surface->configure = NULL;
3775
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003776 free(input_panel_surface);
3777}
3778
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003779static struct input_panel_surface *
3780get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003781{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003782 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003783 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003784 } else {
3785 return NULL;
3786 }
3787}
3788
3789static void
3790input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3791{
3792 struct input_panel_surface *ipsurface = container_of(listener,
3793 struct input_panel_surface,
3794 surface_destroy_listener);
3795
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003796 if (ipsurface->resource) {
3797 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003798 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003799 destroy_input_panel_surface(ipsurface);
3800 }
3801}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003802
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003803static struct input_panel_surface *
3804create_input_panel_surface(struct desktop_shell *shell,
3805 struct weston_surface *surface)
3806{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003807 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003808
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003809 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3810 if (!input_panel_surface)
3811 return NULL;
3812
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003813 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003814 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003815
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003816 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003817
3818 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003819
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003820 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003821 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003822 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003823 &input_panel_surface->surface_destroy_listener);
3824
3825 wl_list_init(&input_panel_surface->link);
3826
3827 return input_panel_surface;
3828}
3829
3830static void
3831input_panel_surface_set_toplevel(struct wl_client *client,
3832 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003833 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003834 uint32_t position)
3835{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003836 struct input_panel_surface *input_panel_surface =
3837 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003838 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003839
3840 wl_list_insert(&shell->input_panel.surfaces,
3841 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003842
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003843 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003844 input_panel_surface->panel = 0;
3845}
3846
3847static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003848input_panel_surface_set_overlay_panel(struct wl_client *client,
3849 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003850{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003851 struct input_panel_surface *input_panel_surface =
3852 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003853 struct desktop_shell *shell = input_panel_surface->shell;
3854
3855 wl_list_insert(&shell->input_panel.surfaces,
3856 &input_panel_surface->link);
3857
3858 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003859}
3860
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003861static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003862 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003863 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003864};
3865
3866static void
3867destroy_input_panel_surface_resource(struct wl_resource *resource)
3868{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003869 struct input_panel_surface *ipsurf =
3870 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003871
3872 destroy_input_panel_surface(ipsurf);
3873}
3874
3875static void
3876input_panel_get_input_panel_surface(struct wl_client *client,
3877 struct wl_resource *resource,
3878 uint32_t id,
3879 struct wl_resource *surface_resource)
3880{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003881 struct weston_surface *surface =
3882 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003883 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003884 struct input_panel_surface *ipsurf;
3885
3886 if (get_input_panel_surface(surface)) {
3887 wl_resource_post_error(surface_resource,
3888 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003889 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003890 return;
3891 }
3892
3893 ipsurf = create_input_panel_surface(shell, surface);
3894 if (!ipsurf) {
3895 wl_resource_post_error(surface_resource,
3896 WL_DISPLAY_ERROR_INVALID_OBJECT,
3897 "surface->configure already set");
3898 return;
3899 }
3900
Jason Ekstranda85118c2013-06-27 20:17:02 -05003901 ipsurf->resource =
3902 wl_resource_create(client,
3903 &wl_input_panel_surface_interface, 1, id);
3904 wl_resource_set_implementation(ipsurf->resource,
3905 &input_panel_surface_implementation,
3906 ipsurf,
3907 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003908}
3909
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003910static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003911 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003912};
3913
3914static void
3915unbind_input_panel(struct wl_resource *resource)
3916{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003917 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003918
3919 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003920}
3921
3922static void
3923bind_input_panel(struct wl_client *client,
3924 void *data, uint32_t version, uint32_t id)
3925{
3926 struct desktop_shell *shell = data;
3927 struct wl_resource *resource;
3928
Jason Ekstranda85118c2013-06-27 20:17:02 -05003929 resource = wl_resource_create(client,
3930 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003931
3932 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003933 wl_resource_set_implementation(resource,
3934 &input_panel_implementation,
3935 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003936 shell->input_panel.binding = resource;
3937 return;
3938 }
3939
3940 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3941 "interface object already bound");
3942 wl_resource_destroy(resource);
3943}
3944
Kristian Høgsberg07045392012-02-19 18:52:44 -05003945struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003946 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003947 struct weston_surface *current;
3948 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003949 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003950};
3951
3952static void
3953switcher_next(struct switcher *switcher)
3954{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003955 struct weston_surface *surface;
3956 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003957 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003958 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003959
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003960 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003961 switch (get_shell_surface_type(surface)) {
3962 case SHELL_SURFACE_TOPLEVEL:
3963 case SHELL_SURFACE_FULLSCREEN:
3964 case SHELL_SURFACE_MAXIMIZED:
3965 if (first == NULL)
3966 first = surface;
3967 if (prev == switcher->current)
3968 next = surface;
3969 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003970 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003971 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003972 weston_surface_damage(surface);
3973 break;
3974 default:
3975 break;
3976 }
Alex Wu1659daa2012-04-01 20:13:09 +08003977
3978 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003979 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003980 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003981 weston_surface_damage(surface);
3982 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003983 }
3984
3985 if (next == NULL)
3986 next = first;
3987
Alex Wu07b26062012-03-12 16:06:01 +08003988 if (next == NULL)
3989 return;
3990
Kristian Høgsberg07045392012-02-19 18:52:44 -05003991 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003992 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003993
3994 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003995 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003996
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003997 shsurf = get_shell_surface(switcher->current);
3998 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003999 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004000}
4001
4002static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004003switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004004{
4005 struct switcher *switcher =
4006 container_of(listener, struct switcher, listener);
4007
4008 switcher_next(switcher);
4009}
4010
4011static void
Daniel Stone351eb612012-05-31 15:27:47 -04004012switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004013{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004014 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004015 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004016 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004017
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004018 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004019 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004020 weston_surface_damage(surface);
4021 }
4022
Alex Wu07b26062012-03-12 16:06:01 +08004023 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004024 activate(switcher->shell, switcher->current,
4025 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004026 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004027 weston_keyboard_end_grab(keyboard);
4028 if (keyboard->input_method_resource)
4029 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004030 free(switcher);
4031}
4032
4033static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004034switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004035 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004036{
4037 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004038 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004039
Daniel Stonec9785ea2012-05-30 16:31:52 +01004040 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004041 switcher_next(switcher);
4042}
4043
4044static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004045switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004046 uint32_t mods_depressed, uint32_t mods_latched,
4047 uint32_t mods_locked, uint32_t group)
4048{
4049 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004050 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004051
Daniel Stone351eb612012-05-31 15:27:47 -04004052 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4053 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004054}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004055
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004056static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004057 switcher_key,
4058 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004059};
4060
4061static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004062switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004063 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004064{
Tiago Vignattibe143262012-04-16 17:31:41 +03004065 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004066 struct switcher *switcher;
4067
4068 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004069 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004070 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004071 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004072 wl_list_init(&switcher->listener.link);
4073
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004074 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004075 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004076 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004077 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4078 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004079 switcher_next(switcher);
4080}
4081
Pekka Paalanen3c647232011-12-22 13:43:43 +02004082static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004083backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004084 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004085{
4086 struct weston_compositor *compositor = data;
4087 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004088 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004089
4090 /* TODO: we're limiting to simple use cases, where we assume just
4091 * control on the primary display. We'd have to extend later if we
4092 * ever get support for setting backlights on random desktop LCD
4093 * panels though */
4094 output = get_default_output(compositor);
4095 if (!output)
4096 return;
4097
4098 if (!output->set_backlight)
4099 return;
4100
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004101 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4102 backlight_new = output->backlight_current - 25;
4103 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4104 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004105
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004106 if (backlight_new < 5)
4107 backlight_new = 5;
4108 if (backlight_new > 255)
4109 backlight_new = 255;
4110
4111 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004112 output->set_backlight(output, output->backlight_current);
4113}
4114
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004115struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004116 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004117 struct weston_seat *seat;
4118 uint32_t key[2];
4119 int key_released[2];
4120};
4121
4122static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004123debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004124 uint32_t key, uint32_t state)
4125{
4126 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004127 struct weston_compositor *ec = db->seat->compositor;
4128 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004129 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004130 uint32_t serial;
4131 int send = 0, terminate = 0;
4132 int check_binding = 1;
4133 int i;
4134
4135 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4136 /* Do not run bindings on key releases */
4137 check_binding = 0;
4138
4139 for (i = 0; i < 2; i++)
4140 if (key == db->key[i])
4141 db->key_released[i] = 1;
4142
4143 if (db->key_released[0] && db->key_released[1]) {
4144 /* All key releases been swalled so end the grab */
4145 terminate = 1;
4146 } else if (key != db->key[0] && key != db->key[1]) {
4147 /* Should not swallow release of other keys */
4148 send = 1;
4149 }
4150 } else if (key == db->key[0] && !db->key_released[0]) {
4151 /* Do not check bindings for the first press of the binding
4152 * key. This allows it to be used as a debug shortcut.
4153 * We still need to swallow this event. */
4154 check_binding = 0;
4155 } else if (db->key[1]) {
4156 /* If we already ran a binding don't process another one since
4157 * we can't keep track of all the binding keys that were
4158 * pressed in order to swallow the release events. */
4159 send = 1;
4160 check_binding = 0;
4161 }
4162
4163 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004164 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4165 key, state)) {
4166 /* We ran a binding so swallow the press and keep the
4167 * grab to swallow the released too. */
4168 send = 0;
4169 terminate = 0;
4170 db->key[1] = key;
4171 } else {
4172 /* Terminate the grab since the key pressed is not a
4173 * debug binding key. */
4174 send = 1;
4175 terminate = 1;
4176 }
4177 }
4178
4179 if (send) {
4180 resource = grab->keyboard->focus_resource;
4181
4182 if (resource) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004183 serial = wl_display_next_serial(display);
4184 wl_keyboard_send_key(resource, serial, time, key, state);
4185 }
4186 }
4187
4188 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004189 weston_keyboard_end_grab(grab->keyboard);
4190 if (grab->keyboard->input_method_resource)
4191 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004192 free(db);
4193 }
4194}
4195
4196static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004197debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004198 uint32_t mods_depressed, uint32_t mods_latched,
4199 uint32_t mods_locked, uint32_t group)
4200{
4201 struct wl_resource *resource;
4202
4203 resource = grab->keyboard->focus_resource;
4204 if (!resource)
4205 return;
4206
4207 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4208 mods_latched, mods_locked, group);
4209}
4210
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004211struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004212 debug_binding_key,
4213 debug_binding_modifiers
4214};
4215
4216static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004217debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004218{
4219 struct debug_binding_grab *grab;
4220
4221 grab = calloc(1, sizeof *grab);
4222 if (!grab)
4223 return;
4224
4225 grab->seat = (struct weston_seat *) seat;
4226 grab->key[0] = key;
4227 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004228 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004229}
4230
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004231static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004232force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004233 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004234{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004235 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004236 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004237 struct desktop_shell *shell = data;
4238 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004239 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004240
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004241 focus_surface = seat->keyboard->focus;
4242 if (!focus_surface)
4243 return;
4244
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004245 wl_signal_emit(&compositor->kill_signal, focus_surface);
4246
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004247 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004248 wl_client_get_credentials(client, &pid, NULL, NULL);
4249
4250 /* Skip clients that we launched ourselves (the credentials of
4251 * the socketpair is ours) */
4252 if (pid == getpid())
4253 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004254
Daniel Stone325fc2d2012-05-30 16:31:58 +01004255 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004256}
4257
4258static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004259workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004260 uint32_t key, void *data)
4261{
4262 struct desktop_shell *shell = data;
4263 unsigned int new_index = shell->workspaces.current;
4264
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004265 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004266 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004267 if (new_index != 0)
4268 new_index--;
4269
4270 change_workspace(shell, new_index);
4271}
4272
4273static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004274workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004275 uint32_t key, void *data)
4276{
4277 struct desktop_shell *shell = data;
4278 unsigned int new_index = shell->workspaces.current;
4279
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004280 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004281 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004282 if (new_index < shell->workspaces.num - 1)
4283 new_index++;
4284
4285 change_workspace(shell, new_index);
4286}
4287
4288static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004289workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004290 uint32_t key, void *data)
4291{
4292 struct desktop_shell *shell = data;
4293 unsigned int new_index;
4294
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004295 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004296 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004297 new_index = key - KEY_F1;
4298 if (new_index >= shell->workspaces.num)
4299 new_index = shell->workspaces.num - 1;
4300
4301 change_workspace(shell, new_index);
4302}
4303
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004304static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004305workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004306 uint32_t key, void *data)
4307{
4308 struct desktop_shell *shell = data;
4309 unsigned int new_index = shell->workspaces.current;
4310
4311 if (shell->locked)
4312 return;
4313
4314 if (new_index != 0)
4315 new_index--;
4316
4317 take_surface_to_workspace_by_seat(shell, seat, new_index);
4318}
4319
4320static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004321workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004322 uint32_t key, void *data)
4323{
4324 struct desktop_shell *shell = data;
4325 unsigned int new_index = shell->workspaces.current;
4326
4327 if (shell->locked)
4328 return;
4329
4330 if (new_index < shell->workspaces.num - 1)
4331 new_index++;
4332
4333 take_surface_to_workspace_by_seat(shell, seat, new_index);
4334}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004335
4336static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004337shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004338{
Tiago Vignattibe143262012-04-16 17:31:41 +03004339 struct desktop_shell *shell =
4340 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004341 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004342
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004343 if (shell->child.client)
4344 wl_client_destroy(shell->child.client);
4345
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004346 wl_list_remove(&shell->idle_listener.link);
4347 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004348 wl_list_remove(&shell->show_input_panel_listener.link);
4349 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004350
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004351 wl_array_for_each(ws, &shell->workspaces.array)
4352 workspace_destroy(*ws);
4353 wl_array_release(&shell->workspaces.array);
4354
Pekka Paalanen3c647232011-12-22 13:43:43 +02004355 free(shell->screensaver.path);
4356 free(shell);
4357}
4358
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004359static void
4360shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4361{
4362 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004363 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004364
4365 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004366 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4367 MODIFIER_CTRL | MODIFIER_ALT,
4368 terminate_binding, ec);
4369 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4370 click_to_activate_binding,
4371 shell);
4372 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4373 MODIFIER_SUPER | MODIFIER_ALT,
4374 surface_opacity_binding, NULL);
4375 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4376 MODIFIER_SUPER, zoom_axis_binding,
4377 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004378
4379 /* configurable bindings */
4380 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004381 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4382 zoom_key_binding, NULL);
4383 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4384 zoom_key_binding, NULL);
4385 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4386 shell);
4387 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4388 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004389
4390 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4391 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4392 rotate_binding, NULL);
4393
Daniel Stone325fc2d2012-05-30 16:31:58 +01004394 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4395 shell);
4396 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4397 ec);
4398 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4399 backlight_binding, ec);
4400 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4401 ec);
4402 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4403 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004404 weston_compositor_add_key_binding(ec, KEY_K, mod,
4405 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004406 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4407 workspace_up_binding, shell);
4408 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4409 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004410 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4411 workspace_move_surface_up_binding,
4412 shell);
4413 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4414 workspace_move_surface_down_binding,
4415 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004416
4417 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4418 if (shell->workspaces.num > 1) {
4419 num_workspace_bindings = shell->workspaces.num;
4420 if (num_workspace_bindings > 6)
4421 num_workspace_bindings = 6;
4422 for (i = 0; i < num_workspace_bindings; i++)
4423 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4424 workspace_f_binding,
4425 shell);
4426 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004427
4428 /* Debug bindings */
4429 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4430 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004431}
4432
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004433WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004434module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004435 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004436{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004437 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004438 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004439 struct workspace **pws;
4440 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004441 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004442
Peter Huttererf3d62272013-08-08 11:57:05 +10004443 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004444 if (shell == NULL)
4445 return -1;
4446
Kristian Høgsberg75840622011-09-06 13:48:16 -04004447 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004448
4449 shell->destroy_listener.notify = shell_destroy;
4450 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004451 shell->idle_listener.notify = idle_handler;
4452 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4453 shell->wake_listener.notify = wake_handler;
4454 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004455 shell->show_input_panel_listener.notify = show_input_panels;
4456 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4457 shell->hide_input_panel_listener.notify = hide_input_panels;
4458 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004459 shell->update_input_panel_listener.notify = update_input_panels;
4460 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004461 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004462 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004463 ec->shell_interface.create_shell_surface = create_shell_surface;
4464 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004465 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004466 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004467 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004468 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004469 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004470
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004471 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004472
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004473 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4474 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004475 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4476 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004477 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004478
4479 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004480 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004481
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004482 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004483
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004484 for (i = 0; i < shell->workspaces.num; i++) {
4485 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4486 if (pws == NULL)
4487 return -1;
4488
4489 *pws = workspace_create();
4490 if (*pws == NULL)
4491 return -1;
4492 }
4493 activate_workspace(shell, 0);
4494
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004495 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004496 wl_list_init(&shell->workspaces.animation.link);
4497 shell->workspaces.animation.frame = animate_workspace_change_frame;
4498
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004499 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004500 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004501 return -1;
4502
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004503 if (wl_global_create(ec->wl_display,
4504 &desktop_shell_interface, 2,
4505 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004506 return -1;
4507
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004508 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4509 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004510 return -1;
4511
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004512 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004513 shell, bind_input_panel) == NULL)
4514 return -1;
4515
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004516 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4517 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004518 return -1;
4519
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004520 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004521
4522 loop = wl_display_get_event_loop(ec->wl_display);
4523 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004524
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004525 shell->screensaver.timer =
4526 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4527
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004528 wl_list_for_each(seat, &ec->seat_list, link)
4529 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004530
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004531 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004532
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004533 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004534
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004535 return 0;
4536}