blob: eb8d802d99f825f50120f02c49b89439f186d461 [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
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010038#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050041
Jonas Ådahle3cddce2012-06-13 00:01:22 +020042#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020043#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044
Juan Zhaoe10d2792012-04-25 19:09:52 +080045enum animation_type {
46 ANIMATION_NONE,
47
48 ANIMATION_ZOOM,
49 ANIMATION_FADE
50};
51
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020052enum fade_type {
53 FADE_IN,
54 FADE_OUT
55};
56
Jonas Ådahl04769742012-06-13 00:01:24 +020057struct focus_state {
58 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040059 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020060 struct weston_surface *keyboard_focus;
61 struct wl_list link;
62 struct wl_listener seat_destroy_listener;
63 struct wl_listener surface_destroy_listener;
64};
65
Jonas Ådahle3cddce2012-06-13 00:01:22 +020066struct workspace {
67 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020068
69 struct wl_list focus_list;
70 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020071};
72
Philipp Brüschweiler88013572012-08-06 13:44:42 +020073struct input_panel_surface {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010074 struct wl_resource resource;
75
76 struct desktop_shell *shell;
77
Philipp Brüschweiler88013572012-08-06 13:44:42 +020078 struct wl_list link;
79 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010080 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020081
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020082 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020083 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020084};
85
Tiago Vignattibe143262012-04-16 17:31:41 +030086struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050087 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040088
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020089 struct wl_listener idle_listener;
90 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040091 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020092 struct wl_listener show_input_panel_listener;
93 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020094 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020095
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050096 struct weston_layer fullscreen_layer;
97 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050098 struct weston_layer background_layer;
99 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200100 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500101
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400102 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300103 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400104
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200105 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500106 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200107 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200108 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200109
110 unsigned deathcount;
111 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200112 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200113
114 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200115 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200116 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200117
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200118 struct {
119 struct weston_surface *surface;
120 pixman_box32_t cursor_rectangle;
121 } text_input;
122
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400123 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500124 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100125
Pekka Paalanen77346a62011-11-30 16:26:35 +0200126 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200127 struct wl_array array;
128 unsigned int current;
129 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200130
Jonas Ådahle9d22502012-08-29 22:13:01 +0200131 struct wl_list client_list;
132
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200133 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200134 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200135 int anim_dir;
136 uint32_t anim_timestamp;
137 double anim_current;
138 struct workspace *anim_from;
139 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200140 } workspaces;
141
142 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200143 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200144 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200145 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500146 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200147 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200148 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500149
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200150 struct {
151 struct wl_resource *binding;
152 struct wl_list surfaces;
153 } input_panel;
154
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200155 struct {
156 struct weston_surface *surface;
157 struct weston_surface_animation *animation;
158 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300159 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200160 } fade;
161
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300162 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800163 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400164};
165
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500166enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200167 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500168 SHELL_SURFACE_TOPLEVEL,
169 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500170 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800171 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500172 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200173};
174
Scott Moreauff1db4a2012-04-17 19:06:18 -0600175struct ping_timer {
176 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600177 uint32_t serial;
178};
179
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200180struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200181 struct wl_resource resource;
182
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500183 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200184 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400185 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300186 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200187
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400188 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400189 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500190 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800191 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800192 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600193 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100194
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500195 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200196 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500197 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200198 } rotation;
199
200 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100201 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500202 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100203 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400204 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500205 } popup;
206
Alex Wu4539b082012-03-01 12:57:46 +0800207 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300208 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400209 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300210 } transient;
211
212 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800213 enum wl_shell_surface_fullscreen_method type;
214 struct weston_transform transform; /* matrix from x, y */
215 uint32_t framerate;
216 struct weston_surface *black_surface;
217 } fullscreen;
218
Scott Moreauff1db4a2012-04-17 19:06:18 -0600219 struct ping_timer *ping_timer;
220
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200221 struct weston_transform workspace_transform;
222
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500223 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500224 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100225 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400226
227 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200228};
229
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300230struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400231 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300232 struct shell_surface *shsurf;
233 struct wl_listener shsurf_destroy_listener;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400234 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235};
236
237struct weston_move_grab {
238 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100239 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500240};
241
Pekka Paalanen460099f2012-01-20 16:48:25 +0200242struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300243 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500244 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200245 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200246 float x;
247 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200248 } center;
249};
250
Giulio Camuffo5085a752013-03-25 21:42:45 +0100251struct shell_seat {
252 struct weston_seat *seat;
253 struct wl_listener seat_destroy_listener;
254
255 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400256 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100257 struct wl_list surfaces_list;
258 struct wl_client *client;
259 int32_t initial_up;
260 } popup_grab;
261};
262
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400263static void
264activate(struct desktop_shell *shell, struct weston_surface *es,
265 struct weston_seat *seat);
266
267static struct workspace *
268get_current_workspace(struct desktop_shell *shell);
269
Alex Wubd3354b2012-04-17 17:20:49 +0800270static struct shell_surface *
271get_shell_surface(struct weston_surface *surface);
272
273static struct desktop_shell *
274shell_surface_get_shell(struct shell_surface *shsurf);
275
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500276static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400277surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500278
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300279static void
280shell_fade_startup(struct desktop_shell *shell);
281
Alex Wubd3354b2012-04-17 17:20:49 +0800282static bool
283shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
284{
285 struct desktop_shell *shell;
286 struct weston_surface *top_fs_es;
287
288 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100289
Alex Wubd3354b2012-04-17 17:20:49 +0800290 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
291 return false;
292
293 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100294 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800295 layer_link);
296 return (shsurf == get_shell_surface(top_fs_es));
297}
298
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500299static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400300destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300301{
302 struct shell_grab *grab;
303
304 grab = container_of(listener, struct shell_grab,
305 shsurf_destroy_listener);
306
307 grab->shsurf = NULL;
308}
309
310static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400311popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400312
313static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300314shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400315 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300316 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400317 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300318 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300319{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300320 struct desktop_shell *shell = shsurf->shell;
321
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400322 popup_grab_end(pointer);
323
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324 grab->grab.interface = interface;
325 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400326 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
327 wl_signal_add(&shsurf->resource.destroy_signal,
328 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300330 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300331
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400332 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300333 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400334 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400335 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336}
337
338static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300339shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300340{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400341 if (grab->shsurf)
342 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300343
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400344 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345}
346
347static void
Alex Wu4539b082012-03-01 12:57:46 +0800348center_on_output(struct weston_surface *surface,
349 struct weston_output *output);
350
Daniel Stone496ca172012-05-30 16:31:42 +0100351static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300352get_modifier(char *modifier)
353{
354 if (!modifier)
355 return MODIFIER_SUPER;
356
357 if (!strcmp("ctrl", modifier))
358 return MODIFIER_CTRL;
359 else if (!strcmp("alt", modifier))
360 return MODIFIER_ALT;
361 else if (!strcmp("super", modifier))
362 return MODIFIER_SUPER;
363 else
364 return MODIFIER_SUPER;
365}
366
Juan Zhaoe10d2792012-04-25 19:09:52 +0800367static enum animation_type
368get_animation_type(char *animation)
369{
370 if (!animation)
371 return ANIMATION_NONE;
372
373 if (!strcmp("zoom", animation))
374 return ANIMATION_ZOOM;
375 else if (!strcmp("fade", animation))
376 return ANIMATION_FADE;
377 else
378 return ANIMATION_NONE;
379}
380
Alex Wu4539b082012-03-01 12:57:46 +0800381static void
Ossama Othmana50e6e42013-05-14 09:48:26 -0700382shell_configuration(struct desktop_shell *shell, int config_fd)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200383{
Pekka Paalanen7296e792011-12-07 16:22:00 +0200384 char *path = NULL;
385 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200386 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300387 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800388 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200389
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400390 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300391 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800392 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200393 { "num-workspaces",
394 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200395 };
396
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400397 struct config_key saver_keys[] = {
398 { "path", CONFIG_KEY_STRING, &path },
399 { "duration", CONFIG_KEY_INTEGER, &duration },
400 };
401
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200402 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400403 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200404 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
405 };
406
Ossama Othmana50e6e42013-05-14 09:48:26 -0700407 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200408
Pekka Paalanen7296e792011-12-07 16:22:00 +0200409 shell->screensaver.path = path;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200410 shell->screensaver.duration = duration * 1000;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300411 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800412 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200413 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
414}
415
416static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200417focus_state_destroy(struct focus_state *state)
418{
419 wl_list_remove(&state->seat_destroy_listener.link);
420 wl_list_remove(&state->surface_destroy_listener.link);
421 free(state);
422}
423
424static void
425focus_state_seat_destroy(struct wl_listener *listener, void *data)
426{
427 struct focus_state *state = container_of(listener,
428 struct focus_state,
429 seat_destroy_listener);
430
431 wl_list_remove(&state->link);
432 focus_state_destroy(state);
433}
434
435static void
436focus_state_surface_destroy(struct wl_listener *listener, void *data)
437{
438 struct focus_state *state = container_of(listener,
439 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400440 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400441 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300442 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400443 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200444
Pekka Paalanen01388e22013-04-25 13:57:44 +0300445 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
446
Kristian Høgsberge3778222012-07-31 17:29:30 -0400447 next = NULL;
448 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300449 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400450 continue;
451
452 next = surface;
453 break;
454 }
455
Pekka Paalanen01388e22013-04-25 13:57:44 +0300456 /* if the focus was a sub-surface, activate its main surface */
457 if (main_surface != state->keyboard_focus)
458 next = main_surface;
459
Kristian Høgsberge3778222012-07-31 17:29:30 -0400460 if (next) {
461 shell = state->seat->compositor->shell_interface.shell;
462 activate(shell, next, state->seat);
463 } else {
464 wl_list_remove(&state->link);
465 focus_state_destroy(state);
466 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200467}
468
469static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400470focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200471{
Jonas Ådahl04769742012-06-13 00:01:24 +0200472 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200473
474 state = malloc(sizeof *state);
475 if (state == NULL)
476 return NULL;
477
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400478 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200479 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400480 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200481
482 state->seat_destroy_listener.notify = focus_state_seat_destroy;
483 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400484 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200485 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400486 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200487
488 return state;
489}
490
Jonas Ådahl8538b222012-08-29 22:13:03 +0200491static struct focus_state *
492ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
493{
494 struct workspace *ws = get_current_workspace(shell);
495 struct focus_state *state;
496
497 wl_list_for_each(state, &ws->focus_list, link)
498 if (state->seat == seat)
499 break;
500
501 if (&state->link == &ws->focus_list)
502 state = focus_state_create(seat, ws);
503
504 return state;
505}
506
Jonas Ådahl04769742012-06-13 00:01:24 +0200507static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400508restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200509{
510 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400511 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200512
513 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400514 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200515
Kristian Høgsberge3148752013-05-06 23:19:49 -0400516 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200517 }
518}
519
520static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200521replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
522 struct weston_seat *seat)
523{
524 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400525 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200526
527 wl_list_for_each(state, &ws->focus_list, link) {
528 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400529 surface = seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200530 state->keyboard_focus =
531 (struct weston_surface *) surface;
532 return;
533 }
534 }
535}
536
537static void
538drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
539 struct weston_surface *surface)
540{
541 struct focus_state *state;
542
543 wl_list_for_each(state, &ws->focus_list, link)
544 if (state->keyboard_focus == surface)
545 state->keyboard_focus = NULL;
546}
547
548static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200549workspace_destroy(struct workspace *ws)
550{
Jonas Ådahl04769742012-06-13 00:01:24 +0200551 struct focus_state *state, *next;
552
553 wl_list_for_each_safe(state, next, &ws->focus_list, link)
554 focus_state_destroy(state);
555
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200556 free(ws);
557}
558
Jonas Ådahl04769742012-06-13 00:01:24 +0200559static void
560seat_destroyed(struct wl_listener *listener, void *data)
561{
562 struct weston_seat *seat = data;
563 struct focus_state *state, *next;
564 struct workspace *ws = container_of(listener,
565 struct workspace,
566 seat_destroyed_listener);
567
568 wl_list_for_each_safe(state, next, &ws->focus_list, link)
569 if (state->seat == seat)
570 wl_list_remove(&state->link);
571}
572
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200573static struct workspace *
574workspace_create(void)
575{
576 struct workspace *ws = malloc(sizeof *ws);
577 if (ws == NULL)
578 return NULL;
579
580 weston_layer_init(&ws->layer, NULL);
581
Jonas Ådahl04769742012-06-13 00:01:24 +0200582 wl_list_init(&ws->focus_list);
583 wl_list_init(&ws->seat_destroyed_listener.link);
584 ws->seat_destroyed_listener.notify = seat_destroyed;
585
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200586 return ws;
587}
588
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200589static int
590workspace_is_empty(struct workspace *ws)
591{
592 return wl_list_empty(&ws->layer.surface_list);
593}
594
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200595static struct workspace *
596get_workspace(struct desktop_shell *shell, unsigned int index)
597{
598 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200599 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200600 pws += index;
601 return *pws;
602}
603
604static struct workspace *
605get_current_workspace(struct desktop_shell *shell)
606{
607 return get_workspace(shell, shell->workspaces.current);
608}
609
610static void
611activate_workspace(struct desktop_shell *shell, unsigned int index)
612{
613 struct workspace *ws;
614
615 ws = get_workspace(shell, index);
616 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
617
618 shell->workspaces.current = index;
619}
620
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200621static unsigned int
622get_output_height(struct weston_output *output)
623{
624 return abs(output->region.extents.y1 - output->region.extents.y2);
625}
626
627static void
628surface_translate(struct weston_surface *surface, double d)
629{
630 struct shell_surface *shsurf = get_shell_surface(surface);
631 struct weston_transform *transform;
632
633 transform = &shsurf->workspace_transform;
634 if (wl_list_empty(&transform->link))
635 wl_list_insert(surface->geometry.transformation_list.prev,
636 &shsurf->workspace_transform.link);
637
638 weston_matrix_init(&shsurf->workspace_transform.matrix);
639 weston_matrix_translate(&shsurf->workspace_transform.matrix,
640 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200641 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200642}
643
644static void
645workspace_translate_out(struct workspace *ws, double fraction)
646{
647 struct weston_surface *surface;
648 unsigned int height;
649 double d;
650
651 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
652 height = get_output_height(surface->output);
653 d = height * fraction;
654
655 surface_translate(surface, d);
656 }
657}
658
659static void
660workspace_translate_in(struct workspace *ws, double fraction)
661{
662 struct weston_surface *surface;
663 unsigned int height;
664 double d;
665
666 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
667 height = get_output_height(surface->output);
668
669 if (fraction > 0)
670 d = -(height - height * fraction);
671 else
672 d = height + height * fraction;
673
674 surface_translate(surface, d);
675 }
676}
677
678static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200679broadcast_current_workspace_state(struct desktop_shell *shell)
680{
681 struct wl_resource *resource;
682
683 wl_list_for_each(resource, &shell->workspaces.client_list, link)
684 workspace_manager_send_state(resource,
685 shell->workspaces.current,
686 shell->workspaces.num);
687}
688
689static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200690reverse_workspace_change_animation(struct desktop_shell *shell,
691 unsigned int index,
692 struct workspace *from,
693 struct workspace *to)
694{
695 shell->workspaces.current = index;
696
697 shell->workspaces.anim_to = to;
698 shell->workspaces.anim_from = from;
699 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
700 shell->workspaces.anim_timestamp = 0;
701
Scott Moreau4272e632012-08-13 09:58:41 -0600702 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200703}
704
705static void
706workspace_deactivate_transforms(struct workspace *ws)
707{
708 struct weston_surface *surface;
709 struct shell_surface *shsurf;
710
711 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
712 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200713 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
714 wl_list_remove(&shsurf->workspace_transform.link);
715 wl_list_init(&shsurf->workspace_transform.link);
716 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200717 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200718 }
719}
720
721static void
722finish_workspace_change_animation(struct desktop_shell *shell,
723 struct workspace *from,
724 struct workspace *to)
725{
Scott Moreau4272e632012-08-13 09:58:41 -0600726 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200727
728 wl_list_remove(&shell->workspaces.animation.link);
729 workspace_deactivate_transforms(from);
730 workspace_deactivate_transforms(to);
731 shell->workspaces.anim_to = NULL;
732
733 wl_list_remove(&shell->workspaces.anim_from->layer.link);
734}
735
736static void
737animate_workspace_change_frame(struct weston_animation *animation,
738 struct weston_output *output, uint32_t msecs)
739{
740 struct desktop_shell *shell =
741 container_of(animation, struct desktop_shell,
742 workspaces.animation);
743 struct workspace *from = shell->workspaces.anim_from;
744 struct workspace *to = shell->workspaces.anim_to;
745 uint32_t t;
746 double x, y;
747
748 if (workspace_is_empty(from) && workspace_is_empty(to)) {
749 finish_workspace_change_animation(shell, from, to);
750 return;
751 }
752
753 if (shell->workspaces.anim_timestamp == 0) {
754 if (shell->workspaces.anim_current == 0.0)
755 shell->workspaces.anim_timestamp = msecs;
756 else
757 shell->workspaces.anim_timestamp =
758 msecs -
759 /* Invers of movement function 'y' below. */
760 (asin(1.0 - shell->workspaces.anim_current) *
761 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
762 M_2_PI);
763 }
764
765 t = msecs - shell->workspaces.anim_timestamp;
766
767 /*
768 * x = [0, π/2]
769 * y(x) = sin(x)
770 */
771 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
772 y = sin(x);
773
774 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600775 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200776
777 workspace_translate_out(from, shell->workspaces.anim_dir * y);
778 workspace_translate_in(to, shell->workspaces.anim_dir * y);
779 shell->workspaces.anim_current = y;
780
Scott Moreau4272e632012-08-13 09:58:41 -0600781 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200782 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200783 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200784 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200785}
786
787static void
788animate_workspace_change(struct desktop_shell *shell,
789 unsigned int index,
790 struct workspace *from,
791 struct workspace *to)
792{
793 struct weston_output *output;
794
795 int dir;
796
797 if (index > shell->workspaces.current)
798 dir = -1;
799 else
800 dir = 1;
801
802 shell->workspaces.current = index;
803
804 shell->workspaces.anim_dir = dir;
805 shell->workspaces.anim_from = from;
806 shell->workspaces.anim_to = to;
807 shell->workspaces.anim_current = 0.0;
808 shell->workspaces.anim_timestamp = 0;
809
810 output = container_of(shell->compositor->output_list.next,
811 struct weston_output, link);
812 wl_list_insert(&output->animation_list,
813 &shell->workspaces.animation.link);
814
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200815 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200816
817 workspace_translate_in(to, 0);
818
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400819 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200820
Scott Moreau4272e632012-08-13 09:58:41 -0600821 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200822}
823
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200824static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200825update_workspace(struct desktop_shell *shell, unsigned int index,
826 struct workspace *from, struct workspace *to)
827{
828 shell->workspaces.current = index;
829 wl_list_insert(&from->layer.link, &to->layer.link);
830 wl_list_remove(&from->layer.link);
831}
832
833static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200834change_workspace(struct desktop_shell *shell, unsigned int index)
835{
836 struct workspace *from;
837 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200838
839 if (index == shell->workspaces.current)
840 return;
841
842 /* Don't change workspace when there is any fullscreen surfaces. */
843 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
844 return;
845
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200846 from = get_current_workspace(shell);
847 to = get_workspace(shell, index);
848
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200849 if (shell->workspaces.anim_from == to &&
850 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200851 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200852 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200853 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200854 return;
855 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200856
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857 if (shell->workspaces.anim_to != NULL)
858 finish_workspace_change_animation(shell,
859 shell->workspaces.anim_from,
860 shell->workspaces.anim_to);
861
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200862 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200863
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200864 if (workspace_is_empty(to) && workspace_is_empty(from))
865 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200866 else
867 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200868
869 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200870}
871
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200872static bool
873workspace_has_only(struct workspace *ws, struct weston_surface *surface)
874{
875 struct wl_list *list = &ws->layer.surface_list;
876 struct wl_list *e;
877
878 if (wl_list_empty(list))
879 return false;
880
881 e = list->next;
882
883 if (e->next != list)
884 return false;
885
886 return container_of(e, struct weston_surface, layer_link) == surface;
887}
888
889static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200890move_surface_to_workspace(struct desktop_shell *shell,
891 struct weston_surface *surface,
892 uint32_t workspace)
893{
894 struct workspace *from;
895 struct workspace *to;
896 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300897 struct weston_surface *focus;
898
899 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900
901 if (workspace == shell->workspaces.current)
902 return;
903
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200904 if (workspace >= shell->workspaces.num)
905 workspace = shell->workspaces.num - 1;
906
Jonas Ådahle9d22502012-08-29 22:13:01 +0200907 from = get_current_workspace(shell);
908 to = get_workspace(shell, workspace);
909
910 wl_list_remove(&surface->layer_link);
911 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
912
913 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300914 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
915 if (!seat->keyboard)
916 continue;
917
918 focus = weston_surface_get_main_surface(seat->keyboard->focus);
919 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400920 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300921 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200922
923 weston_surface_damage_below(surface);
924}
925
926static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200927take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400928 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929 unsigned int index)
930{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300931 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 struct shell_surface *shsurf;
933 struct workspace *from;
934 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200935 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200936
Pekka Paalanen01388e22013-04-25 13:57:44 +0300937 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200938 if (surface == NULL ||
939 index == shell->workspaces.current)
940 return;
941
942 from = get_current_workspace(shell);
943 to = get_workspace(shell, index);
944
945 wl_list_remove(&surface->layer_link);
946 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
947
Jonas Ådahle9d22502012-08-29 22:13:01 +0200948 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200949 drop_focus_state(shell, from, surface);
950
951 if (shell->workspaces.anim_from == to &&
952 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200953 wl_list_remove(&to->layer.link);
954 wl_list_insert(from->layer.link.prev, &to->layer.link);
955
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200956 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200957 broadcast_current_workspace_state(shell);
958
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200959 return;
960 }
961
962 if (shell->workspaces.anim_to != NULL)
963 finish_workspace_change_animation(shell,
964 shell->workspaces.anim_from,
965 shell->workspaces.anim_to);
966
967 if (workspace_is_empty(from) &&
968 workspace_has_only(to, surface))
969 update_workspace(shell, index, from, to);
970 else {
971 shsurf = get_shell_surface(surface);
972 if (wl_list_empty(&shsurf->workspace_transform.link))
973 wl_list_insert(&shell->workspaces.anim_sticky_list,
974 &shsurf->workspace_transform.link);
975
976 animate_workspace_change(shell, index, from, to);
977 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200978
979 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200980
981 state = ensure_focus_state(shell, seat);
982 if (state != NULL)
983 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200984}
985
986static void
987workspace_manager_move_surface(struct wl_client *client,
988 struct wl_resource *resource,
989 struct wl_resource *surface_resource,
990 uint32_t workspace)
991{
992 struct desktop_shell *shell = resource->data;
993 struct weston_surface *surface =
994 (struct weston_surface *) surface_resource;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300995 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200996
Pekka Paalanen01388e22013-04-25 13:57:44 +0300997 main_surface = weston_surface_get_main_surface(surface);
998 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200999}
1000
1001static const struct workspace_manager_interface workspace_manager_implementation = {
1002 workspace_manager_move_surface,
1003};
1004
1005static void
1006unbind_resource(struct wl_resource *resource)
1007{
1008 wl_list_remove(&resource->link);
1009 free(resource);
1010}
1011
1012static void
1013bind_workspace_manager(struct wl_client *client,
1014 void *data, uint32_t version, uint32_t id)
1015{
1016 struct desktop_shell *shell = data;
1017 struct wl_resource *resource;
1018
1019 resource = wl_client_add_object(client, &workspace_manager_interface,
1020 &workspace_manager_implementation,
1021 id, shell);
1022
1023 if (resource == NULL) {
1024 weston_log("couldn't add workspace manager object");
1025 return;
1026 }
1027
1028 resource->destroy = unbind_resource;
1029 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1030
1031 workspace_manager_send_state(resource,
1032 shell->workspaces.current,
1033 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001034}
1035
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001036static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001037noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001038{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001039}
1040
1041static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001042move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001043{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001044 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001045 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001046 struct shell_surface *shsurf = move->base.shsurf;
1047 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001048 int dx = wl_fixed_to_int(pointer->x + move->dx);
1049 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001050
1051 if (!shsurf)
1052 return;
1053
1054 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001055
Daniel Stone103db7f2012-05-08 17:17:55 +01001056 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001057 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001058
1059 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001060}
1061
1062static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001063move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001064 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001065{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001066 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1067 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001068 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001069 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001070
Daniel Stone4dbadb12012-05-30 16:31:51 +01001071 if (pointer->button_count == 0 &&
1072 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001073 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001074 free(grab);
1075 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001076}
1077
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001078static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001079 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001080 move_grab_motion,
1081 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001082};
1083
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001084static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001085surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001086{
1087 struct weston_move_grab *move;
1088
1089 if (!shsurf)
1090 return -1;
1091
1092 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1093 return 0;
1094
1095 move = malloc(sizeof *move);
1096 if (!move)
1097 return -1;
1098
1099 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001100 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001101 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001102 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001103
1104 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001105 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001106
1107 return 0;
1108}
1109
1110static void
1111shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1112 struct wl_resource *seat_resource, uint32_t serial)
1113{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001114 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001115 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001116 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001117
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001118 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001119 if (seat->pointer->button_count == 0 ||
1120 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001121 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001122 return;
1123
Kristian Høgsberge3148752013-05-06 23:19:49 -04001124 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001125 wl_resource_post_no_memory(resource);
1126}
1127
1128struct weston_resize_grab {
1129 struct shell_grab base;
1130 uint32_t edges;
1131 int32_t width, height;
1132};
1133
1134static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001135resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001136{
1137 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001138 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001139 struct shell_surface *shsurf = resize->base.shsurf;
1140 int32_t width, height;
1141 wl_fixed_t from_x, from_y;
1142 wl_fixed_t to_x, to_y;
1143
1144 if (!shsurf)
1145 return;
1146
1147 weston_surface_from_global_fixed(shsurf->surface,
1148 pointer->grab_x, pointer->grab_y,
1149 &from_x, &from_y);
1150 weston_surface_from_global_fixed(shsurf->surface,
1151 pointer->x, pointer->y, &to_x, &to_y);
1152
1153 width = resize->width;
1154 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1155 width += wl_fixed_to_int(from_x - to_x);
1156 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1157 width += wl_fixed_to_int(to_x - from_x);
1158 }
1159
1160 height = resize->height;
1161 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1162 height += wl_fixed_to_int(from_y - to_y);
1163 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1164 height += wl_fixed_to_int(to_y - from_y);
1165 }
1166
1167 shsurf->client->send_configure(shsurf->surface,
1168 resize->edges, width, height);
1169}
1170
1171static void
1172send_configure(struct weston_surface *surface,
1173 uint32_t edges, int32_t width, int32_t height)
1174{
1175 struct shell_surface *shsurf = get_shell_surface(surface);
1176
1177 wl_shell_surface_send_configure(&shsurf->resource,
1178 edges, width, height);
1179}
1180
1181static const struct weston_shell_client shell_client = {
1182 send_configure
1183};
1184
1185static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001186resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001187 uint32_t time, uint32_t button, uint32_t state_w)
1188{
1189 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001190 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001191 enum wl_pointer_button_state state = state_w;
1192
1193 if (pointer->button_count == 0 &&
1194 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1195 shell_grab_end(&resize->base);
1196 free(grab);
1197 }
1198}
1199
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001200static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001201 noop_grab_focus,
1202 resize_grab_motion,
1203 resize_grab_button,
1204};
1205
Giulio Camuffob8366642013-04-25 13:57:46 +03001206/*
1207 * Returns the bounding box of a surface and all its sub-surfaces,
1208 * in the surface coordinates system. */
1209static void
1210surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1211 int32_t *y, int32_t *w, int32_t *h) {
1212 pixman_region32_t region;
1213 pixman_box32_t *box;
1214 struct weston_subsurface *subsurface;
1215
1216 pixman_region32_init_rect(&region, 0, 0,
1217 surface->geometry.width,
1218 surface->geometry.height);
1219
1220 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1221 pixman_region32_union_rect(&region, &region,
1222 subsurface->position.x,
1223 subsurface->position.y,
1224 subsurface->surface->geometry.width,
1225 subsurface->surface->geometry.height);
1226 }
1227
1228 box = pixman_region32_extents(&region);
1229 if (x)
1230 *x = box->x1;
1231 if (y)
1232 *y = box->y1;
1233 if (w)
1234 *w = box->x2 - box->x1;
1235 if (h)
1236 *h = box->y2 - box->y1;
1237
1238 pixman_region32_fini(&region);
1239}
1240
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001241static int
1242surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001243 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001244{
1245 struct weston_resize_grab *resize;
1246
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001247 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1248 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001249 return 0;
1250
1251 if (edges == 0 || edges > 15 ||
1252 (edges & 3) == 3 || (edges & 12) == 12)
1253 return 0;
1254
1255 resize = malloc(sizeof *resize);
1256 if (!resize)
1257 return -1;
1258
1259 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001260 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1261 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001262
1263 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001264 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001265
1266 return 0;
1267}
1268
1269static void
1270shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1271 struct wl_resource *seat_resource, uint32_t serial,
1272 uint32_t edges)
1273{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001274 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001275 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001276 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001277
1278 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1279 return;
1280
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001281 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001282 if (seat->pointer->button_count == 0 ||
1283 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001284 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001285 return;
1286
Kristian Høgsberge3148752013-05-06 23:19:49 -04001287 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001288 wl_resource_post_no_memory(resource);
1289}
1290
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001291static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001292busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001293{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001294 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001295 struct weston_pointer *pointer = base->pointer;
1296 struct weston_surface *surface;
1297 wl_fixed_t sx, sy;
1298
1299 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1300 pointer->x, pointer->y,
1301 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001302
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001303 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001304 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001305 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001306 }
1307}
1308
1309static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001310busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001311{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001312}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001313
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001314static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001315busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001316 uint32_t time, uint32_t button, uint32_t state)
1317{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001318 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001319 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001320 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001321
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001322 if (shsurf && button == BTN_LEFT && state) {
1323 activate(shsurf->shell, shsurf->surface, seat);
1324 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001325 } else if (shsurf && button == BTN_RIGHT && state) {
1326 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001327 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001328 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001329}
1330
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001331static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001332 busy_cursor_grab_focus,
1333 busy_cursor_grab_motion,
1334 busy_cursor_grab_button,
1335};
1336
1337static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001338set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001339{
1340 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001341
1342 grab = malloc(sizeof *grab);
1343 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001344 return;
1345
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001346 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1347 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001348}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001349
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001350static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001351end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001352{
1353 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1354
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001355 if (grab->grab.interface == &busy_cursor_grab_interface &&
1356 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001357 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001358 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001359 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001360}
1361
Scott Moreau9521d5e2012-04-19 13:06:17 -06001362static void
1363ping_timer_destroy(struct shell_surface *shsurf)
1364{
1365 if (!shsurf || !shsurf->ping_timer)
1366 return;
1367
1368 if (shsurf->ping_timer->source)
1369 wl_event_source_remove(shsurf->ping_timer->source);
1370
1371 free(shsurf->ping_timer);
1372 shsurf->ping_timer = NULL;
1373}
1374
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001375static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001376ping_timeout_handler(void *data)
1377{
1378 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001379 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001380
Scott Moreau9521d5e2012-04-19 13:06:17 -06001381 /* Client is not responding */
1382 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001383
1384 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001385 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001386 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001387
1388 return 1;
1389}
1390
1391static void
1392ping_handler(struct weston_surface *surface, uint32_t serial)
1393{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001394 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001395 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001396 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001397
1398 if (!shsurf)
1399 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001400 if (!shsurf->resource.client)
1401 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001402
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001403 if (shsurf->surface == shsurf->shell->grab_surface)
1404 return;
1405
Scott Moreauff1db4a2012-04-17 19:06:18 -06001406 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001407 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001408 if (!shsurf->ping_timer)
1409 return;
1410
1411 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001412 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1413 shsurf->ping_timer->source =
1414 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1415 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1416
1417 wl_shell_surface_send_ping(&shsurf->resource, serial);
1418 }
1419}
1420
1421static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001422handle_pointer_focus(struct wl_listener *listener, void *data)
1423{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001424 struct weston_pointer *pointer = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001425 struct weston_surface *surface =
1426 (struct weston_surface *) pointer->focus;
1427 struct weston_compositor *compositor;
1428 struct shell_surface *shsurf;
1429 uint32_t serial;
1430
1431 if (!surface)
1432 return;
1433
1434 compositor = surface->compositor;
1435 shsurf = get_shell_surface(surface);
1436
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001437 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001438 set_busy_cursor(shsurf, pointer);
1439 } else {
1440 serial = wl_display_next_serial(compositor->wl_display);
1441 ping_handler(surface, serial);
1442 }
1443}
1444
1445static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001446create_pointer_focus_listener(struct weston_seat *seat)
1447{
1448 struct wl_listener *listener;
1449
Kristian Høgsberge3148752013-05-06 23:19:49 -04001450 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001451 return;
1452
1453 listener = malloc(sizeof *listener);
1454 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001455 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001456}
1457
1458static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001459shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1460 uint32_t serial)
1461{
1462 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001463 struct weston_seat *seat;
1464 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001465
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001466 if (shsurf->ping_timer == NULL)
1467 /* Just ignore unsolicited pong. */
1468 return;
1469
Scott Moreauff1db4a2012-04-17 19:06:18 -06001470 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001471 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001472 wl_list_for_each(seat, &ec->seat_list, link) {
1473 if(seat->pointer)
1474 end_busy_cursor(shsurf, seat->pointer);
1475 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001476 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001477 }
1478}
1479
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001480static void
1481shell_surface_set_title(struct wl_client *client,
1482 struct wl_resource *resource, const char *title)
1483{
1484 struct shell_surface *shsurf = resource->data;
1485
1486 free(shsurf->title);
1487 shsurf->title = strdup(title);
1488}
1489
1490static void
1491shell_surface_set_class(struct wl_client *client,
1492 struct wl_resource *resource, const char *class)
1493{
1494 struct shell_surface *shsurf = resource->data;
1495
1496 free(shsurf->class);
1497 shsurf->class = strdup(class);
1498}
1499
Juan Zhao96879df2012-02-07 08:45:41 +08001500static struct weston_output *
1501get_default_output(struct weston_compositor *compositor)
1502{
1503 return container_of(compositor->output_list.next,
1504 struct weston_output, link);
1505}
1506
Alex Wu4539b082012-03-01 12:57:46 +08001507static void
1508shell_unset_fullscreen(struct shell_surface *shsurf)
1509{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001510 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001511 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001512 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1513 shell_surface_is_top_fullscreen(shsurf)) {
1514 weston_output_switch_mode(shsurf->fullscreen_output,
1515 shsurf->fullscreen_output->origin);
1516 }
Alex Wu4539b082012-03-01 12:57:46 +08001517 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1518 shsurf->fullscreen.framerate = 0;
1519 wl_list_remove(&shsurf->fullscreen.transform.link);
1520 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001521 if (shsurf->fullscreen.black_surface)
1522 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001523 shsurf->fullscreen.black_surface = NULL;
1524 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001525 weston_surface_set_position(shsurf->surface,
1526 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001527 if (shsurf->saved_rotation_valid) {
1528 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1529 &shsurf->rotation.transform.link);
1530 shsurf->saved_rotation_valid = false;
1531 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001532
1533 ws = get_current_workspace(shsurf->shell);
1534 wl_list_remove(&shsurf->surface->layer_link);
1535 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001536}
1537
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001538static void
1539shell_unset_maximized(struct shell_surface *shsurf)
1540{
1541 struct workspace *ws;
1542 /* undo all maximized things here */
1543 shsurf->output = get_default_output(shsurf->surface->compositor);
1544 weston_surface_set_position(shsurf->surface,
1545 shsurf->saved_x,
1546 shsurf->saved_y);
1547
1548 if (shsurf->saved_rotation_valid) {
1549 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1550 &shsurf->rotation.transform.link);
1551 shsurf->saved_rotation_valid = false;
1552 }
1553
1554 ws = get_current_workspace(shsurf->shell);
1555 wl_list_remove(&shsurf->surface->layer_link);
1556 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1557}
1558
Pekka Paalanen98262232011-12-01 10:42:22 +02001559static int
1560reset_shell_surface_type(struct shell_surface *surface)
1561{
1562 switch (surface->type) {
1563 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001564 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001565 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001566 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001567 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001568 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001569 case SHELL_SURFACE_NONE:
1570 case SHELL_SURFACE_TOPLEVEL:
1571 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001572 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001573 break;
1574 }
1575
1576 surface->type = SHELL_SURFACE_NONE;
1577 return 0;
1578}
1579
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001580static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001581set_surface_type(struct shell_surface *shsurf)
1582{
1583 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001584 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001585
1586 reset_shell_surface_type(shsurf);
1587
1588 shsurf->type = shsurf->next_type;
1589 shsurf->next_type = SHELL_SURFACE_NONE;
1590
1591 switch (shsurf->type) {
1592 case SHELL_SURFACE_TOPLEVEL:
1593 break;
1594 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001595 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001596 pes->geometry.x + shsurf->transient.x,
1597 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001598 break;
1599
1600 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001601 case SHELL_SURFACE_FULLSCREEN:
1602 shsurf->saved_x = surface->geometry.x;
1603 shsurf->saved_y = surface->geometry.y;
1604 shsurf->saved_position_valid = true;
1605
1606 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1607 wl_list_remove(&shsurf->rotation.transform.link);
1608 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001609 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001610 shsurf->saved_rotation_valid = true;
1611 }
1612 break;
1613
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001614 default:
1615 break;
1616 }
1617}
1618
1619static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001620set_toplevel(struct shell_surface *shsurf)
1621{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001622 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001623}
1624
1625static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001626shell_surface_set_toplevel(struct wl_client *client,
1627 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001628{
Pekka Paalanen98262232011-12-01 10:42:22 +02001629 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001630
Tiago Vignattibc052c92012-04-19 16:18:18 +03001631 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001632}
1633
1634static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001635set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001636 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001637{
1638 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001639 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001640 shsurf->transient.x = x;
1641 shsurf->transient.y = y;
1642 shsurf->transient.flags = flags;
1643 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1644}
1645
1646static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001647shell_surface_set_transient(struct wl_client *client,
1648 struct wl_resource *resource,
1649 struct wl_resource *parent_resource,
1650 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001651{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001652 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001653 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001654
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001655 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001656}
1657
Tiago Vignattibe143262012-04-16 17:31:41 +03001658static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001659shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001660{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001661 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001662}
1663
1664static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001665get_output_panel_height(struct desktop_shell *shell,
1666 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001667{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001668 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001669 int panel_height = 0;
1670
1671 if (!output)
1672 return 0;
1673
Juan Zhao4ab94682012-07-09 22:24:09 -07001674 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001675 if (surface->output == output) {
1676 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001677 break;
1678 }
1679 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001680
Juan Zhao96879df2012-02-07 08:45:41 +08001681 return panel_height;
1682}
1683
1684static void
1685shell_surface_set_maximized(struct wl_client *client,
1686 struct wl_resource *resource,
1687 struct wl_resource *output_resource )
1688{
1689 struct shell_surface *shsurf = resource->data;
1690 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001691 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001692 uint32_t edges = 0, panel_height = 0;
1693
1694 /* get the default output, if the client set it as NULL
1695 check whether the ouput is available */
1696 if (output_resource)
1697 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001698 else if (es->output)
1699 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001700 else
1701 shsurf->output = get_default_output(es->compositor);
1702
Tiago Vignattibe143262012-04-16 17:31:41 +03001703 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001704 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001705 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001706
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001707 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001708 shsurf->output->width,
1709 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001710
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001711 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001712}
1713
Alex Wu21858432012-04-01 20:13:08 +08001714static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001715black_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 +08001716
Alex Wu4539b082012-03-01 12:57:46 +08001717static struct weston_surface *
1718create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001719 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001720 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001721{
1722 struct weston_surface *surface = NULL;
1723
1724 surface = weston_surface_create(ec);
1725 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001726 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001727 return NULL;
1728 }
1729
Alex Wu21858432012-04-01 20:13:08 +08001730 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001731 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001732 weston_surface_configure(surface, x, y, w, h);
1733 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001734 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001735 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001736 pixman_region32_fini(&surface->input);
1737 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001738
Alex Wu4539b082012-03-01 12:57:46 +08001739 return surface;
1740}
1741
1742/* Create black surface and append it to the associated fullscreen surface.
1743 * Handle size dismatch and positioning according to the method. */
1744static void
1745shell_configure_fullscreen(struct shell_surface *shsurf)
1746{
1747 struct weston_output *output = shsurf->fullscreen_output;
1748 struct weston_surface *surface = shsurf->surface;
1749 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001750 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001751 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001752
Alex Wu4539b082012-03-01 12:57:46 +08001753 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001754 shsurf->fullscreen.black_surface =
1755 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001756 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001757 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001758 output->width,
1759 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001760
1761 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1762 wl_list_insert(&surface->layer_link,
1763 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001764 shsurf->fullscreen.black_surface->output = output;
1765
Giulio Camuffob8366642013-04-25 13:57:46 +03001766 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1767 &surf_width, &surf_height);
1768
Alex Wu4539b082012-03-01 12:57:46 +08001769 switch (shsurf->fullscreen.type) {
1770 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001771 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001772 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001773 break;
1774 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001775 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001776 if (output->width == surf_width &&
1777 output->height == surf_height) {
1778 weston_surface_set_position(surface, output->x - surf_x,
1779 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001780 break;
1781 }
1782
Alex Wu4539b082012-03-01 12:57:46 +08001783 matrix = &shsurf->fullscreen.transform.matrix;
1784 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001785
Scott Moreau1bad5db2012-08-18 01:04:05 -06001786 output_aspect = (float) output->width /
1787 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001788 surface_aspect = (float) surface->geometry.width /
1789 (float) surface->geometry.height;
1790 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001791 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001792 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001793 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001794 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001795 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001796
Alex Wu4539b082012-03-01 12:57:46 +08001797 weston_matrix_scale(matrix, scale, scale, 1);
1798 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001799 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001800 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001801 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1802 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001803 weston_surface_set_position(surface, x, y);
1804
Alex Wu4539b082012-03-01 12:57:46 +08001805 break;
1806 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001807 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001808 struct weston_mode mode = {0,
Giulio Camuffob8366642013-04-25 13:57:46 +03001809 surf_width,
1810 surf_height,
Alex Wubd3354b2012-04-17 17:20:49 +08001811 shsurf->fullscreen.framerate};
1812
1813 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001814 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001815 output->x - surf_x,
1816 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001817 output->width,
1818 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001819 break;
1820 }
1821 }
Alex Wu4539b082012-03-01 12:57:46 +08001822 break;
1823 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1824 break;
1825 default:
1826 break;
1827 }
1828}
1829
1830/* make the fullscreen and black surface at the top */
1831static void
1832shell_stack_fullscreen(struct shell_surface *shsurf)
1833{
Alex Wubd3354b2012-04-17 17:20:49 +08001834 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001835 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001836 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001837
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001838 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001839 wl_list_insert(&shell->fullscreen_layer.surface_list,
1840 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001841 weston_surface_damage(surface);
1842
1843 if (!shsurf->fullscreen.black_surface)
1844 shsurf->fullscreen.black_surface =
1845 create_black_surface(surface->compositor,
1846 surface,
1847 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001848 output->width,
1849 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001850
1851 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001852 wl_list_insert(&surface->layer_link,
1853 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001854 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001855}
1856
1857static void
1858shell_map_fullscreen(struct shell_surface *shsurf)
1859{
Alex Wu4539b082012-03-01 12:57:46 +08001860 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001861 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001862}
1863
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001864static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001865set_fullscreen(struct shell_surface *shsurf,
1866 uint32_t method,
1867 uint32_t framerate,
1868 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001869{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001870 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001871
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001872 if (output)
1873 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001874 else if (es->output)
1875 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001876 else
1877 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001878
Alex Wu4539b082012-03-01 12:57:46 +08001879 shsurf->fullscreen_output = shsurf->output;
1880 shsurf->fullscreen.type = method;
1881 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001882 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001883
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001884 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001885 shsurf->output->width,
1886 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001887}
1888
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001889static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001890shell_surface_set_fullscreen(struct wl_client *client,
1891 struct wl_resource *resource,
1892 uint32_t method,
1893 uint32_t framerate,
1894 struct wl_resource *output_resource)
1895{
1896 struct shell_surface *shsurf = resource->data;
1897 struct weston_output *output;
1898
1899 if (output_resource)
1900 output = output_resource->data;
1901 else
1902 output = NULL;
1903
1904 set_fullscreen(shsurf, method, framerate, output);
1905}
1906
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001907static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001908
1909static void
1910destroy_shell_seat(struct wl_listener *listener, void *data)
1911{
1912 struct shell_seat *shseat =
1913 container_of(listener,
1914 struct shell_seat, seat_destroy_listener);
1915 struct shell_surface *shsurf, *prev = NULL;
1916
1917 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001918 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001919 shseat->popup_grab.client = NULL;
1920
1921 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1922 shsurf->popup.shseat = NULL;
1923 if (prev) {
1924 wl_list_init(&prev->popup.grab_link);
1925 }
1926 prev = shsurf;
1927 }
1928 wl_list_init(&prev->popup.grab_link);
1929 }
1930
1931 wl_list_remove(&shseat->seat_destroy_listener.link);
1932 free(shseat);
1933}
1934
1935static struct shell_seat *
1936create_shell_seat(struct weston_seat *seat)
1937{
1938 struct shell_seat *shseat;
1939
1940 shseat = calloc(1, sizeof *shseat);
1941 if (!shseat) {
1942 weston_log("no memory to allocate shell seat\n");
1943 return NULL;
1944 }
1945
1946 shseat->seat = seat;
1947 wl_list_init(&shseat->popup_grab.surfaces_list);
1948
1949 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1950 wl_signal_add(&seat->destroy_signal,
1951 &shseat->seat_destroy_listener);
1952
1953 return shseat;
1954}
1955
1956static struct shell_seat *
1957get_shell_seat(struct weston_seat *seat)
1958{
1959 struct wl_listener *listener;
1960
1961 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1962 if (listener == NULL)
1963 return create_shell_seat(seat);
1964
1965 return container_of(listener,
1966 struct shell_seat, seat_destroy_listener);
1967}
1968
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001969static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001970popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001971{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001972 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001973 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001974 struct shell_seat *shseat =
1975 container_of(grab, struct shell_seat, popup_grab.grab);
1976 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001977 wl_fixed_t sx, sy;
1978
1979 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1980 pointer->x, pointer->y,
1981 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001982
Pekka Paalanencb108432012-01-19 16:25:40 +02001983 if (surface && surface->resource.client == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001984 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001985 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001986 weston_pointer_set_focus(pointer, NULL,
1987 wl_fixed_from_int(0),
1988 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001989 }
1990}
1991
1992static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001993popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001994{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001995 struct weston_pointer *pointer = grab->pointer;
1996 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001997
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001998 if (pointer->focus_resource) {
1999 weston_surface_from_global_fixed(pointer->focus,
2000 pointer->x, pointer->y,
2001 &sx, &sy);
2002 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2003 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002004}
2005
2006static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002007popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002008 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002009{
2010 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002011 struct shell_seat *shseat =
2012 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002013 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002014 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002015 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002016
Daniel Stone37816df2012-05-16 18:45:18 +01002017 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002018 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002019 display = wl_client_get_display(resource->client);
2020 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002021 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002022 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002023 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002024 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002025 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002026 }
2027
Daniel Stone4dbadb12012-05-30 16:31:51 +01002028 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002029 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002030}
2031
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002032static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002033 popup_grab_focus,
2034 popup_grab_motion,
2035 popup_grab_button,
2036};
2037
2038static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002039popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002040{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002041 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002042 struct shell_seat *shseat =
2043 container_of(grab, struct shell_seat, popup_grab.grab);
2044 struct shell_surface *shsurf;
2045 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002046
2047 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002048 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002049 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002050 shseat->popup_grab.grab.interface = NULL;
2051 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002052 /* Send the popup_done event to all the popups open */
2053 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2054 wl_shell_surface_send_popup_done(&shsurf->resource);
2055 shsurf->popup.shseat = NULL;
2056 if (prev) {
2057 wl_list_init(&prev->popup.grab_link);
2058 }
2059 prev = shsurf;
2060 }
2061 wl_list_init(&prev->popup.grab_link);
2062 wl_list_init(&shseat->popup_grab.surfaces_list);
2063 }
2064}
2065
2066static void
2067add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2068{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002069 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002070
2071 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002072 shseat->popup_grab.client = shsurf->resource.client;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002073 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002074 /* We must make sure here that this popup was opened after
2075 * a mouse press, and not just by moving around with other
2076 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002077 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002078 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002079
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002080 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002081 }
2082 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2083}
2084
2085static void
2086remove_popup_grab(struct shell_surface *shsurf)
2087{
2088 struct shell_seat *shseat = shsurf->popup.shseat;
2089
2090 wl_list_remove(&shsurf->popup.grab_link);
2091 wl_list_init(&shsurf->popup.grab_link);
2092 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002093 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002094 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002095 }
2096}
2097
2098static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002099shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002100{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002101 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002102 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002103 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002104
2105 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002106
Pekka Paalanen483243f2013-03-08 14:56:50 +02002107 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002108 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2109 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002110
Kristian Høgsberge3148752013-05-06 23:19:49 -04002111 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002112 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002113 } else {
2114 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002115 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002116 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002117}
2118
2119static void
2120shell_surface_set_popup(struct wl_client *client,
2121 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002122 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002123 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002124 struct wl_resource *parent_resource,
2125 int32_t x, int32_t y, uint32_t flags)
2126{
2127 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002128
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002129 shsurf->type = SHELL_SURFACE_POPUP;
2130 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002131 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002132 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002133 shsurf->popup.x = x;
2134 shsurf->popup.y = y;
2135}
2136
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002137static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002138 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002139 shell_surface_move,
2140 shell_surface_resize,
2141 shell_surface_set_toplevel,
2142 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002143 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002144 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002145 shell_surface_set_maximized,
2146 shell_surface_set_title,
2147 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002148};
2149
2150static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002151destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002152{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002153 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2154 remove_popup_grab(shsurf);
2155 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002156
Alex Wubd3354b2012-04-17 17:20:49 +08002157 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2158 shell_surface_is_top_fullscreen(shsurf)) {
2159 weston_output_switch_mode(shsurf->fullscreen_output,
2160 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002161 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002162
Alex Wuaa08e2d2012-03-05 11:01:40 +08002163 if (shsurf->fullscreen.black_surface)
2164 weston_surface_destroy(shsurf->fullscreen.black_surface);
2165
Alex Wubd3354b2012-04-17 17:20:49 +08002166 /* As destroy_resource() use wl_list_for_each_safe(),
2167 * we can always remove the listener.
2168 */
2169 wl_list_remove(&shsurf->surface_destroy_listener.link);
2170 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002171 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002172 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002173
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002174 wl_list_remove(&shsurf->link);
2175 free(shsurf);
2176}
2177
2178static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002179shell_destroy_shell_surface(struct wl_resource *resource)
2180{
2181 struct shell_surface *shsurf = resource->data;
2182
2183 destroy_shell_surface(shsurf);
2184}
2185
2186static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002187shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002188{
2189 struct shell_surface *shsurf = container_of(listener,
2190 struct shell_surface,
2191 surface_destroy_listener);
2192
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002193 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002194 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002195 } else {
2196 wl_signal_emit(&shsurf->resource.destroy_signal,
2197 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002198 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002199 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002200}
2201
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002202static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002203shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002204
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002205static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002206get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002207{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002208 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002209 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002210 else
2211 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002212}
2213
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002214static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002215create_shell_surface(void *shell, struct weston_surface *surface,
2216 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002217{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002218 struct shell_surface *shsurf;
2219
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002220 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002221 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002222 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002223 }
2224
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002225 shsurf = calloc(1, sizeof *shsurf);
2226 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002227 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002228 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002229 }
2230
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002231 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002232 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002233
Tiago Vignattibc052c92012-04-19 16:18:18 +03002234 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002235 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002236 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002237 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002238 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002239 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2240 shsurf->fullscreen.framerate = 0;
2241 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002242 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002243 wl_list_init(&shsurf->fullscreen.transform.link);
2244
Tiago Vignattibc052c92012-04-19 16:18:18 +03002245 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002246 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002247 wl_signal_add(&surface->resource.destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002248 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002249
2250 /* init link so its safe to always remove it in destroy_shell_surface */
2251 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002252 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002253
Pekka Paalanen460099f2012-01-20 16:48:25 +02002254 /* empty when not in use */
2255 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002256 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002257
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002258 wl_list_init(&shsurf->workspace_transform.link);
2259
Pekka Paalanen98262232011-12-01 10:42:22 +02002260 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002261 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002262
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002263 shsurf->client = client;
2264
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002265 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002266}
2267
2268static void
2269shell_get_shell_surface(struct wl_client *client,
2270 struct wl_resource *resource,
2271 uint32_t id,
2272 struct wl_resource *surface_resource)
2273{
2274 struct weston_surface *surface = surface_resource->data;
2275 struct desktop_shell *shell = resource->data;
2276 struct shell_surface *shsurf;
2277
2278 if (get_shell_surface(surface)) {
2279 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002280 WL_DISPLAY_ERROR_INVALID_OBJECT,
2281 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002282 return;
2283 }
2284
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002285 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002286 if (!shsurf) {
2287 wl_resource_post_error(surface_resource,
2288 WL_DISPLAY_ERROR_INVALID_OBJECT,
2289 "surface->configure already set");
2290 return;
2291 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002292
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002293 shsurf->resource.destroy = shell_destroy_shell_surface;
2294 shsurf->resource.object.id = id;
2295 shsurf->resource.object.interface = &wl_shell_surface_interface;
2296 shsurf->resource.object.implementation =
2297 (void (**)(void)) &shell_surface_implementation;
2298 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002299
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002300 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002301}
2302
2303static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002304 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002305};
2306
Kristian Høgsberg07937562011-04-12 17:25:42 -04002307static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002308shell_fade(struct desktop_shell *shell, enum fade_type type);
2309
2310static int
2311screensaver_timeout(void *data)
2312{
2313 struct desktop_shell *shell = data;
2314
2315 shell_fade(shell, FADE_OUT);
2316
2317 return 1;
2318}
2319
2320static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002321handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002322{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002323 struct desktop_shell *shell =
2324 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002325
Pekka Paalanen18027e52011-12-02 16:31:49 +02002326 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002327
2328 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002329 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002330}
2331
2332static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002333launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002334{
2335 if (shell->screensaver.binding)
2336 return;
2337
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002338 if (!shell->screensaver.path) {
2339 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002340 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002341 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002342
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002343 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002344 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002345 return;
2346 }
2347
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002348 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002349 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002350 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002351 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002352}
2353
2354static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002355terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002356{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002357 if (shell->screensaver.process.pid == 0)
2358 return;
2359
2360 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002361}
2362
2363static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002364configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002365{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002366 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002367
Giulio Camuffo184df502013-02-21 11:29:21 +01002368 if (width == 0)
2369 return;
2370
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002371 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2372 if (s->output == es->output && s != es) {
2373 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002374 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002375 }
2376 }
2377
Giulio Camuffo184df502013-02-21 11:29:21 +01002378 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002379
2380 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002381 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002382 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002383 }
2384}
2385
2386static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002387background_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 -04002388{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002389 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002390
Giulio Camuffo184df502013-02-21 11:29:21 +01002391 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002392}
2393
2394static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002395desktop_shell_set_background(struct wl_client *client,
2396 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002397 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002398 struct wl_resource *surface_resource)
2399{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002400 struct desktop_shell *shell = resource->data;
2401 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002402
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002403 if (surface->configure) {
2404 wl_resource_post_error(surface_resource,
2405 WL_DISPLAY_ERROR_INVALID_OBJECT,
2406 "surface role already assigned");
2407 return;
2408 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002409
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002410 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002411 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002412 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002413 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002414 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002415 surface->output->width,
2416 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002417}
2418
2419static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002420panel_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 -04002421{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002422 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002423
Giulio Camuffo184df502013-02-21 11:29:21 +01002424 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002425}
2426
2427static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002428desktop_shell_set_panel(struct wl_client *client,
2429 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002430 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002431 struct wl_resource *surface_resource)
2432{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002433 struct desktop_shell *shell = resource->data;
2434 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002435
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002436 if (surface->configure) {
2437 wl_resource_post_error(surface_resource,
2438 WL_DISPLAY_ERROR_INVALID_OBJECT,
2439 "surface role already assigned");
2440 return;
2441 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002442
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002443 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002444 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002445 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002446 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002447 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002448 surface->output->width,
2449 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002450}
2451
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002452static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002453lock_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 -04002454{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002455 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002456
Giulio Camuffo184df502013-02-21 11:29:21 +01002457 if (width == 0)
2458 return;
2459
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002460 surface->geometry.width = width;
2461 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002462 center_on_output(surface, get_default_output(shell->compositor));
2463
2464 if (!weston_surface_is_mapped(surface)) {
2465 wl_list_insert(&shell->lock_layer.surface_list,
2466 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002467 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002468 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002469 }
2470}
2471
2472static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002473handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002474{
Tiago Vignattibe143262012-04-16 17:31:41 +03002475 struct desktop_shell *shell =
2476 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002477
Martin Minarik6d118362012-06-07 18:01:59 +02002478 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002479 shell->lock_surface = NULL;
2480}
2481
2482static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002483desktop_shell_set_lock_surface(struct wl_client *client,
2484 struct wl_resource *resource,
2485 struct wl_resource *surface_resource)
2486{
Tiago Vignattibe143262012-04-16 17:31:41 +03002487 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002488 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002489
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002490 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002491
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002492 if (!shell->locked)
2493 return;
2494
Pekka Paalanen98262232011-12-01 10:42:22 +02002495 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002496
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002497 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2498 wl_signal_add(&surface_resource->destroy_signal,
2499 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002500
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002501 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002502 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002503}
2504
2505static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002506resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002507{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002508 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002509
Pekka Paalanen77346a62011-11-30 16:26:35 +02002510 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002511
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002512 wl_list_remove(&shell->lock_layer.link);
2513 wl_list_insert(&shell->compositor->cursor_layer.link,
2514 &shell->fullscreen_layer.link);
2515 wl_list_insert(&shell->fullscreen_layer.link,
2516 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002517 if (shell->showing_input_panels) {
2518 wl_list_insert(&shell->panel_layer.link,
2519 &shell->input_panel_layer.link);
2520 wl_list_insert(&shell->input_panel_layer.link,
2521 &ws->layer.link);
2522 } else {
2523 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2524 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002525
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002526 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002527
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002528 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002529 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002530 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002531}
2532
2533static void
2534desktop_shell_unlock(struct wl_client *client,
2535 struct wl_resource *resource)
2536{
Tiago Vignattibe143262012-04-16 17:31:41 +03002537 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002538
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002539 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002540
2541 if (shell->locked)
2542 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002543}
2544
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002545static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002546desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002547 struct wl_resource *resource,
2548 struct wl_resource *surface_resource)
2549{
2550 struct desktop_shell *shell = resource->data;
2551
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002552 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002553}
2554
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002555static void
2556desktop_shell_desktop_ready(struct wl_client *client,
2557 struct wl_resource *resource)
2558{
2559 struct desktop_shell *shell = resource->data;
2560
2561 shell_fade_startup(shell);
2562}
2563
Kristian Høgsberg75840622011-09-06 13:48:16 -04002564static const struct desktop_shell_interface desktop_shell_implementation = {
2565 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002566 desktop_shell_set_panel,
2567 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002568 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002569 desktop_shell_set_grab_surface,
2570 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002571};
2572
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002573static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002574get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002575{
2576 struct shell_surface *shsurf;
2577
2578 shsurf = get_shell_surface(surface);
2579 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002580 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002581 return shsurf->type;
2582}
2583
Kristian Høgsberg75840622011-09-06 13:48:16 -04002584static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002585move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002586{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002587 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002588 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002589 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002590 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002591
Pekka Paalanen01388e22013-04-25 13:57:44 +03002592 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002593 if (surface == NULL)
2594 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002595
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002596 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002597 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2598 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002599 return;
2600
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002601 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002602}
2603
2604static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002605resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002606{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002607 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002608 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002609 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002610 uint32_t edges = 0;
2611 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002612 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002613
Pekka Paalanen01388e22013-04-25 13:57:44 +03002614 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002615 if (surface == NULL)
2616 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002617
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002618 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002619 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2620 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002621 return;
2622
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002623 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002624 wl_fixed_to_int(seat->pointer->grab_x),
2625 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002626 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002627
Pekka Paalanen60921e52012-01-25 15:55:43 +02002628 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002629 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002630 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002631 edges |= 0;
2632 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002633 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002634
Pekka Paalanen60921e52012-01-25 15:55:43 +02002635 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002636 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002637 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002638 edges |= 0;
2639 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002640 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002641
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002642 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002643}
2644
2645static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002646surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002647 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002648{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002649 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002650 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002651 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002652 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002653 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002654
Pekka Paalanen01388e22013-04-25 13:57:44 +03002655 /* XXX: broken for windows containing sub-surfaces */
2656 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002657 if (surface == NULL)
2658 return;
2659
2660 shsurf = get_shell_surface(surface);
2661 if (!shsurf)
2662 return;
2663
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002664 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002665
Scott Moreau02709af2012-05-22 01:54:10 -06002666 if (surface->alpha > 1.0)
2667 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002668 if (surface->alpha < step)
2669 surface->alpha = step;
2670
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002671 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002672 weston_surface_damage(surface);
2673}
2674
2675static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002676do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002677 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002678{
Daniel Stone37816df2012-05-16 18:45:18 +01002679 struct weston_seat *ws = (struct weston_seat *) seat;
2680 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002681 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002682 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002683
2684 wl_list_for_each(output, &compositor->output_list, link) {
2685 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002686 wl_fixed_to_double(seat->pointer->x),
2687 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002688 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002689 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002690 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002691 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002692 increment = -output->zoom.increment;
2693 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002694 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002695 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002696 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002697 else
2698 increment = 0;
2699
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002700 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002701
Scott Moreaue6603982012-06-11 13:07:51 -06002702 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002703 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002704 else if (output->zoom.level > output->zoom.max_level)
2705 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002706 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002707 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002708 output->disable_planes++;
2709 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002710
Scott Moreaue6603982012-06-11 13:07:51 -06002711 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002712
Scott Moreau8dacaab2012-06-17 18:10:58 -06002713 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002714 }
2715 }
2716}
2717
Scott Moreauccbf29d2012-02-22 14:21:41 -07002718static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002719zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002720 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002721{
2722 do_zoom(seat, time, 0, axis, value);
2723}
2724
2725static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002726zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002727 void *data)
2728{
2729 do_zoom(seat, time, key, 0, 0);
2730}
2731
2732static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002733terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002734 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002735{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002736 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002737
Daniel Stone325fc2d2012-05-30 16:31:58 +01002738 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002739}
2740
2741static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002742rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002743{
2744 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002745 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002746 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002747 struct shell_surface *shsurf = rotate->base.shsurf;
2748 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002749 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002750
2751 if (!shsurf)
2752 return;
2753
2754 surface = shsurf->surface;
2755
2756 cx = 0.5f * surface->geometry.width;
2757 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002758
Daniel Stone37816df2012-05-16 18:45:18 +01002759 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2760 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002761 r = sqrtf(dx * dx + dy * dy);
2762
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002763 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002764 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002765
2766 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002767 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002768 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002769
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002770 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002771 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002772
2773 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002774 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002775 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002776 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002777 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002778
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002779 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002780 &shsurf->surface->geometry.transformation_list,
2781 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002782 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002783 wl_list_init(&shsurf->rotation.transform.link);
2784 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002785 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002786 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002787
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002788 /* We need to adjust the position of the surface
2789 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002790 cposx = surface->geometry.x + cx;
2791 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002792 dposx = rotate->center.x - cposx;
2793 dposy = rotate->center.y - cposy;
2794 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002795 weston_surface_set_position(surface,
2796 surface->geometry.x + dposx,
2797 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002798 }
2799
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002800 /* Repaint implies weston_surface_update_transform(), which
2801 * lazily applies the damage due to rotation update.
2802 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002803 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002804}
2805
2806static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002807rotate_grab_button(struct weston_pointer_grab *grab,
2808 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002809{
2810 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002811 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002812 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002813 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002814 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002815
Daniel Stone4dbadb12012-05-30 16:31:51 +01002816 if (pointer->button_count == 0 &&
2817 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002818 if (shsurf)
2819 weston_matrix_multiply(&shsurf->rotation.rotation,
2820 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002821 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002822 free(rotate);
2823 }
2824}
2825
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002826static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002827 noop_grab_focus,
2828 rotate_grab_motion,
2829 rotate_grab_button,
2830};
2831
2832static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002833surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002834{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002835 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002836 float dx, dy;
2837 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002838
Pekka Paalanen460099f2012-01-20 16:48:25 +02002839 rotate = malloc(sizeof *rotate);
2840 if (!rotate)
2841 return;
2842
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002843 weston_surface_to_global_float(surface->surface,
2844 surface->surface->geometry.width / 2,
2845 surface->surface->geometry.height / 2,
2846 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002847
Daniel Stone37816df2012-05-16 18:45:18 +01002848 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2849 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002850 r = sqrtf(dx * dx + dy * dy);
2851 if (r > 20.0f) {
2852 struct weston_matrix inverse;
2853
2854 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002855 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002856 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002857
2858 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002859 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002860 } else {
2861 weston_matrix_init(&surface->rotation.rotation);
2862 weston_matrix_init(&rotate->rotation);
2863 }
2864
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002865 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2866 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002867}
2868
2869static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002870rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002871 void *data)
2872{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002873 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002874 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002875 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002876 struct shell_surface *surface;
2877
Pekka Paalanen01388e22013-04-25 13:57:44 +03002878 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002879 if (base_surface == NULL)
2880 return;
2881
2882 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002883 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2884 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002885 return;
2886
2887 surface_rotate(surface, seat);
2888}
2889
2890static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002891lower_fullscreen_layer(struct desktop_shell *shell)
2892{
2893 struct workspace *ws;
2894 struct weston_surface *surface, *prev;
2895
2896 ws = get_current_workspace(shell);
2897 wl_list_for_each_reverse_safe(surface, prev,
2898 &shell->fullscreen_layer.surface_list,
2899 layer_link)
2900 weston_surface_restack(surface, &ws->layer.surface_list);
2901}
2902
2903static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002904activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002905 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002906{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002907 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002908 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002909 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002910
Pekka Paalanen01388e22013-04-25 13:57:44 +03002911 main_surface = weston_surface_get_main_surface(es);
2912
Daniel Stone37816df2012-05-16 18:45:18 +01002913 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002914
Jonas Ådahl8538b222012-08-29 22:13:03 +02002915 state = ensure_focus_state(shell, seat);
2916 if (state == NULL)
2917 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002918
2919 state->keyboard_focus = es;
2920 wl_list_remove(&state->surface_destroy_listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002921 wl_signal_add(&es->resource.destroy_signal,
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002922 &state->surface_destroy_listener);
2923
Pekka Paalanen01388e22013-04-25 13:57:44 +03002924 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002925 case SHELL_SURFACE_FULLSCREEN:
2926 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002927 shell_stack_fullscreen(get_shell_surface(main_surface));
2928 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002929 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002930 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002931 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002932 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002933 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002934 }
2935}
2936
Alex Wu21858432012-04-01 20:13:08 +08002937/* no-op func for checking black surface */
2938static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002939black_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 +08002940{
2941}
2942
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002943static bool
Alex Wu21858432012-04-01 20:13:08 +08002944is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2945{
2946 if (es->configure == black_surface_configure) {
2947 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002948 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002949 return true;
2950 }
2951 return false;
2952}
2953
Kristian Høgsberg75840622011-09-06 13:48:16 -04002954static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002955click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002956 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002957{
Daniel Stone37816df2012-05-16 18:45:18 +01002958 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002959 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002960 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002961 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002962
Daniel Stone37816df2012-05-16 18:45:18 +01002963 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002964 if (!focus)
2965 return;
2966
Pekka Paalanen01388e22013-04-25 13:57:44 +03002967 if (is_black_surface(focus, &main_surface))
2968 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002969
Pekka Paalanen01388e22013-04-25 13:57:44 +03002970 main_surface = weston_surface_get_main_surface(focus);
2971 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002972 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002973
Daniel Stone325fc2d2012-05-30 16:31:58 +01002974 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002975 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002976}
2977
2978static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002979lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002980{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002981 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002982
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002983 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002984 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002985 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002986 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002987
2988 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002989
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002990 /* Hide all surfaces by removing the fullscreen, panel and
2991 * toplevel layers. This way nothing else can show or receive
2992 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002993
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002994 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002995 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002996 if (shell->showing_input_panels)
2997 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002998 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002999 wl_list_insert(&shell->compositor->cursor_layer.link,
3000 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003001
Pekka Paalanen77346a62011-11-30 16:26:35 +02003002 launch_screensaver(shell);
3003
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003004 /* TODO: disable bindings that should not work while locked. */
3005
3006 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003007}
3008
3009static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003010unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003011{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003012 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003013 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003014 return;
3015 }
3016
3017 /* If desktop-shell client has gone away, unlock immediately. */
3018 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003019 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003020 return;
3021 }
3022
3023 if (shell->prepare_event_sent)
3024 return;
3025
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003026 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003027 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003028}
3029
3030static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003031shell_fade_done(struct weston_surface_animation *animation, void *data)
3032{
3033 struct desktop_shell *shell = data;
3034
3035 shell->fade.animation = NULL;
3036
3037 switch (shell->fade.type) {
3038 case FADE_IN:
3039 weston_surface_destroy(shell->fade.surface);
3040 shell->fade.surface = NULL;
3041 break;
3042 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003043 lock(shell);
3044 break;
3045 }
3046}
3047
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003048static struct weston_surface *
3049shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003050{
3051 struct weston_compositor *compositor = shell->compositor;
3052 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003053
3054 surface = weston_surface_create(compositor);
3055 if (!surface)
3056 return NULL;
3057
3058 weston_surface_configure(surface, 0, 0, 8192, 8192);
3059 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3060 wl_list_insert(&compositor->fade_layer.surface_list,
3061 &surface->layer_link);
3062 pixman_region32_init(&surface->input);
3063
3064 return surface;
3065}
3066
3067static void
3068shell_fade(struct desktop_shell *shell, enum fade_type type)
3069{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003070 float tint;
3071
3072 switch (type) {
3073 case FADE_IN:
3074 tint = 0.0;
3075 break;
3076 case FADE_OUT:
3077 tint = 1.0;
3078 break;
3079 default:
3080 weston_log("shell: invalid fade type\n");
3081 return;
3082 }
3083
3084 shell->fade.type = type;
3085
3086 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003087 shell->fade.surface = shell_fade_create_surface(shell);
3088 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003089 return;
3090
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003091 shell->fade.surface->alpha = 1.0 - tint;
3092 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003093 }
3094
3095 if (shell->fade.animation)
3096 weston_fade_update(shell->fade.animation,
3097 shell->fade.surface->alpha, tint, 30.0);
3098 else
3099 shell->fade.animation =
3100 weston_fade_run(shell->fade.surface,
3101 1.0 - tint, tint, 30.0,
3102 shell_fade_done, shell);
3103}
3104
3105static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003106do_shell_fade_startup(void *data)
3107{
3108 struct desktop_shell *shell = data;
3109
3110 shell_fade(shell, FADE_IN);
3111}
3112
3113static void
3114shell_fade_startup(struct desktop_shell *shell)
3115{
3116 struct wl_event_loop *loop;
3117
3118 if (!shell->fade.startup_timer)
3119 return;
3120
3121 wl_event_source_remove(shell->fade.startup_timer);
3122 shell->fade.startup_timer = NULL;
3123
3124 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3125 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3126}
3127
3128static int
3129fade_startup_timeout(void *data)
3130{
3131 struct desktop_shell *shell = data;
3132
3133 shell_fade_startup(shell);
3134 return 0;
3135}
3136
3137static void
3138shell_fade_init(struct desktop_shell *shell)
3139{
3140 /* Make compositor output all black, and wait for the desktop-shell
3141 * client to signal it is ready, then fade in. The timer triggers a
3142 * fade-in, in case the desktop-shell client takes too long.
3143 */
3144
3145 struct wl_event_loop *loop;
3146
3147 if (shell->fade.surface != NULL) {
3148 weston_log("%s: warning: fade surface already exists\n",
3149 __func__);
3150 return;
3151 }
3152
3153 shell->fade.surface = shell_fade_create_surface(shell);
3154 if (!shell->fade.surface)
3155 return;
3156
3157 weston_surface_update_transform(shell->fade.surface);
3158 weston_surface_damage(shell->fade.surface);
3159
3160 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3161 shell->fade.startup_timer =
3162 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3163 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3164}
3165
3166static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003167idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003168{
3169 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003170 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003171
3172 shell_fade(shell, FADE_OUT);
3173 /* lock() is called from shell_fade_done() */
3174}
3175
3176static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003177wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003178{
3179 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003180 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003181
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003182 unlock(shell);
3183}
3184
3185static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003186show_input_panels(struct wl_listener *listener, void *data)
3187{
3188 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003189 container_of(listener, struct desktop_shell,
3190 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003191 struct input_panel_surface *surface, *next;
3192 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003193
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003194 shell->text_input.surface = (struct weston_surface*)data;
3195
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003196 if (shell->showing_input_panels)
3197 return;
3198
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003199 shell->showing_input_panels = true;
3200
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003201 if (!shell->locked)
3202 wl_list_insert(&shell->panel_layer.link,
3203 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003204
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003205 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003206 &shell->input_panel.surfaces, link) {
3207 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003208 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003209 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003210 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003211 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003212 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003213 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003214 weston_surface_damage(ws);
3215 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003216 }
3217}
3218
3219static void
3220hide_input_panels(struct wl_listener *listener, void *data)
3221{
3222 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003223 container_of(listener, struct desktop_shell,
3224 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003225 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003226
Jan Arne Petersen61381972013-01-31 15:52:21 +01003227 if (!shell->showing_input_panels)
3228 return;
3229
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003230 shell->showing_input_panels = false;
3231
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003232 if (!shell->locked)
3233 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003234
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003235 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003236 &shell->input_panel_layer.surface_list, layer_link)
3237 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003238}
3239
3240static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003241update_input_panels(struct wl_listener *listener, void *data)
3242{
3243 struct desktop_shell *shell =
3244 container_of(listener, struct desktop_shell,
3245 update_input_panel_listener);
3246
3247 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3248}
3249
3250static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003251center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003252{
Giulio Camuffob8366642013-04-25 13:57:46 +03003253 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003254 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003255
Giulio Camuffob8366642013-04-25 13:57:46 +03003256 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3257
3258 x = output->x + (output->width - width) / 2 - surf_x / 2;
3259 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003260
3261 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003262}
3263
3264static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003265weston_surface_set_initial_position (struct weston_surface *surface,
3266 struct desktop_shell *shell)
3267{
3268 struct weston_compositor *compositor = shell->compositor;
3269 int ix = 0, iy = 0;
3270 int range_x, range_y;
3271 int dx, dy, x, y, panel_height;
3272 struct weston_output *output, *target_output = NULL;
3273 struct weston_seat *seat;
3274
3275 /* As a heuristic place the new window on the same output as the
3276 * pointer. Falling back to the output containing 0, 0.
3277 *
3278 * TODO: Do something clever for touch too?
3279 */
3280 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003281 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003282 ix = wl_fixed_to_int(seat->pointer->x);
3283 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003284 break;
3285 }
3286 }
3287
3288 wl_list_for_each(output, &compositor->output_list, link) {
3289 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3290 target_output = output;
3291 break;
3292 }
3293 }
3294
3295 if (!target_output) {
3296 weston_surface_set_position(surface, 10 + random() % 400,
3297 10 + random() % 400);
3298 return;
3299 }
3300
3301 /* Valid range within output where the surface will still be onscreen.
3302 * If this is negative it means that the surface is bigger than
3303 * output.
3304 */
3305 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003306 range_x = target_output->width - surface->geometry.width;
3307 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003308 surface->geometry.height;
3309
Rob Bradford4cb88c72012-08-13 15:18:44 +01003310 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003311 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003312 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003313 dx = 0;
3314
3315 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003316 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003317 else
3318 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003319
3320 x = target_output->x + dx;
3321 y = target_output->y + dy;
3322
3323 weston_surface_set_position (surface, x, y);
3324}
3325
3326static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003327map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003328 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003329{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003330 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003331 struct shell_surface *shsurf = get_shell_surface(surface);
3332 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003333 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003334 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003335 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003336 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003337 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003338
Pekka Paalanen60921e52012-01-25 15:55:43 +02003339 surface->geometry.width = width;
3340 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003341 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003342
3343 /* initial positioning, see also configure() */
3344 switch (surface_type) {
3345 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003346 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003347 break;
Alex Wu4539b082012-03-01 12:57:46 +08003348 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003349 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003350 shell_map_fullscreen(shsurf);
3351 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003352 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003353 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003354 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003355 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3356 NULL, NULL);
3357 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3358 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003359 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003360 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003361 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003362 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003363 case SHELL_SURFACE_NONE:
3364 weston_surface_set_position(surface,
3365 surface->geometry.x + sx,
3366 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003367 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003368 default:
3369 ;
3370 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003371
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003372 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003373 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003374 case SHELL_SURFACE_POPUP:
3375 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003376 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003377 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3378 break;
Alex Wu4539b082012-03-01 12:57:46 +08003379 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003380 case SHELL_SURFACE_NONE:
3381 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003382 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003383 ws = get_current_workspace(shell);
3384 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003385 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003386 }
3387
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003388 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003389 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003390 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3391 surface->output = shsurf->output;
3392 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003393
Juan Zhao7bb92f02011-12-15 11:31:51 -05003394 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003395 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003396 if (shsurf->transient.flags ==
3397 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3398 break;
3399 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003400 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003401 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003402 if (!shell->locked) {
3403 wl_list_for_each(seat, &compositor->seat_list, link)
3404 activate(shell, surface, seat);
3405 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003406 break;
3407 default:
3408 break;
3409 }
3410
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003411 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003412 {
3413 switch (shell->win_animation_type) {
3414 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003415 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003416 break;
3417 case ANIMATION_ZOOM:
3418 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3419 break;
3420 default:
3421 break;
3422 }
3423 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003424}
3425
3426static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003427configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003428 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003429{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003430 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3431 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003432 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003433
Pekka Paalanen77346a62011-11-30 16:26:35 +02003434 shsurf = get_shell_surface(surface);
3435 if (shsurf)
3436 surface_type = shsurf->type;
3437
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003438 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003439
3440 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003441 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003442 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003443 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003444 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003445 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003446 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003447 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3448 NULL, NULL);
3449 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003450 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003451 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003452 break;
Alex Wu4539b082012-03-01 12:57:46 +08003453 case SHELL_SURFACE_TOPLEVEL:
3454 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003455 default:
3456 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003457 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003458
Alex Wu4539b082012-03-01 12:57:46 +08003459 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003460 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003461 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003462
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003463 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003464 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003465 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003466}
3467
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003468static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003469shell_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 +03003470{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003471 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003472 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003473
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003474 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003475
Giulio Camuffo5085a752013-03-25 21:42:45 +01003476 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3477 remove_popup_grab(shsurf);
3478 }
3479
Giulio Camuffo184df502013-02-21 11:29:21 +01003480 if (width == 0)
3481 return;
3482
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003483 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003484 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003485 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003486 type_changed = 1;
3487 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003488
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003489 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003490 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003491 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003492 es->geometry.width != width ||
3493 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003494 float from_x, from_y;
3495 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003496
3497 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3498 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3499 configure(shell, es,
3500 es->geometry.x + to_x - from_x,
3501 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003502 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003503 }
3504}
3505
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003506static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003507
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003508static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003509desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003510{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003511 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003512 struct desktop_shell *shell =
3513 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003514
3515 shell->child.process.pid = 0;
3516 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003517
3518 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3519 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003520 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003521 shell->child.deathstamp = time;
3522 shell->child.deathcount = 0;
3523 }
3524
3525 shell->child.deathcount++;
3526 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003527 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003528 return;
3529 }
3530
Martin Minarik6d118362012-06-07 18:01:59 +02003531 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003532 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003533 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003534}
3535
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003536static void
3537launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003538{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003539 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003540 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003541
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003542 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003543 &shell->child.process,
3544 shell_exe,
3545 desktop_shell_sigchld);
3546
3547 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003548 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003549}
3550
3551static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003552bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3553{
Tiago Vignattibe143262012-04-16 17:31:41 +03003554 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003555
3556 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003557 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003558}
3559
Kristian Høgsberg75840622011-09-06 13:48:16 -04003560static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003561unbind_desktop_shell(struct wl_resource *resource)
3562{
Tiago Vignattibe143262012-04-16 17:31:41 +03003563 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003564
3565 if (shell->locked)
3566 resume_desktop(shell);
3567
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003568 shell->child.desktop_shell = NULL;
3569 shell->prepare_event_sent = false;
3570 free(resource);
3571}
3572
3573static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003574bind_desktop_shell(struct wl_client *client,
3575 void *data, uint32_t version, uint32_t id)
3576{
Tiago Vignattibe143262012-04-16 17:31:41 +03003577 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003578 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003579
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003580 resource = wl_client_add_object(client, &desktop_shell_interface,
3581 &desktop_shell_implementation,
3582 id, shell);
3583
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003584 if (client == shell->child.client) {
3585 resource->destroy = unbind_desktop_shell;
3586 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003587
3588 if (version < 2)
3589 shell_fade_startup(shell);
3590
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003591 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003592 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003593
3594 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3595 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003596 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003597}
3598
Pekka Paalanen6e168112011-11-24 11:34:05 +02003599static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003600screensaver_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 -04003601{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003602 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003603
Giulio Camuffo184df502013-02-21 11:29:21 +01003604 if (width == 0)
3605 return;
3606
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003607 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003608 if (!shell->locked)
3609 return;
3610
3611 center_on_output(surface, surface->output);
3612
3613 if (wl_list_empty(&surface->layer_link)) {
3614 wl_list_insert(shell->lock_layer.surface_list.prev,
3615 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003616 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003617 wl_event_source_timer_update(shell->screensaver.timer,
3618 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003619 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003620 }
3621}
3622
3623static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003624screensaver_set_surface(struct wl_client *client,
3625 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003626 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003627 struct wl_resource *output_resource)
3628{
Tiago Vignattibe143262012-04-16 17:31:41 +03003629 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003630 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003631 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003632
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003633 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003634 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003635 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003636}
3637
3638static const struct screensaver_interface screensaver_implementation = {
3639 screensaver_set_surface
3640};
3641
3642static void
3643unbind_screensaver(struct wl_resource *resource)
3644{
Tiago Vignattibe143262012-04-16 17:31:41 +03003645 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003646
Pekka Paalanen77346a62011-11-30 16:26:35 +02003647 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003648 free(resource);
3649}
3650
3651static void
3652bind_screensaver(struct wl_client *client,
3653 void *data, uint32_t version, uint32_t id)
3654{
Tiago Vignattibe143262012-04-16 17:31:41 +03003655 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003656 struct wl_resource *resource;
3657
3658 resource = wl_client_add_object(client, &screensaver_interface,
3659 &screensaver_implementation,
3660 id, shell);
3661
Pekka Paalanen77346a62011-11-30 16:26:35 +02003662 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003663 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003664 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003665 return;
3666 }
3667
3668 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3669 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003670 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003671}
3672
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003673static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003674input_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 -04003675{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003676 struct input_panel_surface *ip_surface = surface->configure_private;
3677 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003678 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003679 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003680 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003681
Giulio Camuffo184df502013-02-21 11:29:21 +01003682 if (width == 0)
3683 return;
3684
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003685 if (!weston_surface_is_mapped(surface)) {
3686 if (!shell->showing_input_panels)
3687 return;
3688
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003689 show_surface = 1;
3690 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003691
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003692 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003693
3694 if (ip_surface->panel) {
3695 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3696 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3697 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003698 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003699
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003700 x = ip_surface->output->x + (mode->width - width) / 2;
3701 y = ip_surface->output->y + mode->height - height;
3702 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003703
3704 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003705 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003706 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003707
3708 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003709 wl_list_insert(&shell->input_panel_layer.surface_list,
3710 &surface->layer_link);
3711 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003712 weston_surface_damage(surface);
3713 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3714 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003715}
3716
3717static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003718destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003719{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003720 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003721 wl_list_remove(&input_panel_surface->link);
3722
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003723 input_panel_surface->surface->configure = NULL;
3724
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003725 free(input_panel_surface);
3726}
3727
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003728static struct input_panel_surface *
3729get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003730{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003731 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003732 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003733 } else {
3734 return NULL;
3735 }
3736}
3737
3738static void
3739input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3740{
3741 struct input_panel_surface *ipsurface = container_of(listener,
3742 struct input_panel_surface,
3743 surface_destroy_listener);
3744
3745 if (ipsurface->resource.client) {
3746 wl_resource_destroy(&ipsurface->resource);
3747 } else {
3748 wl_signal_emit(&ipsurface->resource.destroy_signal,
3749 &ipsurface->resource);
3750 destroy_input_panel_surface(ipsurface);
3751 }
3752}
3753static struct input_panel_surface *
3754create_input_panel_surface(struct desktop_shell *shell,
3755 struct weston_surface *surface)
3756{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003757 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003758
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003759 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3760 if (!input_panel_surface)
3761 return NULL;
3762
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003763 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003764 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003765
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003766 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003767
3768 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003769
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003770 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3771 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003772 wl_signal_add(&surface->resource.destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003773 &input_panel_surface->surface_destroy_listener);
3774
3775 wl_list_init(&input_panel_surface->link);
3776
3777 return input_panel_surface;
3778}
3779
3780static void
3781input_panel_surface_set_toplevel(struct wl_client *client,
3782 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003783 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003784 uint32_t position)
3785{
3786 struct input_panel_surface *input_panel_surface = resource->data;
3787 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003788
3789 wl_list_insert(&shell->input_panel.surfaces,
3790 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003791
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003792 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003793 input_panel_surface->panel = 0;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003794
3795 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__,
3796 input_panel_surface->panel,
3797 input_panel_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003798}
3799
3800static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003801input_panel_surface_set_overlay_panel(struct wl_client *client,
3802 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003803{
3804 struct input_panel_surface *input_panel_surface = resource->data;
3805 struct desktop_shell *shell = input_panel_surface->shell;
3806
3807 wl_list_insert(&shell->input_panel.surfaces,
3808 &input_panel_surface->link);
3809
3810 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003811}
3812
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003813static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003814 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003815 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003816};
3817
3818static void
3819destroy_input_panel_surface_resource(struct wl_resource *resource)
3820{
3821 struct input_panel_surface *ipsurf = resource->data;
3822
3823 destroy_input_panel_surface(ipsurf);
3824}
3825
3826static void
3827input_panel_get_input_panel_surface(struct wl_client *client,
3828 struct wl_resource *resource,
3829 uint32_t id,
3830 struct wl_resource *surface_resource)
3831{
3832 struct weston_surface *surface = surface_resource->data;
3833 struct desktop_shell *shell = resource->data;
3834 struct input_panel_surface *ipsurf;
3835
3836 if (get_input_panel_surface(surface)) {
3837 wl_resource_post_error(surface_resource,
3838 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003839 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003840 return;
3841 }
3842
3843 ipsurf = create_input_panel_surface(shell, surface);
3844 if (!ipsurf) {
3845 wl_resource_post_error(surface_resource,
3846 WL_DISPLAY_ERROR_INVALID_OBJECT,
3847 "surface->configure already set");
3848 return;
3849 }
3850
3851 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3852 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003853 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003854 ipsurf->resource.object.implementation =
3855 (void (**)(void)) &input_panel_surface_implementation;
3856 ipsurf->resource.data = ipsurf;
3857
3858 wl_client_add_resource(client, &ipsurf->resource);
3859}
3860
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003861static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003862 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003863};
3864
3865static void
3866unbind_input_panel(struct wl_resource *resource)
3867{
3868 struct desktop_shell *shell = resource->data;
3869
3870 shell->input_panel.binding = NULL;
3871 free(resource);
3872}
3873
3874static void
3875bind_input_panel(struct wl_client *client,
3876 void *data, uint32_t version, uint32_t id)
3877{
3878 struct desktop_shell *shell = data;
3879 struct wl_resource *resource;
3880
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003881 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003882 &input_panel_implementation,
3883 id, shell);
3884
3885 if (shell->input_panel.binding == NULL) {
3886 resource->destroy = unbind_input_panel;
3887 shell->input_panel.binding = resource;
3888 return;
3889 }
3890
3891 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3892 "interface object already bound");
3893 wl_resource_destroy(resource);
3894}
3895
Kristian Høgsberg07045392012-02-19 18:52:44 -05003896struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003897 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003898 struct weston_surface *current;
3899 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003900 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003901};
3902
3903static void
3904switcher_next(struct switcher *switcher)
3905{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003906 struct weston_surface *surface;
3907 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003908 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003909 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003910
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003911 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003912 switch (get_shell_surface_type(surface)) {
3913 case SHELL_SURFACE_TOPLEVEL:
3914 case SHELL_SURFACE_FULLSCREEN:
3915 case SHELL_SURFACE_MAXIMIZED:
3916 if (first == NULL)
3917 first = surface;
3918 if (prev == switcher->current)
3919 next = surface;
3920 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003921 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003922 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003923 weston_surface_damage(surface);
3924 break;
3925 default:
3926 break;
3927 }
Alex Wu1659daa2012-04-01 20:13:09 +08003928
3929 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003930 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003931 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003932 weston_surface_damage(surface);
3933 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003934 }
3935
3936 if (next == NULL)
3937 next = first;
3938
Alex Wu07b26062012-03-12 16:06:01 +08003939 if (next == NULL)
3940 return;
3941
Kristian Høgsberg07045392012-02-19 18:52:44 -05003942 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003943 wl_signal_add(&next->resource.destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003944
3945 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003946 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003947
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003948 shsurf = get_shell_surface(switcher->current);
3949 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003950 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003951}
3952
3953static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003954switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003955{
3956 struct switcher *switcher =
3957 container_of(listener, struct switcher, listener);
3958
3959 switcher_next(switcher);
3960}
3961
3962static void
Daniel Stone351eb612012-05-31 15:27:47 -04003963switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003964{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003965 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003966 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003967 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003968
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003969 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003970 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003971 weston_surface_damage(surface);
3972 }
3973
Alex Wu07b26062012-03-12 16:06:01 +08003974 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003975 activate(switcher->shell, switcher->current,
3976 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003977 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003978 weston_keyboard_end_grab(keyboard);
3979 if (keyboard->input_method_resource)
3980 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003981 free(switcher);
3982}
3983
3984static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003985switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003986 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003987{
3988 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003989 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003990
Daniel Stonec9785ea2012-05-30 16:31:52 +01003991 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003992 switcher_next(switcher);
3993}
3994
3995static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003996switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04003997 uint32_t mods_depressed, uint32_t mods_latched,
3998 uint32_t mods_locked, uint32_t group)
3999{
4000 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004001 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004002
Daniel Stone351eb612012-05-31 15:27:47 -04004003 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4004 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004005}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004006
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004007static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004008 switcher_key,
4009 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004010};
4011
4012static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004013switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004014 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004015{
Tiago Vignattibe143262012-04-16 17:31:41 +03004016 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004017 struct switcher *switcher;
4018
4019 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004020 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004021 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004022 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004023 wl_list_init(&switcher->listener.link);
4024
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004025 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004026 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004027 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4028 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004029 switcher_next(switcher);
4030}
4031
Pekka Paalanen3c647232011-12-22 13:43:43 +02004032static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004033backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004034 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004035{
4036 struct weston_compositor *compositor = data;
4037 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004038 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004039
4040 /* TODO: we're limiting to simple use cases, where we assume just
4041 * control on the primary display. We'd have to extend later if we
4042 * ever get support for setting backlights on random desktop LCD
4043 * panels though */
4044 output = get_default_output(compositor);
4045 if (!output)
4046 return;
4047
4048 if (!output->set_backlight)
4049 return;
4050
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004051 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4052 backlight_new = output->backlight_current - 25;
4053 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4054 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004055
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004056 if (backlight_new < 5)
4057 backlight_new = 5;
4058 if (backlight_new > 255)
4059 backlight_new = 255;
4060
4061 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004062 output->set_backlight(output, output->backlight_current);
4063}
4064
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004065struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004066 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004067 struct weston_seat *seat;
4068 uint32_t key[2];
4069 int key_released[2];
4070};
4071
4072static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004073debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004074 uint32_t key, uint32_t state)
4075{
4076 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4077 struct wl_resource *resource;
4078 struct wl_display *display;
4079 uint32_t serial;
4080 int send = 0, terminate = 0;
4081 int check_binding = 1;
4082 int i;
4083
4084 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4085 /* Do not run bindings on key releases */
4086 check_binding = 0;
4087
4088 for (i = 0; i < 2; i++)
4089 if (key == db->key[i])
4090 db->key_released[i] = 1;
4091
4092 if (db->key_released[0] && db->key_released[1]) {
4093 /* All key releases been swalled so end the grab */
4094 terminate = 1;
4095 } else if (key != db->key[0] && key != db->key[1]) {
4096 /* Should not swallow release of other keys */
4097 send = 1;
4098 }
4099 } else if (key == db->key[0] && !db->key_released[0]) {
4100 /* Do not check bindings for the first press of the binding
4101 * key. This allows it to be used as a debug shortcut.
4102 * We still need to swallow this event. */
4103 check_binding = 0;
4104 } else if (db->key[1]) {
4105 /* If we already ran a binding don't process another one since
4106 * we can't keep track of all the binding keys that were
4107 * pressed in order to swallow the release events. */
4108 send = 1;
4109 check_binding = 0;
4110 }
4111
4112 if (check_binding) {
4113 struct weston_compositor *ec = db->seat->compositor;
4114
4115 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4116 key, state)) {
4117 /* We ran a binding so swallow the press and keep the
4118 * grab to swallow the released too. */
4119 send = 0;
4120 terminate = 0;
4121 db->key[1] = key;
4122 } else {
4123 /* Terminate the grab since the key pressed is not a
4124 * debug binding key. */
4125 send = 1;
4126 terminate = 1;
4127 }
4128 }
4129
4130 if (send) {
4131 resource = grab->keyboard->focus_resource;
4132
4133 if (resource) {
4134 display = wl_client_get_display(resource->client);
4135 serial = wl_display_next_serial(display);
4136 wl_keyboard_send_key(resource, serial, time, key, state);
4137 }
4138 }
4139
4140 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004141 weston_keyboard_end_grab(grab->keyboard);
4142 if (grab->keyboard->input_method_resource)
4143 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004144 free(db);
4145 }
4146}
4147
4148static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004149debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004150 uint32_t mods_depressed, uint32_t mods_latched,
4151 uint32_t mods_locked, uint32_t group)
4152{
4153 struct wl_resource *resource;
4154
4155 resource = grab->keyboard->focus_resource;
4156 if (!resource)
4157 return;
4158
4159 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4160 mods_latched, mods_locked, group);
4161}
4162
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004163struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004164 debug_binding_key,
4165 debug_binding_modifiers
4166};
4167
4168static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004169debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004170{
4171 struct debug_binding_grab *grab;
4172
4173 grab = calloc(1, sizeof *grab);
4174 if (!grab)
4175 return;
4176
4177 grab->seat = (struct weston_seat *) seat;
4178 grab->key[0] = key;
4179 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004180 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004181}
4182
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004183static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004184force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004185 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004186{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004187 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004188 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004189 struct desktop_shell *shell = data;
4190 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004191 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004192
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004193 focus_surface = seat->keyboard->focus;
4194 if (!focus_surface)
4195 return;
4196
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004197 wl_signal_emit(&compositor->kill_signal, focus_surface);
4198
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004199 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03004200 wl_client_get_credentials(client, &pid, NULL, NULL);
4201
4202 /* Skip clients that we launched ourselves (the credentials of
4203 * the socketpair is ours) */
4204 if (pid == getpid())
4205 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004206
Daniel Stone325fc2d2012-05-30 16:31:58 +01004207 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004208}
4209
4210static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004211workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004212 uint32_t key, void *data)
4213{
4214 struct desktop_shell *shell = data;
4215 unsigned int new_index = shell->workspaces.current;
4216
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004217 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004218 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004219 if (new_index != 0)
4220 new_index--;
4221
4222 change_workspace(shell, new_index);
4223}
4224
4225static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004226workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004227 uint32_t key, void *data)
4228{
4229 struct desktop_shell *shell = data;
4230 unsigned int new_index = shell->workspaces.current;
4231
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004232 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004233 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004234 if (new_index < shell->workspaces.num - 1)
4235 new_index++;
4236
4237 change_workspace(shell, new_index);
4238}
4239
4240static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004241workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004242 uint32_t key, void *data)
4243{
4244 struct desktop_shell *shell = data;
4245 unsigned int new_index;
4246
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004247 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004248 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004249 new_index = key - KEY_F1;
4250 if (new_index >= shell->workspaces.num)
4251 new_index = shell->workspaces.num - 1;
4252
4253 change_workspace(shell, new_index);
4254}
4255
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004256static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004257workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004258 uint32_t key, void *data)
4259{
4260 struct desktop_shell *shell = data;
4261 unsigned int new_index = shell->workspaces.current;
4262
4263 if (shell->locked)
4264 return;
4265
4266 if (new_index != 0)
4267 new_index--;
4268
4269 take_surface_to_workspace_by_seat(shell, seat, new_index);
4270}
4271
4272static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004273workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004274 uint32_t key, void *data)
4275{
4276 struct desktop_shell *shell = data;
4277 unsigned int new_index = shell->workspaces.current;
4278
4279 if (shell->locked)
4280 return;
4281
4282 if (new_index < shell->workspaces.num - 1)
4283 new_index++;
4284
4285 take_surface_to_workspace_by_seat(shell, seat, new_index);
4286}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004287
4288static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004289shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004290{
Tiago Vignattibe143262012-04-16 17:31:41 +03004291 struct desktop_shell *shell =
4292 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004293 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004294
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004295 if (shell->child.client)
4296 wl_client_destroy(shell->child.client);
4297
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004298 wl_list_remove(&shell->idle_listener.link);
4299 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004300 wl_list_remove(&shell->show_input_panel_listener.link);
4301 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004302
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004303 wl_array_for_each(ws, &shell->workspaces.array)
4304 workspace_destroy(*ws);
4305 wl_array_release(&shell->workspaces.array);
4306
Pekka Paalanen3c647232011-12-22 13:43:43 +02004307 free(shell->screensaver.path);
4308 free(shell);
4309}
4310
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004311static void
4312shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4313{
4314 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004315 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004316
4317 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004318 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4319 MODIFIER_CTRL | MODIFIER_ALT,
4320 terminate_binding, ec);
4321 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4322 click_to_activate_binding,
4323 shell);
4324 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4325 MODIFIER_SUPER | MODIFIER_ALT,
4326 surface_opacity_binding, NULL);
4327 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4328 MODIFIER_SUPER, zoom_axis_binding,
4329 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004330
4331 /* configurable bindings */
4332 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004333 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4334 zoom_key_binding, NULL);
4335 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4336 zoom_key_binding, NULL);
4337 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4338 shell);
4339 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4340 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004341
4342 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4343 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4344 rotate_binding, NULL);
4345
Daniel Stone325fc2d2012-05-30 16:31:58 +01004346 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4347 shell);
4348 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4349 ec);
4350 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4351 backlight_binding, ec);
4352 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4353 ec);
4354 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4355 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004356 weston_compositor_add_key_binding(ec, KEY_K, mod,
4357 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004358 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4359 workspace_up_binding, shell);
4360 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4361 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004362 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4363 workspace_move_surface_up_binding,
4364 shell);
4365 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4366 workspace_move_surface_down_binding,
4367 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004368
4369 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4370 if (shell->workspaces.num > 1) {
4371 num_workspace_bindings = shell->workspaces.num;
4372 if (num_workspace_bindings > 6)
4373 num_workspace_bindings = 6;
4374 for (i = 0; i < num_workspace_bindings; i++)
4375 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4376 workspace_f_binding,
4377 shell);
4378 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004379
4380 /* Debug bindings */
4381 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4382 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004383}
4384
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004385WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004386module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004387 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004388{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004389 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004390 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004391 struct workspace **pws;
4392 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004393 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004394
4395 shell = malloc(sizeof *shell);
4396 if (shell == NULL)
4397 return -1;
4398
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004399 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004400 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004401
4402 shell->destroy_listener.notify = shell_destroy;
4403 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004404 shell->idle_listener.notify = idle_handler;
4405 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4406 shell->wake_listener.notify = wake_handler;
4407 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004408 shell->show_input_panel_listener.notify = show_input_panels;
4409 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4410 shell->hide_input_panel_listener.notify = hide_input_panels;
4411 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004412 shell->update_input_panel_listener.notify = update_input_panels;
4413 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004414 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004415 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004416 ec->shell_interface.create_shell_surface = create_shell_surface;
4417 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004418 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004419 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004420 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004421 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004422
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004423 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004424
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004425 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4426 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004427 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4428 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004429 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004430
4431 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004432 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004433
Ossama Othmana50e6e42013-05-14 09:48:26 -07004434 shell_configuration(shell, ec->config_fd);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004435
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004436 for (i = 0; i < shell->workspaces.num; i++) {
4437 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4438 if (pws == NULL)
4439 return -1;
4440
4441 *pws = workspace_create();
4442 if (*pws == NULL)
4443 return -1;
4444 }
4445 activate_workspace(shell, 0);
4446
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004447 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004448 wl_list_init(&shell->workspaces.animation.link);
4449 shell->workspaces.animation.frame = animate_workspace_change_frame;
4450
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004451 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4452 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004453 return -1;
4454
Kristian Høgsberg75840622011-09-06 13:48:16 -04004455 if (wl_display_add_global(ec->wl_display,
4456 &desktop_shell_interface,
4457 shell, bind_desktop_shell) == NULL)
4458 return -1;
4459
Pekka Paalanen6e168112011-11-24 11:34:05 +02004460 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4461 shell, bind_screensaver) == NULL)
4462 return -1;
4463
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004464 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004465 shell, bind_input_panel) == NULL)
4466 return -1;
4467
Jonas Ådahle9d22502012-08-29 22:13:01 +02004468 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4469 shell, bind_workspace_manager) == NULL)
4470 return -1;
4471
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004472 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004473
4474 loop = wl_display_get_event_loop(ec->wl_display);
4475 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004476
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004477 shell->screensaver.timer =
4478 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4479
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004480 wl_list_for_each(seat, &ec->seat_list, link)
4481 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004482
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004483 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004484
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004485 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004486
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004487 return 0;
4488}