blob: ebeef55f64d379011010a6ae7f61897f1dcf18b1 [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
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400382shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200383{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400384 struct weston_config_section *section;
385 int duration;
386 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200387
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400388 section = weston_config_get_section(shell->compositor->config,
389 "screensaver", NULL, NULL);
390 weston_config_section_get_string(section,
391 "path", &shell->screensaver.path, NULL);
392 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200393 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400394
395 section = weston_config_get_section(shell->compositor->config,
396 "shell", NULL, NULL);
397 weston_config_section_get_string(section,
398 "binding-modifier", &s, "super");
399 shell->binding_modifier = get_modifier(s);
400 weston_config_section_get_string(section, "animation", &s, "none");
401 shell->win_animation_type = get_animation_type(s);
402 weston_config_section_get_uint(section, "num-workspaces",
403 &shell->workspaces.num,
404 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200405}
406
407static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200408focus_state_destroy(struct focus_state *state)
409{
410 wl_list_remove(&state->seat_destroy_listener.link);
411 wl_list_remove(&state->surface_destroy_listener.link);
412 free(state);
413}
414
415static void
416focus_state_seat_destroy(struct wl_listener *listener, void *data)
417{
418 struct focus_state *state = container_of(listener,
419 struct focus_state,
420 seat_destroy_listener);
421
422 wl_list_remove(&state->link);
423 focus_state_destroy(state);
424}
425
426static void
427focus_state_surface_destroy(struct wl_listener *listener, void *data)
428{
429 struct focus_state *state = container_of(listener,
430 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400431 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400432 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300433 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400434 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200435
Pekka Paalanen01388e22013-04-25 13:57:44 +0300436 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
437
Kristian Høgsberge3778222012-07-31 17:29:30 -0400438 next = NULL;
439 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300440 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400441 continue;
442
443 next = surface;
444 break;
445 }
446
Pekka Paalanen01388e22013-04-25 13:57:44 +0300447 /* if the focus was a sub-surface, activate its main surface */
448 if (main_surface != state->keyboard_focus)
449 next = main_surface;
450
Kristian Høgsberge3778222012-07-31 17:29:30 -0400451 if (next) {
452 shell = state->seat->compositor->shell_interface.shell;
453 activate(shell, next, state->seat);
454 } else {
455 wl_list_remove(&state->link);
456 focus_state_destroy(state);
457 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200458}
459
460static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400461focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200462{
Jonas Ådahl04769742012-06-13 00:01:24 +0200463 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200464
465 state = malloc(sizeof *state);
466 if (state == NULL)
467 return NULL;
468
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400469 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200470 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400471 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200472
473 state->seat_destroy_listener.notify = focus_state_seat_destroy;
474 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400475 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200476 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400477 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200478
479 return state;
480}
481
Jonas Ådahl8538b222012-08-29 22:13:03 +0200482static struct focus_state *
483ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
484{
485 struct workspace *ws = get_current_workspace(shell);
486 struct focus_state *state;
487
488 wl_list_for_each(state, &ws->focus_list, link)
489 if (state->seat == seat)
490 break;
491
492 if (&state->link == &ws->focus_list)
493 state = focus_state_create(seat, ws);
494
495 return state;
496}
497
Jonas Ådahl04769742012-06-13 00:01:24 +0200498static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400499restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200500{
501 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400502 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200503
504 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400505 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200506
Kristian Høgsberge3148752013-05-06 23:19:49 -0400507 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200508 }
509}
510
511static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200512replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
513 struct weston_seat *seat)
514{
515 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400516 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200517
518 wl_list_for_each(state, &ws->focus_list, link) {
519 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400520 surface = seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200521 state->keyboard_focus =
522 (struct weston_surface *) surface;
523 return;
524 }
525 }
526}
527
528static void
529drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
530 struct weston_surface *surface)
531{
532 struct focus_state *state;
533
534 wl_list_for_each(state, &ws->focus_list, link)
535 if (state->keyboard_focus == surface)
536 state->keyboard_focus = NULL;
537}
538
539static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200540workspace_destroy(struct workspace *ws)
541{
Jonas Ådahl04769742012-06-13 00:01:24 +0200542 struct focus_state *state, *next;
543
544 wl_list_for_each_safe(state, next, &ws->focus_list, link)
545 focus_state_destroy(state);
546
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200547 free(ws);
548}
549
Jonas Ådahl04769742012-06-13 00:01:24 +0200550static void
551seat_destroyed(struct wl_listener *listener, void *data)
552{
553 struct weston_seat *seat = data;
554 struct focus_state *state, *next;
555 struct workspace *ws = container_of(listener,
556 struct workspace,
557 seat_destroyed_listener);
558
559 wl_list_for_each_safe(state, next, &ws->focus_list, link)
560 if (state->seat == seat)
561 wl_list_remove(&state->link);
562}
563
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200564static struct workspace *
565workspace_create(void)
566{
567 struct workspace *ws = malloc(sizeof *ws);
568 if (ws == NULL)
569 return NULL;
570
571 weston_layer_init(&ws->layer, NULL);
572
Jonas Ådahl04769742012-06-13 00:01:24 +0200573 wl_list_init(&ws->focus_list);
574 wl_list_init(&ws->seat_destroyed_listener.link);
575 ws->seat_destroyed_listener.notify = seat_destroyed;
576
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200577 return ws;
578}
579
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200580static int
581workspace_is_empty(struct workspace *ws)
582{
583 return wl_list_empty(&ws->layer.surface_list);
584}
585
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200586static struct workspace *
587get_workspace(struct desktop_shell *shell, unsigned int index)
588{
589 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200590 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200591 pws += index;
592 return *pws;
593}
594
595static struct workspace *
596get_current_workspace(struct desktop_shell *shell)
597{
598 return get_workspace(shell, shell->workspaces.current);
599}
600
601static void
602activate_workspace(struct desktop_shell *shell, unsigned int index)
603{
604 struct workspace *ws;
605
606 ws = get_workspace(shell, index);
607 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
608
609 shell->workspaces.current = index;
610}
611
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200612static unsigned int
613get_output_height(struct weston_output *output)
614{
615 return abs(output->region.extents.y1 - output->region.extents.y2);
616}
617
618static void
619surface_translate(struct weston_surface *surface, double d)
620{
621 struct shell_surface *shsurf = get_shell_surface(surface);
622 struct weston_transform *transform;
623
624 transform = &shsurf->workspace_transform;
625 if (wl_list_empty(&transform->link))
626 wl_list_insert(surface->geometry.transformation_list.prev,
627 &shsurf->workspace_transform.link);
628
629 weston_matrix_init(&shsurf->workspace_transform.matrix);
630 weston_matrix_translate(&shsurf->workspace_transform.matrix,
631 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200632 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200633}
634
635static void
636workspace_translate_out(struct workspace *ws, double fraction)
637{
638 struct weston_surface *surface;
639 unsigned int height;
640 double d;
641
642 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
643 height = get_output_height(surface->output);
644 d = height * fraction;
645
646 surface_translate(surface, d);
647 }
648}
649
650static void
651workspace_translate_in(struct workspace *ws, double fraction)
652{
653 struct weston_surface *surface;
654 unsigned int height;
655 double d;
656
657 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
658 height = get_output_height(surface->output);
659
660 if (fraction > 0)
661 d = -(height - height * fraction);
662 else
663 d = height + height * fraction;
664
665 surface_translate(surface, d);
666 }
667}
668
669static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200670broadcast_current_workspace_state(struct desktop_shell *shell)
671{
672 struct wl_resource *resource;
673
674 wl_list_for_each(resource, &shell->workspaces.client_list, link)
675 workspace_manager_send_state(resource,
676 shell->workspaces.current,
677 shell->workspaces.num);
678}
679
680static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200681reverse_workspace_change_animation(struct desktop_shell *shell,
682 unsigned int index,
683 struct workspace *from,
684 struct workspace *to)
685{
686 shell->workspaces.current = index;
687
688 shell->workspaces.anim_to = to;
689 shell->workspaces.anim_from = from;
690 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
691 shell->workspaces.anim_timestamp = 0;
692
Scott Moreau4272e632012-08-13 09:58:41 -0600693 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200694}
695
696static void
697workspace_deactivate_transforms(struct workspace *ws)
698{
699 struct weston_surface *surface;
700 struct shell_surface *shsurf;
701
702 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
703 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200704 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
705 wl_list_remove(&shsurf->workspace_transform.link);
706 wl_list_init(&shsurf->workspace_transform.link);
707 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200708 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200709 }
710}
711
712static void
713finish_workspace_change_animation(struct desktop_shell *shell,
714 struct workspace *from,
715 struct workspace *to)
716{
Scott Moreau4272e632012-08-13 09:58:41 -0600717 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200718
719 wl_list_remove(&shell->workspaces.animation.link);
720 workspace_deactivate_transforms(from);
721 workspace_deactivate_transforms(to);
722 shell->workspaces.anim_to = NULL;
723
724 wl_list_remove(&shell->workspaces.anim_from->layer.link);
725}
726
727static void
728animate_workspace_change_frame(struct weston_animation *animation,
729 struct weston_output *output, uint32_t msecs)
730{
731 struct desktop_shell *shell =
732 container_of(animation, struct desktop_shell,
733 workspaces.animation);
734 struct workspace *from = shell->workspaces.anim_from;
735 struct workspace *to = shell->workspaces.anim_to;
736 uint32_t t;
737 double x, y;
738
739 if (workspace_is_empty(from) && workspace_is_empty(to)) {
740 finish_workspace_change_animation(shell, from, to);
741 return;
742 }
743
744 if (shell->workspaces.anim_timestamp == 0) {
745 if (shell->workspaces.anim_current == 0.0)
746 shell->workspaces.anim_timestamp = msecs;
747 else
748 shell->workspaces.anim_timestamp =
749 msecs -
750 /* Invers of movement function 'y' below. */
751 (asin(1.0 - shell->workspaces.anim_current) *
752 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
753 M_2_PI);
754 }
755
756 t = msecs - shell->workspaces.anim_timestamp;
757
758 /*
759 * x = [0, π/2]
760 * y(x) = sin(x)
761 */
762 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
763 y = sin(x);
764
765 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600766 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200767
768 workspace_translate_out(from, shell->workspaces.anim_dir * y);
769 workspace_translate_in(to, shell->workspaces.anim_dir * y);
770 shell->workspaces.anim_current = y;
771
Scott Moreau4272e632012-08-13 09:58:41 -0600772 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200773 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200774 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200775 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200776}
777
778static void
779animate_workspace_change(struct desktop_shell *shell,
780 unsigned int index,
781 struct workspace *from,
782 struct workspace *to)
783{
784 struct weston_output *output;
785
786 int dir;
787
788 if (index > shell->workspaces.current)
789 dir = -1;
790 else
791 dir = 1;
792
793 shell->workspaces.current = index;
794
795 shell->workspaces.anim_dir = dir;
796 shell->workspaces.anim_from = from;
797 shell->workspaces.anim_to = to;
798 shell->workspaces.anim_current = 0.0;
799 shell->workspaces.anim_timestamp = 0;
800
801 output = container_of(shell->compositor->output_list.next,
802 struct weston_output, link);
803 wl_list_insert(&output->animation_list,
804 &shell->workspaces.animation.link);
805
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200806 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200807
808 workspace_translate_in(to, 0);
809
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400810 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200811
Scott Moreau4272e632012-08-13 09:58:41 -0600812 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200813}
814
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200815static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200816update_workspace(struct desktop_shell *shell, unsigned int index,
817 struct workspace *from, struct workspace *to)
818{
819 shell->workspaces.current = index;
820 wl_list_insert(&from->layer.link, &to->layer.link);
821 wl_list_remove(&from->layer.link);
822}
823
824static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200825change_workspace(struct desktop_shell *shell, unsigned int index)
826{
827 struct workspace *from;
828 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200829
830 if (index == shell->workspaces.current)
831 return;
832
833 /* Don't change workspace when there is any fullscreen surfaces. */
834 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
835 return;
836
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200837 from = get_current_workspace(shell);
838 to = get_workspace(shell, index);
839
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200840 if (shell->workspaces.anim_from == to &&
841 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200842 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200843 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200844 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200845 return;
846 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200847
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200848 if (shell->workspaces.anim_to != NULL)
849 finish_workspace_change_animation(shell,
850 shell->workspaces.anim_from,
851 shell->workspaces.anim_to);
852
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200853 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200854
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200855 if (workspace_is_empty(to) && workspace_is_empty(from))
856 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857 else
858 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200859
860 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200861}
862
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200863static bool
864workspace_has_only(struct workspace *ws, struct weston_surface *surface)
865{
866 struct wl_list *list = &ws->layer.surface_list;
867 struct wl_list *e;
868
869 if (wl_list_empty(list))
870 return false;
871
872 e = list->next;
873
874 if (e->next != list)
875 return false;
876
877 return container_of(e, struct weston_surface, layer_link) == surface;
878}
879
880static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200881move_surface_to_workspace(struct desktop_shell *shell,
882 struct weston_surface *surface,
883 uint32_t workspace)
884{
885 struct workspace *from;
886 struct workspace *to;
887 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300888 struct weston_surface *focus;
889
890 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200891
892 if (workspace == shell->workspaces.current)
893 return;
894
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200895 if (workspace >= shell->workspaces.num)
896 workspace = shell->workspaces.num - 1;
897
Jonas Ådahle9d22502012-08-29 22:13:01 +0200898 from = get_current_workspace(shell);
899 to = get_workspace(shell, workspace);
900
901 wl_list_remove(&surface->layer_link);
902 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
903
904 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300905 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
906 if (!seat->keyboard)
907 continue;
908
909 focus = weston_surface_get_main_surface(seat->keyboard->focus);
910 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400911 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300912 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200913
914 weston_surface_damage_below(surface);
915}
916
917static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200918take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400919 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200920 unsigned int index)
921{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300922 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200923 struct shell_surface *shsurf;
924 struct workspace *from;
925 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200926 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200927
Pekka Paalanen01388e22013-04-25 13:57:44 +0300928 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929 if (surface == NULL ||
930 index == shell->workspaces.current)
931 return;
932
933 from = get_current_workspace(shell);
934 to = get_workspace(shell, index);
935
936 wl_list_remove(&surface->layer_link);
937 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
938
Jonas Ådahle9d22502012-08-29 22:13:01 +0200939 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200940 drop_focus_state(shell, from, surface);
941
942 if (shell->workspaces.anim_from == to &&
943 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200944 wl_list_remove(&to->layer.link);
945 wl_list_insert(from->layer.link.prev, &to->layer.link);
946
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200947 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200948 broadcast_current_workspace_state(shell);
949
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200950 return;
951 }
952
953 if (shell->workspaces.anim_to != NULL)
954 finish_workspace_change_animation(shell,
955 shell->workspaces.anim_from,
956 shell->workspaces.anim_to);
957
958 if (workspace_is_empty(from) &&
959 workspace_has_only(to, surface))
960 update_workspace(shell, index, from, to);
961 else {
962 shsurf = get_shell_surface(surface);
963 if (wl_list_empty(&shsurf->workspace_transform.link))
964 wl_list_insert(&shell->workspaces.anim_sticky_list,
965 &shsurf->workspace_transform.link);
966
967 animate_workspace_change(shell, index, from, to);
968 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200969
970 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200971
972 state = ensure_focus_state(shell, seat);
973 if (state != NULL)
974 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200975}
976
977static void
978workspace_manager_move_surface(struct wl_client *client,
979 struct wl_resource *resource,
980 struct wl_resource *surface_resource,
981 uint32_t workspace)
982{
983 struct desktop_shell *shell = resource->data;
984 struct weston_surface *surface =
985 (struct weston_surface *) surface_resource;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300986 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200987
Pekka Paalanen01388e22013-04-25 13:57:44 +0300988 main_surface = weston_surface_get_main_surface(surface);
989 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200990}
991
992static const struct workspace_manager_interface workspace_manager_implementation = {
993 workspace_manager_move_surface,
994};
995
996static void
997unbind_resource(struct wl_resource *resource)
998{
999 wl_list_remove(&resource->link);
1000 free(resource);
1001}
1002
1003static void
1004bind_workspace_manager(struct wl_client *client,
1005 void *data, uint32_t version, uint32_t id)
1006{
1007 struct desktop_shell *shell = data;
1008 struct wl_resource *resource;
1009
1010 resource = wl_client_add_object(client, &workspace_manager_interface,
1011 &workspace_manager_implementation,
1012 id, shell);
1013
1014 if (resource == NULL) {
1015 weston_log("couldn't add workspace manager object");
1016 return;
1017 }
1018
1019 resource->destroy = unbind_resource;
1020 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1021
1022 workspace_manager_send_state(resource,
1023 shell->workspaces.current,
1024 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001025}
1026
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001027static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001028noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001029{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001030}
1031
1032static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001033move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001034{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001035 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001036 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001037 struct shell_surface *shsurf = move->base.shsurf;
1038 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001039 int dx = wl_fixed_to_int(pointer->x + move->dx);
1040 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001041
1042 if (!shsurf)
1043 return;
1044
1045 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001046
Daniel Stone103db7f2012-05-08 17:17:55 +01001047 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001048 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001049
1050 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001051}
1052
1053static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001054move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001055 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001057 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1058 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001059 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001060 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001061
Daniel Stone4dbadb12012-05-30 16:31:51 +01001062 if (pointer->button_count == 0 &&
1063 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001064 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001065 free(grab);
1066 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001067}
1068
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001069static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001070 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001071 move_grab_motion,
1072 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001073};
1074
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001075static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001076surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001077{
1078 struct weston_move_grab *move;
1079
1080 if (!shsurf)
1081 return -1;
1082
1083 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1084 return 0;
1085
1086 move = malloc(sizeof *move);
1087 if (!move)
1088 return -1;
1089
1090 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001091 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001092 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001093 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001094
1095 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001096 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001097
1098 return 0;
1099}
1100
1101static void
1102shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1103 struct wl_resource *seat_resource, uint32_t serial)
1104{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001105 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001106 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001107 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001108
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001109 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001110 if (seat->pointer->button_count == 0 ||
1111 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001112 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001113 return;
1114
Kristian Høgsberge3148752013-05-06 23:19:49 -04001115 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001116 wl_resource_post_no_memory(resource);
1117}
1118
1119struct weston_resize_grab {
1120 struct shell_grab base;
1121 uint32_t edges;
1122 int32_t width, height;
1123};
1124
1125static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001126resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001127{
1128 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001129 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001130 struct shell_surface *shsurf = resize->base.shsurf;
1131 int32_t width, height;
1132 wl_fixed_t from_x, from_y;
1133 wl_fixed_t to_x, to_y;
1134
1135 if (!shsurf)
1136 return;
1137
1138 weston_surface_from_global_fixed(shsurf->surface,
1139 pointer->grab_x, pointer->grab_y,
1140 &from_x, &from_y);
1141 weston_surface_from_global_fixed(shsurf->surface,
1142 pointer->x, pointer->y, &to_x, &to_y);
1143
1144 width = resize->width;
1145 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1146 width += wl_fixed_to_int(from_x - to_x);
1147 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1148 width += wl_fixed_to_int(to_x - from_x);
1149 }
1150
1151 height = resize->height;
1152 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1153 height += wl_fixed_to_int(from_y - to_y);
1154 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1155 height += wl_fixed_to_int(to_y - from_y);
1156 }
1157
1158 shsurf->client->send_configure(shsurf->surface,
1159 resize->edges, width, height);
1160}
1161
1162static void
1163send_configure(struct weston_surface *surface,
1164 uint32_t edges, int32_t width, int32_t height)
1165{
1166 struct shell_surface *shsurf = get_shell_surface(surface);
1167
1168 wl_shell_surface_send_configure(&shsurf->resource,
1169 edges, width, height);
1170}
1171
1172static const struct weston_shell_client shell_client = {
1173 send_configure
1174};
1175
1176static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001177resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001178 uint32_t time, uint32_t button, uint32_t state_w)
1179{
1180 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001181 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001182 enum wl_pointer_button_state state = state_w;
1183
1184 if (pointer->button_count == 0 &&
1185 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1186 shell_grab_end(&resize->base);
1187 free(grab);
1188 }
1189}
1190
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001191static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001192 noop_grab_focus,
1193 resize_grab_motion,
1194 resize_grab_button,
1195};
1196
Giulio Camuffob8366642013-04-25 13:57:46 +03001197/*
1198 * Returns the bounding box of a surface and all its sub-surfaces,
1199 * in the surface coordinates system. */
1200static void
1201surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1202 int32_t *y, int32_t *w, int32_t *h) {
1203 pixman_region32_t region;
1204 pixman_box32_t *box;
1205 struct weston_subsurface *subsurface;
1206
1207 pixman_region32_init_rect(&region, 0, 0,
1208 surface->geometry.width,
1209 surface->geometry.height);
1210
1211 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1212 pixman_region32_union_rect(&region, &region,
1213 subsurface->position.x,
1214 subsurface->position.y,
1215 subsurface->surface->geometry.width,
1216 subsurface->surface->geometry.height);
1217 }
1218
1219 box = pixman_region32_extents(&region);
1220 if (x)
1221 *x = box->x1;
1222 if (y)
1223 *y = box->y1;
1224 if (w)
1225 *w = box->x2 - box->x1;
1226 if (h)
1227 *h = box->y2 - box->y1;
1228
1229 pixman_region32_fini(&region);
1230}
1231
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001232static int
1233surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001234 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001235{
1236 struct weston_resize_grab *resize;
1237
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001238 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1239 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001240 return 0;
1241
1242 if (edges == 0 || edges > 15 ||
1243 (edges & 3) == 3 || (edges & 12) == 12)
1244 return 0;
1245
1246 resize = malloc(sizeof *resize);
1247 if (!resize)
1248 return -1;
1249
1250 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001251 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1252 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001253
1254 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001255 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001256
1257 return 0;
1258}
1259
1260static void
1261shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1262 struct wl_resource *seat_resource, uint32_t serial,
1263 uint32_t edges)
1264{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001265 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001266 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001267 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001268
1269 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1270 return;
1271
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001272 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001273 if (seat->pointer->button_count == 0 ||
1274 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001275 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001276 return;
1277
Kristian Høgsberge3148752013-05-06 23:19:49 -04001278 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001279 wl_resource_post_no_memory(resource);
1280}
1281
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001282static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001283busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001284{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001285 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001286 struct weston_pointer *pointer = base->pointer;
1287 struct weston_surface *surface;
1288 wl_fixed_t sx, sy;
1289
1290 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1291 pointer->x, pointer->y,
1292 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001293
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001294 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001295 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001296 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001297 }
1298}
1299
1300static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001301busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001302{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001303}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001304
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001305static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001306busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001307 uint32_t time, uint32_t button, uint32_t state)
1308{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001309 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001310 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001311 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001312
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001313 if (shsurf && button == BTN_LEFT && state) {
1314 activate(shsurf->shell, shsurf->surface, seat);
1315 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001316 } else if (shsurf && button == BTN_RIGHT && state) {
1317 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001318 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001319 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001320}
1321
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001322static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001323 busy_cursor_grab_focus,
1324 busy_cursor_grab_motion,
1325 busy_cursor_grab_button,
1326};
1327
1328static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001329set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001330{
1331 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001332
1333 grab = malloc(sizeof *grab);
1334 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001335 return;
1336
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001337 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1338 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001339}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001340
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001341static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001342end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001343{
1344 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1345
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001346 if (grab->grab.interface == &busy_cursor_grab_interface &&
1347 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001348 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001349 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001350 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001351}
1352
Scott Moreau9521d5e2012-04-19 13:06:17 -06001353static void
1354ping_timer_destroy(struct shell_surface *shsurf)
1355{
1356 if (!shsurf || !shsurf->ping_timer)
1357 return;
1358
1359 if (shsurf->ping_timer->source)
1360 wl_event_source_remove(shsurf->ping_timer->source);
1361
1362 free(shsurf->ping_timer);
1363 shsurf->ping_timer = NULL;
1364}
1365
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001366static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001367ping_timeout_handler(void *data)
1368{
1369 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001370 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001371
Scott Moreau9521d5e2012-04-19 13:06:17 -06001372 /* Client is not responding */
1373 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001374
1375 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001376 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001377 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001378
1379 return 1;
1380}
1381
1382static void
1383ping_handler(struct weston_surface *surface, uint32_t serial)
1384{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001385 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001386 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001387 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001388
1389 if (!shsurf)
1390 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001391 if (!shsurf->resource.client)
1392 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001393
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001394 if (shsurf->surface == shsurf->shell->grab_surface)
1395 return;
1396
Scott Moreauff1db4a2012-04-17 19:06:18 -06001397 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001398 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001399 if (!shsurf->ping_timer)
1400 return;
1401
1402 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001403 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1404 shsurf->ping_timer->source =
1405 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1406 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1407
1408 wl_shell_surface_send_ping(&shsurf->resource, serial);
1409 }
1410}
1411
1412static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001413handle_pointer_focus(struct wl_listener *listener, void *data)
1414{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001415 struct weston_pointer *pointer = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001416 struct weston_surface *surface =
1417 (struct weston_surface *) pointer->focus;
1418 struct weston_compositor *compositor;
1419 struct shell_surface *shsurf;
1420 uint32_t serial;
1421
1422 if (!surface)
1423 return;
1424
1425 compositor = surface->compositor;
1426 shsurf = get_shell_surface(surface);
1427
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001428 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001429 set_busy_cursor(shsurf, pointer);
1430 } else {
1431 serial = wl_display_next_serial(compositor->wl_display);
1432 ping_handler(surface, serial);
1433 }
1434}
1435
1436static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001437create_pointer_focus_listener(struct weston_seat *seat)
1438{
1439 struct wl_listener *listener;
1440
Kristian Høgsberge3148752013-05-06 23:19:49 -04001441 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001442 return;
1443
1444 listener = malloc(sizeof *listener);
1445 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001446 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001447}
1448
1449static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001450shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1451 uint32_t serial)
1452{
1453 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001454 struct weston_seat *seat;
1455 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001456
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001457 if (shsurf->ping_timer == NULL)
1458 /* Just ignore unsolicited pong. */
1459 return;
1460
Scott Moreauff1db4a2012-04-17 19:06:18 -06001461 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001462 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001463 wl_list_for_each(seat, &ec->seat_list, link) {
1464 if(seat->pointer)
1465 end_busy_cursor(shsurf, seat->pointer);
1466 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001467 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001468 }
1469}
1470
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001471static void
1472shell_surface_set_title(struct wl_client *client,
1473 struct wl_resource *resource, const char *title)
1474{
1475 struct shell_surface *shsurf = resource->data;
1476
1477 free(shsurf->title);
1478 shsurf->title = strdup(title);
1479}
1480
1481static void
1482shell_surface_set_class(struct wl_client *client,
1483 struct wl_resource *resource, const char *class)
1484{
1485 struct shell_surface *shsurf = resource->data;
1486
1487 free(shsurf->class);
1488 shsurf->class = strdup(class);
1489}
1490
Juan Zhao96879df2012-02-07 08:45:41 +08001491static struct weston_output *
1492get_default_output(struct weston_compositor *compositor)
1493{
1494 return container_of(compositor->output_list.next,
1495 struct weston_output, link);
1496}
1497
Alex Wu4539b082012-03-01 12:57:46 +08001498static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001499restore_output_mode(struct weston_output *output)
1500{
1501 if (output->current != output->origin ||
1502 (int32_t)output->scale != output->origin_scale)
1503 weston_output_switch_mode(output,
1504 output->origin,
1505 output->origin_scale);
1506}
1507
1508static void
1509restore_all_output_modes(struct weston_compositor *compositor)
1510{
1511 struct weston_output *output;
1512
1513 wl_list_for_each(output, &compositor->output_list, link)
1514 restore_output_mode(output);
1515}
1516
1517static void
Alex Wu4539b082012-03-01 12:57:46 +08001518shell_unset_fullscreen(struct shell_surface *shsurf)
1519{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001520 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001521 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001522 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1523 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001524 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001525 }
Alex Wu4539b082012-03-01 12:57:46 +08001526 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1527 shsurf->fullscreen.framerate = 0;
1528 wl_list_remove(&shsurf->fullscreen.transform.link);
1529 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001530 if (shsurf->fullscreen.black_surface)
1531 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001532 shsurf->fullscreen.black_surface = NULL;
1533 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001534 weston_surface_set_position(shsurf->surface,
1535 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001536 if (shsurf->saved_rotation_valid) {
1537 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1538 &shsurf->rotation.transform.link);
1539 shsurf->saved_rotation_valid = false;
1540 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001541
1542 ws = get_current_workspace(shsurf->shell);
1543 wl_list_remove(&shsurf->surface->layer_link);
1544 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001545}
1546
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001547static void
1548shell_unset_maximized(struct shell_surface *shsurf)
1549{
1550 struct workspace *ws;
1551 /* undo all maximized things here */
1552 shsurf->output = get_default_output(shsurf->surface->compositor);
1553 weston_surface_set_position(shsurf->surface,
1554 shsurf->saved_x,
1555 shsurf->saved_y);
1556
1557 if (shsurf->saved_rotation_valid) {
1558 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1559 &shsurf->rotation.transform.link);
1560 shsurf->saved_rotation_valid = false;
1561 }
1562
1563 ws = get_current_workspace(shsurf->shell);
1564 wl_list_remove(&shsurf->surface->layer_link);
1565 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1566}
1567
Pekka Paalanen98262232011-12-01 10:42:22 +02001568static int
1569reset_shell_surface_type(struct shell_surface *surface)
1570{
1571 switch (surface->type) {
1572 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001573 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001574 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001575 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001576 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001577 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001578 case SHELL_SURFACE_NONE:
1579 case SHELL_SURFACE_TOPLEVEL:
1580 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001581 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001582 break;
1583 }
1584
1585 surface->type = SHELL_SURFACE_NONE;
1586 return 0;
1587}
1588
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001589static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001590set_surface_type(struct shell_surface *shsurf)
1591{
1592 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001593 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001594
1595 reset_shell_surface_type(shsurf);
1596
1597 shsurf->type = shsurf->next_type;
1598 shsurf->next_type = SHELL_SURFACE_NONE;
1599
1600 switch (shsurf->type) {
1601 case SHELL_SURFACE_TOPLEVEL:
1602 break;
1603 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001604 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001605 pes->geometry.x + shsurf->transient.x,
1606 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001607 break;
1608
1609 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001610 case SHELL_SURFACE_FULLSCREEN:
1611 shsurf->saved_x = surface->geometry.x;
1612 shsurf->saved_y = surface->geometry.y;
1613 shsurf->saved_position_valid = true;
1614
1615 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1616 wl_list_remove(&shsurf->rotation.transform.link);
1617 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001618 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001619 shsurf->saved_rotation_valid = true;
1620 }
1621 break;
1622
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001623 default:
1624 break;
1625 }
1626}
1627
1628static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001629set_toplevel(struct shell_surface *shsurf)
1630{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001631 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001632}
1633
1634static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001635shell_surface_set_toplevel(struct wl_client *client,
1636 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001637{
Pekka Paalanen98262232011-12-01 10:42:22 +02001638 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001639
Tiago Vignattibc052c92012-04-19 16:18:18 +03001640 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001641}
1642
1643static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001644set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001645 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001646{
1647 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001648 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001649 shsurf->transient.x = x;
1650 shsurf->transient.y = y;
1651 shsurf->transient.flags = flags;
1652 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1653}
1654
1655static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001656shell_surface_set_transient(struct wl_client *client,
1657 struct wl_resource *resource,
1658 struct wl_resource *parent_resource,
1659 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001660{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001661 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001662 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001663
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001664 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001665}
1666
Tiago Vignattibe143262012-04-16 17:31:41 +03001667static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001668shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001669{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001670 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001671}
1672
1673static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001674get_output_panel_height(struct desktop_shell *shell,
1675 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001676{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001677 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001678 int panel_height = 0;
1679
1680 if (!output)
1681 return 0;
1682
Juan Zhao4ab94682012-07-09 22:24:09 -07001683 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001684 if (surface->output == output) {
1685 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001686 break;
1687 }
1688 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001689
Juan Zhao96879df2012-02-07 08:45:41 +08001690 return panel_height;
1691}
1692
1693static void
1694shell_surface_set_maximized(struct wl_client *client,
1695 struct wl_resource *resource,
1696 struct wl_resource *output_resource )
1697{
1698 struct shell_surface *shsurf = resource->data;
1699 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001700 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001701 uint32_t edges = 0, panel_height = 0;
1702
1703 /* get the default output, if the client set it as NULL
1704 check whether the ouput is available */
1705 if (output_resource)
1706 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001707 else if (es->output)
1708 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001709 else
1710 shsurf->output = get_default_output(es->compositor);
1711
Tiago Vignattibe143262012-04-16 17:31:41 +03001712 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001713 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001714 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001715
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001716 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001717 shsurf->output->width,
1718 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001719
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001720 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001721}
1722
Alex Wu21858432012-04-01 20:13:08 +08001723static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001724black_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 +08001725
Alex Wu4539b082012-03-01 12:57:46 +08001726static struct weston_surface *
1727create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001728 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001729 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001730{
1731 struct weston_surface *surface = NULL;
1732
1733 surface = weston_surface_create(ec);
1734 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001735 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001736 return NULL;
1737 }
1738
Alex Wu21858432012-04-01 20:13:08 +08001739 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001740 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001741 weston_surface_configure(surface, x, y, w, h);
1742 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001743 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001744 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001745 pixman_region32_fini(&surface->input);
1746 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001747
Alex Wu4539b082012-03-01 12:57:46 +08001748 return surface;
1749}
1750
1751/* Create black surface and append it to the associated fullscreen surface.
1752 * Handle size dismatch and positioning according to the method. */
1753static void
1754shell_configure_fullscreen(struct shell_surface *shsurf)
1755{
1756 struct weston_output *output = shsurf->fullscreen_output;
1757 struct weston_surface *surface = shsurf->surface;
1758 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001759 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001760 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001761
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001762 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1763 restore_output_mode(output);
1764
Alex Wu4539b082012-03-01 12:57:46 +08001765 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001766 shsurf->fullscreen.black_surface =
1767 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001768 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001769 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001770 output->width,
1771 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001772
1773 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1774 wl_list_insert(&surface->layer_link,
1775 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001776 shsurf->fullscreen.black_surface->output = output;
1777
Giulio Camuffob8366642013-04-25 13:57:46 +03001778 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1779 &surf_width, &surf_height);
1780
Alex Wu4539b082012-03-01 12:57:46 +08001781 switch (shsurf->fullscreen.type) {
1782 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001783 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001784 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001785 break;
1786 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001787 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001788 if (output->width == surf_width &&
1789 output->height == surf_height) {
1790 weston_surface_set_position(surface, output->x - surf_x,
1791 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001792 break;
1793 }
1794
Alex Wu4539b082012-03-01 12:57:46 +08001795 matrix = &shsurf->fullscreen.transform.matrix;
1796 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001797
Scott Moreau1bad5db2012-08-18 01:04:05 -06001798 output_aspect = (float) output->width /
1799 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001800 surface_aspect = (float) surface->geometry.width /
1801 (float) surface->geometry.height;
1802 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001803 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001804 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001805 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001806 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001807 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001808
Alex Wu4539b082012-03-01 12:57:46 +08001809 weston_matrix_scale(matrix, scale, scale, 1);
1810 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001811 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001812 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001813 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1814 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001815 weston_surface_set_position(surface, x, y);
1816
Alex Wu4539b082012-03-01 12:57:46 +08001817 break;
1818 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001819 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001820 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001821 surf_width * surface->buffer_scale,
1822 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001823 shsurf->fullscreen.framerate};
1824
Alexander Larsson355748e2013-05-28 16:23:38 +02001825 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001826 weston_surface_set_position(surface,
1827 output->x - surf_x,
1828 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001829 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001830 output->x - surf_x,
1831 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001832 output->width,
1833 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001834 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001835 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001836 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001837 center_on_output(surface, output);
1838 }
Alex Wubd3354b2012-04-17 17:20:49 +08001839 }
Alex Wu4539b082012-03-01 12:57:46 +08001840 break;
1841 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001842 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001843 break;
1844 default:
1845 break;
1846 }
1847}
1848
1849/* make the fullscreen and black surface at the top */
1850static void
1851shell_stack_fullscreen(struct shell_surface *shsurf)
1852{
Alex Wubd3354b2012-04-17 17:20:49 +08001853 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001854 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001855 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001856
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001857 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001858 wl_list_insert(&shell->fullscreen_layer.surface_list,
1859 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001860 weston_surface_damage(surface);
1861
1862 if (!shsurf->fullscreen.black_surface)
1863 shsurf->fullscreen.black_surface =
1864 create_black_surface(surface->compositor,
1865 surface,
1866 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001867 output->width,
1868 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001869
1870 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001871 wl_list_insert(&surface->layer_link,
1872 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001873 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001874}
1875
1876static void
1877shell_map_fullscreen(struct shell_surface *shsurf)
1878{
Alex Wu4539b082012-03-01 12:57:46 +08001879 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001880 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001881}
1882
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001883static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001884set_fullscreen(struct shell_surface *shsurf,
1885 uint32_t method,
1886 uint32_t framerate,
1887 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001888{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001889 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001890
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001891 if (output)
1892 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001893 else if (es->output)
1894 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001895 else
1896 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001897
Alex Wu4539b082012-03-01 12:57:46 +08001898 shsurf->fullscreen_output = shsurf->output;
1899 shsurf->fullscreen.type = method;
1900 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001901 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001902
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001903 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001904 shsurf->output->width,
1905 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001906}
1907
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001908static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001909shell_surface_set_fullscreen(struct wl_client *client,
1910 struct wl_resource *resource,
1911 uint32_t method,
1912 uint32_t framerate,
1913 struct wl_resource *output_resource)
1914{
1915 struct shell_surface *shsurf = resource->data;
1916 struct weston_output *output;
1917
1918 if (output_resource)
1919 output = output_resource->data;
1920 else
1921 output = NULL;
1922
1923 set_fullscreen(shsurf, method, framerate, output);
1924}
1925
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001926static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001927
1928static void
1929destroy_shell_seat(struct wl_listener *listener, void *data)
1930{
1931 struct shell_seat *shseat =
1932 container_of(listener,
1933 struct shell_seat, seat_destroy_listener);
1934 struct shell_surface *shsurf, *prev = NULL;
1935
1936 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001937 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001938 shseat->popup_grab.client = NULL;
1939
1940 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1941 shsurf->popup.shseat = NULL;
1942 if (prev) {
1943 wl_list_init(&prev->popup.grab_link);
1944 }
1945 prev = shsurf;
1946 }
1947 wl_list_init(&prev->popup.grab_link);
1948 }
1949
1950 wl_list_remove(&shseat->seat_destroy_listener.link);
1951 free(shseat);
1952}
1953
1954static struct shell_seat *
1955create_shell_seat(struct weston_seat *seat)
1956{
1957 struct shell_seat *shseat;
1958
1959 shseat = calloc(1, sizeof *shseat);
1960 if (!shseat) {
1961 weston_log("no memory to allocate shell seat\n");
1962 return NULL;
1963 }
1964
1965 shseat->seat = seat;
1966 wl_list_init(&shseat->popup_grab.surfaces_list);
1967
1968 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1969 wl_signal_add(&seat->destroy_signal,
1970 &shseat->seat_destroy_listener);
1971
1972 return shseat;
1973}
1974
1975static struct shell_seat *
1976get_shell_seat(struct weston_seat *seat)
1977{
1978 struct wl_listener *listener;
1979
1980 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1981 if (listener == NULL)
1982 return create_shell_seat(seat);
1983
1984 return container_of(listener,
1985 struct shell_seat, seat_destroy_listener);
1986}
1987
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001988static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001989popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001990{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001991 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001992 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001993 struct shell_seat *shseat =
1994 container_of(grab, struct shell_seat, popup_grab.grab);
1995 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001996 wl_fixed_t sx, sy;
1997
1998 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1999 pointer->x, pointer->y,
2000 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002001
Pekka Paalanencb108432012-01-19 16:25:40 +02002002 if (surface && surface->resource.client == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002003 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002004 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002005 weston_pointer_set_focus(pointer, NULL,
2006 wl_fixed_from_int(0),
2007 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002008 }
2009}
2010
2011static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002012popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002013{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002014 struct weston_pointer *pointer = grab->pointer;
2015 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002016
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002017 if (pointer->focus_resource) {
2018 weston_surface_from_global_fixed(pointer->focus,
2019 pointer->x, pointer->y,
2020 &sx, &sy);
2021 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2022 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002023}
2024
2025static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002026popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002027 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002028{
2029 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002030 struct shell_seat *shseat =
2031 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002032 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002033 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002034 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002035
Daniel Stone37816df2012-05-16 18:45:18 +01002036 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002037 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002038 display = wl_client_get_display(resource->client);
2039 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002040 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002041 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002042 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002043 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002044 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002045 }
2046
Daniel Stone4dbadb12012-05-30 16:31:51 +01002047 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002048 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002049}
2050
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002051static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002052 popup_grab_focus,
2053 popup_grab_motion,
2054 popup_grab_button,
2055};
2056
2057static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002058popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002059{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002060 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002061 struct shell_seat *shseat =
2062 container_of(grab, struct shell_seat, popup_grab.grab);
2063 struct shell_surface *shsurf;
2064 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002065
2066 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002067 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002068 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002069 shseat->popup_grab.grab.interface = NULL;
2070 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002071 /* Send the popup_done event to all the popups open */
2072 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2073 wl_shell_surface_send_popup_done(&shsurf->resource);
2074 shsurf->popup.shseat = NULL;
2075 if (prev) {
2076 wl_list_init(&prev->popup.grab_link);
2077 }
2078 prev = shsurf;
2079 }
2080 wl_list_init(&prev->popup.grab_link);
2081 wl_list_init(&shseat->popup_grab.surfaces_list);
2082 }
2083}
2084
2085static void
2086add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2087{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002088 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002089
2090 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002091 shseat->popup_grab.client = shsurf->resource.client;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002092 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002093 /* We must make sure here that this popup was opened after
2094 * a mouse press, and not just by moving around with other
2095 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002096 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002097 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002098
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002099 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002100 }
2101 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2102}
2103
2104static void
2105remove_popup_grab(struct shell_surface *shsurf)
2106{
2107 struct shell_seat *shseat = shsurf->popup.shseat;
2108
2109 wl_list_remove(&shsurf->popup.grab_link);
2110 wl_list_init(&shsurf->popup.grab_link);
2111 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002112 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002113 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002114 }
2115}
2116
2117static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002118shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002119{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002120 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002121 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002122 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002123
2124 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002125
Pekka Paalanen483243f2013-03-08 14:56:50 +02002126 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002127 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2128 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002129
Kristian Høgsberge3148752013-05-06 23:19:49 -04002130 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002131 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002132 } else {
2133 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002134 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002135 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002136}
2137
2138static void
2139shell_surface_set_popup(struct wl_client *client,
2140 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002141 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002142 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002143 struct wl_resource *parent_resource,
2144 int32_t x, int32_t y, uint32_t flags)
2145{
2146 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002147
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002148 shsurf->type = SHELL_SURFACE_POPUP;
2149 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002150 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002151 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002152 shsurf->popup.x = x;
2153 shsurf->popup.y = y;
2154}
2155
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002156static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002157 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002158 shell_surface_move,
2159 shell_surface_resize,
2160 shell_surface_set_toplevel,
2161 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002162 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002163 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002164 shell_surface_set_maximized,
2165 shell_surface_set_title,
2166 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002167};
2168
2169static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002170destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002171{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002172 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2173 remove_popup_grab(shsurf);
2174 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002175
Alex Wubd3354b2012-04-17 17:20:49 +08002176 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002177 shell_surface_is_top_fullscreen(shsurf))
2178 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002179
Alex Wuaa08e2d2012-03-05 11:01:40 +08002180 if (shsurf->fullscreen.black_surface)
2181 weston_surface_destroy(shsurf->fullscreen.black_surface);
2182
Alex Wubd3354b2012-04-17 17:20:49 +08002183 /* As destroy_resource() use wl_list_for_each_safe(),
2184 * we can always remove the listener.
2185 */
2186 wl_list_remove(&shsurf->surface_destroy_listener.link);
2187 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002188 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002189 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002190
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002191 wl_list_remove(&shsurf->link);
2192 free(shsurf);
2193}
2194
2195static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002196shell_destroy_shell_surface(struct wl_resource *resource)
2197{
2198 struct shell_surface *shsurf = resource->data;
2199
2200 destroy_shell_surface(shsurf);
2201}
2202
2203static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002204shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002205{
2206 struct shell_surface *shsurf = container_of(listener,
2207 struct shell_surface,
2208 surface_destroy_listener);
2209
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002210 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002211 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002212 } else {
2213 wl_signal_emit(&shsurf->resource.destroy_signal,
2214 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002215 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002216 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002217}
2218
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002219static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002220shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002221
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002222static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002223get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002224{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002225 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002226 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002227 else
2228 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002229}
2230
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002231static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002232create_shell_surface(void *shell, struct weston_surface *surface,
2233 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002234{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002235 struct shell_surface *shsurf;
2236
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002237 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002238 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002239 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002240 }
2241
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002242 shsurf = calloc(1, sizeof *shsurf);
2243 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002244 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002245 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002246 }
2247
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002248 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002249 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002250
Tiago Vignattibc052c92012-04-19 16:18:18 +03002251 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002252 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002253 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002254 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002255 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002256 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2257 shsurf->fullscreen.framerate = 0;
2258 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002259 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002260 wl_list_init(&shsurf->fullscreen.transform.link);
2261
Tiago Vignattibc052c92012-04-19 16:18:18 +03002262 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002263 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002264 wl_signal_add(&surface->resource.destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002265 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002266
2267 /* init link so its safe to always remove it in destroy_shell_surface */
2268 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002269 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002270
Pekka Paalanen460099f2012-01-20 16:48:25 +02002271 /* empty when not in use */
2272 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002273 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002274
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002275 wl_list_init(&shsurf->workspace_transform.link);
2276
Pekka Paalanen98262232011-12-01 10:42:22 +02002277 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002278 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002279
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002280 shsurf->client = client;
2281
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002282 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002283}
2284
2285static void
2286shell_get_shell_surface(struct wl_client *client,
2287 struct wl_resource *resource,
2288 uint32_t id,
2289 struct wl_resource *surface_resource)
2290{
2291 struct weston_surface *surface = surface_resource->data;
2292 struct desktop_shell *shell = resource->data;
2293 struct shell_surface *shsurf;
2294
2295 if (get_shell_surface(surface)) {
2296 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002297 WL_DISPLAY_ERROR_INVALID_OBJECT,
2298 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002299 return;
2300 }
2301
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002302 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002303 if (!shsurf) {
2304 wl_resource_post_error(surface_resource,
2305 WL_DISPLAY_ERROR_INVALID_OBJECT,
2306 "surface->configure already set");
2307 return;
2308 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002309
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002310 shsurf->resource.destroy = shell_destroy_shell_surface;
2311 shsurf->resource.object.id = id;
2312 shsurf->resource.object.interface = &wl_shell_surface_interface;
2313 shsurf->resource.object.implementation =
2314 (void (**)(void)) &shell_surface_implementation;
2315 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002316
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002317 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002318}
2319
2320static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002321 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002322};
2323
Kristian Høgsberg07937562011-04-12 17:25:42 -04002324static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002325shell_fade(struct desktop_shell *shell, enum fade_type type);
2326
2327static int
2328screensaver_timeout(void *data)
2329{
2330 struct desktop_shell *shell = data;
2331
2332 shell_fade(shell, FADE_OUT);
2333
2334 return 1;
2335}
2336
2337static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002338handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002339{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002340 struct desktop_shell *shell =
2341 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002342
Pekka Paalanen18027e52011-12-02 16:31:49 +02002343 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002344
2345 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002346 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002347}
2348
2349static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002350launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002351{
2352 if (shell->screensaver.binding)
2353 return;
2354
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002355 if (!shell->screensaver.path) {
2356 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002357 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002358 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002359
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002360 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002361 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002362 return;
2363 }
2364
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002365 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002366 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002367 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002368 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002369}
2370
2371static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002372terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002373{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002374 if (shell->screensaver.process.pid == 0)
2375 return;
2376
2377 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002378}
2379
2380static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002381configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002382{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002383 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002384
Giulio Camuffo184df502013-02-21 11:29:21 +01002385 if (width == 0)
2386 return;
2387
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002388 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2389 if (s->output == es->output && s != es) {
2390 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002391 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002392 }
2393 }
2394
Giulio Camuffo184df502013-02-21 11:29:21 +01002395 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002396
2397 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002398 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002399 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002400 }
2401}
2402
2403static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002404background_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 -04002405{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002406 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002407
Giulio Camuffo184df502013-02-21 11:29:21 +01002408 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002409}
2410
2411static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002412desktop_shell_set_background(struct wl_client *client,
2413 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002414 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002415 struct wl_resource *surface_resource)
2416{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002417 struct desktop_shell *shell = resource->data;
2418 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002419
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002420 if (surface->configure) {
2421 wl_resource_post_error(surface_resource,
2422 WL_DISPLAY_ERROR_INVALID_OBJECT,
2423 "surface role already assigned");
2424 return;
2425 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002426
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002427 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002428 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002429 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002430 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002431 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002432 surface->output->width,
2433 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002434}
2435
2436static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002437panel_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 -04002438{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002439 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002440
Giulio Camuffo184df502013-02-21 11:29:21 +01002441 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002442}
2443
2444static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002445desktop_shell_set_panel(struct wl_client *client,
2446 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002447 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002448 struct wl_resource *surface_resource)
2449{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002450 struct desktop_shell *shell = resource->data;
2451 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002452
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002453 if (surface->configure) {
2454 wl_resource_post_error(surface_resource,
2455 WL_DISPLAY_ERROR_INVALID_OBJECT,
2456 "surface role already assigned");
2457 return;
2458 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002459
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002460 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002461 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002462 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002463 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002464 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002465 surface->output->width,
2466 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002467}
2468
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002469static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002470lock_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 -04002471{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002472 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002473
Giulio Camuffo184df502013-02-21 11:29:21 +01002474 if (width == 0)
2475 return;
2476
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002477 surface->geometry.width = width;
2478 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002479 center_on_output(surface, get_default_output(shell->compositor));
2480
2481 if (!weston_surface_is_mapped(surface)) {
2482 wl_list_insert(&shell->lock_layer.surface_list,
2483 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002484 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002485 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002486 }
2487}
2488
2489static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002490handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002491{
Tiago Vignattibe143262012-04-16 17:31:41 +03002492 struct desktop_shell *shell =
2493 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002494
Martin Minarik6d118362012-06-07 18:01:59 +02002495 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002496 shell->lock_surface = NULL;
2497}
2498
2499static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002500desktop_shell_set_lock_surface(struct wl_client *client,
2501 struct wl_resource *resource,
2502 struct wl_resource *surface_resource)
2503{
Tiago Vignattibe143262012-04-16 17:31:41 +03002504 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002505 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002506
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002507 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002508
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002509 if (!shell->locked)
2510 return;
2511
Pekka Paalanen98262232011-12-01 10:42:22 +02002512 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002513
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002514 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2515 wl_signal_add(&surface_resource->destroy_signal,
2516 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002517
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002518 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002519 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002520}
2521
2522static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002523resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002524{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002525 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002526
Pekka Paalanen77346a62011-11-30 16:26:35 +02002527 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002528
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002529 wl_list_remove(&shell->lock_layer.link);
2530 wl_list_insert(&shell->compositor->cursor_layer.link,
2531 &shell->fullscreen_layer.link);
2532 wl_list_insert(&shell->fullscreen_layer.link,
2533 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002534 if (shell->showing_input_panels) {
2535 wl_list_insert(&shell->panel_layer.link,
2536 &shell->input_panel_layer.link);
2537 wl_list_insert(&shell->input_panel_layer.link,
2538 &ws->layer.link);
2539 } else {
2540 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2541 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002542
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002543 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002544
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002545 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002546 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002547 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002548}
2549
2550static void
2551desktop_shell_unlock(struct wl_client *client,
2552 struct wl_resource *resource)
2553{
Tiago Vignattibe143262012-04-16 17:31:41 +03002554 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002555
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002556 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002557
2558 if (shell->locked)
2559 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002560}
2561
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002562static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002563desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002564 struct wl_resource *resource,
2565 struct wl_resource *surface_resource)
2566{
2567 struct desktop_shell *shell = resource->data;
2568
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002569 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002570}
2571
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002572static void
2573desktop_shell_desktop_ready(struct wl_client *client,
2574 struct wl_resource *resource)
2575{
2576 struct desktop_shell *shell = resource->data;
2577
2578 shell_fade_startup(shell);
2579}
2580
Kristian Høgsberg75840622011-09-06 13:48:16 -04002581static const struct desktop_shell_interface desktop_shell_implementation = {
2582 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002583 desktop_shell_set_panel,
2584 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002585 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002586 desktop_shell_set_grab_surface,
2587 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002588};
2589
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002590static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002591get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002592{
2593 struct shell_surface *shsurf;
2594
2595 shsurf = get_shell_surface(surface);
2596 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002597 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002598 return shsurf->type;
2599}
2600
Kristian Høgsberg75840622011-09-06 13:48:16 -04002601static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002602move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002603{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002604 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002605 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002606 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002607 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002608
Pekka Paalanen01388e22013-04-25 13:57:44 +03002609 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002610 if (surface == NULL)
2611 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002612
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002613 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002614 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2615 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002616 return;
2617
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002618 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002619}
2620
2621static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002622resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002623{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002624 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002625 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002626 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002627 uint32_t edges = 0;
2628 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002629 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002630
Pekka Paalanen01388e22013-04-25 13:57:44 +03002631 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002632 if (surface == NULL)
2633 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002634
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002635 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002636 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2637 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002638 return;
2639
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002640 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002641 wl_fixed_to_int(seat->pointer->grab_x),
2642 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002643 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002644
Pekka Paalanen60921e52012-01-25 15:55:43 +02002645 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002646 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002647 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002648 edges |= 0;
2649 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002650 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002651
Pekka Paalanen60921e52012-01-25 15:55:43 +02002652 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002653 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002654 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002655 edges |= 0;
2656 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002657 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002658
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002659 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002660}
2661
2662static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002663surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002664 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002665{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002666 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002667 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002668 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002669 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002670 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002671
Pekka Paalanen01388e22013-04-25 13:57:44 +03002672 /* XXX: broken for windows containing sub-surfaces */
2673 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002674 if (surface == NULL)
2675 return;
2676
2677 shsurf = get_shell_surface(surface);
2678 if (!shsurf)
2679 return;
2680
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002681 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002682
Scott Moreau02709af2012-05-22 01:54:10 -06002683 if (surface->alpha > 1.0)
2684 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002685 if (surface->alpha < step)
2686 surface->alpha = step;
2687
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002688 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002689 weston_surface_damage(surface);
2690}
2691
2692static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002693do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002694 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002695{
Daniel Stone37816df2012-05-16 18:45:18 +01002696 struct weston_seat *ws = (struct weston_seat *) seat;
2697 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002698 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002699 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002700
2701 wl_list_for_each(output, &compositor->output_list, link) {
2702 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002703 wl_fixed_to_double(seat->pointer->x),
2704 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002705 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002706 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002707 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002708 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002709 increment = -output->zoom.increment;
2710 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002711 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002712 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002713 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002714 else
2715 increment = 0;
2716
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002717 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002718
Scott Moreaue6603982012-06-11 13:07:51 -06002719 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002720 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002721 else if (output->zoom.level > output->zoom.max_level)
2722 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002723 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002724 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002725 output->disable_planes++;
2726 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002727
Scott Moreaue6603982012-06-11 13:07:51 -06002728 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002729
Scott Moreau8dacaab2012-06-17 18:10:58 -06002730 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002731 }
2732 }
2733}
2734
Scott Moreauccbf29d2012-02-22 14:21:41 -07002735static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002736zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002737 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002738{
2739 do_zoom(seat, time, 0, axis, value);
2740}
2741
2742static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002743zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002744 void *data)
2745{
2746 do_zoom(seat, time, key, 0, 0);
2747}
2748
2749static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002750terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002751 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002752{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002753 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002754
Daniel Stone325fc2d2012-05-30 16:31:58 +01002755 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002756}
2757
2758static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002759rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002760{
2761 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002762 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002763 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002764 struct shell_surface *shsurf = rotate->base.shsurf;
2765 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002766 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002767
2768 if (!shsurf)
2769 return;
2770
2771 surface = shsurf->surface;
2772
2773 cx = 0.5f * surface->geometry.width;
2774 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002775
Daniel Stone37816df2012-05-16 18:45:18 +01002776 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2777 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002778 r = sqrtf(dx * dx + dy * dy);
2779
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002780 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002781 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002782
2783 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002784 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002785 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002786
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002787 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002788 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002789
2790 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002791 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002792 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002793 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002794 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002795
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002796 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002797 &shsurf->surface->geometry.transformation_list,
2798 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002799 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002800 wl_list_init(&shsurf->rotation.transform.link);
2801 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002802 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002803 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002804
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002805 /* We need to adjust the position of the surface
2806 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002807 cposx = surface->geometry.x + cx;
2808 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002809 dposx = rotate->center.x - cposx;
2810 dposy = rotate->center.y - cposy;
2811 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002812 weston_surface_set_position(surface,
2813 surface->geometry.x + dposx,
2814 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002815 }
2816
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002817 /* Repaint implies weston_surface_update_transform(), which
2818 * lazily applies the damage due to rotation update.
2819 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002820 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002821}
2822
2823static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002824rotate_grab_button(struct weston_pointer_grab *grab,
2825 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002826{
2827 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002828 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002829 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002830 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002831 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002832
Daniel Stone4dbadb12012-05-30 16:31:51 +01002833 if (pointer->button_count == 0 &&
2834 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002835 if (shsurf)
2836 weston_matrix_multiply(&shsurf->rotation.rotation,
2837 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002838 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002839 free(rotate);
2840 }
2841}
2842
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002843static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002844 noop_grab_focus,
2845 rotate_grab_motion,
2846 rotate_grab_button,
2847};
2848
2849static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002850surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002851{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002852 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002853 float dx, dy;
2854 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002855
Pekka Paalanen460099f2012-01-20 16:48:25 +02002856 rotate = malloc(sizeof *rotate);
2857 if (!rotate)
2858 return;
2859
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002860 weston_surface_to_global_float(surface->surface,
2861 surface->surface->geometry.width / 2,
2862 surface->surface->geometry.height / 2,
2863 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002864
Daniel Stone37816df2012-05-16 18:45:18 +01002865 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2866 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002867 r = sqrtf(dx * dx + dy * dy);
2868 if (r > 20.0f) {
2869 struct weston_matrix inverse;
2870
2871 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002872 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002873 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002874
2875 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002876 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002877 } else {
2878 weston_matrix_init(&surface->rotation.rotation);
2879 weston_matrix_init(&rotate->rotation);
2880 }
2881
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002882 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2883 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002884}
2885
2886static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002887rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002888 void *data)
2889{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002890 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002891 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002892 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002893 struct shell_surface *surface;
2894
Pekka Paalanen01388e22013-04-25 13:57:44 +03002895 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002896 if (base_surface == NULL)
2897 return;
2898
2899 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002900 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2901 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002902 return;
2903
2904 surface_rotate(surface, seat);
2905}
2906
2907static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002908lower_fullscreen_layer(struct desktop_shell *shell)
2909{
2910 struct workspace *ws;
2911 struct weston_surface *surface, *prev;
2912
2913 ws = get_current_workspace(shell);
2914 wl_list_for_each_reverse_safe(surface, prev,
2915 &shell->fullscreen_layer.surface_list,
2916 layer_link)
2917 weston_surface_restack(surface, &ws->layer.surface_list);
2918}
2919
2920static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002921activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002922 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002923{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002924 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002925 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002926 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002927
Pekka Paalanen01388e22013-04-25 13:57:44 +03002928 main_surface = weston_surface_get_main_surface(es);
2929
Daniel Stone37816df2012-05-16 18:45:18 +01002930 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002931
Jonas Ådahl8538b222012-08-29 22:13:03 +02002932 state = ensure_focus_state(shell, seat);
2933 if (state == NULL)
2934 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002935
2936 state->keyboard_focus = es;
2937 wl_list_remove(&state->surface_destroy_listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002938 wl_signal_add(&es->resource.destroy_signal,
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002939 &state->surface_destroy_listener);
2940
Pekka Paalanen01388e22013-04-25 13:57:44 +03002941 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002942 case SHELL_SURFACE_FULLSCREEN:
2943 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002944 shell_stack_fullscreen(get_shell_surface(main_surface));
2945 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002946 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002947 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002948 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002949 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002950 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002951 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002952 }
2953}
2954
Alex Wu21858432012-04-01 20:13:08 +08002955/* no-op func for checking black surface */
2956static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002957black_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 +08002958{
2959}
2960
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002961static bool
Alex Wu21858432012-04-01 20:13:08 +08002962is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2963{
2964 if (es->configure == black_surface_configure) {
2965 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002966 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002967 return true;
2968 }
2969 return false;
2970}
2971
Kristian Høgsberg75840622011-09-06 13:48:16 -04002972static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002973click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002974 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002975{
Daniel Stone37816df2012-05-16 18:45:18 +01002976 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002977 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002978 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002979 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002980
Daniel Stone37816df2012-05-16 18:45:18 +01002981 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002982 if (!focus)
2983 return;
2984
Pekka Paalanen01388e22013-04-25 13:57:44 +03002985 if (is_black_surface(focus, &main_surface))
2986 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002987
Pekka Paalanen01388e22013-04-25 13:57:44 +03002988 main_surface = weston_surface_get_main_surface(focus);
2989 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002990 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002991
Daniel Stone325fc2d2012-05-30 16:31:58 +01002992 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002993 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002994}
2995
2996static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002997lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002998{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002999 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003000
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003001 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003002 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003003 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003004 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003005
3006 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003007
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003008 /* Hide all surfaces by removing the fullscreen, panel and
3009 * toplevel layers. This way nothing else can show or receive
3010 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003011
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003012 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003013 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003014 if (shell->showing_input_panels)
3015 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003016 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003017 wl_list_insert(&shell->compositor->cursor_layer.link,
3018 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003019
Pekka Paalanen77346a62011-11-30 16:26:35 +02003020 launch_screensaver(shell);
3021
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003022 /* TODO: disable bindings that should not work while locked. */
3023
3024 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003025}
3026
3027static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003028unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003029{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003030 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003031 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003032 return;
3033 }
3034
3035 /* If desktop-shell client has gone away, unlock immediately. */
3036 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003037 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003038 return;
3039 }
3040
3041 if (shell->prepare_event_sent)
3042 return;
3043
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003044 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003045 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003046}
3047
3048static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003049shell_fade_done(struct weston_surface_animation *animation, void *data)
3050{
3051 struct desktop_shell *shell = data;
3052
3053 shell->fade.animation = NULL;
3054
3055 switch (shell->fade.type) {
3056 case FADE_IN:
3057 weston_surface_destroy(shell->fade.surface);
3058 shell->fade.surface = NULL;
3059 break;
3060 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003061 lock(shell);
3062 break;
3063 }
3064}
3065
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003066static struct weston_surface *
3067shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003068{
3069 struct weston_compositor *compositor = shell->compositor;
3070 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003071
3072 surface = weston_surface_create(compositor);
3073 if (!surface)
3074 return NULL;
3075
3076 weston_surface_configure(surface, 0, 0, 8192, 8192);
3077 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3078 wl_list_insert(&compositor->fade_layer.surface_list,
3079 &surface->layer_link);
3080 pixman_region32_init(&surface->input);
3081
3082 return surface;
3083}
3084
3085static void
3086shell_fade(struct desktop_shell *shell, enum fade_type type)
3087{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003088 float tint;
3089
3090 switch (type) {
3091 case FADE_IN:
3092 tint = 0.0;
3093 break;
3094 case FADE_OUT:
3095 tint = 1.0;
3096 break;
3097 default:
3098 weston_log("shell: invalid fade type\n");
3099 return;
3100 }
3101
3102 shell->fade.type = type;
3103
3104 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003105 shell->fade.surface = shell_fade_create_surface(shell);
3106 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003107 return;
3108
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003109 shell->fade.surface->alpha = 1.0 - tint;
3110 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003111 }
3112
3113 if (shell->fade.animation)
3114 weston_fade_update(shell->fade.animation,
3115 shell->fade.surface->alpha, tint, 30.0);
3116 else
3117 shell->fade.animation =
3118 weston_fade_run(shell->fade.surface,
3119 1.0 - tint, tint, 30.0,
3120 shell_fade_done, shell);
3121}
3122
3123static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003124do_shell_fade_startup(void *data)
3125{
3126 struct desktop_shell *shell = data;
3127
3128 shell_fade(shell, FADE_IN);
3129}
3130
3131static void
3132shell_fade_startup(struct desktop_shell *shell)
3133{
3134 struct wl_event_loop *loop;
3135
3136 if (!shell->fade.startup_timer)
3137 return;
3138
3139 wl_event_source_remove(shell->fade.startup_timer);
3140 shell->fade.startup_timer = NULL;
3141
3142 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3143 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3144}
3145
3146static int
3147fade_startup_timeout(void *data)
3148{
3149 struct desktop_shell *shell = data;
3150
3151 shell_fade_startup(shell);
3152 return 0;
3153}
3154
3155static void
3156shell_fade_init(struct desktop_shell *shell)
3157{
3158 /* Make compositor output all black, and wait for the desktop-shell
3159 * client to signal it is ready, then fade in. The timer triggers a
3160 * fade-in, in case the desktop-shell client takes too long.
3161 */
3162
3163 struct wl_event_loop *loop;
3164
3165 if (shell->fade.surface != NULL) {
3166 weston_log("%s: warning: fade surface already exists\n",
3167 __func__);
3168 return;
3169 }
3170
3171 shell->fade.surface = shell_fade_create_surface(shell);
3172 if (!shell->fade.surface)
3173 return;
3174
3175 weston_surface_update_transform(shell->fade.surface);
3176 weston_surface_damage(shell->fade.surface);
3177
3178 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3179 shell->fade.startup_timer =
3180 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3181 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3182}
3183
3184static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003185idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003186{
3187 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003188 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003189
3190 shell_fade(shell, FADE_OUT);
3191 /* lock() is called from shell_fade_done() */
3192}
3193
3194static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003195wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003196{
3197 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003198 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003199
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003200 unlock(shell);
3201}
3202
3203static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003204show_input_panels(struct wl_listener *listener, void *data)
3205{
3206 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003207 container_of(listener, struct desktop_shell,
3208 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003209 struct input_panel_surface *surface, *next;
3210 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003211
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003212 shell->text_input.surface = (struct weston_surface*)data;
3213
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003214 if (shell->showing_input_panels)
3215 return;
3216
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003217 shell->showing_input_panels = true;
3218
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003219 if (!shell->locked)
3220 wl_list_insert(&shell->panel_layer.link,
3221 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003222
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003223 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003224 &shell->input_panel.surfaces, link) {
3225 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003226 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003227 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003228 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003229 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003230 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003231 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003232 weston_surface_damage(ws);
3233 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003234 }
3235}
3236
3237static void
3238hide_input_panels(struct wl_listener *listener, void *data)
3239{
3240 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003241 container_of(listener, struct desktop_shell,
3242 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003243 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003244
Jan Arne Petersen61381972013-01-31 15:52:21 +01003245 if (!shell->showing_input_panels)
3246 return;
3247
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003248 shell->showing_input_panels = false;
3249
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003250 if (!shell->locked)
3251 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003252
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003253 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003254 &shell->input_panel_layer.surface_list, layer_link)
3255 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003256}
3257
3258static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003259update_input_panels(struct wl_listener *listener, void *data)
3260{
3261 struct desktop_shell *shell =
3262 container_of(listener, struct desktop_shell,
3263 update_input_panel_listener);
3264
3265 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3266}
3267
3268static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003269center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003270{
Giulio Camuffob8366642013-04-25 13:57:46 +03003271 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003272 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003273
Giulio Camuffob8366642013-04-25 13:57:46 +03003274 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3275
3276 x = output->x + (output->width - width) / 2 - surf_x / 2;
3277 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003278
3279 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003280}
3281
3282static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003283weston_surface_set_initial_position (struct weston_surface *surface,
3284 struct desktop_shell *shell)
3285{
3286 struct weston_compositor *compositor = shell->compositor;
3287 int ix = 0, iy = 0;
3288 int range_x, range_y;
3289 int dx, dy, x, y, panel_height;
3290 struct weston_output *output, *target_output = NULL;
3291 struct weston_seat *seat;
3292
3293 /* As a heuristic place the new window on the same output as the
3294 * pointer. Falling back to the output containing 0, 0.
3295 *
3296 * TODO: Do something clever for touch too?
3297 */
3298 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003299 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003300 ix = wl_fixed_to_int(seat->pointer->x);
3301 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003302 break;
3303 }
3304 }
3305
3306 wl_list_for_each(output, &compositor->output_list, link) {
3307 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3308 target_output = output;
3309 break;
3310 }
3311 }
3312
3313 if (!target_output) {
3314 weston_surface_set_position(surface, 10 + random() % 400,
3315 10 + random() % 400);
3316 return;
3317 }
3318
3319 /* Valid range within output where the surface will still be onscreen.
3320 * If this is negative it means that the surface is bigger than
3321 * output.
3322 */
3323 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003324 range_x = target_output->width - surface->geometry.width;
3325 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003326 surface->geometry.height;
3327
Rob Bradford4cb88c72012-08-13 15:18:44 +01003328 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003329 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003330 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003331 dx = 0;
3332
3333 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003334 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003335 else
3336 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003337
3338 x = target_output->x + dx;
3339 y = target_output->y + dy;
3340
3341 weston_surface_set_position (surface, x, y);
3342}
3343
3344static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003345map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003346 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003347{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003348 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003349 struct shell_surface *shsurf = get_shell_surface(surface);
3350 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003351 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003352 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003353 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003354 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003355 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003356
Pekka Paalanen60921e52012-01-25 15:55:43 +02003357 surface->geometry.width = width;
3358 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003359 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003360
3361 /* initial positioning, see also configure() */
3362 switch (surface_type) {
3363 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003364 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003365 break;
Alex Wu4539b082012-03-01 12:57:46 +08003366 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003367 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003368 shell_map_fullscreen(shsurf);
3369 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003370 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003371 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003372 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003373 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3374 NULL, NULL);
3375 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3376 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003377 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003378 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003379 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003380 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003381 case SHELL_SURFACE_NONE:
3382 weston_surface_set_position(surface,
3383 surface->geometry.x + sx,
3384 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003385 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003386 default:
3387 ;
3388 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003389
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003390 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003391 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003392 case SHELL_SURFACE_POPUP:
3393 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003394 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003395 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3396 break;
Alex Wu4539b082012-03-01 12:57:46 +08003397 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003398 case SHELL_SURFACE_NONE:
3399 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003400 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003401 ws = get_current_workspace(shell);
3402 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003403 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003404 }
3405
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003406 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003407 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003408 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3409 surface->output = shsurf->output;
3410 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003411
Juan Zhao7bb92f02011-12-15 11:31:51 -05003412 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003413 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003414 if (shsurf->transient.flags ==
3415 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3416 break;
3417 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003418 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003419 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003420 if (!shell->locked) {
3421 wl_list_for_each(seat, &compositor->seat_list, link)
3422 activate(shell, surface, seat);
3423 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003424 break;
3425 default:
3426 break;
3427 }
3428
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003429 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003430 {
3431 switch (shell->win_animation_type) {
3432 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003433 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003434 break;
3435 case ANIMATION_ZOOM:
3436 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3437 break;
3438 default:
3439 break;
3440 }
3441 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003442}
3443
3444static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003445configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003446 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003447{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003448 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3449 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003450 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003451
Pekka Paalanen77346a62011-11-30 16:26:35 +02003452 shsurf = get_shell_surface(surface);
3453 if (shsurf)
3454 surface_type = shsurf->type;
3455
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003456 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003457
3458 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003459 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003460 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003461 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003462 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003463 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003464 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003465 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3466 NULL, NULL);
3467 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003468 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003469 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003470 break;
Alex Wu4539b082012-03-01 12:57:46 +08003471 case SHELL_SURFACE_TOPLEVEL:
3472 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003473 default:
3474 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003475 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003476
Alex Wu4539b082012-03-01 12:57:46 +08003477 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003478 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003479 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003480
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003481 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003482 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003483 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003484}
3485
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003486static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003487shell_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 +03003488{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003489 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003490 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003491
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003492 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003493
Giulio Camuffo5085a752013-03-25 21:42:45 +01003494 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3495 remove_popup_grab(shsurf);
3496 }
3497
Giulio Camuffo184df502013-02-21 11:29:21 +01003498 if (width == 0)
3499 return;
3500
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003501 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003502 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003503 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003504 type_changed = 1;
3505 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003506
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003507 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003508 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003509 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003510 es->geometry.width != width ||
3511 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003512 float from_x, from_y;
3513 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003514
3515 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3516 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3517 configure(shell, es,
3518 es->geometry.x + to_x - from_x,
3519 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003520 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003521 }
3522}
3523
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003524static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003525
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003526static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003527desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003528{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003529 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003530 struct desktop_shell *shell =
3531 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003532
3533 shell->child.process.pid = 0;
3534 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003535
3536 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3537 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003538 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003539 shell->child.deathstamp = time;
3540 shell->child.deathcount = 0;
3541 }
3542
3543 shell->child.deathcount++;
3544 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003545 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003546 return;
3547 }
3548
Martin Minarik6d118362012-06-07 18:01:59 +02003549 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003550 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003551 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003552}
3553
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003554static void
3555launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003556{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003557 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003558 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003559
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003560 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003561 &shell->child.process,
3562 shell_exe,
3563 desktop_shell_sigchld);
3564
3565 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003566 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003567}
3568
3569static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003570bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3571{
Tiago Vignattibe143262012-04-16 17:31:41 +03003572 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003573
3574 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003575 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003576}
3577
Kristian Høgsberg75840622011-09-06 13:48:16 -04003578static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003579unbind_desktop_shell(struct wl_resource *resource)
3580{
Tiago Vignattibe143262012-04-16 17:31:41 +03003581 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003582
3583 if (shell->locked)
3584 resume_desktop(shell);
3585
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003586 shell->child.desktop_shell = NULL;
3587 shell->prepare_event_sent = false;
3588 free(resource);
3589}
3590
3591static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003592bind_desktop_shell(struct wl_client *client,
3593 void *data, uint32_t version, uint32_t id)
3594{
Tiago Vignattibe143262012-04-16 17:31:41 +03003595 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003596 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003597
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003598 resource = wl_client_add_object(client, &desktop_shell_interface,
3599 &desktop_shell_implementation,
3600 id, shell);
3601
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003602 if (client == shell->child.client) {
3603 resource->destroy = unbind_desktop_shell;
3604 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003605
3606 if (version < 2)
3607 shell_fade_startup(shell);
3608
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003609 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003610 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003611
3612 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3613 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003614 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003615}
3616
Pekka Paalanen6e168112011-11-24 11:34:05 +02003617static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003618screensaver_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 -04003619{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003620 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003621
Giulio Camuffo184df502013-02-21 11:29:21 +01003622 if (width == 0)
3623 return;
3624
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003625 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003626 if (!shell->locked)
3627 return;
3628
3629 center_on_output(surface, surface->output);
3630
3631 if (wl_list_empty(&surface->layer_link)) {
3632 wl_list_insert(shell->lock_layer.surface_list.prev,
3633 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003634 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003635 wl_event_source_timer_update(shell->screensaver.timer,
3636 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003637 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003638 }
3639}
3640
3641static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003642screensaver_set_surface(struct wl_client *client,
3643 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003644 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003645 struct wl_resource *output_resource)
3646{
Tiago Vignattibe143262012-04-16 17:31:41 +03003647 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003648 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003649 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003650
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003651 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003652 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003653 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003654}
3655
3656static const struct screensaver_interface screensaver_implementation = {
3657 screensaver_set_surface
3658};
3659
3660static void
3661unbind_screensaver(struct wl_resource *resource)
3662{
Tiago Vignattibe143262012-04-16 17:31:41 +03003663 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003664
Pekka Paalanen77346a62011-11-30 16:26:35 +02003665 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003666 free(resource);
3667}
3668
3669static void
3670bind_screensaver(struct wl_client *client,
3671 void *data, uint32_t version, uint32_t id)
3672{
Tiago Vignattibe143262012-04-16 17:31:41 +03003673 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003674 struct wl_resource *resource;
3675
3676 resource = wl_client_add_object(client, &screensaver_interface,
3677 &screensaver_implementation,
3678 id, shell);
3679
Pekka Paalanen77346a62011-11-30 16:26:35 +02003680 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003681 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003682 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003683 return;
3684 }
3685
3686 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3687 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003688 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003689}
3690
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003691static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003692input_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 -04003693{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003694 struct input_panel_surface *ip_surface = surface->configure_private;
3695 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003696 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003697 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003698 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003699
Giulio Camuffo184df502013-02-21 11:29:21 +01003700 if (width == 0)
3701 return;
3702
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003703 if (!weston_surface_is_mapped(surface)) {
3704 if (!shell->showing_input_panels)
3705 return;
3706
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003707 show_surface = 1;
3708 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003709
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003710 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003711
3712 if (ip_surface->panel) {
3713 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3714 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3715 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003716 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003717
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003718 x = ip_surface->output->x + (mode->width - width) / 2;
3719 y = ip_surface->output->y + mode->height - height;
3720 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003721
3722 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003723 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003724 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003725
3726 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003727 wl_list_insert(&shell->input_panel_layer.surface_list,
3728 &surface->layer_link);
3729 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003730 weston_surface_damage(surface);
3731 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3732 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003733}
3734
3735static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003736destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003737{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003738 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003739 wl_list_remove(&input_panel_surface->link);
3740
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003741 input_panel_surface->surface->configure = NULL;
3742
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003743 free(input_panel_surface);
3744}
3745
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003746static struct input_panel_surface *
3747get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003748{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003749 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003750 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003751 } else {
3752 return NULL;
3753 }
3754}
3755
3756static void
3757input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3758{
3759 struct input_panel_surface *ipsurface = container_of(listener,
3760 struct input_panel_surface,
3761 surface_destroy_listener);
3762
3763 if (ipsurface->resource.client) {
3764 wl_resource_destroy(&ipsurface->resource);
3765 } else {
3766 wl_signal_emit(&ipsurface->resource.destroy_signal,
3767 &ipsurface->resource);
3768 destroy_input_panel_surface(ipsurface);
3769 }
3770}
3771static struct input_panel_surface *
3772create_input_panel_surface(struct desktop_shell *shell,
3773 struct weston_surface *surface)
3774{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003775 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003776
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003777 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3778 if (!input_panel_surface)
3779 return NULL;
3780
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003781 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003782 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003783
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003784 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003785
3786 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003787
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003788 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3789 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003790 wl_signal_add(&surface->resource.destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003791 &input_panel_surface->surface_destroy_listener);
3792
3793 wl_list_init(&input_panel_surface->link);
3794
3795 return input_panel_surface;
3796}
3797
3798static void
3799input_panel_surface_set_toplevel(struct wl_client *client,
3800 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003801 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003802 uint32_t position)
3803{
3804 struct input_panel_surface *input_panel_surface = resource->data;
3805 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003806
3807 wl_list_insert(&shell->input_panel.surfaces,
3808 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003809
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003810 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003811 input_panel_surface->panel = 0;
3812}
3813
3814static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003815input_panel_surface_set_overlay_panel(struct wl_client *client,
3816 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003817{
3818 struct input_panel_surface *input_panel_surface = resource->data;
3819 struct desktop_shell *shell = input_panel_surface->shell;
3820
3821 wl_list_insert(&shell->input_panel.surfaces,
3822 &input_panel_surface->link);
3823
3824 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003825}
3826
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003827static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003828 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003829 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003830};
3831
3832static void
3833destroy_input_panel_surface_resource(struct wl_resource *resource)
3834{
3835 struct input_panel_surface *ipsurf = resource->data;
3836
3837 destroy_input_panel_surface(ipsurf);
3838}
3839
3840static void
3841input_panel_get_input_panel_surface(struct wl_client *client,
3842 struct wl_resource *resource,
3843 uint32_t id,
3844 struct wl_resource *surface_resource)
3845{
3846 struct weston_surface *surface = surface_resource->data;
3847 struct desktop_shell *shell = resource->data;
3848 struct input_panel_surface *ipsurf;
3849
3850 if (get_input_panel_surface(surface)) {
3851 wl_resource_post_error(surface_resource,
3852 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003853 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003854 return;
3855 }
3856
3857 ipsurf = create_input_panel_surface(shell, surface);
3858 if (!ipsurf) {
3859 wl_resource_post_error(surface_resource,
3860 WL_DISPLAY_ERROR_INVALID_OBJECT,
3861 "surface->configure already set");
3862 return;
3863 }
3864
3865 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3866 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003867 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003868 ipsurf->resource.object.implementation =
3869 (void (**)(void)) &input_panel_surface_implementation;
3870 ipsurf->resource.data = ipsurf;
3871
3872 wl_client_add_resource(client, &ipsurf->resource);
3873}
3874
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003875static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003876 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003877};
3878
3879static void
3880unbind_input_panel(struct wl_resource *resource)
3881{
3882 struct desktop_shell *shell = resource->data;
3883
3884 shell->input_panel.binding = NULL;
3885 free(resource);
3886}
3887
3888static void
3889bind_input_panel(struct wl_client *client,
3890 void *data, uint32_t version, uint32_t id)
3891{
3892 struct desktop_shell *shell = data;
3893 struct wl_resource *resource;
3894
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003895 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003896 &input_panel_implementation,
3897 id, shell);
3898
3899 if (shell->input_panel.binding == NULL) {
3900 resource->destroy = unbind_input_panel;
3901 shell->input_panel.binding = resource;
3902 return;
3903 }
3904
3905 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3906 "interface object already bound");
3907 wl_resource_destroy(resource);
3908}
3909
Kristian Høgsberg07045392012-02-19 18:52:44 -05003910struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003911 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003912 struct weston_surface *current;
3913 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003914 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003915};
3916
3917static void
3918switcher_next(struct switcher *switcher)
3919{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003920 struct weston_surface *surface;
3921 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003922 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003923 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003924
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003925 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003926 switch (get_shell_surface_type(surface)) {
3927 case SHELL_SURFACE_TOPLEVEL:
3928 case SHELL_SURFACE_FULLSCREEN:
3929 case SHELL_SURFACE_MAXIMIZED:
3930 if (first == NULL)
3931 first = surface;
3932 if (prev == switcher->current)
3933 next = surface;
3934 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003935 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003936 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003937 weston_surface_damage(surface);
3938 break;
3939 default:
3940 break;
3941 }
Alex Wu1659daa2012-04-01 20:13:09 +08003942
3943 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003944 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003945 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003946 weston_surface_damage(surface);
3947 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003948 }
3949
3950 if (next == NULL)
3951 next = first;
3952
Alex Wu07b26062012-03-12 16:06:01 +08003953 if (next == NULL)
3954 return;
3955
Kristian Høgsberg07045392012-02-19 18:52:44 -05003956 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003957 wl_signal_add(&next->resource.destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003958
3959 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003960 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003961
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003962 shsurf = get_shell_surface(switcher->current);
3963 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003964 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003965}
3966
3967static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003968switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003969{
3970 struct switcher *switcher =
3971 container_of(listener, struct switcher, listener);
3972
3973 switcher_next(switcher);
3974}
3975
3976static void
Daniel Stone351eb612012-05-31 15:27:47 -04003977switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003978{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003979 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003980 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003981 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003982
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003983 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003984 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003985 weston_surface_damage(surface);
3986 }
3987
Alex Wu07b26062012-03-12 16:06:01 +08003988 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003989 activate(switcher->shell, switcher->current,
3990 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003991 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003992 weston_keyboard_end_grab(keyboard);
3993 if (keyboard->input_method_resource)
3994 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003995 free(switcher);
3996}
3997
3998static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003999switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004000 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004001{
4002 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004003 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004004
Daniel Stonec9785ea2012-05-30 16:31:52 +01004005 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004006 switcher_next(switcher);
4007}
4008
4009static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004010switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004011 uint32_t mods_depressed, uint32_t mods_latched,
4012 uint32_t mods_locked, uint32_t group)
4013{
4014 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004015 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004016
Daniel Stone351eb612012-05-31 15:27:47 -04004017 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4018 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004019}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004020
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004021static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004022 switcher_key,
4023 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004024};
4025
4026static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004027switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004028 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004029{
Tiago Vignattibe143262012-04-16 17:31:41 +03004030 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004031 struct switcher *switcher;
4032
4033 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004034 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004035 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004036 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004037 wl_list_init(&switcher->listener.link);
4038
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004039 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004040 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004041 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004042 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4043 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004044 switcher_next(switcher);
4045}
4046
Pekka Paalanen3c647232011-12-22 13:43:43 +02004047static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004048backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004049 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004050{
4051 struct weston_compositor *compositor = data;
4052 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004053 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004054
4055 /* TODO: we're limiting to simple use cases, where we assume just
4056 * control on the primary display. We'd have to extend later if we
4057 * ever get support for setting backlights on random desktop LCD
4058 * panels though */
4059 output = get_default_output(compositor);
4060 if (!output)
4061 return;
4062
4063 if (!output->set_backlight)
4064 return;
4065
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004066 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4067 backlight_new = output->backlight_current - 25;
4068 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4069 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004070
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004071 if (backlight_new < 5)
4072 backlight_new = 5;
4073 if (backlight_new > 255)
4074 backlight_new = 255;
4075
4076 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004077 output->set_backlight(output, output->backlight_current);
4078}
4079
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004080struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004081 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004082 struct weston_seat *seat;
4083 uint32_t key[2];
4084 int key_released[2];
4085};
4086
4087static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004088debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004089 uint32_t key, uint32_t state)
4090{
4091 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4092 struct wl_resource *resource;
4093 struct wl_display *display;
4094 uint32_t serial;
4095 int send = 0, terminate = 0;
4096 int check_binding = 1;
4097 int i;
4098
4099 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4100 /* Do not run bindings on key releases */
4101 check_binding = 0;
4102
4103 for (i = 0; i < 2; i++)
4104 if (key == db->key[i])
4105 db->key_released[i] = 1;
4106
4107 if (db->key_released[0] && db->key_released[1]) {
4108 /* All key releases been swalled so end the grab */
4109 terminate = 1;
4110 } else if (key != db->key[0] && key != db->key[1]) {
4111 /* Should not swallow release of other keys */
4112 send = 1;
4113 }
4114 } else if (key == db->key[0] && !db->key_released[0]) {
4115 /* Do not check bindings for the first press of the binding
4116 * key. This allows it to be used as a debug shortcut.
4117 * We still need to swallow this event. */
4118 check_binding = 0;
4119 } else if (db->key[1]) {
4120 /* If we already ran a binding don't process another one since
4121 * we can't keep track of all the binding keys that were
4122 * pressed in order to swallow the release events. */
4123 send = 1;
4124 check_binding = 0;
4125 }
4126
4127 if (check_binding) {
4128 struct weston_compositor *ec = db->seat->compositor;
4129
4130 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4131 key, state)) {
4132 /* We ran a binding so swallow the press and keep the
4133 * grab to swallow the released too. */
4134 send = 0;
4135 terminate = 0;
4136 db->key[1] = key;
4137 } else {
4138 /* Terminate the grab since the key pressed is not a
4139 * debug binding key. */
4140 send = 1;
4141 terminate = 1;
4142 }
4143 }
4144
4145 if (send) {
4146 resource = grab->keyboard->focus_resource;
4147
4148 if (resource) {
4149 display = wl_client_get_display(resource->client);
4150 serial = wl_display_next_serial(display);
4151 wl_keyboard_send_key(resource, serial, time, key, state);
4152 }
4153 }
4154
4155 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004156 weston_keyboard_end_grab(grab->keyboard);
4157 if (grab->keyboard->input_method_resource)
4158 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004159 free(db);
4160 }
4161}
4162
4163static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004164debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004165 uint32_t mods_depressed, uint32_t mods_latched,
4166 uint32_t mods_locked, uint32_t group)
4167{
4168 struct wl_resource *resource;
4169
4170 resource = grab->keyboard->focus_resource;
4171 if (!resource)
4172 return;
4173
4174 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4175 mods_latched, mods_locked, group);
4176}
4177
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004178struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004179 debug_binding_key,
4180 debug_binding_modifiers
4181};
4182
4183static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004184debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004185{
4186 struct debug_binding_grab *grab;
4187
4188 grab = calloc(1, sizeof *grab);
4189 if (!grab)
4190 return;
4191
4192 grab->seat = (struct weston_seat *) seat;
4193 grab->key[0] = key;
4194 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004195 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004196}
4197
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004198static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004199force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004200 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004201{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004202 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004203 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004204 struct desktop_shell *shell = data;
4205 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004206 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004207
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004208 focus_surface = seat->keyboard->focus;
4209 if (!focus_surface)
4210 return;
4211
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004212 wl_signal_emit(&compositor->kill_signal, focus_surface);
4213
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004214 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03004215 wl_client_get_credentials(client, &pid, NULL, NULL);
4216
4217 /* Skip clients that we launched ourselves (the credentials of
4218 * the socketpair is ours) */
4219 if (pid == getpid())
4220 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004221
Daniel Stone325fc2d2012-05-30 16:31:58 +01004222 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004223}
4224
4225static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004226workspace_up_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 != 0)
4235 new_index--;
4236
4237 change_workspace(shell, new_index);
4238}
4239
4240static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004241workspace_down_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 = shell->workspaces.current;
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 if (new_index < shell->workspaces.num - 1)
4250 new_index++;
4251
4252 change_workspace(shell, new_index);
4253}
4254
4255static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004256workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004257 uint32_t key, void *data)
4258{
4259 struct desktop_shell *shell = data;
4260 unsigned int new_index;
4261
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004262 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004263 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004264 new_index = key - KEY_F1;
4265 if (new_index >= shell->workspaces.num)
4266 new_index = shell->workspaces.num - 1;
4267
4268 change_workspace(shell, new_index);
4269}
4270
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004271static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004272workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004273 uint32_t key, void *data)
4274{
4275 struct desktop_shell *shell = data;
4276 unsigned int new_index = shell->workspaces.current;
4277
4278 if (shell->locked)
4279 return;
4280
4281 if (new_index != 0)
4282 new_index--;
4283
4284 take_surface_to_workspace_by_seat(shell, seat, new_index);
4285}
4286
4287static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004288workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004289 uint32_t key, void *data)
4290{
4291 struct desktop_shell *shell = data;
4292 unsigned int new_index = shell->workspaces.current;
4293
4294 if (shell->locked)
4295 return;
4296
4297 if (new_index < shell->workspaces.num - 1)
4298 new_index++;
4299
4300 take_surface_to_workspace_by_seat(shell, seat, new_index);
4301}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004302
4303static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004304shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004305{
Tiago Vignattibe143262012-04-16 17:31:41 +03004306 struct desktop_shell *shell =
4307 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004308 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004309
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004310 if (shell->child.client)
4311 wl_client_destroy(shell->child.client);
4312
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004313 wl_list_remove(&shell->idle_listener.link);
4314 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004315 wl_list_remove(&shell->show_input_panel_listener.link);
4316 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004317
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004318 wl_array_for_each(ws, &shell->workspaces.array)
4319 workspace_destroy(*ws);
4320 wl_array_release(&shell->workspaces.array);
4321
Pekka Paalanen3c647232011-12-22 13:43:43 +02004322 free(shell->screensaver.path);
4323 free(shell);
4324}
4325
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004326static void
4327shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4328{
4329 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004330 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004331
4332 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004333 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4334 MODIFIER_CTRL | MODIFIER_ALT,
4335 terminate_binding, ec);
4336 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4337 click_to_activate_binding,
4338 shell);
4339 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4340 MODIFIER_SUPER | MODIFIER_ALT,
4341 surface_opacity_binding, NULL);
4342 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4343 MODIFIER_SUPER, zoom_axis_binding,
4344 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004345
4346 /* configurable bindings */
4347 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004348 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4349 zoom_key_binding, NULL);
4350 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4351 zoom_key_binding, NULL);
4352 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4353 shell);
4354 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4355 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004356
4357 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4358 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4359 rotate_binding, NULL);
4360
Daniel Stone325fc2d2012-05-30 16:31:58 +01004361 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4362 shell);
4363 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4364 ec);
4365 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4366 backlight_binding, ec);
4367 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4368 ec);
4369 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4370 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004371 weston_compositor_add_key_binding(ec, KEY_K, mod,
4372 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004373 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4374 workspace_up_binding, shell);
4375 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4376 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004377 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4378 workspace_move_surface_up_binding,
4379 shell);
4380 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4381 workspace_move_surface_down_binding,
4382 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004383
4384 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4385 if (shell->workspaces.num > 1) {
4386 num_workspace_bindings = shell->workspaces.num;
4387 if (num_workspace_bindings > 6)
4388 num_workspace_bindings = 6;
4389 for (i = 0; i < num_workspace_bindings; i++)
4390 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4391 workspace_f_binding,
4392 shell);
4393 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004394
4395 /* Debug bindings */
4396 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4397 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004398}
4399
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004400WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004401module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004402 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004403{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004404 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004405 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004406 struct workspace **pws;
4407 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004408 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004409
4410 shell = malloc(sizeof *shell);
4411 if (shell == NULL)
4412 return -1;
4413
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004414 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004415 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004416
4417 shell->destroy_listener.notify = shell_destroy;
4418 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004419 shell->idle_listener.notify = idle_handler;
4420 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4421 shell->wake_listener.notify = wake_handler;
4422 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004423 shell->show_input_panel_listener.notify = show_input_panels;
4424 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4425 shell->hide_input_panel_listener.notify = hide_input_panels;
4426 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004427 shell->update_input_panel_listener.notify = update_input_panels;
4428 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004429 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004430 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004431 ec->shell_interface.create_shell_surface = create_shell_surface;
4432 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004433 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004434 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004435 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004436 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004437
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004438 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004439
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004440 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4441 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004442 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4443 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004444 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004445
4446 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004447 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004448
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004449 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004450
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004451 for (i = 0; i < shell->workspaces.num; i++) {
4452 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4453 if (pws == NULL)
4454 return -1;
4455
4456 *pws = workspace_create();
4457 if (*pws == NULL)
4458 return -1;
4459 }
4460 activate_workspace(shell, 0);
4461
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004462 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004463 wl_list_init(&shell->workspaces.animation.link);
4464 shell->workspaces.animation.frame = animate_workspace_change_frame;
4465
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004466 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4467 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004468 return -1;
4469
Kristian Høgsberg75840622011-09-06 13:48:16 -04004470 if (wl_display_add_global(ec->wl_display,
4471 &desktop_shell_interface,
4472 shell, bind_desktop_shell) == NULL)
4473 return -1;
4474
Pekka Paalanen6e168112011-11-24 11:34:05 +02004475 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4476 shell, bind_screensaver) == NULL)
4477 return -1;
4478
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004479 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004480 shell, bind_input_panel) == NULL)
4481 return -1;
4482
Jonas Ådahle9d22502012-08-29 22:13:01 +02004483 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4484 shell, bind_workspace_manager) == NULL)
4485 return -1;
4486
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004487 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004488
4489 loop = wl_display_get_event_loop(ec->wl_display);
4490 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004491
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004492 shell->screensaver.timer =
4493 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4494
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004495 wl_list_for_each(seat, &ec->seat_list, link)
4496 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004497
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004498 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004499
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004500 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004501
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004502 return 0;
4503}