blob: de5d6f6ea5872a732699485a48d5cfa9ff369672 [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;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020081};
82
Tiago Vignattibe143262012-04-16 17:31:41 +030083struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040085
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020086 struct wl_listener idle_listener;
87 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040088 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020089 struct wl_listener show_input_panel_listener;
90 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020091
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050092 struct weston_layer fullscreen_layer;
93 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050094 struct weston_layer background_layer;
95 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +020096 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050097
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040098 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030099 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400100
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200101 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500102 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200103 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200104 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200105
106 unsigned deathcount;
107 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200108 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200109
110 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200111 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200112 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200113
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400114 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500115 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100116
Pekka Paalanen77346a62011-11-30 16:26:35 +0200117 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200118 struct wl_array array;
119 unsigned int current;
120 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200121
Jonas Ådahle9d22502012-08-29 22:13:01 +0200122 struct wl_list client_list;
123
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200124 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200125 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200126 int anim_dir;
127 uint32_t anim_timestamp;
128 double anim_current;
129 struct workspace *anim_from;
130 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200131 } workspaces;
132
133 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200134 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200135 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200136 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500137 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200138 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200139 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500140
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200141 struct {
142 struct wl_resource *binding;
143 struct wl_list surfaces;
144 } input_panel;
145
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200146 struct {
147 struct weston_surface *surface;
148 struct weston_surface_animation *animation;
149 enum fade_type type;
150 } fade;
151
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300152 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800153 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400154};
155
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500156enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200157 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500158 SHELL_SURFACE_TOPLEVEL,
159 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500160 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800161 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500162 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200163};
164
Scott Moreauff1db4a2012-04-17 19:06:18 -0600165struct ping_timer {
166 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600167 uint32_t serial;
168};
169
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200170struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200171 struct wl_resource resource;
172
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500173 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200174 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400175 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300176 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200177
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400178 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400179 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500180 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800181 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800182 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600183 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100184
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500185 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200186 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500187 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200188 } rotation;
189
190 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100191 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500192 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100193 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400194 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500195 } popup;
196
Alex Wu4539b082012-03-01 12:57:46 +0800197 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300198 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400199 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300200 } transient;
201
202 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800203 enum wl_shell_surface_fullscreen_method type;
204 struct weston_transform transform; /* matrix from x, y */
205 uint32_t framerate;
206 struct weston_surface *black_surface;
207 } fullscreen;
208
Scott Moreauff1db4a2012-04-17 19:06:18 -0600209 struct ping_timer *ping_timer;
210
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200211 struct weston_transform workspace_transform;
212
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500213 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500214 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100215 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400216
217 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200218};
219
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300220struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700221 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300222 struct shell_surface *shsurf;
223 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300224 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300225};
226
227struct weston_move_grab {
228 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100229 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230};
231
Pekka Paalanen460099f2012-01-20 16:48:25 +0200232struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300233 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500234 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200235 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200236 float x;
237 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200238 } center;
239};
240
Giulio Camuffo5085a752013-03-25 21:42:45 +0100241struct shell_seat {
242 struct weston_seat *seat;
243 struct wl_listener seat_destroy_listener;
244
245 struct {
246 struct wl_pointer_grab grab;
247 struct wl_list surfaces_list;
248 struct wl_client *client;
249 int32_t initial_up;
250 } popup_grab;
251};
252
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400253static void
254activate(struct desktop_shell *shell, struct weston_surface *es,
255 struct weston_seat *seat);
256
257static struct workspace *
258get_current_workspace(struct desktop_shell *shell);
259
Alex Wubd3354b2012-04-17 17:20:49 +0800260static struct shell_surface *
261get_shell_surface(struct weston_surface *surface);
262
263static struct desktop_shell *
264shell_surface_get_shell(struct shell_surface *shsurf);
265
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500266static void
267surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
268
Alex Wubd3354b2012-04-17 17:20:49 +0800269static bool
270shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
271{
272 struct desktop_shell *shell;
273 struct weston_surface *top_fs_es;
274
275 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100276
Alex Wubd3354b2012-04-17 17:20:49 +0800277 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
278 return false;
279
280 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100281 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800282 layer_link);
283 return (shsurf == get_shell_surface(top_fs_es));
284}
285
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500286static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400287destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300288{
289 struct shell_grab *grab;
290
291 grab = container_of(listener, struct shell_grab,
292 shsurf_destroy_listener);
293
294 grab->shsurf = NULL;
295}
296
297static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400298popup_grab_end(struct wl_pointer *pointer);
299
300static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300301shell_grab_start(struct shell_grab *grab,
302 const struct wl_pointer_grab_interface *interface,
303 struct shell_surface *shsurf,
304 struct wl_pointer *pointer,
305 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300307 struct desktop_shell *shell = shsurf->shell;
308
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400309 popup_grab_end(pointer);
310
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300311 grab->grab.interface = interface;
312 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400313 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
314 wl_signal_add(&shsurf->resource.destroy_signal,
315 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300316
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300317 grab->pointer = pointer;
318 grab->grab.focus = &shsurf->surface->surface;
319
320 wl_pointer_start_grab(pointer, &grab->grab);
321 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
322 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
323 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324}
325
326static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300327shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300328{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400329 if (grab->shsurf)
330 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300331
332 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300333}
334
335static void
Alex Wu4539b082012-03-01 12:57:46 +0800336center_on_output(struct weston_surface *surface,
337 struct weston_output *output);
338
Daniel Stone496ca172012-05-30 16:31:42 +0100339static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300340get_modifier(char *modifier)
341{
342 if (!modifier)
343 return MODIFIER_SUPER;
344
345 if (!strcmp("ctrl", modifier))
346 return MODIFIER_CTRL;
347 else if (!strcmp("alt", modifier))
348 return MODIFIER_ALT;
349 else if (!strcmp("super", modifier))
350 return MODIFIER_SUPER;
351 else
352 return MODIFIER_SUPER;
353}
354
Juan Zhaoe10d2792012-04-25 19:09:52 +0800355static enum animation_type
356get_animation_type(char *animation)
357{
358 if (!animation)
359 return ANIMATION_NONE;
360
361 if (!strcmp("zoom", animation))
362 return ANIMATION_ZOOM;
363 else if (!strcmp("fade", animation))
364 return ANIMATION_FADE;
365 else
366 return ANIMATION_NONE;
367}
368
Alex Wu4539b082012-03-01 12:57:46 +0800369static void
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500370shell_configuration(struct desktop_shell *shell, const char *config_file)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200371{
Pekka Paalanen7296e792011-12-07 16:22:00 +0200372 char *path = NULL;
373 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200374 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300375 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800376 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200377
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400378 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300379 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800380 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200381 { "num-workspaces",
382 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200383 };
384
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400385 struct config_key saver_keys[] = {
386 { "path", CONFIG_KEY_STRING, &path },
387 { "duration", CONFIG_KEY_INTEGER, &duration },
388 };
389
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200390 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400391 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200392 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
393 };
394
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500395 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200396
Pekka Paalanen7296e792011-12-07 16:22:00 +0200397 shell->screensaver.path = path;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200398 shell->screensaver.duration = duration * 1000;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300399 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800400 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200401 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
402}
403
404static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200405focus_state_destroy(struct focus_state *state)
406{
407 wl_list_remove(&state->seat_destroy_listener.link);
408 wl_list_remove(&state->surface_destroy_listener.link);
409 free(state);
410}
411
412static void
413focus_state_seat_destroy(struct wl_listener *listener, void *data)
414{
415 struct focus_state *state = container_of(listener,
416 struct focus_state,
417 seat_destroy_listener);
418
419 wl_list_remove(&state->link);
420 focus_state_destroy(state);
421}
422
423static void
424focus_state_surface_destroy(struct wl_listener *listener, void *data)
425{
426 struct focus_state *state = container_of(listener,
427 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400428 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400429 struct desktop_shell *shell;
430 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200431
Kristian Høgsberge3778222012-07-31 17:29:30 -0400432 next = NULL;
433 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
434 if (surface == state->keyboard_focus)
435 continue;
436
437 next = surface;
438 break;
439 }
440
441 if (next) {
442 shell = state->seat->compositor->shell_interface.shell;
443 activate(shell, next, state->seat);
444 } else {
445 wl_list_remove(&state->link);
446 focus_state_destroy(state);
447 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200448}
449
450static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400451focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200452{
Jonas Ådahl04769742012-06-13 00:01:24 +0200453 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200454
455 state = malloc(sizeof *state);
456 if (state == NULL)
457 return NULL;
458
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400459 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200460 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400461 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200462
463 state->seat_destroy_listener.notify = focus_state_seat_destroy;
464 state->surface_destroy_listener.notify = focus_state_surface_destroy;
465 wl_signal_add(&seat->seat.destroy_signal,
466 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400467 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200468
469 return state;
470}
471
Jonas Ådahl8538b222012-08-29 22:13:03 +0200472static struct focus_state *
473ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
474{
475 struct workspace *ws = get_current_workspace(shell);
476 struct focus_state *state;
477
478 wl_list_for_each(state, &ws->focus_list, link)
479 if (state->seat == seat)
480 break;
481
482 if (&state->link == &ws->focus_list)
483 state = focus_state_create(seat, ws);
484
485 return state;
486}
487
Jonas Ådahl04769742012-06-13 00:01:24 +0200488static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400489restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200490{
491 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200492 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200493
494 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200495 surface = state->keyboard_focus ?
496 &state->keyboard_focus->surface : NULL;
497
498 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200499 }
500}
501
502static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200503replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
504 struct weston_seat *seat)
505{
506 struct focus_state *state;
507 struct wl_surface *surface;
508
509 wl_list_for_each(state, &ws->focus_list, link) {
510 if (state->seat == seat) {
511 surface = seat->seat.keyboard->focus;
512 state->keyboard_focus =
513 (struct weston_surface *) surface;
514 return;
515 }
516 }
517}
518
519static void
520drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
521 struct weston_surface *surface)
522{
523 struct focus_state *state;
524
525 wl_list_for_each(state, &ws->focus_list, link)
526 if (state->keyboard_focus == surface)
527 state->keyboard_focus = NULL;
528}
529
530static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200531workspace_destroy(struct workspace *ws)
532{
Jonas Ådahl04769742012-06-13 00:01:24 +0200533 struct focus_state *state, *next;
534
535 wl_list_for_each_safe(state, next, &ws->focus_list, link)
536 focus_state_destroy(state);
537
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200538 free(ws);
539}
540
Jonas Ådahl04769742012-06-13 00:01:24 +0200541static void
542seat_destroyed(struct wl_listener *listener, void *data)
543{
544 struct weston_seat *seat = data;
545 struct focus_state *state, *next;
546 struct workspace *ws = container_of(listener,
547 struct workspace,
548 seat_destroyed_listener);
549
550 wl_list_for_each_safe(state, next, &ws->focus_list, link)
551 if (state->seat == seat)
552 wl_list_remove(&state->link);
553}
554
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200555static struct workspace *
556workspace_create(void)
557{
558 struct workspace *ws = malloc(sizeof *ws);
559 if (ws == NULL)
560 return NULL;
561
562 weston_layer_init(&ws->layer, NULL);
563
Jonas Ådahl04769742012-06-13 00:01:24 +0200564 wl_list_init(&ws->focus_list);
565 wl_list_init(&ws->seat_destroyed_listener.link);
566 ws->seat_destroyed_listener.notify = seat_destroyed;
567
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200568 return ws;
569}
570
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200571static int
572workspace_is_empty(struct workspace *ws)
573{
574 return wl_list_empty(&ws->layer.surface_list);
575}
576
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200577static struct workspace *
578get_workspace(struct desktop_shell *shell, unsigned int index)
579{
580 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200581 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200582 pws += index;
583 return *pws;
584}
585
586static struct workspace *
587get_current_workspace(struct desktop_shell *shell)
588{
589 return get_workspace(shell, shell->workspaces.current);
590}
591
592static void
593activate_workspace(struct desktop_shell *shell, unsigned int index)
594{
595 struct workspace *ws;
596
597 ws = get_workspace(shell, index);
598 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
599
600 shell->workspaces.current = index;
601}
602
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200603static unsigned int
604get_output_height(struct weston_output *output)
605{
606 return abs(output->region.extents.y1 - output->region.extents.y2);
607}
608
609static void
610surface_translate(struct weston_surface *surface, double d)
611{
612 struct shell_surface *shsurf = get_shell_surface(surface);
613 struct weston_transform *transform;
614
615 transform = &shsurf->workspace_transform;
616 if (wl_list_empty(&transform->link))
617 wl_list_insert(surface->geometry.transformation_list.prev,
618 &shsurf->workspace_transform.link);
619
620 weston_matrix_init(&shsurf->workspace_transform.matrix);
621 weston_matrix_translate(&shsurf->workspace_transform.matrix,
622 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200623 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200624}
625
626static void
627workspace_translate_out(struct workspace *ws, double fraction)
628{
629 struct weston_surface *surface;
630 unsigned int height;
631 double d;
632
633 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
634 height = get_output_height(surface->output);
635 d = height * fraction;
636
637 surface_translate(surface, d);
638 }
639}
640
641static void
642workspace_translate_in(struct workspace *ws, double fraction)
643{
644 struct weston_surface *surface;
645 unsigned int height;
646 double d;
647
648 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
649 height = get_output_height(surface->output);
650
651 if (fraction > 0)
652 d = -(height - height * fraction);
653 else
654 d = height + height * fraction;
655
656 surface_translate(surface, d);
657 }
658}
659
660static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200661broadcast_current_workspace_state(struct desktop_shell *shell)
662{
663 struct wl_resource *resource;
664
665 wl_list_for_each(resource, &shell->workspaces.client_list, link)
666 workspace_manager_send_state(resource,
667 shell->workspaces.current,
668 shell->workspaces.num);
669}
670
671static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200672reverse_workspace_change_animation(struct desktop_shell *shell,
673 unsigned int index,
674 struct workspace *from,
675 struct workspace *to)
676{
677 shell->workspaces.current = index;
678
679 shell->workspaces.anim_to = to;
680 shell->workspaces.anim_from = from;
681 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
682 shell->workspaces.anim_timestamp = 0;
683
Scott Moreau4272e632012-08-13 09:58:41 -0600684 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200685}
686
687static void
688workspace_deactivate_transforms(struct workspace *ws)
689{
690 struct weston_surface *surface;
691 struct shell_surface *shsurf;
692
693 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
694 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200695 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
696 wl_list_remove(&shsurf->workspace_transform.link);
697 wl_list_init(&shsurf->workspace_transform.link);
698 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200699 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200700 }
701}
702
703static void
704finish_workspace_change_animation(struct desktop_shell *shell,
705 struct workspace *from,
706 struct workspace *to)
707{
Scott Moreau4272e632012-08-13 09:58:41 -0600708 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200709
710 wl_list_remove(&shell->workspaces.animation.link);
711 workspace_deactivate_transforms(from);
712 workspace_deactivate_transforms(to);
713 shell->workspaces.anim_to = NULL;
714
715 wl_list_remove(&shell->workspaces.anim_from->layer.link);
716}
717
718static void
719animate_workspace_change_frame(struct weston_animation *animation,
720 struct weston_output *output, uint32_t msecs)
721{
722 struct desktop_shell *shell =
723 container_of(animation, struct desktop_shell,
724 workspaces.animation);
725 struct workspace *from = shell->workspaces.anim_from;
726 struct workspace *to = shell->workspaces.anim_to;
727 uint32_t t;
728 double x, y;
729
730 if (workspace_is_empty(from) && workspace_is_empty(to)) {
731 finish_workspace_change_animation(shell, from, to);
732 return;
733 }
734
735 if (shell->workspaces.anim_timestamp == 0) {
736 if (shell->workspaces.anim_current == 0.0)
737 shell->workspaces.anim_timestamp = msecs;
738 else
739 shell->workspaces.anim_timestamp =
740 msecs -
741 /* Invers of movement function 'y' below. */
742 (asin(1.0 - shell->workspaces.anim_current) *
743 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
744 M_2_PI);
745 }
746
747 t = msecs - shell->workspaces.anim_timestamp;
748
749 /*
750 * x = [0, π/2]
751 * y(x) = sin(x)
752 */
753 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
754 y = sin(x);
755
756 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600757 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200758
759 workspace_translate_out(from, shell->workspaces.anim_dir * y);
760 workspace_translate_in(to, shell->workspaces.anim_dir * y);
761 shell->workspaces.anim_current = y;
762
Scott Moreau4272e632012-08-13 09:58:41 -0600763 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200764 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200765 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200766 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200767}
768
769static void
770animate_workspace_change(struct desktop_shell *shell,
771 unsigned int index,
772 struct workspace *from,
773 struct workspace *to)
774{
775 struct weston_output *output;
776
777 int dir;
778
779 if (index > shell->workspaces.current)
780 dir = -1;
781 else
782 dir = 1;
783
784 shell->workspaces.current = index;
785
786 shell->workspaces.anim_dir = dir;
787 shell->workspaces.anim_from = from;
788 shell->workspaces.anim_to = to;
789 shell->workspaces.anim_current = 0.0;
790 shell->workspaces.anim_timestamp = 0;
791
792 output = container_of(shell->compositor->output_list.next,
793 struct weston_output, link);
794 wl_list_insert(&output->animation_list,
795 &shell->workspaces.animation.link);
796
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200797 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200798
799 workspace_translate_in(to, 0);
800
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400801 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200802
Scott Moreau4272e632012-08-13 09:58:41 -0600803 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200804}
805
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200806static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200807update_workspace(struct desktop_shell *shell, unsigned int index,
808 struct workspace *from, struct workspace *to)
809{
810 shell->workspaces.current = index;
811 wl_list_insert(&from->layer.link, &to->layer.link);
812 wl_list_remove(&from->layer.link);
813}
814
815static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200816change_workspace(struct desktop_shell *shell, unsigned int index)
817{
818 struct workspace *from;
819 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200820
821 if (index == shell->workspaces.current)
822 return;
823
824 /* Don't change workspace when there is any fullscreen surfaces. */
825 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
826 return;
827
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828 from = get_current_workspace(shell);
829 to = get_workspace(shell, index);
830
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200831 if (shell->workspaces.anim_from == to &&
832 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200833 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200834 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200835 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200836 return;
837 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200838
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200839 if (shell->workspaces.anim_to != NULL)
840 finish_workspace_change_animation(shell,
841 shell->workspaces.anim_from,
842 shell->workspaces.anim_to);
843
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200844 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200845
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200846 if (workspace_is_empty(to) && workspace_is_empty(from))
847 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200848 else
849 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200850
851 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200852}
853
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200854static bool
855workspace_has_only(struct workspace *ws, struct weston_surface *surface)
856{
857 struct wl_list *list = &ws->layer.surface_list;
858 struct wl_list *e;
859
860 if (wl_list_empty(list))
861 return false;
862
863 e = list->next;
864
865 if (e->next != list)
866 return false;
867
868 return container_of(e, struct weston_surface, layer_link) == surface;
869}
870
871static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200872move_surface_to_workspace(struct desktop_shell *shell,
873 struct weston_surface *surface,
874 uint32_t workspace)
875{
876 struct workspace *from;
877 struct workspace *to;
878 struct weston_seat *seat;
879
880 if (workspace == shell->workspaces.current)
881 return;
882
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200883 if (workspace >= shell->workspaces.num)
884 workspace = shell->workspaces.num - 1;
885
Jonas Ådahle9d22502012-08-29 22:13:01 +0200886 from = get_current_workspace(shell);
887 to = get_workspace(shell, workspace);
888
889 wl_list_remove(&surface->layer_link);
890 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
891
892 drop_focus_state(shell, from, surface);
893 wl_list_for_each(seat, &shell->compositor->seat_list, link)
894 if (seat->has_keyboard &&
Jan Arne Petersena75a7892013-01-16 21:26:50 +0100895 seat->keyboard.keyboard.focus == &surface->surface)
896 wl_keyboard_set_focus(&seat->keyboard.keyboard, NULL);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200897
898 weston_surface_damage_below(surface);
899}
900
901static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200902take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200903 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200904 unsigned int index)
905{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200906 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200907 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200908 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200909 struct shell_surface *shsurf;
910 struct workspace *from;
911 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200912 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200913
914 if (surface == NULL ||
915 index == shell->workspaces.current)
916 return;
917
918 from = get_current_workspace(shell);
919 to = get_workspace(shell, index);
920
921 wl_list_remove(&surface->layer_link);
922 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
923
Jonas Ådahle9d22502012-08-29 22:13:01 +0200924 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200925 drop_focus_state(shell, from, surface);
926
927 if (shell->workspaces.anim_from == to &&
928 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200929 wl_list_remove(&to->layer.link);
930 wl_list_insert(from->layer.link.prev, &to->layer.link);
931
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200933 broadcast_current_workspace_state(shell);
934
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200935 return;
936 }
937
938 if (shell->workspaces.anim_to != NULL)
939 finish_workspace_change_animation(shell,
940 shell->workspaces.anim_from,
941 shell->workspaces.anim_to);
942
943 if (workspace_is_empty(from) &&
944 workspace_has_only(to, surface))
945 update_workspace(shell, index, from, to);
946 else {
947 shsurf = get_shell_surface(surface);
948 if (wl_list_empty(&shsurf->workspace_transform.link))
949 wl_list_insert(&shell->workspaces.anim_sticky_list,
950 &shsurf->workspace_transform.link);
951
952 animate_workspace_change(shell, index, from, to);
953 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200954
955 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200956
957 state = ensure_focus_state(shell, seat);
958 if (state != NULL)
959 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200960}
961
962static void
963workspace_manager_move_surface(struct wl_client *client,
964 struct wl_resource *resource,
965 struct wl_resource *surface_resource,
966 uint32_t workspace)
967{
968 struct desktop_shell *shell = resource->data;
969 struct weston_surface *surface =
970 (struct weston_surface *) surface_resource;
971
972 move_surface_to_workspace(shell, surface, workspace);
973}
974
975static const struct workspace_manager_interface workspace_manager_implementation = {
976 workspace_manager_move_surface,
977};
978
979static void
980unbind_resource(struct wl_resource *resource)
981{
982 wl_list_remove(&resource->link);
983 free(resource);
984}
985
986static void
987bind_workspace_manager(struct wl_client *client,
988 void *data, uint32_t version, uint32_t id)
989{
990 struct desktop_shell *shell = data;
991 struct wl_resource *resource;
992
993 resource = wl_client_add_object(client, &workspace_manager_interface,
994 &workspace_manager_implementation,
995 id, shell);
996
997 if (resource == NULL) {
998 weston_log("couldn't add workspace manager object");
999 return;
1000 }
1001
1002 resource->destroy = unbind_resource;
1003 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1004
1005 workspace_manager_send_state(resource,
1006 shell->workspaces.current,
1007 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001008}
1009
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001010static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001011noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001012 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001013{
1014 grab->focus = NULL;
1015}
1016
1017static void
Scott Moreau447013d2012-02-18 05:05:29 -07001018move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001019 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001020{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001021 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001022 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001023 struct shell_surface *shsurf = move->base.shsurf;
1024 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001025 int dx = wl_fixed_to_int(pointer->x + move->dx);
1026 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001027
1028 if (!shsurf)
1029 return;
1030
1031 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001032
Daniel Stone103db7f2012-05-08 17:17:55 +01001033 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001034 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001035
1036 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001037}
1038
1039static void
Scott Moreau447013d2012-02-18 05:05:29 -07001040move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001041 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001042{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001043 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1044 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001045 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001046 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001047
Daniel Stone4dbadb12012-05-30 16:31:51 +01001048 if (pointer->button_count == 0 &&
1049 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001050 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001051 free(grab);
1052 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001053}
1054
Scott Moreau447013d2012-02-18 05:05:29 -07001055static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001056 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001057 move_grab_motion,
1058 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001059};
1060
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001061static int
1062surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1063{
1064 struct weston_move_grab *move;
1065
1066 if (!shsurf)
1067 return -1;
1068
1069 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1070 return 0;
1071
1072 move = malloc(sizeof *move);
1073 if (!move)
1074 return -1;
1075
1076 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1077 ws->seat.pointer->grab_x;
1078 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1079 ws->seat.pointer->grab_y;
1080
1081 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1082 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1083
1084 return 0;
1085}
1086
1087static void
1088shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1089 struct wl_resource *seat_resource, uint32_t serial)
1090{
1091 struct weston_seat *ws = seat_resource->data;
1092 struct shell_surface *shsurf = resource->data;
1093
1094 if (ws->seat.pointer->button_count == 0 ||
1095 ws->seat.pointer->grab_serial != serial ||
1096 ws->seat.pointer->focus != &shsurf->surface->surface)
1097 return;
1098
1099 if (surface_move(shsurf, ws) < 0)
1100 wl_resource_post_no_memory(resource);
1101}
1102
1103struct weston_resize_grab {
1104 struct shell_grab base;
1105 uint32_t edges;
1106 int32_t width, height;
1107};
1108
1109static void
1110resize_grab_motion(struct wl_pointer_grab *grab,
1111 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1112{
1113 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1114 struct wl_pointer *pointer = grab->pointer;
1115 struct shell_surface *shsurf = resize->base.shsurf;
1116 int32_t width, height;
1117 wl_fixed_t from_x, from_y;
1118 wl_fixed_t to_x, to_y;
1119
1120 if (!shsurf)
1121 return;
1122
1123 weston_surface_from_global_fixed(shsurf->surface,
1124 pointer->grab_x, pointer->grab_y,
1125 &from_x, &from_y);
1126 weston_surface_from_global_fixed(shsurf->surface,
1127 pointer->x, pointer->y, &to_x, &to_y);
1128
1129 width = resize->width;
1130 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1131 width += wl_fixed_to_int(from_x - to_x);
1132 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1133 width += wl_fixed_to_int(to_x - from_x);
1134 }
1135
1136 height = resize->height;
1137 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1138 height += wl_fixed_to_int(from_y - to_y);
1139 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1140 height += wl_fixed_to_int(to_y - from_y);
1141 }
1142
1143 shsurf->client->send_configure(shsurf->surface,
1144 resize->edges, width, height);
1145}
1146
1147static void
1148send_configure(struct weston_surface *surface,
1149 uint32_t edges, int32_t width, int32_t height)
1150{
1151 struct shell_surface *shsurf = get_shell_surface(surface);
1152
1153 wl_shell_surface_send_configure(&shsurf->resource,
1154 edges, width, height);
1155}
1156
1157static const struct weston_shell_client shell_client = {
1158 send_configure
1159};
1160
1161static void
1162resize_grab_button(struct wl_pointer_grab *grab,
1163 uint32_t time, uint32_t button, uint32_t state_w)
1164{
1165 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1166 struct wl_pointer *pointer = grab->pointer;
1167 enum wl_pointer_button_state state = state_w;
1168
1169 if (pointer->button_count == 0 &&
1170 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1171 shell_grab_end(&resize->base);
1172 free(grab);
1173 }
1174}
1175
1176static const struct wl_pointer_grab_interface resize_grab_interface = {
1177 noop_grab_focus,
1178 resize_grab_motion,
1179 resize_grab_button,
1180};
1181
1182static int
1183surface_resize(struct shell_surface *shsurf,
1184 struct weston_seat *ws, uint32_t edges)
1185{
1186 struct weston_resize_grab *resize;
1187
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001188 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1189 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001190 return 0;
1191
1192 if (edges == 0 || edges > 15 ||
1193 (edges & 3) == 3 || (edges & 12) == 12)
1194 return 0;
1195
1196 resize = malloc(sizeof *resize);
1197 if (!resize)
1198 return -1;
1199
1200 resize->edges = edges;
1201 resize->width = shsurf->surface->geometry.width;
1202 resize->height = shsurf->surface->geometry.height;
1203
1204 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1205 ws->seat.pointer, edges);
1206
1207 return 0;
1208}
1209
1210static void
1211shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1212 struct wl_resource *seat_resource, uint32_t serial,
1213 uint32_t edges)
1214{
1215 struct weston_seat *ws = seat_resource->data;
1216 struct shell_surface *shsurf = resource->data;
1217
1218 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1219 return;
1220
1221 if (ws->seat.pointer->button_count == 0 ||
1222 ws->seat.pointer->grab_serial != serial ||
1223 ws->seat.pointer->focus != &shsurf->surface->surface)
1224 return;
1225
1226 if (surface_resize(shsurf, ws, edges) < 0)
1227 wl_resource_post_no_memory(resource);
1228}
1229
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001230static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001231busy_cursor_grab_focus(struct wl_pointer_grab *base,
1232 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001233{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001234 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001235
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001236 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001237 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001238 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001239 }
1240}
1241
1242static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001243busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1244 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001245{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001246}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001247
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001248static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001249busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001250 uint32_t time, uint32_t button, uint32_t state)
1251{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001252 struct shell_grab *grab = (struct shell_grab *) base;
1253 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001254 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001255 (struct weston_surface *) grab->grab.pointer->current;
1256 struct weston_seat *seat =
1257 (struct weston_seat *) grab->grab.pointer->seat;
1258
1259 shsurf = get_shell_surface(surface);
1260 if (shsurf && button == BTN_LEFT && state) {
1261 activate(shsurf->shell, shsurf->surface, seat);
1262 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001263 } else if (shsurf && button == BTN_RIGHT && state) {
1264 activate(shsurf->shell, shsurf->surface, seat);
1265 surface_rotate(shsurf, &seat->seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001266 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001267}
1268
1269static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1270 busy_cursor_grab_focus,
1271 busy_cursor_grab_motion,
1272 busy_cursor_grab_button,
1273};
1274
1275static void
1276set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1277{
1278 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001279
1280 grab = malloc(sizeof *grab);
1281 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001282 return;
1283
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001284 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1285 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001286}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001287
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001288static void
1289end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1290{
1291 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1292
1293 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001294 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001295 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001296 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001297}
1298
Scott Moreau9521d5e2012-04-19 13:06:17 -06001299static void
1300ping_timer_destroy(struct shell_surface *shsurf)
1301{
1302 if (!shsurf || !shsurf->ping_timer)
1303 return;
1304
1305 if (shsurf->ping_timer->source)
1306 wl_event_source_remove(shsurf->ping_timer->source);
1307
1308 free(shsurf->ping_timer);
1309 shsurf->ping_timer = NULL;
1310}
1311
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001312static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001313ping_timeout_handler(void *data)
1314{
1315 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001316 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001317
Scott Moreau9521d5e2012-04-19 13:06:17 -06001318 /* Client is not responding */
1319 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001320
1321 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1322 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1323 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001324
1325 return 1;
1326}
1327
1328static void
1329ping_handler(struct weston_surface *surface, uint32_t serial)
1330{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001331 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001332 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001333 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001334
1335 if (!shsurf)
1336 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001337 if (!shsurf->resource.client)
1338 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001339
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001340 if (shsurf->surface == shsurf->shell->grab_surface)
1341 return;
1342
Scott Moreauff1db4a2012-04-17 19:06:18 -06001343 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001344 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001345 if (!shsurf->ping_timer)
1346 return;
1347
1348 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001349 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1350 shsurf->ping_timer->source =
1351 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1352 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1353
1354 wl_shell_surface_send_ping(&shsurf->resource, serial);
1355 }
1356}
1357
1358static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001359handle_pointer_focus(struct wl_listener *listener, void *data)
1360{
1361 struct wl_pointer *pointer = data;
1362 struct weston_surface *surface =
1363 (struct weston_surface *) pointer->focus;
1364 struct weston_compositor *compositor;
1365 struct shell_surface *shsurf;
1366 uint32_t serial;
1367
1368 if (!surface)
1369 return;
1370
1371 compositor = surface->compositor;
1372 shsurf = get_shell_surface(surface);
1373
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001374 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001375 set_busy_cursor(shsurf, pointer);
1376 } else {
1377 serial = wl_display_next_serial(compositor->wl_display);
1378 ping_handler(surface, serial);
1379 }
1380}
1381
1382static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001383create_pointer_focus_listener(struct weston_seat *seat)
1384{
1385 struct wl_listener *listener;
1386
1387 if (!seat->seat.pointer)
1388 return;
1389
1390 listener = malloc(sizeof *listener);
1391 listener->notify = handle_pointer_focus;
1392 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1393}
1394
1395static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001396shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1397 uint32_t serial)
1398{
1399 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001400 struct desktop_shell *shell = shsurf->shell;
1401 struct weston_seat *seat;
1402 struct weston_compositor *ec = shsurf->surface->compositor;
1403 struct wl_pointer *pointer;
1404 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001405
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001406 if (shsurf->ping_timer == NULL)
1407 /* Just ignore unsolicited pong. */
1408 return;
1409
Scott Moreauff1db4a2012-04-17 19:06:18 -06001410 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001411 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001412 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001413 if (was_unresponsive) {
1414 /* Received pong from previously unresponsive client */
1415 wl_list_for_each(seat, &ec->seat_list, link) {
1416 pointer = seat->seat.pointer;
1417 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001418 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001419 pointer->current ==
1420 &shsurf->surface->surface)
1421 end_busy_cursor(shsurf, pointer);
1422 }
1423 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001424 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001425 }
1426}
1427
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001428static void
1429shell_surface_set_title(struct wl_client *client,
1430 struct wl_resource *resource, const char *title)
1431{
1432 struct shell_surface *shsurf = resource->data;
1433
1434 free(shsurf->title);
1435 shsurf->title = strdup(title);
1436}
1437
1438static void
1439shell_surface_set_class(struct wl_client *client,
1440 struct wl_resource *resource, const char *class)
1441{
1442 struct shell_surface *shsurf = resource->data;
1443
1444 free(shsurf->class);
1445 shsurf->class = strdup(class);
1446}
1447
Juan Zhao96879df2012-02-07 08:45:41 +08001448static struct weston_output *
1449get_default_output(struct weston_compositor *compositor)
1450{
1451 return container_of(compositor->output_list.next,
1452 struct weston_output, link);
1453}
1454
Alex Wu4539b082012-03-01 12:57:46 +08001455static void
1456shell_unset_fullscreen(struct shell_surface *shsurf)
1457{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001458 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001459 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001460 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1461 shell_surface_is_top_fullscreen(shsurf)) {
1462 weston_output_switch_mode(shsurf->fullscreen_output,
1463 shsurf->fullscreen_output->origin);
1464 }
Alex Wu4539b082012-03-01 12:57:46 +08001465 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1466 shsurf->fullscreen.framerate = 0;
1467 wl_list_remove(&shsurf->fullscreen.transform.link);
1468 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001469 if (shsurf->fullscreen.black_surface)
1470 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001471 shsurf->fullscreen.black_surface = NULL;
1472 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001473 weston_surface_set_position(shsurf->surface,
1474 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001475 if (shsurf->saved_rotation_valid) {
1476 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1477 &shsurf->rotation.transform.link);
1478 shsurf->saved_rotation_valid = false;
1479 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001480
1481 ws = get_current_workspace(shsurf->shell);
1482 wl_list_remove(&shsurf->surface->layer_link);
1483 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001484}
1485
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001486static void
1487shell_unset_maximized(struct shell_surface *shsurf)
1488{
1489 struct workspace *ws;
1490 /* undo all maximized things here */
1491 shsurf->output = get_default_output(shsurf->surface->compositor);
1492 weston_surface_set_position(shsurf->surface,
1493 shsurf->saved_x,
1494 shsurf->saved_y);
1495
1496 if (shsurf->saved_rotation_valid) {
1497 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1498 &shsurf->rotation.transform.link);
1499 shsurf->saved_rotation_valid = false;
1500 }
1501
1502 ws = get_current_workspace(shsurf->shell);
1503 wl_list_remove(&shsurf->surface->layer_link);
1504 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1505}
1506
Pekka Paalanen98262232011-12-01 10:42:22 +02001507static int
1508reset_shell_surface_type(struct shell_surface *surface)
1509{
1510 switch (surface->type) {
1511 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001512 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001513 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001514 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001515 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001516 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001517 case SHELL_SURFACE_NONE:
1518 case SHELL_SURFACE_TOPLEVEL:
1519 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001520 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001521 break;
1522 }
1523
1524 surface->type = SHELL_SURFACE_NONE;
1525 return 0;
1526}
1527
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001528static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001529set_surface_type(struct shell_surface *shsurf)
1530{
1531 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001532 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001533
1534 reset_shell_surface_type(shsurf);
1535
1536 shsurf->type = shsurf->next_type;
1537 shsurf->next_type = SHELL_SURFACE_NONE;
1538
1539 switch (shsurf->type) {
1540 case SHELL_SURFACE_TOPLEVEL:
1541 break;
1542 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001543 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001544 pes->geometry.x + shsurf->transient.x,
1545 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001546 break;
1547
1548 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001549 case SHELL_SURFACE_FULLSCREEN:
1550 shsurf->saved_x = surface->geometry.x;
1551 shsurf->saved_y = surface->geometry.y;
1552 shsurf->saved_position_valid = true;
1553
1554 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1555 wl_list_remove(&shsurf->rotation.transform.link);
1556 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001557 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001558 shsurf->saved_rotation_valid = true;
1559 }
1560 break;
1561
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001562 default:
1563 break;
1564 }
1565}
1566
1567static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001568set_toplevel(struct shell_surface *shsurf)
1569{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001570 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001571}
1572
1573static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001574shell_surface_set_toplevel(struct wl_client *client,
1575 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001576{
Pekka Paalanen98262232011-12-01 10:42:22 +02001577 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001578
Tiago Vignattibc052c92012-04-19 16:18:18 +03001579 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001580}
1581
1582static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001583set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001584 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001585{
1586 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001587 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001588 shsurf->transient.x = x;
1589 shsurf->transient.y = y;
1590 shsurf->transient.flags = flags;
1591 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1592}
1593
1594static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001595shell_surface_set_transient(struct wl_client *client,
1596 struct wl_resource *resource,
1597 struct wl_resource *parent_resource,
1598 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001599{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001600 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001601 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001602
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001603 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001604}
1605
Tiago Vignattibe143262012-04-16 17:31:41 +03001606static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001607shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001608{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001609 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001610}
1611
1612static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001613get_output_panel_height(struct desktop_shell *shell,
1614 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001615{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001616 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001617 int panel_height = 0;
1618
1619 if (!output)
1620 return 0;
1621
Juan Zhao4ab94682012-07-09 22:24:09 -07001622 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001623 if (surface->output == output) {
1624 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001625 break;
1626 }
1627 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001628
Juan Zhao96879df2012-02-07 08:45:41 +08001629 return panel_height;
1630}
1631
1632static void
1633shell_surface_set_maximized(struct wl_client *client,
1634 struct wl_resource *resource,
1635 struct wl_resource *output_resource )
1636{
1637 struct shell_surface *shsurf = resource->data;
1638 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001639 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001640 uint32_t edges = 0, panel_height = 0;
1641
1642 /* get the default output, if the client set it as NULL
1643 check whether the ouput is available */
1644 if (output_resource)
1645 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001646 else if (es->output)
1647 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001648 else
1649 shsurf->output = get_default_output(es->compositor);
1650
Tiago Vignattibe143262012-04-16 17:31:41 +03001651 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001652 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001653 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001654
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001655 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001656 shsurf->output->width,
1657 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001658
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001659 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001660}
1661
Alex Wu21858432012-04-01 20:13:08 +08001662static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001663black_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 +08001664
Alex Wu4539b082012-03-01 12:57:46 +08001665static struct weston_surface *
1666create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001667 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001668 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001669{
1670 struct weston_surface *surface = NULL;
1671
1672 surface = weston_surface_create(ec);
1673 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001674 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001675 return NULL;
1676 }
1677
Alex Wu21858432012-04-01 20:13:08 +08001678 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001679 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001680 weston_surface_configure(surface, x, y, w, h);
1681 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001682 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001683 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001684 pixman_region32_fini(&surface->input);
1685 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001686
Alex Wu4539b082012-03-01 12:57:46 +08001687 return surface;
1688}
1689
1690/* Create black surface and append it to the associated fullscreen surface.
1691 * Handle size dismatch and positioning according to the method. */
1692static void
1693shell_configure_fullscreen(struct shell_surface *shsurf)
1694{
1695 struct weston_output *output = shsurf->fullscreen_output;
1696 struct weston_surface *surface = shsurf->surface;
1697 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001698 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001699
Alex Wu4539b082012-03-01 12:57:46 +08001700 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001701 shsurf->fullscreen.black_surface =
1702 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001703 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001704 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001705 output->width,
1706 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001707
1708 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1709 wl_list_insert(&surface->layer_link,
1710 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001711 shsurf->fullscreen.black_surface->output = output;
1712
1713 switch (shsurf->fullscreen.type) {
1714 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001715 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001716 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001717 break;
1718 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001719 /* 1:1 mapping between surface and output dimensions */
1720 if (output->width == surface->geometry.width &&
1721 output->height == surface->geometry.height) {
1722 weston_surface_set_position(surface, output->x, output->y);
1723 break;
1724 }
1725
Alex Wu4539b082012-03-01 12:57:46 +08001726 matrix = &shsurf->fullscreen.transform.matrix;
1727 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001728
Scott Moreau1bad5db2012-08-18 01:04:05 -06001729 output_aspect = (float) output->width /
1730 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001731 surface_aspect = (float) surface->geometry.width /
1732 (float) surface->geometry.height;
1733 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001734 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001735 (float) surface->geometry.width;
1736 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001737 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001738 (float) surface->geometry.height;
1739
Alex Wu4539b082012-03-01 12:57:46 +08001740 weston_matrix_scale(matrix, scale, scale, 1);
1741 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001742 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001743 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001744 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1745 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001746 weston_surface_set_position(surface, x, y);
1747
Alex Wu4539b082012-03-01 12:57:46 +08001748 break;
1749 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001750 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001751 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001752 surface->geometry.width,
1753 surface->geometry.height,
1754 shsurf->fullscreen.framerate};
1755
1756 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001757 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001758 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001759 output->width,
1760 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001761 weston_surface_set_position(surface, output->x, output->y);
1762 break;
1763 }
1764 }
Alex Wu4539b082012-03-01 12:57:46 +08001765 break;
1766 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1767 break;
1768 default:
1769 break;
1770 }
1771}
1772
1773/* make the fullscreen and black surface at the top */
1774static void
1775shell_stack_fullscreen(struct shell_surface *shsurf)
1776{
Alex Wubd3354b2012-04-17 17:20:49 +08001777 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001778 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001779 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001780
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001781 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001782 wl_list_insert(&shell->fullscreen_layer.surface_list,
1783 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001784 weston_surface_damage(surface);
1785
1786 if (!shsurf->fullscreen.black_surface)
1787 shsurf->fullscreen.black_surface =
1788 create_black_surface(surface->compositor,
1789 surface,
1790 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001791 output->width,
1792 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001793
1794 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001795 wl_list_insert(&surface->layer_link,
1796 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001797 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001798}
1799
1800static void
1801shell_map_fullscreen(struct shell_surface *shsurf)
1802{
Alex Wu4539b082012-03-01 12:57:46 +08001803 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001804 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001805}
1806
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001807static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001808set_fullscreen(struct shell_surface *shsurf,
1809 uint32_t method,
1810 uint32_t framerate,
1811 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001812{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001813 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001814
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001815 if (output)
1816 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001817 else if (es->output)
1818 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001819 else
1820 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001821
Alex Wu4539b082012-03-01 12:57:46 +08001822 shsurf->fullscreen_output = shsurf->output;
1823 shsurf->fullscreen.type = method;
1824 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001825 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001826
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001827 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001828 shsurf->output->width,
1829 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001830}
1831
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001832static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001833shell_surface_set_fullscreen(struct wl_client *client,
1834 struct wl_resource *resource,
1835 uint32_t method,
1836 uint32_t framerate,
1837 struct wl_resource *output_resource)
1838{
1839 struct shell_surface *shsurf = resource->data;
1840 struct weston_output *output;
1841
1842 if (output_resource)
1843 output = output_resource->data;
1844 else
1845 output = NULL;
1846
1847 set_fullscreen(shsurf, method, framerate, output);
1848}
1849
Giulio Camuffo5085a752013-03-25 21:42:45 +01001850static const struct wl_pointer_grab_interface popup_grab_interface;
1851
1852static void
1853destroy_shell_seat(struct wl_listener *listener, void *data)
1854{
1855 struct shell_seat *shseat =
1856 container_of(listener,
1857 struct shell_seat, seat_destroy_listener);
1858 struct shell_surface *shsurf, *prev = NULL;
1859
1860 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
1861 wl_pointer_end_grab(shseat->popup_grab.grab.pointer);
1862 shseat->popup_grab.client = NULL;
1863
1864 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1865 shsurf->popup.shseat = NULL;
1866 if (prev) {
1867 wl_list_init(&prev->popup.grab_link);
1868 }
1869 prev = shsurf;
1870 }
1871 wl_list_init(&prev->popup.grab_link);
1872 }
1873
1874 wl_list_remove(&shseat->seat_destroy_listener.link);
1875 free(shseat);
1876}
1877
1878static struct shell_seat *
1879create_shell_seat(struct weston_seat *seat)
1880{
1881 struct shell_seat *shseat;
1882
1883 shseat = calloc(1, sizeof *shseat);
1884 if (!shseat) {
1885 weston_log("no memory to allocate shell seat\n");
1886 return NULL;
1887 }
1888
1889 shseat->seat = seat;
1890 wl_list_init(&shseat->popup_grab.surfaces_list);
1891
1892 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1893 wl_signal_add(&seat->destroy_signal,
1894 &shseat->seat_destroy_listener);
1895
1896 return shseat;
1897}
1898
1899static struct shell_seat *
1900get_shell_seat(struct weston_seat *seat)
1901{
1902 struct wl_listener *listener;
1903
1904 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1905 if (listener == NULL)
1906 return create_shell_seat(seat);
1907
1908 return container_of(listener,
1909 struct shell_seat, seat_destroy_listener);
1910}
1911
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001912static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001913popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001914 struct wl_surface *surface,
1915 wl_fixed_t x,
1916 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001917{
Daniel Stone37816df2012-05-16 18:45:18 +01001918 struct wl_pointer *pointer = grab->pointer;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001919 struct shell_seat *shseat =
1920 container_of(grab, struct shell_seat, popup_grab.grab);
1921 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001922
Pekka Paalanencb108432012-01-19 16:25:40 +02001923 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001924 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001925 grab->focus = surface;
1926 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001927 wl_pointer_set_focus(pointer, NULL,
1928 wl_fixed_from_int(0),
1929 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001930 grab->focus = NULL;
1931 }
1932}
1933
1934static void
Scott Moreau447013d2012-02-18 05:05:29 -07001935popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001936 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001937{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001938 struct wl_resource *resource;
1939
Daniel Stone37816df2012-05-16 18:45:18 +01001940 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001941 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001942 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001943}
1944
1945static void
Scott Moreau447013d2012-02-18 05:05:29 -07001946popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001947 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001948{
1949 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001950 struct shell_seat *shseat =
1951 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001952 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001953 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001954 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001955
Daniel Stone37816df2012-05-16 18:45:18 +01001956 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001957 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001958 display = wl_client_get_display(resource->client);
1959 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001960 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001961 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01001962 (shseat->popup_grab.initial_up ||
1963 time - shseat->seat->pointer.grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001964 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001965 }
1966
Daniel Stone4dbadb12012-05-30 16:31:51 +01001967 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01001968 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001969}
1970
Scott Moreau447013d2012-02-18 05:05:29 -07001971static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001972 popup_grab_focus,
1973 popup_grab_motion,
1974 popup_grab_button,
1975};
1976
1977static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001978popup_grab_end(struct wl_pointer *pointer)
1979{
1980 struct wl_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001981 struct shell_seat *shseat =
1982 container_of(grab, struct shell_seat, popup_grab.grab);
1983 struct shell_surface *shsurf;
1984 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001985
1986 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001987 wl_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001988 shseat->popup_grab.client = NULL;
1989 /* Send the popup_done event to all the popups open */
1990 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1991 wl_shell_surface_send_popup_done(&shsurf->resource);
1992 shsurf->popup.shseat = NULL;
1993 if (prev) {
1994 wl_list_init(&prev->popup.grab_link);
1995 }
1996 prev = shsurf;
1997 }
1998 wl_list_init(&prev->popup.grab_link);
1999 wl_list_init(&shseat->popup_grab.surfaces_list);
2000 }
2001}
2002
2003static void
2004add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2005{
2006 struct wl_seat *seat = &shseat->seat->seat;
2007
2008 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2009 shseat->popup_grab.client = shsurf->surface->surface.resource.client;
2010 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002011 /* We must make sure here that this popup was opened after
2012 * a mouse press, and not just by moving around with other
2013 * popups already open. */
2014 if (shseat->seat->pointer.button_count > 0)
2015 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002016
2017 wl_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
2018 }
2019 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2020}
2021
2022static void
2023remove_popup_grab(struct shell_surface *shsurf)
2024{
2025 struct shell_seat *shseat = shsurf->popup.shseat;
2026
2027 wl_list_remove(&shsurf->popup.grab_link);
2028 wl_list_init(&shsurf->popup.grab_link);
2029 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2030 wl_pointer_end_grab(shseat->popup_grab.grab.pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002031 }
2032}
2033
2034static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002035shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002036{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002037 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002038 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002039 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002040
2041 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002042
Pekka Paalanen483243f2013-03-08 14:56:50 +02002043 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002044 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2045 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002046
Giulio Camuffo5085a752013-03-25 21:42:45 +01002047 if (shseat->seat->pointer.grab_serial == shsurf->popup.serial) {
2048 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002049 } else {
2050 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002051 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002052 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002053}
2054
2055static void
2056shell_surface_set_popup(struct wl_client *client,
2057 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002058 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002059 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002060 struct wl_resource *parent_resource,
2061 int32_t x, int32_t y, uint32_t flags)
2062{
2063 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002064
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002065 shsurf->type = SHELL_SURFACE_POPUP;
2066 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002067 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002068 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002069 shsurf->popup.x = x;
2070 shsurf->popup.y = y;
2071}
2072
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002073static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002074 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002075 shell_surface_move,
2076 shell_surface_resize,
2077 shell_surface_set_toplevel,
2078 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002079 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002080 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002081 shell_surface_set_maximized,
2082 shell_surface_set_title,
2083 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002084};
2085
2086static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002087destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002088{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002089 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2090 remove_popup_grab(shsurf);
2091 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002092
Alex Wubd3354b2012-04-17 17:20:49 +08002093 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2094 shell_surface_is_top_fullscreen(shsurf)) {
2095 weston_output_switch_mode(shsurf->fullscreen_output,
2096 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002097 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002098
Alex Wuaa08e2d2012-03-05 11:01:40 +08002099 if (shsurf->fullscreen.black_surface)
2100 weston_surface_destroy(shsurf->fullscreen.black_surface);
2101
Alex Wubd3354b2012-04-17 17:20:49 +08002102 /* As destroy_resource() use wl_list_for_each_safe(),
2103 * we can always remove the listener.
2104 */
2105 wl_list_remove(&shsurf->surface_destroy_listener.link);
2106 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002107 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002108 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002109
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002110 wl_list_remove(&shsurf->link);
2111 free(shsurf);
2112}
2113
2114static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002115shell_destroy_shell_surface(struct wl_resource *resource)
2116{
2117 struct shell_surface *shsurf = resource->data;
2118
2119 destroy_shell_surface(shsurf);
2120}
2121
2122static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002123shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002124{
2125 struct shell_surface *shsurf = container_of(listener,
2126 struct shell_surface,
2127 surface_destroy_listener);
2128
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002129 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002130 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002131 } else {
2132 wl_signal_emit(&shsurf->resource.destroy_signal,
2133 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002134 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002135 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002136}
2137
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002138static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002139shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002140
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002141static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002142get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002143{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002144 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002145 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002146 else
2147 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002148}
2149
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002150static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002151create_shell_surface(void *shell, struct weston_surface *surface,
2152 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002153{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002154 struct shell_surface *shsurf;
2155
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002156 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002157 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002158 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002159 }
2160
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002161 shsurf = calloc(1, sizeof *shsurf);
2162 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002163 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002164 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002165 }
2166
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002167 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002168 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002169
Tiago Vignattibc052c92012-04-19 16:18:18 +03002170 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002171 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002172 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002173 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002174 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002175 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2176 shsurf->fullscreen.framerate = 0;
2177 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002178 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002179 wl_list_init(&shsurf->fullscreen.transform.link);
2180
Tiago Vignattibc052c92012-04-19 16:18:18 +03002181 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002182 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2183 wl_signal_add(&surface->surface.resource.destroy_signal,
2184 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002185
2186 /* init link so its safe to always remove it in destroy_shell_surface */
2187 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002188 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002189
Pekka Paalanen460099f2012-01-20 16:48:25 +02002190 /* empty when not in use */
2191 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002192 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002193
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002194 wl_list_init(&shsurf->workspace_transform.link);
2195
Pekka Paalanen98262232011-12-01 10:42:22 +02002196 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002197 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002198
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002199 shsurf->client = client;
2200
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002201 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002202}
2203
2204static void
2205shell_get_shell_surface(struct wl_client *client,
2206 struct wl_resource *resource,
2207 uint32_t id,
2208 struct wl_resource *surface_resource)
2209{
2210 struct weston_surface *surface = surface_resource->data;
2211 struct desktop_shell *shell = resource->data;
2212 struct shell_surface *shsurf;
2213
2214 if (get_shell_surface(surface)) {
2215 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002216 WL_DISPLAY_ERROR_INVALID_OBJECT,
2217 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002218 return;
2219 }
2220
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002221 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002222 if (!shsurf) {
2223 wl_resource_post_error(surface_resource,
2224 WL_DISPLAY_ERROR_INVALID_OBJECT,
2225 "surface->configure already set");
2226 return;
2227 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002228
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002229 shsurf->resource.destroy = shell_destroy_shell_surface;
2230 shsurf->resource.object.id = id;
2231 shsurf->resource.object.interface = &wl_shell_surface_interface;
2232 shsurf->resource.object.implementation =
2233 (void (**)(void)) &shell_surface_implementation;
2234 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002235
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002236 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002237}
2238
2239static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002240 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002241};
2242
Kristian Høgsberg07937562011-04-12 17:25:42 -04002243static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002244shell_fade(struct desktop_shell *shell, enum fade_type type);
2245
2246static int
2247screensaver_timeout(void *data)
2248{
2249 struct desktop_shell *shell = data;
2250
2251 shell_fade(shell, FADE_OUT);
2252
2253 return 1;
2254}
2255
2256static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002257handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002258{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002259 struct desktop_shell *shell =
2260 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002261
Pekka Paalanen18027e52011-12-02 16:31:49 +02002262 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002263
2264 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002265 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002266}
2267
2268static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002269launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002270{
2271 if (shell->screensaver.binding)
2272 return;
2273
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002274 if (!shell->screensaver.path) {
2275 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002276 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002277 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002278
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002279 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002280 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002281 return;
2282 }
2283
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002284 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002285 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002286 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002287 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002288}
2289
2290static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002291terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002292{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002293 if (shell->screensaver.process.pid == 0)
2294 return;
2295
2296 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002297}
2298
2299static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002300configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002301{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002302 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002303
Giulio Camuffo184df502013-02-21 11:29:21 +01002304 if (width == 0)
2305 return;
2306
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002307 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2308 if (s->output == es->output && s != es) {
2309 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002310 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002311 }
2312 }
2313
Giulio Camuffo184df502013-02-21 11:29:21 +01002314 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002315
2316 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002317 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002318 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002319 }
2320}
2321
2322static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002323background_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 -04002324{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002325 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002326
Giulio Camuffo184df502013-02-21 11:29:21 +01002327 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002328}
2329
2330static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002331desktop_shell_set_background(struct wl_client *client,
2332 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002333 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002334 struct wl_resource *surface_resource)
2335{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002336 struct desktop_shell *shell = resource->data;
2337 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002338
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002339 if (surface->configure) {
2340 wl_resource_post_error(surface_resource,
2341 WL_DISPLAY_ERROR_INVALID_OBJECT,
2342 "surface role already assigned");
2343 return;
2344 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002345
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002346 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002347 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002348 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002349 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002350 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002351 surface->output->width,
2352 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002353}
2354
2355static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002356panel_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 -04002357{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002358 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002359
Giulio Camuffo184df502013-02-21 11:29:21 +01002360 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002361}
2362
2363static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002364desktop_shell_set_panel(struct wl_client *client,
2365 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002366 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002367 struct wl_resource *surface_resource)
2368{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002369 struct desktop_shell *shell = resource->data;
2370 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002371
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002372 if (surface->configure) {
2373 wl_resource_post_error(surface_resource,
2374 WL_DISPLAY_ERROR_INVALID_OBJECT,
2375 "surface role already assigned");
2376 return;
2377 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002378
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002379 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002380 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002381 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002382 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002383 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002384 surface->output->width,
2385 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002386}
2387
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002388static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002389lock_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 -04002390{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002391 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002392
Giulio Camuffo184df502013-02-21 11:29:21 +01002393 if (width == 0)
2394 return;
2395
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002396 center_on_output(surface, get_default_output(shell->compositor));
2397
2398 if (!weston_surface_is_mapped(surface)) {
2399 wl_list_insert(&shell->lock_layer.surface_list,
2400 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002401 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002402 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002403 }
2404}
2405
2406static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002407handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002408{
Tiago Vignattibe143262012-04-16 17:31:41 +03002409 struct desktop_shell *shell =
2410 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002411
Martin Minarik6d118362012-06-07 18:01:59 +02002412 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002413 shell->lock_surface = NULL;
2414}
2415
2416static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002417desktop_shell_set_lock_surface(struct wl_client *client,
2418 struct wl_resource *resource,
2419 struct wl_resource *surface_resource)
2420{
Tiago Vignattibe143262012-04-16 17:31:41 +03002421 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002422 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002423
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002424 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002425
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002426 if (!shell->locked)
2427 return;
2428
Pekka Paalanen98262232011-12-01 10:42:22 +02002429 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002430
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002431 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2432 wl_signal_add(&surface_resource->destroy_signal,
2433 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002434
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002435 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002436 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002437}
2438
2439static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002440resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002441{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002442 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002443
Pekka Paalanen77346a62011-11-30 16:26:35 +02002444 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002445
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002446 wl_list_remove(&shell->lock_layer.link);
2447 wl_list_insert(&shell->compositor->cursor_layer.link,
2448 &shell->fullscreen_layer.link);
2449 wl_list_insert(&shell->fullscreen_layer.link,
2450 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002451 if (shell->showing_input_panels) {
2452 wl_list_insert(&shell->panel_layer.link,
2453 &shell->input_panel_layer.link);
2454 wl_list_insert(&shell->input_panel_layer.link,
2455 &ws->layer.link);
2456 } else {
2457 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2458 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002459
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002460 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002461
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002462 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002463 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002464 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002465}
2466
2467static void
2468desktop_shell_unlock(struct wl_client *client,
2469 struct wl_resource *resource)
2470{
Tiago Vignattibe143262012-04-16 17:31:41 +03002471 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002472
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002473 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002474
2475 if (shell->locked)
2476 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002477}
2478
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002479static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002480desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002481 struct wl_resource *resource,
2482 struct wl_resource *surface_resource)
2483{
2484 struct desktop_shell *shell = resource->data;
2485
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002486 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002487}
2488
Kristian Høgsberg75840622011-09-06 13:48:16 -04002489static const struct desktop_shell_interface desktop_shell_implementation = {
2490 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002491 desktop_shell_set_panel,
2492 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002493 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002494 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002495};
2496
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002497static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002498get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002499{
2500 struct shell_surface *shsurf;
2501
2502 shsurf = get_shell_surface(surface);
2503 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002504 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002505 return shsurf->type;
2506}
2507
Kristian Høgsberg75840622011-09-06 13:48:16 -04002508static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002509move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002510{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002511 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002512 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002513 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002514
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002515 if (surface == NULL)
2516 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002517
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002518 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002519 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2520 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002521 return;
2522
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002523 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002524}
2525
2526static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002527resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002528{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002529 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002530 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002531 uint32_t edges = 0;
2532 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002533 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002534
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002535 if (surface == NULL)
2536 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002537
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002538 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002539 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2540 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002541 return;
2542
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002543 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002544 wl_fixed_to_int(seat->pointer->grab_x),
2545 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002546 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002547
Pekka Paalanen60921e52012-01-25 15:55:43 +02002548 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002549 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002550 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002551 edges |= 0;
2552 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002553 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002554
Pekka Paalanen60921e52012-01-25 15:55:43 +02002555 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002556 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002557 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002558 edges |= 0;
2559 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002560 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002561
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002562 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002563}
2564
2565static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002566surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002567 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002568{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002569 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002570 struct shell_surface *shsurf;
2571 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002572 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002573
2574 if (surface == NULL)
2575 return;
2576
2577 shsurf = get_shell_surface(surface);
2578 if (!shsurf)
2579 return;
2580
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002581 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002582
Scott Moreau02709af2012-05-22 01:54:10 -06002583 if (surface->alpha > 1.0)
2584 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002585 if (surface->alpha < step)
2586 surface->alpha = step;
2587
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002588 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002589 weston_surface_damage(surface);
2590}
2591
2592static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002593do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002594 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002595{
Daniel Stone37816df2012-05-16 18:45:18 +01002596 struct weston_seat *ws = (struct weston_seat *) seat;
2597 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002598 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002599 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002600
2601 wl_list_for_each(output, &compositor->output_list, link) {
2602 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002603 wl_fixed_to_double(seat->pointer->x),
2604 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002605 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002606 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002607 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002608 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002609 increment = -output->zoom.increment;
2610 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002611 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002612 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002613 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002614 else
2615 increment = 0;
2616
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002617 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002618
Scott Moreaue6603982012-06-11 13:07:51 -06002619 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002620 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002621 else if (output->zoom.level > output->zoom.max_level)
2622 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002623 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002624 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002625 output->disable_planes++;
2626 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002627
Scott Moreaue6603982012-06-11 13:07:51 -06002628 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002629
Scott Moreau8dacaab2012-06-17 18:10:58 -06002630 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002631 }
2632 }
2633}
2634
Scott Moreauccbf29d2012-02-22 14:21:41 -07002635static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002636zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002637 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002638{
2639 do_zoom(seat, time, 0, axis, value);
2640}
2641
2642static void
2643zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2644 void *data)
2645{
2646 do_zoom(seat, time, key, 0, 0);
2647}
2648
2649static void
2650terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2651 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002652{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002653 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002654
Daniel Stone325fc2d2012-05-30 16:31:58 +01002655 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002656}
2657
2658static void
Scott Moreau447013d2012-02-18 05:05:29 -07002659rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002660 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002661{
2662 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002663 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002664 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002665 struct shell_surface *shsurf = rotate->base.shsurf;
2666 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002667 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002668
2669 if (!shsurf)
2670 return;
2671
2672 surface = shsurf->surface;
2673
2674 cx = 0.5f * surface->geometry.width;
2675 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002676
Daniel Stone37816df2012-05-16 18:45:18 +01002677 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2678 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002679 r = sqrtf(dx * dx + dy * dy);
2680
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002681 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002682 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002683
2684 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002685 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002686 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002687
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002688 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002689 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002690
2691 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002692 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002693 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002694 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002695 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002696
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002697 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002698 &shsurf->surface->geometry.transformation_list,
2699 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002700 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002701 wl_list_init(&shsurf->rotation.transform.link);
2702 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002703 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002704 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002705
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002706 /* We need to adjust the position of the surface
2707 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002708 cposx = surface->geometry.x + cx;
2709 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002710 dposx = rotate->center.x - cposx;
2711 dposy = rotate->center.y - cposy;
2712 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002713 weston_surface_set_position(surface,
2714 surface->geometry.x + dposx,
2715 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002716 }
2717
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002718 /* Repaint implies weston_surface_update_transform(), which
2719 * lazily applies the damage due to rotation update.
2720 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002721 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002722}
2723
2724static void
Scott Moreau447013d2012-02-18 05:05:29 -07002725rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002726 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002727{
2728 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002729 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002730 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002731 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002732 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002733
Daniel Stone4dbadb12012-05-30 16:31:51 +01002734 if (pointer->button_count == 0 &&
2735 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002736 if (shsurf)
2737 weston_matrix_multiply(&shsurf->rotation.rotation,
2738 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002739 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002740 free(rotate);
2741 }
2742}
2743
Scott Moreau447013d2012-02-18 05:05:29 -07002744static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002745 noop_grab_focus,
2746 rotate_grab_motion,
2747 rotate_grab_button,
2748};
2749
2750static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002751surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002752{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002753 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002754 float dx, dy;
2755 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002756
Pekka Paalanen460099f2012-01-20 16:48:25 +02002757 rotate = malloc(sizeof *rotate);
2758 if (!rotate)
2759 return;
2760
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002761 weston_surface_to_global_float(surface->surface,
2762 surface->surface->geometry.width / 2,
2763 surface->surface->geometry.height / 2,
2764 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002765
Daniel Stone37816df2012-05-16 18:45:18 +01002766 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2767 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002768 r = sqrtf(dx * dx + dy * dy);
2769 if (r > 20.0f) {
2770 struct weston_matrix inverse;
2771
2772 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002773 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002774 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002775
2776 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002777 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002778 } else {
2779 weston_matrix_init(&surface->rotation.rotation);
2780 weston_matrix_init(&rotate->rotation);
2781 }
2782
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002783 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2784 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002785}
2786
2787static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002788rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2789 void *data)
2790{
2791 struct weston_surface *base_surface =
2792 (struct weston_surface *) seat->pointer->focus;
2793 struct shell_surface *surface;
2794
2795 if (base_surface == NULL)
2796 return;
2797
2798 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002799 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2800 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002801 return;
2802
2803 surface_rotate(surface, seat);
2804}
2805
2806static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002807lower_fullscreen_layer(struct desktop_shell *shell)
2808{
2809 struct workspace *ws;
2810 struct weston_surface *surface, *prev;
2811
2812 ws = get_current_workspace(shell);
2813 wl_list_for_each_reverse_safe(surface, prev,
2814 &shell->fullscreen_layer.surface_list,
2815 layer_link)
2816 weston_surface_restack(surface, &ws->layer.surface_list);
2817}
2818
2819static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002820activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002821 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002822{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002823 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002824 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002825
Daniel Stone37816df2012-05-16 18:45:18 +01002826 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002827
Jonas Ådahl8538b222012-08-29 22:13:03 +02002828 state = ensure_focus_state(shell, seat);
2829 if (state == NULL)
2830 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002831
2832 state->keyboard_focus = es;
2833 wl_list_remove(&state->surface_destroy_listener.link);
2834 wl_signal_add(&es->surface.resource.destroy_signal,
2835 &state->surface_destroy_listener);
2836
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002837 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002838 case SHELL_SURFACE_FULLSCREEN:
2839 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002840 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002841 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002842 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002843 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002844 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002845 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002846 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002847 }
2848}
2849
Alex Wu21858432012-04-01 20:13:08 +08002850/* no-op func for checking black surface */
2851static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002852black_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 +08002853{
2854}
2855
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002856static bool
Alex Wu21858432012-04-01 20:13:08 +08002857is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2858{
2859 if (es->configure == black_surface_configure) {
2860 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002861 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002862 return true;
2863 }
2864 return false;
2865}
2866
Kristian Høgsberg75840622011-09-06 13:48:16 -04002867static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002868click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002869 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002870{
Daniel Stone37816df2012-05-16 18:45:18 +01002871 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002872 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002873 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002874 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002875
Daniel Stone37816df2012-05-16 18:45:18 +01002876 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002877 if (!focus)
2878 return;
2879
Alex Wu21858432012-04-01 20:13:08 +08002880 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002881 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002882
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002883 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2884 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002885
Daniel Stone325fc2d2012-05-30 16:31:58 +01002886 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002887 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002888}
2889
2890static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002891lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002892{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002893 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002894
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002895 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002896 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002897 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002898 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002899
2900 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002901
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002902 /* Hide all surfaces by removing the fullscreen, panel and
2903 * toplevel layers. This way nothing else can show or receive
2904 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002905
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002906 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002907 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002908 if (shell->showing_input_panels)
2909 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002910 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002911 wl_list_insert(&shell->compositor->cursor_layer.link,
2912 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002913
Pekka Paalanen77346a62011-11-30 16:26:35 +02002914 launch_screensaver(shell);
2915
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002916 /* TODO: disable bindings that should not work while locked. */
2917
2918 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002919}
2920
2921static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002922unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002923{
Pekka Paalanend81c2162011-11-16 13:47:34 +02002924 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002925 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002926 return;
2927 }
2928
2929 /* If desktop-shell client has gone away, unlock immediately. */
2930 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002931 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002932 return;
2933 }
2934
2935 if (shell->prepare_event_sent)
2936 return;
2937
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002938 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002939 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002940}
2941
2942static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002943shell_fade_done(struct weston_surface_animation *animation, void *data)
2944{
2945 struct desktop_shell *shell = data;
2946
2947 shell->fade.animation = NULL;
2948
2949 switch (shell->fade.type) {
2950 case FADE_IN:
2951 weston_surface_destroy(shell->fade.surface);
2952 shell->fade.surface = NULL;
2953 break;
2954 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002955 lock(shell);
2956 break;
2957 }
2958}
2959
2960static void
2961shell_fade(struct desktop_shell *shell, enum fade_type type)
2962{
2963 struct weston_compositor *compositor = shell->compositor;
2964 struct weston_surface *surface;
2965 float tint;
2966
2967 switch (type) {
2968 case FADE_IN:
2969 tint = 0.0;
2970 break;
2971 case FADE_OUT:
2972 tint = 1.0;
2973 break;
2974 default:
2975 weston_log("shell: invalid fade type\n");
2976 return;
2977 }
2978
2979 shell->fade.type = type;
2980
2981 if (shell->fade.surface == NULL) {
2982 surface = weston_surface_create(compositor);
2983 if (!surface)
2984 return;
2985
2986 weston_surface_configure(surface, 0, 0, 8192, 8192);
2987 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2988 surface->alpha = 1.0 - tint;
2989 wl_list_insert(&compositor->fade_layer.surface_list,
2990 &surface->layer_link);
2991 weston_surface_update_transform(surface);
2992 shell->fade.surface = surface;
2993 pixman_region32_init(&surface->input);
2994 }
2995
2996 if (shell->fade.animation)
2997 weston_fade_update(shell->fade.animation,
2998 shell->fade.surface->alpha, tint, 30.0);
2999 else
3000 shell->fade.animation =
3001 weston_fade_run(shell->fade.surface,
3002 1.0 - tint, tint, 30.0,
3003 shell_fade_done, shell);
3004}
3005
3006static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003007idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003008{
3009 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003010 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003011
3012 shell_fade(shell, FADE_OUT);
3013 /* lock() is called from shell_fade_done() */
3014}
3015
3016static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003017wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003018{
3019 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003020 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003021
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003022 unlock(shell);
3023}
3024
3025static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003026show_input_panels(struct wl_listener *listener, void *data)
3027{
3028 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003029 container_of(listener, struct desktop_shell,
3030 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003031 struct input_panel_surface *surface, *next;
3032 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003033
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003034 if (shell->showing_input_panels)
3035 return;
3036
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003037 shell->showing_input_panels = true;
3038
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003039 if (!shell->locked)
3040 wl_list_insert(&shell->panel_layer.link,
3041 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003042
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003043 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003044 &shell->input_panel.surfaces, link) {
3045 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003046 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003047 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003048 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003049 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003050 weston_surface_damage(ws);
3051 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003052 }
3053}
3054
3055static void
3056hide_input_panels(struct wl_listener *listener, void *data)
3057{
3058 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003059 container_of(listener, struct desktop_shell,
3060 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003061 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003062
Jan Arne Petersen61381972013-01-31 15:52:21 +01003063 if (!shell->showing_input_panels)
3064 return;
3065
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003066 shell->showing_input_panels = false;
3067
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003068 if (!shell->locked)
3069 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003070
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003071 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003072 &shell->input_panel_layer.surface_list, layer_link)
3073 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003074}
3075
3076static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003077center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003078{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003079 int32_t width = weston_surface_buffer_width(surface);
3080 int32_t height = weston_surface_buffer_height(surface);
3081 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003082
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003083 x = output->x + (output->width - width) / 2;
3084 y = output->y + (output->height - height) / 2;
3085
3086 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003087}
3088
3089static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003090weston_surface_set_initial_position (struct weston_surface *surface,
3091 struct desktop_shell *shell)
3092{
3093 struct weston_compositor *compositor = shell->compositor;
3094 int ix = 0, iy = 0;
3095 int range_x, range_y;
3096 int dx, dy, x, y, panel_height;
3097 struct weston_output *output, *target_output = NULL;
3098 struct weston_seat *seat;
3099
3100 /* As a heuristic place the new window on the same output as the
3101 * pointer. Falling back to the output containing 0, 0.
3102 *
3103 * TODO: Do something clever for touch too?
3104 */
3105 wl_list_for_each(seat, &compositor->seat_list, link) {
3106 if (seat->has_pointer) {
3107 ix = wl_fixed_to_int(seat->pointer.x);
3108 iy = wl_fixed_to_int(seat->pointer.y);
3109 break;
3110 }
3111 }
3112
3113 wl_list_for_each(output, &compositor->output_list, link) {
3114 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3115 target_output = output;
3116 break;
3117 }
3118 }
3119
3120 if (!target_output) {
3121 weston_surface_set_position(surface, 10 + random() % 400,
3122 10 + random() % 400);
3123 return;
3124 }
3125
3126 /* Valid range within output where the surface will still be onscreen.
3127 * If this is negative it means that the surface is bigger than
3128 * output.
3129 */
3130 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003131 range_x = target_output->width - surface->geometry.width;
3132 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003133 surface->geometry.height;
3134
Rob Bradford4cb88c72012-08-13 15:18:44 +01003135 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003136 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003137 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003138 dx = 0;
3139
3140 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003141 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003142 else
3143 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003144
3145 x = target_output->x + dx;
3146 y = target_output->y + dy;
3147
3148 weston_surface_set_position (surface, x, y);
3149}
3150
3151static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003152map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003153 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003154{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003155 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003156 struct shell_surface *shsurf = get_shell_surface(surface);
3157 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003158 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003159 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003160 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003161 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003162
Pekka Paalanen60921e52012-01-25 15:55:43 +02003163 surface->geometry.width = width;
3164 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003165 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003166
3167 /* initial positioning, see also configure() */
3168 switch (surface_type) {
3169 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003170 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003171 break;
Alex Wu4539b082012-03-01 12:57:46 +08003172 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003173 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003174 shell_map_fullscreen(shsurf);
3175 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003176 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003177 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003178 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01003179 weston_surface_set_position(surface, shsurf->output->x,
3180 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08003181 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003182 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003183 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003184 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003185 case SHELL_SURFACE_NONE:
3186 weston_surface_set_position(surface,
3187 surface->geometry.x + sx,
3188 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003189 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003190 default:
3191 ;
3192 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003193
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003194 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003195 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003196 case SHELL_SURFACE_POPUP:
3197 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003198 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003199 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3200 break;
Alex Wu4539b082012-03-01 12:57:46 +08003201 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003202 case SHELL_SURFACE_NONE:
3203 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003204 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003205 ws = get_current_workspace(shell);
3206 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003207 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003208 }
3209
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003210 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003211 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003212 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3213 surface->output = shsurf->output;
3214 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003215
Juan Zhao7bb92f02011-12-15 11:31:51 -05003216 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003217 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003218 if (shsurf->transient.flags ==
3219 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3220 break;
3221 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003222 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003223 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003224 if (!shell->locked) {
3225 wl_list_for_each(seat, &compositor->seat_list, link)
3226 activate(shell, surface, seat);
3227 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003228 break;
3229 default:
3230 break;
3231 }
3232
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003233 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003234 {
3235 switch (shell->win_animation_type) {
3236 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003237 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003238 break;
3239 case ANIMATION_ZOOM:
3240 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3241 break;
3242 default:
3243 break;
3244 }
3245 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003246}
3247
3248static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003249configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003250 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003251{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003252 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3253 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003254
Pekka Paalanen77346a62011-11-30 16:26:35 +02003255 shsurf = get_shell_surface(surface);
3256 if (shsurf)
3257 surface_type = shsurf->type;
3258
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003259 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003260
3261 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003262 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003263 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003264 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003265 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003266 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003267 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003268 surface->geometry.x = surface->output->x;
3269 surface->geometry.y = surface->output->y +
3270 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003271 break;
Alex Wu4539b082012-03-01 12:57:46 +08003272 case SHELL_SURFACE_TOPLEVEL:
3273 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003274 default:
3275 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003276 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003277
Alex Wu4539b082012-03-01 12:57:46 +08003278 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003279 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003280 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003281
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003282 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003283 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003284 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003285}
3286
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003287static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003288shell_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 +03003289{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003290 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003291 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003292
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003293 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003294
Giulio Camuffo5085a752013-03-25 21:42:45 +01003295 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3296 remove_popup_grab(shsurf);
3297 }
3298
Giulio Camuffo184df502013-02-21 11:29:21 +01003299 if (width == 0)
3300 return;
3301
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003302 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003303 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003304 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003305 type_changed = 1;
3306 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003307
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003308 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003309 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003310 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003311 es->geometry.width != width ||
3312 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003313 float from_x, from_y;
3314 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003315
3316 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3317 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3318 configure(shell, es,
3319 es->geometry.x + to_x - from_x,
3320 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003321 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003322 }
3323}
3324
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003325static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003326
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003327static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003328desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003329{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003330 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003331 struct desktop_shell *shell =
3332 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003333
3334 shell->child.process.pid = 0;
3335 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003336
3337 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3338 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003339 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003340 shell->child.deathstamp = time;
3341 shell->child.deathcount = 0;
3342 }
3343
3344 shell->child.deathcount++;
3345 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003346 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003347 return;
3348 }
3349
Martin Minarik6d118362012-06-07 18:01:59 +02003350 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003351 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003352}
3353
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003354static void
3355launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003356{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003357 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003358 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003359
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003360 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003361 &shell->child.process,
3362 shell_exe,
3363 desktop_shell_sigchld);
3364
3365 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003366 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003367}
3368
3369static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003370bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3371{
Tiago Vignattibe143262012-04-16 17:31:41 +03003372 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003373
3374 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003375 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003376}
3377
Kristian Høgsberg75840622011-09-06 13:48:16 -04003378static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003379unbind_desktop_shell(struct wl_resource *resource)
3380{
Tiago Vignattibe143262012-04-16 17:31:41 +03003381 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003382
3383 if (shell->locked)
3384 resume_desktop(shell);
3385
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003386 shell->child.desktop_shell = NULL;
3387 shell->prepare_event_sent = false;
3388 free(resource);
3389}
3390
3391static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003392bind_desktop_shell(struct wl_client *client,
3393 void *data, uint32_t version, uint32_t id)
3394{
Tiago Vignattibe143262012-04-16 17:31:41 +03003395 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003396 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003397
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003398 resource = wl_client_add_object(client, &desktop_shell_interface,
3399 &desktop_shell_implementation,
3400 id, shell);
3401
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003402 if (client == shell->child.client) {
3403 resource->destroy = unbind_desktop_shell;
3404 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003405 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003406 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003407
3408 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3409 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003410 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003411}
3412
Pekka Paalanen6e168112011-11-24 11:34:05 +02003413static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003414screensaver_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 -04003415{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003416 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003417
Giulio Camuffo184df502013-02-21 11:29:21 +01003418 if (width == 0)
3419 return;
3420
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003421 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003422 if (!shell->locked)
3423 return;
3424
3425 center_on_output(surface, surface->output);
3426
3427 if (wl_list_empty(&surface->layer_link)) {
3428 wl_list_insert(shell->lock_layer.surface_list.prev,
3429 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003430 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003431 wl_event_source_timer_update(shell->screensaver.timer,
3432 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003433 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003434 }
3435}
3436
3437static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003438screensaver_set_surface(struct wl_client *client,
3439 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003440 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003441 struct wl_resource *output_resource)
3442{
Tiago Vignattibe143262012-04-16 17:31:41 +03003443 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003444 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003445 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003446
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003447 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003448 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003449 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003450}
3451
3452static const struct screensaver_interface screensaver_implementation = {
3453 screensaver_set_surface
3454};
3455
3456static void
3457unbind_screensaver(struct wl_resource *resource)
3458{
Tiago Vignattibe143262012-04-16 17:31:41 +03003459 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003460
Pekka Paalanen77346a62011-11-30 16:26:35 +02003461 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003462 free(resource);
3463}
3464
3465static void
3466bind_screensaver(struct wl_client *client,
3467 void *data, uint32_t version, uint32_t id)
3468{
Tiago Vignattibe143262012-04-16 17:31:41 +03003469 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003470 struct wl_resource *resource;
3471
3472 resource = wl_client_add_object(client, &screensaver_interface,
3473 &screensaver_implementation,
3474 id, shell);
3475
Pekka Paalanen77346a62011-11-30 16:26:35 +02003476 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003477 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003478 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003479 return;
3480 }
3481
3482 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3483 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003484 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003485}
3486
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003487static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003488input_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 -04003489{
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003490 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003491 float x, y;
3492
Giulio Camuffo184df502013-02-21 11:29:21 +01003493 if (width == 0)
3494 return;
3495
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003496 if (!weston_surface_is_mapped(surface))
3497 return;
3498
3499 mode = surface->output->current;
3500 x = (mode->width - width) / 2;
3501 y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003502
3503 /* Don't map the input panel here, wait for
3504 * show_input_panels signal. */
3505
3506 weston_surface_configure(surface,
3507 surface->output->x + x,
3508 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003509 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003510}
3511
3512static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003513destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003514{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003515 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003516 wl_list_remove(&input_panel_surface->link);
3517
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003518 input_panel_surface->surface->configure = NULL;
3519
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003520 free(input_panel_surface);
3521}
3522
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003523static struct input_panel_surface *
3524get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003525{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003526 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003527 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003528 } else {
3529 return NULL;
3530 }
3531}
3532
3533static void
3534input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3535{
3536 struct input_panel_surface *ipsurface = container_of(listener,
3537 struct input_panel_surface,
3538 surface_destroy_listener);
3539
3540 if (ipsurface->resource.client) {
3541 wl_resource_destroy(&ipsurface->resource);
3542 } else {
3543 wl_signal_emit(&ipsurface->resource.destroy_signal,
3544 &ipsurface->resource);
3545 destroy_input_panel_surface(ipsurface);
3546 }
3547}
3548static struct input_panel_surface *
3549create_input_panel_surface(struct desktop_shell *shell,
3550 struct weston_surface *surface)
3551{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003552 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003553
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003554 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3555 if (!input_panel_surface)
3556 return NULL;
3557
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003558 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003559 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003560
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003561 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003562
3563 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003564
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003565 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3566 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3567 wl_signal_add(&surface->surface.resource.destroy_signal,
3568 &input_panel_surface->surface_destroy_listener);
3569
3570 wl_list_init(&input_panel_surface->link);
3571
3572 return input_panel_surface;
3573}
3574
3575static void
3576input_panel_surface_set_toplevel(struct wl_client *client,
3577 struct wl_resource *resource,
3578 uint32_t position)
3579{
3580 struct input_panel_surface *input_panel_surface = resource->data;
3581 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003582
3583 wl_list_insert(&shell->input_panel.surfaces,
3584 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003585}
3586
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003587static const struct input_panel_surface_interface input_panel_surface_implementation = {
3588 input_panel_surface_set_toplevel
3589};
3590
3591static void
3592destroy_input_panel_surface_resource(struct wl_resource *resource)
3593{
3594 struct input_panel_surface *ipsurf = resource->data;
3595
3596 destroy_input_panel_surface(ipsurf);
3597}
3598
3599static void
3600input_panel_get_input_panel_surface(struct wl_client *client,
3601 struct wl_resource *resource,
3602 uint32_t id,
3603 struct wl_resource *surface_resource)
3604{
3605 struct weston_surface *surface = surface_resource->data;
3606 struct desktop_shell *shell = resource->data;
3607 struct input_panel_surface *ipsurf;
3608
3609 if (get_input_panel_surface(surface)) {
3610 wl_resource_post_error(surface_resource,
3611 WL_DISPLAY_ERROR_INVALID_OBJECT,
3612 "input_panel::get_input_panel_surface already requested");
3613 return;
3614 }
3615
3616 ipsurf = create_input_panel_surface(shell, surface);
3617 if (!ipsurf) {
3618 wl_resource_post_error(surface_resource,
3619 WL_DISPLAY_ERROR_INVALID_OBJECT,
3620 "surface->configure already set");
3621 return;
3622 }
3623
3624 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3625 ipsurf->resource.object.id = id;
3626 ipsurf->resource.object.interface = &input_panel_surface_interface;
3627 ipsurf->resource.object.implementation =
3628 (void (**)(void)) &input_panel_surface_implementation;
3629 ipsurf->resource.data = ipsurf;
3630
3631 wl_client_add_resource(client, &ipsurf->resource);
3632}
3633
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003634static const struct input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003635 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003636};
3637
3638static void
3639unbind_input_panel(struct wl_resource *resource)
3640{
3641 struct desktop_shell *shell = resource->data;
3642
3643 shell->input_panel.binding = NULL;
3644 free(resource);
3645}
3646
3647static void
3648bind_input_panel(struct wl_client *client,
3649 void *data, uint32_t version, uint32_t id)
3650{
3651 struct desktop_shell *shell = data;
3652 struct wl_resource *resource;
3653
3654 resource = wl_client_add_object(client, &input_panel_interface,
3655 &input_panel_implementation,
3656 id, shell);
3657
3658 if (shell->input_panel.binding == NULL) {
3659 resource->destroy = unbind_input_panel;
3660 shell->input_panel.binding = resource;
3661 return;
3662 }
3663
3664 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3665 "interface object already bound");
3666 wl_resource_destroy(resource);
3667}
3668
Kristian Høgsberg07045392012-02-19 18:52:44 -05003669struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003670 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003671 struct weston_surface *current;
3672 struct wl_listener listener;
3673 struct wl_keyboard_grab grab;
3674};
3675
3676static void
3677switcher_next(struct switcher *switcher)
3678{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003679 struct weston_surface *surface;
3680 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003681 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003682 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003683
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003684 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003685 switch (get_shell_surface_type(surface)) {
3686 case SHELL_SURFACE_TOPLEVEL:
3687 case SHELL_SURFACE_FULLSCREEN:
3688 case SHELL_SURFACE_MAXIMIZED:
3689 if (first == NULL)
3690 first = surface;
3691 if (prev == switcher->current)
3692 next = surface;
3693 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003694 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003695 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003696 weston_surface_damage(surface);
3697 break;
3698 default:
3699 break;
3700 }
Alex Wu1659daa2012-04-01 20:13:09 +08003701
3702 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003703 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003704 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003705 weston_surface_damage(surface);
3706 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003707 }
3708
3709 if (next == NULL)
3710 next = first;
3711
Alex Wu07b26062012-03-12 16:06:01 +08003712 if (next == NULL)
3713 return;
3714
Kristian Høgsberg07045392012-02-19 18:52:44 -05003715 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003716 wl_signal_add(&next->surface.resource.destroy_signal,
3717 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003718
3719 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003720 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003721
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003722 shsurf = get_shell_surface(switcher->current);
3723 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003724 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003725}
3726
3727static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003728switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003729{
3730 struct switcher *switcher =
3731 container_of(listener, struct switcher, listener);
3732
3733 switcher_next(switcher);
3734}
3735
3736static void
Daniel Stone351eb612012-05-31 15:27:47 -04003737switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003738{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003739 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003740 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003741 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003742 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003743
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003744 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003745 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003746 weston_surface_damage(surface);
3747 }
3748
Alex Wu07b26062012-03-12 16:06:01 +08003749 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003750 activate(switcher->shell, switcher->current,
3751 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003752 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003753 wl_keyboard_end_grab(keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003754 if (weston_keyboard->input_method_resource)
3755 keyboard->grab = &weston_keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003756 free(switcher);
3757}
3758
3759static void
3760switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003761 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003762{
3763 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003764 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003765
Daniel Stonec9785ea2012-05-30 16:31:52 +01003766 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003767 switcher_next(switcher);
3768}
3769
3770static void
3771switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3772 uint32_t mods_depressed, uint32_t mods_latched,
3773 uint32_t mods_locked, uint32_t group)
3774{
3775 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003776 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003777
Daniel Stone351eb612012-05-31 15:27:47 -04003778 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3779 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003780}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003781
3782static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003783 switcher_key,
3784 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003785};
3786
3787static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003788switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3789 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003790{
Tiago Vignattibe143262012-04-16 17:31:41 +03003791 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003792 struct switcher *switcher;
3793
3794 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003795 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003796 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003797 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003798 wl_list_init(&switcher->listener.link);
3799
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003800 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003801 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003802 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3803 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003804 switcher_next(switcher);
3805}
3806
Pekka Paalanen3c647232011-12-22 13:43:43 +02003807static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003808backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3809 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003810{
3811 struct weston_compositor *compositor = data;
3812 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003813 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003814
3815 /* TODO: we're limiting to simple use cases, where we assume just
3816 * control on the primary display. We'd have to extend later if we
3817 * ever get support for setting backlights on random desktop LCD
3818 * panels though */
3819 output = get_default_output(compositor);
3820 if (!output)
3821 return;
3822
3823 if (!output->set_backlight)
3824 return;
3825
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003826 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3827 backlight_new = output->backlight_current - 25;
3828 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3829 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003830
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003831 if (backlight_new < 5)
3832 backlight_new = 5;
3833 if (backlight_new > 255)
3834 backlight_new = 255;
3835
3836 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003837 output->set_backlight(output, output->backlight_current);
3838}
3839
3840static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003841fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3842 void *data)
3843{
3844 struct desktop_shell *shell = data;
3845 struct weston_compositor *compositor = shell->compositor;
3846 compositor->fan_debug = !compositor->fan_debug;
3847 weston_compositor_damage_all(compositor);
3848}
3849
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003850struct debug_binding_grab {
3851 struct wl_keyboard_grab grab;
3852 struct weston_seat *seat;
3853 uint32_t key[2];
3854 int key_released[2];
3855};
3856
3857static void
3858debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3859 uint32_t key, uint32_t state)
3860{
3861 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3862 struct wl_resource *resource;
3863 struct wl_display *display;
3864 uint32_t serial;
3865 int send = 0, terminate = 0;
3866 int check_binding = 1;
3867 int i;
3868
3869 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3870 /* Do not run bindings on key releases */
3871 check_binding = 0;
3872
3873 for (i = 0; i < 2; i++)
3874 if (key == db->key[i])
3875 db->key_released[i] = 1;
3876
3877 if (db->key_released[0] && db->key_released[1]) {
3878 /* All key releases been swalled so end the grab */
3879 terminate = 1;
3880 } else if (key != db->key[0] && key != db->key[1]) {
3881 /* Should not swallow release of other keys */
3882 send = 1;
3883 }
3884 } else if (key == db->key[0] && !db->key_released[0]) {
3885 /* Do not check bindings for the first press of the binding
3886 * key. This allows it to be used as a debug shortcut.
3887 * We still need to swallow this event. */
3888 check_binding = 0;
3889 } else if (db->key[1]) {
3890 /* If we already ran a binding don't process another one since
3891 * we can't keep track of all the binding keys that were
3892 * pressed in order to swallow the release events. */
3893 send = 1;
3894 check_binding = 0;
3895 }
3896
3897 if (check_binding) {
3898 struct weston_compositor *ec = db->seat->compositor;
3899
3900 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3901 key, state)) {
3902 /* We ran a binding so swallow the press and keep the
3903 * grab to swallow the released too. */
3904 send = 0;
3905 terminate = 0;
3906 db->key[1] = key;
3907 } else {
3908 /* Terminate the grab since the key pressed is not a
3909 * debug binding key. */
3910 send = 1;
3911 terminate = 1;
3912 }
3913 }
3914
3915 if (send) {
3916 resource = grab->keyboard->focus_resource;
3917
3918 if (resource) {
3919 display = wl_client_get_display(resource->client);
3920 serial = wl_display_next_serial(display);
3921 wl_keyboard_send_key(resource, serial, time, key, state);
3922 }
3923 }
3924
3925 if (terminate) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003926 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003927 wl_keyboard_end_grab(grab->keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003928 if (weston_keyboard->input_method_resource)
3929 grab->keyboard->grab = &weston_keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003930 free(db);
3931 }
3932}
3933
3934static void
3935debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3936 uint32_t mods_depressed, uint32_t mods_latched,
3937 uint32_t mods_locked, uint32_t group)
3938{
3939 struct wl_resource *resource;
3940
3941 resource = grab->keyboard->focus_resource;
3942 if (!resource)
3943 return;
3944
3945 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3946 mods_latched, mods_locked, group);
3947}
3948
3949struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3950 debug_binding_key,
3951 debug_binding_modifiers
3952};
3953
3954static void
3955debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3956{
3957 struct debug_binding_grab *grab;
3958
3959 grab = calloc(1, sizeof *grab);
3960 if (!grab)
3961 return;
3962
3963 grab->seat = (struct weston_seat *) seat;
3964 grab->key[0] = key;
3965 grab->grab.interface = &debug_binding_keyboard_grab;
3966 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3967}
3968
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003969static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003970force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3971 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003972{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003973 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003974 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003975 struct desktop_shell *shell = data;
3976 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003977 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003978
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003979 focus_surface = seat->keyboard->focus;
3980 if (!focus_surface)
3981 return;
3982
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003983 wl_signal_emit(&compositor->kill_signal, focus_surface);
3984
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003985 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003986 wl_client_get_credentials(client, &pid, NULL, NULL);
3987
3988 /* Skip clients that we launched ourselves (the credentials of
3989 * the socketpair is ours) */
3990 if (pid == getpid())
3991 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003992
Daniel Stone325fc2d2012-05-30 16:31:58 +01003993 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003994}
3995
3996static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003997workspace_up_binding(struct wl_seat *seat, uint32_t time,
3998 uint32_t key, void *data)
3999{
4000 struct desktop_shell *shell = data;
4001 unsigned int new_index = shell->workspaces.current;
4002
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004003 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004004 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004005 if (new_index != 0)
4006 new_index--;
4007
4008 change_workspace(shell, new_index);
4009}
4010
4011static void
4012workspace_down_binding(struct wl_seat *seat, uint32_t time,
4013 uint32_t key, void *data)
4014{
4015 struct desktop_shell *shell = data;
4016 unsigned int new_index = shell->workspaces.current;
4017
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004018 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004019 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004020 if (new_index < shell->workspaces.num - 1)
4021 new_index++;
4022
4023 change_workspace(shell, new_index);
4024}
4025
4026static void
4027workspace_f_binding(struct wl_seat *seat, uint32_t time,
4028 uint32_t key, void *data)
4029{
4030 struct desktop_shell *shell = data;
4031 unsigned int new_index;
4032
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004033 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004034 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004035 new_index = key - KEY_F1;
4036 if (new_index >= shell->workspaces.num)
4037 new_index = shell->workspaces.num - 1;
4038
4039 change_workspace(shell, new_index);
4040}
4041
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004042static void
4043workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
4044 uint32_t key, void *data)
4045{
4046 struct desktop_shell *shell = data;
4047 unsigned int new_index = shell->workspaces.current;
4048
4049 if (shell->locked)
4050 return;
4051
4052 if (new_index != 0)
4053 new_index--;
4054
4055 take_surface_to_workspace_by_seat(shell, seat, new_index);
4056}
4057
4058static void
4059workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
4060 uint32_t key, void *data)
4061{
4062 struct desktop_shell *shell = data;
4063 unsigned int new_index = shell->workspaces.current;
4064
4065 if (shell->locked)
4066 return;
4067
4068 if (new_index < shell->workspaces.num - 1)
4069 new_index++;
4070
4071 take_surface_to_workspace_by_seat(shell, seat, new_index);
4072}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004073
4074static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004075shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004076{
Tiago Vignattibe143262012-04-16 17:31:41 +03004077 struct desktop_shell *shell =
4078 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004079 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004080
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004081 if (shell->child.client)
4082 wl_client_destroy(shell->child.client);
4083
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004084 wl_list_remove(&shell->idle_listener.link);
4085 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004086 wl_list_remove(&shell->show_input_panel_listener.link);
4087 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004088
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004089 wl_array_for_each(ws, &shell->workspaces.array)
4090 workspace_destroy(*ws);
4091 wl_array_release(&shell->workspaces.array);
4092
Pekka Paalanen3c647232011-12-22 13:43:43 +02004093 free(shell->screensaver.path);
4094 free(shell);
4095}
4096
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004097static void
4098shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4099{
4100 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004101 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004102
4103 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004104 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4105 MODIFIER_CTRL | MODIFIER_ALT,
4106 terminate_binding, ec);
4107 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4108 click_to_activate_binding,
4109 shell);
4110 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4111 MODIFIER_SUPER | MODIFIER_ALT,
4112 surface_opacity_binding, NULL);
4113 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4114 MODIFIER_SUPER, zoom_axis_binding,
4115 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004116
4117 /* configurable bindings */
4118 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004119 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4120 zoom_key_binding, NULL);
4121 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4122 zoom_key_binding, NULL);
4123 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4124 shell);
4125 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4126 resize_binding, shell);
4127 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4128 rotate_binding, NULL);
4129 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4130 shell);
4131 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4132 ec);
4133 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4134 backlight_binding, ec);
4135 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4136 ec);
4137 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4138 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004139 weston_compositor_add_key_binding(ec, KEY_K, mod,
4140 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004141 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4142 workspace_up_binding, shell);
4143 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4144 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004145 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4146 workspace_move_surface_up_binding,
4147 shell);
4148 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4149 workspace_move_surface_down_binding,
4150 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004151
4152 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4153 if (shell->workspaces.num > 1) {
4154 num_workspace_bindings = shell->workspaces.num;
4155 if (num_workspace_bindings > 6)
4156 num_workspace_bindings = 6;
4157 for (i = 0; i < num_workspace_bindings; i++)
4158 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4159 workspace_f_binding,
4160 shell);
4161 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004162
4163 /* Debug bindings */
4164 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4165 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004166 weston_compositor_add_debug_binding(ec, KEY_F,
4167 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004168}
4169
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004170WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004171module_init(struct weston_compositor *ec,
4172 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004173{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004174 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004175 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004176 struct workspace **pws;
4177 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004178 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004179
4180 shell = malloc(sizeof *shell);
4181 if (shell == NULL)
4182 return -1;
4183
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004184 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004185 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004186
4187 shell->destroy_listener.notify = shell_destroy;
4188 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004189 shell->idle_listener.notify = idle_handler;
4190 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4191 shell->wake_listener.notify = wake_handler;
4192 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004193 shell->show_input_panel_listener.notify = show_input_panels;
4194 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4195 shell->hide_input_panel_listener.notify = hide_input_panels;
4196 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004197 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004198 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004199 ec->shell_interface.create_shell_surface = create_shell_surface;
4200 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004201 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004202 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004203 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004204 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004205
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004206 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004207
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004208 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4209 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004210 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4211 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004212 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004213
4214 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004215 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004216
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004217 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004218
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004219 for (i = 0; i < shell->workspaces.num; i++) {
4220 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4221 if (pws == NULL)
4222 return -1;
4223
4224 *pws = workspace_create();
4225 if (*pws == NULL)
4226 return -1;
4227 }
4228 activate_workspace(shell, 0);
4229
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004230 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004231 wl_list_init(&shell->workspaces.animation.link);
4232 shell->workspaces.animation.frame = animate_workspace_change_frame;
4233
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004234 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4235 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004236 return -1;
4237
Kristian Høgsberg75840622011-09-06 13:48:16 -04004238 if (wl_display_add_global(ec->wl_display,
4239 &desktop_shell_interface,
4240 shell, bind_desktop_shell) == NULL)
4241 return -1;
4242
Pekka Paalanen6e168112011-11-24 11:34:05 +02004243 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4244 shell, bind_screensaver) == NULL)
4245 return -1;
4246
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004247 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4248 shell, bind_input_panel) == NULL)
4249 return -1;
4250
Jonas Ådahle9d22502012-08-29 22:13:01 +02004251 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4252 shell, bind_workspace_manager) == NULL)
4253 return -1;
4254
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004255 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004256
4257 loop = wl_display_get_event_loop(ec->wl_display);
4258 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004259
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004260 shell->screensaver.timer =
4261 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4262
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004263 wl_list_for_each(seat, &ec->seat_list, link)
4264 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004265
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004266 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004267
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004268 shell_fade(shell, FADE_IN);
4269
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004270 return 0;
4271}