blob: 9d0ae02ff4895a3b06077a7c4f9aa47d953e3543 [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;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02001989 shseat->popup_grab.grab.interface = NULL;
1990 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01001991 /* Send the popup_done event to all the popups open */
1992 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1993 wl_shell_surface_send_popup_done(&shsurf->resource);
1994 shsurf->popup.shseat = NULL;
1995 if (prev) {
1996 wl_list_init(&prev->popup.grab_link);
1997 }
1998 prev = shsurf;
1999 }
2000 wl_list_init(&prev->popup.grab_link);
2001 wl_list_init(&shseat->popup_grab.surfaces_list);
2002 }
2003}
2004
2005static void
2006add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2007{
2008 struct wl_seat *seat = &shseat->seat->seat;
2009
2010 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2011 shseat->popup_grab.client = shsurf->surface->surface.resource.client;
2012 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002013 /* We must make sure here that this popup was opened after
2014 * a mouse press, and not just by moving around with other
2015 * popups already open. */
2016 if (shseat->seat->pointer.button_count > 0)
2017 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002018
2019 wl_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
2020 }
2021 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2022}
2023
2024static void
2025remove_popup_grab(struct shell_surface *shsurf)
2026{
2027 struct shell_seat *shseat = shsurf->popup.shseat;
2028
2029 wl_list_remove(&shsurf->popup.grab_link);
2030 wl_list_init(&shsurf->popup.grab_link);
2031 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2032 wl_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002033 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002034 }
2035}
2036
2037static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002038shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002039{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002040 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002041 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002042 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002043
2044 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002045
Pekka Paalanen483243f2013-03-08 14:56:50 +02002046 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002047 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2048 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002049
Giulio Camuffo5085a752013-03-25 21:42:45 +01002050 if (shseat->seat->pointer.grab_serial == shsurf->popup.serial) {
2051 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002052 } else {
2053 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002054 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002055 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002056}
2057
2058static void
2059shell_surface_set_popup(struct wl_client *client,
2060 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002061 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002062 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002063 struct wl_resource *parent_resource,
2064 int32_t x, int32_t y, uint32_t flags)
2065{
2066 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002067
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002068 shsurf->type = SHELL_SURFACE_POPUP;
2069 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002070 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002071 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002072 shsurf->popup.x = x;
2073 shsurf->popup.y = y;
2074}
2075
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002076static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002077 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002078 shell_surface_move,
2079 shell_surface_resize,
2080 shell_surface_set_toplevel,
2081 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002082 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002083 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002084 shell_surface_set_maximized,
2085 shell_surface_set_title,
2086 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002087};
2088
2089static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002090destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002091{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002092 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2093 remove_popup_grab(shsurf);
2094 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002095
Alex Wubd3354b2012-04-17 17:20:49 +08002096 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2097 shell_surface_is_top_fullscreen(shsurf)) {
2098 weston_output_switch_mode(shsurf->fullscreen_output,
2099 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002100 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002101
Alex Wuaa08e2d2012-03-05 11:01:40 +08002102 if (shsurf->fullscreen.black_surface)
2103 weston_surface_destroy(shsurf->fullscreen.black_surface);
2104
Alex Wubd3354b2012-04-17 17:20:49 +08002105 /* As destroy_resource() use wl_list_for_each_safe(),
2106 * we can always remove the listener.
2107 */
2108 wl_list_remove(&shsurf->surface_destroy_listener.link);
2109 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002110 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002111 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002112
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002113 wl_list_remove(&shsurf->link);
2114 free(shsurf);
2115}
2116
2117static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002118shell_destroy_shell_surface(struct wl_resource *resource)
2119{
2120 struct shell_surface *shsurf = resource->data;
2121
2122 destroy_shell_surface(shsurf);
2123}
2124
2125static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002126shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002127{
2128 struct shell_surface *shsurf = container_of(listener,
2129 struct shell_surface,
2130 surface_destroy_listener);
2131
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002132 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002133 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002134 } else {
2135 wl_signal_emit(&shsurf->resource.destroy_signal,
2136 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002137 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002138 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002139}
2140
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002141static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002142shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002143
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002144static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002145get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002146{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002147 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002148 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002149 else
2150 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002151}
2152
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002153static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002154create_shell_surface(void *shell, struct weston_surface *surface,
2155 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002156{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002157 struct shell_surface *shsurf;
2158
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002159 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002160 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002161 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002162 }
2163
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002164 shsurf = calloc(1, sizeof *shsurf);
2165 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002166 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002167 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002168 }
2169
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002170 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002171 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002172
Tiago Vignattibc052c92012-04-19 16:18:18 +03002173 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002174 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002175 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002176 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002177 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002178 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2179 shsurf->fullscreen.framerate = 0;
2180 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002181 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002182 wl_list_init(&shsurf->fullscreen.transform.link);
2183
Tiago Vignattibc052c92012-04-19 16:18:18 +03002184 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002185 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2186 wl_signal_add(&surface->surface.resource.destroy_signal,
2187 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002188
2189 /* init link so its safe to always remove it in destroy_shell_surface */
2190 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002191 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002192
Pekka Paalanen460099f2012-01-20 16:48:25 +02002193 /* empty when not in use */
2194 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002195 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002196
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002197 wl_list_init(&shsurf->workspace_transform.link);
2198
Pekka Paalanen98262232011-12-01 10:42:22 +02002199 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002200 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002201
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002202 shsurf->client = client;
2203
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002204 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002205}
2206
2207static void
2208shell_get_shell_surface(struct wl_client *client,
2209 struct wl_resource *resource,
2210 uint32_t id,
2211 struct wl_resource *surface_resource)
2212{
2213 struct weston_surface *surface = surface_resource->data;
2214 struct desktop_shell *shell = resource->data;
2215 struct shell_surface *shsurf;
2216
2217 if (get_shell_surface(surface)) {
2218 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002219 WL_DISPLAY_ERROR_INVALID_OBJECT,
2220 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002221 return;
2222 }
2223
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002224 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002225 if (!shsurf) {
2226 wl_resource_post_error(surface_resource,
2227 WL_DISPLAY_ERROR_INVALID_OBJECT,
2228 "surface->configure already set");
2229 return;
2230 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002231
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002232 shsurf->resource.destroy = shell_destroy_shell_surface;
2233 shsurf->resource.object.id = id;
2234 shsurf->resource.object.interface = &wl_shell_surface_interface;
2235 shsurf->resource.object.implementation =
2236 (void (**)(void)) &shell_surface_implementation;
2237 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002238
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002239 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002240}
2241
2242static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002243 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002244};
2245
Kristian Høgsberg07937562011-04-12 17:25:42 -04002246static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002247shell_fade(struct desktop_shell *shell, enum fade_type type);
2248
2249static int
2250screensaver_timeout(void *data)
2251{
2252 struct desktop_shell *shell = data;
2253
2254 shell_fade(shell, FADE_OUT);
2255
2256 return 1;
2257}
2258
2259static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002260handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002261{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002262 struct desktop_shell *shell =
2263 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002264
Pekka Paalanen18027e52011-12-02 16:31:49 +02002265 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002266
2267 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002268 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002269}
2270
2271static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002272launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002273{
2274 if (shell->screensaver.binding)
2275 return;
2276
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002277 if (!shell->screensaver.path) {
2278 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002279 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002280 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002281
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002282 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002283 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002284 return;
2285 }
2286
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002287 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002288 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002289 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002290 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002291}
2292
2293static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002294terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002295{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002296 if (shell->screensaver.process.pid == 0)
2297 return;
2298
2299 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002300}
2301
2302static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002303configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002304{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002305 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002306
Giulio Camuffo184df502013-02-21 11:29:21 +01002307 if (width == 0)
2308 return;
2309
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002310 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2311 if (s->output == es->output && s != es) {
2312 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002313 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002314 }
2315 }
2316
Giulio Camuffo184df502013-02-21 11:29:21 +01002317 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002318
2319 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002320 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002321 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002322 }
2323}
2324
2325static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002326background_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 -04002327{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002328 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002329
Giulio Camuffo184df502013-02-21 11:29:21 +01002330 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002331}
2332
2333static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002334desktop_shell_set_background(struct wl_client *client,
2335 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002336 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002337 struct wl_resource *surface_resource)
2338{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002339 struct desktop_shell *shell = resource->data;
2340 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002341
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002342 if (surface->configure) {
2343 wl_resource_post_error(surface_resource,
2344 WL_DISPLAY_ERROR_INVALID_OBJECT,
2345 "surface role already assigned");
2346 return;
2347 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002348
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002349 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002350 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002351 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002352 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002353 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002354 surface->output->width,
2355 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002356}
2357
2358static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002359panel_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 -04002360{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002361 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002362
Giulio Camuffo184df502013-02-21 11:29:21 +01002363 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002364}
2365
2366static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002367desktop_shell_set_panel(struct wl_client *client,
2368 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002369 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002370 struct wl_resource *surface_resource)
2371{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002372 struct desktop_shell *shell = resource->data;
2373 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002374
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002375 if (surface->configure) {
2376 wl_resource_post_error(surface_resource,
2377 WL_DISPLAY_ERROR_INVALID_OBJECT,
2378 "surface role already assigned");
2379 return;
2380 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002381
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002382 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002383 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002384 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002385 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002386 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002387 surface->output->width,
2388 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002389}
2390
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002391static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002392lock_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 -04002393{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002394 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002395
Giulio Camuffo184df502013-02-21 11:29:21 +01002396 if (width == 0)
2397 return;
2398
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002399 center_on_output(surface, get_default_output(shell->compositor));
2400
2401 if (!weston_surface_is_mapped(surface)) {
2402 wl_list_insert(&shell->lock_layer.surface_list,
2403 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002404 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002405 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002406 }
2407}
2408
2409static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002410handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002411{
Tiago Vignattibe143262012-04-16 17:31:41 +03002412 struct desktop_shell *shell =
2413 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002414
Martin Minarik6d118362012-06-07 18:01:59 +02002415 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002416 shell->lock_surface = NULL;
2417}
2418
2419static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002420desktop_shell_set_lock_surface(struct wl_client *client,
2421 struct wl_resource *resource,
2422 struct wl_resource *surface_resource)
2423{
Tiago Vignattibe143262012-04-16 17:31:41 +03002424 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002425 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002426
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002427 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002428
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002429 if (!shell->locked)
2430 return;
2431
Pekka Paalanen98262232011-12-01 10:42:22 +02002432 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002433
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002434 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2435 wl_signal_add(&surface_resource->destroy_signal,
2436 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002437
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002438 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002439 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002440}
2441
2442static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002443resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002444{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002445 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002446
Pekka Paalanen77346a62011-11-30 16:26:35 +02002447 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002448
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002449 wl_list_remove(&shell->lock_layer.link);
2450 wl_list_insert(&shell->compositor->cursor_layer.link,
2451 &shell->fullscreen_layer.link);
2452 wl_list_insert(&shell->fullscreen_layer.link,
2453 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002454 if (shell->showing_input_panels) {
2455 wl_list_insert(&shell->panel_layer.link,
2456 &shell->input_panel_layer.link);
2457 wl_list_insert(&shell->input_panel_layer.link,
2458 &ws->layer.link);
2459 } else {
2460 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2461 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002462
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002463 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002464
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002465 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002466 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002467 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002468}
2469
2470static void
2471desktop_shell_unlock(struct wl_client *client,
2472 struct wl_resource *resource)
2473{
Tiago Vignattibe143262012-04-16 17:31:41 +03002474 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002475
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002476 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002477
2478 if (shell->locked)
2479 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002480}
2481
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002482static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002483desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002484 struct wl_resource *resource,
2485 struct wl_resource *surface_resource)
2486{
2487 struct desktop_shell *shell = resource->data;
2488
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002489 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002490}
2491
Kristian Høgsberg75840622011-09-06 13:48:16 -04002492static const struct desktop_shell_interface desktop_shell_implementation = {
2493 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002494 desktop_shell_set_panel,
2495 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002496 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002497 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002498};
2499
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002500static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002501get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002502{
2503 struct shell_surface *shsurf;
2504
2505 shsurf = get_shell_surface(surface);
2506 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002507 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002508 return shsurf->type;
2509}
2510
Kristian Høgsberg75840622011-09-06 13:48:16 -04002511static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002512move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002513{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002514 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002515 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002516 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002517
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002518 if (surface == NULL)
2519 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002520
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002521 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002522 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2523 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002524 return;
2525
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002526 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002527}
2528
2529static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002530resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002531{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002532 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002533 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002534 uint32_t edges = 0;
2535 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002536 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002537
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002538 if (surface == NULL)
2539 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002540
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002541 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002542 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2543 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002544 return;
2545
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002546 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002547 wl_fixed_to_int(seat->pointer->grab_x),
2548 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002549 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002550
Pekka Paalanen60921e52012-01-25 15:55:43 +02002551 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002552 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002553 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002554 edges |= 0;
2555 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002556 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002557
Pekka Paalanen60921e52012-01-25 15:55:43 +02002558 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002559 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002560 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002561 edges |= 0;
2562 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002563 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002564
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002565 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002566}
2567
2568static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002569surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002570 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002571{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002572 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002573 struct shell_surface *shsurf;
2574 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002575 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002576
2577 if (surface == NULL)
2578 return;
2579
2580 shsurf = get_shell_surface(surface);
2581 if (!shsurf)
2582 return;
2583
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002584 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002585
Scott Moreau02709af2012-05-22 01:54:10 -06002586 if (surface->alpha > 1.0)
2587 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002588 if (surface->alpha < step)
2589 surface->alpha = step;
2590
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002591 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002592 weston_surface_damage(surface);
2593}
2594
2595static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002596do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002597 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002598{
Daniel Stone37816df2012-05-16 18:45:18 +01002599 struct weston_seat *ws = (struct weston_seat *) seat;
2600 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002601 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002602 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002603
2604 wl_list_for_each(output, &compositor->output_list, link) {
2605 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002606 wl_fixed_to_double(seat->pointer->x),
2607 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002608 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002609 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002610 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002611 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002612 increment = -output->zoom.increment;
2613 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002614 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002615 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002616 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002617 else
2618 increment = 0;
2619
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002620 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002621
Scott Moreaue6603982012-06-11 13:07:51 -06002622 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002623 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002624 else if (output->zoom.level > output->zoom.max_level)
2625 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002626 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002627 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002628 output->disable_planes++;
2629 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002630
Scott Moreaue6603982012-06-11 13:07:51 -06002631 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002632
Scott Moreau8dacaab2012-06-17 18:10:58 -06002633 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002634 }
2635 }
2636}
2637
Scott Moreauccbf29d2012-02-22 14:21:41 -07002638static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002639zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002640 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002641{
2642 do_zoom(seat, time, 0, axis, value);
2643}
2644
2645static void
2646zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2647 void *data)
2648{
2649 do_zoom(seat, time, key, 0, 0);
2650}
2651
2652static void
2653terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2654 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002655{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002656 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002657
Daniel Stone325fc2d2012-05-30 16:31:58 +01002658 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002659}
2660
2661static void
Scott Moreau447013d2012-02-18 05:05:29 -07002662rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002663 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002664{
2665 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002666 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002667 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002668 struct shell_surface *shsurf = rotate->base.shsurf;
2669 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002670 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002671
2672 if (!shsurf)
2673 return;
2674
2675 surface = shsurf->surface;
2676
2677 cx = 0.5f * surface->geometry.width;
2678 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002679
Daniel Stone37816df2012-05-16 18:45:18 +01002680 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2681 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002682 r = sqrtf(dx * dx + dy * dy);
2683
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002684 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002685 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002686
2687 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002688 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002689 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002690
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002691 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002692 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002693
2694 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002695 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002696 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002697 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002698 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002699
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002700 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002701 &shsurf->surface->geometry.transformation_list,
2702 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002703 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002704 wl_list_init(&shsurf->rotation.transform.link);
2705 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002706 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002707 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002708
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002709 /* We need to adjust the position of the surface
2710 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002711 cposx = surface->geometry.x + cx;
2712 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002713 dposx = rotate->center.x - cposx;
2714 dposy = rotate->center.y - cposy;
2715 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002716 weston_surface_set_position(surface,
2717 surface->geometry.x + dposx,
2718 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002719 }
2720
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002721 /* Repaint implies weston_surface_update_transform(), which
2722 * lazily applies the damage due to rotation update.
2723 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002724 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002725}
2726
2727static void
Scott Moreau447013d2012-02-18 05:05:29 -07002728rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002729 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002730{
2731 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002732 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002733 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002734 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002735 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002736
Daniel Stone4dbadb12012-05-30 16:31:51 +01002737 if (pointer->button_count == 0 &&
2738 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002739 if (shsurf)
2740 weston_matrix_multiply(&shsurf->rotation.rotation,
2741 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002742 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002743 free(rotate);
2744 }
2745}
2746
Scott Moreau447013d2012-02-18 05:05:29 -07002747static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002748 noop_grab_focus,
2749 rotate_grab_motion,
2750 rotate_grab_button,
2751};
2752
2753static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002754surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002755{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002756 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002757 float dx, dy;
2758 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002759
Pekka Paalanen460099f2012-01-20 16:48:25 +02002760 rotate = malloc(sizeof *rotate);
2761 if (!rotate)
2762 return;
2763
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002764 weston_surface_to_global_float(surface->surface,
2765 surface->surface->geometry.width / 2,
2766 surface->surface->geometry.height / 2,
2767 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002768
Daniel Stone37816df2012-05-16 18:45:18 +01002769 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2770 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002771 r = sqrtf(dx * dx + dy * dy);
2772 if (r > 20.0f) {
2773 struct weston_matrix inverse;
2774
2775 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002776 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002777 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002778
2779 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002780 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002781 } else {
2782 weston_matrix_init(&surface->rotation.rotation);
2783 weston_matrix_init(&rotate->rotation);
2784 }
2785
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002786 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2787 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002788}
2789
2790static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002791rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2792 void *data)
2793{
2794 struct weston_surface *base_surface =
2795 (struct weston_surface *) seat->pointer->focus;
2796 struct shell_surface *surface;
2797
2798 if (base_surface == NULL)
2799 return;
2800
2801 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002802 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2803 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002804 return;
2805
2806 surface_rotate(surface, seat);
2807}
2808
2809static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002810lower_fullscreen_layer(struct desktop_shell *shell)
2811{
2812 struct workspace *ws;
2813 struct weston_surface *surface, *prev;
2814
2815 ws = get_current_workspace(shell);
2816 wl_list_for_each_reverse_safe(surface, prev,
2817 &shell->fullscreen_layer.surface_list,
2818 layer_link)
2819 weston_surface_restack(surface, &ws->layer.surface_list);
2820}
2821
2822static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002823activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002824 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002825{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002826 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002827 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002828
Daniel Stone37816df2012-05-16 18:45:18 +01002829 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002830
Jonas Ådahl8538b222012-08-29 22:13:03 +02002831 state = ensure_focus_state(shell, seat);
2832 if (state == NULL)
2833 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002834
2835 state->keyboard_focus = es;
2836 wl_list_remove(&state->surface_destroy_listener.link);
2837 wl_signal_add(&es->surface.resource.destroy_signal,
2838 &state->surface_destroy_listener);
2839
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002840 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002841 case SHELL_SURFACE_FULLSCREEN:
2842 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002843 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002844 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002845 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002846 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002847 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002848 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002849 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002850 }
2851}
2852
Alex Wu21858432012-04-01 20:13:08 +08002853/* no-op func for checking black surface */
2854static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002855black_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 +08002856{
2857}
2858
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002859static bool
Alex Wu21858432012-04-01 20:13:08 +08002860is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2861{
2862 if (es->configure == black_surface_configure) {
2863 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002864 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002865 return true;
2866 }
2867 return false;
2868}
2869
Kristian Høgsberg75840622011-09-06 13:48:16 -04002870static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002871click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002872 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002873{
Daniel Stone37816df2012-05-16 18:45:18 +01002874 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002875 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002876 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002877 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002878
Daniel Stone37816df2012-05-16 18:45:18 +01002879 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002880 if (!focus)
2881 return;
2882
Alex Wu21858432012-04-01 20:13:08 +08002883 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002884 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002885
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002886 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2887 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002888
Daniel Stone325fc2d2012-05-30 16:31:58 +01002889 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002890 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002891}
2892
2893static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002894lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002895{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002896 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002897
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002898 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002899 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002900 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002901 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002902
2903 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002904
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002905 /* Hide all surfaces by removing the fullscreen, panel and
2906 * toplevel layers. This way nothing else can show or receive
2907 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002908
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002909 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002910 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002911 if (shell->showing_input_panels)
2912 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002913 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002914 wl_list_insert(&shell->compositor->cursor_layer.link,
2915 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002916
Pekka Paalanen77346a62011-11-30 16:26:35 +02002917 launch_screensaver(shell);
2918
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002919 /* TODO: disable bindings that should not work while locked. */
2920
2921 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002922}
2923
2924static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002925unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002926{
Pekka Paalanend81c2162011-11-16 13:47:34 +02002927 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002928 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002929 return;
2930 }
2931
2932 /* If desktop-shell client has gone away, unlock immediately. */
2933 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002934 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002935 return;
2936 }
2937
2938 if (shell->prepare_event_sent)
2939 return;
2940
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002941 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002942 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002943}
2944
2945static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002946shell_fade_done(struct weston_surface_animation *animation, void *data)
2947{
2948 struct desktop_shell *shell = data;
2949
2950 shell->fade.animation = NULL;
2951
2952 switch (shell->fade.type) {
2953 case FADE_IN:
2954 weston_surface_destroy(shell->fade.surface);
2955 shell->fade.surface = NULL;
2956 break;
2957 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002958 lock(shell);
2959 break;
2960 }
2961}
2962
2963static void
2964shell_fade(struct desktop_shell *shell, enum fade_type type)
2965{
2966 struct weston_compositor *compositor = shell->compositor;
2967 struct weston_surface *surface;
2968 float tint;
2969
2970 switch (type) {
2971 case FADE_IN:
2972 tint = 0.0;
2973 break;
2974 case FADE_OUT:
2975 tint = 1.0;
2976 break;
2977 default:
2978 weston_log("shell: invalid fade type\n");
2979 return;
2980 }
2981
2982 shell->fade.type = type;
2983
2984 if (shell->fade.surface == NULL) {
2985 surface = weston_surface_create(compositor);
2986 if (!surface)
2987 return;
2988
2989 weston_surface_configure(surface, 0, 0, 8192, 8192);
2990 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2991 surface->alpha = 1.0 - tint;
2992 wl_list_insert(&compositor->fade_layer.surface_list,
2993 &surface->layer_link);
2994 weston_surface_update_transform(surface);
2995 shell->fade.surface = surface;
2996 pixman_region32_init(&surface->input);
2997 }
2998
2999 if (shell->fade.animation)
3000 weston_fade_update(shell->fade.animation,
3001 shell->fade.surface->alpha, tint, 30.0);
3002 else
3003 shell->fade.animation =
3004 weston_fade_run(shell->fade.surface,
3005 1.0 - tint, tint, 30.0,
3006 shell_fade_done, shell);
3007}
3008
3009static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003010idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003011{
3012 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003013 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003014
3015 shell_fade(shell, FADE_OUT);
3016 /* lock() is called from shell_fade_done() */
3017}
3018
3019static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003020wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003021{
3022 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003023 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003024
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003025 unlock(shell);
3026}
3027
3028static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003029show_input_panels(struct wl_listener *listener, void *data)
3030{
3031 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003032 container_of(listener, struct desktop_shell,
3033 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003034 struct input_panel_surface *surface, *next;
3035 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003036
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003037 if (shell->showing_input_panels)
3038 return;
3039
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003040 shell->showing_input_panels = true;
3041
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003042 if (!shell->locked)
3043 wl_list_insert(&shell->panel_layer.link,
3044 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003045
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003046 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003047 &shell->input_panel.surfaces, link) {
3048 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003049 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003050 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003051 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003052 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003053 weston_surface_damage(ws);
3054 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003055 }
3056}
3057
3058static void
3059hide_input_panels(struct wl_listener *listener, void *data)
3060{
3061 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003062 container_of(listener, struct desktop_shell,
3063 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003064 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003065
Jan Arne Petersen61381972013-01-31 15:52:21 +01003066 if (!shell->showing_input_panels)
3067 return;
3068
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003069 shell->showing_input_panels = false;
3070
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003071 if (!shell->locked)
3072 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003073
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003074 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003075 &shell->input_panel_layer.surface_list, layer_link)
3076 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003077}
3078
3079static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003080center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003081{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003082 int32_t width = weston_surface_buffer_width(surface);
3083 int32_t height = weston_surface_buffer_height(surface);
3084 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003085
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003086 x = output->x + (output->width - width) / 2;
3087 y = output->y + (output->height - height) / 2;
3088
3089 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003090}
3091
3092static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003093weston_surface_set_initial_position (struct weston_surface *surface,
3094 struct desktop_shell *shell)
3095{
3096 struct weston_compositor *compositor = shell->compositor;
3097 int ix = 0, iy = 0;
3098 int range_x, range_y;
3099 int dx, dy, x, y, panel_height;
3100 struct weston_output *output, *target_output = NULL;
3101 struct weston_seat *seat;
3102
3103 /* As a heuristic place the new window on the same output as the
3104 * pointer. Falling back to the output containing 0, 0.
3105 *
3106 * TODO: Do something clever for touch too?
3107 */
3108 wl_list_for_each(seat, &compositor->seat_list, link) {
3109 if (seat->has_pointer) {
3110 ix = wl_fixed_to_int(seat->pointer.x);
3111 iy = wl_fixed_to_int(seat->pointer.y);
3112 break;
3113 }
3114 }
3115
3116 wl_list_for_each(output, &compositor->output_list, link) {
3117 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3118 target_output = output;
3119 break;
3120 }
3121 }
3122
3123 if (!target_output) {
3124 weston_surface_set_position(surface, 10 + random() % 400,
3125 10 + random() % 400);
3126 return;
3127 }
3128
3129 /* Valid range within output where the surface will still be onscreen.
3130 * If this is negative it means that the surface is bigger than
3131 * output.
3132 */
3133 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003134 range_x = target_output->width - surface->geometry.width;
3135 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003136 surface->geometry.height;
3137
Rob Bradford4cb88c72012-08-13 15:18:44 +01003138 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003139 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003140 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003141 dx = 0;
3142
3143 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003144 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003145 else
3146 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003147
3148 x = target_output->x + dx;
3149 y = target_output->y + dy;
3150
3151 weston_surface_set_position (surface, x, y);
3152}
3153
3154static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003155map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003156 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003157{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003158 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003159 struct shell_surface *shsurf = get_shell_surface(surface);
3160 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003161 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003162 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003163 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003164 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003165
Pekka Paalanen60921e52012-01-25 15:55:43 +02003166 surface->geometry.width = width;
3167 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003168 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003169
3170 /* initial positioning, see also configure() */
3171 switch (surface_type) {
3172 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003173 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003174 break;
Alex Wu4539b082012-03-01 12:57:46 +08003175 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003176 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003177 shell_map_fullscreen(shsurf);
3178 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003179 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003180 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003181 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01003182 weston_surface_set_position(surface, shsurf->output->x,
3183 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08003184 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003185 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003186 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003187 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003188 case SHELL_SURFACE_NONE:
3189 weston_surface_set_position(surface,
3190 surface->geometry.x + sx,
3191 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003192 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003193 default:
3194 ;
3195 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003196
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003197 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003198 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003199 case SHELL_SURFACE_POPUP:
3200 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003201 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003202 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3203 break;
Alex Wu4539b082012-03-01 12:57:46 +08003204 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003205 case SHELL_SURFACE_NONE:
3206 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003207 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003208 ws = get_current_workspace(shell);
3209 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003210 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003211 }
3212
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003213 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003214 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003215 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3216 surface->output = shsurf->output;
3217 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003218
Juan Zhao7bb92f02011-12-15 11:31:51 -05003219 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003220 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003221 if (shsurf->transient.flags ==
3222 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3223 break;
3224 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003225 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003226 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003227 if (!shell->locked) {
3228 wl_list_for_each(seat, &compositor->seat_list, link)
3229 activate(shell, surface, seat);
3230 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003231 break;
3232 default:
3233 break;
3234 }
3235
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003236 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003237 {
3238 switch (shell->win_animation_type) {
3239 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003240 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003241 break;
3242 case ANIMATION_ZOOM:
3243 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3244 break;
3245 default:
3246 break;
3247 }
3248 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003249}
3250
3251static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003252configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003253 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003254{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003255 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3256 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003257
Pekka Paalanen77346a62011-11-30 16:26:35 +02003258 shsurf = get_shell_surface(surface);
3259 if (shsurf)
3260 surface_type = shsurf->type;
3261
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003262 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003263
3264 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003265 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003266 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003267 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003268 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003269 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003270 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003271 surface->geometry.x = surface->output->x;
3272 surface->geometry.y = surface->output->y +
3273 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003274 break;
Alex Wu4539b082012-03-01 12:57:46 +08003275 case SHELL_SURFACE_TOPLEVEL:
3276 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003277 default:
3278 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003279 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003280
Alex Wu4539b082012-03-01 12:57:46 +08003281 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003282 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003283 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003284
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003285 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003286 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003287 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003288}
3289
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003290static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003291shell_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 +03003292{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003293 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003294 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003295
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003296 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003297
Giulio Camuffo5085a752013-03-25 21:42:45 +01003298 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3299 remove_popup_grab(shsurf);
3300 }
3301
Giulio Camuffo184df502013-02-21 11:29:21 +01003302 if (width == 0)
3303 return;
3304
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003305 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003306 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003307 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003308 type_changed = 1;
3309 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003310
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003311 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003312 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003313 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003314 es->geometry.width != width ||
3315 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003316 float from_x, from_y;
3317 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003318
3319 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3320 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3321 configure(shell, es,
3322 es->geometry.x + to_x - from_x,
3323 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003324 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003325 }
3326}
3327
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003328static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003329
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003330static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003331desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003332{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003333 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003334 struct desktop_shell *shell =
3335 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003336
3337 shell->child.process.pid = 0;
3338 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003339
3340 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3341 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003342 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003343 shell->child.deathstamp = time;
3344 shell->child.deathcount = 0;
3345 }
3346
3347 shell->child.deathcount++;
3348 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003349 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003350 return;
3351 }
3352
Martin Minarik6d118362012-06-07 18:01:59 +02003353 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003354 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003355}
3356
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003357static void
3358launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003359{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003360 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003361 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003362
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003363 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003364 &shell->child.process,
3365 shell_exe,
3366 desktop_shell_sigchld);
3367
3368 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003369 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003370}
3371
3372static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003373bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3374{
Tiago Vignattibe143262012-04-16 17:31:41 +03003375 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003376
3377 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003378 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003379}
3380
Kristian Høgsberg75840622011-09-06 13:48:16 -04003381static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003382unbind_desktop_shell(struct wl_resource *resource)
3383{
Tiago Vignattibe143262012-04-16 17:31:41 +03003384 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003385
3386 if (shell->locked)
3387 resume_desktop(shell);
3388
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003389 shell->child.desktop_shell = NULL;
3390 shell->prepare_event_sent = false;
3391 free(resource);
3392}
3393
3394static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003395bind_desktop_shell(struct wl_client *client,
3396 void *data, uint32_t version, uint32_t id)
3397{
Tiago Vignattibe143262012-04-16 17:31:41 +03003398 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003399 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003400
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003401 resource = wl_client_add_object(client, &desktop_shell_interface,
3402 &desktop_shell_implementation,
3403 id, shell);
3404
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003405 if (client == shell->child.client) {
3406 resource->destroy = unbind_desktop_shell;
3407 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003408 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003409 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003410
3411 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3412 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003413 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003414}
3415
Pekka Paalanen6e168112011-11-24 11:34:05 +02003416static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003417screensaver_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 -04003418{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003419 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003420
Giulio Camuffo184df502013-02-21 11:29:21 +01003421 if (width == 0)
3422 return;
3423
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003424 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003425 if (!shell->locked)
3426 return;
3427
3428 center_on_output(surface, surface->output);
3429
3430 if (wl_list_empty(&surface->layer_link)) {
3431 wl_list_insert(shell->lock_layer.surface_list.prev,
3432 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003433 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003434 wl_event_source_timer_update(shell->screensaver.timer,
3435 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003436 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003437 }
3438}
3439
3440static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003441screensaver_set_surface(struct wl_client *client,
3442 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003443 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003444 struct wl_resource *output_resource)
3445{
Tiago Vignattibe143262012-04-16 17:31:41 +03003446 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003447 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003448 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003449
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003450 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003451 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003452 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003453}
3454
3455static const struct screensaver_interface screensaver_implementation = {
3456 screensaver_set_surface
3457};
3458
3459static void
3460unbind_screensaver(struct wl_resource *resource)
3461{
Tiago Vignattibe143262012-04-16 17:31:41 +03003462 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003463
Pekka Paalanen77346a62011-11-30 16:26:35 +02003464 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003465 free(resource);
3466}
3467
3468static void
3469bind_screensaver(struct wl_client *client,
3470 void *data, uint32_t version, uint32_t id)
3471{
Tiago Vignattibe143262012-04-16 17:31:41 +03003472 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003473 struct wl_resource *resource;
3474
3475 resource = wl_client_add_object(client, &screensaver_interface,
3476 &screensaver_implementation,
3477 id, shell);
3478
Pekka Paalanen77346a62011-11-30 16:26:35 +02003479 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003480 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003481 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003482 return;
3483 }
3484
3485 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3486 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003487 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003488}
3489
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003490static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003491input_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 -04003492{
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003493 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003494 float x, y;
3495
Giulio Camuffo184df502013-02-21 11:29:21 +01003496 if (width == 0)
3497 return;
3498
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003499 if (!weston_surface_is_mapped(surface))
3500 return;
3501
3502 mode = surface->output->current;
3503 x = (mode->width - width) / 2;
3504 y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003505
3506 /* Don't map the input panel here, wait for
3507 * show_input_panels signal. */
3508
3509 weston_surface_configure(surface,
3510 surface->output->x + x,
3511 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003512 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003513}
3514
3515static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003516destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003517{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003518 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003519 wl_list_remove(&input_panel_surface->link);
3520
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003521 input_panel_surface->surface->configure = NULL;
3522
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003523 free(input_panel_surface);
3524}
3525
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003526static struct input_panel_surface *
3527get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003528{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003529 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003530 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003531 } else {
3532 return NULL;
3533 }
3534}
3535
3536static void
3537input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3538{
3539 struct input_panel_surface *ipsurface = container_of(listener,
3540 struct input_panel_surface,
3541 surface_destroy_listener);
3542
3543 if (ipsurface->resource.client) {
3544 wl_resource_destroy(&ipsurface->resource);
3545 } else {
3546 wl_signal_emit(&ipsurface->resource.destroy_signal,
3547 &ipsurface->resource);
3548 destroy_input_panel_surface(ipsurface);
3549 }
3550}
3551static struct input_panel_surface *
3552create_input_panel_surface(struct desktop_shell *shell,
3553 struct weston_surface *surface)
3554{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003555 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003556
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003557 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3558 if (!input_panel_surface)
3559 return NULL;
3560
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003561 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003562 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003563
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003564 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003565
3566 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003567
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003568 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3569 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3570 wl_signal_add(&surface->surface.resource.destroy_signal,
3571 &input_panel_surface->surface_destroy_listener);
3572
3573 wl_list_init(&input_panel_surface->link);
3574
3575 return input_panel_surface;
3576}
3577
3578static void
3579input_panel_surface_set_toplevel(struct wl_client *client,
3580 struct wl_resource *resource,
3581 uint32_t position)
3582{
3583 struct input_panel_surface *input_panel_surface = resource->data;
3584 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003585
3586 wl_list_insert(&shell->input_panel.surfaces,
3587 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003588}
3589
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003590static const struct input_panel_surface_interface input_panel_surface_implementation = {
3591 input_panel_surface_set_toplevel
3592};
3593
3594static void
3595destroy_input_panel_surface_resource(struct wl_resource *resource)
3596{
3597 struct input_panel_surface *ipsurf = resource->data;
3598
3599 destroy_input_panel_surface(ipsurf);
3600}
3601
3602static void
3603input_panel_get_input_panel_surface(struct wl_client *client,
3604 struct wl_resource *resource,
3605 uint32_t id,
3606 struct wl_resource *surface_resource)
3607{
3608 struct weston_surface *surface = surface_resource->data;
3609 struct desktop_shell *shell = resource->data;
3610 struct input_panel_surface *ipsurf;
3611
3612 if (get_input_panel_surface(surface)) {
3613 wl_resource_post_error(surface_resource,
3614 WL_DISPLAY_ERROR_INVALID_OBJECT,
3615 "input_panel::get_input_panel_surface already requested");
3616 return;
3617 }
3618
3619 ipsurf = create_input_panel_surface(shell, surface);
3620 if (!ipsurf) {
3621 wl_resource_post_error(surface_resource,
3622 WL_DISPLAY_ERROR_INVALID_OBJECT,
3623 "surface->configure already set");
3624 return;
3625 }
3626
3627 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3628 ipsurf->resource.object.id = id;
3629 ipsurf->resource.object.interface = &input_panel_surface_interface;
3630 ipsurf->resource.object.implementation =
3631 (void (**)(void)) &input_panel_surface_implementation;
3632 ipsurf->resource.data = ipsurf;
3633
3634 wl_client_add_resource(client, &ipsurf->resource);
3635}
3636
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003637static const struct input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003638 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003639};
3640
3641static void
3642unbind_input_panel(struct wl_resource *resource)
3643{
3644 struct desktop_shell *shell = resource->data;
3645
3646 shell->input_panel.binding = NULL;
3647 free(resource);
3648}
3649
3650static void
3651bind_input_panel(struct wl_client *client,
3652 void *data, uint32_t version, uint32_t id)
3653{
3654 struct desktop_shell *shell = data;
3655 struct wl_resource *resource;
3656
3657 resource = wl_client_add_object(client, &input_panel_interface,
3658 &input_panel_implementation,
3659 id, shell);
3660
3661 if (shell->input_panel.binding == NULL) {
3662 resource->destroy = unbind_input_panel;
3663 shell->input_panel.binding = resource;
3664 return;
3665 }
3666
3667 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3668 "interface object already bound");
3669 wl_resource_destroy(resource);
3670}
3671
Kristian Høgsberg07045392012-02-19 18:52:44 -05003672struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003673 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003674 struct weston_surface *current;
3675 struct wl_listener listener;
3676 struct wl_keyboard_grab grab;
3677};
3678
3679static void
3680switcher_next(struct switcher *switcher)
3681{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003682 struct weston_surface *surface;
3683 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003684 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003685 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003686
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003687 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003688 switch (get_shell_surface_type(surface)) {
3689 case SHELL_SURFACE_TOPLEVEL:
3690 case SHELL_SURFACE_FULLSCREEN:
3691 case SHELL_SURFACE_MAXIMIZED:
3692 if (first == NULL)
3693 first = surface;
3694 if (prev == switcher->current)
3695 next = surface;
3696 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003697 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003698 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003699 weston_surface_damage(surface);
3700 break;
3701 default:
3702 break;
3703 }
Alex Wu1659daa2012-04-01 20:13:09 +08003704
3705 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003706 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003707 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003708 weston_surface_damage(surface);
3709 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003710 }
3711
3712 if (next == NULL)
3713 next = first;
3714
Alex Wu07b26062012-03-12 16:06:01 +08003715 if (next == NULL)
3716 return;
3717
Kristian Høgsberg07045392012-02-19 18:52:44 -05003718 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003719 wl_signal_add(&next->surface.resource.destroy_signal,
3720 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003721
3722 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003723 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003724
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003725 shsurf = get_shell_surface(switcher->current);
3726 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003727 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003728}
3729
3730static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003731switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003732{
3733 struct switcher *switcher =
3734 container_of(listener, struct switcher, listener);
3735
3736 switcher_next(switcher);
3737}
3738
3739static void
Daniel Stone351eb612012-05-31 15:27:47 -04003740switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003741{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003742 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003743 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003744 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003745 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003746
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003747 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003748 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003749 weston_surface_damage(surface);
3750 }
3751
Alex Wu07b26062012-03-12 16:06:01 +08003752 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003753 activate(switcher->shell, switcher->current,
3754 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003755 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003756 wl_keyboard_end_grab(keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003757 if (weston_keyboard->input_method_resource)
3758 keyboard->grab = &weston_keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003759 free(switcher);
3760}
3761
3762static void
3763switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003764 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003765{
3766 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003767 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003768
Daniel Stonec9785ea2012-05-30 16:31:52 +01003769 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003770 switcher_next(switcher);
3771}
3772
3773static void
3774switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3775 uint32_t mods_depressed, uint32_t mods_latched,
3776 uint32_t mods_locked, uint32_t group)
3777{
3778 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003779 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003780
Daniel Stone351eb612012-05-31 15:27:47 -04003781 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3782 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003783}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003784
3785static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003786 switcher_key,
3787 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003788};
3789
3790static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003791switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3792 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003793{
Tiago Vignattibe143262012-04-16 17:31:41 +03003794 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003795 struct switcher *switcher;
3796
3797 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003798 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003799 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003800 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003801 wl_list_init(&switcher->listener.link);
3802
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003803 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003804 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003805 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3806 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003807 switcher_next(switcher);
3808}
3809
Pekka Paalanen3c647232011-12-22 13:43:43 +02003810static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003811backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3812 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003813{
3814 struct weston_compositor *compositor = data;
3815 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003816 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003817
3818 /* TODO: we're limiting to simple use cases, where we assume just
3819 * control on the primary display. We'd have to extend later if we
3820 * ever get support for setting backlights on random desktop LCD
3821 * panels though */
3822 output = get_default_output(compositor);
3823 if (!output)
3824 return;
3825
3826 if (!output->set_backlight)
3827 return;
3828
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003829 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3830 backlight_new = output->backlight_current - 25;
3831 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3832 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003833
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003834 if (backlight_new < 5)
3835 backlight_new = 5;
3836 if (backlight_new > 255)
3837 backlight_new = 255;
3838
3839 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003840 output->set_backlight(output, output->backlight_current);
3841}
3842
3843static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003844fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3845 void *data)
3846{
3847 struct desktop_shell *shell = data;
3848 struct weston_compositor *compositor = shell->compositor;
3849 compositor->fan_debug = !compositor->fan_debug;
3850 weston_compositor_damage_all(compositor);
3851}
3852
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003853struct debug_binding_grab {
3854 struct wl_keyboard_grab grab;
3855 struct weston_seat *seat;
3856 uint32_t key[2];
3857 int key_released[2];
3858};
3859
3860static void
3861debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3862 uint32_t key, uint32_t state)
3863{
3864 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3865 struct wl_resource *resource;
3866 struct wl_display *display;
3867 uint32_t serial;
3868 int send = 0, terminate = 0;
3869 int check_binding = 1;
3870 int i;
3871
3872 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3873 /* Do not run bindings on key releases */
3874 check_binding = 0;
3875
3876 for (i = 0; i < 2; i++)
3877 if (key == db->key[i])
3878 db->key_released[i] = 1;
3879
3880 if (db->key_released[0] && db->key_released[1]) {
3881 /* All key releases been swalled so end the grab */
3882 terminate = 1;
3883 } else if (key != db->key[0] && key != db->key[1]) {
3884 /* Should not swallow release of other keys */
3885 send = 1;
3886 }
3887 } else if (key == db->key[0] && !db->key_released[0]) {
3888 /* Do not check bindings for the first press of the binding
3889 * key. This allows it to be used as a debug shortcut.
3890 * We still need to swallow this event. */
3891 check_binding = 0;
3892 } else if (db->key[1]) {
3893 /* If we already ran a binding don't process another one since
3894 * we can't keep track of all the binding keys that were
3895 * pressed in order to swallow the release events. */
3896 send = 1;
3897 check_binding = 0;
3898 }
3899
3900 if (check_binding) {
3901 struct weston_compositor *ec = db->seat->compositor;
3902
3903 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3904 key, state)) {
3905 /* We ran a binding so swallow the press and keep the
3906 * grab to swallow the released too. */
3907 send = 0;
3908 terminate = 0;
3909 db->key[1] = key;
3910 } else {
3911 /* Terminate the grab since the key pressed is not a
3912 * debug binding key. */
3913 send = 1;
3914 terminate = 1;
3915 }
3916 }
3917
3918 if (send) {
3919 resource = grab->keyboard->focus_resource;
3920
3921 if (resource) {
3922 display = wl_client_get_display(resource->client);
3923 serial = wl_display_next_serial(display);
3924 wl_keyboard_send_key(resource, serial, time, key, state);
3925 }
3926 }
3927
3928 if (terminate) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003929 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003930 wl_keyboard_end_grab(grab->keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003931 if (weston_keyboard->input_method_resource)
3932 grab->keyboard->grab = &weston_keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003933 free(db);
3934 }
3935}
3936
3937static void
3938debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3939 uint32_t mods_depressed, uint32_t mods_latched,
3940 uint32_t mods_locked, uint32_t group)
3941{
3942 struct wl_resource *resource;
3943
3944 resource = grab->keyboard->focus_resource;
3945 if (!resource)
3946 return;
3947
3948 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3949 mods_latched, mods_locked, group);
3950}
3951
3952struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3953 debug_binding_key,
3954 debug_binding_modifiers
3955};
3956
3957static void
3958debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3959{
3960 struct debug_binding_grab *grab;
3961
3962 grab = calloc(1, sizeof *grab);
3963 if (!grab)
3964 return;
3965
3966 grab->seat = (struct weston_seat *) seat;
3967 grab->key[0] = key;
3968 grab->grab.interface = &debug_binding_keyboard_grab;
3969 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3970}
3971
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003972static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003973force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3974 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003975{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003976 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003977 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003978 struct desktop_shell *shell = data;
3979 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003980 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003981
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003982 focus_surface = seat->keyboard->focus;
3983 if (!focus_surface)
3984 return;
3985
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003986 wl_signal_emit(&compositor->kill_signal, focus_surface);
3987
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003988 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003989 wl_client_get_credentials(client, &pid, NULL, NULL);
3990
3991 /* Skip clients that we launched ourselves (the credentials of
3992 * the socketpair is ours) */
3993 if (pid == getpid())
3994 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003995
Daniel Stone325fc2d2012-05-30 16:31:58 +01003996 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003997}
3998
3999static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004000workspace_up_binding(struct wl_seat *seat, uint32_t time,
4001 uint32_t key, void *data)
4002{
4003 struct desktop_shell *shell = data;
4004 unsigned int new_index = shell->workspaces.current;
4005
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004006 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004007 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004008 if (new_index != 0)
4009 new_index--;
4010
4011 change_workspace(shell, new_index);
4012}
4013
4014static void
4015workspace_down_binding(struct wl_seat *seat, uint32_t time,
4016 uint32_t key, void *data)
4017{
4018 struct desktop_shell *shell = data;
4019 unsigned int new_index = shell->workspaces.current;
4020
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004021 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004022 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004023 if (new_index < shell->workspaces.num - 1)
4024 new_index++;
4025
4026 change_workspace(shell, new_index);
4027}
4028
4029static void
4030workspace_f_binding(struct wl_seat *seat, uint32_t time,
4031 uint32_t key, void *data)
4032{
4033 struct desktop_shell *shell = data;
4034 unsigned int new_index;
4035
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004036 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004037 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004038 new_index = key - KEY_F1;
4039 if (new_index >= shell->workspaces.num)
4040 new_index = shell->workspaces.num - 1;
4041
4042 change_workspace(shell, new_index);
4043}
4044
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004045static void
4046workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
4047 uint32_t key, void *data)
4048{
4049 struct desktop_shell *shell = data;
4050 unsigned int new_index = shell->workspaces.current;
4051
4052 if (shell->locked)
4053 return;
4054
4055 if (new_index != 0)
4056 new_index--;
4057
4058 take_surface_to_workspace_by_seat(shell, seat, new_index);
4059}
4060
4061static void
4062workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
4063 uint32_t key, void *data)
4064{
4065 struct desktop_shell *shell = data;
4066 unsigned int new_index = shell->workspaces.current;
4067
4068 if (shell->locked)
4069 return;
4070
4071 if (new_index < shell->workspaces.num - 1)
4072 new_index++;
4073
4074 take_surface_to_workspace_by_seat(shell, seat, new_index);
4075}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004076
4077static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004078shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004079{
Tiago Vignattibe143262012-04-16 17:31:41 +03004080 struct desktop_shell *shell =
4081 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004082 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004083
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004084 if (shell->child.client)
4085 wl_client_destroy(shell->child.client);
4086
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004087 wl_list_remove(&shell->idle_listener.link);
4088 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004089 wl_list_remove(&shell->show_input_panel_listener.link);
4090 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004091
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004092 wl_array_for_each(ws, &shell->workspaces.array)
4093 workspace_destroy(*ws);
4094 wl_array_release(&shell->workspaces.array);
4095
Pekka Paalanen3c647232011-12-22 13:43:43 +02004096 free(shell->screensaver.path);
4097 free(shell);
4098}
4099
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004100static void
4101shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4102{
4103 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004104 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004105
4106 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004107 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4108 MODIFIER_CTRL | MODIFIER_ALT,
4109 terminate_binding, ec);
4110 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4111 click_to_activate_binding,
4112 shell);
4113 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4114 MODIFIER_SUPER | MODIFIER_ALT,
4115 surface_opacity_binding, NULL);
4116 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4117 MODIFIER_SUPER, zoom_axis_binding,
4118 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004119
4120 /* configurable bindings */
4121 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004122 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4123 zoom_key_binding, NULL);
4124 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4125 zoom_key_binding, NULL);
4126 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4127 shell);
4128 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4129 resize_binding, shell);
4130 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4131 rotate_binding, NULL);
4132 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4133 shell);
4134 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4135 ec);
4136 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4137 backlight_binding, ec);
4138 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4139 ec);
4140 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4141 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004142 weston_compositor_add_key_binding(ec, KEY_K, mod,
4143 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004144 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4145 workspace_up_binding, shell);
4146 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4147 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004148 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4149 workspace_move_surface_up_binding,
4150 shell);
4151 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4152 workspace_move_surface_down_binding,
4153 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004154
4155 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4156 if (shell->workspaces.num > 1) {
4157 num_workspace_bindings = shell->workspaces.num;
4158 if (num_workspace_bindings > 6)
4159 num_workspace_bindings = 6;
4160 for (i = 0; i < num_workspace_bindings; i++)
4161 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4162 workspace_f_binding,
4163 shell);
4164 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004165
4166 /* Debug bindings */
4167 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4168 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004169 weston_compositor_add_debug_binding(ec, KEY_F,
4170 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004171}
4172
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004173WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004174module_init(struct weston_compositor *ec,
4175 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004176{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004177 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004178 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004179 struct workspace **pws;
4180 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004181 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004182
4183 shell = malloc(sizeof *shell);
4184 if (shell == NULL)
4185 return -1;
4186
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004187 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004188 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004189
4190 shell->destroy_listener.notify = shell_destroy;
4191 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004192 shell->idle_listener.notify = idle_handler;
4193 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4194 shell->wake_listener.notify = wake_handler;
4195 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004196 shell->show_input_panel_listener.notify = show_input_panels;
4197 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4198 shell->hide_input_panel_listener.notify = hide_input_panels;
4199 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004200 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004201 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004202 ec->shell_interface.create_shell_surface = create_shell_surface;
4203 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004204 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004205 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004206 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004207 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004208
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004209 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004210
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004211 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4212 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004213 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4214 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004215 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004216
4217 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004218 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004219
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004220 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004221
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004222 for (i = 0; i < shell->workspaces.num; i++) {
4223 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4224 if (pws == NULL)
4225 return -1;
4226
4227 *pws = workspace_create();
4228 if (*pws == NULL)
4229 return -1;
4230 }
4231 activate_workspace(shell, 0);
4232
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004233 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004234 wl_list_init(&shell->workspaces.animation.link);
4235 shell->workspaces.animation.frame = animate_workspace_change_frame;
4236
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004237 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4238 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004239 return -1;
4240
Kristian Høgsberg75840622011-09-06 13:48:16 -04004241 if (wl_display_add_global(ec->wl_display,
4242 &desktop_shell_interface,
4243 shell, bind_desktop_shell) == NULL)
4244 return -1;
4245
Pekka Paalanen6e168112011-11-24 11:34:05 +02004246 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4247 shell, bind_screensaver) == NULL)
4248 return -1;
4249
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004250 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4251 shell, bind_input_panel) == NULL)
4252 return -1;
4253
Jonas Ådahle9d22502012-08-29 22:13:01 +02004254 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4255 shell, bind_workspace_manager) == NULL)
4256 return -1;
4257
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004258 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004259
4260 loop = wl_display_get_event_loop(ec->wl_display);
4261 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004262
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004263 shell->screensaver.timer =
4264 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4265
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004266 wl_list_for_each(seat, &ec->seat_list, link)
4267 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004268
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004269 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004270
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004271 shell_fade(shell, FADE_IN);
4272
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004273 return 0;
4274}