blob: da9193c8d2a58318cca30aec306c224b317d8fc8 [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;
1679 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001680 weston_surface_configure(surface, x, y, w, h);
1681 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001682 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001683 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001684 pixman_region32_fini(&surface->input);
1685 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001686
Alex Wu4539b082012-03-01 12:57:46 +08001687 return surface;
1688}
1689
1690/* Create black surface and append it to the associated fullscreen surface.
1691 * Handle size dismatch and positioning according to the method. */
1692static void
1693shell_configure_fullscreen(struct shell_surface *shsurf)
1694{
1695 struct weston_output *output = shsurf->fullscreen_output;
1696 struct weston_surface *surface = shsurf->surface;
1697 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001698 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001699
Alex Wu4539b082012-03-01 12:57:46 +08001700 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001701 shsurf->fullscreen.black_surface =
1702 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001703 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001704 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001705 output->width,
1706 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001707
1708 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1709 wl_list_insert(&surface->layer_link,
1710 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001711 shsurf->fullscreen.black_surface->output = output;
1712
1713 switch (shsurf->fullscreen.type) {
1714 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001715 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001716 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001717 break;
1718 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001719 /* 1:1 mapping between surface and output dimensions */
1720 if (output->width == surface->geometry.width &&
1721 output->height == surface->geometry.height) {
1722 weston_surface_set_position(surface, output->x, output->y);
1723 break;
1724 }
1725
Alex Wu4539b082012-03-01 12:57:46 +08001726 matrix = &shsurf->fullscreen.transform.matrix;
1727 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001728
Scott Moreau1bad5db2012-08-18 01:04:05 -06001729 output_aspect = (float) output->width /
1730 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001731 surface_aspect = (float) surface->geometry.width /
1732 (float) surface->geometry.height;
1733 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001734 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001735 (float) surface->geometry.width;
1736 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001737 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001738 (float) surface->geometry.height;
1739
Alex Wu4539b082012-03-01 12:57:46 +08001740 weston_matrix_scale(matrix, scale, scale, 1);
1741 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001742 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001743 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001744 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1745 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001746 weston_surface_set_position(surface, x, y);
1747
Alex Wu4539b082012-03-01 12:57:46 +08001748 break;
1749 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001750 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001751 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001752 surface->geometry.width,
1753 surface->geometry.height,
1754 shsurf->fullscreen.framerate};
1755
1756 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001757 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001758 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001759 output->width,
1760 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001761 weston_surface_set_position(surface, output->x, output->y);
1762 break;
1763 }
1764 }
Alex Wu4539b082012-03-01 12:57:46 +08001765 break;
1766 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1767 break;
1768 default:
1769 break;
1770 }
1771}
1772
1773/* make the fullscreen and black surface at the top */
1774static void
1775shell_stack_fullscreen(struct shell_surface *shsurf)
1776{
Alex Wubd3354b2012-04-17 17:20:49 +08001777 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001778 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001779 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001780
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001781 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001782 wl_list_insert(&shell->fullscreen_layer.surface_list,
1783 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001784 weston_surface_damage(surface);
1785
1786 if (!shsurf->fullscreen.black_surface)
1787 shsurf->fullscreen.black_surface =
1788 create_black_surface(surface->compositor,
1789 surface,
1790 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001791 output->width,
1792 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001793
1794 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001795 wl_list_insert(&surface->layer_link,
1796 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001797 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001798}
1799
1800static void
1801shell_map_fullscreen(struct shell_surface *shsurf)
1802{
Alex Wu4539b082012-03-01 12:57:46 +08001803 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001804 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001805}
1806
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001807static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001808set_fullscreen(struct shell_surface *shsurf,
1809 uint32_t method,
1810 uint32_t framerate,
1811 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001812{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001813 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001814
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001815 if (output)
1816 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001817 else if (es->output)
1818 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001819 else
1820 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001821
Alex Wu4539b082012-03-01 12:57:46 +08001822 shsurf->fullscreen_output = shsurf->output;
1823 shsurf->fullscreen.type = method;
1824 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001825 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001826
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001827 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001828 shsurf->output->width,
1829 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001830}
1831
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001832static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001833shell_surface_set_fullscreen(struct wl_client *client,
1834 struct wl_resource *resource,
1835 uint32_t method,
1836 uint32_t framerate,
1837 struct wl_resource *output_resource)
1838{
1839 struct shell_surface *shsurf = resource->data;
1840 struct weston_output *output;
1841
1842 if (output_resource)
1843 output = output_resource->data;
1844 else
1845 output = NULL;
1846
1847 set_fullscreen(shsurf, method, framerate, output);
1848}
1849
Giulio Camuffo5085a752013-03-25 21:42:45 +01001850static const struct wl_pointer_grab_interface popup_grab_interface;
1851
1852static void
1853destroy_shell_seat(struct wl_listener *listener, void *data)
1854{
1855 struct shell_seat *shseat =
1856 container_of(listener,
1857 struct shell_seat, seat_destroy_listener);
1858 struct shell_surface *shsurf, *prev = NULL;
1859
1860 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
1861 wl_pointer_end_grab(shseat->popup_grab.grab.pointer);
1862 shseat->popup_grab.client = NULL;
1863
1864 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1865 shsurf->popup.shseat = NULL;
1866 if (prev) {
1867 wl_list_init(&prev->popup.grab_link);
1868 }
1869 prev = shsurf;
1870 }
1871 wl_list_init(&prev->popup.grab_link);
1872 }
1873
1874 wl_list_remove(&shseat->seat_destroy_listener.link);
1875 free(shseat);
1876}
1877
1878static struct shell_seat *
1879create_shell_seat(struct weston_seat *seat)
1880{
1881 struct shell_seat *shseat;
1882
1883 shseat = calloc(1, sizeof *shseat);
1884 if (!shseat) {
1885 weston_log("no memory to allocate shell seat\n");
1886 return NULL;
1887 }
1888
1889 shseat->seat = seat;
1890 wl_list_init(&shseat->popup_grab.surfaces_list);
1891
1892 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1893 wl_signal_add(&seat->destroy_signal,
1894 &shseat->seat_destroy_listener);
1895
1896 return shseat;
1897}
1898
1899static struct shell_seat *
1900get_shell_seat(struct weston_seat *seat)
1901{
1902 struct wl_listener *listener;
1903
1904 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1905 if (listener == NULL)
1906 return create_shell_seat(seat);
1907
1908 return container_of(listener,
1909 struct shell_seat, seat_destroy_listener);
1910}
1911
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001912static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001913popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001914 struct wl_surface *surface,
1915 wl_fixed_t x,
1916 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001917{
Daniel Stone37816df2012-05-16 18:45:18 +01001918 struct wl_pointer *pointer = grab->pointer;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001919 struct shell_seat *shseat =
1920 container_of(grab, struct shell_seat, popup_grab.grab);
1921 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001922
Pekka Paalanencb108432012-01-19 16:25:40 +02001923 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001924 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001925 grab->focus = surface;
1926 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001927 wl_pointer_set_focus(pointer, NULL,
1928 wl_fixed_from_int(0),
1929 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001930 grab->focus = NULL;
1931 }
1932}
1933
1934static void
Scott Moreau447013d2012-02-18 05:05:29 -07001935popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001936 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001937{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001938 struct wl_resource *resource;
1939
Daniel Stone37816df2012-05-16 18:45:18 +01001940 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001941 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001942 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001943}
1944
1945static void
Scott Moreau447013d2012-02-18 05:05:29 -07001946popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001947 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001948{
1949 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001950 struct shell_seat *shseat =
1951 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001952 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001953 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001954 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001955
Daniel Stone37816df2012-05-16 18:45:18 +01001956 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001957 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001958 display = wl_client_get_display(resource->client);
1959 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001960 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001961 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01001962 (shseat->popup_grab.initial_up ||
1963 time - shseat->seat->pointer.grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001964 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001965 }
1966
Daniel Stone4dbadb12012-05-30 16:31:51 +01001967 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01001968 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001969}
1970
Scott Moreau447013d2012-02-18 05:05:29 -07001971static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001972 popup_grab_focus,
1973 popup_grab_motion,
1974 popup_grab_button,
1975};
1976
1977static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001978popup_grab_end(struct wl_pointer *pointer)
1979{
1980 struct wl_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001981 struct shell_seat *shseat =
1982 container_of(grab, struct shell_seat, popup_grab.grab);
1983 struct shell_surface *shsurf;
1984 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001985
1986 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001987 wl_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001988 shseat->popup_grab.client = NULL;
1989 /* Send the popup_done event to all the popups open */
1990 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1991 wl_shell_surface_send_popup_done(&shsurf->resource);
1992 shsurf->popup.shseat = NULL;
1993 if (prev) {
1994 wl_list_init(&prev->popup.grab_link);
1995 }
1996 prev = shsurf;
1997 }
1998 wl_list_init(&prev->popup.grab_link);
1999 wl_list_init(&shseat->popup_grab.surfaces_list);
2000 }
2001}
2002
2003static void
2004add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2005{
2006 struct wl_seat *seat = &shseat->seat->seat;
2007
2008 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2009 shseat->popup_grab.client = shsurf->surface->surface.resource.client;
2010 shseat->popup_grab.grab.interface = &popup_grab_interface;
2011 shseat->popup_grab.initial_up = 0;
2012
2013 wl_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
2014 }
2015 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2016}
2017
2018static void
2019remove_popup_grab(struct shell_surface *shsurf)
2020{
2021 struct shell_seat *shseat = shsurf->popup.shseat;
2022
2023 wl_list_remove(&shsurf->popup.grab_link);
2024 wl_list_init(&shsurf->popup.grab_link);
2025 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2026 wl_pointer_end_grab(shseat->popup_grab.grab.pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002027 }
2028}
2029
2030static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002031shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002032{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002033 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002034 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002035 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002036
2037 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002038
Pekka Paalanen483243f2013-03-08 14:56:50 +02002039 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002040 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2041 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002042
Giulio Camuffo5085a752013-03-25 21:42:45 +01002043 if (shseat->seat->pointer.grab_serial == shsurf->popup.serial) {
2044 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002045 } else {
2046 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002047 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002048 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002049}
2050
2051static void
2052shell_surface_set_popup(struct wl_client *client,
2053 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002054 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002055 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002056 struct wl_resource *parent_resource,
2057 int32_t x, int32_t y, uint32_t flags)
2058{
2059 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002060
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002061 shsurf->type = SHELL_SURFACE_POPUP;
2062 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002063 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002064 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002065 shsurf->popup.x = x;
2066 shsurf->popup.y = y;
2067}
2068
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002069static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002070 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002071 shell_surface_move,
2072 shell_surface_resize,
2073 shell_surface_set_toplevel,
2074 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002075 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002076 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002077 shell_surface_set_maximized,
2078 shell_surface_set_title,
2079 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002080};
2081
2082static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002083destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002084{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002085 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2086 remove_popup_grab(shsurf);
2087 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002088
Alex Wubd3354b2012-04-17 17:20:49 +08002089 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2090 shell_surface_is_top_fullscreen(shsurf)) {
2091 weston_output_switch_mode(shsurf->fullscreen_output,
2092 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002093 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002094
Alex Wuaa08e2d2012-03-05 11:01:40 +08002095 if (shsurf->fullscreen.black_surface)
2096 weston_surface_destroy(shsurf->fullscreen.black_surface);
2097
Alex Wubd3354b2012-04-17 17:20:49 +08002098 /* As destroy_resource() use wl_list_for_each_safe(),
2099 * we can always remove the listener.
2100 */
2101 wl_list_remove(&shsurf->surface_destroy_listener.link);
2102 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002103 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002104 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002105
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002106 wl_list_remove(&shsurf->link);
2107 free(shsurf);
2108}
2109
2110static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002111shell_destroy_shell_surface(struct wl_resource *resource)
2112{
2113 struct shell_surface *shsurf = resource->data;
2114
2115 destroy_shell_surface(shsurf);
2116}
2117
2118static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002119shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002120{
2121 struct shell_surface *shsurf = container_of(listener,
2122 struct shell_surface,
2123 surface_destroy_listener);
2124
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002125 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002126 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002127 } else {
2128 wl_signal_emit(&shsurf->resource.destroy_signal,
2129 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002130 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002131 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002132}
2133
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002134static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002135shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002136
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002137static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002138get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002139{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002140 if (surface->configure == shell_surface_configure)
2141 return surface->private;
2142 else
2143 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002144}
2145
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002146static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002147create_shell_surface(void *shell, struct weston_surface *surface,
2148 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002149{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002150 struct shell_surface *shsurf;
2151
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002152 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002153 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002154 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002155 }
2156
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002157 shsurf = calloc(1, sizeof *shsurf);
2158 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002159 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002160 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002161 }
2162
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002163 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002164 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002165
Tiago Vignattibc052c92012-04-19 16:18:18 +03002166 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002167 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002168 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002169 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002170 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002171 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2172 shsurf->fullscreen.framerate = 0;
2173 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002174 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002175 wl_list_init(&shsurf->fullscreen.transform.link);
2176
Tiago Vignattibc052c92012-04-19 16:18:18 +03002177 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002178 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2179 wl_signal_add(&surface->surface.resource.destroy_signal,
2180 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002181
2182 /* init link so its safe to always remove it in destroy_shell_surface */
2183 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002184 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002185
Pekka Paalanen460099f2012-01-20 16:48:25 +02002186 /* empty when not in use */
2187 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002188 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002189
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002190 wl_list_init(&shsurf->workspace_transform.link);
2191
Pekka Paalanen98262232011-12-01 10:42:22 +02002192 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002193 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002194
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002195 shsurf->client = client;
2196
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002197 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002198}
2199
2200static void
2201shell_get_shell_surface(struct wl_client *client,
2202 struct wl_resource *resource,
2203 uint32_t id,
2204 struct wl_resource *surface_resource)
2205{
2206 struct weston_surface *surface = surface_resource->data;
2207 struct desktop_shell *shell = resource->data;
2208 struct shell_surface *shsurf;
2209
2210 if (get_shell_surface(surface)) {
2211 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002212 WL_DISPLAY_ERROR_INVALID_OBJECT,
2213 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002214 return;
2215 }
2216
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002217 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002218 if (!shsurf) {
2219 wl_resource_post_error(surface_resource,
2220 WL_DISPLAY_ERROR_INVALID_OBJECT,
2221 "surface->configure already set");
2222 return;
2223 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002224
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002225 shsurf->resource.destroy = shell_destroy_shell_surface;
2226 shsurf->resource.object.id = id;
2227 shsurf->resource.object.interface = &wl_shell_surface_interface;
2228 shsurf->resource.object.implementation =
2229 (void (**)(void)) &shell_surface_implementation;
2230 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002231
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002232 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002233}
2234
2235static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002236 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002237};
2238
Kristian Høgsberg07937562011-04-12 17:25:42 -04002239static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002240shell_fade(struct desktop_shell *shell, enum fade_type type);
2241
2242static int
2243screensaver_timeout(void *data)
2244{
2245 struct desktop_shell *shell = data;
2246
2247 shell_fade(shell, FADE_OUT);
2248
2249 return 1;
2250}
2251
2252static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002253handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002254{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002255 struct desktop_shell *shell =
2256 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002257
Pekka Paalanen18027e52011-12-02 16:31:49 +02002258 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002259
2260 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002261 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002262}
2263
2264static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002265launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002266{
2267 if (shell->screensaver.binding)
2268 return;
2269
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002270 if (!shell->screensaver.path) {
2271 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002272 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002273 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002274
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002275 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002276 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002277 return;
2278 }
2279
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002280 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002281 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002282 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002283 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002284}
2285
2286static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002287terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002288{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002289 if (shell->screensaver.process.pid == 0)
2290 return;
2291
2292 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002293}
2294
2295static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002296configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002297{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002298 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002299
Giulio Camuffo184df502013-02-21 11:29:21 +01002300 if (width == 0)
2301 return;
2302
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002303 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2304 if (s->output == es->output && s != es) {
2305 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002306 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002307 }
2308 }
2309
Giulio Camuffo184df502013-02-21 11:29:21 +01002310 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002311
2312 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002313 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002314 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002315 }
2316}
2317
2318static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002319background_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 -04002320{
2321 struct desktop_shell *shell = es->private;
2322
Giulio Camuffo184df502013-02-21 11:29:21 +01002323 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002324}
2325
2326static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002327desktop_shell_set_background(struct wl_client *client,
2328 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002329 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002330 struct wl_resource *surface_resource)
2331{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002332 struct desktop_shell *shell = resource->data;
2333 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002334
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002335 if (surface->configure) {
2336 wl_resource_post_error(surface_resource,
2337 WL_DISPLAY_ERROR_INVALID_OBJECT,
2338 "surface role already assigned");
2339 return;
2340 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002341
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002342 surface->configure = background_configure;
2343 surface->private = shell;
2344 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002345 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002346 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002347 surface->output->width,
2348 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002349}
2350
2351static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002352panel_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 -04002353{
2354 struct desktop_shell *shell = es->private;
2355
Giulio Camuffo184df502013-02-21 11:29:21 +01002356 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002357}
2358
2359static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002360desktop_shell_set_panel(struct wl_client *client,
2361 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002362 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002363 struct wl_resource *surface_resource)
2364{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002365 struct desktop_shell *shell = resource->data;
2366 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002367
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002368 if (surface->configure) {
2369 wl_resource_post_error(surface_resource,
2370 WL_DISPLAY_ERROR_INVALID_OBJECT,
2371 "surface role already assigned");
2372 return;
2373 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002374
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002375 surface->configure = panel_configure;
2376 surface->private = shell;
2377 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002378 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002379 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002380 surface->output->width,
2381 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002382}
2383
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002384static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002385lock_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 -04002386{
2387 struct desktop_shell *shell = surface->private;
2388
Giulio Camuffo184df502013-02-21 11:29:21 +01002389 if (width == 0)
2390 return;
2391
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002392 center_on_output(surface, get_default_output(shell->compositor));
2393
2394 if (!weston_surface_is_mapped(surface)) {
2395 wl_list_insert(&shell->lock_layer.surface_list,
2396 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002397 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002398 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002399 }
2400}
2401
2402static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002403handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002404{
Tiago Vignattibe143262012-04-16 17:31:41 +03002405 struct desktop_shell *shell =
2406 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002407
Martin Minarik6d118362012-06-07 18:01:59 +02002408 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002409 shell->lock_surface = NULL;
2410}
2411
2412static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002413desktop_shell_set_lock_surface(struct wl_client *client,
2414 struct wl_resource *resource,
2415 struct wl_resource *surface_resource)
2416{
Tiago Vignattibe143262012-04-16 17:31:41 +03002417 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002418 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002419
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002420 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002421
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002422 if (!shell->locked)
2423 return;
2424
Pekka Paalanen98262232011-12-01 10:42:22 +02002425 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002426
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002427 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2428 wl_signal_add(&surface_resource->destroy_signal,
2429 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002430
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002431 surface->configure = lock_surface_configure;
2432 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002433}
2434
2435static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002436resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002437{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002438 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002439
Pekka Paalanen77346a62011-11-30 16:26:35 +02002440 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002441
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002442 wl_list_remove(&shell->lock_layer.link);
2443 wl_list_insert(&shell->compositor->cursor_layer.link,
2444 &shell->fullscreen_layer.link);
2445 wl_list_insert(&shell->fullscreen_layer.link,
2446 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002447 if (shell->showing_input_panels) {
2448 wl_list_insert(&shell->panel_layer.link,
2449 &shell->input_panel_layer.link);
2450 wl_list_insert(&shell->input_panel_layer.link,
2451 &ws->layer.link);
2452 } else {
2453 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2454 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002455
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002456 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002457
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002458 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002459 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002460 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002461}
2462
2463static void
2464desktop_shell_unlock(struct wl_client *client,
2465 struct wl_resource *resource)
2466{
Tiago Vignattibe143262012-04-16 17:31:41 +03002467 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002468
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002469 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002470
2471 if (shell->locked)
2472 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002473}
2474
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002475static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002476desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002477 struct wl_resource *resource,
2478 struct wl_resource *surface_resource)
2479{
2480 struct desktop_shell *shell = resource->data;
2481
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002482 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002483}
2484
Kristian Høgsberg75840622011-09-06 13:48:16 -04002485static const struct desktop_shell_interface desktop_shell_implementation = {
2486 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002487 desktop_shell_set_panel,
2488 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002489 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002490 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002491};
2492
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002493static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002494get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002495{
2496 struct shell_surface *shsurf;
2497
2498 shsurf = get_shell_surface(surface);
2499 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002500 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002501 return shsurf->type;
2502}
2503
Kristian Høgsberg75840622011-09-06 13:48:16 -04002504static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002505move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002506{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002507 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002508 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002509 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002510
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002511 if (surface == NULL)
2512 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002513
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002514 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002515 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2516 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002517 return;
2518
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002519 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002520}
2521
2522static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002523resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002524{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002525 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002526 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002527 uint32_t edges = 0;
2528 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002529 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002530
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002531 if (surface == NULL)
2532 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002533
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002534 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002535 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2536 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002537 return;
2538
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002539 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002540 wl_fixed_to_int(seat->pointer->grab_x),
2541 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002542 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002543
Pekka Paalanen60921e52012-01-25 15:55:43 +02002544 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002545 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002546 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002547 edges |= 0;
2548 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002549 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002550
Pekka Paalanen60921e52012-01-25 15:55:43 +02002551 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002552 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002553 else if (y < 2 * surface->geometry.height / 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_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002557
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002558 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002559}
2560
2561static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002562surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002563 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002564{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002565 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002566 struct shell_surface *shsurf;
2567 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002568 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002569
2570 if (surface == NULL)
2571 return;
2572
2573 shsurf = get_shell_surface(surface);
2574 if (!shsurf)
2575 return;
2576
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002577 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002578
Scott Moreau02709af2012-05-22 01:54:10 -06002579 if (surface->alpha > 1.0)
2580 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002581 if (surface->alpha < step)
2582 surface->alpha = step;
2583
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002584 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002585 weston_surface_damage(surface);
2586}
2587
2588static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002589do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002590 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002591{
Daniel Stone37816df2012-05-16 18:45:18 +01002592 struct weston_seat *ws = (struct weston_seat *) seat;
2593 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002594 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002595 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002596
2597 wl_list_for_each(output, &compositor->output_list, link) {
2598 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002599 wl_fixed_to_double(seat->pointer->x),
2600 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002601 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002602 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002603 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002604 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002605 increment = -output->zoom.increment;
2606 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002607 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002608 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002609 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002610 else
2611 increment = 0;
2612
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002613 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002614
Scott Moreaue6603982012-06-11 13:07:51 -06002615 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002616 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002617 else if (output->zoom.level > output->zoom.max_level)
2618 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002619 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002620 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002621 output->disable_planes++;
2622 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002623
Scott Moreaue6603982012-06-11 13:07:51 -06002624 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002625
Scott Moreau8dacaab2012-06-17 18:10:58 -06002626 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002627 }
2628 }
2629}
2630
Scott Moreauccbf29d2012-02-22 14:21:41 -07002631static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002632zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002633 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002634{
2635 do_zoom(seat, time, 0, axis, value);
2636}
2637
2638static void
2639zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2640 void *data)
2641{
2642 do_zoom(seat, time, key, 0, 0);
2643}
2644
2645static void
2646terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2647 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002648{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002649 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002650
Daniel Stone325fc2d2012-05-30 16:31:58 +01002651 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002652}
2653
2654static void
Scott Moreau447013d2012-02-18 05:05:29 -07002655rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002656 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002657{
2658 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002659 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002660 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002661 struct shell_surface *shsurf = rotate->base.shsurf;
2662 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002663 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002664
2665 if (!shsurf)
2666 return;
2667
2668 surface = shsurf->surface;
2669
2670 cx = 0.5f * surface->geometry.width;
2671 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002672
Daniel Stone37816df2012-05-16 18:45:18 +01002673 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2674 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002675 r = sqrtf(dx * dx + dy * dy);
2676
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002677 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002678 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002679
2680 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002681 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002682 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002683
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002684 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002685 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002686
2687 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002688 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002689 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002690 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002691 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002692
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002693 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002694 &shsurf->surface->geometry.transformation_list,
2695 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002696 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002697 wl_list_init(&shsurf->rotation.transform.link);
2698 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002699 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002700 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002701
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002702 /* We need to adjust the position of the surface
2703 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002704 cposx = surface->geometry.x + cx;
2705 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002706 dposx = rotate->center.x - cposx;
2707 dposy = rotate->center.y - cposy;
2708 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002709 weston_surface_set_position(surface,
2710 surface->geometry.x + dposx,
2711 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002712 }
2713
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002714 /* Repaint implies weston_surface_update_transform(), which
2715 * lazily applies the damage due to rotation update.
2716 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002717 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002718}
2719
2720static void
Scott Moreau447013d2012-02-18 05:05:29 -07002721rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002722 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002723{
2724 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002725 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002726 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002727 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002728 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002729
Daniel Stone4dbadb12012-05-30 16:31:51 +01002730 if (pointer->button_count == 0 &&
2731 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002732 if (shsurf)
2733 weston_matrix_multiply(&shsurf->rotation.rotation,
2734 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002735 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002736 free(rotate);
2737 }
2738}
2739
Scott Moreau447013d2012-02-18 05:05:29 -07002740static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002741 noop_grab_focus,
2742 rotate_grab_motion,
2743 rotate_grab_button,
2744};
2745
2746static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002747surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002748{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002749 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002750 float dx, dy;
2751 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002752
Pekka Paalanen460099f2012-01-20 16:48:25 +02002753 rotate = malloc(sizeof *rotate);
2754 if (!rotate)
2755 return;
2756
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002757 weston_surface_to_global_float(surface->surface,
2758 surface->surface->geometry.width / 2,
2759 surface->surface->geometry.height / 2,
2760 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002761
Daniel Stone37816df2012-05-16 18:45:18 +01002762 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2763 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002764 r = sqrtf(dx * dx + dy * dy);
2765 if (r > 20.0f) {
2766 struct weston_matrix inverse;
2767
2768 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002769 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002770 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002771
2772 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002773 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002774 } else {
2775 weston_matrix_init(&surface->rotation.rotation);
2776 weston_matrix_init(&rotate->rotation);
2777 }
2778
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002779 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2780 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002781}
2782
2783static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002784rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2785 void *data)
2786{
2787 struct weston_surface *base_surface =
2788 (struct weston_surface *) seat->pointer->focus;
2789 struct shell_surface *surface;
2790
2791 if (base_surface == NULL)
2792 return;
2793
2794 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002795 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2796 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002797 return;
2798
2799 surface_rotate(surface, seat);
2800}
2801
2802static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002803lower_fullscreen_layer(struct desktop_shell *shell)
2804{
2805 struct workspace *ws;
2806 struct weston_surface *surface, *prev;
2807
2808 ws = get_current_workspace(shell);
2809 wl_list_for_each_reverse_safe(surface, prev,
2810 &shell->fullscreen_layer.surface_list,
2811 layer_link)
2812 weston_surface_restack(surface, &ws->layer.surface_list);
2813}
2814
2815static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002816activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002817 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002818{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002819 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002820 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002821
Daniel Stone37816df2012-05-16 18:45:18 +01002822 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002823
Jonas Ådahl8538b222012-08-29 22:13:03 +02002824 state = ensure_focus_state(shell, seat);
2825 if (state == NULL)
2826 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002827
2828 state->keyboard_focus = es;
2829 wl_list_remove(&state->surface_destroy_listener.link);
2830 wl_signal_add(&es->surface.resource.destroy_signal,
2831 &state->surface_destroy_listener);
2832
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002833 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002834 case SHELL_SURFACE_FULLSCREEN:
2835 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002836 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002837 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002838 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002839 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002840 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002841 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002842 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002843 }
2844}
2845
Alex Wu21858432012-04-01 20:13:08 +08002846/* no-op func for checking black surface */
2847static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002848black_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 +08002849{
2850}
2851
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002852static bool
Alex Wu21858432012-04-01 20:13:08 +08002853is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2854{
2855 if (es->configure == black_surface_configure) {
2856 if (fs_surface)
2857 *fs_surface = (struct weston_surface *)es->private;
2858 return true;
2859 }
2860 return false;
2861}
2862
Kristian Høgsberg75840622011-09-06 13:48:16 -04002863static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002864click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002865 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002866{
Daniel Stone37816df2012-05-16 18:45:18 +01002867 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002868 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002869 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002870 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002871
Daniel Stone37816df2012-05-16 18:45:18 +01002872 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002873 if (!focus)
2874 return;
2875
Alex Wu21858432012-04-01 20:13:08 +08002876 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002877 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002878
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002879 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2880 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002881
Daniel Stone325fc2d2012-05-30 16:31:58 +01002882 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002883 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002884}
2885
2886static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002887lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002888{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002889 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002890
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002891 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002892 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002893 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002894 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002895
2896 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002897
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002898 /* Hide all surfaces by removing the fullscreen, panel and
2899 * toplevel layers. This way nothing else can show or receive
2900 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002901
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002902 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002903 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002904 if (shell->showing_input_panels)
2905 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002906 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002907 wl_list_insert(&shell->compositor->cursor_layer.link,
2908 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002909
Pekka Paalanen77346a62011-11-30 16:26:35 +02002910 launch_screensaver(shell);
2911
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002912 /* TODO: disable bindings that should not work while locked. */
2913
2914 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002915}
2916
2917static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002918unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002919{
Pekka Paalanend81c2162011-11-16 13:47:34 +02002920 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002921 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002922 return;
2923 }
2924
2925 /* If desktop-shell client has gone away, unlock immediately. */
2926 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002927 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002928 return;
2929 }
2930
2931 if (shell->prepare_event_sent)
2932 return;
2933
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002934 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002935 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002936}
2937
2938static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002939shell_fade_done(struct weston_surface_animation *animation, void *data)
2940{
2941 struct desktop_shell *shell = data;
2942
2943 shell->fade.animation = NULL;
2944
2945 switch (shell->fade.type) {
2946 case FADE_IN:
2947 weston_surface_destroy(shell->fade.surface);
2948 shell->fade.surface = NULL;
2949 break;
2950 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002951 lock(shell);
2952 break;
2953 }
2954}
2955
2956static void
2957shell_fade(struct desktop_shell *shell, enum fade_type type)
2958{
2959 struct weston_compositor *compositor = shell->compositor;
2960 struct weston_surface *surface;
2961 float tint;
2962
2963 switch (type) {
2964 case FADE_IN:
2965 tint = 0.0;
2966 break;
2967 case FADE_OUT:
2968 tint = 1.0;
2969 break;
2970 default:
2971 weston_log("shell: invalid fade type\n");
2972 return;
2973 }
2974
2975 shell->fade.type = type;
2976
2977 if (shell->fade.surface == NULL) {
2978 surface = weston_surface_create(compositor);
2979 if (!surface)
2980 return;
2981
2982 weston_surface_configure(surface, 0, 0, 8192, 8192);
2983 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2984 surface->alpha = 1.0 - tint;
2985 wl_list_insert(&compositor->fade_layer.surface_list,
2986 &surface->layer_link);
2987 weston_surface_update_transform(surface);
2988 shell->fade.surface = surface;
2989 pixman_region32_init(&surface->input);
2990 }
2991
2992 if (shell->fade.animation)
2993 weston_fade_update(shell->fade.animation,
2994 shell->fade.surface->alpha, tint, 30.0);
2995 else
2996 shell->fade.animation =
2997 weston_fade_run(shell->fade.surface,
2998 1.0 - tint, tint, 30.0,
2999 shell_fade_done, shell);
3000}
3001
3002static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003003idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003004{
3005 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003006 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003007
3008 shell_fade(shell, FADE_OUT);
3009 /* lock() is called from shell_fade_done() */
3010}
3011
3012static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003013wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003014{
3015 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003016 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003017
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003018 unlock(shell);
3019}
3020
3021static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003022show_input_panels(struct wl_listener *listener, void *data)
3023{
3024 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003025 container_of(listener, struct desktop_shell,
3026 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003027 struct input_panel_surface *surface, *next;
3028 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003029
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003030 if (shell->showing_input_panels)
3031 return;
3032
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003033 shell->showing_input_panels = true;
3034
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003035 if (!shell->locked)
3036 wl_list_insert(&shell->panel_layer.link,
3037 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003038
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003039 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003040 &shell->input_panel.surfaces, link) {
3041 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003042 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003043 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003044 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003045 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003046 weston_surface_damage(ws);
3047 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003048 }
3049}
3050
3051static void
3052hide_input_panels(struct wl_listener *listener, void *data)
3053{
3054 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003055 container_of(listener, struct desktop_shell,
3056 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003057 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003058
Jan Arne Petersen61381972013-01-31 15:52:21 +01003059 if (!shell->showing_input_panels)
3060 return;
3061
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003062 shell->showing_input_panels = false;
3063
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003064 if (!shell->locked)
3065 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003066
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003067 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003068 &shell->input_panel_layer.surface_list, layer_link)
3069 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003070}
3071
3072static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003073center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003074{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003075 int32_t width = weston_surface_buffer_width(surface);
3076 int32_t height = weston_surface_buffer_height(surface);
3077 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003078
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003079 x = output->x + (output->width - width) / 2;
3080 y = output->y + (output->height - height) / 2;
3081
3082 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003083}
3084
3085static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003086weston_surface_set_initial_position (struct weston_surface *surface,
3087 struct desktop_shell *shell)
3088{
3089 struct weston_compositor *compositor = shell->compositor;
3090 int ix = 0, iy = 0;
3091 int range_x, range_y;
3092 int dx, dy, x, y, panel_height;
3093 struct weston_output *output, *target_output = NULL;
3094 struct weston_seat *seat;
3095
3096 /* As a heuristic place the new window on the same output as the
3097 * pointer. Falling back to the output containing 0, 0.
3098 *
3099 * TODO: Do something clever for touch too?
3100 */
3101 wl_list_for_each(seat, &compositor->seat_list, link) {
3102 if (seat->has_pointer) {
3103 ix = wl_fixed_to_int(seat->pointer.x);
3104 iy = wl_fixed_to_int(seat->pointer.y);
3105 break;
3106 }
3107 }
3108
3109 wl_list_for_each(output, &compositor->output_list, link) {
3110 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3111 target_output = output;
3112 break;
3113 }
3114 }
3115
3116 if (!target_output) {
3117 weston_surface_set_position(surface, 10 + random() % 400,
3118 10 + random() % 400);
3119 return;
3120 }
3121
3122 /* Valid range within output where the surface will still be onscreen.
3123 * If this is negative it means that the surface is bigger than
3124 * output.
3125 */
3126 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003127 range_x = target_output->width - surface->geometry.width;
3128 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003129 surface->geometry.height;
3130
Rob Bradford4cb88c72012-08-13 15:18:44 +01003131 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003132 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003133 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003134 dx = 0;
3135
3136 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003137 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003138 else
3139 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003140
3141 x = target_output->x + dx;
3142 y = target_output->y + dy;
3143
3144 weston_surface_set_position (surface, x, y);
3145}
3146
3147static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003148map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003149 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003150{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003151 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003152 struct shell_surface *shsurf = get_shell_surface(surface);
3153 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003154 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003155 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003156 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003157 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003158
Pekka Paalanen60921e52012-01-25 15:55:43 +02003159 surface->geometry.width = width;
3160 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003161 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003162
3163 /* initial positioning, see also configure() */
3164 switch (surface_type) {
3165 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003166 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003167 break;
Alex Wu4539b082012-03-01 12:57:46 +08003168 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003169 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003170 shell_map_fullscreen(shsurf);
3171 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003172 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003173 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003174 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01003175 weston_surface_set_position(surface, shsurf->output->x,
3176 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08003177 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003178 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003179 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003180 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003181 case SHELL_SURFACE_NONE:
3182 weston_surface_set_position(surface,
3183 surface->geometry.x + sx,
3184 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003185 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003186 default:
3187 ;
3188 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003189
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003190 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003191 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003192 case SHELL_SURFACE_POPUP:
3193 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003194 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003195 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3196 break;
Alex Wu4539b082012-03-01 12:57:46 +08003197 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003198 case SHELL_SURFACE_NONE:
3199 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003200 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003201 ws = get_current_workspace(shell);
3202 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003203 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003204 }
3205
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003206 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003207 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003208 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3209 surface->output = shsurf->output;
3210 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003211
Juan Zhao7bb92f02011-12-15 11:31:51 -05003212 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003213 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003214 if (shsurf->transient.flags ==
3215 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3216 break;
3217 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003218 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003219 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003220 if (!shell->locked) {
3221 wl_list_for_each(seat, &compositor->seat_list, link)
3222 activate(shell, surface, seat);
3223 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003224 break;
3225 default:
3226 break;
3227 }
3228
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003229 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003230 {
3231 switch (shell->win_animation_type) {
3232 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003233 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003234 break;
3235 case ANIMATION_ZOOM:
3236 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3237 break;
3238 default:
3239 break;
3240 }
3241 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003242}
3243
3244static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003245configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003246 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003247{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003248 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3249 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003250
Pekka Paalanen77346a62011-11-30 16:26:35 +02003251 shsurf = get_shell_surface(surface);
3252 if (shsurf)
3253 surface_type = shsurf->type;
3254
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003255 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003256
3257 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003258 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003259 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003260 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003261 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003262 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003263 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003264 surface->geometry.x = surface->output->x;
3265 surface->geometry.y = surface->output->y +
3266 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003267 break;
Alex Wu4539b082012-03-01 12:57:46 +08003268 case SHELL_SURFACE_TOPLEVEL:
3269 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003270 default:
3271 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003272 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003273
Alex Wu4539b082012-03-01 12:57:46 +08003274 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003275 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003276 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003277
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003278 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003279 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003280 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003281}
3282
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003283static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003284shell_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 +03003285{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003286 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003287 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003288
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003289 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003290
Giulio Camuffo5085a752013-03-25 21:42:45 +01003291 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3292 remove_popup_grab(shsurf);
3293 }
3294
Giulio Camuffo184df502013-02-21 11:29:21 +01003295 if (width == 0)
3296 return;
3297
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003298 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003299 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003300 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003301 type_changed = 1;
3302 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003303
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003304 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003305 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003306 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003307 es->geometry.width != width ||
3308 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003309 float from_x, from_y;
3310 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003311
3312 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3313 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3314 configure(shell, es,
3315 es->geometry.x + to_x - from_x,
3316 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003317 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003318 }
3319}
3320
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003321static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003322
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003323static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003324desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003325{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003326 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003327 struct desktop_shell *shell =
3328 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003329
3330 shell->child.process.pid = 0;
3331 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003332
3333 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3334 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003335 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003336 shell->child.deathstamp = time;
3337 shell->child.deathcount = 0;
3338 }
3339
3340 shell->child.deathcount++;
3341 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003342 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003343 return;
3344 }
3345
Martin Minarik6d118362012-06-07 18:01:59 +02003346 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003347 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003348}
3349
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003350static void
3351launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003352{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003353 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003354 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003355
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003356 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003357 &shell->child.process,
3358 shell_exe,
3359 desktop_shell_sigchld);
3360
3361 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003362 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003363}
3364
3365static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003366bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3367{
Tiago Vignattibe143262012-04-16 17:31:41 +03003368 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003369
3370 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003371 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003372}
3373
Kristian Høgsberg75840622011-09-06 13:48:16 -04003374static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003375unbind_desktop_shell(struct wl_resource *resource)
3376{
Tiago Vignattibe143262012-04-16 17:31:41 +03003377 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003378
3379 if (shell->locked)
3380 resume_desktop(shell);
3381
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003382 shell->child.desktop_shell = NULL;
3383 shell->prepare_event_sent = false;
3384 free(resource);
3385}
3386
3387static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003388bind_desktop_shell(struct wl_client *client,
3389 void *data, uint32_t version, uint32_t id)
3390{
Tiago Vignattibe143262012-04-16 17:31:41 +03003391 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003392 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003393
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003394 resource = wl_client_add_object(client, &desktop_shell_interface,
3395 &desktop_shell_implementation,
3396 id, shell);
3397
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003398 if (client == shell->child.client) {
3399 resource->destroy = unbind_desktop_shell;
3400 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003401 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003402 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003403
3404 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3405 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003406 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003407}
3408
Pekka Paalanen6e168112011-11-24 11:34:05 +02003409static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003410screensaver_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 -04003411{
3412 struct desktop_shell *shell = surface->private;
3413
Giulio Camuffo184df502013-02-21 11:29:21 +01003414 if (width == 0)
3415 return;
3416
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003417 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003418 if (!shell->locked)
3419 return;
3420
3421 center_on_output(surface, surface->output);
3422
3423 if (wl_list_empty(&surface->layer_link)) {
3424 wl_list_insert(shell->lock_layer.surface_list.prev,
3425 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003426 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003427 wl_event_source_timer_update(shell->screensaver.timer,
3428 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003429 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003430 }
3431}
3432
3433static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003434screensaver_set_surface(struct wl_client *client,
3435 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003436 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003437 struct wl_resource *output_resource)
3438{
Tiago Vignattibe143262012-04-16 17:31:41 +03003439 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003440 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003441 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003442
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003443 surface->configure = screensaver_configure;
3444 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003445 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003446}
3447
3448static const struct screensaver_interface screensaver_implementation = {
3449 screensaver_set_surface
3450};
3451
3452static void
3453unbind_screensaver(struct wl_resource *resource)
3454{
Tiago Vignattibe143262012-04-16 17:31:41 +03003455 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003456
Pekka Paalanen77346a62011-11-30 16:26:35 +02003457 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003458 free(resource);
3459}
3460
3461static void
3462bind_screensaver(struct wl_client *client,
3463 void *data, uint32_t version, uint32_t id)
3464{
Tiago Vignattibe143262012-04-16 17:31:41 +03003465 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003466 struct wl_resource *resource;
3467
3468 resource = wl_client_add_object(client, &screensaver_interface,
3469 &screensaver_implementation,
3470 id, shell);
3471
Pekka Paalanen77346a62011-11-30 16:26:35 +02003472 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003473 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003474 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003475 return;
3476 }
3477
3478 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3479 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003480 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003481}
3482
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003483static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003484input_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 -04003485{
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003486 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003487 float x, y;
3488
Giulio Camuffo184df502013-02-21 11:29:21 +01003489 if (width == 0)
3490 return;
3491
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003492 if (!weston_surface_is_mapped(surface))
3493 return;
3494
3495 mode = surface->output->current;
3496 x = (mode->width - width) / 2;
3497 y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003498
3499 /* Don't map the input panel here, wait for
3500 * show_input_panels signal. */
3501
3502 weston_surface_configure(surface,
3503 surface->output->x + x,
3504 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003505 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003506}
3507
3508static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003509destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003510{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003511 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003512 wl_list_remove(&input_panel_surface->link);
3513
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003514 input_panel_surface->surface->configure = NULL;
3515
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003516 free(input_panel_surface);
3517}
3518
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003519static struct input_panel_surface *
3520get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003521{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003522 if (surface->configure == input_panel_configure) {
3523 return surface->private;
3524 } else {
3525 return NULL;
3526 }
3527}
3528
3529static void
3530input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3531{
3532 struct input_panel_surface *ipsurface = container_of(listener,
3533 struct input_panel_surface,
3534 surface_destroy_listener);
3535
3536 if (ipsurface->resource.client) {
3537 wl_resource_destroy(&ipsurface->resource);
3538 } else {
3539 wl_signal_emit(&ipsurface->resource.destroy_signal,
3540 &ipsurface->resource);
3541 destroy_input_panel_surface(ipsurface);
3542 }
3543}
3544static struct input_panel_surface *
3545create_input_panel_surface(struct desktop_shell *shell,
3546 struct weston_surface *surface)
3547{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003548 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003549
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003550 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3551 if (!input_panel_surface)
3552 return NULL;
3553
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003554 surface->configure = input_panel_configure;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003555 surface->private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003556
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003557 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003558
3559 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003560
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003561 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3562 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3563 wl_signal_add(&surface->surface.resource.destroy_signal,
3564 &input_panel_surface->surface_destroy_listener);
3565
3566 wl_list_init(&input_panel_surface->link);
3567
3568 return input_panel_surface;
3569}
3570
3571static void
3572input_panel_surface_set_toplevel(struct wl_client *client,
3573 struct wl_resource *resource,
3574 uint32_t position)
3575{
3576 struct input_panel_surface *input_panel_surface = resource->data;
3577 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003578
3579 wl_list_insert(&shell->input_panel.surfaces,
3580 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003581}
3582
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003583static const struct input_panel_surface_interface input_panel_surface_implementation = {
3584 input_panel_surface_set_toplevel
3585};
3586
3587static void
3588destroy_input_panel_surface_resource(struct wl_resource *resource)
3589{
3590 struct input_panel_surface *ipsurf = resource->data;
3591
3592 destroy_input_panel_surface(ipsurf);
3593}
3594
3595static void
3596input_panel_get_input_panel_surface(struct wl_client *client,
3597 struct wl_resource *resource,
3598 uint32_t id,
3599 struct wl_resource *surface_resource)
3600{
3601 struct weston_surface *surface = surface_resource->data;
3602 struct desktop_shell *shell = resource->data;
3603 struct input_panel_surface *ipsurf;
3604
3605 if (get_input_panel_surface(surface)) {
3606 wl_resource_post_error(surface_resource,
3607 WL_DISPLAY_ERROR_INVALID_OBJECT,
3608 "input_panel::get_input_panel_surface already requested");
3609 return;
3610 }
3611
3612 ipsurf = create_input_panel_surface(shell, surface);
3613 if (!ipsurf) {
3614 wl_resource_post_error(surface_resource,
3615 WL_DISPLAY_ERROR_INVALID_OBJECT,
3616 "surface->configure already set");
3617 return;
3618 }
3619
3620 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3621 ipsurf->resource.object.id = id;
3622 ipsurf->resource.object.interface = &input_panel_surface_interface;
3623 ipsurf->resource.object.implementation =
3624 (void (**)(void)) &input_panel_surface_implementation;
3625 ipsurf->resource.data = ipsurf;
3626
3627 wl_client_add_resource(client, &ipsurf->resource);
3628}
3629
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003630static const struct input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003631 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003632};
3633
3634static void
3635unbind_input_panel(struct wl_resource *resource)
3636{
3637 struct desktop_shell *shell = resource->data;
3638
3639 shell->input_panel.binding = NULL;
3640 free(resource);
3641}
3642
3643static void
3644bind_input_panel(struct wl_client *client,
3645 void *data, uint32_t version, uint32_t id)
3646{
3647 struct desktop_shell *shell = data;
3648 struct wl_resource *resource;
3649
3650 resource = wl_client_add_object(client, &input_panel_interface,
3651 &input_panel_implementation,
3652 id, shell);
3653
3654 if (shell->input_panel.binding == NULL) {
3655 resource->destroy = unbind_input_panel;
3656 shell->input_panel.binding = resource;
3657 return;
3658 }
3659
3660 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3661 "interface object already bound");
3662 wl_resource_destroy(resource);
3663}
3664
Kristian Høgsberg07045392012-02-19 18:52:44 -05003665struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003666 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003667 struct weston_surface *current;
3668 struct wl_listener listener;
3669 struct wl_keyboard_grab grab;
3670};
3671
3672static void
3673switcher_next(struct switcher *switcher)
3674{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003675 struct weston_surface *surface;
3676 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003677 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003678 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003679
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003680 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003681 switch (get_shell_surface_type(surface)) {
3682 case SHELL_SURFACE_TOPLEVEL:
3683 case SHELL_SURFACE_FULLSCREEN:
3684 case SHELL_SURFACE_MAXIMIZED:
3685 if (first == NULL)
3686 first = surface;
3687 if (prev == switcher->current)
3688 next = surface;
3689 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003690 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003691 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003692 weston_surface_damage(surface);
3693 break;
3694 default:
3695 break;
3696 }
Alex Wu1659daa2012-04-01 20:13:09 +08003697
3698 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003699 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003700 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003701 weston_surface_damage(surface);
3702 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003703 }
3704
3705 if (next == NULL)
3706 next = first;
3707
Alex Wu07b26062012-03-12 16:06:01 +08003708 if (next == NULL)
3709 return;
3710
Kristian Høgsberg07045392012-02-19 18:52:44 -05003711 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003712 wl_signal_add(&next->surface.resource.destroy_signal,
3713 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003714
3715 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003716 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003717
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003718 shsurf = get_shell_surface(switcher->current);
3719 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003720 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003721}
3722
3723static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003724switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003725{
3726 struct switcher *switcher =
3727 container_of(listener, struct switcher, listener);
3728
3729 switcher_next(switcher);
3730}
3731
3732static void
Daniel Stone351eb612012-05-31 15:27:47 -04003733switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003734{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003735 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003736 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003737 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003738 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003739
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003740 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003741 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003742 weston_surface_damage(surface);
3743 }
3744
Alex Wu07b26062012-03-12 16:06:01 +08003745 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003746 activate(switcher->shell, switcher->current,
3747 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003748 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003749 wl_keyboard_end_grab(keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003750 if (weston_keyboard->input_method_resource)
3751 keyboard->grab = &weston_keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003752 free(switcher);
3753}
3754
3755static void
3756switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003757 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003758{
3759 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003760 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003761
Daniel Stonec9785ea2012-05-30 16:31:52 +01003762 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003763 switcher_next(switcher);
3764}
3765
3766static void
3767switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3768 uint32_t mods_depressed, uint32_t mods_latched,
3769 uint32_t mods_locked, uint32_t group)
3770{
3771 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003772 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003773
Daniel Stone351eb612012-05-31 15:27:47 -04003774 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3775 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003776}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003777
3778static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003779 switcher_key,
3780 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003781};
3782
3783static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003784switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3785 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003786{
Tiago Vignattibe143262012-04-16 17:31:41 +03003787 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003788 struct switcher *switcher;
3789
3790 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003791 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003792 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003793 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003794 wl_list_init(&switcher->listener.link);
3795
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003796 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003797 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003798 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3799 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003800 switcher_next(switcher);
3801}
3802
Pekka Paalanen3c647232011-12-22 13:43:43 +02003803static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003804backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3805 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003806{
3807 struct weston_compositor *compositor = data;
3808 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003809 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003810
3811 /* TODO: we're limiting to simple use cases, where we assume just
3812 * control on the primary display. We'd have to extend later if we
3813 * ever get support for setting backlights on random desktop LCD
3814 * panels though */
3815 output = get_default_output(compositor);
3816 if (!output)
3817 return;
3818
3819 if (!output->set_backlight)
3820 return;
3821
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003822 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3823 backlight_new = output->backlight_current - 25;
3824 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3825 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003826
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003827 if (backlight_new < 5)
3828 backlight_new = 5;
3829 if (backlight_new > 255)
3830 backlight_new = 255;
3831
3832 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003833 output->set_backlight(output, output->backlight_current);
3834}
3835
3836static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003837fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3838 void *data)
3839{
3840 struct desktop_shell *shell = data;
3841 struct weston_compositor *compositor = shell->compositor;
3842 compositor->fan_debug = !compositor->fan_debug;
3843 weston_compositor_damage_all(compositor);
3844}
3845
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003846struct debug_binding_grab {
3847 struct wl_keyboard_grab grab;
3848 struct weston_seat *seat;
3849 uint32_t key[2];
3850 int key_released[2];
3851};
3852
3853static void
3854debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3855 uint32_t key, uint32_t state)
3856{
3857 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3858 struct wl_resource *resource;
3859 struct wl_display *display;
3860 uint32_t serial;
3861 int send = 0, terminate = 0;
3862 int check_binding = 1;
3863 int i;
3864
3865 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3866 /* Do not run bindings on key releases */
3867 check_binding = 0;
3868
3869 for (i = 0; i < 2; i++)
3870 if (key == db->key[i])
3871 db->key_released[i] = 1;
3872
3873 if (db->key_released[0] && db->key_released[1]) {
3874 /* All key releases been swalled so end the grab */
3875 terminate = 1;
3876 } else if (key != db->key[0] && key != db->key[1]) {
3877 /* Should not swallow release of other keys */
3878 send = 1;
3879 }
3880 } else if (key == db->key[0] && !db->key_released[0]) {
3881 /* Do not check bindings for the first press of the binding
3882 * key. This allows it to be used as a debug shortcut.
3883 * We still need to swallow this event. */
3884 check_binding = 0;
3885 } else if (db->key[1]) {
3886 /* If we already ran a binding don't process another one since
3887 * we can't keep track of all the binding keys that were
3888 * pressed in order to swallow the release events. */
3889 send = 1;
3890 check_binding = 0;
3891 }
3892
3893 if (check_binding) {
3894 struct weston_compositor *ec = db->seat->compositor;
3895
3896 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3897 key, state)) {
3898 /* We ran a binding so swallow the press and keep the
3899 * grab to swallow the released too. */
3900 send = 0;
3901 terminate = 0;
3902 db->key[1] = key;
3903 } else {
3904 /* Terminate the grab since the key pressed is not a
3905 * debug binding key. */
3906 send = 1;
3907 terminate = 1;
3908 }
3909 }
3910
3911 if (send) {
3912 resource = grab->keyboard->focus_resource;
3913
3914 if (resource) {
3915 display = wl_client_get_display(resource->client);
3916 serial = wl_display_next_serial(display);
3917 wl_keyboard_send_key(resource, serial, time, key, state);
3918 }
3919 }
3920
3921 if (terminate) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003922 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003923 wl_keyboard_end_grab(grab->keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003924 if (weston_keyboard->input_method_resource)
3925 grab->keyboard->grab = &weston_keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003926 free(db);
3927 }
3928}
3929
3930static void
3931debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3932 uint32_t mods_depressed, uint32_t mods_latched,
3933 uint32_t mods_locked, uint32_t group)
3934{
3935 struct wl_resource *resource;
3936
3937 resource = grab->keyboard->focus_resource;
3938 if (!resource)
3939 return;
3940
3941 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3942 mods_latched, mods_locked, group);
3943}
3944
3945struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3946 debug_binding_key,
3947 debug_binding_modifiers
3948};
3949
3950static void
3951debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3952{
3953 struct debug_binding_grab *grab;
3954
3955 grab = calloc(1, sizeof *grab);
3956 if (!grab)
3957 return;
3958
3959 grab->seat = (struct weston_seat *) seat;
3960 grab->key[0] = key;
3961 grab->grab.interface = &debug_binding_keyboard_grab;
3962 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3963}
3964
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003965static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003966force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3967 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003968{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003969 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003970 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003971 struct desktop_shell *shell = data;
3972 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003973 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003974
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003975 focus_surface = seat->keyboard->focus;
3976 if (!focus_surface)
3977 return;
3978
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003979 wl_signal_emit(&compositor->kill_signal, focus_surface);
3980
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003981 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003982 wl_client_get_credentials(client, &pid, NULL, NULL);
3983
3984 /* Skip clients that we launched ourselves (the credentials of
3985 * the socketpair is ours) */
3986 if (pid == getpid())
3987 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003988
Daniel Stone325fc2d2012-05-30 16:31:58 +01003989 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003990}
3991
3992static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003993workspace_up_binding(struct wl_seat *seat, uint32_t time,
3994 uint32_t key, void *data)
3995{
3996 struct desktop_shell *shell = data;
3997 unsigned int new_index = shell->workspaces.current;
3998
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003999 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004000 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004001 if (new_index != 0)
4002 new_index--;
4003
4004 change_workspace(shell, new_index);
4005}
4006
4007static void
4008workspace_down_binding(struct wl_seat *seat, uint32_t time,
4009 uint32_t key, void *data)
4010{
4011 struct desktop_shell *shell = data;
4012 unsigned int new_index = shell->workspaces.current;
4013
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004014 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004015 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004016 if (new_index < shell->workspaces.num - 1)
4017 new_index++;
4018
4019 change_workspace(shell, new_index);
4020}
4021
4022static void
4023workspace_f_binding(struct wl_seat *seat, uint32_t time,
4024 uint32_t key, void *data)
4025{
4026 struct desktop_shell *shell = data;
4027 unsigned int new_index;
4028
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004029 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004030 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004031 new_index = key - KEY_F1;
4032 if (new_index >= shell->workspaces.num)
4033 new_index = shell->workspaces.num - 1;
4034
4035 change_workspace(shell, new_index);
4036}
4037
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004038static void
4039workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
4040 uint32_t key, void *data)
4041{
4042 struct desktop_shell *shell = data;
4043 unsigned int new_index = shell->workspaces.current;
4044
4045 if (shell->locked)
4046 return;
4047
4048 if (new_index != 0)
4049 new_index--;
4050
4051 take_surface_to_workspace_by_seat(shell, seat, new_index);
4052}
4053
4054static void
4055workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
4056 uint32_t key, void *data)
4057{
4058 struct desktop_shell *shell = data;
4059 unsigned int new_index = shell->workspaces.current;
4060
4061 if (shell->locked)
4062 return;
4063
4064 if (new_index < shell->workspaces.num - 1)
4065 new_index++;
4066
4067 take_surface_to_workspace_by_seat(shell, seat, new_index);
4068}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004069
4070static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004071shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004072{
Tiago Vignattibe143262012-04-16 17:31:41 +03004073 struct desktop_shell *shell =
4074 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004075 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004076
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004077 if (shell->child.client)
4078 wl_client_destroy(shell->child.client);
4079
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004080 wl_list_remove(&shell->idle_listener.link);
4081 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004082 wl_list_remove(&shell->show_input_panel_listener.link);
4083 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004084
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004085 wl_array_for_each(ws, &shell->workspaces.array)
4086 workspace_destroy(*ws);
4087 wl_array_release(&shell->workspaces.array);
4088
Pekka Paalanen3c647232011-12-22 13:43:43 +02004089 free(shell->screensaver.path);
4090 free(shell);
4091}
4092
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004093static void
4094shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4095{
4096 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004097 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004098
4099 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004100 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4101 MODIFIER_CTRL | MODIFIER_ALT,
4102 terminate_binding, ec);
4103 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4104 click_to_activate_binding,
4105 shell);
4106 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4107 MODIFIER_SUPER | MODIFIER_ALT,
4108 surface_opacity_binding, NULL);
4109 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4110 MODIFIER_SUPER, zoom_axis_binding,
4111 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004112
4113 /* configurable bindings */
4114 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004115 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4116 zoom_key_binding, NULL);
4117 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4118 zoom_key_binding, NULL);
4119 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4120 shell);
4121 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4122 resize_binding, shell);
4123 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4124 rotate_binding, NULL);
4125 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4126 shell);
4127 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4128 ec);
4129 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4130 backlight_binding, ec);
4131 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4132 ec);
4133 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4134 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004135 weston_compositor_add_key_binding(ec, KEY_K, mod,
4136 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004137 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4138 workspace_up_binding, shell);
4139 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4140 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004141 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4142 workspace_move_surface_up_binding,
4143 shell);
4144 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4145 workspace_move_surface_down_binding,
4146 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004147
4148 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4149 if (shell->workspaces.num > 1) {
4150 num_workspace_bindings = shell->workspaces.num;
4151 if (num_workspace_bindings > 6)
4152 num_workspace_bindings = 6;
4153 for (i = 0; i < num_workspace_bindings; i++)
4154 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4155 workspace_f_binding,
4156 shell);
4157 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004158
4159 /* Debug bindings */
4160 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4161 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004162 weston_compositor_add_debug_binding(ec, KEY_F,
4163 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004164}
4165
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004166WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004167module_init(struct weston_compositor *ec,
4168 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004169{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004170 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004171 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004172 struct workspace **pws;
4173 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004174 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004175
4176 shell = malloc(sizeof *shell);
4177 if (shell == NULL)
4178 return -1;
4179
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004180 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004181 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004182
4183 shell->destroy_listener.notify = shell_destroy;
4184 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004185 shell->idle_listener.notify = idle_handler;
4186 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4187 shell->wake_listener.notify = wake_handler;
4188 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004189 shell->show_input_panel_listener.notify = show_input_panels;
4190 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4191 shell->hide_input_panel_listener.notify = hide_input_panels;
4192 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004193 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004194 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004195 ec->shell_interface.create_shell_surface = create_shell_surface;
4196 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004197 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004198 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004199 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004200 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004201
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004202 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004203
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004204 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4205 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004206 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4207 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004208 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004209
4210 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004211 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004212
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004213 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004214
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004215 for (i = 0; i < shell->workspaces.num; i++) {
4216 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4217 if (pws == NULL)
4218 return -1;
4219
4220 *pws = workspace_create();
4221 if (*pws == NULL)
4222 return -1;
4223 }
4224 activate_workspace(shell, 0);
4225
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004226 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004227 wl_list_init(&shell->workspaces.animation.link);
4228 shell->workspaces.animation.frame = animate_workspace_change_frame;
4229
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004230 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4231 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004232 return -1;
4233
Kristian Høgsberg75840622011-09-06 13:48:16 -04004234 if (wl_display_add_global(ec->wl_display,
4235 &desktop_shell_interface,
4236 shell, bind_desktop_shell) == NULL)
4237 return -1;
4238
Pekka Paalanen6e168112011-11-24 11:34:05 +02004239 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4240 shell, bind_screensaver) == NULL)
4241 return -1;
4242
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004243 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4244 shell, bind_input_panel) == NULL)
4245 return -1;
4246
Jonas Ådahle9d22502012-08-29 22:13:01 +02004247 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4248 shell, bind_workspace_manager) == NULL)
4249 return -1;
4250
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004251 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004252
4253 loop = wl_display_get_event_loop(ec->wl_display);
4254 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004255
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004256 shell->screensaver.timer =
4257 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4258
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004259 wl_list_for_each(seat, &ec->seat_list, link)
4260 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004261
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004262 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004263
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004264 shell_fade(shell, FADE_IN);
4265
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004266 return 0;
4267}