blob: ebe9b9f84cfa3c089a78d022c18d2826af41a6a9 [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);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002059 struct wl_display *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) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002065 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002066 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002067 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002068 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002069 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002070 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002071 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002072 }
2073
Daniel Stone4dbadb12012-05-30 16:31:51 +01002074 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002075 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002076}
2077
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002078static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002079 popup_grab_focus,
2080 popup_grab_motion,
2081 popup_grab_button,
2082};
2083
2084static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002085popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002086{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002087 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002088 struct shell_seat *shseat =
2089 container_of(grab, struct shell_seat, popup_grab.grab);
2090 struct shell_surface *shsurf;
2091 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002092
2093 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002094 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002095 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002096 shseat->popup_grab.grab.interface = NULL;
2097 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002098 /* Send the popup_done event to all the popups open */
2099 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002100 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002101 shsurf->popup.shseat = NULL;
2102 if (prev) {
2103 wl_list_init(&prev->popup.grab_link);
2104 }
2105 prev = shsurf;
2106 }
2107 wl_list_init(&prev->popup.grab_link);
2108 wl_list_init(&shseat->popup_grab.surfaces_list);
2109 }
2110}
2111
2112static void
2113add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2114{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002115 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002116
2117 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002118 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002119 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002120 /* We must make sure here that this popup was opened after
2121 * a mouse press, and not just by moving around with other
2122 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002123 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002124 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002125
Rob Bradforddfe31052013-06-26 19:49:11 +01002126 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002127 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002128 } else {
2129 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002130 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002131}
2132
2133static void
2134remove_popup_grab(struct shell_surface *shsurf)
2135{
2136 struct shell_seat *shseat = shsurf->popup.shseat;
2137
2138 wl_list_remove(&shsurf->popup.grab_link);
2139 wl_list_init(&shsurf->popup.grab_link);
2140 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002141 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002142 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002143 }
2144}
2145
2146static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002147shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002148{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002149 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002150 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002151 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002152
2153 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002154
Pekka Paalanen483243f2013-03-08 14:56:50 +02002155 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002156 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2157 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002158
Kristian Høgsberge3148752013-05-06 23:19:49 -04002159 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002160 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002161 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002162 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002163 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002164 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002165}
2166
2167static void
2168shell_surface_set_popup(struct wl_client *client,
2169 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002170 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002171 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002172 struct wl_resource *parent_resource,
2173 int32_t x, int32_t y, uint32_t flags)
2174{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002175 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002176
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002177 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002178 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002179 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002180 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002181 shsurf->popup.x = x;
2182 shsurf->popup.y = y;
2183}
2184
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002185static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002186 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002187 shell_surface_move,
2188 shell_surface_resize,
2189 shell_surface_set_toplevel,
2190 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002191 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002192 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002193 shell_surface_set_maximized,
2194 shell_surface_set_title,
2195 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002196};
2197
2198static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002199destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002200{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002201 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2202
Giulio Camuffo5085a752013-03-25 21:42:45 +01002203 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2204 remove_popup_grab(shsurf);
2205 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002206
Alex Wubd3354b2012-04-17 17:20:49 +08002207 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002208 shell_surface_is_top_fullscreen(shsurf))
2209 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002210
Alex Wuaa08e2d2012-03-05 11:01:40 +08002211 if (shsurf->fullscreen.black_surface)
2212 weston_surface_destroy(shsurf->fullscreen.black_surface);
2213
Alex Wubd3354b2012-04-17 17:20:49 +08002214 /* As destroy_resource() use wl_list_for_each_safe(),
2215 * we can always remove the listener.
2216 */
2217 wl_list_remove(&shsurf->surface_destroy_listener.link);
2218 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002219 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002220 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002221
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002222 wl_list_remove(&shsurf->link);
2223 free(shsurf);
2224}
2225
2226static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002227shell_destroy_shell_surface(struct wl_resource *resource)
2228{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002229 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002230
2231 destroy_shell_surface(shsurf);
2232}
2233
2234static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002235shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002236{
2237 struct shell_surface *shsurf = container_of(listener,
2238 struct shell_surface,
2239 surface_destroy_listener);
2240
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002241 if (shsurf->resource)
2242 wl_resource_destroy(shsurf->resource);
2243 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002244 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002245}
2246
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002247static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002248shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002249
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002250static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002251get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002252{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002253 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002254 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002255 else
2256 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002257}
2258
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002259static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002260create_shell_surface(void *shell, struct weston_surface *surface,
2261 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002262{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002263 struct shell_surface *shsurf;
2264
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002265 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002266 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002267 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002268 }
2269
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002270 shsurf = calloc(1, sizeof *shsurf);
2271 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002272 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002273 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002274 }
2275
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002276 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002277 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002278
Tiago Vignattibc052c92012-04-19 16:18:18 +03002279 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002280 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002281 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002282 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002283 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002284 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2285 shsurf->fullscreen.framerate = 0;
2286 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002287 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002288 wl_list_init(&shsurf->fullscreen.transform.link);
2289
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002290 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002291 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002292 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002293 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002294
2295 /* init link so its safe to always remove it in destroy_shell_surface */
2296 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002297 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002298
Pekka Paalanen460099f2012-01-20 16:48:25 +02002299 /* empty when not in use */
2300 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002301 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002302
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002303 wl_list_init(&shsurf->workspace_transform.link);
2304
Pekka Paalanen98262232011-12-01 10:42:22 +02002305 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002306 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002307
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002308 shsurf->client = client;
2309
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002310 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002311}
2312
2313static void
2314shell_get_shell_surface(struct wl_client *client,
2315 struct wl_resource *resource,
2316 uint32_t id,
2317 struct wl_resource *surface_resource)
2318{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002319 struct weston_surface *surface =
2320 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002321 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002322 struct shell_surface *shsurf;
2323
2324 if (get_shell_surface(surface)) {
2325 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002326 WL_DISPLAY_ERROR_INVALID_OBJECT,
2327 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002328 return;
2329 }
2330
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002331 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002332 if (!shsurf) {
2333 wl_resource_post_error(surface_resource,
2334 WL_DISPLAY_ERROR_INVALID_OBJECT,
2335 "surface->configure already set");
2336 return;
2337 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002338
Jason Ekstranda85118c2013-06-27 20:17:02 -05002339 shsurf->resource =
2340 wl_resource_create(client,
2341 &wl_shell_surface_interface, 1, id);
2342 wl_resource_set_implementation(shsurf->resource,
2343 &shell_surface_implementation,
2344 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002345}
2346
2347static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002348 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002349};
2350
Kristian Høgsberg07937562011-04-12 17:25:42 -04002351static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002352shell_fade(struct desktop_shell *shell, enum fade_type type);
2353
2354static int
2355screensaver_timeout(void *data)
2356{
2357 struct desktop_shell *shell = data;
2358
2359 shell_fade(shell, FADE_OUT);
2360
2361 return 1;
2362}
2363
2364static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002365handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002366{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002367 struct desktop_shell *shell =
2368 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002369
Pekka Paalanen18027e52011-12-02 16:31:49 +02002370 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002371
2372 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002373 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002374}
2375
2376static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002377launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002378{
2379 if (shell->screensaver.binding)
2380 return;
2381
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002382 if (!shell->screensaver.path) {
2383 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002384 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002385 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002386
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002387 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002388 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002389 return;
2390 }
2391
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002392 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002393 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002394 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002395 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002396}
2397
2398static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002399terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002400{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002401 if (shell->screensaver.process.pid == 0)
2402 return;
2403
2404 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002405}
2406
2407static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002408configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002409{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002410 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002411
Giulio Camuffo184df502013-02-21 11:29:21 +01002412 if (width == 0)
2413 return;
2414
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002415 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2416 if (s->output == es->output && s != es) {
2417 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002418 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002419 }
2420 }
2421
Giulio Camuffo184df502013-02-21 11:29:21 +01002422 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002423
2424 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002425 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002426 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002427 }
2428}
2429
2430static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002431background_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 -04002432{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002433 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002434
Giulio Camuffo184df502013-02-21 11:29:21 +01002435 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002436}
2437
2438static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002439desktop_shell_set_background(struct wl_client *client,
2440 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002441 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002442 struct wl_resource *surface_resource)
2443{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002444 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002445 struct weston_surface *surface =
2446 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002447
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002448 if (surface->configure) {
2449 wl_resource_post_error(surface_resource,
2450 WL_DISPLAY_ERROR_INVALID_OBJECT,
2451 "surface role already assigned");
2452 return;
2453 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002454
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002455 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002456 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002457 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002458 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002459 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002460 surface->output->width,
2461 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002462}
2463
2464static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002465panel_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 -04002466{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002467 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002468
Giulio Camuffo184df502013-02-21 11:29:21 +01002469 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002470}
2471
2472static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002473desktop_shell_set_panel(struct wl_client *client,
2474 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002475 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002476 struct wl_resource *surface_resource)
2477{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002478 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002479 struct weston_surface *surface =
2480 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002481
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002482 if (surface->configure) {
2483 wl_resource_post_error(surface_resource,
2484 WL_DISPLAY_ERROR_INVALID_OBJECT,
2485 "surface role already assigned");
2486 return;
2487 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002488
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002489 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002490 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002491 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002492 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002493 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002494 surface->output->width,
2495 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002496}
2497
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002498static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002499lock_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 -04002500{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002501 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002502
Giulio Camuffo184df502013-02-21 11:29:21 +01002503 if (width == 0)
2504 return;
2505
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002506 surface->geometry.width = width;
2507 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002508 center_on_output(surface, get_default_output(shell->compositor));
2509
2510 if (!weston_surface_is_mapped(surface)) {
2511 wl_list_insert(&shell->lock_layer.surface_list,
2512 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002513 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002514 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002515 }
2516}
2517
2518static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002519handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002520{
Tiago Vignattibe143262012-04-16 17:31:41 +03002521 struct desktop_shell *shell =
2522 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002523
Martin Minarik6d118362012-06-07 18:01:59 +02002524 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002525 shell->lock_surface = NULL;
2526}
2527
2528static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002529desktop_shell_set_lock_surface(struct wl_client *client,
2530 struct wl_resource *resource,
2531 struct wl_resource *surface_resource)
2532{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002533 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002534 struct weston_surface *surface =
2535 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002536
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002537 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002538
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002539 if (!shell->locked)
2540 return;
2541
Pekka Paalanen98262232011-12-01 10:42:22 +02002542 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002543
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002544 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002545 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002546 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002547
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002548 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002549 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002550}
2551
2552static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002553resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002554{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002555 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002556
Pekka Paalanen77346a62011-11-30 16:26:35 +02002557 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002558
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002559 wl_list_remove(&shell->lock_layer.link);
2560 wl_list_insert(&shell->compositor->cursor_layer.link,
2561 &shell->fullscreen_layer.link);
2562 wl_list_insert(&shell->fullscreen_layer.link,
2563 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002564 if (shell->showing_input_panels) {
2565 wl_list_insert(&shell->panel_layer.link,
2566 &shell->input_panel_layer.link);
2567 wl_list_insert(&shell->input_panel_layer.link,
2568 &ws->layer.link);
2569 } else {
2570 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2571 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002572
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002573 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002574
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002575 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002576 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002577 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002578}
2579
2580static void
2581desktop_shell_unlock(struct wl_client *client,
2582 struct wl_resource *resource)
2583{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002584 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002585
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002586 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002587
2588 if (shell->locked)
2589 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002590}
2591
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002592static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002593desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002594 struct wl_resource *resource,
2595 struct wl_resource *surface_resource)
2596{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002597 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002598
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002599 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002600}
2601
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002602static void
2603desktop_shell_desktop_ready(struct wl_client *client,
2604 struct wl_resource *resource)
2605{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002606 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002607
2608 shell_fade_startup(shell);
2609}
2610
Kristian Høgsberg75840622011-09-06 13:48:16 -04002611static const struct desktop_shell_interface desktop_shell_implementation = {
2612 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002613 desktop_shell_set_panel,
2614 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002615 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002616 desktop_shell_set_grab_surface,
2617 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002618};
2619
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002620static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002621get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002622{
2623 struct shell_surface *shsurf;
2624
2625 shsurf = get_shell_surface(surface);
2626 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002627 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002628 return shsurf->type;
2629}
2630
Kristian Høgsberg75840622011-09-06 13:48:16 -04002631static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002632move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002633{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002634 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002635 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002636 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002637 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002638
Pekka Paalanen01388e22013-04-25 13:57:44 +03002639 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002640 if (surface == NULL)
2641 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002642
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002643 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002644 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2645 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002646 return;
2647
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002648 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002649}
2650
2651static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002652resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002653{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002654 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002655 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002656 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002657 uint32_t edges = 0;
2658 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002659 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002660
Pekka Paalanen01388e22013-04-25 13:57:44 +03002661 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002662 if (surface == NULL)
2663 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002664
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002665 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002666 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2667 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002668 return;
2669
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002670 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002671 wl_fixed_to_int(seat->pointer->grab_x),
2672 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002673 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002674
Pekka Paalanen60921e52012-01-25 15:55:43 +02002675 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002676 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002677 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002678 edges |= 0;
2679 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002680 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002681
Pekka Paalanen60921e52012-01-25 15:55:43 +02002682 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002683 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002684 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002685 edges |= 0;
2686 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002687 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002688
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002689 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002690}
2691
2692static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002693surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002694 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002695{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002696 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002697 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002698 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002699 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002700 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002701
Pekka Paalanen01388e22013-04-25 13:57:44 +03002702 /* XXX: broken for windows containing sub-surfaces */
2703 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002704 if (surface == NULL)
2705 return;
2706
2707 shsurf = get_shell_surface(surface);
2708 if (!shsurf)
2709 return;
2710
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002711 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002712
Scott Moreau02709af2012-05-22 01:54:10 -06002713 if (surface->alpha > 1.0)
2714 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002715 if (surface->alpha < step)
2716 surface->alpha = step;
2717
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002718 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002719 weston_surface_damage(surface);
2720}
2721
2722static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002723do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002724 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002725{
Daniel Stone37816df2012-05-16 18:45:18 +01002726 struct weston_seat *ws = (struct weston_seat *) seat;
2727 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002728 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002729 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002730
2731 wl_list_for_each(output, &compositor->output_list, link) {
2732 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002733 wl_fixed_to_double(seat->pointer->x),
2734 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002735 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002736 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002737 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002738 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002739 increment = -output->zoom.increment;
2740 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002741 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002742 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002743 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002744 else
2745 increment = 0;
2746
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002747 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002748
Scott Moreaue6603982012-06-11 13:07:51 -06002749 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002750 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002751 else if (output->zoom.level > output->zoom.max_level)
2752 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002753 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002754 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002755 output->disable_planes++;
2756 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002757
Scott Moreaue6603982012-06-11 13:07:51 -06002758 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002759
Scott Moreau8dacaab2012-06-17 18:10:58 -06002760 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002761 }
2762 }
2763}
2764
Scott Moreauccbf29d2012-02-22 14:21:41 -07002765static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002766zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002767 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002768{
2769 do_zoom(seat, time, 0, axis, value);
2770}
2771
2772static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002773zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002774 void *data)
2775{
2776 do_zoom(seat, time, key, 0, 0);
2777}
2778
2779static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002780terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002781 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002782{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002783 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002784
Daniel Stone325fc2d2012-05-30 16:31:58 +01002785 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002786}
2787
2788static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002789rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002790{
2791 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002792 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002793 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002794 struct shell_surface *shsurf = rotate->base.shsurf;
2795 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002796 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002797
2798 if (!shsurf)
2799 return;
2800
2801 surface = shsurf->surface;
2802
2803 cx = 0.5f * surface->geometry.width;
2804 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002805
Daniel Stone37816df2012-05-16 18:45:18 +01002806 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2807 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002808 r = sqrtf(dx * dx + dy * dy);
2809
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002810 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002811 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002812
2813 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002814 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002815 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002816
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002817 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002818 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002819
2820 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002821 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002822 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002823 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002824 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002825
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002826 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002827 &shsurf->surface->geometry.transformation_list,
2828 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002829 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002830 wl_list_init(&shsurf->rotation.transform.link);
2831 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002832 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002833 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002834
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002835 /* We need to adjust the position of the surface
2836 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002837 cposx = surface->geometry.x + cx;
2838 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002839 dposx = rotate->center.x - cposx;
2840 dposy = rotate->center.y - cposy;
2841 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002842 weston_surface_set_position(surface,
2843 surface->geometry.x + dposx,
2844 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002845 }
2846
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002847 /* Repaint implies weston_surface_update_transform(), which
2848 * lazily applies the damage due to rotation update.
2849 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002850 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002851}
2852
2853static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002854rotate_grab_button(struct weston_pointer_grab *grab,
2855 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002856{
2857 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002858 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002859 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002860 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002861 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002862
Daniel Stone4dbadb12012-05-30 16:31:51 +01002863 if (pointer->button_count == 0 &&
2864 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002865 if (shsurf)
2866 weston_matrix_multiply(&shsurf->rotation.rotation,
2867 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002868 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002869 free(rotate);
2870 }
2871}
2872
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002873static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002874 noop_grab_focus,
2875 rotate_grab_motion,
2876 rotate_grab_button,
2877};
2878
2879static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002880surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002881{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002882 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002883 float dx, dy;
2884 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002885
Pekka Paalanen460099f2012-01-20 16:48:25 +02002886 rotate = malloc(sizeof *rotate);
2887 if (!rotate)
2888 return;
2889
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002890 weston_surface_to_global_float(surface->surface,
Louis-Francis Ratté-Boulianne9a4f10f2013-07-03 15:58:22 +02002891 surface->surface->geometry.width * 0.5f,
2892 surface->surface->geometry.height * 0.5f,
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002893 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002894
Daniel Stone37816df2012-05-16 18:45:18 +01002895 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2896 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002897 r = sqrtf(dx * dx + dy * dy);
2898 if (r > 20.0f) {
2899 struct weston_matrix inverse;
2900
2901 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002902 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002903 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002904
2905 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002906 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002907 } else {
2908 weston_matrix_init(&surface->rotation.rotation);
2909 weston_matrix_init(&rotate->rotation);
2910 }
2911
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002912 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2913 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002914}
2915
2916static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002917rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002918 void *data)
2919{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002920 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002921 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002922 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002923 struct shell_surface *surface;
2924
Pekka Paalanen01388e22013-04-25 13:57:44 +03002925 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002926 if (base_surface == NULL)
2927 return;
2928
2929 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002930 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2931 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002932 return;
2933
2934 surface_rotate(surface, seat);
2935}
2936
2937static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002938lower_fullscreen_layer(struct desktop_shell *shell)
2939{
2940 struct workspace *ws;
2941 struct weston_surface *surface, *prev;
2942
2943 ws = get_current_workspace(shell);
2944 wl_list_for_each_reverse_safe(surface, prev,
2945 &shell->fullscreen_layer.surface_list,
2946 layer_link)
2947 weston_surface_restack(surface, &ws->layer.surface_list);
2948}
2949
2950static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002951activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002952 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002953{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002954 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002955 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002956 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002957
Pekka Paalanen01388e22013-04-25 13:57:44 +03002958 main_surface = weston_surface_get_main_surface(es);
2959
Daniel Stone37816df2012-05-16 18:45:18 +01002960 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002961
Jonas Ådahl8538b222012-08-29 22:13:03 +02002962 state = ensure_focus_state(shell, seat);
2963 if (state == NULL)
2964 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002965
2966 state->keyboard_focus = es;
2967 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002968 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002969
Pekka Paalanen01388e22013-04-25 13:57:44 +03002970 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002971 case SHELL_SURFACE_FULLSCREEN:
2972 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002973 shell_stack_fullscreen(get_shell_surface(main_surface));
2974 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002975 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002976 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002977 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002978 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002979 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002980 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002981 }
2982}
2983
Alex Wu21858432012-04-01 20:13:08 +08002984/* no-op func for checking black surface */
2985static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002986black_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 +08002987{
2988}
2989
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002990static bool
Alex Wu21858432012-04-01 20:13:08 +08002991is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2992{
2993 if (es->configure == black_surface_configure) {
2994 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002995 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002996 return true;
2997 }
2998 return false;
2999}
3000
Kristian Høgsberg75840622011-09-06 13:48:16 -04003001static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003002click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003003 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003004{
Daniel Stone37816df2012-05-16 18:45:18 +01003005 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003006 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003007 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003008 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003009
Daniel Stone37816df2012-05-16 18:45:18 +01003010 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003011 if (!focus)
3012 return;
3013
Pekka Paalanen01388e22013-04-25 13:57:44 +03003014 if (is_black_surface(focus, &main_surface))
3015 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003016
Pekka Paalanen01388e22013-04-25 13:57:44 +03003017 main_surface = weston_surface_get_main_surface(focus);
3018 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003019 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003020
Daniel Stone325fc2d2012-05-30 16:31:58 +01003021 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003022 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003023}
3024
3025static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003026lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003027{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003028 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003029
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003030 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003031 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003032 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003033 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003034
3035 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003036
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003037 /* Hide all surfaces by removing the fullscreen, panel and
3038 * toplevel layers. This way nothing else can show or receive
3039 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003040
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003041 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003042 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003043 if (shell->showing_input_panels)
3044 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003045 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003046 wl_list_insert(&shell->compositor->cursor_layer.link,
3047 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003048
Pekka Paalanen77346a62011-11-30 16:26:35 +02003049 launch_screensaver(shell);
3050
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003051 /* TODO: disable bindings that should not work while locked. */
3052
3053 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003054}
3055
3056static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003057unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003058{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003059 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003060 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003061 return;
3062 }
3063
3064 /* If desktop-shell client has gone away, unlock immediately. */
3065 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003066 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003067 return;
3068 }
3069
3070 if (shell->prepare_event_sent)
3071 return;
3072
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003073 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003074 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003075}
3076
3077static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003078shell_fade_done(struct weston_surface_animation *animation, void *data)
3079{
3080 struct desktop_shell *shell = data;
3081
3082 shell->fade.animation = NULL;
3083
3084 switch (shell->fade.type) {
3085 case FADE_IN:
3086 weston_surface_destroy(shell->fade.surface);
3087 shell->fade.surface = NULL;
3088 break;
3089 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003090 lock(shell);
3091 break;
3092 }
3093}
3094
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003095static struct weston_surface *
3096shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003097{
3098 struct weston_compositor *compositor = shell->compositor;
3099 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003100
3101 surface = weston_surface_create(compositor);
3102 if (!surface)
3103 return NULL;
3104
3105 weston_surface_configure(surface, 0, 0, 8192, 8192);
3106 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3107 wl_list_insert(&compositor->fade_layer.surface_list,
3108 &surface->layer_link);
3109 pixman_region32_init(&surface->input);
3110
3111 return surface;
3112}
3113
3114static void
3115shell_fade(struct desktop_shell *shell, enum fade_type type)
3116{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003117 float tint;
3118
3119 switch (type) {
3120 case FADE_IN:
3121 tint = 0.0;
3122 break;
3123 case FADE_OUT:
3124 tint = 1.0;
3125 break;
3126 default:
3127 weston_log("shell: invalid fade type\n");
3128 return;
3129 }
3130
3131 shell->fade.type = type;
3132
3133 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003134 shell->fade.surface = shell_fade_create_surface(shell);
3135 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003136 return;
3137
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003138 shell->fade.surface->alpha = 1.0 - tint;
3139 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003140 }
3141
3142 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003143 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003144 else
3145 shell->fade.animation =
3146 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003147 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003148 shell_fade_done, shell);
3149}
3150
3151static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003152do_shell_fade_startup(void *data)
3153{
3154 struct desktop_shell *shell = data;
3155
3156 shell_fade(shell, FADE_IN);
3157}
3158
3159static void
3160shell_fade_startup(struct desktop_shell *shell)
3161{
3162 struct wl_event_loop *loop;
3163
3164 if (!shell->fade.startup_timer)
3165 return;
3166
3167 wl_event_source_remove(shell->fade.startup_timer);
3168 shell->fade.startup_timer = NULL;
3169
3170 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3171 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3172}
3173
3174static int
3175fade_startup_timeout(void *data)
3176{
3177 struct desktop_shell *shell = data;
3178
3179 shell_fade_startup(shell);
3180 return 0;
3181}
3182
3183static void
3184shell_fade_init(struct desktop_shell *shell)
3185{
3186 /* Make compositor output all black, and wait for the desktop-shell
3187 * client to signal it is ready, then fade in. The timer triggers a
3188 * fade-in, in case the desktop-shell client takes too long.
3189 */
3190
3191 struct wl_event_loop *loop;
3192
3193 if (shell->fade.surface != NULL) {
3194 weston_log("%s: warning: fade surface already exists\n",
3195 __func__);
3196 return;
3197 }
3198
3199 shell->fade.surface = shell_fade_create_surface(shell);
3200 if (!shell->fade.surface)
3201 return;
3202
3203 weston_surface_update_transform(shell->fade.surface);
3204 weston_surface_damage(shell->fade.surface);
3205
3206 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3207 shell->fade.startup_timer =
3208 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3209 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3210}
3211
3212static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003213idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003214{
3215 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003216 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003217
3218 shell_fade(shell, FADE_OUT);
3219 /* lock() is called from shell_fade_done() */
3220}
3221
3222static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003223wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003224{
3225 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003226 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003227
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003228 unlock(shell);
3229}
3230
3231static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003232show_input_panels(struct wl_listener *listener, void *data)
3233{
3234 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003235 container_of(listener, struct desktop_shell,
3236 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003237 struct input_panel_surface *surface, *next;
3238 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003239
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003240 shell->text_input.surface = (struct weston_surface*)data;
3241
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003242 if (shell->showing_input_panels)
3243 return;
3244
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003245 shell->showing_input_panels = true;
3246
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003247 if (!shell->locked)
3248 wl_list_insert(&shell->panel_layer.link,
3249 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003250
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003251 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003252 &shell->input_panel.surfaces, link) {
3253 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003254 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003255 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003256 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003257 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003258 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003259 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003260 weston_surface_damage(ws);
3261 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003262 }
3263}
3264
3265static void
3266hide_input_panels(struct wl_listener *listener, void *data)
3267{
3268 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003269 container_of(listener, struct desktop_shell,
3270 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003271 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003272
Jan Arne Petersen61381972013-01-31 15:52:21 +01003273 if (!shell->showing_input_panels)
3274 return;
3275
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003276 shell->showing_input_panels = false;
3277
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003278 if (!shell->locked)
3279 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003280
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003281 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003282 &shell->input_panel_layer.surface_list, layer_link)
3283 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003284}
3285
3286static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003287update_input_panels(struct wl_listener *listener, void *data)
3288{
3289 struct desktop_shell *shell =
3290 container_of(listener, struct desktop_shell,
3291 update_input_panel_listener);
3292
3293 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3294}
3295
3296static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003297center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003298{
Giulio Camuffob8366642013-04-25 13:57:46 +03003299 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003300 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003301
Giulio Camuffob8366642013-04-25 13:57:46 +03003302 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3303
3304 x = output->x + (output->width - width) / 2 - surf_x / 2;
3305 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003306
3307 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003308}
3309
3310static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003311weston_surface_set_initial_position (struct weston_surface *surface,
3312 struct desktop_shell *shell)
3313{
3314 struct weston_compositor *compositor = shell->compositor;
3315 int ix = 0, iy = 0;
3316 int range_x, range_y;
3317 int dx, dy, x, y, panel_height;
3318 struct weston_output *output, *target_output = NULL;
3319 struct weston_seat *seat;
3320
3321 /* As a heuristic place the new window on the same output as the
3322 * pointer. Falling back to the output containing 0, 0.
3323 *
3324 * TODO: Do something clever for touch too?
3325 */
3326 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003327 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003328 ix = wl_fixed_to_int(seat->pointer->x);
3329 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003330 break;
3331 }
3332 }
3333
3334 wl_list_for_each(output, &compositor->output_list, link) {
3335 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3336 target_output = output;
3337 break;
3338 }
3339 }
3340
3341 if (!target_output) {
3342 weston_surface_set_position(surface, 10 + random() % 400,
3343 10 + random() % 400);
3344 return;
3345 }
3346
3347 /* Valid range within output where the surface will still be onscreen.
3348 * If this is negative it means that the surface is bigger than
3349 * output.
3350 */
3351 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003352 range_x = target_output->width - surface->geometry.width;
3353 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003354 surface->geometry.height;
3355
Rob Bradford4cb88c72012-08-13 15:18:44 +01003356 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003357 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003358 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003359 dx = 0;
3360
3361 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003362 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003363 else
3364 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003365
3366 x = target_output->x + dx;
3367 y = target_output->y + dy;
3368
3369 weston_surface_set_position (surface, x, y);
3370}
3371
3372static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003373map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003374 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003375{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003376 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003377 struct shell_surface *shsurf = get_shell_surface(surface);
3378 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003379 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003380 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003381 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003382 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003383 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003384
Pekka Paalanen60921e52012-01-25 15:55:43 +02003385 surface->geometry.width = width;
3386 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003387 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003388
3389 /* initial positioning, see also configure() */
3390 switch (surface_type) {
3391 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003392 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003393 break;
Alex Wu4539b082012-03-01 12:57:46 +08003394 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003395 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003396 shell_map_fullscreen(shsurf);
3397 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003398 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003399 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003400 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003401 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3402 NULL, NULL);
3403 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3404 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003405 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003406 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003407 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003408 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003409 case SHELL_SURFACE_NONE:
3410 weston_surface_set_position(surface,
3411 surface->geometry.x + sx,
3412 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003413 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003414 default:
3415 ;
3416 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003417
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003418 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003419 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003420 case SHELL_SURFACE_POPUP:
3421 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003422 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003423 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3424 break;
Alex Wu4539b082012-03-01 12:57:46 +08003425 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003426 case SHELL_SURFACE_NONE:
3427 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003428 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003429 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003430 ws = get_current_workspace(shell);
3431 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003432 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003433 }
3434
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003435 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003436 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003437 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3438 surface->output = shsurf->output;
3439 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003440
Juan Zhao7bb92f02011-12-15 11:31:51 -05003441 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003442 /* XXX: xwayland's using the same fields for transient type */
3443 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003444 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003445 if (shsurf->transient.flags ==
3446 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3447 break;
3448 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003449 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003450 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003451 if (!shell->locked) {
3452 wl_list_for_each(seat, &compositor->seat_list, link)
3453 activate(shell, surface, seat);
3454 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003455 break;
3456 default:
3457 break;
3458 }
3459
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003460 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003461 {
3462 switch (shell->win_animation_type) {
3463 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003464 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003465 break;
3466 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003467 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003468 break;
3469 default:
3470 break;
3471 }
3472 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003473}
3474
3475static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003476configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003477 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003478{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003479 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3480 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003481 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003482
Pekka Paalanen77346a62011-11-30 16:26:35 +02003483 shsurf = get_shell_surface(surface);
3484 if (shsurf)
3485 surface_type = shsurf->type;
3486
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003487 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003488
3489 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003490 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003491 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003492 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003493 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003494 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003495 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003496 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3497 NULL, NULL);
3498 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003499 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003500 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003501 break;
Alex Wu4539b082012-03-01 12:57:46 +08003502 case SHELL_SURFACE_TOPLEVEL:
3503 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003504 default:
3505 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003506 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003507
Alex Wu4539b082012-03-01 12:57:46 +08003508 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003509 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003510 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003511
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003512 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003513 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003514 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003515}
3516
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003517static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003518shell_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 +03003519{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003520 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003521 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003522
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003523 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003524
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003525 if (!weston_surface_is_mapped(es) &&
3526 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003527 remove_popup_grab(shsurf);
3528 }
3529
Giulio Camuffo184df502013-02-21 11:29:21 +01003530 if (width == 0)
3531 return;
3532
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003533 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003534 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003535 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003536 type_changed = 1;
3537 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003538
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003539 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003540 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003541 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003542 es->geometry.width != width ||
3543 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003544 float from_x, from_y;
3545 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003546
3547 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3548 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3549 configure(shell, es,
3550 es->geometry.x + to_x - from_x,
3551 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003552 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003553 }
3554}
3555
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003556static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003557
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003558static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003559desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003560{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003561 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003562 struct desktop_shell *shell =
3563 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003564
3565 shell->child.process.pid = 0;
3566 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003567
3568 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3569 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003570 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003571 shell->child.deathstamp = time;
3572 shell->child.deathcount = 0;
3573 }
3574
3575 shell->child.deathcount++;
3576 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003577 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003578 return;
3579 }
3580
Martin Minarik6d118362012-06-07 18:01:59 +02003581 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003582 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003583 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003584}
3585
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003586static void
3587launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003588{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003589 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003590 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003591
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003592 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003593 &shell->child.process,
3594 shell_exe,
3595 desktop_shell_sigchld);
3596
3597 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003598 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003599}
3600
3601static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003602bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3603{
Tiago Vignattibe143262012-04-16 17:31:41 +03003604 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003605 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003606
Jason Ekstranda85118c2013-06-27 20:17:02 -05003607 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3608 if (resource)
3609 wl_resource_set_implementation(resource, &shell_implementation,
3610 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003611}
3612
Kristian Høgsberg75840622011-09-06 13:48:16 -04003613static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003614unbind_desktop_shell(struct wl_resource *resource)
3615{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003616 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003617
3618 if (shell->locked)
3619 resume_desktop(shell);
3620
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003621 shell->child.desktop_shell = NULL;
3622 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003623}
3624
3625static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003626bind_desktop_shell(struct wl_client *client,
3627 void *data, uint32_t version, uint32_t id)
3628{
Tiago Vignattibe143262012-04-16 17:31:41 +03003629 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003630 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003631
Jason Ekstranda85118c2013-06-27 20:17:02 -05003632 resource = wl_resource_create(client, &desktop_shell_interface,
3633 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003634
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003635 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003636 wl_resource_set_implementation(resource,
3637 &desktop_shell_implementation,
3638 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003639 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003640
3641 if (version < 2)
3642 shell_fade_startup(shell);
3643
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003644 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003645 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003646
3647 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3648 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003649 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003650}
3651
Pekka Paalanen6e168112011-11-24 11:34:05 +02003652static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003653screensaver_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 -04003654{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003655 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003656
Giulio Camuffo184df502013-02-21 11:29:21 +01003657 if (width == 0)
3658 return;
3659
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003660 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003661 if (!shell->locked)
3662 return;
3663
3664 center_on_output(surface, surface->output);
3665
3666 if (wl_list_empty(&surface->layer_link)) {
3667 wl_list_insert(shell->lock_layer.surface_list.prev,
3668 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003669 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003670 wl_event_source_timer_update(shell->screensaver.timer,
3671 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003672 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003673 }
3674}
3675
3676static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003677screensaver_set_surface(struct wl_client *client,
3678 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003679 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003680 struct wl_resource *output_resource)
3681{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003682 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003683 struct weston_surface *surface =
3684 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003685 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003686
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003687 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003688 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003689 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003690}
3691
3692static const struct screensaver_interface screensaver_implementation = {
3693 screensaver_set_surface
3694};
3695
3696static void
3697unbind_screensaver(struct wl_resource *resource)
3698{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003699 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003700
Pekka Paalanen77346a62011-11-30 16:26:35 +02003701 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003702}
3703
3704static void
3705bind_screensaver(struct wl_client *client,
3706 void *data, uint32_t version, uint32_t id)
3707{
Tiago Vignattibe143262012-04-16 17:31:41 +03003708 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003709 struct wl_resource *resource;
3710
Jason Ekstranda85118c2013-06-27 20:17:02 -05003711 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003712
Pekka Paalanen77346a62011-11-30 16:26:35 +02003713 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003714 wl_resource_set_implementation(resource,
3715 &screensaver_implementation,
3716 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003717 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003718 return;
3719 }
3720
3721 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3722 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003723 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003724}
3725
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003726static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003727input_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 -04003728{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003729 struct input_panel_surface *ip_surface = surface->configure_private;
3730 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003731 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003732 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003733
Giulio Camuffo184df502013-02-21 11:29:21 +01003734 if (width == 0)
3735 return;
3736
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003737 if (!weston_surface_is_mapped(surface)) {
3738 if (!shell->showing_input_panels)
3739 return;
3740
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003741 show_surface = 1;
3742 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003743
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003744 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003745
3746 if (ip_surface->panel) {
3747 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3748 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3749 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01003750 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
3751 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003752 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003753
3754 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003755 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003756 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003757
3758 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003759 wl_list_insert(&shell->input_panel_layer.surface_list,
3760 &surface->layer_link);
3761 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003762 weston_surface_damage(surface);
3763 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3764 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003765}
3766
3767static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003768destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003769{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003770 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3771
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003772 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003773 wl_list_remove(&input_panel_surface->link);
3774
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003775 input_panel_surface->surface->configure = NULL;
3776
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003777 free(input_panel_surface);
3778}
3779
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003780static struct input_panel_surface *
3781get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003782{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003783 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003784 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003785 } else {
3786 return NULL;
3787 }
3788}
3789
3790static void
3791input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3792{
3793 struct input_panel_surface *ipsurface = container_of(listener,
3794 struct input_panel_surface,
3795 surface_destroy_listener);
3796
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003797 if (ipsurface->resource) {
3798 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003799 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003800 destroy_input_panel_surface(ipsurface);
3801 }
3802}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003803
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003804static struct input_panel_surface *
3805create_input_panel_surface(struct desktop_shell *shell,
3806 struct weston_surface *surface)
3807{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003808 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003809
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003810 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3811 if (!input_panel_surface)
3812 return NULL;
3813
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003814 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003815 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003816
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003817 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003818
3819 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003820
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003821 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003822 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003823 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003824 &input_panel_surface->surface_destroy_listener);
3825
3826 wl_list_init(&input_panel_surface->link);
3827
3828 return input_panel_surface;
3829}
3830
3831static void
3832input_panel_surface_set_toplevel(struct wl_client *client,
3833 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003834 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003835 uint32_t position)
3836{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003837 struct input_panel_surface *input_panel_surface =
3838 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003839 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003840
3841 wl_list_insert(&shell->input_panel.surfaces,
3842 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003843
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003844 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003845 input_panel_surface->panel = 0;
3846}
3847
3848static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003849input_panel_surface_set_overlay_panel(struct wl_client *client,
3850 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003851{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003852 struct input_panel_surface *input_panel_surface =
3853 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003854 struct desktop_shell *shell = input_panel_surface->shell;
3855
3856 wl_list_insert(&shell->input_panel.surfaces,
3857 &input_panel_surface->link);
3858
3859 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003860}
3861
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003862static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003863 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003864 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003865};
3866
3867static void
3868destroy_input_panel_surface_resource(struct wl_resource *resource)
3869{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003870 struct input_panel_surface *ipsurf =
3871 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003872
3873 destroy_input_panel_surface(ipsurf);
3874}
3875
3876static void
3877input_panel_get_input_panel_surface(struct wl_client *client,
3878 struct wl_resource *resource,
3879 uint32_t id,
3880 struct wl_resource *surface_resource)
3881{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003882 struct weston_surface *surface =
3883 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003884 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003885 struct input_panel_surface *ipsurf;
3886
3887 if (get_input_panel_surface(surface)) {
3888 wl_resource_post_error(surface_resource,
3889 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003890 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003891 return;
3892 }
3893
3894 ipsurf = create_input_panel_surface(shell, surface);
3895 if (!ipsurf) {
3896 wl_resource_post_error(surface_resource,
3897 WL_DISPLAY_ERROR_INVALID_OBJECT,
3898 "surface->configure already set");
3899 return;
3900 }
3901
Jason Ekstranda85118c2013-06-27 20:17:02 -05003902 ipsurf->resource =
3903 wl_resource_create(client,
3904 &wl_input_panel_surface_interface, 1, id);
3905 wl_resource_set_implementation(ipsurf->resource,
3906 &input_panel_surface_implementation,
3907 ipsurf,
3908 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003909}
3910
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003911static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003912 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003913};
3914
3915static void
3916unbind_input_panel(struct wl_resource *resource)
3917{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003918 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003919
3920 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003921}
3922
3923static void
3924bind_input_panel(struct wl_client *client,
3925 void *data, uint32_t version, uint32_t id)
3926{
3927 struct desktop_shell *shell = data;
3928 struct wl_resource *resource;
3929
Jason Ekstranda85118c2013-06-27 20:17:02 -05003930 resource = wl_resource_create(client,
3931 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003932
3933 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003934 wl_resource_set_implementation(resource,
3935 &input_panel_implementation,
3936 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003937 shell->input_panel.binding = resource;
3938 return;
3939 }
3940
3941 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3942 "interface object already bound");
3943 wl_resource_destroy(resource);
3944}
3945
Kristian Høgsberg07045392012-02-19 18:52:44 -05003946struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003947 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003948 struct weston_surface *current;
3949 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003950 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003951};
3952
3953static void
3954switcher_next(struct switcher *switcher)
3955{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003956 struct weston_surface *surface;
3957 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003958 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003959 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003960
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003961 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003962 switch (get_shell_surface_type(surface)) {
3963 case SHELL_SURFACE_TOPLEVEL:
3964 case SHELL_SURFACE_FULLSCREEN:
3965 case SHELL_SURFACE_MAXIMIZED:
3966 if (first == NULL)
3967 first = surface;
3968 if (prev == switcher->current)
3969 next = surface;
3970 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003971 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003972 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003973 weston_surface_damage(surface);
3974 break;
3975 default:
3976 break;
3977 }
Alex Wu1659daa2012-04-01 20:13:09 +08003978
3979 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003980 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003981 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003982 weston_surface_damage(surface);
3983 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003984 }
3985
3986 if (next == NULL)
3987 next = first;
3988
Alex Wu07b26062012-03-12 16:06:01 +08003989 if (next == NULL)
3990 return;
3991
Kristian Høgsberg07045392012-02-19 18:52:44 -05003992 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003993 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003994
3995 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003996 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003997
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003998 shsurf = get_shell_surface(switcher->current);
3999 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004000 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004001}
4002
4003static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004004switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004005{
4006 struct switcher *switcher =
4007 container_of(listener, struct switcher, listener);
4008
4009 switcher_next(switcher);
4010}
4011
4012static void
Daniel Stone351eb612012-05-31 15:27:47 -04004013switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004014{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004015 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004016 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004017 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004018
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004019 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004020 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004021 weston_surface_damage(surface);
4022 }
4023
Alex Wu07b26062012-03-12 16:06:01 +08004024 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004025 activate(switcher->shell, switcher->current,
4026 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004027 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004028 weston_keyboard_end_grab(keyboard);
4029 if (keyboard->input_method_resource)
4030 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004031 free(switcher);
4032}
4033
4034static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004035switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004036 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004037{
4038 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004039 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004040
Daniel Stonec9785ea2012-05-30 16:31:52 +01004041 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004042 switcher_next(switcher);
4043}
4044
4045static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004046switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004047 uint32_t mods_depressed, uint32_t mods_latched,
4048 uint32_t mods_locked, uint32_t group)
4049{
4050 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004051 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004052
Daniel Stone351eb612012-05-31 15:27:47 -04004053 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4054 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004055}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004056
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004057static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004058 switcher_key,
4059 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004060};
4061
4062static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004063switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004064 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004065{
Tiago Vignattibe143262012-04-16 17:31:41 +03004066 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004067 struct switcher *switcher;
4068
4069 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004070 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004071 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004072 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004073 wl_list_init(&switcher->listener.link);
4074
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004075 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004076 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004077 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004078 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4079 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004080 switcher_next(switcher);
4081}
4082
Pekka Paalanen3c647232011-12-22 13:43:43 +02004083static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004084backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004085 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004086{
4087 struct weston_compositor *compositor = data;
4088 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004089 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004090
4091 /* TODO: we're limiting to simple use cases, where we assume just
4092 * control on the primary display. We'd have to extend later if we
4093 * ever get support for setting backlights on random desktop LCD
4094 * panels though */
4095 output = get_default_output(compositor);
4096 if (!output)
4097 return;
4098
4099 if (!output->set_backlight)
4100 return;
4101
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004102 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4103 backlight_new = output->backlight_current - 25;
4104 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4105 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004106
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004107 if (backlight_new < 5)
4108 backlight_new = 5;
4109 if (backlight_new > 255)
4110 backlight_new = 255;
4111
4112 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004113 output->set_backlight(output, output->backlight_current);
4114}
4115
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004116struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004117 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004118 struct weston_seat *seat;
4119 uint32_t key[2];
4120 int key_released[2];
4121};
4122
4123static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004124debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004125 uint32_t key, uint32_t state)
4126{
4127 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4128 struct wl_resource *resource;
4129 struct wl_display *display;
4130 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) {
4164 struct weston_compositor *ec = db->seat->compositor;
4165
4166 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4167 key, state)) {
4168 /* We ran a binding so swallow the press and keep the
4169 * grab to swallow the released too. */
4170 send = 0;
4171 terminate = 0;
4172 db->key[1] = key;
4173 } else {
4174 /* Terminate the grab since the key pressed is not a
4175 * debug binding key. */
4176 send = 1;
4177 terminate = 1;
4178 }
4179 }
4180
4181 if (send) {
4182 resource = grab->keyboard->focus_resource;
4183
4184 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004185 display = wl_client_get_display(wl_resource_get_client(resource));
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004186 serial = wl_display_next_serial(display);
4187 wl_keyboard_send_key(resource, serial, time, key, state);
4188 }
4189 }
4190
4191 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004192 weston_keyboard_end_grab(grab->keyboard);
4193 if (grab->keyboard->input_method_resource)
4194 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004195 free(db);
4196 }
4197}
4198
4199static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004200debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004201 uint32_t mods_depressed, uint32_t mods_latched,
4202 uint32_t mods_locked, uint32_t group)
4203{
4204 struct wl_resource *resource;
4205
4206 resource = grab->keyboard->focus_resource;
4207 if (!resource)
4208 return;
4209
4210 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4211 mods_latched, mods_locked, group);
4212}
4213
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004214struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004215 debug_binding_key,
4216 debug_binding_modifiers
4217};
4218
4219static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004220debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004221{
4222 struct debug_binding_grab *grab;
4223
4224 grab = calloc(1, sizeof *grab);
4225 if (!grab)
4226 return;
4227
4228 grab->seat = (struct weston_seat *) seat;
4229 grab->key[0] = key;
4230 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004231 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004232}
4233
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004234static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004235force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004236 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004237{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004238 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004239 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004240 struct desktop_shell *shell = data;
4241 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004242 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004243
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004244 focus_surface = seat->keyboard->focus;
4245 if (!focus_surface)
4246 return;
4247
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004248 wl_signal_emit(&compositor->kill_signal, focus_surface);
4249
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004250 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004251 wl_client_get_credentials(client, &pid, NULL, NULL);
4252
4253 /* Skip clients that we launched ourselves (the credentials of
4254 * the socketpair is ours) */
4255 if (pid == getpid())
4256 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004257
Daniel Stone325fc2d2012-05-30 16:31:58 +01004258 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004259}
4260
4261static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004262workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004263 uint32_t key, void *data)
4264{
4265 struct desktop_shell *shell = data;
4266 unsigned int new_index = shell->workspaces.current;
4267
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004268 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004269 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004270 if (new_index != 0)
4271 new_index--;
4272
4273 change_workspace(shell, new_index);
4274}
4275
4276static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004277workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004278 uint32_t key, void *data)
4279{
4280 struct desktop_shell *shell = data;
4281 unsigned int new_index = shell->workspaces.current;
4282
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004283 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004284 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004285 if (new_index < shell->workspaces.num - 1)
4286 new_index++;
4287
4288 change_workspace(shell, new_index);
4289}
4290
4291static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004292workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004293 uint32_t key, void *data)
4294{
4295 struct desktop_shell *shell = data;
4296 unsigned int new_index;
4297
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004298 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004299 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004300 new_index = key - KEY_F1;
4301 if (new_index >= shell->workspaces.num)
4302 new_index = shell->workspaces.num - 1;
4303
4304 change_workspace(shell, new_index);
4305}
4306
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004307static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004308workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004309 uint32_t key, void *data)
4310{
4311 struct desktop_shell *shell = data;
4312 unsigned int new_index = shell->workspaces.current;
4313
4314 if (shell->locked)
4315 return;
4316
4317 if (new_index != 0)
4318 new_index--;
4319
4320 take_surface_to_workspace_by_seat(shell, seat, new_index);
4321}
4322
4323static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004324workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004325 uint32_t key, void *data)
4326{
4327 struct desktop_shell *shell = data;
4328 unsigned int new_index = shell->workspaces.current;
4329
4330 if (shell->locked)
4331 return;
4332
4333 if (new_index < shell->workspaces.num - 1)
4334 new_index++;
4335
4336 take_surface_to_workspace_by_seat(shell, seat, new_index);
4337}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004338
4339static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004340shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004341{
Tiago Vignattibe143262012-04-16 17:31:41 +03004342 struct desktop_shell *shell =
4343 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004344 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004345
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004346 if (shell->child.client)
4347 wl_client_destroy(shell->child.client);
4348
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004349 wl_list_remove(&shell->idle_listener.link);
4350 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004351 wl_list_remove(&shell->show_input_panel_listener.link);
4352 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004353
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004354 wl_array_for_each(ws, &shell->workspaces.array)
4355 workspace_destroy(*ws);
4356 wl_array_release(&shell->workspaces.array);
4357
Pekka Paalanen3c647232011-12-22 13:43:43 +02004358 free(shell->screensaver.path);
4359 free(shell);
4360}
4361
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004362static void
4363shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4364{
4365 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004366 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004367
4368 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004369 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4370 MODIFIER_CTRL | MODIFIER_ALT,
4371 terminate_binding, ec);
4372 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4373 click_to_activate_binding,
4374 shell);
4375 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4376 MODIFIER_SUPER | MODIFIER_ALT,
4377 surface_opacity_binding, NULL);
4378 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4379 MODIFIER_SUPER, zoom_axis_binding,
4380 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004381
4382 /* configurable bindings */
4383 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004384 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4385 zoom_key_binding, NULL);
4386 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4387 zoom_key_binding, NULL);
4388 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4389 shell);
4390 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4391 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004392
4393 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4394 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4395 rotate_binding, NULL);
4396
Daniel Stone325fc2d2012-05-30 16:31:58 +01004397 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4398 shell);
4399 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4400 ec);
4401 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4402 backlight_binding, ec);
4403 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4404 ec);
4405 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4406 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004407 weston_compositor_add_key_binding(ec, KEY_K, mod,
4408 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004409 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4410 workspace_up_binding, shell);
4411 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4412 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004413 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4414 workspace_move_surface_up_binding,
4415 shell);
4416 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4417 workspace_move_surface_down_binding,
4418 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004419
4420 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4421 if (shell->workspaces.num > 1) {
4422 num_workspace_bindings = shell->workspaces.num;
4423 if (num_workspace_bindings > 6)
4424 num_workspace_bindings = 6;
4425 for (i = 0; i < num_workspace_bindings; i++)
4426 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4427 workspace_f_binding,
4428 shell);
4429 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004430
4431 /* Debug bindings */
4432 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4433 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004434}
4435
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004436WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004437module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004438 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004439{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004440 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004441 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004442 struct workspace **pws;
4443 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004444 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004445
Peter Huttererf3d62272013-08-08 11:57:05 +10004446 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004447 if (shell == NULL)
4448 return -1;
4449
Kristian Høgsberg75840622011-09-06 13:48:16 -04004450 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004451
4452 shell->destroy_listener.notify = shell_destroy;
4453 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004454 shell->idle_listener.notify = idle_handler;
4455 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4456 shell->wake_listener.notify = wake_handler;
4457 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004458 shell->show_input_panel_listener.notify = show_input_panels;
4459 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4460 shell->hide_input_panel_listener.notify = hide_input_panels;
4461 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004462 shell->update_input_panel_listener.notify = update_input_panels;
4463 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004464 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004465 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004466 ec->shell_interface.create_shell_surface = create_shell_surface;
4467 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004468 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004469 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004470 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004471 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004472 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004473
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004474 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004475
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004476 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4477 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004478 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4479 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004480 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004481
4482 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004483 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004484
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004485 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004486
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004487 for (i = 0; i < shell->workspaces.num; i++) {
4488 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4489 if (pws == NULL)
4490 return -1;
4491
4492 *pws = workspace_create();
4493 if (*pws == NULL)
4494 return -1;
4495 }
4496 activate_workspace(shell, 0);
4497
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004498 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004499 wl_list_init(&shell->workspaces.animation.link);
4500 shell->workspaces.animation.frame = animate_workspace_change_frame;
4501
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004502 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004503 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004504 return -1;
4505
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004506 if (wl_global_create(ec->wl_display,
4507 &desktop_shell_interface, 2,
4508 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004509 return -1;
4510
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004511 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4512 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004513 return -1;
4514
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004515 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004516 shell, bind_input_panel) == NULL)
4517 return -1;
4518
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004519 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4520 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004521 return -1;
4522
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004523 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004524
4525 loop = wl_display_get_event_loop(ec->wl_display);
4526 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004527
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004528 shell->screensaver.timer =
4529 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4530
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004531 wl_list_for_each(seat, &ec->seat_list, link)
4532 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004533
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004534 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004535
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004536 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004537
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004538 return 0;
4539}