blob: 565d0e26226cab3dc57cb03ea8f27f9e5c049810 [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;
193 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100194 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400195 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500196 } popup;
197
Alex Wu4539b082012-03-01 12:57:46 +0800198 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300199 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400200 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300201 } transient;
202
203 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800204 enum wl_shell_surface_fullscreen_method type;
205 struct weston_transform transform; /* matrix from x, y */
206 uint32_t framerate;
207 struct weston_surface *black_surface;
208 } fullscreen;
209
Scott Moreauff1db4a2012-04-17 19:06:18 -0600210 struct ping_timer *ping_timer;
211
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200212 struct weston_transform workspace_transform;
213
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500214 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500215 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100216 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400217
218 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200219};
220
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300221struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700222 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300223 struct shell_surface *shsurf;
224 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300225 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300226};
227
228struct weston_move_grab {
229 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100230 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500231};
232
Pekka Paalanen460099f2012-01-20 16:48:25 +0200233struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300234 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500235 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200236 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200237 float x;
238 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200239 } center;
240};
241
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400242static void
243activate(struct desktop_shell *shell, struct weston_surface *es,
244 struct weston_seat *seat);
245
246static struct workspace *
247get_current_workspace(struct desktop_shell *shell);
248
Alex Wubd3354b2012-04-17 17:20:49 +0800249static struct shell_surface *
250get_shell_surface(struct weston_surface *surface);
251
252static struct desktop_shell *
253shell_surface_get_shell(struct shell_surface *shsurf);
254
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500255static void
256surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
257
Alex Wubd3354b2012-04-17 17:20:49 +0800258static bool
259shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
260{
261 struct desktop_shell *shell;
262 struct weston_surface *top_fs_es;
263
264 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100265
Alex Wubd3354b2012-04-17 17:20:49 +0800266 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
267 return false;
268
269 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100270 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800271 layer_link);
272 return (shsurf == get_shell_surface(top_fs_es));
273}
274
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500275static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400276destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300277{
278 struct shell_grab *grab;
279
280 grab = container_of(listener, struct shell_grab,
281 shsurf_destroy_listener);
282
283 grab->shsurf = NULL;
284}
285
286static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400287popup_grab_end(struct wl_pointer *pointer);
288
289static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300290shell_grab_start(struct shell_grab *grab,
291 const struct wl_pointer_grab_interface *interface,
292 struct shell_surface *shsurf,
293 struct wl_pointer *pointer,
294 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300295{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300296 struct desktop_shell *shell = shsurf->shell;
297
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400298 popup_grab_end(pointer);
299
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300300 grab->grab.interface = interface;
301 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400302 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
303 wl_signal_add(&shsurf->resource.destroy_signal,
304 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300305
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300306 grab->pointer = pointer;
307 grab->grab.focus = &shsurf->surface->surface;
308
309 wl_pointer_start_grab(pointer, &grab->grab);
310 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
311 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
312 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300313}
314
315static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300316shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300317{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400318 if (grab->shsurf)
319 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300320
321 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322}
323
324static void
Alex Wu4539b082012-03-01 12:57:46 +0800325center_on_output(struct weston_surface *surface,
326 struct weston_output *output);
327
Daniel Stone496ca172012-05-30 16:31:42 +0100328static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300329get_modifier(char *modifier)
330{
331 if (!modifier)
332 return MODIFIER_SUPER;
333
334 if (!strcmp("ctrl", modifier))
335 return MODIFIER_CTRL;
336 else if (!strcmp("alt", modifier))
337 return MODIFIER_ALT;
338 else if (!strcmp("super", modifier))
339 return MODIFIER_SUPER;
340 else
341 return MODIFIER_SUPER;
342}
343
Juan Zhaoe10d2792012-04-25 19:09:52 +0800344static enum animation_type
345get_animation_type(char *animation)
346{
347 if (!animation)
348 return ANIMATION_NONE;
349
350 if (!strcmp("zoom", animation))
351 return ANIMATION_ZOOM;
352 else if (!strcmp("fade", animation))
353 return ANIMATION_FADE;
354 else
355 return ANIMATION_NONE;
356}
357
Alex Wu4539b082012-03-01 12:57:46 +0800358static void
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500359shell_configuration(struct desktop_shell *shell, const char *config_file)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200360{
Pekka Paalanen7296e792011-12-07 16:22:00 +0200361 char *path = NULL;
362 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200363 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300364 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800365 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200366
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400367 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300368 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800369 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200370 { "num-workspaces",
371 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200372 };
373
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400374 struct config_key saver_keys[] = {
375 { "path", CONFIG_KEY_STRING, &path },
376 { "duration", CONFIG_KEY_INTEGER, &duration },
377 };
378
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200379 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400380 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200381 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
382 };
383
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500384 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200385
Pekka Paalanen7296e792011-12-07 16:22:00 +0200386 shell->screensaver.path = path;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200387 shell->screensaver.duration = duration * 1000;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300388 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800389 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200390 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
391}
392
393static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200394focus_state_destroy(struct focus_state *state)
395{
396 wl_list_remove(&state->seat_destroy_listener.link);
397 wl_list_remove(&state->surface_destroy_listener.link);
398 free(state);
399}
400
401static void
402focus_state_seat_destroy(struct wl_listener *listener, void *data)
403{
404 struct focus_state *state = container_of(listener,
405 struct focus_state,
406 seat_destroy_listener);
407
408 wl_list_remove(&state->link);
409 focus_state_destroy(state);
410}
411
412static void
413focus_state_surface_destroy(struct wl_listener *listener, void *data)
414{
415 struct focus_state *state = container_of(listener,
416 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400417 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400418 struct desktop_shell *shell;
419 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200420
Kristian Høgsberge3778222012-07-31 17:29:30 -0400421 next = NULL;
422 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
423 if (surface == state->keyboard_focus)
424 continue;
425
426 next = surface;
427 break;
428 }
429
430 if (next) {
431 shell = state->seat->compositor->shell_interface.shell;
432 activate(shell, next, state->seat);
433 } else {
434 wl_list_remove(&state->link);
435 focus_state_destroy(state);
436 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200437}
438
439static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400440focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200441{
Jonas Ådahl04769742012-06-13 00:01:24 +0200442 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200443
444 state = malloc(sizeof *state);
445 if (state == NULL)
446 return NULL;
447
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400448 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200449 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400450 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200451
452 state->seat_destroy_listener.notify = focus_state_seat_destroy;
453 state->surface_destroy_listener.notify = focus_state_surface_destroy;
454 wl_signal_add(&seat->seat.destroy_signal,
455 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400456 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200457
458 return state;
459}
460
Jonas Ådahl8538b222012-08-29 22:13:03 +0200461static struct focus_state *
462ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
463{
464 struct workspace *ws = get_current_workspace(shell);
465 struct focus_state *state;
466
467 wl_list_for_each(state, &ws->focus_list, link)
468 if (state->seat == seat)
469 break;
470
471 if (&state->link == &ws->focus_list)
472 state = focus_state_create(seat, ws);
473
474 return state;
475}
476
Jonas Ådahl04769742012-06-13 00:01:24 +0200477static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400478restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200479{
480 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200481 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200482
483 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200484 surface = state->keyboard_focus ?
485 &state->keyboard_focus->surface : NULL;
486
487 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200488 }
489}
490
491static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200492replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
493 struct weston_seat *seat)
494{
495 struct focus_state *state;
496 struct wl_surface *surface;
497
498 wl_list_for_each(state, &ws->focus_list, link) {
499 if (state->seat == seat) {
500 surface = seat->seat.keyboard->focus;
501 state->keyboard_focus =
502 (struct weston_surface *) surface;
503 return;
504 }
505 }
506}
507
508static void
509drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
510 struct weston_surface *surface)
511{
512 struct focus_state *state;
513
514 wl_list_for_each(state, &ws->focus_list, link)
515 if (state->keyboard_focus == surface)
516 state->keyboard_focus = NULL;
517}
518
519static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200520workspace_destroy(struct workspace *ws)
521{
Jonas Ådahl04769742012-06-13 00:01:24 +0200522 struct focus_state *state, *next;
523
524 wl_list_for_each_safe(state, next, &ws->focus_list, link)
525 focus_state_destroy(state);
526
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200527 free(ws);
528}
529
Jonas Ådahl04769742012-06-13 00:01:24 +0200530static void
531seat_destroyed(struct wl_listener *listener, void *data)
532{
533 struct weston_seat *seat = data;
534 struct focus_state *state, *next;
535 struct workspace *ws = container_of(listener,
536 struct workspace,
537 seat_destroyed_listener);
538
539 wl_list_for_each_safe(state, next, &ws->focus_list, link)
540 if (state->seat == seat)
541 wl_list_remove(&state->link);
542}
543
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200544static struct workspace *
545workspace_create(void)
546{
547 struct workspace *ws = malloc(sizeof *ws);
548 if (ws == NULL)
549 return NULL;
550
551 weston_layer_init(&ws->layer, NULL);
552
Jonas Ådahl04769742012-06-13 00:01:24 +0200553 wl_list_init(&ws->focus_list);
554 wl_list_init(&ws->seat_destroyed_listener.link);
555 ws->seat_destroyed_listener.notify = seat_destroyed;
556
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200557 return ws;
558}
559
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200560static int
561workspace_is_empty(struct workspace *ws)
562{
563 return wl_list_empty(&ws->layer.surface_list);
564}
565
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200566static struct workspace *
567get_workspace(struct desktop_shell *shell, unsigned int index)
568{
569 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200570 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200571 pws += index;
572 return *pws;
573}
574
575static struct workspace *
576get_current_workspace(struct desktop_shell *shell)
577{
578 return get_workspace(shell, shell->workspaces.current);
579}
580
581static void
582activate_workspace(struct desktop_shell *shell, unsigned int index)
583{
584 struct workspace *ws;
585
586 ws = get_workspace(shell, index);
587 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
588
589 shell->workspaces.current = index;
590}
591
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200592static unsigned int
593get_output_height(struct weston_output *output)
594{
595 return abs(output->region.extents.y1 - output->region.extents.y2);
596}
597
598static void
599surface_translate(struct weston_surface *surface, double d)
600{
601 struct shell_surface *shsurf = get_shell_surface(surface);
602 struct weston_transform *transform;
603
604 transform = &shsurf->workspace_transform;
605 if (wl_list_empty(&transform->link))
606 wl_list_insert(surface->geometry.transformation_list.prev,
607 &shsurf->workspace_transform.link);
608
609 weston_matrix_init(&shsurf->workspace_transform.matrix);
610 weston_matrix_translate(&shsurf->workspace_transform.matrix,
611 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200612 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200613}
614
615static void
616workspace_translate_out(struct workspace *ws, double fraction)
617{
618 struct weston_surface *surface;
619 unsigned int height;
620 double d;
621
622 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
623 height = get_output_height(surface->output);
624 d = height * fraction;
625
626 surface_translate(surface, d);
627 }
628}
629
630static void
631workspace_translate_in(struct workspace *ws, double fraction)
632{
633 struct weston_surface *surface;
634 unsigned int height;
635 double d;
636
637 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
638 height = get_output_height(surface->output);
639
640 if (fraction > 0)
641 d = -(height - height * fraction);
642 else
643 d = height + height * fraction;
644
645 surface_translate(surface, d);
646 }
647}
648
649static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200650broadcast_current_workspace_state(struct desktop_shell *shell)
651{
652 struct wl_resource *resource;
653
654 wl_list_for_each(resource, &shell->workspaces.client_list, link)
655 workspace_manager_send_state(resource,
656 shell->workspaces.current,
657 shell->workspaces.num);
658}
659
660static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200661reverse_workspace_change_animation(struct desktop_shell *shell,
662 unsigned int index,
663 struct workspace *from,
664 struct workspace *to)
665{
666 shell->workspaces.current = index;
667
668 shell->workspaces.anim_to = to;
669 shell->workspaces.anim_from = from;
670 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
671 shell->workspaces.anim_timestamp = 0;
672
Scott Moreau4272e632012-08-13 09:58:41 -0600673 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200674}
675
676static void
677workspace_deactivate_transforms(struct workspace *ws)
678{
679 struct weston_surface *surface;
680 struct shell_surface *shsurf;
681
682 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
683 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200684 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
685 wl_list_remove(&shsurf->workspace_transform.link);
686 wl_list_init(&shsurf->workspace_transform.link);
687 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200688 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200689 }
690}
691
692static void
693finish_workspace_change_animation(struct desktop_shell *shell,
694 struct workspace *from,
695 struct workspace *to)
696{
Scott Moreau4272e632012-08-13 09:58:41 -0600697 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200698
699 wl_list_remove(&shell->workspaces.animation.link);
700 workspace_deactivate_transforms(from);
701 workspace_deactivate_transforms(to);
702 shell->workspaces.anim_to = NULL;
703
704 wl_list_remove(&shell->workspaces.anim_from->layer.link);
705}
706
707static void
708animate_workspace_change_frame(struct weston_animation *animation,
709 struct weston_output *output, uint32_t msecs)
710{
711 struct desktop_shell *shell =
712 container_of(animation, struct desktop_shell,
713 workspaces.animation);
714 struct workspace *from = shell->workspaces.anim_from;
715 struct workspace *to = shell->workspaces.anim_to;
716 uint32_t t;
717 double x, y;
718
719 if (workspace_is_empty(from) && workspace_is_empty(to)) {
720 finish_workspace_change_animation(shell, from, to);
721 return;
722 }
723
724 if (shell->workspaces.anim_timestamp == 0) {
725 if (shell->workspaces.anim_current == 0.0)
726 shell->workspaces.anim_timestamp = msecs;
727 else
728 shell->workspaces.anim_timestamp =
729 msecs -
730 /* Invers of movement function 'y' below. */
731 (asin(1.0 - shell->workspaces.anim_current) *
732 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
733 M_2_PI);
734 }
735
736 t = msecs - shell->workspaces.anim_timestamp;
737
738 /*
739 * x = [0, π/2]
740 * y(x) = sin(x)
741 */
742 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
743 y = sin(x);
744
745 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600746 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200747
748 workspace_translate_out(from, shell->workspaces.anim_dir * y);
749 workspace_translate_in(to, shell->workspaces.anim_dir * y);
750 shell->workspaces.anim_current = y;
751
Scott Moreau4272e632012-08-13 09:58:41 -0600752 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200753 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200754 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200755 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200756}
757
758static void
759animate_workspace_change(struct desktop_shell *shell,
760 unsigned int index,
761 struct workspace *from,
762 struct workspace *to)
763{
764 struct weston_output *output;
765
766 int dir;
767
768 if (index > shell->workspaces.current)
769 dir = -1;
770 else
771 dir = 1;
772
773 shell->workspaces.current = index;
774
775 shell->workspaces.anim_dir = dir;
776 shell->workspaces.anim_from = from;
777 shell->workspaces.anim_to = to;
778 shell->workspaces.anim_current = 0.0;
779 shell->workspaces.anim_timestamp = 0;
780
781 output = container_of(shell->compositor->output_list.next,
782 struct weston_output, link);
783 wl_list_insert(&output->animation_list,
784 &shell->workspaces.animation.link);
785
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200786 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200787
788 workspace_translate_in(to, 0);
789
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400790 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200791
Scott Moreau4272e632012-08-13 09:58:41 -0600792 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200793}
794
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200795static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200796update_workspace(struct desktop_shell *shell, unsigned int index,
797 struct workspace *from, struct workspace *to)
798{
799 shell->workspaces.current = index;
800 wl_list_insert(&from->layer.link, &to->layer.link);
801 wl_list_remove(&from->layer.link);
802}
803
804static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200805change_workspace(struct desktop_shell *shell, unsigned int index)
806{
807 struct workspace *from;
808 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200809
810 if (index == shell->workspaces.current)
811 return;
812
813 /* Don't change workspace when there is any fullscreen surfaces. */
814 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
815 return;
816
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200817 from = get_current_workspace(shell);
818 to = get_workspace(shell, index);
819
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200820 if (shell->workspaces.anim_from == to &&
821 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200822 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200823 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200824 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200825 return;
826 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200827
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200828 if (shell->workspaces.anim_to != NULL)
829 finish_workspace_change_animation(shell,
830 shell->workspaces.anim_from,
831 shell->workspaces.anim_to);
832
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200833 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200834
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200835 if (workspace_is_empty(to) && workspace_is_empty(from))
836 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200837 else
838 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200839
840 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200841}
842
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200843static bool
844workspace_has_only(struct workspace *ws, struct weston_surface *surface)
845{
846 struct wl_list *list = &ws->layer.surface_list;
847 struct wl_list *e;
848
849 if (wl_list_empty(list))
850 return false;
851
852 e = list->next;
853
854 if (e->next != list)
855 return false;
856
857 return container_of(e, struct weston_surface, layer_link) == surface;
858}
859
860static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200861move_surface_to_workspace(struct desktop_shell *shell,
862 struct weston_surface *surface,
863 uint32_t workspace)
864{
865 struct workspace *from;
866 struct workspace *to;
867 struct weston_seat *seat;
868
869 if (workspace == shell->workspaces.current)
870 return;
871
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200872 if (workspace >= shell->workspaces.num)
873 workspace = shell->workspaces.num - 1;
874
Jonas Ådahle9d22502012-08-29 22:13:01 +0200875 from = get_current_workspace(shell);
876 to = get_workspace(shell, workspace);
877
878 wl_list_remove(&surface->layer_link);
879 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
880
881 drop_focus_state(shell, from, surface);
882 wl_list_for_each(seat, &shell->compositor->seat_list, link)
883 if (seat->has_keyboard &&
Jan Arne Petersena75a7892013-01-16 21:26:50 +0100884 seat->keyboard.keyboard.focus == &surface->surface)
885 wl_keyboard_set_focus(&seat->keyboard.keyboard, NULL);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200886
887 weston_surface_damage_below(surface);
888}
889
890static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200891take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200892 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200893 unsigned int index)
894{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200895 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200896 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200897 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200898 struct shell_surface *shsurf;
899 struct workspace *from;
900 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200901 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200902
903 if (surface == NULL ||
904 index == shell->workspaces.current)
905 return;
906
907 from = get_current_workspace(shell);
908 to = get_workspace(shell, index);
909
910 wl_list_remove(&surface->layer_link);
911 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
912
Jonas Ådahle9d22502012-08-29 22:13:01 +0200913 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200914 drop_focus_state(shell, from, surface);
915
916 if (shell->workspaces.anim_from == to &&
917 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200918 wl_list_remove(&to->layer.link);
919 wl_list_insert(from->layer.link.prev, &to->layer.link);
920
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200921 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200922 broadcast_current_workspace_state(shell);
923
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200924 return;
925 }
926
927 if (shell->workspaces.anim_to != NULL)
928 finish_workspace_change_animation(shell,
929 shell->workspaces.anim_from,
930 shell->workspaces.anim_to);
931
932 if (workspace_is_empty(from) &&
933 workspace_has_only(to, surface))
934 update_workspace(shell, index, from, to);
935 else {
936 shsurf = get_shell_surface(surface);
937 if (wl_list_empty(&shsurf->workspace_transform.link))
938 wl_list_insert(&shell->workspaces.anim_sticky_list,
939 &shsurf->workspace_transform.link);
940
941 animate_workspace_change(shell, index, from, to);
942 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200943
944 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200945
946 state = ensure_focus_state(shell, seat);
947 if (state != NULL)
948 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200949}
950
951static void
952workspace_manager_move_surface(struct wl_client *client,
953 struct wl_resource *resource,
954 struct wl_resource *surface_resource,
955 uint32_t workspace)
956{
957 struct desktop_shell *shell = resource->data;
958 struct weston_surface *surface =
959 (struct weston_surface *) surface_resource;
960
961 move_surface_to_workspace(shell, surface, workspace);
962}
963
964static const struct workspace_manager_interface workspace_manager_implementation = {
965 workspace_manager_move_surface,
966};
967
968static void
969unbind_resource(struct wl_resource *resource)
970{
971 wl_list_remove(&resource->link);
972 free(resource);
973}
974
975static void
976bind_workspace_manager(struct wl_client *client,
977 void *data, uint32_t version, uint32_t id)
978{
979 struct desktop_shell *shell = data;
980 struct wl_resource *resource;
981
982 resource = wl_client_add_object(client, &workspace_manager_interface,
983 &workspace_manager_implementation,
984 id, shell);
985
986 if (resource == NULL) {
987 weston_log("couldn't add workspace manager object");
988 return;
989 }
990
991 resource->destroy = unbind_resource;
992 wl_list_insert(&shell->workspaces.client_list, &resource->link);
993
994 workspace_manager_send_state(resource,
995 shell->workspaces.current,
996 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200997}
998
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200999static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001000noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001001 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001002{
1003 grab->focus = NULL;
1004}
1005
1006static void
Scott Moreau447013d2012-02-18 05:05:29 -07001007move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001008 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001009{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001010 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001011 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001012 struct shell_surface *shsurf = move->base.shsurf;
1013 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001014 int dx = wl_fixed_to_int(pointer->x + move->dx);
1015 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001016
1017 if (!shsurf)
1018 return;
1019
1020 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001021
Daniel Stone103db7f2012-05-08 17:17:55 +01001022 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001023 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001024
1025 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001026}
1027
1028static void
Scott Moreau447013d2012-02-18 05:05:29 -07001029move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001030 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001031{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001032 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1033 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001034 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001035 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001036
Daniel Stone4dbadb12012-05-30 16:31:51 +01001037 if (pointer->button_count == 0 &&
1038 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001039 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001040 free(grab);
1041 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001042}
1043
Scott Moreau447013d2012-02-18 05:05:29 -07001044static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001045 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001046 move_grab_motion,
1047 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001048};
1049
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001050static int
1051surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1052{
1053 struct weston_move_grab *move;
1054
1055 if (!shsurf)
1056 return -1;
1057
1058 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1059 return 0;
1060
1061 move = malloc(sizeof *move);
1062 if (!move)
1063 return -1;
1064
1065 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1066 ws->seat.pointer->grab_x;
1067 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1068 ws->seat.pointer->grab_y;
1069
1070 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1071 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1072
1073 return 0;
1074}
1075
1076static void
1077shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1078 struct wl_resource *seat_resource, uint32_t serial)
1079{
1080 struct weston_seat *ws = seat_resource->data;
1081 struct shell_surface *shsurf = resource->data;
1082
1083 if (ws->seat.pointer->button_count == 0 ||
1084 ws->seat.pointer->grab_serial != serial ||
1085 ws->seat.pointer->focus != &shsurf->surface->surface)
1086 return;
1087
1088 if (surface_move(shsurf, ws) < 0)
1089 wl_resource_post_no_memory(resource);
1090}
1091
1092struct weston_resize_grab {
1093 struct shell_grab base;
1094 uint32_t edges;
1095 int32_t width, height;
1096};
1097
1098static void
1099resize_grab_motion(struct wl_pointer_grab *grab,
1100 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1101{
1102 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1103 struct wl_pointer *pointer = grab->pointer;
1104 struct shell_surface *shsurf = resize->base.shsurf;
1105 int32_t width, height;
1106 wl_fixed_t from_x, from_y;
1107 wl_fixed_t to_x, to_y;
1108
1109 if (!shsurf)
1110 return;
1111
1112 weston_surface_from_global_fixed(shsurf->surface,
1113 pointer->grab_x, pointer->grab_y,
1114 &from_x, &from_y);
1115 weston_surface_from_global_fixed(shsurf->surface,
1116 pointer->x, pointer->y, &to_x, &to_y);
1117
1118 width = resize->width;
1119 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1120 width += wl_fixed_to_int(from_x - to_x);
1121 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1122 width += wl_fixed_to_int(to_x - from_x);
1123 }
1124
1125 height = resize->height;
1126 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1127 height += wl_fixed_to_int(from_y - to_y);
1128 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1129 height += wl_fixed_to_int(to_y - from_y);
1130 }
1131
1132 shsurf->client->send_configure(shsurf->surface,
1133 resize->edges, width, height);
1134}
1135
1136static void
1137send_configure(struct weston_surface *surface,
1138 uint32_t edges, int32_t width, int32_t height)
1139{
1140 struct shell_surface *shsurf = get_shell_surface(surface);
1141
1142 wl_shell_surface_send_configure(&shsurf->resource,
1143 edges, width, height);
1144}
1145
1146static const struct weston_shell_client shell_client = {
1147 send_configure
1148};
1149
1150static void
1151resize_grab_button(struct wl_pointer_grab *grab,
1152 uint32_t time, uint32_t button, uint32_t state_w)
1153{
1154 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1155 struct wl_pointer *pointer = grab->pointer;
1156 enum wl_pointer_button_state state = state_w;
1157
1158 if (pointer->button_count == 0 &&
1159 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1160 shell_grab_end(&resize->base);
1161 free(grab);
1162 }
1163}
1164
1165static const struct wl_pointer_grab_interface resize_grab_interface = {
1166 noop_grab_focus,
1167 resize_grab_motion,
1168 resize_grab_button,
1169};
1170
1171static int
1172surface_resize(struct shell_surface *shsurf,
1173 struct weston_seat *ws, uint32_t edges)
1174{
1175 struct weston_resize_grab *resize;
1176
1177 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1178 return 0;
1179
1180 if (edges == 0 || edges > 15 ||
1181 (edges & 3) == 3 || (edges & 12) == 12)
1182 return 0;
1183
1184 resize = malloc(sizeof *resize);
1185 if (!resize)
1186 return -1;
1187
1188 resize->edges = edges;
1189 resize->width = shsurf->surface->geometry.width;
1190 resize->height = shsurf->surface->geometry.height;
1191
1192 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1193 ws->seat.pointer, edges);
1194
1195 return 0;
1196}
1197
1198static void
1199shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1200 struct wl_resource *seat_resource, uint32_t serial,
1201 uint32_t edges)
1202{
1203 struct weston_seat *ws = seat_resource->data;
1204 struct shell_surface *shsurf = resource->data;
1205
1206 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1207 return;
1208
1209 if (ws->seat.pointer->button_count == 0 ||
1210 ws->seat.pointer->grab_serial != serial ||
1211 ws->seat.pointer->focus != &shsurf->surface->surface)
1212 return;
1213
1214 if (surface_resize(shsurf, ws, edges) < 0)
1215 wl_resource_post_no_memory(resource);
1216}
1217
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001218static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001219busy_cursor_grab_focus(struct wl_pointer_grab *base,
1220 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001221{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001222 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001223
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001224 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001225 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001226 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001227 }
1228}
1229
1230static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001231busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1232 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001233{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001234}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001235
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001236static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001237busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001238 uint32_t time, uint32_t button, uint32_t state)
1239{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001240 struct shell_grab *grab = (struct shell_grab *) base;
1241 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001242 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001243 (struct weston_surface *) grab->grab.pointer->current;
1244 struct weston_seat *seat =
1245 (struct weston_seat *) grab->grab.pointer->seat;
1246
1247 shsurf = get_shell_surface(surface);
1248 if (shsurf && button == BTN_LEFT && state) {
1249 activate(shsurf->shell, shsurf->surface, seat);
1250 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001251 } else if (shsurf && button == BTN_RIGHT && state) {
1252 activate(shsurf->shell, shsurf->surface, seat);
1253 surface_rotate(shsurf, &seat->seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001254 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001255}
1256
1257static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1258 busy_cursor_grab_focus,
1259 busy_cursor_grab_motion,
1260 busy_cursor_grab_button,
1261};
1262
1263static void
1264set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1265{
1266 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001267
1268 grab = malloc(sizeof *grab);
1269 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001270 return;
1271
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001272 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1273 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001274}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001275
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001276static void
1277end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1278{
1279 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1280
1281 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001282 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001283 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001284 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001285}
1286
Scott Moreau9521d5e2012-04-19 13:06:17 -06001287static void
1288ping_timer_destroy(struct shell_surface *shsurf)
1289{
1290 if (!shsurf || !shsurf->ping_timer)
1291 return;
1292
1293 if (shsurf->ping_timer->source)
1294 wl_event_source_remove(shsurf->ping_timer->source);
1295
1296 free(shsurf->ping_timer);
1297 shsurf->ping_timer = NULL;
1298}
1299
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001300static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001301ping_timeout_handler(void *data)
1302{
1303 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001304 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001305
Scott Moreau9521d5e2012-04-19 13:06:17 -06001306 /* Client is not responding */
1307 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001308
1309 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1310 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1311 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001312
1313 return 1;
1314}
1315
1316static void
1317ping_handler(struct weston_surface *surface, uint32_t serial)
1318{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001319 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001320 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001321 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001322
1323 if (!shsurf)
1324 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001325 if (!shsurf->resource.client)
1326 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001327
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001328 if (shsurf->surface == shsurf->shell->grab_surface)
1329 return;
1330
Scott Moreauff1db4a2012-04-17 19:06:18 -06001331 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001332 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001333 if (!shsurf->ping_timer)
1334 return;
1335
1336 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001337 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1338 shsurf->ping_timer->source =
1339 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1340 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1341
1342 wl_shell_surface_send_ping(&shsurf->resource, serial);
1343 }
1344}
1345
1346static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001347handle_pointer_focus(struct wl_listener *listener, void *data)
1348{
1349 struct wl_pointer *pointer = data;
1350 struct weston_surface *surface =
1351 (struct weston_surface *) pointer->focus;
1352 struct weston_compositor *compositor;
1353 struct shell_surface *shsurf;
1354 uint32_t serial;
1355
1356 if (!surface)
1357 return;
1358
1359 compositor = surface->compositor;
1360 shsurf = get_shell_surface(surface);
1361
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001362 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001363 set_busy_cursor(shsurf, pointer);
1364 } else {
1365 serial = wl_display_next_serial(compositor->wl_display);
1366 ping_handler(surface, serial);
1367 }
1368}
1369
1370static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001371create_pointer_focus_listener(struct weston_seat *seat)
1372{
1373 struct wl_listener *listener;
1374
1375 if (!seat->seat.pointer)
1376 return;
1377
1378 listener = malloc(sizeof *listener);
1379 listener->notify = handle_pointer_focus;
1380 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1381}
1382
1383static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001384shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1385 uint32_t serial)
1386{
1387 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001388 struct desktop_shell *shell = shsurf->shell;
1389 struct weston_seat *seat;
1390 struct weston_compositor *ec = shsurf->surface->compositor;
1391 struct wl_pointer *pointer;
1392 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001393
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001394 if (shsurf->ping_timer == NULL)
1395 /* Just ignore unsolicited pong. */
1396 return;
1397
Scott Moreauff1db4a2012-04-17 19:06:18 -06001398 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001399 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001400 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001401 if (was_unresponsive) {
1402 /* Received pong from previously unresponsive client */
1403 wl_list_for_each(seat, &ec->seat_list, link) {
1404 pointer = seat->seat.pointer;
1405 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001406 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001407 pointer->current ==
1408 &shsurf->surface->surface)
1409 end_busy_cursor(shsurf, pointer);
1410 }
1411 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001412 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001413 }
1414}
1415
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001416static void
1417shell_surface_set_title(struct wl_client *client,
1418 struct wl_resource *resource, const char *title)
1419{
1420 struct shell_surface *shsurf = resource->data;
1421
1422 free(shsurf->title);
1423 shsurf->title = strdup(title);
1424}
1425
1426static void
1427shell_surface_set_class(struct wl_client *client,
1428 struct wl_resource *resource, const char *class)
1429{
1430 struct shell_surface *shsurf = resource->data;
1431
1432 free(shsurf->class);
1433 shsurf->class = strdup(class);
1434}
1435
Juan Zhao96879df2012-02-07 08:45:41 +08001436static struct weston_output *
1437get_default_output(struct weston_compositor *compositor)
1438{
1439 return container_of(compositor->output_list.next,
1440 struct weston_output, link);
1441}
1442
Alex Wu4539b082012-03-01 12:57:46 +08001443static void
1444shell_unset_fullscreen(struct shell_surface *shsurf)
1445{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001446 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001447 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001448 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1449 shell_surface_is_top_fullscreen(shsurf)) {
1450 weston_output_switch_mode(shsurf->fullscreen_output,
1451 shsurf->fullscreen_output->origin);
1452 }
Alex Wu4539b082012-03-01 12:57:46 +08001453 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1454 shsurf->fullscreen.framerate = 0;
1455 wl_list_remove(&shsurf->fullscreen.transform.link);
1456 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001457 if (shsurf->fullscreen.black_surface)
1458 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001459 shsurf->fullscreen.black_surface = NULL;
1460 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001461 weston_surface_set_position(shsurf->surface,
1462 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001463 if (shsurf->saved_rotation_valid) {
1464 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1465 &shsurf->rotation.transform.link);
1466 shsurf->saved_rotation_valid = false;
1467 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001468
1469 ws = get_current_workspace(shsurf->shell);
1470 wl_list_remove(&shsurf->surface->layer_link);
1471 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001472}
1473
Pekka Paalanen98262232011-12-01 10:42:22 +02001474static int
1475reset_shell_surface_type(struct shell_surface *surface)
1476{
1477 switch (surface->type) {
1478 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001479 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001480 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001481 case SHELL_SURFACE_MAXIMIZED:
1482 surface->output = get_default_output(surface->surface->compositor);
1483 weston_surface_set_position(surface->surface,
1484 surface->saved_x,
1485 surface->saved_y);
1486 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001487 case SHELL_SURFACE_NONE:
1488 case SHELL_SURFACE_TOPLEVEL:
1489 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001490 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001491 break;
1492 }
1493
1494 surface->type = SHELL_SURFACE_NONE;
1495 return 0;
1496}
1497
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001498static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001499set_surface_type(struct shell_surface *shsurf)
1500{
1501 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001502 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001503
1504 reset_shell_surface_type(shsurf);
1505
1506 shsurf->type = shsurf->next_type;
1507 shsurf->next_type = SHELL_SURFACE_NONE;
1508
1509 switch (shsurf->type) {
1510 case SHELL_SURFACE_TOPLEVEL:
1511 break;
1512 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001513 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001514 pes->geometry.x + shsurf->transient.x,
1515 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001516 break;
1517
1518 case SHELL_SURFACE_MAXIMIZED:
1519 shsurf->saved_x = surface->geometry.x;
1520 shsurf->saved_y = surface->geometry.y;
1521 shsurf->saved_position_valid = true;
1522 break;
1523
1524 case SHELL_SURFACE_FULLSCREEN:
1525 shsurf->saved_x = surface->geometry.x;
1526 shsurf->saved_y = surface->geometry.y;
1527 shsurf->saved_position_valid = true;
1528
1529 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1530 wl_list_remove(&shsurf->rotation.transform.link);
1531 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001532 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001533 shsurf->saved_rotation_valid = true;
1534 }
1535 break;
1536
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001537 default:
1538 break;
1539 }
1540}
1541
1542static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001543set_toplevel(struct shell_surface *shsurf)
1544{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001545 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001546}
1547
1548static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001549shell_surface_set_toplevel(struct wl_client *client,
1550 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001551{
Pekka Paalanen98262232011-12-01 10:42:22 +02001552 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001553
Tiago Vignattibc052c92012-04-19 16:18:18 +03001554 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001555}
1556
1557static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001558set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001559 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001560{
1561 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001562 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001563 shsurf->transient.x = x;
1564 shsurf->transient.y = y;
1565 shsurf->transient.flags = flags;
1566 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1567}
1568
1569static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001570shell_surface_set_transient(struct wl_client *client,
1571 struct wl_resource *resource,
1572 struct wl_resource *parent_resource,
1573 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001574{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001575 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001576 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001577
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001578 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001579}
1580
Tiago Vignattibe143262012-04-16 17:31:41 +03001581static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001582shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001583{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001584 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001585}
1586
1587static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001588get_output_panel_height(struct desktop_shell *shell,
1589 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001590{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001591 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001592 int panel_height = 0;
1593
1594 if (!output)
1595 return 0;
1596
Juan Zhao4ab94682012-07-09 22:24:09 -07001597 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001598 if (surface->output == output) {
1599 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001600 break;
1601 }
1602 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001603
Juan Zhao96879df2012-02-07 08:45:41 +08001604 return panel_height;
1605}
1606
1607static void
1608shell_surface_set_maximized(struct wl_client *client,
1609 struct wl_resource *resource,
1610 struct wl_resource *output_resource )
1611{
1612 struct shell_surface *shsurf = resource->data;
1613 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001614 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001615 uint32_t edges = 0, panel_height = 0;
1616
1617 /* get the default output, if the client set it as NULL
1618 check whether the ouput is available */
1619 if (output_resource)
1620 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001621 else if (es->output)
1622 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001623 else
1624 shsurf->output = get_default_output(es->compositor);
1625
Tiago Vignattibe143262012-04-16 17:31:41 +03001626 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001627 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001628 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001629
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001630 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001631 shsurf->output->width,
1632 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001633
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001634 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001635}
1636
Alex Wu21858432012-04-01 20:13:08 +08001637static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001638black_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 +08001639
Alex Wu4539b082012-03-01 12:57:46 +08001640static struct weston_surface *
1641create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001642 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001643 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001644{
1645 struct weston_surface *surface = NULL;
1646
1647 surface = weston_surface_create(ec);
1648 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001649 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001650 return NULL;
1651 }
1652
Alex Wu21858432012-04-01 20:13:08 +08001653 surface->configure = black_surface_configure;
1654 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001655 weston_surface_configure(surface, x, y, w, h);
1656 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001657 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001658 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001659 pixman_region32_fini(&surface->input);
1660 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001661
Alex Wu4539b082012-03-01 12:57:46 +08001662 return surface;
1663}
1664
1665/* Create black surface and append it to the associated fullscreen surface.
1666 * Handle size dismatch and positioning according to the method. */
1667static void
1668shell_configure_fullscreen(struct shell_surface *shsurf)
1669{
1670 struct weston_output *output = shsurf->fullscreen_output;
1671 struct weston_surface *surface = shsurf->surface;
1672 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001673 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001674
Alex Wu4539b082012-03-01 12:57:46 +08001675 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001676 shsurf->fullscreen.black_surface =
1677 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001678 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001679 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001680 output->width,
1681 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001682
1683 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1684 wl_list_insert(&surface->layer_link,
1685 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001686 shsurf->fullscreen.black_surface->output = output;
1687
1688 switch (shsurf->fullscreen.type) {
1689 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001690 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001691 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001692 break;
1693 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001694 /* 1:1 mapping between surface and output dimensions */
1695 if (output->width == surface->geometry.width &&
1696 output->height == surface->geometry.height) {
1697 weston_surface_set_position(surface, output->x, output->y);
1698 break;
1699 }
1700
Alex Wu4539b082012-03-01 12:57:46 +08001701 matrix = &shsurf->fullscreen.transform.matrix;
1702 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001703
Scott Moreau1bad5db2012-08-18 01:04:05 -06001704 output_aspect = (float) output->width /
1705 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001706 surface_aspect = (float) surface->geometry.width /
1707 (float) surface->geometry.height;
1708 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001709 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001710 (float) surface->geometry.width;
1711 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001712 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001713 (float) surface->geometry.height;
1714
Alex Wu4539b082012-03-01 12:57:46 +08001715 weston_matrix_scale(matrix, scale, scale, 1);
1716 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001717 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001718 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001719 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1720 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001721 weston_surface_set_position(surface, x, y);
1722
Alex Wu4539b082012-03-01 12:57:46 +08001723 break;
1724 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001725 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001726 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001727 surface->geometry.width,
1728 surface->geometry.height,
1729 shsurf->fullscreen.framerate};
1730
1731 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001732 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001733 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001734 output->width,
1735 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001736 weston_surface_set_position(surface, output->x, output->y);
1737 break;
1738 }
1739 }
Alex Wu4539b082012-03-01 12:57:46 +08001740 break;
1741 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1742 break;
1743 default:
1744 break;
1745 }
1746}
1747
1748/* make the fullscreen and black surface at the top */
1749static void
1750shell_stack_fullscreen(struct shell_surface *shsurf)
1751{
Alex Wubd3354b2012-04-17 17:20:49 +08001752 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001753 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001754 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001755
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001756 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001757 wl_list_insert(&shell->fullscreen_layer.surface_list,
1758 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001759 weston_surface_damage(surface);
1760
1761 if (!shsurf->fullscreen.black_surface)
1762 shsurf->fullscreen.black_surface =
1763 create_black_surface(surface->compositor,
1764 surface,
1765 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001766 output->width,
1767 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001768
1769 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001770 wl_list_insert(&surface->layer_link,
1771 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001772 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001773}
1774
1775static void
1776shell_map_fullscreen(struct shell_surface *shsurf)
1777{
Alex Wu4539b082012-03-01 12:57:46 +08001778 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001779 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001780}
1781
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001782static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001783set_fullscreen(struct shell_surface *shsurf,
1784 uint32_t method,
1785 uint32_t framerate,
1786 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001787{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001788 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001789
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001790 if (output)
1791 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001792 else if (es->output)
1793 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001794 else
1795 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001796
Alex Wu4539b082012-03-01 12:57:46 +08001797 shsurf->fullscreen_output = shsurf->output;
1798 shsurf->fullscreen.type = method;
1799 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001800 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001801
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001802 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001803 shsurf->output->width,
1804 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001805}
1806
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001807static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001808shell_surface_set_fullscreen(struct wl_client *client,
1809 struct wl_resource *resource,
1810 uint32_t method,
1811 uint32_t framerate,
1812 struct wl_resource *output_resource)
1813{
1814 struct shell_surface *shsurf = resource->data;
1815 struct weston_output *output;
1816
1817 if (output_resource)
1818 output = output_resource->data;
1819 else
1820 output = NULL;
1821
1822 set_fullscreen(shsurf, method, framerate, output);
1823}
1824
1825static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001826popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001827 struct wl_surface *surface,
1828 wl_fixed_t x,
1829 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001830{
Daniel Stone37816df2012-05-16 18:45:18 +01001831 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001832 struct shell_surface *priv =
1833 container_of(grab, struct shell_surface, popup.grab);
1834 struct wl_client *client = priv->surface->surface.resource.client;
1835
Pekka Paalanencb108432012-01-19 16:25:40 +02001836 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001837 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001838 grab->focus = surface;
1839 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001840 wl_pointer_set_focus(pointer, NULL,
1841 wl_fixed_from_int(0),
1842 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001843 grab->focus = NULL;
1844 }
1845}
1846
1847static void
Scott Moreau447013d2012-02-18 05:05:29 -07001848popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001849 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001850{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001851 struct wl_resource *resource;
1852
Daniel Stone37816df2012-05-16 18:45:18 +01001853 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001854 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001855 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001856}
1857
1858static void
Scott Moreau447013d2012-02-18 05:05:29 -07001859popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001860 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001861{
1862 struct wl_resource *resource;
1863 struct shell_surface *shsurf =
1864 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001865 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001866 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001867 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001868
Daniel Stone37816df2012-05-16 18:45:18 +01001869 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001870 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001871 display = wl_client_get_display(resource->client);
1872 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001873 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001874 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001875 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001876 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001877 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001878 }
1879
Daniel Stone4dbadb12012-05-30 16:31:51 +01001880 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001881 shsurf->popup.initial_up = 1;
1882}
1883
Scott Moreau447013d2012-02-18 05:05:29 -07001884static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001885 popup_grab_focus,
1886 popup_grab_motion,
1887 popup_grab_button,
1888};
1889
1890static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001891popup_grab_end(struct wl_pointer *pointer)
1892{
1893 struct wl_pointer_grab *grab = pointer->grab;
1894 struct shell_surface *shsurf =
1895 container_of(grab, struct shell_surface, popup.grab);
1896
1897 if (pointer->grab->interface == &popup_grab_interface) {
1898 wl_shell_surface_send_popup_done(&shsurf->resource);
1899 wl_pointer_end_grab(grab->pointer);
1900 shsurf->popup.grab.pointer = NULL;
1901 }
1902}
1903
1904static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001905shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001906{
Daniel Stone37816df2012-05-16 18:45:18 +01001907 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001908 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001909 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001910
1911 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001912 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001913
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001914 shsurf->popup.initial_up = 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001915 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001916 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1917 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001918
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001919 /* We don't require the grab to still be active, but if another
1920 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001921 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1922 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001923 } else {
1924 wl_shell_surface_send_popup_done(&shsurf->resource);
1925 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001926}
1927
1928static void
1929shell_surface_set_popup(struct wl_client *client,
1930 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001931 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001932 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001933 struct wl_resource *parent_resource,
1934 int32_t x, int32_t y, uint32_t flags)
1935{
1936 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001937
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001938 shsurf->type = SHELL_SURFACE_POPUP;
1939 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001940 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001941 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001942 shsurf->popup.x = x;
1943 shsurf->popup.y = y;
1944}
1945
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001946static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001947 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001948 shell_surface_move,
1949 shell_surface_resize,
1950 shell_surface_set_toplevel,
1951 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001952 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001953 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001954 shell_surface_set_maximized,
1955 shell_surface_set_title,
1956 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001957};
1958
1959static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001960destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001961{
Daniel Stone37816df2012-05-16 18:45:18 +01001962 if (shsurf->popup.grab.pointer)
1963 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001964
Alex Wubd3354b2012-04-17 17:20:49 +08001965 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1966 shell_surface_is_top_fullscreen(shsurf)) {
1967 weston_output_switch_mode(shsurf->fullscreen_output,
1968 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001969 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001970
Alex Wuaa08e2d2012-03-05 11:01:40 +08001971 if (shsurf->fullscreen.black_surface)
1972 weston_surface_destroy(shsurf->fullscreen.black_surface);
1973
Alex Wubd3354b2012-04-17 17:20:49 +08001974 /* As destroy_resource() use wl_list_for_each_safe(),
1975 * we can always remove the listener.
1976 */
1977 wl_list_remove(&shsurf->surface_destroy_listener.link);
1978 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001979 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07001980 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08001981
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001982 wl_list_remove(&shsurf->link);
1983 free(shsurf);
1984}
1985
1986static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001987shell_destroy_shell_surface(struct wl_resource *resource)
1988{
1989 struct shell_surface *shsurf = resource->data;
1990
1991 destroy_shell_surface(shsurf);
1992}
1993
1994static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001995shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001996{
1997 struct shell_surface *shsurf = container_of(listener,
1998 struct shell_surface,
1999 surface_destroy_listener);
2000
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002001 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002002 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002003 } else {
2004 wl_signal_emit(&shsurf->resource.destroy_signal,
2005 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002006 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002007 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002008}
2009
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002010static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002011shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002012
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002013static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002014get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002015{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002016 if (surface->configure == shell_surface_configure)
2017 return surface->private;
2018 else
2019 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002020}
2021
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002022static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002023create_shell_surface(void *shell, struct weston_surface *surface,
2024 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002025{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002026 struct shell_surface *shsurf;
2027
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002028 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002029 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002030 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002031 }
2032
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002033 shsurf = calloc(1, sizeof *shsurf);
2034 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002035 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002036 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002037 }
2038
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002039 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002040 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002041
Tiago Vignattibc052c92012-04-19 16:18:18 +03002042 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002043 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002044 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002045 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002046 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002047 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2048 shsurf->fullscreen.framerate = 0;
2049 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002050 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002051 wl_list_init(&shsurf->fullscreen.transform.link);
2052
Tiago Vignattibc052c92012-04-19 16:18:18 +03002053 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002054 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2055 wl_signal_add(&surface->surface.resource.destroy_signal,
2056 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002057
2058 /* init link so its safe to always remove it in destroy_shell_surface */
2059 wl_list_init(&shsurf->link);
2060
Pekka Paalanen460099f2012-01-20 16:48:25 +02002061 /* empty when not in use */
2062 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002063 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002064
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002065 wl_list_init(&shsurf->workspace_transform.link);
2066
Pekka Paalanen98262232011-12-01 10:42:22 +02002067 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002068 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002069
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002070 shsurf->client = client;
2071
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002072 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002073}
2074
2075static void
2076shell_get_shell_surface(struct wl_client *client,
2077 struct wl_resource *resource,
2078 uint32_t id,
2079 struct wl_resource *surface_resource)
2080{
2081 struct weston_surface *surface = surface_resource->data;
2082 struct desktop_shell *shell = resource->data;
2083 struct shell_surface *shsurf;
2084
2085 if (get_shell_surface(surface)) {
2086 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002087 WL_DISPLAY_ERROR_INVALID_OBJECT,
2088 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002089 return;
2090 }
2091
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002092 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002093 if (!shsurf) {
2094 wl_resource_post_error(surface_resource,
2095 WL_DISPLAY_ERROR_INVALID_OBJECT,
2096 "surface->configure already set");
2097 return;
2098 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002099
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002100 shsurf->resource.destroy = shell_destroy_shell_surface;
2101 shsurf->resource.object.id = id;
2102 shsurf->resource.object.interface = &wl_shell_surface_interface;
2103 shsurf->resource.object.implementation =
2104 (void (**)(void)) &shell_surface_implementation;
2105 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002106
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002107 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002108}
2109
2110static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002111 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002112};
2113
Kristian Høgsberg07937562011-04-12 17:25:42 -04002114static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002115shell_fade(struct desktop_shell *shell, enum fade_type type);
2116
2117static int
2118screensaver_timeout(void *data)
2119{
2120 struct desktop_shell *shell = data;
2121
2122 shell_fade(shell, FADE_OUT);
2123
2124 return 1;
2125}
2126
2127static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002128handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002129{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002130 struct desktop_shell *shell =
2131 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002132
Pekka Paalanen18027e52011-12-02 16:31:49 +02002133 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002134
2135 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002136 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002137}
2138
2139static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002140launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002141{
2142 if (shell->screensaver.binding)
2143 return;
2144
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002145 if (!shell->screensaver.path) {
2146 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002147 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002148 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002149
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002150 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002151 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002152 return;
2153 }
2154
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002155 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002156 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002157 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002158 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002159}
2160
2161static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002162terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002163{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002164 if (shell->screensaver.process.pid == 0)
2165 return;
2166
2167 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002168}
2169
2170static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002171configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002172{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002173 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002174
Giulio Camuffo184df502013-02-21 11:29:21 +01002175 if (width == 0)
2176 return;
2177
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002178 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2179 if (s->output == es->output && s != es) {
2180 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002181 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002182 }
2183 }
2184
Giulio Camuffo184df502013-02-21 11:29:21 +01002185 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002186
2187 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002188 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002189 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002190 }
2191}
2192
2193static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002194background_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 -04002195{
2196 struct desktop_shell *shell = es->private;
2197
Giulio Camuffo184df502013-02-21 11:29:21 +01002198 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002199}
2200
2201static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002202desktop_shell_set_background(struct wl_client *client,
2203 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002204 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002205 struct wl_resource *surface_resource)
2206{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002207 struct desktop_shell *shell = resource->data;
2208 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002209
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002210 if (surface->configure) {
2211 wl_resource_post_error(surface_resource,
2212 WL_DISPLAY_ERROR_INVALID_OBJECT,
2213 "surface role already assigned");
2214 return;
2215 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002216
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002217 surface->configure = background_configure;
2218 surface->private = shell;
2219 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002220 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002221 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002222 surface->output->width,
2223 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002224}
2225
2226static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002227panel_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 -04002228{
2229 struct desktop_shell *shell = es->private;
2230
Giulio Camuffo184df502013-02-21 11:29:21 +01002231 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002232}
2233
2234static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002235desktop_shell_set_panel(struct wl_client *client,
2236 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002237 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002238 struct wl_resource *surface_resource)
2239{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002240 struct desktop_shell *shell = resource->data;
2241 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002242
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002243 if (surface->configure) {
2244 wl_resource_post_error(surface_resource,
2245 WL_DISPLAY_ERROR_INVALID_OBJECT,
2246 "surface role already assigned");
2247 return;
2248 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002249
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002250 surface->configure = panel_configure;
2251 surface->private = shell;
2252 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002253 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002254 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002255 surface->output->width,
2256 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002257}
2258
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002259static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002260lock_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 -04002261{
2262 struct desktop_shell *shell = surface->private;
2263
Giulio Camuffo184df502013-02-21 11:29:21 +01002264 if (width == 0)
2265 return;
2266
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002267 center_on_output(surface, get_default_output(shell->compositor));
2268
2269 if (!weston_surface_is_mapped(surface)) {
2270 wl_list_insert(&shell->lock_layer.surface_list,
2271 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002272 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002273 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002274 }
2275}
2276
2277static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002278handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002279{
Tiago Vignattibe143262012-04-16 17:31:41 +03002280 struct desktop_shell *shell =
2281 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002282
Martin Minarik6d118362012-06-07 18:01:59 +02002283 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002284 shell->lock_surface = NULL;
2285}
2286
2287static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002288desktop_shell_set_lock_surface(struct wl_client *client,
2289 struct wl_resource *resource,
2290 struct wl_resource *surface_resource)
2291{
Tiago Vignattibe143262012-04-16 17:31:41 +03002292 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002293 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002294
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002295 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002296
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002297 if (!shell->locked)
2298 return;
2299
Pekka Paalanen98262232011-12-01 10:42:22 +02002300 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002301
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002302 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2303 wl_signal_add(&surface_resource->destroy_signal,
2304 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002305
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002306 surface->configure = lock_surface_configure;
2307 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002308}
2309
2310static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002311resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002312{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002313 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002314
Pekka Paalanen77346a62011-11-30 16:26:35 +02002315 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002316
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002317 wl_list_remove(&shell->lock_layer.link);
2318 wl_list_insert(&shell->compositor->cursor_layer.link,
2319 &shell->fullscreen_layer.link);
2320 wl_list_insert(&shell->fullscreen_layer.link,
2321 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002322 if (shell->showing_input_panels) {
2323 wl_list_insert(&shell->panel_layer.link,
2324 &shell->input_panel_layer.link);
2325 wl_list_insert(&shell->input_panel_layer.link,
2326 &ws->layer.link);
2327 } else {
2328 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2329 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002330
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002331 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002332
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002333 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002334 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002335 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002336}
2337
2338static void
2339desktop_shell_unlock(struct wl_client *client,
2340 struct wl_resource *resource)
2341{
Tiago Vignattibe143262012-04-16 17:31:41 +03002342 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002343
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002344 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002345
2346 if (shell->locked)
2347 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002348}
2349
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002350static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002351desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002352 struct wl_resource *resource,
2353 struct wl_resource *surface_resource)
2354{
2355 struct desktop_shell *shell = resource->data;
2356
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002357 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002358}
2359
Kristian Høgsberg75840622011-09-06 13:48:16 -04002360static const struct desktop_shell_interface desktop_shell_implementation = {
2361 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002362 desktop_shell_set_panel,
2363 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002364 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002365 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002366};
2367
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002368static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002369get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002370{
2371 struct shell_surface *shsurf;
2372
2373 shsurf = get_shell_surface(surface);
2374 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002375 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002376 return shsurf->type;
2377}
2378
Kristian Høgsberg75840622011-09-06 13:48:16 -04002379static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002380move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002381{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002382 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002383 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002384 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002385
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002386 if (surface == NULL)
2387 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002388
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002389 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002390 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002391 return;
2392
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002393 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002394}
2395
2396static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002397resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002398{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002399 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002400 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002401 uint32_t edges = 0;
2402 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002403 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002404
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002405 if (surface == NULL)
2406 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002407
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002408 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002409 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002410 return;
2411
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002412 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002413 wl_fixed_to_int(seat->pointer->grab_x),
2414 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002415 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002416
Pekka Paalanen60921e52012-01-25 15:55:43 +02002417 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002418 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002419 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002420 edges |= 0;
2421 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002422 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002423
Pekka Paalanen60921e52012-01-25 15:55:43 +02002424 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002425 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002426 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002427 edges |= 0;
2428 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002429 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002430
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002431 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002432}
2433
2434static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002435surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002436 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002437{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002438 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002439 struct shell_surface *shsurf;
2440 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002441 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002442
2443 if (surface == NULL)
2444 return;
2445
2446 shsurf = get_shell_surface(surface);
2447 if (!shsurf)
2448 return;
2449
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002450 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002451
Scott Moreau02709af2012-05-22 01:54:10 -06002452 if (surface->alpha > 1.0)
2453 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002454 if (surface->alpha < step)
2455 surface->alpha = step;
2456
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002457 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002458 weston_surface_damage(surface);
2459}
2460
2461static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002462do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002463 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002464{
Daniel Stone37816df2012-05-16 18:45:18 +01002465 struct weston_seat *ws = (struct weston_seat *) seat;
2466 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002467 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002468 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002469
2470 wl_list_for_each(output, &compositor->output_list, link) {
2471 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002472 wl_fixed_to_double(seat->pointer->x),
2473 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002474 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002475 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002476 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002477 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002478 increment = -output->zoom.increment;
2479 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002480 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002481 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002482 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002483 else
2484 increment = 0;
2485
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002486 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002487
Scott Moreaue6603982012-06-11 13:07:51 -06002488 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002489 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002490 else if (output->zoom.level > output->zoom.max_level)
2491 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002492 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002493 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002494 output->disable_planes++;
2495 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002496
Scott Moreaue6603982012-06-11 13:07:51 -06002497 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002498
Scott Moreau8dacaab2012-06-17 18:10:58 -06002499 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002500 }
2501 }
2502}
2503
Scott Moreauccbf29d2012-02-22 14:21:41 -07002504static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002505zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002506 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002507{
2508 do_zoom(seat, time, 0, axis, value);
2509}
2510
2511static void
2512zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2513 void *data)
2514{
2515 do_zoom(seat, time, key, 0, 0);
2516}
2517
2518static void
2519terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2520 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002521{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002522 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002523
Daniel Stone325fc2d2012-05-30 16:31:58 +01002524 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002525}
2526
2527static void
Scott Moreau447013d2012-02-18 05:05:29 -07002528rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002529 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002530{
2531 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002532 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002533 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002534 struct shell_surface *shsurf = rotate->base.shsurf;
2535 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002536 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002537
2538 if (!shsurf)
2539 return;
2540
2541 surface = shsurf->surface;
2542
2543 cx = 0.5f * surface->geometry.width;
2544 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002545
Daniel Stone37816df2012-05-16 18:45:18 +01002546 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2547 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002548 r = sqrtf(dx * dx + dy * dy);
2549
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002550 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002551 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002552
2553 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002554 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002555 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002556
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002557 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002558 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002559
2560 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002561 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002562 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002563 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002564 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002565
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002566 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002567 &shsurf->surface->geometry.transformation_list,
2568 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002569 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002570 wl_list_init(&shsurf->rotation.transform.link);
2571 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002572 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002573 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002574
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002575 /* We need to adjust the position of the surface
2576 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002577 cposx = surface->geometry.x + cx;
2578 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002579 dposx = rotate->center.x - cposx;
2580 dposy = rotate->center.y - cposy;
2581 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002582 weston_surface_set_position(surface,
2583 surface->geometry.x + dposx,
2584 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002585 }
2586
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002587 /* Repaint implies weston_surface_update_transform(), which
2588 * lazily applies the damage due to rotation update.
2589 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002590 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002591}
2592
2593static void
Scott Moreau447013d2012-02-18 05:05:29 -07002594rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002595 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002596{
2597 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002598 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002599 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002600 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002601 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002602
Daniel Stone4dbadb12012-05-30 16:31:51 +01002603 if (pointer->button_count == 0 &&
2604 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002605 if (shsurf)
2606 weston_matrix_multiply(&shsurf->rotation.rotation,
2607 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002608 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002609 free(rotate);
2610 }
2611}
2612
Scott Moreau447013d2012-02-18 05:05:29 -07002613static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002614 noop_grab_focus,
2615 rotate_grab_motion,
2616 rotate_grab_button,
2617};
2618
2619static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002620surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002621{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002622 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002623 float dx, dy;
2624 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002625
Pekka Paalanen460099f2012-01-20 16:48:25 +02002626 rotate = malloc(sizeof *rotate);
2627 if (!rotate)
2628 return;
2629
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002630 weston_surface_to_global_float(surface->surface,
2631 surface->surface->geometry.width / 2,
2632 surface->surface->geometry.height / 2,
2633 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002634
Daniel Stone37816df2012-05-16 18:45:18 +01002635 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2636 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002637 r = sqrtf(dx * dx + dy * dy);
2638 if (r > 20.0f) {
2639 struct weston_matrix inverse;
2640
2641 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002642 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002643 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002644
2645 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002646 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002647 } else {
2648 weston_matrix_init(&surface->rotation.rotation);
2649 weston_matrix_init(&rotate->rotation);
2650 }
2651
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002652 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2653 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002654}
2655
2656static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002657rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2658 void *data)
2659{
2660 struct weston_surface *base_surface =
2661 (struct weston_surface *) seat->pointer->focus;
2662 struct shell_surface *surface;
2663
2664 if (base_surface == NULL)
2665 return;
2666
2667 surface = get_shell_surface(base_surface);
2668 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2669 return;
2670
2671 surface_rotate(surface, seat);
2672}
2673
2674static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002675lower_fullscreen_layer(struct desktop_shell *shell)
2676{
2677 struct workspace *ws;
2678 struct weston_surface *surface, *prev;
2679
2680 ws = get_current_workspace(shell);
2681 wl_list_for_each_reverse_safe(surface, prev,
2682 &shell->fullscreen_layer.surface_list,
2683 layer_link)
2684 weston_surface_restack(surface, &ws->layer.surface_list);
2685}
2686
2687static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002688activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002689 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002690{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002691 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002692 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002693
Daniel Stone37816df2012-05-16 18:45:18 +01002694 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002695
Jonas Ådahl8538b222012-08-29 22:13:03 +02002696 state = ensure_focus_state(shell, seat);
2697 if (state == NULL)
2698 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002699
2700 state->keyboard_focus = es;
2701 wl_list_remove(&state->surface_destroy_listener.link);
2702 wl_signal_add(&es->surface.resource.destroy_signal,
2703 &state->surface_destroy_listener);
2704
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002705 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002706 case SHELL_SURFACE_FULLSCREEN:
2707 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002708 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002709 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002710 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002711 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002712 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002713 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002714 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002715 }
2716}
2717
Alex Wu21858432012-04-01 20:13:08 +08002718/* no-op func for checking black surface */
2719static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002720black_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 +08002721{
2722}
2723
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002724static bool
Alex Wu21858432012-04-01 20:13:08 +08002725is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2726{
2727 if (es->configure == black_surface_configure) {
2728 if (fs_surface)
2729 *fs_surface = (struct weston_surface *)es->private;
2730 return true;
2731 }
2732 return false;
2733}
2734
Kristian Høgsberg75840622011-09-06 13:48:16 -04002735static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002736click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002737 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002738{
Daniel Stone37816df2012-05-16 18:45:18 +01002739 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002740 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002741 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002742 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002743
Daniel Stone37816df2012-05-16 18:45:18 +01002744 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002745 if (!focus)
2746 return;
2747
Alex Wu21858432012-04-01 20:13:08 +08002748 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002749 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002750
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002751 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2752 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002753
Daniel Stone325fc2d2012-05-30 16:31:58 +01002754 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002755 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002756}
2757
2758static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002759lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002760{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002761 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002762
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002763 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002764 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002765 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002766 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002767
2768 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002769
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002770 /* Hide all surfaces by removing the fullscreen, panel and
2771 * toplevel layers. This way nothing else can show or receive
2772 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002773
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002774 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002775 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002776 if (shell->showing_input_panels)
2777 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002778 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002779 wl_list_insert(&shell->compositor->cursor_layer.link,
2780 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002781
Pekka Paalanen77346a62011-11-30 16:26:35 +02002782 launch_screensaver(shell);
2783
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002784 /* TODO: disable bindings that should not work while locked. */
2785
2786 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002787}
2788
2789static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002790unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002791{
Pekka Paalanend81c2162011-11-16 13:47:34 +02002792 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002793 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002794 return;
2795 }
2796
2797 /* If desktop-shell client has gone away, unlock immediately. */
2798 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002799 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002800 return;
2801 }
2802
2803 if (shell->prepare_event_sent)
2804 return;
2805
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002806 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002807 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002808}
2809
2810static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002811shell_fade_done(struct weston_surface_animation *animation, void *data)
2812{
2813 struct desktop_shell *shell = data;
2814
2815 shell->fade.animation = NULL;
2816
2817 switch (shell->fade.type) {
2818 case FADE_IN:
2819 weston_surface_destroy(shell->fade.surface);
2820 shell->fade.surface = NULL;
2821 break;
2822 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002823 lock(shell);
2824 break;
2825 }
2826}
2827
2828static void
2829shell_fade(struct desktop_shell *shell, enum fade_type type)
2830{
2831 struct weston_compositor *compositor = shell->compositor;
2832 struct weston_surface *surface;
2833 float tint;
2834
2835 switch (type) {
2836 case FADE_IN:
2837 tint = 0.0;
2838 break;
2839 case FADE_OUT:
2840 tint = 1.0;
2841 break;
2842 default:
2843 weston_log("shell: invalid fade type\n");
2844 return;
2845 }
2846
2847 shell->fade.type = type;
2848
2849 if (shell->fade.surface == NULL) {
2850 surface = weston_surface_create(compositor);
2851 if (!surface)
2852 return;
2853
2854 weston_surface_configure(surface, 0, 0, 8192, 8192);
2855 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2856 surface->alpha = 1.0 - tint;
2857 wl_list_insert(&compositor->fade_layer.surface_list,
2858 &surface->layer_link);
2859 weston_surface_update_transform(surface);
2860 shell->fade.surface = surface;
2861 pixman_region32_init(&surface->input);
2862 }
2863
2864 if (shell->fade.animation)
2865 weston_fade_update(shell->fade.animation,
2866 shell->fade.surface->alpha, tint, 30.0);
2867 else
2868 shell->fade.animation =
2869 weston_fade_run(shell->fade.surface,
2870 1.0 - tint, tint, 30.0,
2871 shell_fade_done, shell);
2872}
2873
2874static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002875idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002876{
2877 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002878 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002879
2880 shell_fade(shell, FADE_OUT);
2881 /* lock() is called from shell_fade_done() */
2882}
2883
2884static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002885wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002886{
2887 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002888 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002889
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002890 unlock(shell);
2891}
2892
2893static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002894show_input_panels(struct wl_listener *listener, void *data)
2895{
2896 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002897 container_of(listener, struct desktop_shell,
2898 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002899 struct input_panel_surface *surface, *next;
2900 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002901
Jan Arne Petersen451a9712013-02-11 15:10:11 +01002902 if (shell->showing_input_panels)
2903 return;
2904
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002905 shell->showing_input_panels = true;
2906
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002907 if (!shell->locked)
2908 wl_list_insert(&shell->panel_layer.link,
2909 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002910
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002911 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002912 &shell->input_panel.surfaces, link) {
2913 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002914 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002915 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002916 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002917 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002918 weston_surface_damage(ws);
2919 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002920 }
2921}
2922
2923static void
2924hide_input_panels(struct wl_listener *listener, void *data)
2925{
2926 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002927 container_of(listener, struct desktop_shell,
2928 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002929 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002930
Jan Arne Petersen61381972013-01-31 15:52:21 +01002931 if (!shell->showing_input_panels)
2932 return;
2933
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002934 shell->showing_input_panels = false;
2935
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002936 if (!shell->locked)
2937 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002938
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002939 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002940 &shell->input_panel_layer.surface_list, layer_link)
2941 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002942}
2943
2944static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002945center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002946{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002947 int32_t width = weston_surface_buffer_width(surface);
2948 int32_t height = weston_surface_buffer_height(surface);
2949 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002950
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002951 x = output->x + (output->width - width) / 2;
2952 y = output->y + (output->height - height) / 2;
2953
2954 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002955}
2956
2957static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002958weston_surface_set_initial_position (struct weston_surface *surface,
2959 struct desktop_shell *shell)
2960{
2961 struct weston_compositor *compositor = shell->compositor;
2962 int ix = 0, iy = 0;
2963 int range_x, range_y;
2964 int dx, dy, x, y, panel_height;
2965 struct weston_output *output, *target_output = NULL;
2966 struct weston_seat *seat;
2967
2968 /* As a heuristic place the new window on the same output as the
2969 * pointer. Falling back to the output containing 0, 0.
2970 *
2971 * TODO: Do something clever for touch too?
2972 */
2973 wl_list_for_each(seat, &compositor->seat_list, link) {
2974 if (seat->has_pointer) {
2975 ix = wl_fixed_to_int(seat->pointer.x);
2976 iy = wl_fixed_to_int(seat->pointer.y);
2977 break;
2978 }
2979 }
2980
2981 wl_list_for_each(output, &compositor->output_list, link) {
2982 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2983 target_output = output;
2984 break;
2985 }
2986 }
2987
2988 if (!target_output) {
2989 weston_surface_set_position(surface, 10 + random() % 400,
2990 10 + random() % 400);
2991 return;
2992 }
2993
2994 /* Valid range within output where the surface will still be onscreen.
2995 * If this is negative it means that the surface is bigger than
2996 * output.
2997 */
2998 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002999 range_x = target_output->width - surface->geometry.width;
3000 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003001 surface->geometry.height;
3002
Rob Bradford4cb88c72012-08-13 15:18:44 +01003003 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003004 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003005 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003006 dx = 0;
3007
3008 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003009 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003010 else
3011 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003012
3013 x = target_output->x + dx;
3014 y = target_output->y + dy;
3015
3016 weston_surface_set_position (surface, x, y);
3017}
3018
3019static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003020map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003021 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003022{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003023 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003024 struct shell_surface *shsurf = get_shell_surface(surface);
3025 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003026 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003027 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003028 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003029 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003030
Pekka Paalanen60921e52012-01-25 15:55:43 +02003031 surface->geometry.width = width;
3032 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003033 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003034
3035 /* initial positioning, see also configure() */
3036 switch (surface_type) {
3037 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003038 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003039 break;
Alex Wu4539b082012-03-01 12:57:46 +08003040 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003041 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003042 shell_map_fullscreen(shsurf);
3043 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003044 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003045 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003046 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01003047 weston_surface_set_position(surface, shsurf->output->x,
3048 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08003049 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003050 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003051 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003052 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003053 case SHELL_SURFACE_NONE:
3054 weston_surface_set_position(surface,
3055 surface->geometry.x + sx,
3056 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003057 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003058 default:
3059 ;
3060 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003061
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003062 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003063 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003064 case SHELL_SURFACE_POPUP:
3065 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003066 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003067 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3068 break;
Alex Wu4539b082012-03-01 12:57:46 +08003069 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003070 case SHELL_SURFACE_NONE:
3071 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003072 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003073 ws = get_current_workspace(shell);
3074 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003075 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003076 }
3077
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003078 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003079 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003080 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3081 surface->output = shsurf->output;
3082 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003083
Juan Zhao7bb92f02011-12-15 11:31:51 -05003084 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003085 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003086 if (shsurf->transient.flags ==
3087 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3088 break;
3089 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003090 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003091 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003092 if (!shell->locked) {
3093 wl_list_for_each(seat, &compositor->seat_list, link)
3094 activate(shell, surface, seat);
3095 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003096 break;
3097 default:
3098 break;
3099 }
3100
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003101 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003102 {
3103 switch (shell->win_animation_type) {
3104 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003105 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003106 break;
3107 case ANIMATION_ZOOM:
3108 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3109 break;
3110 default:
3111 break;
3112 }
3113 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003114}
3115
3116static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003117configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003118 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003119{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003120 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3121 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003122
Pekka Paalanen77346a62011-11-30 16:26:35 +02003123 shsurf = get_shell_surface(surface);
3124 if (shsurf)
3125 surface_type = shsurf->type;
3126
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003127 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003128
3129 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003130 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003131 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003132 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003133 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003134 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003135 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003136 surface->geometry.x = surface->output->x;
3137 surface->geometry.y = surface->output->y +
3138 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003139 break;
Alex Wu4539b082012-03-01 12:57:46 +08003140 case SHELL_SURFACE_TOPLEVEL:
3141 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003142 default:
3143 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003144 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003145
Alex Wu4539b082012-03-01 12:57:46 +08003146 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003147 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003148 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003149
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003150 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003151 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003152 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003153}
3154
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003155static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003156shell_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 +03003157{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003158 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003159 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003160
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003161 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003162
Giulio Camuffo184df502013-02-21 11:29:21 +01003163 if (width == 0)
3164 return;
3165
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003166 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003167 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003168 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003169 type_changed = 1;
3170 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003171
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003172 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003173 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003174 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003175 es->geometry.width != width ||
3176 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003177 float from_x, from_y;
3178 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003179
3180 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3181 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3182 configure(shell, es,
3183 es->geometry.x + to_x - from_x,
3184 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003185 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003186 }
3187}
3188
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003189static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003190
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003191static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003192desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003193{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003194 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003195 struct desktop_shell *shell =
3196 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003197
3198 shell->child.process.pid = 0;
3199 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003200
3201 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3202 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003203 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003204 shell->child.deathstamp = time;
3205 shell->child.deathcount = 0;
3206 }
3207
3208 shell->child.deathcount++;
3209 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003210 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003211 return;
3212 }
3213
Martin Minarik6d118362012-06-07 18:01:59 +02003214 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003215 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003216}
3217
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003218static void
3219launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003220{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003221 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003222 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003223
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003224 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003225 &shell->child.process,
3226 shell_exe,
3227 desktop_shell_sigchld);
3228
3229 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003230 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003231}
3232
3233static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003234bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3235{
Tiago Vignattibe143262012-04-16 17:31:41 +03003236 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003237
3238 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003239 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003240}
3241
Kristian Høgsberg75840622011-09-06 13:48:16 -04003242static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003243unbind_desktop_shell(struct wl_resource *resource)
3244{
Tiago Vignattibe143262012-04-16 17:31:41 +03003245 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003246
3247 if (shell->locked)
3248 resume_desktop(shell);
3249
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003250 shell->child.desktop_shell = NULL;
3251 shell->prepare_event_sent = false;
3252 free(resource);
3253}
3254
3255static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003256bind_desktop_shell(struct wl_client *client,
3257 void *data, uint32_t version, uint32_t id)
3258{
Tiago Vignattibe143262012-04-16 17:31:41 +03003259 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003260 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003261
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003262 resource = wl_client_add_object(client, &desktop_shell_interface,
3263 &desktop_shell_implementation,
3264 id, shell);
3265
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003266 if (client == shell->child.client) {
3267 resource->destroy = unbind_desktop_shell;
3268 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003269 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003270 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003271
3272 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3273 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003274 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003275}
3276
Pekka Paalanen6e168112011-11-24 11:34:05 +02003277static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003278screensaver_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 -04003279{
3280 struct desktop_shell *shell = surface->private;
3281
Giulio Camuffo184df502013-02-21 11:29:21 +01003282 if (width == 0)
3283 return;
3284
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003285 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003286 if (!shell->locked)
3287 return;
3288
3289 center_on_output(surface, surface->output);
3290
3291 if (wl_list_empty(&surface->layer_link)) {
3292 wl_list_insert(shell->lock_layer.surface_list.prev,
3293 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003294 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003295 wl_event_source_timer_update(shell->screensaver.timer,
3296 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003297 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003298 }
3299}
3300
3301static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003302screensaver_set_surface(struct wl_client *client,
3303 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003304 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003305 struct wl_resource *output_resource)
3306{
Tiago Vignattibe143262012-04-16 17:31:41 +03003307 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003308 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003309 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003310
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003311 surface->configure = screensaver_configure;
3312 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003313 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003314}
3315
3316static const struct screensaver_interface screensaver_implementation = {
3317 screensaver_set_surface
3318};
3319
3320static void
3321unbind_screensaver(struct wl_resource *resource)
3322{
Tiago Vignattibe143262012-04-16 17:31:41 +03003323 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003324
Pekka Paalanen77346a62011-11-30 16:26:35 +02003325 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003326 free(resource);
3327}
3328
3329static void
3330bind_screensaver(struct wl_client *client,
3331 void *data, uint32_t version, uint32_t id)
3332{
Tiago Vignattibe143262012-04-16 17:31:41 +03003333 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003334 struct wl_resource *resource;
3335
3336 resource = wl_client_add_object(client, &screensaver_interface,
3337 &screensaver_implementation,
3338 id, shell);
3339
Pekka Paalanen77346a62011-11-30 16:26:35 +02003340 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003341 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003342 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003343 return;
3344 }
3345
3346 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3347 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003348 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003349}
3350
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003351static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003352input_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 -04003353{
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003354 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003355 float x, y;
3356
Giulio Camuffo184df502013-02-21 11:29:21 +01003357 if (width == 0)
3358 return;
3359
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003360 if (!weston_surface_is_mapped(surface))
3361 return;
3362
3363 mode = surface->output->current;
3364 x = (mode->width - width) / 2;
3365 y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003366
3367 /* Don't map the input panel here, wait for
3368 * show_input_panels signal. */
3369
3370 weston_surface_configure(surface,
3371 surface->output->x + x,
3372 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003373 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003374}
3375
3376static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003377destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003378{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003379 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003380 wl_list_remove(&input_panel_surface->link);
3381
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003382 input_panel_surface->surface->configure = NULL;
3383
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003384 free(input_panel_surface);
3385}
3386
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003387static struct input_panel_surface *
3388get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003389{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003390 if (surface->configure == input_panel_configure) {
3391 return surface->private;
3392 } else {
3393 return NULL;
3394 }
3395}
3396
3397static void
3398input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3399{
3400 struct input_panel_surface *ipsurface = container_of(listener,
3401 struct input_panel_surface,
3402 surface_destroy_listener);
3403
3404 if (ipsurface->resource.client) {
3405 wl_resource_destroy(&ipsurface->resource);
3406 } else {
3407 wl_signal_emit(&ipsurface->resource.destroy_signal,
3408 &ipsurface->resource);
3409 destroy_input_panel_surface(ipsurface);
3410 }
3411}
3412static struct input_panel_surface *
3413create_input_panel_surface(struct desktop_shell *shell,
3414 struct weston_surface *surface)
3415{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003416 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003417
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003418 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3419 if (!input_panel_surface)
3420 return NULL;
3421
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003422 surface->configure = input_panel_configure;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003423 surface->private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003424
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003425 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003426
3427 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003428
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003429 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3430 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3431 wl_signal_add(&surface->surface.resource.destroy_signal,
3432 &input_panel_surface->surface_destroy_listener);
3433
3434 wl_list_init(&input_panel_surface->link);
3435
3436 return input_panel_surface;
3437}
3438
3439static void
3440input_panel_surface_set_toplevel(struct wl_client *client,
3441 struct wl_resource *resource,
3442 uint32_t position)
3443{
3444 struct input_panel_surface *input_panel_surface = resource->data;
3445 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003446
3447 wl_list_insert(&shell->input_panel.surfaces,
3448 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003449}
3450
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003451static const struct input_panel_surface_interface input_panel_surface_implementation = {
3452 input_panel_surface_set_toplevel
3453};
3454
3455static void
3456destroy_input_panel_surface_resource(struct wl_resource *resource)
3457{
3458 struct input_panel_surface *ipsurf = resource->data;
3459
3460 destroy_input_panel_surface(ipsurf);
3461}
3462
3463static void
3464input_panel_get_input_panel_surface(struct wl_client *client,
3465 struct wl_resource *resource,
3466 uint32_t id,
3467 struct wl_resource *surface_resource)
3468{
3469 struct weston_surface *surface = surface_resource->data;
3470 struct desktop_shell *shell = resource->data;
3471 struct input_panel_surface *ipsurf;
3472
3473 if (get_input_panel_surface(surface)) {
3474 wl_resource_post_error(surface_resource,
3475 WL_DISPLAY_ERROR_INVALID_OBJECT,
3476 "input_panel::get_input_panel_surface already requested");
3477 return;
3478 }
3479
3480 ipsurf = create_input_panel_surface(shell, surface);
3481 if (!ipsurf) {
3482 wl_resource_post_error(surface_resource,
3483 WL_DISPLAY_ERROR_INVALID_OBJECT,
3484 "surface->configure already set");
3485 return;
3486 }
3487
3488 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3489 ipsurf->resource.object.id = id;
3490 ipsurf->resource.object.interface = &input_panel_surface_interface;
3491 ipsurf->resource.object.implementation =
3492 (void (**)(void)) &input_panel_surface_implementation;
3493 ipsurf->resource.data = ipsurf;
3494
3495 wl_client_add_resource(client, &ipsurf->resource);
3496}
3497
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003498static const struct input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003499 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003500};
3501
3502static void
3503unbind_input_panel(struct wl_resource *resource)
3504{
3505 struct desktop_shell *shell = resource->data;
3506
3507 shell->input_panel.binding = NULL;
3508 free(resource);
3509}
3510
3511static void
3512bind_input_panel(struct wl_client *client,
3513 void *data, uint32_t version, uint32_t id)
3514{
3515 struct desktop_shell *shell = data;
3516 struct wl_resource *resource;
3517
3518 resource = wl_client_add_object(client, &input_panel_interface,
3519 &input_panel_implementation,
3520 id, shell);
3521
3522 if (shell->input_panel.binding == NULL) {
3523 resource->destroy = unbind_input_panel;
3524 shell->input_panel.binding = resource;
3525 return;
3526 }
3527
3528 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3529 "interface object already bound");
3530 wl_resource_destroy(resource);
3531}
3532
Kristian Høgsberg07045392012-02-19 18:52:44 -05003533struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003534 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003535 struct weston_surface *current;
3536 struct wl_listener listener;
3537 struct wl_keyboard_grab grab;
3538};
3539
3540static void
3541switcher_next(struct switcher *switcher)
3542{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003543 struct weston_surface *surface;
3544 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003545 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003546 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003547
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003548 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003549 switch (get_shell_surface_type(surface)) {
3550 case SHELL_SURFACE_TOPLEVEL:
3551 case SHELL_SURFACE_FULLSCREEN:
3552 case SHELL_SURFACE_MAXIMIZED:
3553 if (first == NULL)
3554 first = surface;
3555 if (prev == switcher->current)
3556 next = surface;
3557 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003558 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003559 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003560 weston_surface_damage(surface);
3561 break;
3562 default:
3563 break;
3564 }
Alex Wu1659daa2012-04-01 20:13:09 +08003565
3566 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003567 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003568 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003569 weston_surface_damage(surface);
3570 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003571 }
3572
3573 if (next == NULL)
3574 next = first;
3575
Alex Wu07b26062012-03-12 16:06:01 +08003576 if (next == NULL)
3577 return;
3578
Kristian Høgsberg07045392012-02-19 18:52:44 -05003579 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003580 wl_signal_add(&next->surface.resource.destroy_signal,
3581 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003582
3583 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003584 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003585
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003586 shsurf = get_shell_surface(switcher->current);
3587 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003588 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003589}
3590
3591static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003592switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003593{
3594 struct switcher *switcher =
3595 container_of(listener, struct switcher, listener);
3596
3597 switcher_next(switcher);
3598}
3599
3600static void
Daniel Stone351eb612012-05-31 15:27:47 -04003601switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003602{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003603 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003604 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003605 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003606 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003607
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003608 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003609 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003610 weston_surface_damage(surface);
3611 }
3612
Alex Wu07b26062012-03-12 16:06:01 +08003613 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003614 activate(switcher->shell, switcher->current,
3615 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003616 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003617 wl_keyboard_end_grab(keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003618 if (weston_keyboard->input_method_resource)
3619 keyboard->grab = &weston_keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003620 free(switcher);
3621}
3622
3623static void
3624switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003625 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003626{
3627 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003628 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003629
Daniel Stonec9785ea2012-05-30 16:31:52 +01003630 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003631 switcher_next(switcher);
3632}
3633
3634static void
3635switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3636 uint32_t mods_depressed, uint32_t mods_latched,
3637 uint32_t mods_locked, uint32_t group)
3638{
3639 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003640 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003641
Daniel Stone351eb612012-05-31 15:27:47 -04003642 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3643 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003644}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003645
3646static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003647 switcher_key,
3648 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003649};
3650
3651static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003652switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3653 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003654{
Tiago Vignattibe143262012-04-16 17:31:41 +03003655 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003656 struct switcher *switcher;
3657
3658 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003659 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003660 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003661 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003662 wl_list_init(&switcher->listener.link);
3663
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003664 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003665 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003666 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3667 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003668 switcher_next(switcher);
3669}
3670
Pekka Paalanen3c647232011-12-22 13:43:43 +02003671static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003672backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3673 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003674{
3675 struct weston_compositor *compositor = data;
3676 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003677 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003678
3679 /* TODO: we're limiting to simple use cases, where we assume just
3680 * control on the primary display. We'd have to extend later if we
3681 * ever get support for setting backlights on random desktop LCD
3682 * panels though */
3683 output = get_default_output(compositor);
3684 if (!output)
3685 return;
3686
3687 if (!output->set_backlight)
3688 return;
3689
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003690 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3691 backlight_new = output->backlight_current - 25;
3692 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3693 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003694
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003695 if (backlight_new < 5)
3696 backlight_new = 5;
3697 if (backlight_new > 255)
3698 backlight_new = 255;
3699
3700 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003701 output->set_backlight(output, output->backlight_current);
3702}
3703
3704static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003705fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3706 void *data)
3707{
3708 struct desktop_shell *shell = data;
3709 struct weston_compositor *compositor = shell->compositor;
3710 compositor->fan_debug = !compositor->fan_debug;
3711 weston_compositor_damage_all(compositor);
3712}
3713
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003714struct debug_binding_grab {
3715 struct wl_keyboard_grab grab;
3716 struct weston_seat *seat;
3717 uint32_t key[2];
3718 int key_released[2];
3719};
3720
3721static void
3722debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3723 uint32_t key, uint32_t state)
3724{
3725 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3726 struct wl_resource *resource;
3727 struct wl_display *display;
3728 uint32_t serial;
3729 int send = 0, terminate = 0;
3730 int check_binding = 1;
3731 int i;
3732
3733 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3734 /* Do not run bindings on key releases */
3735 check_binding = 0;
3736
3737 for (i = 0; i < 2; i++)
3738 if (key == db->key[i])
3739 db->key_released[i] = 1;
3740
3741 if (db->key_released[0] && db->key_released[1]) {
3742 /* All key releases been swalled so end the grab */
3743 terminate = 1;
3744 } else if (key != db->key[0] && key != db->key[1]) {
3745 /* Should not swallow release of other keys */
3746 send = 1;
3747 }
3748 } else if (key == db->key[0] && !db->key_released[0]) {
3749 /* Do not check bindings for the first press of the binding
3750 * key. This allows it to be used as a debug shortcut.
3751 * We still need to swallow this event. */
3752 check_binding = 0;
3753 } else if (db->key[1]) {
3754 /* If we already ran a binding don't process another one since
3755 * we can't keep track of all the binding keys that were
3756 * pressed in order to swallow the release events. */
3757 send = 1;
3758 check_binding = 0;
3759 }
3760
3761 if (check_binding) {
3762 struct weston_compositor *ec = db->seat->compositor;
3763
3764 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3765 key, state)) {
3766 /* We ran a binding so swallow the press and keep the
3767 * grab to swallow the released too. */
3768 send = 0;
3769 terminate = 0;
3770 db->key[1] = key;
3771 } else {
3772 /* Terminate the grab since the key pressed is not a
3773 * debug binding key. */
3774 send = 1;
3775 terminate = 1;
3776 }
3777 }
3778
3779 if (send) {
3780 resource = grab->keyboard->focus_resource;
3781
3782 if (resource) {
3783 display = wl_client_get_display(resource->client);
3784 serial = wl_display_next_serial(display);
3785 wl_keyboard_send_key(resource, serial, time, key, state);
3786 }
3787 }
3788
3789 if (terminate) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003790 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003791 wl_keyboard_end_grab(grab->keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003792 if (weston_keyboard->input_method_resource)
3793 grab->keyboard->grab = &weston_keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003794 free(db);
3795 }
3796}
3797
3798static void
3799debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3800 uint32_t mods_depressed, uint32_t mods_latched,
3801 uint32_t mods_locked, uint32_t group)
3802{
3803 struct wl_resource *resource;
3804
3805 resource = grab->keyboard->focus_resource;
3806 if (!resource)
3807 return;
3808
3809 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3810 mods_latched, mods_locked, group);
3811}
3812
3813struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3814 debug_binding_key,
3815 debug_binding_modifiers
3816};
3817
3818static void
3819debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3820{
3821 struct debug_binding_grab *grab;
3822
3823 grab = calloc(1, sizeof *grab);
3824 if (!grab)
3825 return;
3826
3827 grab->seat = (struct weston_seat *) seat;
3828 grab->key[0] = key;
3829 grab->grab.interface = &debug_binding_keyboard_grab;
3830 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3831}
3832
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003833static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003834force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3835 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003836{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003837 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003838 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003839 struct desktop_shell *shell = data;
3840 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003841 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003842
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003843 focus_surface = seat->keyboard->focus;
3844 if (!focus_surface)
3845 return;
3846
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003847 wl_signal_emit(&compositor->kill_signal, focus_surface);
3848
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003849 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003850 wl_client_get_credentials(client, &pid, NULL, NULL);
3851
3852 /* Skip clients that we launched ourselves (the credentials of
3853 * the socketpair is ours) */
3854 if (pid == getpid())
3855 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003856
Daniel Stone325fc2d2012-05-30 16:31:58 +01003857 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003858}
3859
3860static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003861workspace_up_binding(struct wl_seat *seat, uint32_t time,
3862 uint32_t key, void *data)
3863{
3864 struct desktop_shell *shell = data;
3865 unsigned int new_index = shell->workspaces.current;
3866
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003867 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003868 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003869 if (new_index != 0)
3870 new_index--;
3871
3872 change_workspace(shell, new_index);
3873}
3874
3875static void
3876workspace_down_binding(struct wl_seat *seat, uint32_t time,
3877 uint32_t key, void *data)
3878{
3879 struct desktop_shell *shell = data;
3880 unsigned int new_index = shell->workspaces.current;
3881
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003882 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003883 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003884 if (new_index < shell->workspaces.num - 1)
3885 new_index++;
3886
3887 change_workspace(shell, new_index);
3888}
3889
3890static void
3891workspace_f_binding(struct wl_seat *seat, uint32_t time,
3892 uint32_t key, void *data)
3893{
3894 struct desktop_shell *shell = data;
3895 unsigned int new_index;
3896
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003897 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003898 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003899 new_index = key - KEY_F1;
3900 if (new_index >= shell->workspaces.num)
3901 new_index = shell->workspaces.num - 1;
3902
3903 change_workspace(shell, new_index);
3904}
3905
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003906static void
3907workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3908 uint32_t key, void *data)
3909{
3910 struct desktop_shell *shell = data;
3911 unsigned int new_index = shell->workspaces.current;
3912
3913 if (shell->locked)
3914 return;
3915
3916 if (new_index != 0)
3917 new_index--;
3918
3919 take_surface_to_workspace_by_seat(shell, seat, new_index);
3920}
3921
3922static void
3923workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3924 uint32_t key, void *data)
3925{
3926 struct desktop_shell *shell = data;
3927 unsigned int new_index = shell->workspaces.current;
3928
3929 if (shell->locked)
3930 return;
3931
3932 if (new_index < shell->workspaces.num - 1)
3933 new_index++;
3934
3935 take_surface_to_workspace_by_seat(shell, seat, new_index);
3936}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003937
3938static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003939shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003940{
Tiago Vignattibe143262012-04-16 17:31:41 +03003941 struct desktop_shell *shell =
3942 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003943 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003944
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003945 if (shell->child.client)
3946 wl_client_destroy(shell->child.client);
3947
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003948 wl_list_remove(&shell->idle_listener.link);
3949 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003950 wl_list_remove(&shell->show_input_panel_listener.link);
3951 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003952
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003953 wl_array_for_each(ws, &shell->workspaces.array)
3954 workspace_destroy(*ws);
3955 wl_array_release(&shell->workspaces.array);
3956
Pekka Paalanen3c647232011-12-22 13:43:43 +02003957 free(shell->screensaver.path);
3958 free(shell);
3959}
3960
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003961static void
3962shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3963{
3964 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003965 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003966
3967 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003968 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3969 MODIFIER_CTRL | MODIFIER_ALT,
3970 terminate_binding, ec);
3971 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3972 click_to_activate_binding,
3973 shell);
3974 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3975 MODIFIER_SUPER | MODIFIER_ALT,
3976 surface_opacity_binding, NULL);
3977 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3978 MODIFIER_SUPER, zoom_axis_binding,
3979 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003980
3981 /* configurable bindings */
3982 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003983 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3984 zoom_key_binding, NULL);
3985 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3986 zoom_key_binding, NULL);
3987 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3988 shell);
3989 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3990 resize_binding, shell);
3991 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3992 rotate_binding, NULL);
3993 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3994 shell);
3995 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3996 ec);
3997 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3998 backlight_binding, ec);
3999 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4000 ec);
4001 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4002 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004003 weston_compositor_add_key_binding(ec, KEY_K, mod,
4004 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004005 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4006 workspace_up_binding, shell);
4007 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4008 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004009 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4010 workspace_move_surface_up_binding,
4011 shell);
4012 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4013 workspace_move_surface_down_binding,
4014 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004015
4016 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4017 if (shell->workspaces.num > 1) {
4018 num_workspace_bindings = shell->workspaces.num;
4019 if (num_workspace_bindings > 6)
4020 num_workspace_bindings = 6;
4021 for (i = 0; i < num_workspace_bindings; i++)
4022 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4023 workspace_f_binding,
4024 shell);
4025 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004026
4027 /* Debug bindings */
4028 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4029 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004030 weston_compositor_add_debug_binding(ec, KEY_F,
4031 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004032}
4033
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004034WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004035module_init(struct weston_compositor *ec,
4036 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004037{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004038 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004039 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004040 struct workspace **pws;
4041 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004042 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004043
4044 shell = malloc(sizeof *shell);
4045 if (shell == NULL)
4046 return -1;
4047
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004048 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004049 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004050
4051 shell->destroy_listener.notify = shell_destroy;
4052 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004053 shell->idle_listener.notify = idle_handler;
4054 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4055 shell->wake_listener.notify = wake_handler;
4056 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004057 shell->show_input_panel_listener.notify = show_input_panels;
4058 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4059 shell->hide_input_panel_listener.notify = hide_input_panels;
4060 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004061 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004062 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004063 ec->shell_interface.create_shell_surface = create_shell_surface;
4064 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004065 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004066 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004067 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004068 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004069
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004070 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004071
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004072 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4073 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004074 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4075 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004076 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004077
4078 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004079 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004080
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004081 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004082
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004083 for (i = 0; i < shell->workspaces.num; i++) {
4084 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4085 if (pws == NULL)
4086 return -1;
4087
4088 *pws = workspace_create();
4089 if (*pws == NULL)
4090 return -1;
4091 }
4092 activate_workspace(shell, 0);
4093
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004094 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004095 wl_list_init(&shell->workspaces.animation.link);
4096 shell->workspaces.animation.frame = animate_workspace_change_frame;
4097
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004098 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4099 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004100 return -1;
4101
Kristian Høgsberg75840622011-09-06 13:48:16 -04004102 if (wl_display_add_global(ec->wl_display,
4103 &desktop_shell_interface,
4104 shell, bind_desktop_shell) == NULL)
4105 return -1;
4106
Pekka Paalanen6e168112011-11-24 11:34:05 +02004107 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4108 shell, bind_screensaver) == NULL)
4109 return -1;
4110
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004111 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4112 shell, bind_input_panel) == NULL)
4113 return -1;
4114
Jonas Ådahle9d22502012-08-29 22:13:01 +02004115 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4116 shell, bind_workspace_manager) == NULL)
4117 return -1;
4118
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004119 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004120
4121 loop = wl_display_get_event_loop(ec->wl_display);
4122 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004123
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004124 shell->screensaver.timer =
4125 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4126
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004127 wl_list_for_each(seat, &ec->seat_list, link)
4128 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004129
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004130 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004131
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004132 shell_fade(shell, FADE_IN);
4133
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004134 return 0;
4135}