blob: d747bc15675aef2eb291ddcf00a9e6ba2122dc8a [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050026#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040027#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020028#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
51 ANIMATION_FADE
52};
53
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020054enum fade_type {
55 FADE_IN,
56 FADE_OUT
57};
58
Jonas Ådahl04769742012-06-13 00:01:24 +020059struct focus_state {
60 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040061 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020062 struct weston_surface *keyboard_focus;
63 struct wl_list link;
64 struct wl_listener seat_destroy_listener;
65 struct wl_listener surface_destroy_listener;
66};
67
Jonas Ådahle3cddce2012-06-13 00:01:22 +020068struct workspace {
69 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020070
71 struct wl_list focus_list;
72 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020073};
74
Philipp Brüschweiler88013572012-08-06 13:44:42 +020075struct input_panel_surface {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010076 struct wl_resource resource;
77
78 struct desktop_shell *shell;
79
Philipp Brüschweiler88013572012-08-06 13:44:42 +020080 struct wl_list link;
81 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010082 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020083
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020084 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020085 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020086};
87
Tiago Vignattibe143262012-04-16 17:31:41 +030088struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050089 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040090
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020091 struct wl_listener idle_listener;
92 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040093 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020094 struct wl_listener show_input_panel_listener;
95 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020096 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020097
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050098 struct weston_layer fullscreen_layer;
99 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500100 struct weston_layer background_layer;
101 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200102 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500103
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400104 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300105 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400106
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200107 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200109 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200110 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200111
112 unsigned deathcount;
113 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200114 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200115
116 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200117 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200118 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200119
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200120 struct {
121 struct weston_surface *surface;
122 pixman_box32_t cursor_rectangle;
123 } text_input;
124
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400125 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500126 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100127
Pekka Paalanen77346a62011-11-30 16:26:35 +0200128 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200129 struct wl_array array;
130 unsigned int current;
131 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200132
Jonas Ådahle9d22502012-08-29 22:13:01 +0200133 struct wl_list client_list;
134
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200135 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200136 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200137 int anim_dir;
138 uint32_t anim_timestamp;
139 double anim_current;
140 struct workspace *anim_from;
141 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200142 } workspaces;
143
144 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200145 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200146 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200147 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200149 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200150 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500151
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200152 struct {
153 struct wl_resource *binding;
154 struct wl_list surfaces;
155 } input_panel;
156
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200157 struct {
158 struct weston_surface *surface;
159 struct weston_surface_animation *animation;
160 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300161 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200162 } fade;
163
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300164 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800165 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400166};
167
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500168enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200169 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500170 SHELL_SURFACE_TOPLEVEL,
171 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500172 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800173 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300174 SHELL_SURFACE_POPUP,
175 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200176};
177
Scott Moreauff1db4a2012-04-17 19:06:18 -0600178struct ping_timer {
179 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600180 uint32_t serial;
181};
182
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200183struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200184 struct wl_resource resource;
185
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500186 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200187 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400188 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300189 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200190
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400191 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400192 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500193 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800194 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800195 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600196 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100197
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500198 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200199 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500200 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200201 } rotation;
202
203 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100204 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500205 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100206 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400207 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500208 } popup;
209
Alex Wu4539b082012-03-01 12:57:46 +0800210 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300211 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400212 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300213 } transient;
214
215 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800216 enum wl_shell_surface_fullscreen_method type;
217 struct weston_transform transform; /* matrix from x, y */
218 uint32_t framerate;
219 struct weston_surface *black_surface;
220 } fullscreen;
221
Scott Moreauff1db4a2012-04-17 19:06:18 -0600222 struct ping_timer *ping_timer;
223
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200224 struct weston_transform workspace_transform;
225
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500226 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500227 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100228 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400229
230 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200231};
232
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300233struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400234 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235 struct shell_surface *shsurf;
236 struct wl_listener shsurf_destroy_listener;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400237 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300238};
239
240struct weston_move_grab {
241 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100242 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500243};
244
Pekka Paalanen460099f2012-01-20 16:48:25 +0200245struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300246 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500247 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200248 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200249 float x;
250 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200251 } center;
252};
253
Giulio Camuffo5085a752013-03-25 21:42:45 +0100254struct shell_seat {
255 struct weston_seat *seat;
256 struct wl_listener seat_destroy_listener;
257
258 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400259 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100260 struct wl_list surfaces_list;
261 struct wl_client *client;
262 int32_t initial_up;
263 } popup_grab;
264};
265
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400266static void
267activate(struct desktop_shell *shell, struct weston_surface *es,
268 struct weston_seat *seat);
269
270static struct workspace *
271get_current_workspace(struct desktop_shell *shell);
272
Alex Wubd3354b2012-04-17 17:20:49 +0800273static struct shell_surface *
274get_shell_surface(struct weston_surface *surface);
275
276static struct desktop_shell *
277shell_surface_get_shell(struct shell_surface *shsurf);
278
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500279static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400280surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500281
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300282static void
283shell_fade_startup(struct desktop_shell *shell);
284
Alex Wubd3354b2012-04-17 17:20:49 +0800285static bool
286shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
287{
288 struct desktop_shell *shell;
289 struct weston_surface *top_fs_es;
290
291 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100292
Alex Wubd3354b2012-04-17 17:20:49 +0800293 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
294 return false;
295
296 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100297 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800298 layer_link);
299 return (shsurf == get_shell_surface(top_fs_es));
300}
301
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500302static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400303destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300304{
305 struct shell_grab *grab;
306
307 grab = container_of(listener, struct shell_grab,
308 shsurf_destroy_listener);
309
310 grab->shsurf = NULL;
311}
312
313static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400314popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400315
316static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300317shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400318 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 struct desktop_shell *shell = shsurf->shell;
324
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400325 popup_grab_end(pointer);
326
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327 grab->grab.interface = interface;
328 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400329 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
330 wl_signal_add(&shsurf->resource.destroy_signal,
331 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300333 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300334
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400335 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300336 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400337 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400338 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300339}
340
341static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300342shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300343{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400344 if (grab->shsurf)
345 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300346
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400347 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300348}
349
350static void
Alex Wu4539b082012-03-01 12:57:46 +0800351center_on_output(struct weston_surface *surface,
352 struct weston_output *output);
353
Daniel Stone496ca172012-05-30 16:31:42 +0100354static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300355get_modifier(char *modifier)
356{
357 if (!modifier)
358 return MODIFIER_SUPER;
359
360 if (!strcmp("ctrl", modifier))
361 return MODIFIER_CTRL;
362 else if (!strcmp("alt", modifier))
363 return MODIFIER_ALT;
364 else if (!strcmp("super", modifier))
365 return MODIFIER_SUPER;
366 else
367 return MODIFIER_SUPER;
368}
369
Juan Zhaoe10d2792012-04-25 19:09:52 +0800370static enum animation_type
371get_animation_type(char *animation)
372{
373 if (!animation)
374 return ANIMATION_NONE;
375
376 if (!strcmp("zoom", animation))
377 return ANIMATION_ZOOM;
378 else if (!strcmp("fade", animation))
379 return ANIMATION_FADE;
380 else
381 return ANIMATION_NONE;
382}
383
Alex Wu4539b082012-03-01 12:57:46 +0800384static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400385shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200386{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400387 struct weston_config_section *section;
388 int duration;
389 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200390
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400391 section = weston_config_get_section(shell->compositor->config,
392 "screensaver", NULL, NULL);
393 weston_config_section_get_string(section,
394 "path", &shell->screensaver.path, NULL);
395 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200396 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400397
398 section = weston_config_get_section(shell->compositor->config,
399 "shell", NULL, NULL);
400 weston_config_section_get_string(section,
401 "binding-modifier", &s, "super");
402 shell->binding_modifier = get_modifier(s);
403 weston_config_section_get_string(section, "animation", &s, "none");
404 shell->win_animation_type = get_animation_type(s);
405 weston_config_section_get_uint(section, "num-workspaces",
406 &shell->workspaces.num,
407 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200408}
409
410static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200411focus_state_destroy(struct focus_state *state)
412{
413 wl_list_remove(&state->seat_destroy_listener.link);
414 wl_list_remove(&state->surface_destroy_listener.link);
415 free(state);
416}
417
418static void
419focus_state_seat_destroy(struct wl_listener *listener, void *data)
420{
421 struct focus_state *state = container_of(listener,
422 struct focus_state,
423 seat_destroy_listener);
424
425 wl_list_remove(&state->link);
426 focus_state_destroy(state);
427}
428
429static void
430focus_state_surface_destroy(struct wl_listener *listener, void *data)
431{
432 struct focus_state *state = container_of(listener,
433 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400434 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400435 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300436 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400437 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200438
Pekka Paalanen01388e22013-04-25 13:57:44 +0300439 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
440
Kristian Høgsberge3778222012-07-31 17:29:30 -0400441 next = NULL;
442 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300443 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400444 continue;
445
446 next = surface;
447 break;
448 }
449
Pekka Paalanen01388e22013-04-25 13:57:44 +0300450 /* if the focus was a sub-surface, activate its main surface */
451 if (main_surface != state->keyboard_focus)
452 next = main_surface;
453
Kristian Høgsberge3778222012-07-31 17:29:30 -0400454 if (next) {
455 shell = state->seat->compositor->shell_interface.shell;
456 activate(shell, next, state->seat);
457 } else {
458 wl_list_remove(&state->link);
459 focus_state_destroy(state);
460 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200461}
462
463static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400464focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200465{
Jonas Ådahl04769742012-06-13 00:01:24 +0200466 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200467
468 state = malloc(sizeof *state);
469 if (state == NULL)
470 return NULL;
471
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400472 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200473 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400474 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200475
476 state->seat_destroy_listener.notify = focus_state_seat_destroy;
477 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400478 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200479 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400480 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200481
482 return state;
483}
484
Jonas Ådahl8538b222012-08-29 22:13:03 +0200485static struct focus_state *
486ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
487{
488 struct workspace *ws = get_current_workspace(shell);
489 struct focus_state *state;
490
491 wl_list_for_each(state, &ws->focus_list, link)
492 if (state->seat == seat)
493 break;
494
495 if (&state->link == &ws->focus_list)
496 state = focus_state_create(seat, ws);
497
498 return state;
499}
500
Jonas Ådahl04769742012-06-13 00:01:24 +0200501static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400502restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200503{
504 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400505 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200506
507 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400508 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200509
Kristian Høgsberge3148752013-05-06 23:19:49 -0400510 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200511 }
512}
513
514static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200515replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
516 struct weston_seat *seat)
517{
518 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400519 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200520
521 wl_list_for_each(state, &ws->focus_list, link) {
522 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400523 surface = seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200524 state->keyboard_focus =
525 (struct weston_surface *) surface;
526 return;
527 }
528 }
529}
530
531static void
532drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
533 struct weston_surface *surface)
534{
535 struct focus_state *state;
536
537 wl_list_for_each(state, &ws->focus_list, link)
538 if (state->keyboard_focus == surface)
539 state->keyboard_focus = NULL;
540}
541
542static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200543workspace_destroy(struct workspace *ws)
544{
Jonas Ådahl04769742012-06-13 00:01:24 +0200545 struct focus_state *state, *next;
546
547 wl_list_for_each_safe(state, next, &ws->focus_list, link)
548 focus_state_destroy(state);
549
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200550 free(ws);
551}
552
Jonas Ådahl04769742012-06-13 00:01:24 +0200553static void
554seat_destroyed(struct wl_listener *listener, void *data)
555{
556 struct weston_seat *seat = data;
557 struct focus_state *state, *next;
558 struct workspace *ws = container_of(listener,
559 struct workspace,
560 seat_destroyed_listener);
561
562 wl_list_for_each_safe(state, next, &ws->focus_list, link)
563 if (state->seat == seat)
564 wl_list_remove(&state->link);
565}
566
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200567static struct workspace *
568workspace_create(void)
569{
570 struct workspace *ws = malloc(sizeof *ws);
571 if (ws == NULL)
572 return NULL;
573
574 weston_layer_init(&ws->layer, NULL);
575
Jonas Ådahl04769742012-06-13 00:01:24 +0200576 wl_list_init(&ws->focus_list);
577 wl_list_init(&ws->seat_destroyed_listener.link);
578 ws->seat_destroyed_listener.notify = seat_destroyed;
579
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200580 return ws;
581}
582
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200583static int
584workspace_is_empty(struct workspace *ws)
585{
586 return wl_list_empty(&ws->layer.surface_list);
587}
588
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200589static struct workspace *
590get_workspace(struct desktop_shell *shell, unsigned int index)
591{
592 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200593 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200594 pws += index;
595 return *pws;
596}
597
598static struct workspace *
599get_current_workspace(struct desktop_shell *shell)
600{
601 return get_workspace(shell, shell->workspaces.current);
602}
603
604static void
605activate_workspace(struct desktop_shell *shell, unsigned int index)
606{
607 struct workspace *ws;
608
609 ws = get_workspace(shell, index);
610 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
611
612 shell->workspaces.current = index;
613}
614
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200615static unsigned int
616get_output_height(struct weston_output *output)
617{
618 return abs(output->region.extents.y1 - output->region.extents.y2);
619}
620
621static void
622surface_translate(struct weston_surface *surface, double d)
623{
624 struct shell_surface *shsurf = get_shell_surface(surface);
625 struct weston_transform *transform;
626
627 transform = &shsurf->workspace_transform;
628 if (wl_list_empty(&transform->link))
629 wl_list_insert(surface->geometry.transformation_list.prev,
630 &shsurf->workspace_transform.link);
631
632 weston_matrix_init(&shsurf->workspace_transform.matrix);
633 weston_matrix_translate(&shsurf->workspace_transform.matrix,
634 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200635 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200636}
637
638static void
639workspace_translate_out(struct workspace *ws, double fraction)
640{
641 struct weston_surface *surface;
642 unsigned int height;
643 double d;
644
645 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
646 height = get_output_height(surface->output);
647 d = height * fraction;
648
649 surface_translate(surface, d);
650 }
651}
652
653static void
654workspace_translate_in(struct workspace *ws, double fraction)
655{
656 struct weston_surface *surface;
657 unsigned int height;
658 double d;
659
660 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
661 height = get_output_height(surface->output);
662
663 if (fraction > 0)
664 d = -(height - height * fraction);
665 else
666 d = height + height * fraction;
667
668 surface_translate(surface, d);
669 }
670}
671
672static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200673broadcast_current_workspace_state(struct desktop_shell *shell)
674{
675 struct wl_resource *resource;
676
677 wl_list_for_each(resource, &shell->workspaces.client_list, link)
678 workspace_manager_send_state(resource,
679 shell->workspaces.current,
680 shell->workspaces.num);
681}
682
683static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200684reverse_workspace_change_animation(struct desktop_shell *shell,
685 unsigned int index,
686 struct workspace *from,
687 struct workspace *to)
688{
689 shell->workspaces.current = index;
690
691 shell->workspaces.anim_to = to;
692 shell->workspaces.anim_from = from;
693 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
694 shell->workspaces.anim_timestamp = 0;
695
Scott Moreau4272e632012-08-13 09:58:41 -0600696 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200697}
698
699static void
700workspace_deactivate_transforms(struct workspace *ws)
701{
702 struct weston_surface *surface;
703 struct shell_surface *shsurf;
704
705 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
706 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200707 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
708 wl_list_remove(&shsurf->workspace_transform.link);
709 wl_list_init(&shsurf->workspace_transform.link);
710 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200711 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200712 }
713}
714
715static void
716finish_workspace_change_animation(struct desktop_shell *shell,
717 struct workspace *from,
718 struct workspace *to)
719{
Scott Moreau4272e632012-08-13 09:58:41 -0600720 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200721
722 wl_list_remove(&shell->workspaces.animation.link);
723 workspace_deactivate_transforms(from);
724 workspace_deactivate_transforms(to);
725 shell->workspaces.anim_to = NULL;
726
727 wl_list_remove(&shell->workspaces.anim_from->layer.link);
728}
729
730static void
731animate_workspace_change_frame(struct weston_animation *animation,
732 struct weston_output *output, uint32_t msecs)
733{
734 struct desktop_shell *shell =
735 container_of(animation, struct desktop_shell,
736 workspaces.animation);
737 struct workspace *from = shell->workspaces.anim_from;
738 struct workspace *to = shell->workspaces.anim_to;
739 uint32_t t;
740 double x, y;
741
742 if (workspace_is_empty(from) && workspace_is_empty(to)) {
743 finish_workspace_change_animation(shell, from, to);
744 return;
745 }
746
747 if (shell->workspaces.anim_timestamp == 0) {
748 if (shell->workspaces.anim_current == 0.0)
749 shell->workspaces.anim_timestamp = msecs;
750 else
751 shell->workspaces.anim_timestamp =
752 msecs -
753 /* Invers of movement function 'y' below. */
754 (asin(1.0 - shell->workspaces.anim_current) *
755 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
756 M_2_PI);
757 }
758
759 t = msecs - shell->workspaces.anim_timestamp;
760
761 /*
762 * x = [0, π/2]
763 * y(x) = sin(x)
764 */
765 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
766 y = sin(x);
767
768 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600769 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200770
771 workspace_translate_out(from, shell->workspaces.anim_dir * y);
772 workspace_translate_in(to, shell->workspaces.anim_dir * y);
773 shell->workspaces.anim_current = y;
774
Scott Moreau4272e632012-08-13 09:58:41 -0600775 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200776 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200777 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200778 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200779}
780
781static void
782animate_workspace_change(struct desktop_shell *shell,
783 unsigned int index,
784 struct workspace *from,
785 struct workspace *to)
786{
787 struct weston_output *output;
788
789 int dir;
790
791 if (index > shell->workspaces.current)
792 dir = -1;
793 else
794 dir = 1;
795
796 shell->workspaces.current = index;
797
798 shell->workspaces.anim_dir = dir;
799 shell->workspaces.anim_from = from;
800 shell->workspaces.anim_to = to;
801 shell->workspaces.anim_current = 0.0;
802 shell->workspaces.anim_timestamp = 0;
803
804 output = container_of(shell->compositor->output_list.next,
805 struct weston_output, link);
806 wl_list_insert(&output->animation_list,
807 &shell->workspaces.animation.link);
808
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200809 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200810
811 workspace_translate_in(to, 0);
812
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400813 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200814
Scott Moreau4272e632012-08-13 09:58:41 -0600815 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200816}
817
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200818static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200819update_workspace(struct desktop_shell *shell, unsigned int index,
820 struct workspace *from, struct workspace *to)
821{
822 shell->workspaces.current = index;
823 wl_list_insert(&from->layer.link, &to->layer.link);
824 wl_list_remove(&from->layer.link);
825}
826
827static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828change_workspace(struct desktop_shell *shell, unsigned int index)
829{
830 struct workspace *from;
831 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200832
833 if (index == shell->workspaces.current)
834 return;
835
836 /* Don't change workspace when there is any fullscreen surfaces. */
837 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
838 return;
839
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200840 from = get_current_workspace(shell);
841 to = get_workspace(shell, index);
842
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200843 if (shell->workspaces.anim_from == to &&
844 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200845 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200846 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200847 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200848 return;
849 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200850
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851 if (shell->workspaces.anim_to != NULL)
852 finish_workspace_change_animation(shell,
853 shell->workspaces.anim_from,
854 shell->workspaces.anim_to);
855
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200856 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200857
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200858 if (workspace_is_empty(to) && workspace_is_empty(from))
859 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200860 else
861 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200862
863 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200864}
865
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200866static bool
867workspace_has_only(struct workspace *ws, struct weston_surface *surface)
868{
869 struct wl_list *list = &ws->layer.surface_list;
870 struct wl_list *e;
871
872 if (wl_list_empty(list))
873 return false;
874
875 e = list->next;
876
877 if (e->next != list)
878 return false;
879
880 return container_of(e, struct weston_surface, layer_link) == surface;
881}
882
883static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200884move_surface_to_workspace(struct desktop_shell *shell,
885 struct weston_surface *surface,
886 uint32_t workspace)
887{
888 struct workspace *from;
889 struct workspace *to;
890 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300891 struct weston_surface *focus;
892
893 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200894
895 if (workspace == shell->workspaces.current)
896 return;
897
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200898 if (workspace >= shell->workspaces.num)
899 workspace = shell->workspaces.num - 1;
900
Jonas Ådahle9d22502012-08-29 22:13:01 +0200901 from = get_current_workspace(shell);
902 to = get_workspace(shell, workspace);
903
904 wl_list_remove(&surface->layer_link);
905 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
906
907 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300908 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
909 if (!seat->keyboard)
910 continue;
911
912 focus = weston_surface_get_main_surface(seat->keyboard->focus);
913 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400914 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300915 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200916
917 weston_surface_damage_below(surface);
918}
919
920static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200921take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400922 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200923 unsigned int index)
924{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300925 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200926 struct shell_surface *shsurf;
927 struct workspace *from;
928 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200929 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200930
Pekka Paalanen01388e22013-04-25 13:57:44 +0300931 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 if (surface == NULL ||
933 index == shell->workspaces.current)
934 return;
935
936 from = get_current_workspace(shell);
937 to = get_workspace(shell, index);
938
939 wl_list_remove(&surface->layer_link);
940 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
941
Jonas Ådahle9d22502012-08-29 22:13:01 +0200942 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200943 drop_focus_state(shell, from, surface);
944
945 if (shell->workspaces.anim_from == to &&
946 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200947 wl_list_remove(&to->layer.link);
948 wl_list_insert(from->layer.link.prev, &to->layer.link);
949
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200950 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200951 broadcast_current_workspace_state(shell);
952
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200953 return;
954 }
955
956 if (shell->workspaces.anim_to != NULL)
957 finish_workspace_change_animation(shell,
958 shell->workspaces.anim_from,
959 shell->workspaces.anim_to);
960
961 if (workspace_is_empty(from) &&
962 workspace_has_only(to, surface))
963 update_workspace(shell, index, from, to);
964 else {
965 shsurf = get_shell_surface(surface);
966 if (wl_list_empty(&shsurf->workspace_transform.link))
967 wl_list_insert(&shell->workspaces.anim_sticky_list,
968 &shsurf->workspace_transform.link);
969
970 animate_workspace_change(shell, index, from, to);
971 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200972
973 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200974
975 state = ensure_focus_state(shell, seat);
976 if (state != NULL)
977 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200978}
979
980static void
981workspace_manager_move_surface(struct wl_client *client,
982 struct wl_resource *resource,
983 struct wl_resource *surface_resource,
984 uint32_t workspace)
985{
986 struct desktop_shell *shell = resource->data;
987 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500988 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300989 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200990
Pekka Paalanen01388e22013-04-25 13:57:44 +0300991 main_surface = weston_surface_get_main_surface(surface);
992 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200993}
994
995static const struct workspace_manager_interface workspace_manager_implementation = {
996 workspace_manager_move_surface,
997};
998
999static void
1000unbind_resource(struct wl_resource *resource)
1001{
1002 wl_list_remove(&resource->link);
1003 free(resource);
1004}
1005
1006static void
1007bind_workspace_manager(struct wl_client *client,
1008 void *data, uint32_t version, uint32_t id)
1009{
1010 struct desktop_shell *shell = data;
1011 struct wl_resource *resource;
1012
1013 resource = wl_client_add_object(client, &workspace_manager_interface,
1014 &workspace_manager_implementation,
1015 id, shell);
1016
1017 if (resource == NULL) {
1018 weston_log("couldn't add workspace manager object");
1019 return;
1020 }
1021
1022 resource->destroy = unbind_resource;
1023 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1024
1025 workspace_manager_send_state(resource,
1026 shell->workspaces.current,
1027 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001028}
1029
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001030static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001031noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001032{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001033}
1034
1035static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001036move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001037{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001038 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001039 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001040 struct shell_surface *shsurf = move->base.shsurf;
1041 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001042 int dx = wl_fixed_to_int(pointer->x + move->dx);
1043 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001044
1045 if (!shsurf)
1046 return;
1047
1048 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001049
Daniel Stone103db7f2012-05-08 17:17:55 +01001050 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001051 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001052
1053 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001054}
1055
1056static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001057move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001058 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001059{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001060 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1061 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001062 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001063 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001064
Daniel Stone4dbadb12012-05-30 16:31:51 +01001065 if (pointer->button_count == 0 &&
1066 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001067 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001068 free(grab);
1069 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001070}
1071
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001072static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001073 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001074 move_grab_motion,
1075 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001076};
1077
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001078static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001079surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001080{
1081 struct weston_move_grab *move;
1082
1083 if (!shsurf)
1084 return -1;
1085
1086 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1087 return 0;
1088
1089 move = malloc(sizeof *move);
1090 if (!move)
1091 return -1;
1092
1093 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001094 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001095 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001096 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001097
1098 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001099 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001100
1101 return 0;
1102}
1103
1104static void
1105shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1106 struct wl_resource *seat_resource, uint32_t serial)
1107{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001108 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001109 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001110 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001111
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001112 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001113 if (seat->pointer->button_count == 0 ||
1114 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001115 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001116 return;
1117
Kristian Høgsberge3148752013-05-06 23:19:49 -04001118 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001119 wl_resource_post_no_memory(resource);
1120}
1121
1122struct weston_resize_grab {
1123 struct shell_grab base;
1124 uint32_t edges;
1125 int32_t width, height;
1126};
1127
1128static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001129resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001130{
1131 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001132 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001133 struct shell_surface *shsurf = resize->base.shsurf;
1134 int32_t width, height;
1135 wl_fixed_t from_x, from_y;
1136 wl_fixed_t to_x, to_y;
1137
1138 if (!shsurf)
1139 return;
1140
1141 weston_surface_from_global_fixed(shsurf->surface,
1142 pointer->grab_x, pointer->grab_y,
1143 &from_x, &from_y);
1144 weston_surface_from_global_fixed(shsurf->surface,
1145 pointer->x, pointer->y, &to_x, &to_y);
1146
1147 width = resize->width;
1148 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1149 width += wl_fixed_to_int(from_x - to_x);
1150 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1151 width += wl_fixed_to_int(to_x - from_x);
1152 }
1153
1154 height = resize->height;
1155 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1156 height += wl_fixed_to_int(from_y - to_y);
1157 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1158 height += wl_fixed_to_int(to_y - from_y);
1159 }
1160
1161 shsurf->client->send_configure(shsurf->surface,
1162 resize->edges, width, height);
1163}
1164
1165static void
1166send_configure(struct weston_surface *surface,
1167 uint32_t edges, int32_t width, int32_t height)
1168{
1169 struct shell_surface *shsurf = get_shell_surface(surface);
1170
1171 wl_shell_surface_send_configure(&shsurf->resource,
1172 edges, width, height);
1173}
1174
1175static const struct weston_shell_client shell_client = {
1176 send_configure
1177};
1178
1179static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001180resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001181 uint32_t time, uint32_t button, uint32_t state_w)
1182{
1183 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001184 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001185 enum wl_pointer_button_state state = state_w;
1186
1187 if (pointer->button_count == 0 &&
1188 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1189 shell_grab_end(&resize->base);
1190 free(grab);
1191 }
1192}
1193
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001194static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001195 noop_grab_focus,
1196 resize_grab_motion,
1197 resize_grab_button,
1198};
1199
Giulio Camuffob8366642013-04-25 13:57:46 +03001200/*
1201 * Returns the bounding box of a surface and all its sub-surfaces,
1202 * in the surface coordinates system. */
1203static void
1204surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1205 int32_t *y, int32_t *w, int32_t *h) {
1206 pixman_region32_t region;
1207 pixman_box32_t *box;
1208 struct weston_subsurface *subsurface;
1209
1210 pixman_region32_init_rect(&region, 0, 0,
1211 surface->geometry.width,
1212 surface->geometry.height);
1213
1214 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1215 pixman_region32_union_rect(&region, &region,
1216 subsurface->position.x,
1217 subsurface->position.y,
1218 subsurface->surface->geometry.width,
1219 subsurface->surface->geometry.height);
1220 }
1221
1222 box = pixman_region32_extents(&region);
1223 if (x)
1224 *x = box->x1;
1225 if (y)
1226 *y = box->y1;
1227 if (w)
1228 *w = box->x2 - box->x1;
1229 if (h)
1230 *h = box->y2 - box->y1;
1231
1232 pixman_region32_fini(&region);
1233}
1234
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001235static int
1236surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001237 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001238{
1239 struct weston_resize_grab *resize;
1240
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001241 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1242 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001243 return 0;
1244
1245 if (edges == 0 || edges > 15 ||
1246 (edges & 3) == 3 || (edges & 12) == 12)
1247 return 0;
1248
1249 resize = malloc(sizeof *resize);
1250 if (!resize)
1251 return -1;
1252
1253 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001254 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1255 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001256
1257 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001258 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001259
1260 return 0;
1261}
1262
1263static void
1264shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1265 struct wl_resource *seat_resource, uint32_t serial,
1266 uint32_t edges)
1267{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001268 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001269 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001270 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001271
1272 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1273 return;
1274
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001275 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001276 if (seat->pointer->button_count == 0 ||
1277 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001278 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001279 return;
1280
Kristian Høgsberge3148752013-05-06 23:19:49 -04001281 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001282 wl_resource_post_no_memory(resource);
1283}
1284
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001285static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001286busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001287{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001288 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001289 struct weston_pointer *pointer = base->pointer;
1290 struct weston_surface *surface;
1291 wl_fixed_t sx, sy;
1292
1293 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1294 pointer->x, pointer->y,
1295 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001296
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001297 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001298 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001299 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001300 }
1301}
1302
1303static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001304busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001305{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001306}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001307
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001308static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001309busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001310 uint32_t time, uint32_t button, uint32_t state)
1311{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001312 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001313 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001314 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001315
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001316 if (shsurf && button == BTN_LEFT && state) {
1317 activate(shsurf->shell, shsurf->surface, seat);
1318 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001319 } else if (shsurf && button == BTN_RIGHT && state) {
1320 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001321 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001322 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001323}
1324
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001325static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001326 busy_cursor_grab_focus,
1327 busy_cursor_grab_motion,
1328 busy_cursor_grab_button,
1329};
1330
1331static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001332set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001333{
1334 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001335
1336 grab = malloc(sizeof *grab);
1337 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001338 return;
1339
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001340 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1341 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001342}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001343
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001344static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001345end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001346{
1347 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1348
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001349 if (grab->grab.interface == &busy_cursor_grab_interface &&
1350 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001351 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001352 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001353 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001354}
1355
Scott Moreau9521d5e2012-04-19 13:06:17 -06001356static void
1357ping_timer_destroy(struct shell_surface *shsurf)
1358{
1359 if (!shsurf || !shsurf->ping_timer)
1360 return;
1361
1362 if (shsurf->ping_timer->source)
1363 wl_event_source_remove(shsurf->ping_timer->source);
1364
1365 free(shsurf->ping_timer);
1366 shsurf->ping_timer = NULL;
1367}
1368
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001369static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001370ping_timeout_handler(void *data)
1371{
1372 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001373 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001374
Scott Moreau9521d5e2012-04-19 13:06:17 -06001375 /* Client is not responding */
1376 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001377
1378 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001379 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001380 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001381
1382 return 1;
1383}
1384
1385static void
1386ping_handler(struct weston_surface *surface, uint32_t serial)
1387{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001388 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001389 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001390 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001391
1392 if (!shsurf)
1393 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001394 if (!shsurf->resource.client)
1395 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001396
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001397 if (shsurf->surface == shsurf->shell->grab_surface)
1398 return;
1399
Scott Moreauff1db4a2012-04-17 19:06:18 -06001400 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001401 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001402 if (!shsurf->ping_timer)
1403 return;
1404
1405 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001406 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1407 shsurf->ping_timer->source =
1408 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1409 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1410
1411 wl_shell_surface_send_ping(&shsurf->resource, serial);
1412 }
1413}
1414
1415static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001416handle_pointer_focus(struct wl_listener *listener, void *data)
1417{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001418 struct weston_pointer *pointer = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001419 struct weston_surface *surface =
1420 (struct weston_surface *) pointer->focus;
1421 struct weston_compositor *compositor;
1422 struct shell_surface *shsurf;
1423 uint32_t serial;
1424
1425 if (!surface)
1426 return;
1427
1428 compositor = surface->compositor;
1429 shsurf = get_shell_surface(surface);
1430
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001431 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001432 set_busy_cursor(shsurf, pointer);
1433 } else {
1434 serial = wl_display_next_serial(compositor->wl_display);
1435 ping_handler(surface, serial);
1436 }
1437}
1438
1439static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001440create_pointer_focus_listener(struct weston_seat *seat)
1441{
1442 struct wl_listener *listener;
1443
Kristian Høgsberge3148752013-05-06 23:19:49 -04001444 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001445 return;
1446
1447 listener = malloc(sizeof *listener);
1448 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001449 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001450}
1451
1452static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001453shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1454 uint32_t serial)
1455{
1456 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001457 struct weston_seat *seat;
1458 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001459
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001460 if (shsurf->ping_timer == NULL)
1461 /* Just ignore unsolicited pong. */
1462 return;
1463
Scott Moreauff1db4a2012-04-17 19:06:18 -06001464 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001465 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001466 wl_list_for_each(seat, &ec->seat_list, link) {
1467 if(seat->pointer)
1468 end_busy_cursor(shsurf, seat->pointer);
1469 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001470 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001471 }
1472}
1473
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001474static void
1475shell_surface_set_title(struct wl_client *client,
1476 struct wl_resource *resource, const char *title)
1477{
1478 struct shell_surface *shsurf = resource->data;
1479
1480 free(shsurf->title);
1481 shsurf->title = strdup(title);
1482}
1483
1484static void
1485shell_surface_set_class(struct wl_client *client,
1486 struct wl_resource *resource, const char *class)
1487{
1488 struct shell_surface *shsurf = resource->data;
1489
1490 free(shsurf->class);
1491 shsurf->class = strdup(class);
1492}
1493
Juan Zhao96879df2012-02-07 08:45:41 +08001494static struct weston_output *
1495get_default_output(struct weston_compositor *compositor)
1496{
1497 return container_of(compositor->output_list.next,
1498 struct weston_output, link);
1499}
1500
Alex Wu4539b082012-03-01 12:57:46 +08001501static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001502restore_output_mode(struct weston_output *output)
1503{
1504 if (output->current != output->origin ||
1505 (int32_t)output->scale != output->origin_scale)
1506 weston_output_switch_mode(output,
1507 output->origin,
1508 output->origin_scale);
1509}
1510
1511static void
1512restore_all_output_modes(struct weston_compositor *compositor)
1513{
1514 struct weston_output *output;
1515
1516 wl_list_for_each(output, &compositor->output_list, link)
1517 restore_output_mode(output);
1518}
1519
1520static void
Alex Wu4539b082012-03-01 12:57:46 +08001521shell_unset_fullscreen(struct shell_surface *shsurf)
1522{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001523 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001524 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001525 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1526 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001527 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001528 }
Alex Wu4539b082012-03-01 12:57:46 +08001529 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1530 shsurf->fullscreen.framerate = 0;
1531 wl_list_remove(&shsurf->fullscreen.transform.link);
1532 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001533 if (shsurf->fullscreen.black_surface)
1534 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001535 shsurf->fullscreen.black_surface = NULL;
1536 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001537 weston_surface_set_position(shsurf->surface,
1538 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001539 if (shsurf->saved_rotation_valid) {
1540 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1541 &shsurf->rotation.transform.link);
1542 shsurf->saved_rotation_valid = false;
1543 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001544
1545 ws = get_current_workspace(shsurf->shell);
1546 wl_list_remove(&shsurf->surface->layer_link);
1547 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001548}
1549
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001550static void
1551shell_unset_maximized(struct shell_surface *shsurf)
1552{
1553 struct workspace *ws;
1554 /* undo all maximized things here */
1555 shsurf->output = get_default_output(shsurf->surface->compositor);
1556 weston_surface_set_position(shsurf->surface,
1557 shsurf->saved_x,
1558 shsurf->saved_y);
1559
1560 if (shsurf->saved_rotation_valid) {
1561 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1562 &shsurf->rotation.transform.link);
1563 shsurf->saved_rotation_valid = false;
1564 }
1565
1566 ws = get_current_workspace(shsurf->shell);
1567 wl_list_remove(&shsurf->surface->layer_link);
1568 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1569}
1570
Pekka Paalanen98262232011-12-01 10:42:22 +02001571static int
1572reset_shell_surface_type(struct shell_surface *surface)
1573{
1574 switch (surface->type) {
1575 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001576 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001577 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001578 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001579 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001580 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001581 case SHELL_SURFACE_NONE:
1582 case SHELL_SURFACE_TOPLEVEL:
1583 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001584 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001585 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001586 break;
1587 }
1588
1589 surface->type = SHELL_SURFACE_NONE;
1590 return 0;
1591}
1592
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001593static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001594set_surface_type(struct shell_surface *shsurf)
1595{
1596 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001597 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001598
1599 reset_shell_surface_type(shsurf);
1600
1601 shsurf->type = shsurf->next_type;
1602 shsurf->next_type = SHELL_SURFACE_NONE;
1603
1604 switch (shsurf->type) {
1605 case SHELL_SURFACE_TOPLEVEL:
1606 break;
1607 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001608 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001609 pes->geometry.x + shsurf->transient.x,
1610 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001611 break;
1612
1613 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001614 case SHELL_SURFACE_FULLSCREEN:
1615 shsurf->saved_x = surface->geometry.x;
1616 shsurf->saved_y = surface->geometry.y;
1617 shsurf->saved_position_valid = true;
1618
1619 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1620 wl_list_remove(&shsurf->rotation.transform.link);
1621 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001622 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001623 shsurf->saved_rotation_valid = true;
1624 }
1625 break;
1626
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001627 case SHELL_SURFACE_XWAYLAND:
1628 weston_surface_set_position(surface, shsurf->transient.x,
1629 shsurf->transient.y);
1630 break;
1631
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001632 default:
1633 break;
1634 }
1635}
1636
1637static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001638set_toplevel(struct shell_surface *shsurf)
1639{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001640 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001641}
1642
1643static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001644shell_surface_set_toplevel(struct wl_client *client,
1645 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001646{
Pekka Paalanen98262232011-12-01 10:42:22 +02001647 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001648
Tiago Vignattibc052c92012-04-19 16:18:18 +03001649 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001650}
1651
1652static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001653set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001654 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001655{
1656 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001657 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001658 shsurf->transient.x = x;
1659 shsurf->transient.y = y;
1660 shsurf->transient.flags = flags;
1661 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1662}
1663
1664static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001665shell_surface_set_transient(struct wl_client *client,
1666 struct wl_resource *resource,
1667 struct wl_resource *parent_resource,
1668 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001669{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001670 struct shell_surface *shsurf = resource->data;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001671 struct weston_surface *parent =
1672 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001673
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001674 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001675}
1676
Tiago Vignattibe143262012-04-16 17:31:41 +03001677static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001678shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001679{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001680 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001681}
1682
1683static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001684get_output_panel_height(struct desktop_shell *shell,
1685 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001686{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001687 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001688 int panel_height = 0;
1689
1690 if (!output)
1691 return 0;
1692
Juan Zhao4ab94682012-07-09 22:24:09 -07001693 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001694 if (surface->output == output) {
1695 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001696 break;
1697 }
1698 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001699
Juan Zhao96879df2012-02-07 08:45:41 +08001700 return panel_height;
1701}
1702
1703static void
1704shell_surface_set_maximized(struct wl_client *client,
1705 struct wl_resource *resource,
1706 struct wl_resource *output_resource )
1707{
1708 struct shell_surface *shsurf = resource->data;
1709 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001710 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001711 uint32_t edges = 0, panel_height = 0;
1712
1713 /* get the default output, if the client set it as NULL
1714 check whether the ouput is available */
1715 if (output_resource)
1716 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001717 else if (es->output)
1718 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001719 else
1720 shsurf->output = get_default_output(es->compositor);
1721
Tiago Vignattibe143262012-04-16 17:31:41 +03001722 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001723 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001724 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001725
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001726 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001727 shsurf->output->width,
1728 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001729
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001730 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001731}
1732
Alex Wu21858432012-04-01 20:13:08 +08001733static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001734black_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 +08001735
Alex Wu4539b082012-03-01 12:57:46 +08001736static struct weston_surface *
1737create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001738 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001739 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001740{
1741 struct weston_surface *surface = NULL;
1742
1743 surface = weston_surface_create(ec);
1744 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001745 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001746 return NULL;
1747 }
1748
Alex Wu21858432012-04-01 20:13:08 +08001749 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001750 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001751 weston_surface_configure(surface, x, y, w, h);
1752 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001753 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001754 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001755 pixman_region32_fini(&surface->input);
1756 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001757
Alex Wu4539b082012-03-01 12:57:46 +08001758 return surface;
1759}
1760
1761/* Create black surface and append it to the associated fullscreen surface.
1762 * Handle size dismatch and positioning according to the method. */
1763static void
1764shell_configure_fullscreen(struct shell_surface *shsurf)
1765{
1766 struct weston_output *output = shsurf->fullscreen_output;
1767 struct weston_surface *surface = shsurf->surface;
1768 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001769 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001770 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001771
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001772 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1773 restore_output_mode(output);
1774
Alex Wu4539b082012-03-01 12:57:46 +08001775 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001776 shsurf->fullscreen.black_surface =
1777 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001778 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001779 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001780 output->width,
1781 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001782
1783 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1784 wl_list_insert(&surface->layer_link,
1785 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001786 shsurf->fullscreen.black_surface->output = output;
1787
Giulio Camuffob8366642013-04-25 13:57:46 +03001788 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1789 &surf_width, &surf_height);
1790
Alex Wu4539b082012-03-01 12:57:46 +08001791 switch (shsurf->fullscreen.type) {
1792 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001793 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001794 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001795 break;
1796 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001797 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001798 if (output->width == surf_width &&
1799 output->height == surf_height) {
1800 weston_surface_set_position(surface, output->x - surf_x,
1801 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001802 break;
1803 }
1804
Alex Wu4539b082012-03-01 12:57:46 +08001805 matrix = &shsurf->fullscreen.transform.matrix;
1806 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001807
Scott Moreau1bad5db2012-08-18 01:04:05 -06001808 output_aspect = (float) output->width /
1809 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001810 surface_aspect = (float) surface->geometry.width /
1811 (float) surface->geometry.height;
1812 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001813 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001814 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001815 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001816 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001817 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001818
Alex Wu4539b082012-03-01 12:57:46 +08001819 weston_matrix_scale(matrix, scale, scale, 1);
1820 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001821 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001822 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001823 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1824 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001825 weston_surface_set_position(surface, x, y);
1826
Alex Wu4539b082012-03-01 12:57:46 +08001827 break;
1828 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001829 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001830 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001831 surf_width * surface->buffer_scale,
1832 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001833 shsurf->fullscreen.framerate};
1834
Alexander Larsson355748e2013-05-28 16:23:38 +02001835 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001836 weston_surface_set_position(surface,
1837 output->x - surf_x,
1838 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001839 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001840 output->x - surf_x,
1841 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001842 output->width,
1843 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001844 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001845 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001846 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001847 center_on_output(surface, output);
1848 }
Alex Wubd3354b2012-04-17 17:20:49 +08001849 }
Alex Wu4539b082012-03-01 12:57:46 +08001850 break;
1851 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001852 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001853 break;
1854 default:
1855 break;
1856 }
1857}
1858
1859/* make the fullscreen and black surface at the top */
1860static void
1861shell_stack_fullscreen(struct shell_surface *shsurf)
1862{
Alex Wubd3354b2012-04-17 17:20:49 +08001863 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001864 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001865 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001866
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001867 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001868 wl_list_insert(&shell->fullscreen_layer.surface_list,
1869 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001870 weston_surface_damage(surface);
1871
1872 if (!shsurf->fullscreen.black_surface)
1873 shsurf->fullscreen.black_surface =
1874 create_black_surface(surface->compositor,
1875 surface,
1876 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001877 output->width,
1878 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001879
1880 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001881 wl_list_insert(&surface->layer_link,
1882 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001883 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001884}
1885
1886static void
1887shell_map_fullscreen(struct shell_surface *shsurf)
1888{
Alex Wu4539b082012-03-01 12:57:46 +08001889 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001890 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001891}
1892
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001893static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001894set_fullscreen(struct shell_surface *shsurf,
1895 uint32_t method,
1896 uint32_t framerate,
1897 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001898{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001899 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001900
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001901 if (output)
1902 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001903 else if (es->output)
1904 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001905 else
1906 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001907
Alex Wu4539b082012-03-01 12:57:46 +08001908 shsurf->fullscreen_output = shsurf->output;
1909 shsurf->fullscreen.type = method;
1910 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001911 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001912
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001913 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001914 shsurf->output->width,
1915 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001916}
1917
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001918static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001919shell_surface_set_fullscreen(struct wl_client *client,
1920 struct wl_resource *resource,
1921 uint32_t method,
1922 uint32_t framerate,
1923 struct wl_resource *output_resource)
1924{
1925 struct shell_surface *shsurf = resource->data;
1926 struct weston_output *output;
1927
1928 if (output_resource)
1929 output = output_resource->data;
1930 else
1931 output = NULL;
1932
1933 set_fullscreen(shsurf, method, framerate, output);
1934}
1935
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001936static void
1937set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1938{
1939 /* XXX: using the same fields for transient type */
1940 shsurf->transient.x = x;
1941 shsurf->transient.y = y;
1942 shsurf->transient.flags = flags;
1943 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1944}
1945
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001946static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001947
1948static void
1949destroy_shell_seat(struct wl_listener *listener, void *data)
1950{
1951 struct shell_seat *shseat =
1952 container_of(listener,
1953 struct shell_seat, seat_destroy_listener);
1954 struct shell_surface *shsurf, *prev = NULL;
1955
1956 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001957 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001958 shseat->popup_grab.client = NULL;
1959
1960 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1961 shsurf->popup.shseat = NULL;
1962 if (prev) {
1963 wl_list_init(&prev->popup.grab_link);
1964 }
1965 prev = shsurf;
1966 }
1967 wl_list_init(&prev->popup.grab_link);
1968 }
1969
1970 wl_list_remove(&shseat->seat_destroy_listener.link);
1971 free(shseat);
1972}
1973
1974static struct shell_seat *
1975create_shell_seat(struct weston_seat *seat)
1976{
1977 struct shell_seat *shseat;
1978
1979 shseat = calloc(1, sizeof *shseat);
1980 if (!shseat) {
1981 weston_log("no memory to allocate shell seat\n");
1982 return NULL;
1983 }
1984
1985 shseat->seat = seat;
1986 wl_list_init(&shseat->popup_grab.surfaces_list);
1987
1988 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1989 wl_signal_add(&seat->destroy_signal,
1990 &shseat->seat_destroy_listener);
1991
1992 return shseat;
1993}
1994
1995static struct shell_seat *
1996get_shell_seat(struct weston_seat *seat)
1997{
1998 struct wl_listener *listener;
1999
2000 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2001 if (listener == NULL)
2002 return create_shell_seat(seat);
2003
2004 return container_of(listener,
2005 struct shell_seat, seat_destroy_listener);
2006}
2007
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002008static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002009popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002010{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002011 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002012 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002013 struct shell_seat *shseat =
2014 container_of(grab, struct shell_seat, popup_grab.grab);
2015 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002016 wl_fixed_t sx, sy;
2017
2018 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2019 pointer->x, pointer->y,
2020 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002021
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002022 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002023 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002024 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002025 weston_pointer_set_focus(pointer, NULL,
2026 wl_fixed_from_int(0),
2027 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002028 }
2029}
2030
2031static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002032popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002033{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002034 struct weston_pointer *pointer = grab->pointer;
2035 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002036
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002037 if (pointer->focus_resource) {
2038 weston_surface_from_global_fixed(pointer->focus,
2039 pointer->x, pointer->y,
2040 &sx, &sy);
2041 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2042 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002043}
2044
2045static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002046popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002047 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002048{
2049 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002050 struct shell_seat *shseat =
2051 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002052 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002053 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002054 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002055
Daniel Stone37816df2012-05-16 18:45:18 +01002056 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002057 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002058 display = wl_client_get_display(resource->client);
2059 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002060 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002061 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002062 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002063 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002064 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002065 }
2066
Daniel Stone4dbadb12012-05-30 16:31:51 +01002067 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002068 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002069}
2070
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002071static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002072 popup_grab_focus,
2073 popup_grab_motion,
2074 popup_grab_button,
2075};
2076
2077static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002078popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002079{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002080 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002081 struct shell_seat *shseat =
2082 container_of(grab, struct shell_seat, popup_grab.grab);
2083 struct shell_surface *shsurf;
2084 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002085
2086 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002087 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002088 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002089 shseat->popup_grab.grab.interface = NULL;
2090 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002091 /* Send the popup_done event to all the popups open */
2092 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2093 wl_shell_surface_send_popup_done(&shsurf->resource);
2094 shsurf->popup.shseat = NULL;
2095 if (prev) {
2096 wl_list_init(&prev->popup.grab_link);
2097 }
2098 prev = shsurf;
2099 }
2100 wl_list_init(&prev->popup.grab_link);
2101 wl_list_init(&shseat->popup_grab.surfaces_list);
2102 }
2103}
2104
2105static void
2106add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2107{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002108 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002109
2110 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002111 shseat->popup_grab.client = shsurf->resource.client;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002112 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002113 /* We must make sure here that this popup was opened after
2114 * a mouse press, and not just by moving around with other
2115 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002116 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002117 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002118
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002119 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002120 }
2121 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2122}
2123
2124static void
2125remove_popup_grab(struct shell_surface *shsurf)
2126{
2127 struct shell_seat *shseat = shsurf->popup.shseat;
2128
2129 wl_list_remove(&shsurf->popup.grab_link);
2130 wl_list_init(&shsurf->popup.grab_link);
2131 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002132 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002133 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002134 }
2135}
2136
2137static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002138shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002139{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002140 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002141 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002142 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002143
2144 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002145
Pekka Paalanen483243f2013-03-08 14:56:50 +02002146 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002147 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2148 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002149
Kristian Høgsberge3148752013-05-06 23:19:49 -04002150 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002151 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002152 } else {
2153 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002154 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002155 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002156}
2157
2158static void
2159shell_surface_set_popup(struct wl_client *client,
2160 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002161 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002162 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002163 struct wl_resource *parent_resource,
2164 int32_t x, int32_t y, uint32_t flags)
2165{
2166 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002167
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002168 shsurf->type = SHELL_SURFACE_POPUP;
2169 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002170 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002171 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002172 shsurf->popup.x = x;
2173 shsurf->popup.y = y;
2174}
2175
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002176static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002177 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002178 shell_surface_move,
2179 shell_surface_resize,
2180 shell_surface_set_toplevel,
2181 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002182 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002183 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002184 shell_surface_set_maximized,
2185 shell_surface_set_title,
2186 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002187};
2188
2189static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002190destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002191{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002192 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2193 remove_popup_grab(shsurf);
2194 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002195
Alex Wubd3354b2012-04-17 17:20:49 +08002196 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002197 shell_surface_is_top_fullscreen(shsurf))
2198 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002199
Alex Wuaa08e2d2012-03-05 11:01:40 +08002200 if (shsurf->fullscreen.black_surface)
2201 weston_surface_destroy(shsurf->fullscreen.black_surface);
2202
Alex Wubd3354b2012-04-17 17:20:49 +08002203 /* As destroy_resource() use wl_list_for_each_safe(),
2204 * we can always remove the listener.
2205 */
2206 wl_list_remove(&shsurf->surface_destroy_listener.link);
2207 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002208 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002209 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002210
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002211 wl_list_remove(&shsurf->link);
2212 free(shsurf);
2213}
2214
2215static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002216shell_destroy_shell_surface(struct wl_resource *resource)
2217{
2218 struct shell_surface *shsurf = resource->data;
2219
2220 destroy_shell_surface(shsurf);
2221}
2222
2223static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002224shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002225{
2226 struct shell_surface *shsurf = container_of(listener,
2227 struct shell_surface,
2228 surface_destroy_listener);
2229
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002230 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002231 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002232 } else {
2233 wl_signal_emit(&shsurf->resource.destroy_signal,
2234 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002235 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002236 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002237}
2238
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002239static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002240shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002241
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002242static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002243get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002244{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002245 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002246 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002247 else
2248 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002249}
2250
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002251static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002252create_shell_surface(void *shell, struct weston_surface *surface,
2253 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002254{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002255 struct shell_surface *shsurf;
2256
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002257 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002258 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002259 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002260 }
2261
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002262 shsurf = calloc(1, sizeof *shsurf);
2263 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002264 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002265 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002266 }
2267
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002268 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002269 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002270
Tiago Vignattibc052c92012-04-19 16:18:18 +03002271 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002272 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002273 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002274 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002275 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002276 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2277 shsurf->fullscreen.framerate = 0;
2278 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002279 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002280 wl_list_init(&shsurf->fullscreen.transform.link);
2281
Tiago Vignattibc052c92012-04-19 16:18:18 +03002282 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002283 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002284 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002285 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002286
2287 /* init link so its safe to always remove it in destroy_shell_surface */
2288 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002289 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002290
Pekka Paalanen460099f2012-01-20 16:48:25 +02002291 /* empty when not in use */
2292 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002293 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002294
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002295 wl_list_init(&shsurf->workspace_transform.link);
2296
Pekka Paalanen98262232011-12-01 10:42:22 +02002297 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002298 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002299
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002300 shsurf->client = client;
2301
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002302 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002303}
2304
2305static void
2306shell_get_shell_surface(struct wl_client *client,
2307 struct wl_resource *resource,
2308 uint32_t id,
2309 struct wl_resource *surface_resource)
2310{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002311 struct weston_surface *surface =
2312 wl_resource_get_user_data(surface_resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002313 struct desktop_shell *shell = resource->data;
2314 struct shell_surface *shsurf;
2315
2316 if (get_shell_surface(surface)) {
2317 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002318 WL_DISPLAY_ERROR_INVALID_OBJECT,
2319 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002320 return;
2321 }
2322
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002323 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002324 if (!shsurf) {
2325 wl_resource_post_error(surface_resource,
2326 WL_DISPLAY_ERROR_INVALID_OBJECT,
2327 "surface->configure already set");
2328 return;
2329 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002330
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002331 shsurf->resource.destroy = shell_destroy_shell_surface;
2332 shsurf->resource.object.id = id;
2333 shsurf->resource.object.interface = &wl_shell_surface_interface;
2334 shsurf->resource.object.implementation =
2335 (void (**)(void)) &shell_surface_implementation;
2336 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002337
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002338 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002339}
2340
2341static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002342 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002343};
2344
Kristian Høgsberg07937562011-04-12 17:25:42 -04002345static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002346shell_fade(struct desktop_shell *shell, enum fade_type type);
2347
2348static int
2349screensaver_timeout(void *data)
2350{
2351 struct desktop_shell *shell = data;
2352
2353 shell_fade(shell, FADE_OUT);
2354
2355 return 1;
2356}
2357
2358static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002359handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002360{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002361 struct desktop_shell *shell =
2362 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002363
Pekka Paalanen18027e52011-12-02 16:31:49 +02002364 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002365
2366 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002367 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002368}
2369
2370static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002371launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002372{
2373 if (shell->screensaver.binding)
2374 return;
2375
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002376 if (!shell->screensaver.path) {
2377 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002378 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002379 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002380
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002381 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002382 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002383 return;
2384 }
2385
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002386 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002387 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002388 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002389 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002390}
2391
2392static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002393terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002394{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002395 if (shell->screensaver.process.pid == 0)
2396 return;
2397
2398 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002399}
2400
2401static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002402configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002403{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002404 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002405
Giulio Camuffo184df502013-02-21 11:29:21 +01002406 if (width == 0)
2407 return;
2408
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002409 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2410 if (s->output == es->output && s != es) {
2411 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002412 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002413 }
2414 }
2415
Giulio Camuffo184df502013-02-21 11:29:21 +01002416 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002417
2418 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002419 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002420 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002421 }
2422}
2423
2424static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002425background_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 -04002426{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002427 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002428
Giulio Camuffo184df502013-02-21 11:29:21 +01002429 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002430}
2431
2432static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002433desktop_shell_set_background(struct wl_client *client,
2434 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002435 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002436 struct wl_resource *surface_resource)
2437{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002438 struct desktop_shell *shell = resource->data;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002439 struct weston_surface *surface =
2440 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002441
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002442 if (surface->configure) {
2443 wl_resource_post_error(surface_resource,
2444 WL_DISPLAY_ERROR_INVALID_OBJECT,
2445 "surface role already assigned");
2446 return;
2447 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002448
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002449 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002450 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002451 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002452 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002453 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002454 surface->output->width,
2455 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002456}
2457
2458static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002459panel_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 -04002460{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002461 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002462
Giulio Camuffo184df502013-02-21 11:29:21 +01002463 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002464}
2465
2466static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002467desktop_shell_set_panel(struct wl_client *client,
2468 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002469 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002470 struct wl_resource *surface_resource)
2471{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002472 struct desktop_shell *shell = resource->data;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002473 struct weston_surface *surface =
2474 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002475
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002476 if (surface->configure) {
2477 wl_resource_post_error(surface_resource,
2478 WL_DISPLAY_ERROR_INVALID_OBJECT,
2479 "surface role already assigned");
2480 return;
2481 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002482
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002483 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002484 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002485 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002486 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002487 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002488 surface->output->width,
2489 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002490}
2491
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002492static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002493lock_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 -04002494{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002495 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002496
Giulio Camuffo184df502013-02-21 11:29:21 +01002497 if (width == 0)
2498 return;
2499
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002500 surface->geometry.width = width;
2501 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002502 center_on_output(surface, get_default_output(shell->compositor));
2503
2504 if (!weston_surface_is_mapped(surface)) {
2505 wl_list_insert(&shell->lock_layer.surface_list,
2506 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002507 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002508 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002509 }
2510}
2511
2512static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002513handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002514{
Tiago Vignattibe143262012-04-16 17:31:41 +03002515 struct desktop_shell *shell =
2516 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002517
Martin Minarik6d118362012-06-07 18:01:59 +02002518 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002519 shell->lock_surface = NULL;
2520}
2521
2522static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002523desktop_shell_set_lock_surface(struct wl_client *client,
2524 struct wl_resource *resource,
2525 struct wl_resource *surface_resource)
2526{
Tiago Vignattibe143262012-04-16 17:31:41 +03002527 struct desktop_shell *shell = resource->data;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002528 struct weston_surface *surface =
2529 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002530
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002531 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002532
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002533 if (!shell->locked)
2534 return;
2535
Pekka Paalanen98262232011-12-01 10:42:22 +02002536 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002537
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002538 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2539 wl_signal_add(&surface_resource->destroy_signal,
2540 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002541
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002542 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002543 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002544}
2545
2546static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002547resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002548{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002549 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002550
Pekka Paalanen77346a62011-11-30 16:26:35 +02002551 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002552
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002553 wl_list_remove(&shell->lock_layer.link);
2554 wl_list_insert(&shell->compositor->cursor_layer.link,
2555 &shell->fullscreen_layer.link);
2556 wl_list_insert(&shell->fullscreen_layer.link,
2557 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002558 if (shell->showing_input_panels) {
2559 wl_list_insert(&shell->panel_layer.link,
2560 &shell->input_panel_layer.link);
2561 wl_list_insert(&shell->input_panel_layer.link,
2562 &ws->layer.link);
2563 } else {
2564 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2565 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002566
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002567 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002568
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002569 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002570 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002571 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002572}
2573
2574static void
2575desktop_shell_unlock(struct wl_client *client,
2576 struct wl_resource *resource)
2577{
Tiago Vignattibe143262012-04-16 17:31:41 +03002578 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002579
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002580 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002581
2582 if (shell->locked)
2583 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002584}
2585
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002586static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002587desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002588 struct wl_resource *resource,
2589 struct wl_resource *surface_resource)
2590{
2591 struct desktop_shell *shell = resource->data;
2592
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002593 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002594}
2595
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002596static void
2597desktop_shell_desktop_ready(struct wl_client *client,
2598 struct wl_resource *resource)
2599{
2600 struct desktop_shell *shell = resource->data;
2601
2602 shell_fade_startup(shell);
2603}
2604
Kristian Høgsberg75840622011-09-06 13:48:16 -04002605static const struct desktop_shell_interface desktop_shell_implementation = {
2606 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002607 desktop_shell_set_panel,
2608 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002609 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002610 desktop_shell_set_grab_surface,
2611 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002612};
2613
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002614static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002615get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002616{
2617 struct shell_surface *shsurf;
2618
2619 shsurf = get_shell_surface(surface);
2620 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002621 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002622 return shsurf->type;
2623}
2624
Kristian Høgsberg75840622011-09-06 13:48:16 -04002625static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002626move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002627{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002628 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002629 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002630 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002631 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002632
Pekka Paalanen01388e22013-04-25 13:57:44 +03002633 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002634 if (surface == NULL)
2635 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002636
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002637 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002638 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2639 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002640 return;
2641
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002642 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002643}
2644
2645static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002646resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002647{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002648 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002649 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002650 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002651 uint32_t edges = 0;
2652 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002653 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002654
Pekka Paalanen01388e22013-04-25 13:57:44 +03002655 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002656 if (surface == NULL)
2657 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002658
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002659 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002660 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2661 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002662 return;
2663
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002664 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002665 wl_fixed_to_int(seat->pointer->grab_x),
2666 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002667 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002668
Pekka Paalanen60921e52012-01-25 15:55:43 +02002669 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002670 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002671 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002672 edges |= 0;
2673 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002674 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002675
Pekka Paalanen60921e52012-01-25 15:55:43 +02002676 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002677 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002678 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002679 edges |= 0;
2680 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002681 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002682
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002683 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002684}
2685
2686static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002687surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002688 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002689{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002690 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002691 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002692 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002693 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002694 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002695
Pekka Paalanen01388e22013-04-25 13:57:44 +03002696 /* XXX: broken for windows containing sub-surfaces */
2697 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002698 if (surface == NULL)
2699 return;
2700
2701 shsurf = get_shell_surface(surface);
2702 if (!shsurf)
2703 return;
2704
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002705 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002706
Scott Moreau02709af2012-05-22 01:54:10 -06002707 if (surface->alpha > 1.0)
2708 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002709 if (surface->alpha < step)
2710 surface->alpha = step;
2711
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002712 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002713 weston_surface_damage(surface);
2714}
2715
2716static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002717do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002718 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002719{
Daniel Stone37816df2012-05-16 18:45:18 +01002720 struct weston_seat *ws = (struct weston_seat *) seat;
2721 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002722 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002723 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002724
2725 wl_list_for_each(output, &compositor->output_list, link) {
2726 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002727 wl_fixed_to_double(seat->pointer->x),
2728 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002729 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002730 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002731 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002732 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002733 increment = -output->zoom.increment;
2734 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002735 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002736 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002737 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002738 else
2739 increment = 0;
2740
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002741 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002742
Scott Moreaue6603982012-06-11 13:07:51 -06002743 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002744 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002745 else if (output->zoom.level > output->zoom.max_level)
2746 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002747 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002748 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002749 output->disable_planes++;
2750 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002751
Scott Moreaue6603982012-06-11 13:07:51 -06002752 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002753
Scott Moreau8dacaab2012-06-17 18:10:58 -06002754 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002755 }
2756 }
2757}
2758
Scott Moreauccbf29d2012-02-22 14:21:41 -07002759static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002760zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002761 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002762{
2763 do_zoom(seat, time, 0, axis, value);
2764}
2765
2766static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002767zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002768 void *data)
2769{
2770 do_zoom(seat, time, key, 0, 0);
2771}
2772
2773static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002774terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002775 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002776{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002777 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002778
Daniel Stone325fc2d2012-05-30 16:31:58 +01002779 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002780}
2781
2782static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002783rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002784{
2785 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002786 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002787 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002788 struct shell_surface *shsurf = rotate->base.shsurf;
2789 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002790 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002791
2792 if (!shsurf)
2793 return;
2794
2795 surface = shsurf->surface;
2796
2797 cx = 0.5f * surface->geometry.width;
2798 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002799
Daniel Stone37816df2012-05-16 18:45:18 +01002800 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2801 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002802 r = sqrtf(dx * dx + dy * dy);
2803
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002804 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002805 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002806
2807 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002808 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002809 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002810
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002811 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002812 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002813
2814 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002815 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002816 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002817 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002818 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002819
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002820 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002821 &shsurf->surface->geometry.transformation_list,
2822 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002823 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002824 wl_list_init(&shsurf->rotation.transform.link);
2825 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002826 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002827 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002828
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002829 /* We need to adjust the position of the surface
2830 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002831 cposx = surface->geometry.x + cx;
2832 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002833 dposx = rotate->center.x - cposx;
2834 dposy = rotate->center.y - cposy;
2835 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002836 weston_surface_set_position(surface,
2837 surface->geometry.x + dposx,
2838 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002839 }
2840
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002841 /* Repaint implies weston_surface_update_transform(), which
2842 * lazily applies the damage due to rotation update.
2843 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002844 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002845}
2846
2847static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002848rotate_grab_button(struct weston_pointer_grab *grab,
2849 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002850{
2851 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002852 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002853 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002854 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002855 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002856
Daniel Stone4dbadb12012-05-30 16:31:51 +01002857 if (pointer->button_count == 0 &&
2858 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002859 if (shsurf)
2860 weston_matrix_multiply(&shsurf->rotation.rotation,
2861 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002862 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002863 free(rotate);
2864 }
2865}
2866
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002867static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002868 noop_grab_focus,
2869 rotate_grab_motion,
2870 rotate_grab_button,
2871};
2872
2873static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002874surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002875{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002876 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002877 float dx, dy;
2878 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002879
Pekka Paalanen460099f2012-01-20 16:48:25 +02002880 rotate = malloc(sizeof *rotate);
2881 if (!rotate)
2882 return;
2883
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002884 weston_surface_to_global_float(surface->surface,
2885 surface->surface->geometry.width / 2,
2886 surface->surface->geometry.height / 2,
2887 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002888
Daniel Stone37816df2012-05-16 18:45:18 +01002889 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2890 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002891 r = sqrtf(dx * dx + dy * dy);
2892 if (r > 20.0f) {
2893 struct weston_matrix inverse;
2894
2895 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002896 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002897 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002898
2899 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002900 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002901 } else {
2902 weston_matrix_init(&surface->rotation.rotation);
2903 weston_matrix_init(&rotate->rotation);
2904 }
2905
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002906 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2907 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002908}
2909
2910static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002911rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002912 void *data)
2913{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002914 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002915 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002916 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002917 struct shell_surface *surface;
2918
Pekka Paalanen01388e22013-04-25 13:57:44 +03002919 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002920 if (base_surface == NULL)
2921 return;
2922
2923 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002924 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2925 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002926 return;
2927
2928 surface_rotate(surface, seat);
2929}
2930
2931static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002932lower_fullscreen_layer(struct desktop_shell *shell)
2933{
2934 struct workspace *ws;
2935 struct weston_surface *surface, *prev;
2936
2937 ws = get_current_workspace(shell);
2938 wl_list_for_each_reverse_safe(surface, prev,
2939 &shell->fullscreen_layer.surface_list,
2940 layer_link)
2941 weston_surface_restack(surface, &ws->layer.surface_list);
2942}
2943
2944static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002945activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002946 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002947{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002948 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002949 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002950 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002951
Pekka Paalanen01388e22013-04-25 13:57:44 +03002952 main_surface = weston_surface_get_main_surface(es);
2953
Daniel Stone37816df2012-05-16 18:45:18 +01002954 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002955
Jonas Ådahl8538b222012-08-29 22:13:03 +02002956 state = ensure_focus_state(shell, seat);
2957 if (state == NULL)
2958 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002959
2960 state->keyboard_focus = es;
2961 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002962 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002963
Pekka Paalanen01388e22013-04-25 13:57:44 +03002964 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002965 case SHELL_SURFACE_FULLSCREEN:
2966 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002967 shell_stack_fullscreen(get_shell_surface(main_surface));
2968 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002969 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002970 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002971 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002972 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002973 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002974 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002975 }
2976}
2977
Alex Wu21858432012-04-01 20:13:08 +08002978/* no-op func for checking black surface */
2979static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002980black_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 +08002981{
2982}
2983
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002984static bool
Alex Wu21858432012-04-01 20:13:08 +08002985is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2986{
2987 if (es->configure == black_surface_configure) {
2988 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002989 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002990 return true;
2991 }
2992 return false;
2993}
2994
Kristian Høgsberg75840622011-09-06 13:48:16 -04002995static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002996click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002997 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002998{
Daniel Stone37816df2012-05-16 18:45:18 +01002999 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003000 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003001 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003002 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003003
Daniel Stone37816df2012-05-16 18:45:18 +01003004 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003005 if (!focus)
3006 return;
3007
Pekka Paalanen01388e22013-04-25 13:57:44 +03003008 if (is_black_surface(focus, &main_surface))
3009 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003010
Pekka Paalanen01388e22013-04-25 13:57:44 +03003011 main_surface = weston_surface_get_main_surface(focus);
3012 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003013 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003014
Daniel Stone325fc2d2012-05-30 16:31:58 +01003015 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003016 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003017}
3018
3019static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003020lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003021{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003022 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003023
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003024 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003025 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003026 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003027 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003028
3029 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003030
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003031 /* Hide all surfaces by removing the fullscreen, panel and
3032 * toplevel layers. This way nothing else can show or receive
3033 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003034
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003035 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003036 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003037 if (shell->showing_input_panels)
3038 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003039 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003040 wl_list_insert(&shell->compositor->cursor_layer.link,
3041 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003042
Pekka Paalanen77346a62011-11-30 16:26:35 +02003043 launch_screensaver(shell);
3044
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003045 /* TODO: disable bindings that should not work while locked. */
3046
3047 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003048}
3049
3050static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003051unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003052{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003053 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003054 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003055 return;
3056 }
3057
3058 /* If desktop-shell client has gone away, unlock immediately. */
3059 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003060 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003061 return;
3062 }
3063
3064 if (shell->prepare_event_sent)
3065 return;
3066
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003067 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003068 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003069}
3070
3071static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003072shell_fade_done(struct weston_surface_animation *animation, void *data)
3073{
3074 struct desktop_shell *shell = data;
3075
3076 shell->fade.animation = NULL;
3077
3078 switch (shell->fade.type) {
3079 case FADE_IN:
3080 weston_surface_destroy(shell->fade.surface);
3081 shell->fade.surface = NULL;
3082 break;
3083 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003084 lock(shell);
3085 break;
3086 }
3087}
3088
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003089static struct weston_surface *
3090shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003091{
3092 struct weston_compositor *compositor = shell->compositor;
3093 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003094
3095 surface = weston_surface_create(compositor);
3096 if (!surface)
3097 return NULL;
3098
3099 weston_surface_configure(surface, 0, 0, 8192, 8192);
3100 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3101 wl_list_insert(&compositor->fade_layer.surface_list,
3102 &surface->layer_link);
3103 pixman_region32_init(&surface->input);
3104
3105 return surface;
3106}
3107
3108static void
3109shell_fade(struct desktop_shell *shell, enum fade_type type)
3110{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003111 float tint;
3112
3113 switch (type) {
3114 case FADE_IN:
3115 tint = 0.0;
3116 break;
3117 case FADE_OUT:
3118 tint = 1.0;
3119 break;
3120 default:
3121 weston_log("shell: invalid fade type\n");
3122 return;
3123 }
3124
3125 shell->fade.type = type;
3126
3127 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003128 shell->fade.surface = shell_fade_create_surface(shell);
3129 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003130 return;
3131
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003132 shell->fade.surface->alpha = 1.0 - tint;
3133 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003134 }
3135
3136 if (shell->fade.animation)
3137 weston_fade_update(shell->fade.animation,
3138 shell->fade.surface->alpha, tint, 30.0);
3139 else
3140 shell->fade.animation =
3141 weston_fade_run(shell->fade.surface,
3142 1.0 - tint, tint, 30.0,
3143 shell_fade_done, shell);
3144}
3145
3146static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003147do_shell_fade_startup(void *data)
3148{
3149 struct desktop_shell *shell = data;
3150
3151 shell_fade(shell, FADE_IN);
3152}
3153
3154static void
3155shell_fade_startup(struct desktop_shell *shell)
3156{
3157 struct wl_event_loop *loop;
3158
3159 if (!shell->fade.startup_timer)
3160 return;
3161
3162 wl_event_source_remove(shell->fade.startup_timer);
3163 shell->fade.startup_timer = NULL;
3164
3165 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3166 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3167}
3168
3169static int
3170fade_startup_timeout(void *data)
3171{
3172 struct desktop_shell *shell = data;
3173
3174 shell_fade_startup(shell);
3175 return 0;
3176}
3177
3178static void
3179shell_fade_init(struct desktop_shell *shell)
3180{
3181 /* Make compositor output all black, and wait for the desktop-shell
3182 * client to signal it is ready, then fade in. The timer triggers a
3183 * fade-in, in case the desktop-shell client takes too long.
3184 */
3185
3186 struct wl_event_loop *loop;
3187
3188 if (shell->fade.surface != NULL) {
3189 weston_log("%s: warning: fade surface already exists\n",
3190 __func__);
3191 return;
3192 }
3193
3194 shell->fade.surface = shell_fade_create_surface(shell);
3195 if (!shell->fade.surface)
3196 return;
3197
3198 weston_surface_update_transform(shell->fade.surface);
3199 weston_surface_damage(shell->fade.surface);
3200
3201 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3202 shell->fade.startup_timer =
3203 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3204 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3205}
3206
3207static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003208idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003209{
3210 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003211 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003212
3213 shell_fade(shell, FADE_OUT);
3214 /* lock() is called from shell_fade_done() */
3215}
3216
3217static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003218wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003219{
3220 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003221 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003222
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003223 unlock(shell);
3224}
3225
3226static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003227show_input_panels(struct wl_listener *listener, void *data)
3228{
3229 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003230 container_of(listener, struct desktop_shell,
3231 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003232 struct input_panel_surface *surface, *next;
3233 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003234
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003235 shell->text_input.surface = (struct weston_surface*)data;
3236
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003237 if (shell->showing_input_panels)
3238 return;
3239
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003240 shell->showing_input_panels = true;
3241
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003242 if (!shell->locked)
3243 wl_list_insert(&shell->panel_layer.link,
3244 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003245
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003246 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003247 &shell->input_panel.surfaces, link) {
3248 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003249 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003250 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003251 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003252 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003253 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003254 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003255 weston_surface_damage(ws);
3256 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003257 }
3258}
3259
3260static void
3261hide_input_panels(struct wl_listener *listener, void *data)
3262{
3263 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003264 container_of(listener, struct desktop_shell,
3265 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003266 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003267
Jan Arne Petersen61381972013-01-31 15:52:21 +01003268 if (!shell->showing_input_panels)
3269 return;
3270
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003271 shell->showing_input_panels = false;
3272
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003273 if (!shell->locked)
3274 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003275
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003276 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003277 &shell->input_panel_layer.surface_list, layer_link)
3278 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003279}
3280
3281static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003282update_input_panels(struct wl_listener *listener, void *data)
3283{
3284 struct desktop_shell *shell =
3285 container_of(listener, struct desktop_shell,
3286 update_input_panel_listener);
3287
3288 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3289}
3290
3291static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003292center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003293{
Giulio Camuffob8366642013-04-25 13:57:46 +03003294 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003295 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003296
Giulio Camuffob8366642013-04-25 13:57:46 +03003297 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3298
3299 x = output->x + (output->width - width) / 2 - surf_x / 2;
3300 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003301
3302 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003303}
3304
3305static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003306weston_surface_set_initial_position (struct weston_surface *surface,
3307 struct desktop_shell *shell)
3308{
3309 struct weston_compositor *compositor = shell->compositor;
3310 int ix = 0, iy = 0;
3311 int range_x, range_y;
3312 int dx, dy, x, y, panel_height;
3313 struct weston_output *output, *target_output = NULL;
3314 struct weston_seat *seat;
3315
3316 /* As a heuristic place the new window on the same output as the
3317 * pointer. Falling back to the output containing 0, 0.
3318 *
3319 * TODO: Do something clever for touch too?
3320 */
3321 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003322 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003323 ix = wl_fixed_to_int(seat->pointer->x);
3324 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003325 break;
3326 }
3327 }
3328
3329 wl_list_for_each(output, &compositor->output_list, link) {
3330 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3331 target_output = output;
3332 break;
3333 }
3334 }
3335
3336 if (!target_output) {
3337 weston_surface_set_position(surface, 10 + random() % 400,
3338 10 + random() % 400);
3339 return;
3340 }
3341
3342 /* Valid range within output where the surface will still be onscreen.
3343 * If this is negative it means that the surface is bigger than
3344 * output.
3345 */
3346 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003347 range_x = target_output->width - surface->geometry.width;
3348 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003349 surface->geometry.height;
3350
Rob Bradford4cb88c72012-08-13 15:18:44 +01003351 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003352 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003353 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003354 dx = 0;
3355
3356 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003357 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003358 else
3359 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003360
3361 x = target_output->x + dx;
3362 y = target_output->y + dy;
3363
3364 weston_surface_set_position (surface, x, y);
3365}
3366
3367static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003368map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003369 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003370{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003371 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003372 struct shell_surface *shsurf = get_shell_surface(surface);
3373 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003374 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003375 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003376 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003377 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003378 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003379
Pekka Paalanen60921e52012-01-25 15:55:43 +02003380 surface->geometry.width = width;
3381 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003382 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003383
3384 /* initial positioning, see also configure() */
3385 switch (surface_type) {
3386 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003387 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003388 break;
Alex Wu4539b082012-03-01 12:57:46 +08003389 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003390 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003391 shell_map_fullscreen(shsurf);
3392 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003393 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003394 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003395 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003396 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3397 NULL, NULL);
3398 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3399 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003400 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003401 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003402 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003403 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003404 case SHELL_SURFACE_NONE:
3405 weston_surface_set_position(surface,
3406 surface->geometry.x + sx,
3407 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003408 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003409 default:
3410 ;
3411 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003412
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003413 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003414 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003415 case SHELL_SURFACE_POPUP:
3416 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003417 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003418 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3419 break;
Alex Wu4539b082012-03-01 12:57:46 +08003420 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003421 case SHELL_SURFACE_NONE:
3422 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003423 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003424 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003425 ws = get_current_workspace(shell);
3426 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003427 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003428 }
3429
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003430 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003431 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003432 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3433 surface->output = shsurf->output;
3434 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003435
Juan Zhao7bb92f02011-12-15 11:31:51 -05003436 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003437 /* XXX: xwayland's using the same fields for transient type */
3438 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003439 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003440 if (shsurf->transient.flags ==
3441 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3442 break;
3443 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003444 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003445 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003446 if (!shell->locked) {
3447 wl_list_for_each(seat, &compositor->seat_list, link)
3448 activate(shell, surface, seat);
3449 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003450 break;
3451 default:
3452 break;
3453 }
3454
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003455 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003456 {
3457 switch (shell->win_animation_type) {
3458 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003459 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003460 break;
3461 case ANIMATION_ZOOM:
3462 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3463 break;
3464 default:
3465 break;
3466 }
3467 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003468}
3469
3470static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003471configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003472 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003473{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003474 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3475 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003476 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003477
Pekka Paalanen77346a62011-11-30 16:26:35 +02003478 shsurf = get_shell_surface(surface);
3479 if (shsurf)
3480 surface_type = shsurf->type;
3481
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003482 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003483
3484 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003485 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003486 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003487 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003488 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003489 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003490 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003491 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3492 NULL, NULL);
3493 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003494 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003495 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003496 break;
Alex Wu4539b082012-03-01 12:57:46 +08003497 case SHELL_SURFACE_TOPLEVEL:
3498 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003499 default:
3500 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003501 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003502
Alex Wu4539b082012-03-01 12:57:46 +08003503 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003504 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003505 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003506
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003507 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003508 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003509 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003510}
3511
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003512static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003513shell_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 +03003514{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003515 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003516 struct desktop_shell *shell = shsurf->shell;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003517 struct weston_compositor *compositor = shsurf->surface->compositor;
Giulio Camuffo184df502013-02-21 11:29:21 +01003518
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003519 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003520
Giulio Camuffo5085a752013-03-25 21:42:45 +01003521 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3522 remove_popup_grab(shsurf);
3523 }
3524
Giulio Camuffo184df502013-02-21 11:29:21 +01003525 if (width == 0)
3526 return;
3527
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003528 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003529 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003530 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003531 type_changed = 1;
3532 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003533
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003534 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003535 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003536 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003537 es->geometry.width != width ||
3538 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003539 float from_x, from_y;
3540 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003541
3542 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3543 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3544 configure(shell, es,
3545 es->geometry.x + to_x - from_x,
3546 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003547 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003548 }
3549}
3550
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003551static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003552
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003553static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003554desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003555{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003556 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003557 struct desktop_shell *shell =
3558 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003559
3560 shell->child.process.pid = 0;
3561 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003562
3563 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3564 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003565 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003566 shell->child.deathstamp = time;
3567 shell->child.deathcount = 0;
3568 }
3569
3570 shell->child.deathcount++;
3571 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003572 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003573 return;
3574 }
3575
Martin Minarik6d118362012-06-07 18:01:59 +02003576 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003577 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003578 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003579}
3580
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003581static void
3582launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003583{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003584 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003585 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003586
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003587 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003588 &shell->child.process,
3589 shell_exe,
3590 desktop_shell_sigchld);
3591
3592 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003593 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003594}
3595
3596static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003597bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3598{
Tiago Vignattibe143262012-04-16 17:31:41 +03003599 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003600
3601 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003602 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003603}
3604
Kristian Høgsberg75840622011-09-06 13:48:16 -04003605static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003606unbind_desktop_shell(struct wl_resource *resource)
3607{
Tiago Vignattibe143262012-04-16 17:31:41 +03003608 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003609
3610 if (shell->locked)
3611 resume_desktop(shell);
3612
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003613 shell->child.desktop_shell = NULL;
3614 shell->prepare_event_sent = false;
3615 free(resource);
3616}
3617
3618static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003619bind_desktop_shell(struct wl_client *client,
3620 void *data, uint32_t version, uint32_t id)
3621{
Tiago Vignattibe143262012-04-16 17:31:41 +03003622 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003623 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003624
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003625 resource = wl_client_add_object(client, &desktop_shell_interface,
3626 &desktop_shell_implementation,
3627 id, shell);
3628
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003629 if (client == shell->child.client) {
3630 resource->destroy = unbind_desktop_shell;
3631 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003632
3633 if (version < 2)
3634 shell_fade_startup(shell);
3635
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003636 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003637 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003638
3639 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3640 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003641 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003642}
3643
Pekka Paalanen6e168112011-11-24 11:34:05 +02003644static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003645screensaver_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 -04003646{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003647 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003648
Giulio Camuffo184df502013-02-21 11:29:21 +01003649 if (width == 0)
3650 return;
3651
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003652 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003653 if (!shell->locked)
3654 return;
3655
3656 center_on_output(surface, surface->output);
3657
3658 if (wl_list_empty(&surface->layer_link)) {
3659 wl_list_insert(shell->lock_layer.surface_list.prev,
3660 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003661 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003662 wl_event_source_timer_update(shell->screensaver.timer,
3663 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003664 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003665 }
3666}
3667
3668static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003669screensaver_set_surface(struct wl_client *client,
3670 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003671 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003672 struct wl_resource *output_resource)
3673{
Tiago Vignattibe143262012-04-16 17:31:41 +03003674 struct desktop_shell *shell = resource->data;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003675 struct weston_surface *surface =
3676 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003677 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003678
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003679 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003680 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003681 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003682}
3683
3684static const struct screensaver_interface screensaver_implementation = {
3685 screensaver_set_surface
3686};
3687
3688static void
3689unbind_screensaver(struct wl_resource *resource)
3690{
Tiago Vignattibe143262012-04-16 17:31:41 +03003691 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003692
Pekka Paalanen77346a62011-11-30 16:26:35 +02003693 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003694 free(resource);
3695}
3696
3697static void
3698bind_screensaver(struct wl_client *client,
3699 void *data, uint32_t version, uint32_t id)
3700{
Tiago Vignattibe143262012-04-16 17:31:41 +03003701 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003702 struct wl_resource *resource;
3703
3704 resource = wl_client_add_object(client, &screensaver_interface,
3705 &screensaver_implementation,
3706 id, shell);
3707
Pekka Paalanen77346a62011-11-30 16:26:35 +02003708 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003709 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003710 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003711 return;
3712 }
3713
3714 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3715 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003716 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003717}
3718
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003719static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003720input_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 -04003721{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003722 struct input_panel_surface *ip_surface = surface->configure_private;
3723 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003724 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003725 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003726 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003727
Giulio Camuffo184df502013-02-21 11:29:21 +01003728 if (width == 0)
3729 return;
3730
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003731 if (!weston_surface_is_mapped(surface)) {
3732 if (!shell->showing_input_panels)
3733 return;
3734
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003735 show_surface = 1;
3736 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003737
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003738 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003739
3740 if (ip_surface->panel) {
3741 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3742 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3743 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003744 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003745
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003746 x = ip_surface->output->x + (mode->width - width) / 2;
3747 y = ip_surface->output->y + mode->height - height;
3748 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003749
3750 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003751 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003752 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003753
3754 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003755 wl_list_insert(&shell->input_panel_layer.surface_list,
3756 &surface->layer_link);
3757 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003758 weston_surface_damage(surface);
3759 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3760 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003761}
3762
3763static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003764destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003765{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003766 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003767 wl_list_remove(&input_panel_surface->link);
3768
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003769 input_panel_surface->surface->configure = NULL;
3770
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003771 free(input_panel_surface);
3772}
3773
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003774static struct input_panel_surface *
3775get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003776{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003777 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003778 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003779 } else {
3780 return NULL;
3781 }
3782}
3783
3784static void
3785input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3786{
3787 struct input_panel_surface *ipsurface = container_of(listener,
3788 struct input_panel_surface,
3789 surface_destroy_listener);
3790
3791 if (ipsurface->resource.client) {
3792 wl_resource_destroy(&ipsurface->resource);
3793 } else {
3794 wl_signal_emit(&ipsurface->resource.destroy_signal,
3795 &ipsurface->resource);
3796 destroy_input_panel_surface(ipsurface);
3797 }
3798}
3799static struct input_panel_surface *
3800create_input_panel_surface(struct desktop_shell *shell,
3801 struct weston_surface *surface)
3802{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003803 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003804
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003805 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3806 if (!input_panel_surface)
3807 return NULL;
3808
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003809 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003810 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003811
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003812 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003813
3814 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003815
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003816 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3817 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003818 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003819 &input_panel_surface->surface_destroy_listener);
3820
3821 wl_list_init(&input_panel_surface->link);
3822
3823 return input_panel_surface;
3824}
3825
3826static void
3827input_panel_surface_set_toplevel(struct wl_client *client,
3828 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003829 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003830 uint32_t position)
3831{
3832 struct input_panel_surface *input_panel_surface = resource->data;
3833 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003834
3835 wl_list_insert(&shell->input_panel.surfaces,
3836 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003837
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003838 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003839 input_panel_surface->panel = 0;
3840}
3841
3842static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003843input_panel_surface_set_overlay_panel(struct wl_client *client,
3844 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003845{
3846 struct input_panel_surface *input_panel_surface = resource->data;
3847 struct desktop_shell *shell = input_panel_surface->shell;
3848
3849 wl_list_insert(&shell->input_panel.surfaces,
3850 &input_panel_surface->link);
3851
3852 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003853}
3854
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003855static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003856 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003857 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003858};
3859
3860static void
3861destroy_input_panel_surface_resource(struct wl_resource *resource)
3862{
3863 struct input_panel_surface *ipsurf = resource->data;
3864
3865 destroy_input_panel_surface(ipsurf);
3866}
3867
3868static void
3869input_panel_get_input_panel_surface(struct wl_client *client,
3870 struct wl_resource *resource,
3871 uint32_t id,
3872 struct wl_resource *surface_resource)
3873{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003874 struct weston_surface *surface =
3875 wl_resource_get_user_data(surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003876 struct desktop_shell *shell = resource->data;
3877 struct input_panel_surface *ipsurf;
3878
3879 if (get_input_panel_surface(surface)) {
3880 wl_resource_post_error(surface_resource,
3881 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003882 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003883 return;
3884 }
3885
3886 ipsurf = create_input_panel_surface(shell, surface);
3887 if (!ipsurf) {
3888 wl_resource_post_error(surface_resource,
3889 WL_DISPLAY_ERROR_INVALID_OBJECT,
3890 "surface->configure already set");
3891 return;
3892 }
3893
3894 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3895 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003896 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003897 ipsurf->resource.object.implementation =
3898 (void (**)(void)) &input_panel_surface_implementation;
3899 ipsurf->resource.data = ipsurf;
3900
3901 wl_client_add_resource(client, &ipsurf->resource);
3902}
3903
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003904static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003905 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003906};
3907
3908static void
3909unbind_input_panel(struct wl_resource *resource)
3910{
3911 struct desktop_shell *shell = resource->data;
3912
3913 shell->input_panel.binding = NULL;
3914 free(resource);
3915}
3916
3917static void
3918bind_input_panel(struct wl_client *client,
3919 void *data, uint32_t version, uint32_t id)
3920{
3921 struct desktop_shell *shell = data;
3922 struct wl_resource *resource;
3923
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003924 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003925 &input_panel_implementation,
3926 id, shell);
3927
3928 if (shell->input_panel.binding == NULL) {
3929 resource->destroy = unbind_input_panel;
3930 shell->input_panel.binding = resource;
3931 return;
3932 }
3933
3934 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3935 "interface object already bound");
3936 wl_resource_destroy(resource);
3937}
3938
Kristian Høgsberg07045392012-02-19 18:52:44 -05003939struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003940 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003941 struct weston_surface *current;
3942 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003943 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003944};
3945
3946static void
3947switcher_next(struct switcher *switcher)
3948{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003949 struct weston_surface *surface;
3950 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003951 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003952 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003953
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003954 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003955 switch (get_shell_surface_type(surface)) {
3956 case SHELL_SURFACE_TOPLEVEL:
3957 case SHELL_SURFACE_FULLSCREEN:
3958 case SHELL_SURFACE_MAXIMIZED:
3959 if (first == NULL)
3960 first = surface;
3961 if (prev == switcher->current)
3962 next = surface;
3963 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003964 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003965 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003966 weston_surface_damage(surface);
3967 break;
3968 default:
3969 break;
3970 }
Alex Wu1659daa2012-04-01 20:13:09 +08003971
3972 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003973 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003974 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003975 weston_surface_damage(surface);
3976 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003977 }
3978
3979 if (next == NULL)
3980 next = first;
3981
Alex Wu07b26062012-03-12 16:06:01 +08003982 if (next == NULL)
3983 return;
3984
Kristian Høgsberg07045392012-02-19 18:52:44 -05003985 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003986 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003987
3988 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003989 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003990
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003991 shsurf = get_shell_surface(switcher->current);
3992 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003993 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003994}
3995
3996static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003997switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003998{
3999 struct switcher *switcher =
4000 container_of(listener, struct switcher, listener);
4001
4002 switcher_next(switcher);
4003}
4004
4005static void
Daniel Stone351eb612012-05-31 15:27:47 -04004006switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004007{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004008 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004009 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004010 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004011
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004012 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004013 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004014 weston_surface_damage(surface);
4015 }
4016
Alex Wu07b26062012-03-12 16:06:01 +08004017 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004018 activate(switcher->shell, switcher->current,
4019 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004020 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004021 weston_keyboard_end_grab(keyboard);
4022 if (keyboard->input_method_resource)
4023 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004024 free(switcher);
4025}
4026
4027static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004028switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004029 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004030{
4031 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004032 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004033
Daniel Stonec9785ea2012-05-30 16:31:52 +01004034 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004035 switcher_next(switcher);
4036}
4037
4038static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004039switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004040 uint32_t mods_depressed, uint32_t mods_latched,
4041 uint32_t mods_locked, uint32_t group)
4042{
4043 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004044 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004045
Daniel Stone351eb612012-05-31 15:27:47 -04004046 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4047 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004048}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004049
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004050static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004051 switcher_key,
4052 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004053};
4054
4055static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004056switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004057 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004058{
Tiago Vignattibe143262012-04-16 17:31:41 +03004059 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004060 struct switcher *switcher;
4061
4062 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004063 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004064 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004065 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004066 wl_list_init(&switcher->listener.link);
4067
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004068 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004069 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004070 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004071 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4072 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004073 switcher_next(switcher);
4074}
4075
Pekka Paalanen3c647232011-12-22 13:43:43 +02004076static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004077backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004078 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004079{
4080 struct weston_compositor *compositor = data;
4081 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004082 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004083
4084 /* TODO: we're limiting to simple use cases, where we assume just
4085 * control on the primary display. We'd have to extend later if we
4086 * ever get support for setting backlights on random desktop LCD
4087 * panels though */
4088 output = get_default_output(compositor);
4089 if (!output)
4090 return;
4091
4092 if (!output->set_backlight)
4093 return;
4094
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004095 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4096 backlight_new = output->backlight_current - 25;
4097 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4098 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004099
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004100 if (backlight_new < 5)
4101 backlight_new = 5;
4102 if (backlight_new > 255)
4103 backlight_new = 255;
4104
4105 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004106 output->set_backlight(output, output->backlight_current);
4107}
4108
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004109struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004110 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004111 struct weston_seat *seat;
4112 uint32_t key[2];
4113 int key_released[2];
4114};
4115
4116static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004117debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004118 uint32_t key, uint32_t state)
4119{
4120 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4121 struct wl_resource *resource;
4122 struct wl_display *display;
4123 uint32_t serial;
4124 int send = 0, terminate = 0;
4125 int check_binding = 1;
4126 int i;
4127
4128 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4129 /* Do not run bindings on key releases */
4130 check_binding = 0;
4131
4132 for (i = 0; i < 2; i++)
4133 if (key == db->key[i])
4134 db->key_released[i] = 1;
4135
4136 if (db->key_released[0] && db->key_released[1]) {
4137 /* All key releases been swalled so end the grab */
4138 terminate = 1;
4139 } else if (key != db->key[0] && key != db->key[1]) {
4140 /* Should not swallow release of other keys */
4141 send = 1;
4142 }
4143 } else if (key == db->key[0] && !db->key_released[0]) {
4144 /* Do not check bindings for the first press of the binding
4145 * key. This allows it to be used as a debug shortcut.
4146 * We still need to swallow this event. */
4147 check_binding = 0;
4148 } else if (db->key[1]) {
4149 /* If we already ran a binding don't process another one since
4150 * we can't keep track of all the binding keys that were
4151 * pressed in order to swallow the release events. */
4152 send = 1;
4153 check_binding = 0;
4154 }
4155
4156 if (check_binding) {
4157 struct weston_compositor *ec = db->seat->compositor;
4158
4159 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4160 key, state)) {
4161 /* We ran a binding so swallow the press and keep the
4162 * grab to swallow the released too. */
4163 send = 0;
4164 terminate = 0;
4165 db->key[1] = key;
4166 } else {
4167 /* Terminate the grab since the key pressed is not a
4168 * debug binding key. */
4169 send = 1;
4170 terminate = 1;
4171 }
4172 }
4173
4174 if (send) {
4175 resource = grab->keyboard->focus_resource;
4176
4177 if (resource) {
4178 display = wl_client_get_display(resource->client);
4179 serial = wl_display_next_serial(display);
4180 wl_keyboard_send_key(resource, serial, time, key, state);
4181 }
4182 }
4183
4184 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004185 weston_keyboard_end_grab(grab->keyboard);
4186 if (grab->keyboard->input_method_resource)
4187 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004188 free(db);
4189 }
4190}
4191
4192static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004193debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004194 uint32_t mods_depressed, uint32_t mods_latched,
4195 uint32_t mods_locked, uint32_t group)
4196{
4197 struct wl_resource *resource;
4198
4199 resource = grab->keyboard->focus_resource;
4200 if (!resource)
4201 return;
4202
4203 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4204 mods_latched, mods_locked, group);
4205}
4206
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004207struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004208 debug_binding_key,
4209 debug_binding_modifiers
4210};
4211
4212static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004213debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004214{
4215 struct debug_binding_grab *grab;
4216
4217 grab = calloc(1, sizeof *grab);
4218 if (!grab)
4219 return;
4220
4221 grab->seat = (struct weston_seat *) seat;
4222 grab->key[0] = key;
4223 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004224 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004225}
4226
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004227static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004228force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004229 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004230{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004231 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004232 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004233 struct desktop_shell *shell = data;
4234 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004235 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004236
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004237 focus_surface = seat->keyboard->focus;
4238 if (!focus_surface)
4239 return;
4240
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004241 wl_signal_emit(&compositor->kill_signal, focus_surface);
4242
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004243 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004244 wl_client_get_credentials(client, &pid, NULL, NULL);
4245
4246 /* Skip clients that we launched ourselves (the credentials of
4247 * the socketpair is ours) */
4248 if (pid == getpid())
4249 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004250
Daniel Stone325fc2d2012-05-30 16:31:58 +01004251 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004252}
4253
4254static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004255workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004256 uint32_t key, void *data)
4257{
4258 struct desktop_shell *shell = data;
4259 unsigned int new_index = shell->workspaces.current;
4260
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004261 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004262 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004263 if (new_index != 0)
4264 new_index--;
4265
4266 change_workspace(shell, new_index);
4267}
4268
4269static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004270workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004271 uint32_t key, void *data)
4272{
4273 struct desktop_shell *shell = data;
4274 unsigned int new_index = shell->workspaces.current;
4275
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004276 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004277 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004278 if (new_index < shell->workspaces.num - 1)
4279 new_index++;
4280
4281 change_workspace(shell, new_index);
4282}
4283
4284static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004285workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004286 uint32_t key, void *data)
4287{
4288 struct desktop_shell *shell = data;
4289 unsigned int new_index;
4290
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004291 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004292 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004293 new_index = key - KEY_F1;
4294 if (new_index >= shell->workspaces.num)
4295 new_index = shell->workspaces.num - 1;
4296
4297 change_workspace(shell, new_index);
4298}
4299
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004300static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004301workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004302 uint32_t key, void *data)
4303{
4304 struct desktop_shell *shell = data;
4305 unsigned int new_index = shell->workspaces.current;
4306
4307 if (shell->locked)
4308 return;
4309
4310 if (new_index != 0)
4311 new_index--;
4312
4313 take_surface_to_workspace_by_seat(shell, seat, new_index);
4314}
4315
4316static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004317workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004318 uint32_t key, void *data)
4319{
4320 struct desktop_shell *shell = data;
4321 unsigned int new_index = shell->workspaces.current;
4322
4323 if (shell->locked)
4324 return;
4325
4326 if (new_index < shell->workspaces.num - 1)
4327 new_index++;
4328
4329 take_surface_to_workspace_by_seat(shell, seat, new_index);
4330}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004331
4332static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004333shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004334{
Tiago Vignattibe143262012-04-16 17:31:41 +03004335 struct desktop_shell *shell =
4336 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004337 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004338
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004339 if (shell->child.client)
4340 wl_client_destroy(shell->child.client);
4341
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004342 wl_list_remove(&shell->idle_listener.link);
4343 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004344 wl_list_remove(&shell->show_input_panel_listener.link);
4345 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004346
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004347 wl_array_for_each(ws, &shell->workspaces.array)
4348 workspace_destroy(*ws);
4349 wl_array_release(&shell->workspaces.array);
4350
Pekka Paalanen3c647232011-12-22 13:43:43 +02004351 free(shell->screensaver.path);
4352 free(shell);
4353}
4354
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004355static void
4356shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4357{
4358 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004359 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004360
4361 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004362 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4363 MODIFIER_CTRL | MODIFIER_ALT,
4364 terminate_binding, ec);
4365 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4366 click_to_activate_binding,
4367 shell);
4368 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4369 MODIFIER_SUPER | MODIFIER_ALT,
4370 surface_opacity_binding, NULL);
4371 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4372 MODIFIER_SUPER, zoom_axis_binding,
4373 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004374
4375 /* configurable bindings */
4376 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004377 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4378 zoom_key_binding, NULL);
4379 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4380 zoom_key_binding, NULL);
4381 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4382 shell);
4383 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4384 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004385
4386 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4387 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4388 rotate_binding, NULL);
4389
Daniel Stone325fc2d2012-05-30 16:31:58 +01004390 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4391 shell);
4392 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4393 ec);
4394 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4395 backlight_binding, ec);
4396 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4397 ec);
4398 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4399 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004400 weston_compositor_add_key_binding(ec, KEY_K, mod,
4401 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004402 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4403 workspace_up_binding, shell);
4404 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4405 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004406 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4407 workspace_move_surface_up_binding,
4408 shell);
4409 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4410 workspace_move_surface_down_binding,
4411 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004412
4413 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4414 if (shell->workspaces.num > 1) {
4415 num_workspace_bindings = shell->workspaces.num;
4416 if (num_workspace_bindings > 6)
4417 num_workspace_bindings = 6;
4418 for (i = 0; i < num_workspace_bindings; i++)
4419 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4420 workspace_f_binding,
4421 shell);
4422 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004423
4424 /* Debug bindings */
4425 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4426 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004427}
4428
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004429WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004430module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004431 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004432{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004433 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004434 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004435 struct workspace **pws;
4436 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004437 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004438
4439 shell = malloc(sizeof *shell);
4440 if (shell == NULL)
4441 return -1;
4442
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004443 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004444 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004445
4446 shell->destroy_listener.notify = shell_destroy;
4447 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004448 shell->idle_listener.notify = idle_handler;
4449 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4450 shell->wake_listener.notify = wake_handler;
4451 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004452 shell->show_input_panel_listener.notify = show_input_panels;
4453 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4454 shell->hide_input_panel_listener.notify = hide_input_panels;
4455 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004456 shell->update_input_panel_listener.notify = update_input_panels;
4457 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004458 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004459 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004460 ec->shell_interface.create_shell_surface = create_shell_surface;
4461 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004462 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004463 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004464 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004465 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004466 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004467
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004468 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004469
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004470 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4471 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004472 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4473 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004474 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004475
4476 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004477 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004478
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004479 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004480
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004481 for (i = 0; i < shell->workspaces.num; i++) {
4482 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4483 if (pws == NULL)
4484 return -1;
4485
4486 *pws = workspace_create();
4487 if (*pws == NULL)
4488 return -1;
4489 }
4490 activate_workspace(shell, 0);
4491
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004492 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004493 wl_list_init(&shell->workspaces.animation.link);
4494 shell->workspaces.animation.frame = animate_workspace_change_frame;
4495
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004496 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4497 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004498 return -1;
4499
Kristian Høgsberg75840622011-09-06 13:48:16 -04004500 if (wl_display_add_global(ec->wl_display,
4501 &desktop_shell_interface,
4502 shell, bind_desktop_shell) == NULL)
4503 return -1;
4504
Pekka Paalanen6e168112011-11-24 11:34:05 +02004505 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4506 shell, bind_screensaver) == NULL)
4507 return -1;
4508
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004509 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004510 shell, bind_input_panel) == NULL)
4511 return -1;
4512
Jonas Ådahle9d22502012-08-29 22:13:01 +02004513 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4514 shell, bind_workspace_manager) == NULL)
4515 return -1;
4516
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004517 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004518
4519 loop = wl_display_get_event_loop(ec->wl_display);
4520 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004521
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004522 shell->screensaver.timer =
4523 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4524
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004525 wl_list_for_each(seat, &ec->seat_list, link)
4526 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004527
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004528 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004529
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004530 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004531
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004532 return 0;
4533}