blob: 26f0d605115d9d3f3d626e7e65c4a58c5dbfb6fd [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 {
Scott Moreau447013d2012-02-18 05:05:29 -0700191 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500192 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200193 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500194 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100195 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400196 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500197 } popup;
198
Alex Wu4539b082012-03-01 12:57:46 +0800199 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300200 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400201 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300202 } transient;
203
204 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800205 enum wl_shell_surface_fullscreen_method type;
206 struct weston_transform transform; /* matrix from x, y */
207 uint32_t framerate;
208 struct weston_surface *black_surface;
209 } fullscreen;
210
Scott Moreauff1db4a2012-04-17 19:06:18 -0600211 struct ping_timer *ping_timer;
212
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200213 struct weston_transform workspace_transform;
214
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500215 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500216 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100217 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400218
219 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200220};
221
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300222struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700223 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300224 struct shell_surface *shsurf;
225 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300226 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300227};
228
229struct weston_move_grab {
230 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100231 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500232};
233
Pekka Paalanen460099f2012-01-20 16:48:25 +0200234struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500236 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200237 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200238 float x;
239 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200240 } center;
241};
242
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400243static void
244activate(struct desktop_shell *shell, struct weston_surface *es,
245 struct weston_seat *seat);
246
247static struct workspace *
248get_current_workspace(struct desktop_shell *shell);
249
Alex Wubd3354b2012-04-17 17:20:49 +0800250static struct shell_surface *
251get_shell_surface(struct weston_surface *surface);
252
253static struct desktop_shell *
254shell_surface_get_shell(struct shell_surface *shsurf);
255
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500256static void
257surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
258
Alex Wubd3354b2012-04-17 17:20:49 +0800259static bool
260shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
261{
262 struct desktop_shell *shell;
263 struct weston_surface *top_fs_es;
264
265 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100266
Alex Wubd3354b2012-04-17 17:20:49 +0800267 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
268 return false;
269
270 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100271 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800272 layer_link);
273 return (shsurf == get_shell_surface(top_fs_es));
274}
275
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500276static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400277destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300278{
279 struct shell_grab *grab;
280
281 grab = container_of(listener, struct shell_grab,
282 shsurf_destroy_listener);
283
284 grab->shsurf = NULL;
285}
286
287static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400288popup_grab_end(struct wl_pointer *pointer);
289
290static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300291shell_grab_start(struct shell_grab *grab,
292 const struct wl_pointer_grab_interface *interface,
293 struct shell_surface *shsurf,
294 struct wl_pointer *pointer,
295 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300296{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300297 struct desktop_shell *shell = shsurf->shell;
298
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400299 popup_grab_end(pointer);
300
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300301 grab->grab.interface = interface;
302 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400303 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
304 wl_signal_add(&shsurf->resource.destroy_signal,
305 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300307 grab->pointer = pointer;
308 grab->grab.focus = &shsurf->surface->surface;
309
310 wl_pointer_start_grab(pointer, &grab->grab);
311 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
312 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
313 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300314}
315
316static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300317shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300318{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400319 if (grab->shsurf)
320 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321
322 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323}
324
325static void
Alex Wu4539b082012-03-01 12:57:46 +0800326center_on_output(struct weston_surface *surface,
327 struct weston_output *output);
328
Daniel Stone496ca172012-05-30 16:31:42 +0100329static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300330get_modifier(char *modifier)
331{
332 if (!modifier)
333 return MODIFIER_SUPER;
334
335 if (!strcmp("ctrl", modifier))
336 return MODIFIER_CTRL;
337 else if (!strcmp("alt", modifier))
338 return MODIFIER_ALT;
339 else if (!strcmp("super", modifier))
340 return MODIFIER_SUPER;
341 else
342 return MODIFIER_SUPER;
343}
344
Juan Zhaoe10d2792012-04-25 19:09:52 +0800345static enum animation_type
346get_animation_type(char *animation)
347{
348 if (!animation)
349 return ANIMATION_NONE;
350
351 if (!strcmp("zoom", animation))
352 return ANIMATION_ZOOM;
353 else if (!strcmp("fade", animation))
354 return ANIMATION_FADE;
355 else
356 return ANIMATION_NONE;
357}
358
Alex Wu4539b082012-03-01 12:57:46 +0800359static void
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500360shell_configuration(struct desktop_shell *shell, const char *config_file)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200361{
Pekka Paalanen7296e792011-12-07 16:22:00 +0200362 char *path = NULL;
363 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200364 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300365 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800366 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200367
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400368 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300369 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800370 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200371 { "num-workspaces",
372 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200373 };
374
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400375 struct config_key saver_keys[] = {
376 { "path", CONFIG_KEY_STRING, &path },
377 { "duration", CONFIG_KEY_INTEGER, &duration },
378 };
379
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200380 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400381 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200382 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
383 };
384
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500385 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200386
Pekka Paalanen7296e792011-12-07 16:22:00 +0200387 shell->screensaver.path = path;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200388 shell->screensaver.duration = duration * 1000;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300389 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800390 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200391 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
392}
393
394static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200395focus_state_destroy(struct focus_state *state)
396{
397 wl_list_remove(&state->seat_destroy_listener.link);
398 wl_list_remove(&state->surface_destroy_listener.link);
399 free(state);
400}
401
402static void
403focus_state_seat_destroy(struct wl_listener *listener, void *data)
404{
405 struct focus_state *state = container_of(listener,
406 struct focus_state,
407 seat_destroy_listener);
408
409 wl_list_remove(&state->link);
410 focus_state_destroy(state);
411}
412
413static void
414focus_state_surface_destroy(struct wl_listener *listener, void *data)
415{
416 struct focus_state *state = container_of(listener,
417 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400418 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400419 struct desktop_shell *shell;
420 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200421
Kristian Høgsberge3778222012-07-31 17:29:30 -0400422 next = NULL;
423 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
424 if (surface == state->keyboard_focus)
425 continue;
426
427 next = surface;
428 break;
429 }
430
431 if (next) {
432 shell = state->seat->compositor->shell_interface.shell;
433 activate(shell, next, state->seat);
434 } else {
435 wl_list_remove(&state->link);
436 focus_state_destroy(state);
437 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200438}
439
440static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400441focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200442{
Jonas Ådahl04769742012-06-13 00:01:24 +0200443 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200444
445 state = malloc(sizeof *state);
446 if (state == NULL)
447 return NULL;
448
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400449 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200450 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400451 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200452
453 state->seat_destroy_listener.notify = focus_state_seat_destroy;
454 state->surface_destroy_listener.notify = focus_state_surface_destroy;
455 wl_signal_add(&seat->seat.destroy_signal,
456 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400457 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200458
459 return state;
460}
461
Jonas Ådahl8538b222012-08-29 22:13:03 +0200462static struct focus_state *
463ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
464{
465 struct workspace *ws = get_current_workspace(shell);
466 struct focus_state *state;
467
468 wl_list_for_each(state, &ws->focus_list, link)
469 if (state->seat == seat)
470 break;
471
472 if (&state->link == &ws->focus_list)
473 state = focus_state_create(seat, ws);
474
475 return state;
476}
477
Jonas Ådahl04769742012-06-13 00:01:24 +0200478static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400479restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200480{
481 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200482 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200483
484 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200485 surface = state->keyboard_focus ?
486 &state->keyboard_focus->surface : NULL;
487
488 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200489 }
490}
491
492static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200493replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
494 struct weston_seat *seat)
495{
496 struct focus_state *state;
497 struct wl_surface *surface;
498
499 wl_list_for_each(state, &ws->focus_list, link) {
500 if (state->seat == seat) {
501 surface = seat->seat.keyboard->focus;
502 state->keyboard_focus =
503 (struct weston_surface *) surface;
504 return;
505 }
506 }
507}
508
509static void
510drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
511 struct weston_surface *surface)
512{
513 struct focus_state *state;
514
515 wl_list_for_each(state, &ws->focus_list, link)
516 if (state->keyboard_focus == surface)
517 state->keyboard_focus = NULL;
518}
519
520static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200521workspace_destroy(struct workspace *ws)
522{
Jonas Ådahl04769742012-06-13 00:01:24 +0200523 struct focus_state *state, *next;
524
525 wl_list_for_each_safe(state, next, &ws->focus_list, link)
526 focus_state_destroy(state);
527
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200528 free(ws);
529}
530
Jonas Ådahl04769742012-06-13 00:01:24 +0200531static void
532seat_destroyed(struct wl_listener *listener, void *data)
533{
534 struct weston_seat *seat = data;
535 struct focus_state *state, *next;
536 struct workspace *ws = container_of(listener,
537 struct workspace,
538 seat_destroyed_listener);
539
540 wl_list_for_each_safe(state, next, &ws->focus_list, link)
541 if (state->seat == seat)
542 wl_list_remove(&state->link);
543}
544
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200545static struct workspace *
546workspace_create(void)
547{
548 struct workspace *ws = malloc(sizeof *ws);
549 if (ws == NULL)
550 return NULL;
551
552 weston_layer_init(&ws->layer, NULL);
553
Jonas Ådahl04769742012-06-13 00:01:24 +0200554 wl_list_init(&ws->focus_list);
555 wl_list_init(&ws->seat_destroyed_listener.link);
556 ws->seat_destroyed_listener.notify = seat_destroyed;
557
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200558 return ws;
559}
560
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200561static int
562workspace_is_empty(struct workspace *ws)
563{
564 return wl_list_empty(&ws->layer.surface_list);
565}
566
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200567static struct workspace *
568get_workspace(struct desktop_shell *shell, unsigned int index)
569{
570 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200571 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200572 pws += index;
573 return *pws;
574}
575
576static struct workspace *
577get_current_workspace(struct desktop_shell *shell)
578{
579 return get_workspace(shell, shell->workspaces.current);
580}
581
582static void
583activate_workspace(struct desktop_shell *shell, unsigned int index)
584{
585 struct workspace *ws;
586
587 ws = get_workspace(shell, index);
588 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
589
590 shell->workspaces.current = index;
591}
592
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200593static unsigned int
594get_output_height(struct weston_output *output)
595{
596 return abs(output->region.extents.y1 - output->region.extents.y2);
597}
598
599static void
600surface_translate(struct weston_surface *surface, double d)
601{
602 struct shell_surface *shsurf = get_shell_surface(surface);
603 struct weston_transform *transform;
604
605 transform = &shsurf->workspace_transform;
606 if (wl_list_empty(&transform->link))
607 wl_list_insert(surface->geometry.transformation_list.prev,
608 &shsurf->workspace_transform.link);
609
610 weston_matrix_init(&shsurf->workspace_transform.matrix);
611 weston_matrix_translate(&shsurf->workspace_transform.matrix,
612 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200613 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200614}
615
616static void
617workspace_translate_out(struct workspace *ws, double fraction)
618{
619 struct weston_surface *surface;
620 unsigned int height;
621 double d;
622
623 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
624 height = get_output_height(surface->output);
625 d = height * fraction;
626
627 surface_translate(surface, d);
628 }
629}
630
631static void
632workspace_translate_in(struct workspace *ws, double fraction)
633{
634 struct weston_surface *surface;
635 unsigned int height;
636 double d;
637
638 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
639 height = get_output_height(surface->output);
640
641 if (fraction > 0)
642 d = -(height - height * fraction);
643 else
644 d = height + height * fraction;
645
646 surface_translate(surface, d);
647 }
648}
649
650static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200651broadcast_current_workspace_state(struct desktop_shell *shell)
652{
653 struct wl_resource *resource;
654
655 wl_list_for_each(resource, &shell->workspaces.client_list, link)
656 workspace_manager_send_state(resource,
657 shell->workspaces.current,
658 shell->workspaces.num);
659}
660
661static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200662reverse_workspace_change_animation(struct desktop_shell *shell,
663 unsigned int index,
664 struct workspace *from,
665 struct workspace *to)
666{
667 shell->workspaces.current = index;
668
669 shell->workspaces.anim_to = to;
670 shell->workspaces.anim_from = from;
671 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
672 shell->workspaces.anim_timestamp = 0;
673
Scott Moreau4272e632012-08-13 09:58:41 -0600674 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200675}
676
677static void
678workspace_deactivate_transforms(struct workspace *ws)
679{
680 struct weston_surface *surface;
681 struct shell_surface *shsurf;
682
683 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
684 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200685 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
686 wl_list_remove(&shsurf->workspace_transform.link);
687 wl_list_init(&shsurf->workspace_transform.link);
688 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200689 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200690 }
691}
692
693static void
694finish_workspace_change_animation(struct desktop_shell *shell,
695 struct workspace *from,
696 struct workspace *to)
697{
Scott Moreau4272e632012-08-13 09:58:41 -0600698 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200699
700 wl_list_remove(&shell->workspaces.animation.link);
701 workspace_deactivate_transforms(from);
702 workspace_deactivate_transforms(to);
703 shell->workspaces.anim_to = NULL;
704
705 wl_list_remove(&shell->workspaces.anim_from->layer.link);
706}
707
708static void
709animate_workspace_change_frame(struct weston_animation *animation,
710 struct weston_output *output, uint32_t msecs)
711{
712 struct desktop_shell *shell =
713 container_of(animation, struct desktop_shell,
714 workspaces.animation);
715 struct workspace *from = shell->workspaces.anim_from;
716 struct workspace *to = shell->workspaces.anim_to;
717 uint32_t t;
718 double x, y;
719
720 if (workspace_is_empty(from) && workspace_is_empty(to)) {
721 finish_workspace_change_animation(shell, from, to);
722 return;
723 }
724
725 if (shell->workspaces.anim_timestamp == 0) {
726 if (shell->workspaces.anim_current == 0.0)
727 shell->workspaces.anim_timestamp = msecs;
728 else
729 shell->workspaces.anim_timestamp =
730 msecs -
731 /* Invers of movement function 'y' below. */
732 (asin(1.0 - shell->workspaces.anim_current) *
733 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
734 M_2_PI);
735 }
736
737 t = msecs - shell->workspaces.anim_timestamp;
738
739 /*
740 * x = [0, π/2]
741 * y(x) = sin(x)
742 */
743 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
744 y = sin(x);
745
746 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600747 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200748
749 workspace_translate_out(from, shell->workspaces.anim_dir * y);
750 workspace_translate_in(to, shell->workspaces.anim_dir * y);
751 shell->workspaces.anim_current = y;
752
Scott Moreau4272e632012-08-13 09:58:41 -0600753 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200754 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200755 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200756 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200757}
758
759static void
760animate_workspace_change(struct desktop_shell *shell,
761 unsigned int index,
762 struct workspace *from,
763 struct workspace *to)
764{
765 struct weston_output *output;
766
767 int dir;
768
769 if (index > shell->workspaces.current)
770 dir = -1;
771 else
772 dir = 1;
773
774 shell->workspaces.current = index;
775
776 shell->workspaces.anim_dir = dir;
777 shell->workspaces.anim_from = from;
778 shell->workspaces.anim_to = to;
779 shell->workspaces.anim_current = 0.0;
780 shell->workspaces.anim_timestamp = 0;
781
782 output = container_of(shell->compositor->output_list.next,
783 struct weston_output, link);
784 wl_list_insert(&output->animation_list,
785 &shell->workspaces.animation.link);
786
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200787 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200788
789 workspace_translate_in(to, 0);
790
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400791 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200792
Scott Moreau4272e632012-08-13 09:58:41 -0600793 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200794}
795
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200796static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200797update_workspace(struct desktop_shell *shell, unsigned int index,
798 struct workspace *from, struct workspace *to)
799{
800 shell->workspaces.current = index;
801 wl_list_insert(&from->layer.link, &to->layer.link);
802 wl_list_remove(&from->layer.link);
803}
804
805static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200806change_workspace(struct desktop_shell *shell, unsigned int index)
807{
808 struct workspace *from;
809 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200810
811 if (index == shell->workspaces.current)
812 return;
813
814 /* Don't change workspace when there is any fullscreen surfaces. */
815 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
816 return;
817
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200818 from = get_current_workspace(shell);
819 to = get_workspace(shell, index);
820
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200821 if (shell->workspaces.anim_from == to &&
822 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200823 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200824 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200825 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200826 return;
827 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200829 if (shell->workspaces.anim_to != NULL)
830 finish_workspace_change_animation(shell,
831 shell->workspaces.anim_from,
832 shell->workspaces.anim_to);
833
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200834 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200835
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200836 if (workspace_is_empty(to) && workspace_is_empty(from))
837 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200838 else
839 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200840
841 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200842}
843
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200844static bool
845workspace_has_only(struct workspace *ws, struct weston_surface *surface)
846{
847 struct wl_list *list = &ws->layer.surface_list;
848 struct wl_list *e;
849
850 if (wl_list_empty(list))
851 return false;
852
853 e = list->next;
854
855 if (e->next != list)
856 return false;
857
858 return container_of(e, struct weston_surface, layer_link) == surface;
859}
860
861static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200862move_surface_to_workspace(struct desktop_shell *shell,
863 struct weston_surface *surface,
864 uint32_t workspace)
865{
866 struct workspace *from;
867 struct workspace *to;
868 struct weston_seat *seat;
869
870 if (workspace == shell->workspaces.current)
871 return;
872
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200873 if (workspace >= shell->workspaces.num)
874 workspace = shell->workspaces.num - 1;
875
Jonas Ådahle9d22502012-08-29 22:13:01 +0200876 from = get_current_workspace(shell);
877 to = get_workspace(shell, workspace);
878
879 wl_list_remove(&surface->layer_link);
880 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
881
882 drop_focus_state(shell, from, surface);
883 wl_list_for_each(seat, &shell->compositor->seat_list, link)
884 if (seat->has_keyboard &&
Jan Arne Petersena75a7892013-01-16 21:26:50 +0100885 seat->keyboard.keyboard.focus == &surface->surface)
886 wl_keyboard_set_focus(&seat->keyboard.keyboard, NULL);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200887
888 weston_surface_damage_below(surface);
889}
890
891static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200892take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200893 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200894 unsigned int index)
895{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200896 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200897 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200898 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200899 struct shell_surface *shsurf;
900 struct workspace *from;
901 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200902 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200903
904 if (surface == NULL ||
905 index == shell->workspaces.current)
906 return;
907
908 from = get_current_workspace(shell);
909 to = get_workspace(shell, index);
910
911 wl_list_remove(&surface->layer_link);
912 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
913
Jonas Ådahle9d22502012-08-29 22:13:01 +0200914 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200915 drop_focus_state(shell, from, surface);
916
917 if (shell->workspaces.anim_from == to &&
918 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200919 wl_list_remove(&to->layer.link);
920 wl_list_insert(from->layer.link.prev, &to->layer.link);
921
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200922 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200923 broadcast_current_workspace_state(shell);
924
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200925 return;
926 }
927
928 if (shell->workspaces.anim_to != NULL)
929 finish_workspace_change_animation(shell,
930 shell->workspaces.anim_from,
931 shell->workspaces.anim_to);
932
933 if (workspace_is_empty(from) &&
934 workspace_has_only(to, surface))
935 update_workspace(shell, index, from, to);
936 else {
937 shsurf = get_shell_surface(surface);
938 if (wl_list_empty(&shsurf->workspace_transform.link))
939 wl_list_insert(&shell->workspaces.anim_sticky_list,
940 &shsurf->workspace_transform.link);
941
942 animate_workspace_change(shell, index, from, to);
943 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200944
945 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200946
947 state = ensure_focus_state(shell, seat);
948 if (state != NULL)
949 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200950}
951
952static void
953workspace_manager_move_surface(struct wl_client *client,
954 struct wl_resource *resource,
955 struct wl_resource *surface_resource,
956 uint32_t workspace)
957{
958 struct desktop_shell *shell = resource->data;
959 struct weston_surface *surface =
960 (struct weston_surface *) surface_resource;
961
962 move_surface_to_workspace(shell, surface, workspace);
963}
964
965static const struct workspace_manager_interface workspace_manager_implementation = {
966 workspace_manager_move_surface,
967};
968
969static void
970unbind_resource(struct wl_resource *resource)
971{
972 wl_list_remove(&resource->link);
973 free(resource);
974}
975
976static void
977bind_workspace_manager(struct wl_client *client,
978 void *data, uint32_t version, uint32_t id)
979{
980 struct desktop_shell *shell = data;
981 struct wl_resource *resource;
982
983 resource = wl_client_add_object(client, &workspace_manager_interface,
984 &workspace_manager_implementation,
985 id, shell);
986
987 if (resource == NULL) {
988 weston_log("couldn't add workspace manager object");
989 return;
990 }
991
992 resource->destroy = unbind_resource;
993 wl_list_insert(&shell->workspaces.client_list, &resource->link);
994
995 workspace_manager_send_state(resource,
996 shell->workspaces.current,
997 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200998}
999
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001000static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001001noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001002 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001003{
1004 grab->focus = NULL;
1005}
1006
1007static void
Scott Moreau447013d2012-02-18 05:05:29 -07001008move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001009 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001010{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001011 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001012 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001013 struct shell_surface *shsurf = move->base.shsurf;
1014 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001015 int dx = wl_fixed_to_int(pointer->x + move->dx);
1016 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001017
1018 if (!shsurf)
1019 return;
1020
1021 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001022
Daniel Stone103db7f2012-05-08 17:17:55 +01001023 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001024 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001025
1026 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001027}
1028
1029static void
Scott Moreau447013d2012-02-18 05:05:29 -07001030move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001031 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001032{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001033 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1034 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001035 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001036 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001037
Daniel Stone4dbadb12012-05-30 16:31:51 +01001038 if (pointer->button_count == 0 &&
1039 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001040 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001041 free(grab);
1042 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001043}
1044
Scott Moreau447013d2012-02-18 05:05:29 -07001045static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001046 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001047 move_grab_motion,
1048 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001049};
1050
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001051static int
1052surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1053{
1054 struct weston_move_grab *move;
1055
1056 if (!shsurf)
1057 return -1;
1058
1059 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1060 return 0;
1061
1062 move = malloc(sizeof *move);
1063 if (!move)
1064 return -1;
1065
1066 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1067 ws->seat.pointer->grab_x;
1068 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1069 ws->seat.pointer->grab_y;
1070
1071 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1072 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1073
1074 return 0;
1075}
1076
1077static void
1078shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1079 struct wl_resource *seat_resource, uint32_t serial)
1080{
1081 struct weston_seat *ws = seat_resource->data;
1082 struct shell_surface *shsurf = resource->data;
1083
1084 if (ws->seat.pointer->button_count == 0 ||
1085 ws->seat.pointer->grab_serial != serial ||
1086 ws->seat.pointer->focus != &shsurf->surface->surface)
1087 return;
1088
1089 if (surface_move(shsurf, ws) < 0)
1090 wl_resource_post_no_memory(resource);
1091}
1092
1093struct weston_resize_grab {
1094 struct shell_grab base;
1095 uint32_t edges;
1096 int32_t width, height;
1097};
1098
1099static void
1100resize_grab_motion(struct wl_pointer_grab *grab,
1101 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1102{
1103 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1104 struct wl_pointer *pointer = grab->pointer;
1105 struct shell_surface *shsurf = resize->base.shsurf;
1106 int32_t width, height;
1107 wl_fixed_t from_x, from_y;
1108 wl_fixed_t to_x, to_y;
1109
1110 if (!shsurf)
1111 return;
1112
1113 weston_surface_from_global_fixed(shsurf->surface,
1114 pointer->grab_x, pointer->grab_y,
1115 &from_x, &from_y);
1116 weston_surface_from_global_fixed(shsurf->surface,
1117 pointer->x, pointer->y, &to_x, &to_y);
1118
1119 width = resize->width;
1120 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1121 width += wl_fixed_to_int(from_x - to_x);
1122 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1123 width += wl_fixed_to_int(to_x - from_x);
1124 }
1125
1126 height = resize->height;
1127 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1128 height += wl_fixed_to_int(from_y - to_y);
1129 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1130 height += wl_fixed_to_int(to_y - from_y);
1131 }
1132
1133 shsurf->client->send_configure(shsurf->surface,
1134 resize->edges, width, height);
1135}
1136
1137static void
1138send_configure(struct weston_surface *surface,
1139 uint32_t edges, int32_t width, int32_t height)
1140{
1141 struct shell_surface *shsurf = get_shell_surface(surface);
1142
1143 wl_shell_surface_send_configure(&shsurf->resource,
1144 edges, width, height);
1145}
1146
1147static const struct weston_shell_client shell_client = {
1148 send_configure
1149};
1150
1151static void
1152resize_grab_button(struct wl_pointer_grab *grab,
1153 uint32_t time, uint32_t button, uint32_t state_w)
1154{
1155 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1156 struct wl_pointer *pointer = grab->pointer;
1157 enum wl_pointer_button_state state = state_w;
1158
1159 if (pointer->button_count == 0 &&
1160 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1161 shell_grab_end(&resize->base);
1162 free(grab);
1163 }
1164}
1165
1166static const struct wl_pointer_grab_interface resize_grab_interface = {
1167 noop_grab_focus,
1168 resize_grab_motion,
1169 resize_grab_button,
1170};
1171
1172static int
1173surface_resize(struct shell_surface *shsurf,
1174 struct weston_seat *ws, uint32_t edges)
1175{
1176 struct weston_resize_grab *resize;
1177
1178 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1179 return 0;
1180
1181 if (edges == 0 || edges > 15 ||
1182 (edges & 3) == 3 || (edges & 12) == 12)
1183 return 0;
1184
1185 resize = malloc(sizeof *resize);
1186 if (!resize)
1187 return -1;
1188
1189 resize->edges = edges;
1190 resize->width = shsurf->surface->geometry.width;
1191 resize->height = shsurf->surface->geometry.height;
1192
1193 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1194 ws->seat.pointer, edges);
1195
1196 return 0;
1197}
1198
1199static void
1200shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1201 struct wl_resource *seat_resource, uint32_t serial,
1202 uint32_t edges)
1203{
1204 struct weston_seat *ws = seat_resource->data;
1205 struct shell_surface *shsurf = resource->data;
1206
1207 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1208 return;
1209
1210 if (ws->seat.pointer->button_count == 0 ||
1211 ws->seat.pointer->grab_serial != serial ||
1212 ws->seat.pointer->focus != &shsurf->surface->surface)
1213 return;
1214
1215 if (surface_resize(shsurf, ws, edges) < 0)
1216 wl_resource_post_no_memory(resource);
1217}
1218
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001219static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001220busy_cursor_grab_focus(struct wl_pointer_grab *base,
1221 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001222{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001223 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001224
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001225 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001226 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001227 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001228 }
1229}
1230
1231static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001232busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1233 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001234{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001235}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001236
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001237static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001238busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001239 uint32_t time, uint32_t button, uint32_t state)
1240{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001241 struct shell_grab *grab = (struct shell_grab *) base;
1242 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001243 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001244 (struct weston_surface *) grab->grab.pointer->current;
1245 struct weston_seat *seat =
1246 (struct weston_seat *) grab->grab.pointer->seat;
1247
1248 shsurf = get_shell_surface(surface);
1249 if (shsurf && button == BTN_LEFT && state) {
1250 activate(shsurf->shell, shsurf->surface, seat);
1251 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001252 } else if (shsurf && button == BTN_RIGHT && state) {
1253 activate(shsurf->shell, shsurf->surface, seat);
1254 surface_rotate(shsurf, &seat->seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001255 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001256}
1257
1258static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1259 busy_cursor_grab_focus,
1260 busy_cursor_grab_motion,
1261 busy_cursor_grab_button,
1262};
1263
1264static void
1265set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1266{
1267 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001268
1269 grab = malloc(sizeof *grab);
1270 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001271 return;
1272
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001273 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1274 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001275}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001276
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001277static void
1278end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1279{
1280 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1281
1282 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001283 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001284 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001285 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001286}
1287
Scott Moreau9521d5e2012-04-19 13:06:17 -06001288static void
1289ping_timer_destroy(struct shell_surface *shsurf)
1290{
1291 if (!shsurf || !shsurf->ping_timer)
1292 return;
1293
1294 if (shsurf->ping_timer->source)
1295 wl_event_source_remove(shsurf->ping_timer->source);
1296
1297 free(shsurf->ping_timer);
1298 shsurf->ping_timer = NULL;
1299}
1300
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001301static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001302ping_timeout_handler(void *data)
1303{
1304 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001305 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001306
Scott Moreau9521d5e2012-04-19 13:06:17 -06001307 /* Client is not responding */
1308 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001309
1310 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1311 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1312 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001313
1314 return 1;
1315}
1316
1317static void
1318ping_handler(struct weston_surface *surface, uint32_t serial)
1319{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001320 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001321 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001322 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001323
1324 if (!shsurf)
1325 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001326 if (!shsurf->resource.client)
1327 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001328
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001329 if (shsurf->surface == shsurf->shell->grab_surface)
1330 return;
1331
Scott Moreauff1db4a2012-04-17 19:06:18 -06001332 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001333 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001334 if (!shsurf->ping_timer)
1335 return;
1336
1337 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001338 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1339 shsurf->ping_timer->source =
1340 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1341 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1342
1343 wl_shell_surface_send_ping(&shsurf->resource, serial);
1344 }
1345}
1346
1347static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001348handle_pointer_focus(struct wl_listener *listener, void *data)
1349{
1350 struct wl_pointer *pointer = data;
1351 struct weston_surface *surface =
1352 (struct weston_surface *) pointer->focus;
1353 struct weston_compositor *compositor;
1354 struct shell_surface *shsurf;
1355 uint32_t serial;
1356
1357 if (!surface)
1358 return;
1359
1360 compositor = surface->compositor;
1361 shsurf = get_shell_surface(surface);
1362
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001363 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001364 set_busy_cursor(shsurf, pointer);
1365 } else {
1366 serial = wl_display_next_serial(compositor->wl_display);
1367 ping_handler(surface, serial);
1368 }
1369}
1370
1371static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001372create_pointer_focus_listener(struct weston_seat *seat)
1373{
1374 struct wl_listener *listener;
1375
1376 if (!seat->seat.pointer)
1377 return;
1378
1379 listener = malloc(sizeof *listener);
1380 listener->notify = handle_pointer_focus;
1381 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1382}
1383
1384static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001385shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1386 uint32_t serial)
1387{
1388 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001389 struct desktop_shell *shell = shsurf->shell;
1390 struct weston_seat *seat;
1391 struct weston_compositor *ec = shsurf->surface->compositor;
1392 struct wl_pointer *pointer;
1393 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001394
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001395 if (shsurf->ping_timer == NULL)
1396 /* Just ignore unsolicited pong. */
1397 return;
1398
Scott Moreauff1db4a2012-04-17 19:06:18 -06001399 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001400 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001401 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001402 if (was_unresponsive) {
1403 /* Received pong from previously unresponsive client */
1404 wl_list_for_each(seat, &ec->seat_list, link) {
1405 pointer = seat->seat.pointer;
1406 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001407 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001408 pointer->current ==
1409 &shsurf->surface->surface)
1410 end_busy_cursor(shsurf, pointer);
1411 }
1412 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001413 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001414 }
1415}
1416
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001417static void
1418shell_surface_set_title(struct wl_client *client,
1419 struct wl_resource *resource, const char *title)
1420{
1421 struct shell_surface *shsurf = resource->data;
1422
1423 free(shsurf->title);
1424 shsurf->title = strdup(title);
1425}
1426
1427static void
1428shell_surface_set_class(struct wl_client *client,
1429 struct wl_resource *resource, const char *class)
1430{
1431 struct shell_surface *shsurf = resource->data;
1432
1433 free(shsurf->class);
1434 shsurf->class = strdup(class);
1435}
1436
Juan Zhao96879df2012-02-07 08:45:41 +08001437static struct weston_output *
1438get_default_output(struct weston_compositor *compositor)
1439{
1440 return container_of(compositor->output_list.next,
1441 struct weston_output, link);
1442}
1443
Alex Wu4539b082012-03-01 12:57:46 +08001444static void
1445shell_unset_fullscreen(struct shell_surface *shsurf)
1446{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001447 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001448 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001449 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1450 shell_surface_is_top_fullscreen(shsurf)) {
1451 weston_output_switch_mode(shsurf->fullscreen_output,
1452 shsurf->fullscreen_output->origin);
1453 }
Alex Wu4539b082012-03-01 12:57:46 +08001454 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1455 shsurf->fullscreen.framerate = 0;
1456 wl_list_remove(&shsurf->fullscreen.transform.link);
1457 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001458 if (shsurf->fullscreen.black_surface)
1459 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001460 shsurf->fullscreen.black_surface = NULL;
1461 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001462 weston_surface_set_position(shsurf->surface,
1463 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001464 if (shsurf->saved_rotation_valid) {
1465 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1466 &shsurf->rotation.transform.link);
1467 shsurf->saved_rotation_valid = false;
1468 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001469
1470 ws = get_current_workspace(shsurf->shell);
1471 wl_list_remove(&shsurf->surface->layer_link);
1472 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001473}
1474
Pekka Paalanen98262232011-12-01 10:42:22 +02001475static int
1476reset_shell_surface_type(struct shell_surface *surface)
1477{
1478 switch (surface->type) {
1479 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001480 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001481 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001482 case SHELL_SURFACE_MAXIMIZED:
1483 surface->output = get_default_output(surface->surface->compositor);
1484 weston_surface_set_position(surface->surface,
1485 surface->saved_x,
1486 surface->saved_y);
1487 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001488 case SHELL_SURFACE_NONE:
1489 case SHELL_SURFACE_TOPLEVEL:
1490 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001491 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001492 break;
1493 }
1494
1495 surface->type = SHELL_SURFACE_NONE;
1496 return 0;
1497}
1498
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001499static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001500set_surface_type(struct shell_surface *shsurf)
1501{
1502 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001503 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001504
1505 reset_shell_surface_type(shsurf);
1506
1507 shsurf->type = shsurf->next_type;
1508 shsurf->next_type = SHELL_SURFACE_NONE;
1509
1510 switch (shsurf->type) {
1511 case SHELL_SURFACE_TOPLEVEL:
1512 break;
1513 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001514 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001515 pes->geometry.x + shsurf->transient.x,
1516 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001517 break;
1518
1519 case SHELL_SURFACE_MAXIMIZED:
1520 shsurf->saved_x = surface->geometry.x;
1521 shsurf->saved_y = surface->geometry.y;
1522 shsurf->saved_position_valid = true;
1523 break;
1524
1525 case SHELL_SURFACE_FULLSCREEN:
1526 shsurf->saved_x = surface->geometry.x;
1527 shsurf->saved_y = surface->geometry.y;
1528 shsurf->saved_position_valid = true;
1529
1530 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1531 wl_list_remove(&shsurf->rotation.transform.link);
1532 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001533 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001534 shsurf->saved_rotation_valid = true;
1535 }
1536 break;
1537
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001538 default:
1539 break;
1540 }
1541}
1542
1543static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001544set_toplevel(struct shell_surface *shsurf)
1545{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001546 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001547}
1548
1549static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001550shell_surface_set_toplevel(struct wl_client *client,
1551 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001552{
Pekka Paalanen98262232011-12-01 10:42:22 +02001553 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001554
Tiago Vignattibc052c92012-04-19 16:18:18 +03001555 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001556}
1557
1558static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001559set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001560 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001561{
1562 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001563 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001564 shsurf->transient.x = x;
1565 shsurf->transient.y = y;
1566 shsurf->transient.flags = flags;
1567 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1568}
1569
1570static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001571shell_surface_set_transient(struct wl_client *client,
1572 struct wl_resource *resource,
1573 struct wl_resource *parent_resource,
1574 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001575{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001576 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001577 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001578
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001579 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001580}
1581
Tiago Vignattibe143262012-04-16 17:31:41 +03001582static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001583shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001584{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001585 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001586}
1587
1588static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001589get_output_panel_height(struct desktop_shell *shell,
1590 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001591{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001592 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001593 int panel_height = 0;
1594
1595 if (!output)
1596 return 0;
1597
Juan Zhao4ab94682012-07-09 22:24:09 -07001598 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001599 if (surface->output == output) {
1600 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001601 break;
1602 }
1603 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001604
Juan Zhao96879df2012-02-07 08:45:41 +08001605 return panel_height;
1606}
1607
1608static void
1609shell_surface_set_maximized(struct wl_client *client,
1610 struct wl_resource *resource,
1611 struct wl_resource *output_resource )
1612{
1613 struct shell_surface *shsurf = resource->data;
1614 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001615 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001616 uint32_t edges = 0, panel_height = 0;
1617
1618 /* get the default output, if the client set it as NULL
1619 check whether the ouput is available */
1620 if (output_resource)
1621 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001622 else if (es->output)
1623 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001624 else
1625 shsurf->output = get_default_output(es->compositor);
1626
Tiago Vignattibe143262012-04-16 17:31:41 +03001627 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001628 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001629 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001630
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001631 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001632 shsurf->output->width,
1633 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001634
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001635 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001636}
1637
Alex Wu21858432012-04-01 20:13:08 +08001638static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001639black_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 +08001640
Alex Wu4539b082012-03-01 12:57:46 +08001641static struct weston_surface *
1642create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001643 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001644 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001645{
1646 struct weston_surface *surface = NULL;
1647
1648 surface = weston_surface_create(ec);
1649 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001650 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001651 return NULL;
1652 }
1653
Alex Wu21858432012-04-01 20:13:08 +08001654 surface->configure = black_surface_configure;
1655 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001656 weston_surface_configure(surface, x, y, w, h);
1657 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001658 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001659 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001660 pixman_region32_fini(&surface->input);
1661 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001662
Alex Wu4539b082012-03-01 12:57:46 +08001663 return surface;
1664}
1665
1666/* Create black surface and append it to the associated fullscreen surface.
1667 * Handle size dismatch and positioning according to the method. */
1668static void
1669shell_configure_fullscreen(struct shell_surface *shsurf)
1670{
1671 struct weston_output *output = shsurf->fullscreen_output;
1672 struct weston_surface *surface = shsurf->surface;
1673 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001674 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001675
Alex Wu4539b082012-03-01 12:57:46 +08001676 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001677 shsurf->fullscreen.black_surface =
1678 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001679 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001680 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001681 output->width,
1682 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001683
1684 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1685 wl_list_insert(&surface->layer_link,
1686 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001687 shsurf->fullscreen.black_surface->output = output;
1688
1689 switch (shsurf->fullscreen.type) {
1690 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001691 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001692 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001693 break;
1694 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001695 /* 1:1 mapping between surface and output dimensions */
1696 if (output->width == surface->geometry.width &&
1697 output->height == surface->geometry.height) {
1698 weston_surface_set_position(surface, output->x, output->y);
1699 break;
1700 }
1701
Alex Wu4539b082012-03-01 12:57:46 +08001702 matrix = &shsurf->fullscreen.transform.matrix;
1703 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001704
Scott Moreau1bad5db2012-08-18 01:04:05 -06001705 output_aspect = (float) output->width /
1706 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001707 surface_aspect = (float) surface->geometry.width /
1708 (float) surface->geometry.height;
1709 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001710 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001711 (float) surface->geometry.width;
1712 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001713 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001714 (float) surface->geometry.height;
1715
Alex Wu4539b082012-03-01 12:57:46 +08001716 weston_matrix_scale(matrix, scale, scale, 1);
1717 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001718 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001719 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001720 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1721 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001722 weston_surface_set_position(surface, x, y);
1723
Alex Wu4539b082012-03-01 12:57:46 +08001724 break;
1725 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001726 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001727 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001728 surface->geometry.width,
1729 surface->geometry.height,
1730 shsurf->fullscreen.framerate};
1731
1732 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001733 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001734 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001735 output->width,
1736 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001737 weston_surface_set_position(surface, output->x, output->y);
1738 break;
1739 }
1740 }
Alex Wu4539b082012-03-01 12:57:46 +08001741 break;
1742 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1743 break;
1744 default:
1745 break;
1746 }
1747}
1748
1749/* make the fullscreen and black surface at the top */
1750static void
1751shell_stack_fullscreen(struct shell_surface *shsurf)
1752{
Alex Wubd3354b2012-04-17 17:20:49 +08001753 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001754 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001755 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001756
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001757 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001758 wl_list_insert(&shell->fullscreen_layer.surface_list,
1759 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001760 weston_surface_damage(surface);
1761
1762 if (!shsurf->fullscreen.black_surface)
1763 shsurf->fullscreen.black_surface =
1764 create_black_surface(surface->compositor,
1765 surface,
1766 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001767 output->width,
1768 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001769
1770 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001771 wl_list_insert(&surface->layer_link,
1772 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001773 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001774}
1775
1776static void
1777shell_map_fullscreen(struct shell_surface *shsurf)
1778{
Alex Wu4539b082012-03-01 12:57:46 +08001779 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001780 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001781}
1782
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001783static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001784set_fullscreen(struct shell_surface *shsurf,
1785 uint32_t method,
1786 uint32_t framerate,
1787 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001788{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001789 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001790
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001791 if (output)
1792 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001793 else if (es->output)
1794 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001795 else
1796 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001797
Alex Wu4539b082012-03-01 12:57:46 +08001798 shsurf->fullscreen_output = shsurf->output;
1799 shsurf->fullscreen.type = method;
1800 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001801 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001802
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001803 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001804 shsurf->output->width,
1805 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001806}
1807
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001808static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001809shell_surface_set_fullscreen(struct wl_client *client,
1810 struct wl_resource *resource,
1811 uint32_t method,
1812 uint32_t framerate,
1813 struct wl_resource *output_resource)
1814{
1815 struct shell_surface *shsurf = resource->data;
1816 struct weston_output *output;
1817
1818 if (output_resource)
1819 output = output_resource->data;
1820 else
1821 output = NULL;
1822
1823 set_fullscreen(shsurf, method, framerate, output);
1824}
1825
1826static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001827popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001828 struct wl_surface *surface,
1829 wl_fixed_t x,
1830 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001831{
Daniel Stone37816df2012-05-16 18:45:18 +01001832 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001833 struct shell_surface *priv =
1834 container_of(grab, struct shell_surface, popup.grab);
1835 struct wl_client *client = priv->surface->surface.resource.client;
1836
Pekka Paalanencb108432012-01-19 16:25:40 +02001837 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001838 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001839 grab->focus = surface;
1840 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001841 wl_pointer_set_focus(pointer, NULL,
1842 wl_fixed_from_int(0),
1843 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001844 grab->focus = NULL;
1845 }
1846}
1847
1848static void
Scott Moreau447013d2012-02-18 05:05:29 -07001849popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001850 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001851{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001852 struct wl_resource *resource;
1853
Daniel Stone37816df2012-05-16 18:45:18 +01001854 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001855 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001856 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001857}
1858
1859static void
Scott Moreau447013d2012-02-18 05:05:29 -07001860popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001861 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001862{
1863 struct wl_resource *resource;
1864 struct shell_surface *shsurf =
1865 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001866 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001867 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001868 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001869
Daniel Stone37816df2012-05-16 18:45:18 +01001870 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001871 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001872 display = wl_client_get_display(resource->client);
1873 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001874 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001875 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001876 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001877 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001878 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001879 }
1880
Daniel Stone4dbadb12012-05-30 16:31:51 +01001881 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001882 shsurf->popup.initial_up = 1;
1883}
1884
Scott Moreau447013d2012-02-18 05:05:29 -07001885static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001886 popup_grab_focus,
1887 popup_grab_motion,
1888 popup_grab_button,
1889};
1890
1891static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001892popup_grab_end(struct wl_pointer *pointer)
1893{
1894 struct wl_pointer_grab *grab = pointer->grab;
1895 struct shell_surface *shsurf =
1896 container_of(grab, struct shell_surface, popup.grab);
1897
1898 if (pointer->grab->interface == &popup_grab_interface) {
1899 wl_shell_surface_send_popup_done(&shsurf->resource);
1900 wl_pointer_end_grab(grab->pointer);
1901 shsurf->popup.grab.pointer = NULL;
1902 }
1903}
1904
1905static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001906shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001907{
Daniel Stone37816df2012-05-16 18:45:18 +01001908 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001909 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001910 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001911
Giulio Camuffo8aa16172013-02-18 22:26:01 +01001912 /* Remove the old transform. We don't want to add it twice
1913 * otherwise Weston will go into an infinite loop when going
1914 * through the transforms. */
1915 if (!wl_list_empty(&shsurf->popup.parent_transform.link)) {
1916 wl_list_remove(&shsurf->popup.parent_transform.link);
1917 wl_list_init(&shsurf->popup.parent_transform.link);
1918 }
1919
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001920 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001921 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001922
Pekka Paalanen938269a2012-02-07 14:19:01 +02001923 weston_surface_update_transform(parent);
1924 if (parent->transform.enabled) {
1925 shsurf->popup.parent_transform.matrix =
1926 parent->transform.matrix;
1927 } else {
1928 /* construct x, y translation matrix */
1929 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03001930 shsurf->popup.parent_transform.matrix.type =
1931 WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen938269a2012-02-07 14:19:01 +02001932 shsurf->popup.parent_transform.matrix.d[12] =
1933 parent->geometry.x;
1934 shsurf->popup.parent_transform.matrix.d[13] =
1935 parent->geometry.y;
1936 }
1937 wl_list_insert(es->geometry.transformation_list.prev,
1938 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001939
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001940 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001941 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1942 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001943
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001944 /* We don't require the grab to still be active, but if another
1945 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001946 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1947 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001948 } else {
1949 wl_shell_surface_send_popup_done(&shsurf->resource);
1950 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001951}
1952
1953static void
1954shell_surface_set_popup(struct wl_client *client,
1955 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001956 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001957 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001958 struct wl_resource *parent_resource,
1959 int32_t x, int32_t y, uint32_t flags)
1960{
1961 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001962
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001963 shsurf->type = SHELL_SURFACE_POPUP;
1964 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001965 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001966 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001967 shsurf->popup.x = x;
1968 shsurf->popup.y = y;
1969}
1970
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001971static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001972 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001973 shell_surface_move,
1974 shell_surface_resize,
1975 shell_surface_set_toplevel,
1976 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001977 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001978 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001979 shell_surface_set_maximized,
1980 shell_surface_set_title,
1981 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001982};
1983
1984static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001985destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001986{
Daniel Stone37816df2012-05-16 18:45:18 +01001987 if (shsurf->popup.grab.pointer)
1988 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001989
Alex Wubd3354b2012-04-17 17:20:49 +08001990 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1991 shell_surface_is_top_fullscreen(shsurf)) {
1992 weston_output_switch_mode(shsurf->fullscreen_output,
1993 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001994 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001995
Alex Wuaa08e2d2012-03-05 11:01:40 +08001996 if (shsurf->fullscreen.black_surface)
1997 weston_surface_destroy(shsurf->fullscreen.black_surface);
1998
Alex Wubd3354b2012-04-17 17:20:49 +08001999 /* As destroy_resource() use wl_list_for_each_safe(),
2000 * we can always remove the listener.
2001 */
2002 wl_list_remove(&shsurf->surface_destroy_listener.link);
2003 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002004 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002005 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002006
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002007 wl_list_remove(&shsurf->link);
2008 free(shsurf);
2009}
2010
2011static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002012shell_destroy_shell_surface(struct wl_resource *resource)
2013{
2014 struct shell_surface *shsurf = resource->data;
2015
2016 destroy_shell_surface(shsurf);
2017}
2018
2019static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002020shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002021{
2022 struct shell_surface *shsurf = container_of(listener,
2023 struct shell_surface,
2024 surface_destroy_listener);
2025
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002026 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002027 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002028 } else {
2029 wl_signal_emit(&shsurf->resource.destroy_signal,
2030 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002031 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002032 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002033}
2034
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002035static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002036shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002037
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002038static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002039get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002040{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002041 if (surface->configure == shell_surface_configure)
2042 return surface->private;
2043 else
2044 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002045}
2046
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002047static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002048create_shell_surface(void *shell, struct weston_surface *surface,
2049 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002050{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002051 struct shell_surface *shsurf;
2052
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002053 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002054 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002055 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002056 }
2057
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002058 shsurf = calloc(1, sizeof *shsurf);
2059 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002060 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002061 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002062 }
2063
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002064 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002065 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002066
Tiago Vignattibc052c92012-04-19 16:18:18 +03002067 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002068 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002069 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002070 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002071 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002072 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2073 shsurf->fullscreen.framerate = 0;
2074 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002075 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002076 wl_list_init(&shsurf->fullscreen.transform.link);
2077
Tiago Vignattibc052c92012-04-19 16:18:18 +03002078 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002079 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2080 wl_signal_add(&surface->surface.resource.destroy_signal,
2081 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002082
2083 /* init link so its safe to always remove it in destroy_shell_surface */
2084 wl_list_init(&shsurf->link);
2085
Pekka Paalanen460099f2012-01-20 16:48:25 +02002086 /* empty when not in use */
2087 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002088 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002089
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002090 wl_list_init(&shsurf->workspace_transform.link);
Giulio Camuffo8aa16172013-02-18 22:26:01 +01002091 wl_list_init(&shsurf->popup.parent_transform.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002092
Pekka Paalanen98262232011-12-01 10:42:22 +02002093 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002094 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002095
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002096 shsurf->client = client;
2097
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002098 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002099}
2100
2101static void
2102shell_get_shell_surface(struct wl_client *client,
2103 struct wl_resource *resource,
2104 uint32_t id,
2105 struct wl_resource *surface_resource)
2106{
2107 struct weston_surface *surface = surface_resource->data;
2108 struct desktop_shell *shell = resource->data;
2109 struct shell_surface *shsurf;
2110
2111 if (get_shell_surface(surface)) {
2112 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002113 WL_DISPLAY_ERROR_INVALID_OBJECT,
2114 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002115 return;
2116 }
2117
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002118 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002119 if (!shsurf) {
2120 wl_resource_post_error(surface_resource,
2121 WL_DISPLAY_ERROR_INVALID_OBJECT,
2122 "surface->configure already set");
2123 return;
2124 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002125
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002126 shsurf->resource.destroy = shell_destroy_shell_surface;
2127 shsurf->resource.object.id = id;
2128 shsurf->resource.object.interface = &wl_shell_surface_interface;
2129 shsurf->resource.object.implementation =
2130 (void (**)(void)) &shell_surface_implementation;
2131 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002132
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002133 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002134}
2135
2136static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002137 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002138};
2139
Kristian Høgsberg07937562011-04-12 17:25:42 -04002140static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002141shell_fade(struct desktop_shell *shell, enum fade_type type);
2142
2143static int
2144screensaver_timeout(void *data)
2145{
2146 struct desktop_shell *shell = data;
2147
2148 shell_fade(shell, FADE_OUT);
2149
2150 return 1;
2151}
2152
2153static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002154handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002155{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002156 struct desktop_shell *shell =
2157 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002158
Pekka Paalanen18027e52011-12-02 16:31:49 +02002159 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002160
2161 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002162 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002163}
2164
2165static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002166launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002167{
2168 if (shell->screensaver.binding)
2169 return;
2170
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002171 if (!shell->screensaver.path) {
2172 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002173 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002174 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002175
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002176 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002177 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002178 return;
2179 }
2180
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002181 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002182 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002183 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002184 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002185}
2186
2187static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002188terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002189{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002190 if (shell->screensaver.process.pid == 0)
2191 return;
2192
2193 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002194}
2195
2196static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002197configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002198{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002199 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002200
Giulio Camuffo184df502013-02-21 11:29:21 +01002201 if (width == 0)
2202 return;
2203
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002204 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2205 if (s->output == es->output && s != es) {
2206 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002207 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002208 }
2209 }
2210
Giulio Camuffo184df502013-02-21 11:29:21 +01002211 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002212
2213 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002214 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002215 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002216 }
2217}
2218
2219static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002220background_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 -04002221{
2222 struct desktop_shell *shell = es->private;
2223
Giulio Camuffo184df502013-02-21 11:29:21 +01002224 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002225}
2226
2227static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002228desktop_shell_set_background(struct wl_client *client,
2229 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002230 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002231 struct wl_resource *surface_resource)
2232{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002233 struct desktop_shell *shell = resource->data;
2234 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002235
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002236 if (surface->configure) {
2237 wl_resource_post_error(surface_resource,
2238 WL_DISPLAY_ERROR_INVALID_OBJECT,
2239 "surface role already assigned");
2240 return;
2241 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002242
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002243 surface->configure = background_configure;
2244 surface->private = shell;
2245 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002246 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002247 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002248 surface->output->width,
2249 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002250}
2251
2252static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002253panel_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 -04002254{
2255 struct desktop_shell *shell = es->private;
2256
Giulio Camuffo184df502013-02-21 11:29:21 +01002257 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002258}
2259
2260static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002261desktop_shell_set_panel(struct wl_client *client,
2262 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002263 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002264 struct wl_resource *surface_resource)
2265{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002266 struct desktop_shell *shell = resource->data;
2267 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002268
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002269 if (surface->configure) {
2270 wl_resource_post_error(surface_resource,
2271 WL_DISPLAY_ERROR_INVALID_OBJECT,
2272 "surface role already assigned");
2273 return;
2274 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002275
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002276 surface->configure = panel_configure;
2277 surface->private = shell;
2278 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002279 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002280 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002281 surface->output->width,
2282 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002283}
2284
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002285static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002286lock_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 -04002287{
2288 struct desktop_shell *shell = surface->private;
2289
Giulio Camuffo184df502013-02-21 11:29:21 +01002290 if (width == 0)
2291 return;
2292
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002293 center_on_output(surface, get_default_output(shell->compositor));
2294
2295 if (!weston_surface_is_mapped(surface)) {
2296 wl_list_insert(&shell->lock_layer.surface_list,
2297 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002298 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002299 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002300 }
2301}
2302
2303static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002304handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002305{
Tiago Vignattibe143262012-04-16 17:31:41 +03002306 struct desktop_shell *shell =
2307 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002308
Martin Minarik6d118362012-06-07 18:01:59 +02002309 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002310 shell->lock_surface = NULL;
2311}
2312
2313static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002314desktop_shell_set_lock_surface(struct wl_client *client,
2315 struct wl_resource *resource,
2316 struct wl_resource *surface_resource)
2317{
Tiago Vignattibe143262012-04-16 17:31:41 +03002318 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002319 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002320
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002321 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002322
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002323 if (!shell->locked)
2324 return;
2325
Pekka Paalanen98262232011-12-01 10:42:22 +02002326 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002327
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002328 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2329 wl_signal_add(&surface_resource->destroy_signal,
2330 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002331
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002332 surface->configure = lock_surface_configure;
2333 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002334}
2335
2336static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002337resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002338{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002339 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002340
Pekka Paalanen77346a62011-11-30 16:26:35 +02002341 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002342
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002343 wl_list_remove(&shell->lock_layer.link);
2344 wl_list_insert(&shell->compositor->cursor_layer.link,
2345 &shell->fullscreen_layer.link);
2346 wl_list_insert(&shell->fullscreen_layer.link,
2347 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002348 if (shell->showing_input_panels) {
2349 wl_list_insert(&shell->panel_layer.link,
2350 &shell->input_panel_layer.link);
2351 wl_list_insert(&shell->input_panel_layer.link,
2352 &ws->layer.link);
2353 } else {
2354 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2355 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002356
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002357 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002358
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002359 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002360 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002361 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002362}
2363
2364static void
2365desktop_shell_unlock(struct wl_client *client,
2366 struct wl_resource *resource)
2367{
Tiago Vignattibe143262012-04-16 17:31:41 +03002368 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002369
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002370 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002371
2372 if (shell->locked)
2373 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002374}
2375
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002376static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002377desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002378 struct wl_resource *resource,
2379 struct wl_resource *surface_resource)
2380{
2381 struct desktop_shell *shell = resource->data;
2382
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002383 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002384}
2385
Kristian Høgsberg75840622011-09-06 13:48:16 -04002386static const struct desktop_shell_interface desktop_shell_implementation = {
2387 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002388 desktop_shell_set_panel,
2389 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002390 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002391 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002392};
2393
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002394static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002395get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002396{
2397 struct shell_surface *shsurf;
2398
2399 shsurf = get_shell_surface(surface);
2400 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002401 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002402 return shsurf->type;
2403}
2404
Kristian Høgsberg75840622011-09-06 13:48:16 -04002405static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002406move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002407{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002408 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002409 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002410 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002411
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002412 if (surface == NULL)
2413 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002414
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002415 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002416 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002417 return;
2418
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002419 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002420}
2421
2422static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002423resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002424{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002425 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002426 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002427 uint32_t edges = 0;
2428 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002429 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002430
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002431 if (surface == NULL)
2432 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002433
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002434 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002435 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002436 return;
2437
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002438 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002439 wl_fixed_to_int(seat->pointer->grab_x),
2440 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002441 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002442
Pekka Paalanen60921e52012-01-25 15:55:43 +02002443 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002444 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002445 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002446 edges |= 0;
2447 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002448 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002449
Pekka Paalanen60921e52012-01-25 15:55:43 +02002450 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002451 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002452 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002453 edges |= 0;
2454 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002455 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002456
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002457 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002458}
2459
2460static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002461surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002462 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002463{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002464 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002465 struct shell_surface *shsurf;
2466 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002467 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002468
2469 if (surface == NULL)
2470 return;
2471
2472 shsurf = get_shell_surface(surface);
2473 if (!shsurf)
2474 return;
2475
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002476 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002477
Scott Moreau02709af2012-05-22 01:54:10 -06002478 if (surface->alpha > 1.0)
2479 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002480 if (surface->alpha < step)
2481 surface->alpha = step;
2482
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002483 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002484 weston_surface_damage(surface);
2485}
2486
2487static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002488do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002489 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002490{
Daniel Stone37816df2012-05-16 18:45:18 +01002491 struct weston_seat *ws = (struct weston_seat *) seat;
2492 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002493 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002494 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002495
2496 wl_list_for_each(output, &compositor->output_list, link) {
2497 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002498 wl_fixed_to_double(seat->pointer->x),
2499 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002500 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002501 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002502 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002503 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002504 increment = -output->zoom.increment;
2505 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002506 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002507 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002508 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002509 else
2510 increment = 0;
2511
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002512 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002513
Scott Moreaue6603982012-06-11 13:07:51 -06002514 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002515 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002516 else if (output->zoom.level > output->zoom.max_level)
2517 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002518 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002519 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002520 output->disable_planes++;
2521 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002522
Scott Moreaue6603982012-06-11 13:07:51 -06002523 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002524
Scott Moreau8dacaab2012-06-17 18:10:58 -06002525 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002526 }
2527 }
2528}
2529
Scott Moreauccbf29d2012-02-22 14:21:41 -07002530static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002531zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002532 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002533{
2534 do_zoom(seat, time, 0, axis, value);
2535}
2536
2537static void
2538zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2539 void *data)
2540{
2541 do_zoom(seat, time, key, 0, 0);
2542}
2543
2544static void
2545terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2546 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002547{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002548 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002549
Daniel Stone325fc2d2012-05-30 16:31:58 +01002550 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002551}
2552
2553static void
Scott Moreau447013d2012-02-18 05:05:29 -07002554rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002555 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002556{
2557 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002558 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002559 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002560 struct shell_surface *shsurf = rotate->base.shsurf;
2561 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002562 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002563
2564 if (!shsurf)
2565 return;
2566
2567 surface = shsurf->surface;
2568
2569 cx = 0.5f * surface->geometry.width;
2570 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002571
Daniel Stone37816df2012-05-16 18:45:18 +01002572 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2573 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002574 r = sqrtf(dx * dx + dy * dy);
2575
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002576 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002577 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002578
2579 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002580 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002581 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002582
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002583 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002584 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002585
2586 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002587 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002588 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002589 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002590 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002591
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002592 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002593 &shsurf->surface->geometry.transformation_list,
2594 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002595 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002596 wl_list_init(&shsurf->rotation.transform.link);
2597 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002598 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002599 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002600
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002601 /* We need to adjust the position of the surface
2602 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002603 cposx = surface->geometry.x + cx;
2604 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002605 dposx = rotate->center.x - cposx;
2606 dposy = rotate->center.y - cposy;
2607 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002608 weston_surface_set_position(surface,
2609 surface->geometry.x + dposx,
2610 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002611 }
2612
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002613 /* Repaint implies weston_surface_update_transform(), which
2614 * lazily applies the damage due to rotation update.
2615 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002616 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002617}
2618
2619static void
Scott Moreau447013d2012-02-18 05:05:29 -07002620rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002621 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002622{
2623 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002624 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002625 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002626 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002627 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002628
Daniel Stone4dbadb12012-05-30 16:31:51 +01002629 if (pointer->button_count == 0 &&
2630 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002631 if (shsurf)
2632 weston_matrix_multiply(&shsurf->rotation.rotation,
2633 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002634 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002635 free(rotate);
2636 }
2637}
2638
Scott Moreau447013d2012-02-18 05:05:29 -07002639static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002640 noop_grab_focus,
2641 rotate_grab_motion,
2642 rotate_grab_button,
2643};
2644
2645static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002646surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002647{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002648 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002649 float dx, dy;
2650 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002651
Pekka Paalanen460099f2012-01-20 16:48:25 +02002652 rotate = malloc(sizeof *rotate);
2653 if (!rotate)
2654 return;
2655
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002656 weston_surface_to_global_float(surface->surface,
2657 surface->surface->geometry.width / 2,
2658 surface->surface->geometry.height / 2,
2659 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002660
Daniel Stone37816df2012-05-16 18:45:18 +01002661 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2662 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002663 r = sqrtf(dx * dx + dy * dy);
2664 if (r > 20.0f) {
2665 struct weston_matrix inverse;
2666
2667 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002668 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002669 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002670
2671 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002672 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002673 } else {
2674 weston_matrix_init(&surface->rotation.rotation);
2675 weston_matrix_init(&rotate->rotation);
2676 }
2677
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002678 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2679 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002680}
2681
2682static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002683rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2684 void *data)
2685{
2686 struct weston_surface *base_surface =
2687 (struct weston_surface *) seat->pointer->focus;
2688 struct shell_surface *surface;
2689
2690 if (base_surface == NULL)
2691 return;
2692
2693 surface = get_shell_surface(base_surface);
2694 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2695 return;
2696
2697 surface_rotate(surface, seat);
2698}
2699
2700static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002701lower_fullscreen_layer(struct desktop_shell *shell)
2702{
2703 struct workspace *ws;
2704 struct weston_surface *surface, *prev;
2705
2706 ws = get_current_workspace(shell);
2707 wl_list_for_each_reverse_safe(surface, prev,
2708 &shell->fullscreen_layer.surface_list,
2709 layer_link)
2710 weston_surface_restack(surface, &ws->layer.surface_list);
2711}
2712
2713static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002714activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002715 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002716{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002717 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002718 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002719
Daniel Stone37816df2012-05-16 18:45:18 +01002720 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002721
Jonas Ådahl8538b222012-08-29 22:13:03 +02002722 state = ensure_focus_state(shell, seat);
2723 if (state == NULL)
2724 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002725
2726 state->keyboard_focus = es;
2727 wl_list_remove(&state->surface_destroy_listener.link);
2728 wl_signal_add(&es->surface.resource.destroy_signal,
2729 &state->surface_destroy_listener);
2730
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002731 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002732 case SHELL_SURFACE_FULLSCREEN:
2733 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002734 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002735 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002736 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002737 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002738 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002739 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002740 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002741 }
2742}
2743
Alex Wu21858432012-04-01 20:13:08 +08002744/* no-op func for checking black surface */
2745static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002746black_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 +08002747{
2748}
2749
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002750static bool
Alex Wu21858432012-04-01 20:13:08 +08002751is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2752{
2753 if (es->configure == black_surface_configure) {
2754 if (fs_surface)
2755 *fs_surface = (struct weston_surface *)es->private;
2756 return true;
2757 }
2758 return false;
2759}
2760
Kristian Høgsberg75840622011-09-06 13:48:16 -04002761static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002762click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002763 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002764{
Daniel Stone37816df2012-05-16 18:45:18 +01002765 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002766 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002767 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002768 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002769
Daniel Stone37816df2012-05-16 18:45:18 +01002770 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002771 if (!focus)
2772 return;
2773
Alex Wu21858432012-04-01 20:13:08 +08002774 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002775 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002776
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002777 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2778 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002779
Daniel Stone325fc2d2012-05-30 16:31:58 +01002780 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002781 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002782}
2783
2784static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002785lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002786{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002787 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002788
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002789 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002790 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002791 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002792 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002793
2794 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002795
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002796 /* Hide all surfaces by removing the fullscreen, panel and
2797 * toplevel layers. This way nothing else can show or receive
2798 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002799
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002800 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002801 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002802 if (shell->showing_input_panels)
2803 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002804 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002805 wl_list_insert(&shell->compositor->cursor_layer.link,
2806 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002807
Pekka Paalanen77346a62011-11-30 16:26:35 +02002808 launch_screensaver(shell);
2809
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002810 /* TODO: disable bindings that should not work while locked. */
2811
2812 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002813}
2814
2815static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002816unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002817{
Pekka Paalanend81c2162011-11-16 13:47:34 +02002818 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002819 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002820 return;
2821 }
2822
2823 /* If desktop-shell client has gone away, unlock immediately. */
2824 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002825 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002826 return;
2827 }
2828
2829 if (shell->prepare_event_sent)
2830 return;
2831
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002832 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002833 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002834}
2835
2836static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002837shell_fade_done(struct weston_surface_animation *animation, void *data)
2838{
2839 struct desktop_shell *shell = data;
2840
2841 shell->fade.animation = NULL;
2842
2843 switch (shell->fade.type) {
2844 case FADE_IN:
2845 weston_surface_destroy(shell->fade.surface);
2846 shell->fade.surface = NULL;
2847 break;
2848 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002849 lock(shell);
2850 break;
2851 }
2852}
2853
2854static void
2855shell_fade(struct desktop_shell *shell, enum fade_type type)
2856{
2857 struct weston_compositor *compositor = shell->compositor;
2858 struct weston_surface *surface;
2859 float tint;
2860
2861 switch (type) {
2862 case FADE_IN:
2863 tint = 0.0;
2864 break;
2865 case FADE_OUT:
2866 tint = 1.0;
2867 break;
2868 default:
2869 weston_log("shell: invalid fade type\n");
2870 return;
2871 }
2872
2873 shell->fade.type = type;
2874
2875 if (shell->fade.surface == NULL) {
2876 surface = weston_surface_create(compositor);
2877 if (!surface)
2878 return;
2879
2880 weston_surface_configure(surface, 0, 0, 8192, 8192);
2881 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2882 surface->alpha = 1.0 - tint;
2883 wl_list_insert(&compositor->fade_layer.surface_list,
2884 &surface->layer_link);
2885 weston_surface_update_transform(surface);
2886 shell->fade.surface = surface;
2887 pixman_region32_init(&surface->input);
2888 }
2889
2890 if (shell->fade.animation)
2891 weston_fade_update(shell->fade.animation,
2892 shell->fade.surface->alpha, tint, 30.0);
2893 else
2894 shell->fade.animation =
2895 weston_fade_run(shell->fade.surface,
2896 1.0 - tint, tint, 30.0,
2897 shell_fade_done, shell);
2898}
2899
2900static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002901idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002902{
2903 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002904 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002905
2906 shell_fade(shell, FADE_OUT);
2907 /* lock() is called from shell_fade_done() */
2908}
2909
2910static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002911wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002912{
2913 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002914 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002915
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002916 unlock(shell);
2917}
2918
2919static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002920show_input_panels(struct wl_listener *listener, void *data)
2921{
2922 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002923 container_of(listener, struct desktop_shell,
2924 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002925 struct input_panel_surface *surface, *next;
2926 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002927
Jan Arne Petersen451a9712013-02-11 15:10:11 +01002928 if (shell->showing_input_panels)
2929 return;
2930
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002931 shell->showing_input_panels = true;
2932
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002933 if (!shell->locked)
2934 wl_list_insert(&shell->panel_layer.link,
2935 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002936
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002937 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002938 &shell->input_panel.surfaces, link) {
2939 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002940 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002941 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002942 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002943 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002944 weston_surface_damage(ws);
2945 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002946 }
2947}
2948
2949static void
2950hide_input_panels(struct wl_listener *listener, void *data)
2951{
2952 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002953 container_of(listener, struct desktop_shell,
2954 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002955 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002956
Jan Arne Petersen61381972013-01-31 15:52:21 +01002957 if (!shell->showing_input_panels)
2958 return;
2959
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002960 shell->showing_input_panels = false;
2961
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002962 if (!shell->locked)
2963 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002964
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002965 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002966 &shell->input_panel_layer.surface_list, layer_link)
2967 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002968}
2969
2970static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002971center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002972{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002973 int32_t width = weston_surface_buffer_width(surface);
2974 int32_t height = weston_surface_buffer_height(surface);
2975 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002976
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002977 x = output->x + (output->width - width) / 2;
2978 y = output->y + (output->height - height) / 2;
2979
2980 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002981}
2982
2983static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002984weston_surface_set_initial_position (struct weston_surface *surface,
2985 struct desktop_shell *shell)
2986{
2987 struct weston_compositor *compositor = shell->compositor;
2988 int ix = 0, iy = 0;
2989 int range_x, range_y;
2990 int dx, dy, x, y, panel_height;
2991 struct weston_output *output, *target_output = NULL;
2992 struct weston_seat *seat;
2993
2994 /* As a heuristic place the new window on the same output as the
2995 * pointer. Falling back to the output containing 0, 0.
2996 *
2997 * TODO: Do something clever for touch too?
2998 */
2999 wl_list_for_each(seat, &compositor->seat_list, link) {
3000 if (seat->has_pointer) {
3001 ix = wl_fixed_to_int(seat->pointer.x);
3002 iy = wl_fixed_to_int(seat->pointer.y);
3003 break;
3004 }
3005 }
3006
3007 wl_list_for_each(output, &compositor->output_list, link) {
3008 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3009 target_output = output;
3010 break;
3011 }
3012 }
3013
3014 if (!target_output) {
3015 weston_surface_set_position(surface, 10 + random() % 400,
3016 10 + random() % 400);
3017 return;
3018 }
3019
3020 /* Valid range within output where the surface will still be onscreen.
3021 * If this is negative it means that the surface is bigger than
3022 * output.
3023 */
3024 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003025 range_x = target_output->width - surface->geometry.width;
3026 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003027 surface->geometry.height;
3028
Rob Bradford4cb88c72012-08-13 15:18:44 +01003029 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003030 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003031 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003032 dx = 0;
3033
3034 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003035 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003036 else
3037 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003038
3039 x = target_output->x + dx;
3040 y = target_output->y + dy;
3041
3042 weston_surface_set_position (surface, x, y);
3043}
3044
3045static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003046map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003047 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003048{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003049 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003050 struct shell_surface *shsurf = get_shell_surface(surface);
3051 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003052 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003053 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003054 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003055 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003056
Pekka Paalanen60921e52012-01-25 15:55:43 +02003057 surface->geometry.width = width;
3058 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003059 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003060
3061 /* initial positioning, see also configure() */
3062 switch (surface_type) {
3063 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003064 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003065 break;
Alex Wu4539b082012-03-01 12:57:46 +08003066 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003067 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003068 shell_map_fullscreen(shsurf);
3069 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003070 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003071 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003072 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01003073 weston_surface_set_position(surface, shsurf->output->x,
3074 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08003075 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003076 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003077 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003078 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003079 case SHELL_SURFACE_NONE:
3080 weston_surface_set_position(surface,
3081 surface->geometry.x + sx,
3082 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003083 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003084 default:
3085 ;
3086 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003087
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003088 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003089 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003090 case SHELL_SURFACE_POPUP:
3091 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003092 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003093 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3094 break;
Alex Wu4539b082012-03-01 12:57:46 +08003095 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003096 case SHELL_SURFACE_NONE:
3097 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003098 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003099 ws = get_current_workspace(shell);
3100 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003101 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003102 }
3103
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003104 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003105 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003106 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3107 surface->output = shsurf->output;
3108 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003109
Juan Zhao7bb92f02011-12-15 11:31:51 -05003110 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003111 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003112 if (shsurf->transient.flags ==
3113 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3114 break;
3115 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003116 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003117 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003118 if (!shell->locked) {
3119 wl_list_for_each(seat, &compositor->seat_list, link)
3120 activate(shell, surface, seat);
3121 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003122 break;
3123 default:
3124 break;
3125 }
3126
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003127 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003128 {
3129 switch (shell->win_animation_type) {
3130 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003131 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003132 break;
3133 case ANIMATION_ZOOM:
3134 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3135 break;
3136 default:
3137 break;
3138 }
3139 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003140}
3141
3142static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003143configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003144 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003145{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003146 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3147 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003148
Pekka Paalanen77346a62011-11-30 16:26:35 +02003149 shsurf = get_shell_surface(surface);
3150 if (shsurf)
3151 surface_type = shsurf->type;
3152
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003153 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003154
3155 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003156 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003157 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003158 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003159 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003160 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003161 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003162 surface->geometry.x = surface->output->x;
3163 surface->geometry.y = surface->output->y +
3164 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003165 break;
Alex Wu4539b082012-03-01 12:57:46 +08003166 case SHELL_SURFACE_TOPLEVEL:
3167 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003168 default:
3169 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003170 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003171
Alex Wu4539b082012-03-01 12:57:46 +08003172 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003173 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003174 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003175
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003176 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003177 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003178 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003179}
3180
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003181static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003182shell_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 +03003183{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003184 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003185 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003186
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003187 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003188
Giulio Camuffo184df502013-02-21 11:29:21 +01003189 if (width == 0)
3190 return;
3191
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003192 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003193 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003194 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003195 type_changed = 1;
3196 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003197
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003198 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003199 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003200 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003201 es->geometry.width != width ||
3202 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003203 float from_x, from_y;
3204 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003205
3206 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3207 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3208 configure(shell, es,
3209 es->geometry.x + to_x - from_x,
3210 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003211 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003212 }
3213}
3214
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003215static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003216
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003217static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003218desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003219{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003220 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003221 struct desktop_shell *shell =
3222 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003223
3224 shell->child.process.pid = 0;
3225 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003226
3227 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3228 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003229 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003230 shell->child.deathstamp = time;
3231 shell->child.deathcount = 0;
3232 }
3233
3234 shell->child.deathcount++;
3235 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003236 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003237 return;
3238 }
3239
Martin Minarik6d118362012-06-07 18:01:59 +02003240 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003241 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003242}
3243
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003244static void
3245launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003246{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003247 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003248 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003249
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003250 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003251 &shell->child.process,
3252 shell_exe,
3253 desktop_shell_sigchld);
3254
3255 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003256 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003257}
3258
3259static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003260bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3261{
Tiago Vignattibe143262012-04-16 17:31:41 +03003262 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003263
3264 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003265 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003266}
3267
Kristian Høgsberg75840622011-09-06 13:48:16 -04003268static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003269unbind_desktop_shell(struct wl_resource *resource)
3270{
Tiago Vignattibe143262012-04-16 17:31:41 +03003271 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003272
3273 if (shell->locked)
3274 resume_desktop(shell);
3275
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003276 shell->child.desktop_shell = NULL;
3277 shell->prepare_event_sent = false;
3278 free(resource);
3279}
3280
3281static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003282bind_desktop_shell(struct wl_client *client,
3283 void *data, uint32_t version, uint32_t id)
3284{
Tiago Vignattibe143262012-04-16 17:31:41 +03003285 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003286 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003287
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003288 resource = wl_client_add_object(client, &desktop_shell_interface,
3289 &desktop_shell_implementation,
3290 id, shell);
3291
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003292 if (client == shell->child.client) {
3293 resource->destroy = unbind_desktop_shell;
3294 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003295 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003296 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003297
3298 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3299 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003300 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003301}
3302
Pekka Paalanen6e168112011-11-24 11:34:05 +02003303static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003304screensaver_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 -04003305{
3306 struct desktop_shell *shell = surface->private;
3307
Giulio Camuffo184df502013-02-21 11:29:21 +01003308 if (width == 0)
3309 return;
3310
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003311 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003312 if (!shell->locked)
3313 return;
3314
3315 center_on_output(surface, surface->output);
3316
3317 if (wl_list_empty(&surface->layer_link)) {
3318 wl_list_insert(shell->lock_layer.surface_list.prev,
3319 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003320 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003321 wl_event_source_timer_update(shell->screensaver.timer,
3322 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003323 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003324 }
3325}
3326
3327static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003328screensaver_set_surface(struct wl_client *client,
3329 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003330 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003331 struct wl_resource *output_resource)
3332{
Tiago Vignattibe143262012-04-16 17:31:41 +03003333 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003334 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003335 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003336
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003337 surface->configure = screensaver_configure;
3338 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003339 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003340}
3341
3342static const struct screensaver_interface screensaver_implementation = {
3343 screensaver_set_surface
3344};
3345
3346static void
3347unbind_screensaver(struct wl_resource *resource)
3348{
Tiago Vignattibe143262012-04-16 17:31:41 +03003349 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003350
Pekka Paalanen77346a62011-11-30 16:26:35 +02003351 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003352 free(resource);
3353}
3354
3355static void
3356bind_screensaver(struct wl_client *client,
3357 void *data, uint32_t version, uint32_t id)
3358{
Tiago Vignattibe143262012-04-16 17:31:41 +03003359 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003360 struct wl_resource *resource;
3361
3362 resource = wl_client_add_object(client, &screensaver_interface,
3363 &screensaver_implementation,
3364 id, shell);
3365
Pekka Paalanen77346a62011-11-30 16:26:35 +02003366 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003367 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003368 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003369 return;
3370 }
3371
3372 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3373 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003374 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003375}
3376
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003377static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003378input_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 -04003379{
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003380 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003381 float x, y;
3382
Giulio Camuffo184df502013-02-21 11:29:21 +01003383 if (width == 0)
3384 return;
3385
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003386 if (!weston_surface_is_mapped(surface))
3387 return;
3388
3389 mode = surface->output->current;
3390 x = (mode->width - width) / 2;
3391 y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003392
3393 /* Don't map the input panel here, wait for
3394 * show_input_panels signal. */
3395
3396 weston_surface_configure(surface,
3397 surface->output->x + x,
3398 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003399 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003400}
3401
3402static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003403destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003404{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003405 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003406 wl_list_remove(&input_panel_surface->link);
3407
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003408 input_panel_surface->surface->configure = NULL;
3409
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003410 free(input_panel_surface);
3411}
3412
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003413static struct input_panel_surface *
3414get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003415{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003416 if (surface->configure == input_panel_configure) {
3417 return surface->private;
3418 } else {
3419 return NULL;
3420 }
3421}
3422
3423static void
3424input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3425{
3426 struct input_panel_surface *ipsurface = container_of(listener,
3427 struct input_panel_surface,
3428 surface_destroy_listener);
3429
3430 if (ipsurface->resource.client) {
3431 wl_resource_destroy(&ipsurface->resource);
3432 } else {
3433 wl_signal_emit(&ipsurface->resource.destroy_signal,
3434 &ipsurface->resource);
3435 destroy_input_panel_surface(ipsurface);
3436 }
3437}
3438static struct input_panel_surface *
3439create_input_panel_surface(struct desktop_shell *shell,
3440 struct weston_surface *surface)
3441{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003442 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003443
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003444 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3445 if (!input_panel_surface)
3446 return NULL;
3447
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003448 surface->configure = input_panel_configure;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003449 surface->private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003450
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003451 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003452
3453 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003454
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003455 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3456 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3457 wl_signal_add(&surface->surface.resource.destroy_signal,
3458 &input_panel_surface->surface_destroy_listener);
3459
3460 wl_list_init(&input_panel_surface->link);
3461
3462 return input_panel_surface;
3463}
3464
3465static void
3466input_panel_surface_set_toplevel(struct wl_client *client,
3467 struct wl_resource *resource,
3468 uint32_t position)
3469{
3470 struct input_panel_surface *input_panel_surface = resource->data;
3471 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003472
3473 wl_list_insert(&shell->input_panel.surfaces,
3474 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003475}
3476
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003477static const struct input_panel_surface_interface input_panel_surface_implementation = {
3478 input_panel_surface_set_toplevel
3479};
3480
3481static void
3482destroy_input_panel_surface_resource(struct wl_resource *resource)
3483{
3484 struct input_panel_surface *ipsurf = resource->data;
3485
3486 destroy_input_panel_surface(ipsurf);
3487}
3488
3489static void
3490input_panel_get_input_panel_surface(struct wl_client *client,
3491 struct wl_resource *resource,
3492 uint32_t id,
3493 struct wl_resource *surface_resource)
3494{
3495 struct weston_surface *surface = surface_resource->data;
3496 struct desktop_shell *shell = resource->data;
3497 struct input_panel_surface *ipsurf;
3498
3499 if (get_input_panel_surface(surface)) {
3500 wl_resource_post_error(surface_resource,
3501 WL_DISPLAY_ERROR_INVALID_OBJECT,
3502 "input_panel::get_input_panel_surface already requested");
3503 return;
3504 }
3505
3506 ipsurf = create_input_panel_surface(shell, surface);
3507 if (!ipsurf) {
3508 wl_resource_post_error(surface_resource,
3509 WL_DISPLAY_ERROR_INVALID_OBJECT,
3510 "surface->configure already set");
3511 return;
3512 }
3513
3514 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3515 ipsurf->resource.object.id = id;
3516 ipsurf->resource.object.interface = &input_panel_surface_interface;
3517 ipsurf->resource.object.implementation =
3518 (void (**)(void)) &input_panel_surface_implementation;
3519 ipsurf->resource.data = ipsurf;
3520
3521 wl_client_add_resource(client, &ipsurf->resource);
3522}
3523
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003524static const struct input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003525 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003526};
3527
3528static void
3529unbind_input_panel(struct wl_resource *resource)
3530{
3531 struct desktop_shell *shell = resource->data;
3532
3533 shell->input_panel.binding = NULL;
3534 free(resource);
3535}
3536
3537static void
3538bind_input_panel(struct wl_client *client,
3539 void *data, uint32_t version, uint32_t id)
3540{
3541 struct desktop_shell *shell = data;
3542 struct wl_resource *resource;
3543
3544 resource = wl_client_add_object(client, &input_panel_interface,
3545 &input_panel_implementation,
3546 id, shell);
3547
3548 if (shell->input_panel.binding == NULL) {
3549 resource->destroy = unbind_input_panel;
3550 shell->input_panel.binding = resource;
3551 return;
3552 }
3553
3554 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3555 "interface object already bound");
3556 wl_resource_destroy(resource);
3557}
3558
Kristian Høgsberg07045392012-02-19 18:52:44 -05003559struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003560 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003561 struct weston_surface *current;
3562 struct wl_listener listener;
3563 struct wl_keyboard_grab grab;
3564};
3565
3566static void
3567switcher_next(struct switcher *switcher)
3568{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003569 struct weston_surface *surface;
3570 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003571 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003572 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003573
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003574 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003575 switch (get_shell_surface_type(surface)) {
3576 case SHELL_SURFACE_TOPLEVEL:
3577 case SHELL_SURFACE_FULLSCREEN:
3578 case SHELL_SURFACE_MAXIMIZED:
3579 if (first == NULL)
3580 first = surface;
3581 if (prev == switcher->current)
3582 next = surface;
3583 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003584 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003585 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003586 weston_surface_damage(surface);
3587 break;
3588 default:
3589 break;
3590 }
Alex Wu1659daa2012-04-01 20:13:09 +08003591
3592 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003593 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003594 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003595 weston_surface_damage(surface);
3596 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003597 }
3598
3599 if (next == NULL)
3600 next = first;
3601
Alex Wu07b26062012-03-12 16:06:01 +08003602 if (next == NULL)
3603 return;
3604
Kristian Høgsberg07045392012-02-19 18:52:44 -05003605 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003606 wl_signal_add(&next->surface.resource.destroy_signal,
3607 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003608
3609 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003610 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003611
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003612 shsurf = get_shell_surface(switcher->current);
3613 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003614 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003615}
3616
3617static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003618switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003619{
3620 struct switcher *switcher =
3621 container_of(listener, struct switcher, listener);
3622
3623 switcher_next(switcher);
3624}
3625
3626static void
Daniel Stone351eb612012-05-31 15:27:47 -04003627switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003628{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003629 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003630 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003631 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003632 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003633
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003634 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003635 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003636 weston_surface_damage(surface);
3637 }
3638
Alex Wu07b26062012-03-12 16:06:01 +08003639 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003640 activate(switcher->shell, switcher->current,
3641 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003642 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003643 wl_keyboard_end_grab(keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003644 if (weston_keyboard->input_method_resource)
3645 keyboard->grab = &weston_keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003646 free(switcher);
3647}
3648
3649static void
3650switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003651 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003652{
3653 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003654 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003655
Daniel Stonec9785ea2012-05-30 16:31:52 +01003656 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003657 switcher_next(switcher);
3658}
3659
3660static void
3661switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3662 uint32_t mods_depressed, uint32_t mods_latched,
3663 uint32_t mods_locked, uint32_t group)
3664{
3665 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003666 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003667
Daniel Stone351eb612012-05-31 15:27:47 -04003668 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3669 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003670}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003671
3672static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003673 switcher_key,
3674 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003675};
3676
3677static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003678switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3679 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003680{
Tiago Vignattibe143262012-04-16 17:31:41 +03003681 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003682 struct switcher *switcher;
3683
3684 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003685 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003686 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003687 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003688 wl_list_init(&switcher->listener.link);
3689
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003690 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003691 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003692 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3693 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003694 switcher_next(switcher);
3695}
3696
Pekka Paalanen3c647232011-12-22 13:43:43 +02003697static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003698backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3699 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003700{
3701 struct weston_compositor *compositor = data;
3702 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003703 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003704
3705 /* TODO: we're limiting to simple use cases, where we assume just
3706 * control on the primary display. We'd have to extend later if we
3707 * ever get support for setting backlights on random desktop LCD
3708 * panels though */
3709 output = get_default_output(compositor);
3710 if (!output)
3711 return;
3712
3713 if (!output->set_backlight)
3714 return;
3715
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003716 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3717 backlight_new = output->backlight_current - 25;
3718 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3719 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003720
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003721 if (backlight_new < 5)
3722 backlight_new = 5;
3723 if (backlight_new > 255)
3724 backlight_new = 255;
3725
3726 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003727 output->set_backlight(output, output->backlight_current);
3728}
3729
3730static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003731fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3732 void *data)
3733{
3734 struct desktop_shell *shell = data;
3735 struct weston_compositor *compositor = shell->compositor;
3736 compositor->fan_debug = !compositor->fan_debug;
3737 weston_compositor_damage_all(compositor);
3738}
3739
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003740struct debug_binding_grab {
3741 struct wl_keyboard_grab grab;
3742 struct weston_seat *seat;
3743 uint32_t key[2];
3744 int key_released[2];
3745};
3746
3747static void
3748debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3749 uint32_t key, uint32_t state)
3750{
3751 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3752 struct wl_resource *resource;
3753 struct wl_display *display;
3754 uint32_t serial;
3755 int send = 0, terminate = 0;
3756 int check_binding = 1;
3757 int i;
3758
3759 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3760 /* Do not run bindings on key releases */
3761 check_binding = 0;
3762
3763 for (i = 0; i < 2; i++)
3764 if (key == db->key[i])
3765 db->key_released[i] = 1;
3766
3767 if (db->key_released[0] && db->key_released[1]) {
3768 /* All key releases been swalled so end the grab */
3769 terminate = 1;
3770 } else if (key != db->key[0] && key != db->key[1]) {
3771 /* Should not swallow release of other keys */
3772 send = 1;
3773 }
3774 } else if (key == db->key[0] && !db->key_released[0]) {
3775 /* Do not check bindings for the first press of the binding
3776 * key. This allows it to be used as a debug shortcut.
3777 * We still need to swallow this event. */
3778 check_binding = 0;
3779 } else if (db->key[1]) {
3780 /* If we already ran a binding don't process another one since
3781 * we can't keep track of all the binding keys that were
3782 * pressed in order to swallow the release events. */
3783 send = 1;
3784 check_binding = 0;
3785 }
3786
3787 if (check_binding) {
3788 struct weston_compositor *ec = db->seat->compositor;
3789
3790 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3791 key, state)) {
3792 /* We ran a binding so swallow the press and keep the
3793 * grab to swallow the released too. */
3794 send = 0;
3795 terminate = 0;
3796 db->key[1] = key;
3797 } else {
3798 /* Terminate the grab since the key pressed is not a
3799 * debug binding key. */
3800 send = 1;
3801 terminate = 1;
3802 }
3803 }
3804
3805 if (send) {
3806 resource = grab->keyboard->focus_resource;
3807
3808 if (resource) {
3809 display = wl_client_get_display(resource->client);
3810 serial = wl_display_next_serial(display);
3811 wl_keyboard_send_key(resource, serial, time, key, state);
3812 }
3813 }
3814
3815 if (terminate) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003816 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003817 wl_keyboard_end_grab(grab->keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003818 if (weston_keyboard->input_method_resource)
3819 grab->keyboard->grab = &weston_keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003820 free(db);
3821 }
3822}
3823
3824static void
3825debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3826 uint32_t mods_depressed, uint32_t mods_latched,
3827 uint32_t mods_locked, uint32_t group)
3828{
3829 struct wl_resource *resource;
3830
3831 resource = grab->keyboard->focus_resource;
3832 if (!resource)
3833 return;
3834
3835 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3836 mods_latched, mods_locked, group);
3837}
3838
3839struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3840 debug_binding_key,
3841 debug_binding_modifiers
3842};
3843
3844static void
3845debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3846{
3847 struct debug_binding_grab *grab;
3848
3849 grab = calloc(1, sizeof *grab);
3850 if (!grab)
3851 return;
3852
3853 grab->seat = (struct weston_seat *) seat;
3854 grab->key[0] = key;
3855 grab->grab.interface = &debug_binding_keyboard_grab;
3856 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3857}
3858
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003859static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003860force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3861 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003862{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003863 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003864 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003865 struct desktop_shell *shell = data;
3866 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003867 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003868
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003869 focus_surface = seat->keyboard->focus;
3870 if (!focus_surface)
3871 return;
3872
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003873 wl_signal_emit(&compositor->kill_signal, focus_surface);
3874
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003875 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003876 wl_client_get_credentials(client, &pid, NULL, NULL);
3877
3878 /* Skip clients that we launched ourselves (the credentials of
3879 * the socketpair is ours) */
3880 if (pid == getpid())
3881 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003882
Daniel Stone325fc2d2012-05-30 16:31:58 +01003883 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003884}
3885
3886static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003887workspace_up_binding(struct wl_seat *seat, uint32_t time,
3888 uint32_t key, void *data)
3889{
3890 struct desktop_shell *shell = data;
3891 unsigned int new_index = shell->workspaces.current;
3892
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003893 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003894 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003895 if (new_index != 0)
3896 new_index--;
3897
3898 change_workspace(shell, new_index);
3899}
3900
3901static void
3902workspace_down_binding(struct wl_seat *seat, uint32_t time,
3903 uint32_t key, void *data)
3904{
3905 struct desktop_shell *shell = data;
3906 unsigned int new_index = shell->workspaces.current;
3907
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003908 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003909 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003910 if (new_index < shell->workspaces.num - 1)
3911 new_index++;
3912
3913 change_workspace(shell, new_index);
3914}
3915
3916static void
3917workspace_f_binding(struct wl_seat *seat, uint32_t time,
3918 uint32_t key, void *data)
3919{
3920 struct desktop_shell *shell = data;
3921 unsigned int new_index;
3922
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003923 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003924 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003925 new_index = key - KEY_F1;
3926 if (new_index >= shell->workspaces.num)
3927 new_index = shell->workspaces.num - 1;
3928
3929 change_workspace(shell, new_index);
3930}
3931
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003932static void
3933workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3934 uint32_t key, void *data)
3935{
3936 struct desktop_shell *shell = data;
3937 unsigned int new_index = shell->workspaces.current;
3938
3939 if (shell->locked)
3940 return;
3941
3942 if (new_index != 0)
3943 new_index--;
3944
3945 take_surface_to_workspace_by_seat(shell, seat, new_index);
3946}
3947
3948static void
3949workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3950 uint32_t key, void *data)
3951{
3952 struct desktop_shell *shell = data;
3953 unsigned int new_index = shell->workspaces.current;
3954
3955 if (shell->locked)
3956 return;
3957
3958 if (new_index < shell->workspaces.num - 1)
3959 new_index++;
3960
3961 take_surface_to_workspace_by_seat(shell, seat, new_index);
3962}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003963
3964static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003965shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003966{
Tiago Vignattibe143262012-04-16 17:31:41 +03003967 struct desktop_shell *shell =
3968 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003969 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003970
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003971 if (shell->child.client)
3972 wl_client_destroy(shell->child.client);
3973
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003974 wl_list_remove(&shell->idle_listener.link);
3975 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003976 wl_list_remove(&shell->show_input_panel_listener.link);
3977 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003978
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003979 wl_array_for_each(ws, &shell->workspaces.array)
3980 workspace_destroy(*ws);
3981 wl_array_release(&shell->workspaces.array);
3982
Pekka Paalanen3c647232011-12-22 13:43:43 +02003983 free(shell->screensaver.path);
3984 free(shell);
3985}
3986
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003987static void
3988shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3989{
3990 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003991 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003992
3993 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003994 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3995 MODIFIER_CTRL | MODIFIER_ALT,
3996 terminate_binding, ec);
3997 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3998 click_to_activate_binding,
3999 shell);
4000 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4001 MODIFIER_SUPER | MODIFIER_ALT,
4002 surface_opacity_binding, NULL);
4003 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4004 MODIFIER_SUPER, zoom_axis_binding,
4005 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004006
4007 /* configurable bindings */
4008 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004009 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4010 zoom_key_binding, NULL);
4011 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4012 zoom_key_binding, NULL);
4013 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4014 shell);
4015 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4016 resize_binding, shell);
4017 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4018 rotate_binding, NULL);
4019 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4020 shell);
4021 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4022 ec);
4023 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4024 backlight_binding, ec);
4025 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4026 ec);
4027 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4028 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004029 weston_compositor_add_key_binding(ec, KEY_K, mod,
4030 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004031 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4032 workspace_up_binding, shell);
4033 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4034 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004035 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4036 workspace_move_surface_up_binding,
4037 shell);
4038 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4039 workspace_move_surface_down_binding,
4040 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004041
4042 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4043 if (shell->workspaces.num > 1) {
4044 num_workspace_bindings = shell->workspaces.num;
4045 if (num_workspace_bindings > 6)
4046 num_workspace_bindings = 6;
4047 for (i = 0; i < num_workspace_bindings; i++)
4048 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4049 workspace_f_binding,
4050 shell);
4051 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004052
4053 /* Debug bindings */
4054 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4055 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004056 weston_compositor_add_debug_binding(ec, KEY_F,
4057 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004058}
4059
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004060WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004061module_init(struct weston_compositor *ec,
4062 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004063{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004064 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004065 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004066 struct workspace **pws;
4067 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004068 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004069
4070 shell = malloc(sizeof *shell);
4071 if (shell == NULL)
4072 return -1;
4073
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004074 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004075 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004076
4077 shell->destroy_listener.notify = shell_destroy;
4078 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004079 shell->idle_listener.notify = idle_handler;
4080 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4081 shell->wake_listener.notify = wake_handler;
4082 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004083 shell->show_input_panel_listener.notify = show_input_panels;
4084 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4085 shell->hide_input_panel_listener.notify = hide_input_panels;
4086 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004087 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004088 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004089 ec->shell_interface.create_shell_surface = create_shell_surface;
4090 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004091 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004092 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004093 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004094 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004095
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004096 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004097
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004098 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4099 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004100 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4101 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004102 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004103
4104 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004105 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004106
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004107 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004108
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004109 for (i = 0; i < shell->workspaces.num; i++) {
4110 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4111 if (pws == NULL)
4112 return -1;
4113
4114 *pws = workspace_create();
4115 if (*pws == NULL)
4116 return -1;
4117 }
4118 activate_workspace(shell, 0);
4119
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004120 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004121 wl_list_init(&shell->workspaces.animation.link);
4122 shell->workspaces.animation.frame = animate_workspace_change_frame;
4123
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004124 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4125 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004126 return -1;
4127
Kristian Høgsberg75840622011-09-06 13:48:16 -04004128 if (wl_display_add_global(ec->wl_display,
4129 &desktop_shell_interface,
4130 shell, bind_desktop_shell) == NULL)
4131 return -1;
4132
Pekka Paalanen6e168112011-11-24 11:34:05 +02004133 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4134 shell, bind_screensaver) == NULL)
4135 return -1;
4136
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004137 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4138 shell, bind_input_panel) == NULL)
4139 return -1;
4140
Jonas Ådahle9d22502012-08-29 22:13:01 +02004141 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4142 shell, bind_workspace_manager) == NULL)
4143 return -1;
4144
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004145 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004146
4147 loop = wl_display_get_event_loop(ec->wl_display);
4148 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004149
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004150 shell->screensaver.timer =
4151 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4152
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004153 wl_list_for_each(seat, &ec->seat_list, link)
4154 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004155
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004156 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004157
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004158 shell_fade(shell, FADE_IN);
4159
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004160 return 0;
4161}