blob: 9fcce9f4559b66d8a720b123fcbb38ba4add04a8 [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"
Jonas Ådahle9d22502012-08-29 22:13:01 +020038#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020039#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020042#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043
Juan Zhaoe10d2792012-04-25 19:09:52 +080044enum animation_type {
45 ANIMATION_NONE,
46
47 ANIMATION_ZOOM,
48 ANIMATION_FADE
49};
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040053 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020054 struct weston_surface *keyboard_focus;
55 struct wl_list link;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
58};
59
Jonas Ådahle3cddce2012-06-13 00:01:22 +020060struct workspace {
61 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020062
63 struct wl_list focus_list;
64 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020065};
66
Philipp Brüschweiler88013572012-08-06 13:44:42 +020067struct input_panel_surface {
68 struct wl_list link;
69 struct weston_surface *surface;
70 struct wl_listener listener;
71};
72
Tiago Vignattibe143262012-04-16 17:31:41 +030073struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050074 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040075
76 struct wl_listener lock_listener;
77 struct wl_listener unlock_listener;
78 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020079 struct wl_listener show_input_panel_listener;
80 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020081
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050082 struct weston_layer fullscreen_layer;
83 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050084 struct weston_layer background_layer;
85 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +020086 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050087
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040088 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030089 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040090
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020091 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050092 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020093 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020094 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020095
96 unsigned deathcount;
97 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020098 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020099
100 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200101 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200102 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200103
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400104 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500105 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100106
Pekka Paalanen77346a62011-11-30 16:26:35 +0200107 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200108 struct wl_array array;
109 unsigned int current;
110 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200111
Jonas Ådahle9d22502012-08-29 22:13:01 +0200112 struct wl_list client_list;
113
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200114 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200115 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200116 int anim_dir;
117 uint32_t anim_timestamp;
118 double anim_current;
119 struct workspace *anim_from;
120 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200121 } workspaces;
122
123 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200124 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200125 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200126 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500127 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200128 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500129
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200130 struct {
131 struct wl_resource *binding;
132 struct wl_list surfaces;
133 } input_panel;
134
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300135 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800136 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400137};
138
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500139enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200140 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500141 SHELL_SURFACE_TOPLEVEL,
142 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800144 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200146};
147
Scott Moreauff1db4a2012-04-17 19:06:18 -0600148struct ping_timer {
149 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600150 uint32_t serial;
151};
152
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200153struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200154 struct wl_resource resource;
155
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200157 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400158 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300159 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200160
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400161 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400162 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500163 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800164 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800165 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600166 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100167
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500168 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200169 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500170 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200171 } rotation;
172
173 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700174 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500175 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200176 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500177 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100178 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400179 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500180 } popup;
181
Alex Wu4539b082012-03-01 12:57:46 +0800182 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300183 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400184 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300185 } transient;
186
187 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800188 enum wl_shell_surface_fullscreen_method type;
189 struct weston_transform transform; /* matrix from x, y */
190 uint32_t framerate;
191 struct weston_surface *black_surface;
192 } fullscreen;
193
Scott Moreauff1db4a2012-04-17 19:06:18 -0600194 struct ping_timer *ping_timer;
195
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200196 struct weston_transform workspace_transform;
197
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500198 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500199 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100200 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400201
202 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200203};
204
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700206 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207 struct shell_surface *shsurf;
208 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300209 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210};
211
212struct weston_move_grab {
213 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100214 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500215};
216
Pekka Paalanen460099f2012-01-20 16:48:25 +0200217struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300218 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500219 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200220 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200221 float x;
222 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200223 } center;
224};
225
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400226static void
227activate(struct desktop_shell *shell, struct weston_surface *es,
228 struct weston_seat *seat);
229
230static struct workspace *
231get_current_workspace(struct desktop_shell *shell);
232
Alex Wubd3354b2012-04-17 17:20:49 +0800233static struct shell_surface *
234get_shell_surface(struct weston_surface *surface);
235
236static struct desktop_shell *
237shell_surface_get_shell(struct shell_surface *shsurf);
238
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500239static void
240surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
241
Alex Wubd3354b2012-04-17 17:20:49 +0800242static bool
243shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
244{
245 struct desktop_shell *shell;
246 struct weston_surface *top_fs_es;
247
248 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100249
Alex Wubd3354b2012-04-17 17:20:49 +0800250 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
251 return false;
252
253 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100254 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800255 layer_link);
256 return (shsurf == get_shell_surface(top_fs_es));
257}
258
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500259static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400260destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300261{
262 struct shell_grab *grab;
263
264 grab = container_of(listener, struct shell_grab,
265 shsurf_destroy_listener);
266
267 grab->shsurf = NULL;
268}
269
270static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400271popup_grab_end(struct wl_pointer *pointer);
272
273static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300274shell_grab_start(struct shell_grab *grab,
275 const struct wl_pointer_grab_interface *interface,
276 struct shell_surface *shsurf,
277 struct wl_pointer *pointer,
278 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300280 struct desktop_shell *shell = shsurf->shell;
281
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400282 popup_grab_end(pointer);
283
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284 grab->grab.interface = interface;
285 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400286 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
287 wl_signal_add(&shsurf->resource.destroy_signal,
288 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300289
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300290 grab->pointer = pointer;
291 grab->grab.focus = &shsurf->surface->surface;
292
293 wl_pointer_start_grab(pointer, &grab->grab);
294 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
295 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
296 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300297}
298
299static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300300shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300301{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400302 if (grab->shsurf)
303 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300304
305 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306}
307
308static void
Alex Wu4539b082012-03-01 12:57:46 +0800309center_on_output(struct weston_surface *surface,
310 struct weston_output *output);
311
Daniel Stone496ca172012-05-30 16:31:42 +0100312static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300313get_modifier(char *modifier)
314{
315 if (!modifier)
316 return MODIFIER_SUPER;
317
318 if (!strcmp("ctrl", modifier))
319 return MODIFIER_CTRL;
320 else if (!strcmp("alt", modifier))
321 return MODIFIER_ALT;
322 else if (!strcmp("super", modifier))
323 return MODIFIER_SUPER;
324 else
325 return MODIFIER_SUPER;
326}
327
Juan Zhaoe10d2792012-04-25 19:09:52 +0800328static enum animation_type
329get_animation_type(char *animation)
330{
331 if (!animation)
332 return ANIMATION_NONE;
333
334 if (!strcmp("zoom", animation))
335 return ANIMATION_ZOOM;
336 else if (!strcmp("fade", animation))
337 return ANIMATION_FADE;
338 else
339 return ANIMATION_NONE;
340}
341
Alex Wu4539b082012-03-01 12:57:46 +0800342static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300343shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200344{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200345 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200346 char *path = NULL;
347 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200348 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300349 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800350 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200351
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400352 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300353 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800354 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200355 { "num-workspaces",
356 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200357 };
358
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400359 struct config_key saver_keys[] = {
360 { "path", CONFIG_KEY_STRING, &path },
361 { "duration", CONFIG_KEY_INTEGER, &duration },
362 };
363
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200364 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400365 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200366 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
367 };
368
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200369 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500370 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200371 free(config_file);
372
Pekka Paalanen7296e792011-12-07 16:22:00 +0200373 shell->screensaver.path = path;
374 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300375 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800376 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200377 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
378}
379
380static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200381focus_state_destroy(struct focus_state *state)
382{
383 wl_list_remove(&state->seat_destroy_listener.link);
384 wl_list_remove(&state->surface_destroy_listener.link);
385 free(state);
386}
387
388static void
389focus_state_seat_destroy(struct wl_listener *listener, void *data)
390{
391 struct focus_state *state = container_of(listener,
392 struct focus_state,
393 seat_destroy_listener);
394
395 wl_list_remove(&state->link);
396 focus_state_destroy(state);
397}
398
399static void
400focus_state_surface_destroy(struct wl_listener *listener, void *data)
401{
402 struct focus_state *state = container_of(listener,
403 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400404 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400405 struct desktop_shell *shell;
406 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200407
Kristian Høgsberge3778222012-07-31 17:29:30 -0400408 next = NULL;
409 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
410 if (surface == state->keyboard_focus)
411 continue;
412
413 next = surface;
414 break;
415 }
416
417 if (next) {
418 shell = state->seat->compositor->shell_interface.shell;
419 activate(shell, next, state->seat);
420 } else {
421 wl_list_remove(&state->link);
422 focus_state_destroy(state);
423 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200424}
425
426static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400427focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200428{
Jonas Ådahl04769742012-06-13 00:01:24 +0200429 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200430
431 state = malloc(sizeof *state);
432 if (state == NULL)
433 return NULL;
434
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400435 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200436 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400437 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200438
439 state->seat_destroy_listener.notify = focus_state_seat_destroy;
440 state->surface_destroy_listener.notify = focus_state_surface_destroy;
441 wl_signal_add(&seat->seat.destroy_signal,
442 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400443 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200444
445 return state;
446}
447
Jonas Ådahl8538b222012-08-29 22:13:03 +0200448static struct focus_state *
449ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
450{
451 struct workspace *ws = get_current_workspace(shell);
452 struct focus_state *state;
453
454 wl_list_for_each(state, &ws->focus_list, link)
455 if (state->seat == seat)
456 break;
457
458 if (&state->link == &ws->focus_list)
459 state = focus_state_create(seat, ws);
460
461 return state;
462}
463
Jonas Ådahl04769742012-06-13 00:01:24 +0200464static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400465restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200466{
467 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200468 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200469
470 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200471 surface = state->keyboard_focus ?
472 &state->keyboard_focus->surface : NULL;
473
474 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200475 }
476}
477
478static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200479replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
480 struct weston_seat *seat)
481{
482 struct focus_state *state;
483 struct wl_surface *surface;
484
485 wl_list_for_each(state, &ws->focus_list, link) {
486 if (state->seat == seat) {
487 surface = seat->seat.keyboard->focus;
488 state->keyboard_focus =
489 (struct weston_surface *) surface;
490 return;
491 }
492 }
493}
494
495static void
496drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
497 struct weston_surface *surface)
498{
499 struct focus_state *state;
500
501 wl_list_for_each(state, &ws->focus_list, link)
502 if (state->keyboard_focus == surface)
503 state->keyboard_focus = NULL;
504}
505
506static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200507workspace_destroy(struct workspace *ws)
508{
Jonas Ådahl04769742012-06-13 00:01:24 +0200509 struct focus_state *state, *next;
510
511 wl_list_for_each_safe(state, next, &ws->focus_list, link)
512 focus_state_destroy(state);
513
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200514 free(ws);
515}
516
Jonas Ådahl04769742012-06-13 00:01:24 +0200517static void
518seat_destroyed(struct wl_listener *listener, void *data)
519{
520 struct weston_seat *seat = data;
521 struct focus_state *state, *next;
522 struct workspace *ws = container_of(listener,
523 struct workspace,
524 seat_destroyed_listener);
525
526 wl_list_for_each_safe(state, next, &ws->focus_list, link)
527 if (state->seat == seat)
528 wl_list_remove(&state->link);
529}
530
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200531static struct workspace *
532workspace_create(void)
533{
534 struct workspace *ws = malloc(sizeof *ws);
535 if (ws == NULL)
536 return NULL;
537
538 weston_layer_init(&ws->layer, NULL);
539
Jonas Ådahl04769742012-06-13 00:01:24 +0200540 wl_list_init(&ws->focus_list);
541 wl_list_init(&ws->seat_destroyed_listener.link);
542 ws->seat_destroyed_listener.notify = seat_destroyed;
543
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200544 return ws;
545}
546
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200547static int
548workspace_is_empty(struct workspace *ws)
549{
550 return wl_list_empty(&ws->layer.surface_list);
551}
552
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200553static struct workspace *
554get_workspace(struct desktop_shell *shell, unsigned int index)
555{
556 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200557 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200558 pws += index;
559 return *pws;
560}
561
562static struct workspace *
563get_current_workspace(struct desktop_shell *shell)
564{
565 return get_workspace(shell, shell->workspaces.current);
566}
567
568static void
569activate_workspace(struct desktop_shell *shell, unsigned int index)
570{
571 struct workspace *ws;
572
573 ws = get_workspace(shell, index);
574 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
575
576 shell->workspaces.current = index;
577}
578
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200579static unsigned int
580get_output_height(struct weston_output *output)
581{
582 return abs(output->region.extents.y1 - output->region.extents.y2);
583}
584
585static void
586surface_translate(struct weston_surface *surface, double d)
587{
588 struct shell_surface *shsurf = get_shell_surface(surface);
589 struct weston_transform *transform;
590
591 transform = &shsurf->workspace_transform;
592 if (wl_list_empty(&transform->link))
593 wl_list_insert(surface->geometry.transformation_list.prev,
594 &shsurf->workspace_transform.link);
595
596 weston_matrix_init(&shsurf->workspace_transform.matrix);
597 weston_matrix_translate(&shsurf->workspace_transform.matrix,
598 0.0, d, 0.0);
599 surface->geometry.dirty = 1;
600}
601
602static void
603workspace_translate_out(struct workspace *ws, double fraction)
604{
605 struct weston_surface *surface;
606 unsigned int height;
607 double d;
608
609 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
610 height = get_output_height(surface->output);
611 d = height * fraction;
612
613 surface_translate(surface, d);
614 }
615}
616
617static void
618workspace_translate_in(struct workspace *ws, double fraction)
619{
620 struct weston_surface *surface;
621 unsigned int height;
622 double d;
623
624 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
625 height = get_output_height(surface->output);
626
627 if (fraction > 0)
628 d = -(height - height * fraction);
629 else
630 d = height + height * fraction;
631
632 surface_translate(surface, d);
633 }
634}
635
636static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200637broadcast_current_workspace_state(struct desktop_shell *shell)
638{
639 struct wl_resource *resource;
640
641 wl_list_for_each(resource, &shell->workspaces.client_list, link)
642 workspace_manager_send_state(resource,
643 shell->workspaces.current,
644 shell->workspaces.num);
645}
646
647static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200648reverse_workspace_change_animation(struct desktop_shell *shell,
649 unsigned int index,
650 struct workspace *from,
651 struct workspace *to)
652{
653 shell->workspaces.current = index;
654
655 shell->workspaces.anim_to = to;
656 shell->workspaces.anim_from = from;
657 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
658 shell->workspaces.anim_timestamp = 0;
659
Scott Moreau4272e632012-08-13 09:58:41 -0600660 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200661}
662
663static void
664workspace_deactivate_transforms(struct workspace *ws)
665{
666 struct weston_surface *surface;
667 struct shell_surface *shsurf;
668
669 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
670 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200671 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
672 wl_list_remove(&shsurf->workspace_transform.link);
673 wl_list_init(&shsurf->workspace_transform.link);
674 }
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200675 shsurf->surface->geometry.dirty = 1;
676 }
677}
678
679static void
680finish_workspace_change_animation(struct desktop_shell *shell,
681 struct workspace *from,
682 struct workspace *to)
683{
Scott Moreau4272e632012-08-13 09:58:41 -0600684 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200685
686 wl_list_remove(&shell->workspaces.animation.link);
687 workspace_deactivate_transforms(from);
688 workspace_deactivate_transforms(to);
689 shell->workspaces.anim_to = NULL;
690
691 wl_list_remove(&shell->workspaces.anim_from->layer.link);
692}
693
694static void
695animate_workspace_change_frame(struct weston_animation *animation,
696 struct weston_output *output, uint32_t msecs)
697{
698 struct desktop_shell *shell =
699 container_of(animation, struct desktop_shell,
700 workspaces.animation);
701 struct workspace *from = shell->workspaces.anim_from;
702 struct workspace *to = shell->workspaces.anim_to;
703 uint32_t t;
704 double x, y;
705
706 if (workspace_is_empty(from) && workspace_is_empty(to)) {
707 finish_workspace_change_animation(shell, from, to);
708 return;
709 }
710
711 if (shell->workspaces.anim_timestamp == 0) {
712 if (shell->workspaces.anim_current == 0.0)
713 shell->workspaces.anim_timestamp = msecs;
714 else
715 shell->workspaces.anim_timestamp =
716 msecs -
717 /* Invers of movement function 'y' below. */
718 (asin(1.0 - shell->workspaces.anim_current) *
719 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
720 M_2_PI);
721 }
722
723 t = msecs - shell->workspaces.anim_timestamp;
724
725 /*
726 * x = [0, π/2]
727 * y(x) = sin(x)
728 */
729 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
730 y = sin(x);
731
732 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600733 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200734
735 workspace_translate_out(from, shell->workspaces.anim_dir * y);
736 workspace_translate_in(to, shell->workspaces.anim_dir * y);
737 shell->workspaces.anim_current = y;
738
Scott Moreau4272e632012-08-13 09:58:41 -0600739 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200740 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200741 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200742 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200743}
744
745static void
746animate_workspace_change(struct desktop_shell *shell,
747 unsigned int index,
748 struct workspace *from,
749 struct workspace *to)
750{
751 struct weston_output *output;
752
753 int dir;
754
755 if (index > shell->workspaces.current)
756 dir = -1;
757 else
758 dir = 1;
759
760 shell->workspaces.current = index;
761
762 shell->workspaces.anim_dir = dir;
763 shell->workspaces.anim_from = from;
764 shell->workspaces.anim_to = to;
765 shell->workspaces.anim_current = 0.0;
766 shell->workspaces.anim_timestamp = 0;
767
768 output = container_of(shell->compositor->output_list.next,
769 struct weston_output, link);
770 wl_list_insert(&output->animation_list,
771 &shell->workspaces.animation.link);
772
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200773 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200774
775 workspace_translate_in(to, 0);
776
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400777 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200778
Scott Moreau4272e632012-08-13 09:58:41 -0600779 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200780}
781
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200782static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200783update_workspace(struct desktop_shell *shell, unsigned int index,
784 struct workspace *from, struct workspace *to)
785{
786 shell->workspaces.current = index;
787 wl_list_insert(&from->layer.link, &to->layer.link);
788 wl_list_remove(&from->layer.link);
789}
790
791static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200792change_workspace(struct desktop_shell *shell, unsigned int index)
793{
794 struct workspace *from;
795 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200796
797 if (index == shell->workspaces.current)
798 return;
799
800 /* Don't change workspace when there is any fullscreen surfaces. */
801 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
802 return;
803
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200804 from = get_current_workspace(shell);
805 to = get_workspace(shell, index);
806
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200807 if (shell->workspaces.anim_from == to &&
808 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200809 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200810 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200811 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200812 return;
813 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200814
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200815 if (shell->workspaces.anim_to != NULL)
816 finish_workspace_change_animation(shell,
817 shell->workspaces.anim_from,
818 shell->workspaces.anim_to);
819
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200820 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200821
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200822 if (workspace_is_empty(to) && workspace_is_empty(from))
823 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200824 else
825 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200826
827 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200828}
829
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200830static bool
831workspace_has_only(struct workspace *ws, struct weston_surface *surface)
832{
833 struct wl_list *list = &ws->layer.surface_list;
834 struct wl_list *e;
835
836 if (wl_list_empty(list))
837 return false;
838
839 e = list->next;
840
841 if (e->next != list)
842 return false;
843
844 return container_of(e, struct weston_surface, layer_link) == surface;
845}
846
847static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200848move_surface_to_workspace(struct desktop_shell *shell,
849 struct weston_surface *surface,
850 uint32_t workspace)
851{
852 struct workspace *from;
853 struct workspace *to;
854 struct weston_seat *seat;
855
856 if (workspace == shell->workspaces.current)
857 return;
858
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200859 if (workspace >= shell->workspaces.num)
860 workspace = shell->workspaces.num - 1;
861
Jonas Ådahle9d22502012-08-29 22:13:01 +0200862 from = get_current_workspace(shell);
863 to = get_workspace(shell, workspace);
864
865 wl_list_remove(&surface->layer_link);
866 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
867
868 drop_focus_state(shell, from, surface);
869 wl_list_for_each(seat, &shell->compositor->seat_list, link)
870 if (seat->has_keyboard &&
871 seat->keyboard.focus == &surface->surface)
872 wl_keyboard_set_focus(&seat->keyboard, NULL);
873
874 weston_surface_damage_below(surface);
875}
876
877static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200878take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200879 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200880 unsigned int index)
881{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200882 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200883 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200884 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200885 struct shell_surface *shsurf;
886 struct workspace *from;
887 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200888 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200889
890 if (surface == NULL ||
891 index == shell->workspaces.current)
892 return;
893
894 from = get_current_workspace(shell);
895 to = get_workspace(shell, index);
896
897 wl_list_remove(&surface->layer_link);
898 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
899
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200901 drop_focus_state(shell, from, surface);
902
903 if (shell->workspaces.anim_from == to &&
904 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200905 wl_list_remove(&to->layer.link);
906 wl_list_insert(from->layer.link.prev, &to->layer.link);
907
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200908 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200909 broadcast_current_workspace_state(shell);
910
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200911 return;
912 }
913
914 if (shell->workspaces.anim_to != NULL)
915 finish_workspace_change_animation(shell,
916 shell->workspaces.anim_from,
917 shell->workspaces.anim_to);
918
919 if (workspace_is_empty(from) &&
920 workspace_has_only(to, surface))
921 update_workspace(shell, index, from, to);
922 else {
923 shsurf = get_shell_surface(surface);
924 if (wl_list_empty(&shsurf->workspace_transform.link))
925 wl_list_insert(&shell->workspaces.anim_sticky_list,
926 &shsurf->workspace_transform.link);
927
928 animate_workspace_change(shell, index, from, to);
929 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200930
931 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200932
933 state = ensure_focus_state(shell, seat);
934 if (state != NULL)
935 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200936}
937
938static void
939workspace_manager_move_surface(struct wl_client *client,
940 struct wl_resource *resource,
941 struct wl_resource *surface_resource,
942 uint32_t workspace)
943{
944 struct desktop_shell *shell = resource->data;
945 struct weston_surface *surface =
946 (struct weston_surface *) surface_resource;
947
948 move_surface_to_workspace(shell, surface, workspace);
949}
950
951static const struct workspace_manager_interface workspace_manager_implementation = {
952 workspace_manager_move_surface,
953};
954
955static void
956unbind_resource(struct wl_resource *resource)
957{
958 wl_list_remove(&resource->link);
959 free(resource);
960}
961
962static void
963bind_workspace_manager(struct wl_client *client,
964 void *data, uint32_t version, uint32_t id)
965{
966 struct desktop_shell *shell = data;
967 struct wl_resource *resource;
968
969 resource = wl_client_add_object(client, &workspace_manager_interface,
970 &workspace_manager_implementation,
971 id, shell);
972
973 if (resource == NULL) {
974 weston_log("couldn't add workspace manager object");
975 return;
976 }
977
978 resource->destroy = unbind_resource;
979 wl_list_insert(&shell->workspaces.client_list, &resource->link);
980
981 workspace_manager_send_state(resource,
982 shell->workspaces.current,
983 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200984}
985
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200986static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400987noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100988 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500989{
990 grab->focus = NULL;
991}
992
993static void
Scott Moreau447013d2012-02-18 05:05:29 -0700994move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100995 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500996{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500997 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100998 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300999 struct shell_surface *shsurf = move->base.shsurf;
1000 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001001 int dx = wl_fixed_to_int(pointer->x + move->dx);
1002 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001003
1004 if (!shsurf)
1005 return;
1006
1007 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001008
Daniel Stone103db7f2012-05-08 17:17:55 +01001009 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001010 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001011
1012 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001013}
1014
1015static void
Scott Moreau447013d2012-02-18 05:05:29 -07001016move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001017 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001018{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001019 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1020 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001021 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001022 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023
Daniel Stone4dbadb12012-05-30 16:31:51 +01001024 if (pointer->button_count == 0 &&
1025 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001026 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001027 free(grab);
1028 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001029}
1030
Scott Moreau447013d2012-02-18 05:05:29 -07001031static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001032 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001033 move_grab_motion,
1034 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001035};
1036
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001037static int
1038surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1039{
1040 struct weston_move_grab *move;
1041
1042 if (!shsurf)
1043 return -1;
1044
1045 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1046 return 0;
1047
1048 move = malloc(sizeof *move);
1049 if (!move)
1050 return -1;
1051
1052 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1053 ws->seat.pointer->grab_x;
1054 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1055 ws->seat.pointer->grab_y;
1056
1057 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1058 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1059
1060 return 0;
1061}
1062
1063static void
1064shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1065 struct wl_resource *seat_resource, uint32_t serial)
1066{
1067 struct weston_seat *ws = seat_resource->data;
1068 struct shell_surface *shsurf = resource->data;
1069
1070 if (ws->seat.pointer->button_count == 0 ||
1071 ws->seat.pointer->grab_serial != serial ||
1072 ws->seat.pointer->focus != &shsurf->surface->surface)
1073 return;
1074
1075 if (surface_move(shsurf, ws) < 0)
1076 wl_resource_post_no_memory(resource);
1077}
1078
1079struct weston_resize_grab {
1080 struct shell_grab base;
1081 uint32_t edges;
1082 int32_t width, height;
1083};
1084
1085static void
1086resize_grab_motion(struct wl_pointer_grab *grab,
1087 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1088{
1089 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1090 struct wl_pointer *pointer = grab->pointer;
1091 struct shell_surface *shsurf = resize->base.shsurf;
1092 int32_t width, height;
1093 wl_fixed_t from_x, from_y;
1094 wl_fixed_t to_x, to_y;
1095
1096 if (!shsurf)
1097 return;
1098
1099 weston_surface_from_global_fixed(shsurf->surface,
1100 pointer->grab_x, pointer->grab_y,
1101 &from_x, &from_y);
1102 weston_surface_from_global_fixed(shsurf->surface,
1103 pointer->x, pointer->y, &to_x, &to_y);
1104
1105 width = resize->width;
1106 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1107 width += wl_fixed_to_int(from_x - to_x);
1108 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1109 width += wl_fixed_to_int(to_x - from_x);
1110 }
1111
1112 height = resize->height;
1113 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1114 height += wl_fixed_to_int(from_y - to_y);
1115 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1116 height += wl_fixed_to_int(to_y - from_y);
1117 }
1118
1119 shsurf->client->send_configure(shsurf->surface,
1120 resize->edges, width, height);
1121}
1122
1123static void
1124send_configure(struct weston_surface *surface,
1125 uint32_t edges, int32_t width, int32_t height)
1126{
1127 struct shell_surface *shsurf = get_shell_surface(surface);
1128
1129 wl_shell_surface_send_configure(&shsurf->resource,
1130 edges, width, height);
1131}
1132
1133static const struct weston_shell_client shell_client = {
1134 send_configure
1135};
1136
1137static void
1138resize_grab_button(struct wl_pointer_grab *grab,
1139 uint32_t time, uint32_t button, uint32_t state_w)
1140{
1141 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1142 struct wl_pointer *pointer = grab->pointer;
1143 enum wl_pointer_button_state state = state_w;
1144
1145 if (pointer->button_count == 0 &&
1146 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1147 shell_grab_end(&resize->base);
1148 free(grab);
1149 }
1150}
1151
1152static const struct wl_pointer_grab_interface resize_grab_interface = {
1153 noop_grab_focus,
1154 resize_grab_motion,
1155 resize_grab_button,
1156};
1157
1158static int
1159surface_resize(struct shell_surface *shsurf,
1160 struct weston_seat *ws, uint32_t edges)
1161{
1162 struct weston_resize_grab *resize;
1163
1164 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1165 return 0;
1166
1167 if (edges == 0 || edges > 15 ||
1168 (edges & 3) == 3 || (edges & 12) == 12)
1169 return 0;
1170
1171 resize = malloc(sizeof *resize);
1172 if (!resize)
1173 return -1;
1174
1175 resize->edges = edges;
1176 resize->width = shsurf->surface->geometry.width;
1177 resize->height = shsurf->surface->geometry.height;
1178
1179 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1180 ws->seat.pointer, edges);
1181
1182 return 0;
1183}
1184
1185static void
1186shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1187 struct wl_resource *seat_resource, uint32_t serial,
1188 uint32_t edges)
1189{
1190 struct weston_seat *ws = seat_resource->data;
1191 struct shell_surface *shsurf = resource->data;
1192
1193 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1194 return;
1195
1196 if (ws->seat.pointer->button_count == 0 ||
1197 ws->seat.pointer->grab_serial != serial ||
1198 ws->seat.pointer->focus != &shsurf->surface->surface)
1199 return;
1200
1201 if (surface_resize(shsurf, ws, edges) < 0)
1202 wl_resource_post_no_memory(resource);
1203}
1204
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001205static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001206busy_cursor_grab_focus(struct wl_pointer_grab *base,
1207 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001208{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001209 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001210
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001211 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001212 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001213 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001214 }
1215}
1216
1217static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001218busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1219 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001220{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001221}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001222
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001223static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001224busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001225 uint32_t time, uint32_t button, uint32_t state)
1226{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001227 struct shell_grab *grab = (struct shell_grab *) base;
1228 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001229 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001230 (struct weston_surface *) grab->grab.pointer->current;
1231 struct weston_seat *seat =
1232 (struct weston_seat *) grab->grab.pointer->seat;
1233
1234 shsurf = get_shell_surface(surface);
1235 if (shsurf && button == BTN_LEFT && state) {
1236 activate(shsurf->shell, shsurf->surface, seat);
1237 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001238 } else if (shsurf && button == BTN_RIGHT && state) {
1239 activate(shsurf->shell, shsurf->surface, seat);
1240 surface_rotate(shsurf, &seat->seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001241 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001242}
1243
1244static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1245 busy_cursor_grab_focus,
1246 busy_cursor_grab_motion,
1247 busy_cursor_grab_button,
1248};
1249
1250static void
1251set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1252{
1253 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001254
1255 grab = malloc(sizeof *grab);
1256 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001257 return;
1258
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001259 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1260 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001261}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001262
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001263static void
1264end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1265{
1266 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1267
1268 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001269 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001270 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001271 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001272}
1273
Scott Moreau9521d5e2012-04-19 13:06:17 -06001274static void
1275ping_timer_destroy(struct shell_surface *shsurf)
1276{
1277 if (!shsurf || !shsurf->ping_timer)
1278 return;
1279
1280 if (shsurf->ping_timer->source)
1281 wl_event_source_remove(shsurf->ping_timer->source);
1282
1283 free(shsurf->ping_timer);
1284 shsurf->ping_timer = NULL;
1285}
1286
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001287static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001288ping_timeout_handler(void *data)
1289{
1290 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001291 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001292
Scott Moreau9521d5e2012-04-19 13:06:17 -06001293 /* Client is not responding */
1294 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001295
1296 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1297 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1298 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001299
1300 return 1;
1301}
1302
1303static void
1304ping_handler(struct weston_surface *surface, uint32_t serial)
1305{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001306 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001307 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001308 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001309
1310 if (!shsurf)
1311 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001312 if (!shsurf->resource.client)
1313 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001314
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001315 if (shsurf->surface == shsurf->shell->grab_surface)
1316 return;
1317
Scott Moreauff1db4a2012-04-17 19:06:18 -06001318 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001319 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001320 if (!shsurf->ping_timer)
1321 return;
1322
1323 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001324 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1325 shsurf->ping_timer->source =
1326 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1327 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1328
1329 wl_shell_surface_send_ping(&shsurf->resource, serial);
1330 }
1331}
1332
1333static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001334handle_pointer_focus(struct wl_listener *listener, void *data)
1335{
1336 struct wl_pointer *pointer = data;
1337 struct weston_surface *surface =
1338 (struct weston_surface *) pointer->focus;
1339 struct weston_compositor *compositor;
1340 struct shell_surface *shsurf;
1341 uint32_t serial;
1342
1343 if (!surface)
1344 return;
1345
1346 compositor = surface->compositor;
1347 shsurf = get_shell_surface(surface);
1348
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001349 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001350 set_busy_cursor(shsurf, pointer);
1351 } else {
1352 serial = wl_display_next_serial(compositor->wl_display);
1353 ping_handler(surface, serial);
1354 }
1355}
1356
1357static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001358create_pointer_focus_listener(struct weston_seat *seat)
1359{
1360 struct wl_listener *listener;
1361
1362 if (!seat->seat.pointer)
1363 return;
1364
1365 listener = malloc(sizeof *listener);
1366 listener->notify = handle_pointer_focus;
1367 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1368}
1369
1370static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001371shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1372 uint32_t serial)
1373{
1374 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001375 struct desktop_shell *shell = shsurf->shell;
1376 struct weston_seat *seat;
1377 struct weston_compositor *ec = shsurf->surface->compositor;
1378 struct wl_pointer *pointer;
1379 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001380
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001381 if (shsurf->ping_timer == NULL)
1382 /* Just ignore unsolicited pong. */
1383 return;
1384
Scott Moreauff1db4a2012-04-17 19:06:18 -06001385 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001386 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001387 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001388 if (was_unresponsive) {
1389 /* Received pong from previously unresponsive client */
1390 wl_list_for_each(seat, &ec->seat_list, link) {
1391 pointer = seat->seat.pointer;
1392 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001393 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001394 pointer->current ==
1395 &shsurf->surface->surface)
1396 end_busy_cursor(shsurf, pointer);
1397 }
1398 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001399 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001400 }
1401}
1402
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001403static void
1404shell_surface_set_title(struct wl_client *client,
1405 struct wl_resource *resource, const char *title)
1406{
1407 struct shell_surface *shsurf = resource->data;
1408
1409 free(shsurf->title);
1410 shsurf->title = strdup(title);
1411}
1412
1413static void
1414shell_surface_set_class(struct wl_client *client,
1415 struct wl_resource *resource, const char *class)
1416{
1417 struct shell_surface *shsurf = resource->data;
1418
1419 free(shsurf->class);
1420 shsurf->class = strdup(class);
1421}
1422
Juan Zhao96879df2012-02-07 08:45:41 +08001423static struct weston_output *
1424get_default_output(struct weston_compositor *compositor)
1425{
1426 return container_of(compositor->output_list.next,
1427 struct weston_output, link);
1428}
1429
Alex Wu4539b082012-03-01 12:57:46 +08001430static void
1431shell_unset_fullscreen(struct shell_surface *shsurf)
1432{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001433 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001434 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001435 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1436 shell_surface_is_top_fullscreen(shsurf)) {
1437 weston_output_switch_mode(shsurf->fullscreen_output,
1438 shsurf->fullscreen_output->origin);
1439 }
Alex Wu4539b082012-03-01 12:57:46 +08001440 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1441 shsurf->fullscreen.framerate = 0;
1442 wl_list_remove(&shsurf->fullscreen.transform.link);
1443 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001444 if (shsurf->fullscreen.black_surface)
1445 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001446 shsurf->fullscreen.black_surface = NULL;
1447 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001448 weston_surface_set_position(shsurf->surface,
1449 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001450 if (shsurf->saved_rotation_valid) {
1451 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1452 &shsurf->rotation.transform.link);
1453 shsurf->saved_rotation_valid = false;
1454 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001455
1456 ws = get_current_workspace(shsurf->shell);
1457 wl_list_remove(&shsurf->surface->layer_link);
1458 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001459}
1460
Pekka Paalanen98262232011-12-01 10:42:22 +02001461static int
1462reset_shell_surface_type(struct shell_surface *surface)
1463{
1464 switch (surface->type) {
1465 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001466 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001467 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001468 case SHELL_SURFACE_MAXIMIZED:
1469 surface->output = get_default_output(surface->surface->compositor);
1470 weston_surface_set_position(surface->surface,
1471 surface->saved_x,
1472 surface->saved_y);
1473 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001474 case SHELL_SURFACE_NONE:
1475 case SHELL_SURFACE_TOPLEVEL:
1476 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001477 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001478 break;
1479 }
1480
1481 surface->type = SHELL_SURFACE_NONE;
1482 return 0;
1483}
1484
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001485static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001486set_surface_type(struct shell_surface *shsurf)
1487{
1488 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001489 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001490
1491 reset_shell_surface_type(shsurf);
1492
1493 shsurf->type = shsurf->next_type;
1494 shsurf->next_type = SHELL_SURFACE_NONE;
1495
1496 switch (shsurf->type) {
1497 case SHELL_SURFACE_TOPLEVEL:
1498 break;
1499 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001500 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001501 pes->geometry.x + shsurf->transient.x,
1502 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001503 break;
1504
1505 case SHELL_SURFACE_MAXIMIZED:
1506 shsurf->saved_x = surface->geometry.x;
1507 shsurf->saved_y = surface->geometry.y;
1508 shsurf->saved_position_valid = true;
1509 break;
1510
1511 case SHELL_SURFACE_FULLSCREEN:
1512 shsurf->saved_x = surface->geometry.x;
1513 shsurf->saved_y = surface->geometry.y;
1514 shsurf->saved_position_valid = true;
1515
1516 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1517 wl_list_remove(&shsurf->rotation.transform.link);
1518 wl_list_init(&shsurf->rotation.transform.link);
1519 shsurf->surface->geometry.dirty = 1;
1520 shsurf->saved_rotation_valid = true;
1521 }
1522 break;
1523
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001524 default:
1525 break;
1526 }
1527}
1528
1529static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001530set_toplevel(struct shell_surface *shsurf)
1531{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001532 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001533}
1534
1535static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001536shell_surface_set_toplevel(struct wl_client *client,
1537 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001538{
Pekka Paalanen98262232011-12-01 10:42:22 +02001539 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001540
Tiago Vignattibc052c92012-04-19 16:18:18 +03001541 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001542}
1543
1544static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001545set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001546 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001547{
1548 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001549 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001550 shsurf->transient.x = x;
1551 shsurf->transient.y = y;
1552 shsurf->transient.flags = flags;
1553 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1554}
1555
1556static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001557shell_surface_set_transient(struct wl_client *client,
1558 struct wl_resource *resource,
1559 struct wl_resource *parent_resource,
1560 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001561{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001562 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001563 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001564
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001565 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001566}
1567
Tiago Vignattibe143262012-04-16 17:31:41 +03001568static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001569shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001570{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001571 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001572}
1573
1574static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001575get_output_panel_height(struct desktop_shell *shell,
1576 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001577{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001578 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001579 int panel_height = 0;
1580
1581 if (!output)
1582 return 0;
1583
Juan Zhao4ab94682012-07-09 22:24:09 -07001584 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001585 if (surface->output == output) {
1586 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001587 break;
1588 }
1589 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001590
Juan Zhao96879df2012-02-07 08:45:41 +08001591 return panel_height;
1592}
1593
1594static void
1595shell_surface_set_maximized(struct wl_client *client,
1596 struct wl_resource *resource,
1597 struct wl_resource *output_resource )
1598{
1599 struct shell_surface *shsurf = resource->data;
1600 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001601 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001602 uint32_t edges = 0, panel_height = 0;
1603
1604 /* get the default output, if the client set it as NULL
1605 check whether the ouput is available */
1606 if (output_resource)
1607 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001608 else if (es->output)
1609 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001610 else
1611 shsurf->output = get_default_output(es->compositor);
1612
Tiago Vignattibe143262012-04-16 17:31:41 +03001613 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001614 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001615 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001616
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001617 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001618 shsurf->output->width,
1619 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001620
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001621 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001622}
1623
Alex Wu21858432012-04-01 20:13:08 +08001624static void
1625black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1626
Alex Wu4539b082012-03-01 12:57:46 +08001627static struct weston_surface *
1628create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001629 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001630 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001631{
1632 struct weston_surface *surface = NULL;
1633
1634 surface = weston_surface_create(ec);
1635 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001636 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001637 return NULL;
1638 }
1639
Alex Wu21858432012-04-01 20:13:08 +08001640 surface->configure = black_surface_configure;
1641 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001642 weston_surface_configure(surface, x, y, w, h);
1643 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001644 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001645 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001646 pixman_region32_fini(&surface->input);
1647 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001648
Alex Wu4539b082012-03-01 12:57:46 +08001649 return surface;
1650}
1651
1652/* Create black surface and append it to the associated fullscreen surface.
1653 * Handle size dismatch and positioning according to the method. */
1654static void
1655shell_configure_fullscreen(struct shell_surface *shsurf)
1656{
1657 struct weston_output *output = shsurf->fullscreen_output;
1658 struct weston_surface *surface = shsurf->surface;
1659 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001660 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001661
Alex Wu4539b082012-03-01 12:57:46 +08001662 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001663 shsurf->fullscreen.black_surface =
1664 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001665 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001666 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001667 output->width,
1668 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001669
1670 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1671 wl_list_insert(&surface->layer_link,
1672 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001673 shsurf->fullscreen.black_surface->output = output;
1674
1675 switch (shsurf->fullscreen.type) {
1676 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001677 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001678 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001679 break;
1680 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001681 /* 1:1 mapping between surface and output dimensions */
1682 if (output->width == surface->geometry.width &&
1683 output->height == surface->geometry.height) {
1684 weston_surface_set_position(surface, output->x, output->y);
1685 break;
1686 }
1687
Alex Wu4539b082012-03-01 12:57:46 +08001688 matrix = &shsurf->fullscreen.transform.matrix;
1689 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001690
Scott Moreau1bad5db2012-08-18 01:04:05 -06001691 output_aspect = (float) output->width /
1692 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001693 surface_aspect = (float) surface->geometry.width /
1694 (float) surface->geometry.height;
1695 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001696 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001697 (float) surface->geometry.width;
1698 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001699 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001700 (float) surface->geometry.height;
1701
Alex Wu4539b082012-03-01 12:57:46 +08001702 weston_matrix_scale(matrix, scale, scale, 1);
1703 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001704 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001705 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001706 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1707 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001708 weston_surface_set_position(surface, x, y);
1709
Alex Wu4539b082012-03-01 12:57:46 +08001710 break;
1711 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001712 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001713 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001714 surface->geometry.width,
1715 surface->geometry.height,
1716 shsurf->fullscreen.framerate};
1717
1718 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001719 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001720 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001721 output->width,
1722 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001723 weston_surface_set_position(surface, output->x, output->y);
1724 break;
1725 }
1726 }
Alex Wu4539b082012-03-01 12:57:46 +08001727 break;
1728 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1729 break;
1730 default:
1731 break;
1732 }
1733}
1734
1735/* make the fullscreen and black surface at the top */
1736static void
1737shell_stack_fullscreen(struct shell_surface *shsurf)
1738{
Alex Wubd3354b2012-04-17 17:20:49 +08001739 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001740 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001741 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001742
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001743 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001744 wl_list_insert(&shell->fullscreen_layer.surface_list,
1745 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001746 weston_surface_damage(surface);
1747
1748 if (!shsurf->fullscreen.black_surface)
1749 shsurf->fullscreen.black_surface =
1750 create_black_surface(surface->compositor,
1751 surface,
1752 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001753 output->width,
1754 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001755
1756 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001757 wl_list_insert(&surface->layer_link,
1758 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001759 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001760}
1761
1762static void
1763shell_map_fullscreen(struct shell_surface *shsurf)
1764{
Alex Wu4539b082012-03-01 12:57:46 +08001765 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001766 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001767}
1768
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001769static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001770set_fullscreen(struct shell_surface *shsurf,
1771 uint32_t method,
1772 uint32_t framerate,
1773 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001774{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001775 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001776
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001777 if (output)
1778 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001779 else if (es->output)
1780 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001781 else
1782 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001783
Alex Wu4539b082012-03-01 12:57:46 +08001784 shsurf->fullscreen_output = shsurf->output;
1785 shsurf->fullscreen.type = method;
1786 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001787 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001788
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001789 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001790 shsurf->output->width,
1791 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001792}
1793
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001794static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001795shell_surface_set_fullscreen(struct wl_client *client,
1796 struct wl_resource *resource,
1797 uint32_t method,
1798 uint32_t framerate,
1799 struct wl_resource *output_resource)
1800{
1801 struct shell_surface *shsurf = resource->data;
1802 struct weston_output *output;
1803
1804 if (output_resource)
1805 output = output_resource->data;
1806 else
1807 output = NULL;
1808
1809 set_fullscreen(shsurf, method, framerate, output);
1810}
1811
1812static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001813popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001814 struct wl_surface *surface,
1815 wl_fixed_t x,
1816 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001817{
Daniel Stone37816df2012-05-16 18:45:18 +01001818 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001819 struct shell_surface *priv =
1820 container_of(grab, struct shell_surface, popup.grab);
1821 struct wl_client *client = priv->surface->surface.resource.client;
1822
Pekka Paalanencb108432012-01-19 16:25:40 +02001823 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001824 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001825 grab->focus = surface;
1826 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001827 wl_pointer_set_focus(pointer, NULL,
1828 wl_fixed_from_int(0),
1829 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001830 grab->focus = NULL;
1831 }
1832}
1833
1834static void
Scott Moreau447013d2012-02-18 05:05:29 -07001835popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001836 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001837{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001838 struct wl_resource *resource;
1839
Daniel Stone37816df2012-05-16 18:45:18 +01001840 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001841 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001842 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001843}
1844
1845static void
Scott Moreau447013d2012-02-18 05:05:29 -07001846popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001847 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001848{
1849 struct wl_resource *resource;
1850 struct shell_surface *shsurf =
1851 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001852 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001853 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001854 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001855
Daniel Stone37816df2012-05-16 18:45:18 +01001856 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001857 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001858 display = wl_client_get_display(resource->client);
1859 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001860 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001861 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001862 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001863 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001864 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001865 }
1866
Daniel Stone4dbadb12012-05-30 16:31:51 +01001867 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001868 shsurf->popup.initial_up = 1;
1869}
1870
Scott Moreau447013d2012-02-18 05:05:29 -07001871static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001872 popup_grab_focus,
1873 popup_grab_motion,
1874 popup_grab_button,
1875};
1876
1877static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001878popup_grab_end(struct wl_pointer *pointer)
1879{
1880 struct wl_pointer_grab *grab = pointer->grab;
1881 struct shell_surface *shsurf =
1882 container_of(grab, struct shell_surface, popup.grab);
1883
1884 if (pointer->grab->interface == &popup_grab_interface) {
1885 wl_shell_surface_send_popup_done(&shsurf->resource);
1886 wl_pointer_end_grab(grab->pointer);
1887 shsurf->popup.grab.pointer = NULL;
1888 }
1889}
1890
1891static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001892shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001893{
Daniel Stone37816df2012-05-16 18:45:18 +01001894 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001895 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001896 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001897
1898 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001899 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001900
Pekka Paalanen938269a2012-02-07 14:19:01 +02001901 weston_surface_update_transform(parent);
1902 if (parent->transform.enabled) {
1903 shsurf->popup.parent_transform.matrix =
1904 parent->transform.matrix;
1905 } else {
1906 /* construct x, y translation matrix */
1907 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03001908 shsurf->popup.parent_transform.matrix.type =
1909 WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen938269a2012-02-07 14:19:01 +02001910 shsurf->popup.parent_transform.matrix.d[12] =
1911 parent->geometry.x;
1912 shsurf->popup.parent_transform.matrix.d[13] =
1913 parent->geometry.y;
1914 }
1915 wl_list_insert(es->geometry.transformation_list.prev,
1916 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001917
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001918 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001919 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1920 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001921
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001922 /* We don't require the grab to still be active, but if another
1923 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001924 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1925 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001926 } else {
1927 wl_shell_surface_send_popup_done(&shsurf->resource);
1928 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001929}
1930
1931static void
1932shell_surface_set_popup(struct wl_client *client,
1933 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001934 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001935 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001936 struct wl_resource *parent_resource,
1937 int32_t x, int32_t y, uint32_t flags)
1938{
1939 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001940
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001941 shsurf->type = SHELL_SURFACE_POPUP;
1942 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001943 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001944 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001945 shsurf->popup.x = x;
1946 shsurf->popup.y = y;
1947}
1948
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001949static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001950 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001951 shell_surface_move,
1952 shell_surface_resize,
1953 shell_surface_set_toplevel,
1954 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001955 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001956 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001957 shell_surface_set_maximized,
1958 shell_surface_set_title,
1959 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001960};
1961
1962static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001963destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001964{
Daniel Stone37816df2012-05-16 18:45:18 +01001965 if (shsurf->popup.grab.pointer)
1966 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001967
Alex Wubd3354b2012-04-17 17:20:49 +08001968 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1969 shell_surface_is_top_fullscreen(shsurf)) {
1970 weston_output_switch_mode(shsurf->fullscreen_output,
1971 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001972 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001973
Alex Wuaa08e2d2012-03-05 11:01:40 +08001974 if (shsurf->fullscreen.black_surface)
1975 weston_surface_destroy(shsurf->fullscreen.black_surface);
1976
Alex Wubd3354b2012-04-17 17:20:49 +08001977 /* As destroy_resource() use wl_list_for_each_safe(),
1978 * we can always remove the listener.
1979 */
1980 wl_list_remove(&shsurf->surface_destroy_listener.link);
1981 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001982 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001983
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001984 wl_list_remove(&shsurf->link);
1985 free(shsurf);
1986}
1987
1988static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001989shell_destroy_shell_surface(struct wl_resource *resource)
1990{
1991 struct shell_surface *shsurf = resource->data;
1992
1993 destroy_shell_surface(shsurf);
1994}
1995
1996static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001997shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001998{
1999 struct shell_surface *shsurf = container_of(listener,
2000 struct shell_surface,
2001 surface_destroy_listener);
2002
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002003 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002004 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002005 } else {
2006 wl_signal_emit(&shsurf->resource.destroy_signal,
2007 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002008 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002009 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002010}
2011
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002012static void
2013shell_surface_configure(struct weston_surface *, int32_t, int32_t);
2014
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002015static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002016get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002017{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002018 if (surface->configure == shell_surface_configure)
2019 return surface->private;
2020 else
2021 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002022}
2023
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002024static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002025create_shell_surface(void *shell, struct weston_surface *surface,
2026 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002027{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002028 struct shell_surface *shsurf;
2029
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002030 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002031 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002032 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002033 }
2034
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002035 shsurf = calloc(1, sizeof *shsurf);
2036 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002037 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002038 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002039 }
2040
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002041 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002042 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002043
Tiago Vignattibc052c92012-04-19 16:18:18 +03002044 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002045 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002046 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002047 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002048 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002049 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2050 shsurf->fullscreen.framerate = 0;
2051 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002052 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002053 wl_list_init(&shsurf->fullscreen.transform.link);
2054
Tiago Vignattibc052c92012-04-19 16:18:18 +03002055 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002056 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2057 wl_signal_add(&surface->surface.resource.destroy_signal,
2058 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002059
2060 /* init link so its safe to always remove it in destroy_shell_surface */
2061 wl_list_init(&shsurf->link);
2062
Pekka Paalanen460099f2012-01-20 16:48:25 +02002063 /* empty when not in use */
2064 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002065 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002066
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002067 wl_list_init(&shsurf->workspace_transform.link);
2068
Pekka Paalanen98262232011-12-01 10:42:22 +02002069 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002070 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002071
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002072 shsurf->client = client;
2073
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002074 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002075}
2076
2077static void
2078shell_get_shell_surface(struct wl_client *client,
2079 struct wl_resource *resource,
2080 uint32_t id,
2081 struct wl_resource *surface_resource)
2082{
2083 struct weston_surface *surface = surface_resource->data;
2084 struct desktop_shell *shell = resource->data;
2085 struct shell_surface *shsurf;
2086
2087 if (get_shell_surface(surface)) {
2088 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002089 WL_DISPLAY_ERROR_INVALID_OBJECT,
2090 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002091 return;
2092 }
2093
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002094 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002095 if (!shsurf) {
2096 wl_resource_post_error(surface_resource,
2097 WL_DISPLAY_ERROR_INVALID_OBJECT,
2098 "surface->configure already set");
2099 return;
2100 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002101
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002102 shsurf->resource.destroy = shell_destroy_shell_surface;
2103 shsurf->resource.object.id = id;
2104 shsurf->resource.object.interface = &wl_shell_surface_interface;
2105 shsurf->resource.object.implementation =
2106 (void (**)(void)) &shell_surface_implementation;
2107 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002108
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002109 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002110}
2111
2112static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002113 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002114};
2115
Kristian Høgsberg07937562011-04-12 17:25:42 -04002116static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002117handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002118{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002119 struct desktop_shell *shell =
2120 container_of(proc, struct desktop_shell, screensaver.process);
2121 struct weston_output *output;
2122
Pekka Paalanen18027e52011-12-02 16:31:49 +02002123 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002124
2125 if (shell->locked)
2126 wl_list_for_each(output, &shell->compositor->output_list, link)
2127 if (output->set_dpms)
2128 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002129}
2130
2131static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002132launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002133{
2134 if (shell->screensaver.binding)
2135 return;
2136
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002137 if (!shell->screensaver.path)
2138 return;
2139
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002140 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002141 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002142 return;
2143 }
2144
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002145 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002146 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002147 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002148 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002149}
2150
2151static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002152terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002153{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002154 if (shell->screensaver.process.pid == 0)
2155 return;
2156
2157 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002158}
2159
2160static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002161configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002162{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002163 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002164
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002165 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2166 if (s->output == es->output && s != es) {
2167 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002168 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002169 }
2170 }
2171
2172 weston_surface_configure(es, es->output->x, es->output->y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002173 weston_surface_buffer_width(es),
2174 weston_surface_buffer_height(es));
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002175
2176 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002177 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002178 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002179 }
2180}
2181
2182static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002183background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2184{
2185 struct desktop_shell *shell = es->private;
2186
2187 configure_static_surface(es, &shell->background_layer);
2188}
2189
2190static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002191desktop_shell_set_background(struct wl_client *client,
2192 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002193 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002194 struct wl_resource *surface_resource)
2195{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002196 struct desktop_shell *shell = resource->data;
2197 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002198
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002199 if (surface->configure) {
2200 wl_resource_post_error(surface_resource,
2201 WL_DISPLAY_ERROR_INVALID_OBJECT,
2202 "surface role already assigned");
2203 return;
2204 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002205
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002206 surface->configure = background_configure;
2207 surface->private = shell;
2208 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002209 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002210 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002211 surface->output->width,
2212 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002213}
2214
2215static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002216panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2217{
2218 struct desktop_shell *shell = es->private;
2219
2220 configure_static_surface(es, &shell->panel_layer);
2221}
2222
2223static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002224desktop_shell_set_panel(struct wl_client *client,
2225 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002226 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002227 struct wl_resource *surface_resource)
2228{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002229 struct desktop_shell *shell = resource->data;
2230 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002231
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002232 if (surface->configure) {
2233 wl_resource_post_error(surface_resource,
2234 WL_DISPLAY_ERROR_INVALID_OBJECT,
2235 "surface role already assigned");
2236 return;
2237 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002238
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002239 surface->configure = panel_configure;
2240 surface->private = shell;
2241 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002242 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002243 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002244 surface->output->width,
2245 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002246}
2247
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002248static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002249lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2250{
2251 struct desktop_shell *shell = surface->private;
2252
2253 center_on_output(surface, get_default_output(shell->compositor));
2254
2255 if (!weston_surface_is_mapped(surface)) {
2256 wl_list_insert(&shell->lock_layer.surface_list,
2257 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002258 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002259 weston_compositor_wake(shell->compositor);
2260 }
2261}
2262
2263static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002264handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002265{
Tiago Vignattibe143262012-04-16 17:31:41 +03002266 struct desktop_shell *shell =
2267 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002268
Martin Minarik6d118362012-06-07 18:01:59 +02002269 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002270 shell->lock_surface = NULL;
2271}
2272
2273static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002274desktop_shell_set_lock_surface(struct wl_client *client,
2275 struct wl_resource *resource,
2276 struct wl_resource *surface_resource)
2277{
Tiago Vignattibe143262012-04-16 17:31:41 +03002278 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002279 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002280
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002281 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002282
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002283 if (!shell->locked)
2284 return;
2285
Pekka Paalanen98262232011-12-01 10:42:22 +02002286 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002287
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002288 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2289 wl_signal_add(&surface_resource->destroy_signal,
2290 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002291
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002292 surface->configure = lock_surface_configure;
2293 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002294}
2295
2296static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002297resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002298{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002299 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002300
Pekka Paalanen77346a62011-11-30 16:26:35 +02002301 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002302
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002303 wl_list_remove(&shell->lock_layer.link);
2304 wl_list_insert(&shell->compositor->cursor_layer.link,
2305 &shell->fullscreen_layer.link);
2306 wl_list_insert(&shell->fullscreen_layer.link,
2307 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002308 if (shell->showing_input_panels) {
2309 wl_list_insert(&shell->panel_layer.link,
2310 &shell->input_panel_layer.link);
2311 wl_list_insert(&shell->input_panel_layer.link,
2312 &ws->layer.link);
2313 } else {
2314 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2315 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002316
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002317 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002318
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002319 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002320 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002321 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002322 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002323}
2324
2325static void
2326desktop_shell_unlock(struct wl_client *client,
2327 struct wl_resource *resource)
2328{
Tiago Vignattibe143262012-04-16 17:31:41 +03002329 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002330
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002331 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002332
2333 if (shell->locked)
2334 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002335}
2336
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002337static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002338desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002339 struct wl_resource *resource,
2340 struct wl_resource *surface_resource)
2341{
2342 struct desktop_shell *shell = resource->data;
2343
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002344 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002345}
2346
Kristian Høgsberg75840622011-09-06 13:48:16 -04002347static const struct desktop_shell_interface desktop_shell_implementation = {
2348 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002349 desktop_shell_set_panel,
2350 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002351 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002352 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002353};
2354
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002355static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002356get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002357{
2358 struct shell_surface *shsurf;
2359
2360 shsurf = get_shell_surface(surface);
2361 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002362 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002363 return shsurf->type;
2364}
2365
Kristian Høgsberg75840622011-09-06 13:48:16 -04002366static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002367move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002368{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002369 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002370 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002371 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002372
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002373 if (surface == NULL)
2374 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002375
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002376 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002377 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002378 return;
2379
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002380 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002381}
2382
2383static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002384resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002385{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002386 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002387 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002388 uint32_t edges = 0;
2389 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002390 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002391
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002392 if (surface == NULL)
2393 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002394
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002395 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002396 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002397 return;
2398
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002399 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002400 wl_fixed_to_int(seat->pointer->grab_x),
2401 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002402 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002403
Pekka Paalanen60921e52012-01-25 15:55:43 +02002404 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002405 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002406 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002407 edges |= 0;
2408 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002409 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002410
Pekka Paalanen60921e52012-01-25 15:55:43 +02002411 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002412 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002413 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002414 edges |= 0;
2415 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002416 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002417
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002418 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002419}
2420
2421static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002422surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002423 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002424{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002425 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002426 struct shell_surface *shsurf;
2427 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002428 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002429
2430 if (surface == NULL)
2431 return;
2432
2433 shsurf = get_shell_surface(surface);
2434 if (!shsurf)
2435 return;
2436
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002437 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002438
Scott Moreau02709af2012-05-22 01:54:10 -06002439 if (surface->alpha > 1.0)
2440 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002441 if (surface->alpha < step)
2442 surface->alpha = step;
2443
2444 surface->geometry.dirty = 1;
2445 weston_surface_damage(surface);
2446}
2447
2448static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002449do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002450 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002451{
Daniel Stone37816df2012-05-16 18:45:18 +01002452 struct weston_seat *ws = (struct weston_seat *) seat;
2453 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002454 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002455 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002456
2457 wl_list_for_each(output, &compositor->output_list, link) {
2458 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002459 wl_fixed_to_double(seat->pointer->x),
2460 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002461 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002462 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002463 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002464 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002465 increment = -output->zoom.increment;
2466 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002467 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002468 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002469 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002470 else
2471 increment = 0;
2472
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002473 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002474
Scott Moreaue6603982012-06-11 13:07:51 -06002475 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002476 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002477 else if (output->zoom.level > output->zoom.max_level)
2478 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002479 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002480 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002481 output->disable_planes++;
2482 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002483
Scott Moreaue6603982012-06-11 13:07:51 -06002484 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002485
Scott Moreau8dacaab2012-06-17 18:10:58 -06002486 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002487 }
2488 }
2489}
2490
Scott Moreauccbf29d2012-02-22 14:21:41 -07002491static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002492zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002493 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002494{
2495 do_zoom(seat, time, 0, axis, value);
2496}
2497
2498static void
2499zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2500 void *data)
2501{
2502 do_zoom(seat, time, key, 0, 0);
2503}
2504
2505static void
2506terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2507 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002508{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002509 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002510
Daniel Stone325fc2d2012-05-30 16:31:58 +01002511 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002512}
2513
2514static void
Scott Moreau447013d2012-02-18 05:05:29 -07002515rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002516 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002517{
2518 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002519 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002520 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002521 struct shell_surface *shsurf = rotate->base.shsurf;
2522 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002523 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002524
2525 if (!shsurf)
2526 return;
2527
2528 surface = shsurf->surface;
2529
2530 cx = 0.5f * surface->geometry.width;
2531 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002532
Daniel Stone37816df2012-05-16 18:45:18 +01002533 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2534 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002535 r = sqrtf(dx * dx + dy * dy);
2536
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002537 wl_list_remove(&shsurf->rotation.transform.link);
2538 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002539
2540 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002541 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002542 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002543
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002544 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002545 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002546
2547 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002548 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002549 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002550 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002551 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002552
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002553 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002554 &shsurf->surface->geometry.transformation_list,
2555 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002556 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002557 wl_list_init(&shsurf->rotation.transform.link);
2558 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002559 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002560 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002561
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002562 /* We need to adjust the position of the surface
2563 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002564 cposx = surface->geometry.x + cx;
2565 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002566 dposx = rotate->center.x - cposx;
2567 dposy = rotate->center.y - cposy;
2568 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002569 weston_surface_set_position(surface,
2570 surface->geometry.x + dposx,
2571 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002572 }
2573
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002574 /* Repaint implies weston_surface_update_transform(), which
2575 * lazily applies the damage due to rotation update.
2576 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002577 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002578}
2579
2580static void
Scott Moreau447013d2012-02-18 05:05:29 -07002581rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002582 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002583{
2584 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002585 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002586 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002587 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002588 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002589
Daniel Stone4dbadb12012-05-30 16:31:51 +01002590 if (pointer->button_count == 0 &&
2591 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002592 if (shsurf)
2593 weston_matrix_multiply(&shsurf->rotation.rotation,
2594 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002595 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002596 free(rotate);
2597 }
2598}
2599
Scott Moreau447013d2012-02-18 05:05:29 -07002600static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002601 noop_grab_focus,
2602 rotate_grab_motion,
2603 rotate_grab_button,
2604};
2605
2606static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002607surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002608{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002609 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002610 float dx, dy;
2611 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002612
Pekka Paalanen460099f2012-01-20 16:48:25 +02002613 rotate = malloc(sizeof *rotate);
2614 if (!rotate)
2615 return;
2616
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002617 weston_surface_to_global_float(surface->surface,
2618 surface->surface->geometry.width / 2,
2619 surface->surface->geometry.height / 2,
2620 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002621
Daniel Stone37816df2012-05-16 18:45:18 +01002622 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2623 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002624 r = sqrtf(dx * dx + dy * dy);
2625 if (r > 20.0f) {
2626 struct weston_matrix inverse;
2627
2628 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002629 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002630 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002631
2632 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002633 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002634 } else {
2635 weston_matrix_init(&surface->rotation.rotation);
2636 weston_matrix_init(&rotate->rotation);
2637 }
2638
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002639 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2640 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002641}
2642
2643static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002644rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2645 void *data)
2646{
2647 struct weston_surface *base_surface =
2648 (struct weston_surface *) seat->pointer->focus;
2649 struct shell_surface *surface;
2650
2651 if (base_surface == NULL)
2652 return;
2653
2654 surface = get_shell_surface(base_surface);
2655 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2656 return;
2657
2658 surface_rotate(surface, seat);
2659}
2660
2661static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002662lower_fullscreen_layer(struct desktop_shell *shell)
2663{
2664 struct workspace *ws;
2665 struct weston_surface *surface, *prev;
2666
2667 ws = get_current_workspace(shell);
2668 wl_list_for_each_reverse_safe(surface, prev,
2669 &shell->fullscreen_layer.surface_list,
2670 layer_link)
2671 weston_surface_restack(surface, &ws->layer.surface_list);
2672}
2673
2674static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002675activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002676 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002677{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002678 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002679 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002680
Daniel Stone37816df2012-05-16 18:45:18 +01002681 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002682
Jonas Ådahl8538b222012-08-29 22:13:03 +02002683 state = ensure_focus_state(shell, seat);
2684 if (state == NULL)
2685 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002686
2687 state->keyboard_focus = es;
2688 wl_list_remove(&state->surface_destroy_listener.link);
2689 wl_signal_add(&es->surface.resource.destroy_signal,
2690 &state->surface_destroy_listener);
2691
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002692 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002693 case SHELL_SURFACE_FULLSCREEN:
2694 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002695 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002696 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002697 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002698 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002699 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002700 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002701 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002702 }
2703}
2704
Alex Wu21858432012-04-01 20:13:08 +08002705/* no-op func for checking black surface */
2706static void
2707black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2708{
2709}
2710
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002711static bool
Alex Wu21858432012-04-01 20:13:08 +08002712is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2713{
2714 if (es->configure == black_surface_configure) {
2715 if (fs_surface)
2716 *fs_surface = (struct weston_surface *)es->private;
2717 return true;
2718 }
2719 return false;
2720}
2721
Kristian Høgsberg75840622011-09-06 13:48:16 -04002722static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002723click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002724 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002725{
Daniel Stone37816df2012-05-16 18:45:18 +01002726 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002727 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002728 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002729 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002730
Daniel Stone37816df2012-05-16 18:45:18 +01002731 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002732 if (!focus)
2733 return;
2734
Alex Wu21858432012-04-01 20:13:08 +08002735 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002736 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002737
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002738 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2739 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002740
Daniel Stone325fc2d2012-05-30 16:31:58 +01002741 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002742 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002743}
2744
2745static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002746lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002747{
Tiago Vignattibe143262012-04-16 17:31:41 +03002748 struct desktop_shell *shell =
2749 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002750 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002751 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002752
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002753 if (shell->locked) {
2754 wl_list_for_each(output, &shell->compositor->output_list, link)
2755 /* TODO: find a way to jump to other DPMS levels */
2756 if (output->set_dpms)
2757 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002758 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002759 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002760
2761 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002762
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002763 /* Hide all surfaces by removing the fullscreen, panel and
2764 * toplevel layers. This way nothing else can show or receive
2765 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002766
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002767 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002768 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002769 if (shell->showing_input_panels)
2770 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002771 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002772 wl_list_insert(&shell->compositor->cursor_layer.link,
2773 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002774
Pekka Paalanen77346a62011-11-30 16:26:35 +02002775 launch_screensaver(shell);
2776
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002777 /* TODO: disable bindings that should not work while locked. */
2778
2779 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002780}
2781
2782static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002783unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002784{
Tiago Vignattibe143262012-04-16 17:31:41 +03002785 struct desktop_shell *shell =
2786 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002787
Pekka Paalanend81c2162011-11-16 13:47:34 +02002788 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002789 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002790 return;
2791 }
2792
2793 /* If desktop-shell client has gone away, unlock immediately. */
2794 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002795 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002796 return;
2797 }
2798
2799 if (shell->prepare_event_sent)
2800 return;
2801
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002802 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002803 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002804}
2805
2806static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002807show_input_panels(struct wl_listener *listener, void *data)
2808{
2809 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002810 container_of(listener, struct desktop_shell,
2811 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002812 struct input_panel_surface *surface, *next;
2813 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002814
Jan Arne Petersen451a9712013-02-11 15:10:11 +01002815 if (shell->showing_input_panels)
2816 return;
2817
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002818 shell->showing_input_panels = true;
2819
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002820 if (!shell->locked)
2821 wl_list_insert(&shell->panel_layer.link,
2822 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002823
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002824 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002825 &shell->input_panel.surfaces, link) {
2826 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002827 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002828 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002829 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002830 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002831 weston_surface_damage(ws);
2832 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002833 }
2834}
2835
2836static void
2837hide_input_panels(struct wl_listener *listener, void *data)
2838{
2839 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002840 container_of(listener, struct desktop_shell,
2841 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002842 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002843
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002844 shell->showing_input_panels = false;
2845
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002846 if (!shell->locked)
2847 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002848
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002849 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002850 &shell->input_panel_layer.surface_list, layer_link)
2851 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002852}
2853
2854static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002855center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002856{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002857 int32_t width = weston_surface_buffer_width(surface);
2858 int32_t height = weston_surface_buffer_height(surface);
2859 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002860
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002861 x = output->x + (output->width - width) / 2;
2862 y = output->y + (output->height - height) / 2;
2863
2864 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002865}
2866
2867static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002868weston_surface_set_initial_position (struct weston_surface *surface,
2869 struct desktop_shell *shell)
2870{
2871 struct weston_compositor *compositor = shell->compositor;
2872 int ix = 0, iy = 0;
2873 int range_x, range_y;
2874 int dx, dy, x, y, panel_height;
2875 struct weston_output *output, *target_output = NULL;
2876 struct weston_seat *seat;
2877
2878 /* As a heuristic place the new window on the same output as the
2879 * pointer. Falling back to the output containing 0, 0.
2880 *
2881 * TODO: Do something clever for touch too?
2882 */
2883 wl_list_for_each(seat, &compositor->seat_list, link) {
2884 if (seat->has_pointer) {
2885 ix = wl_fixed_to_int(seat->pointer.x);
2886 iy = wl_fixed_to_int(seat->pointer.y);
2887 break;
2888 }
2889 }
2890
2891 wl_list_for_each(output, &compositor->output_list, link) {
2892 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2893 target_output = output;
2894 break;
2895 }
2896 }
2897
2898 if (!target_output) {
2899 weston_surface_set_position(surface, 10 + random() % 400,
2900 10 + random() % 400);
2901 return;
2902 }
2903
2904 /* Valid range within output where the surface will still be onscreen.
2905 * If this is negative it means that the surface is bigger than
2906 * output.
2907 */
2908 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002909 range_x = target_output->width - surface->geometry.width;
2910 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002911 surface->geometry.height;
2912
Rob Bradford4cb88c72012-08-13 15:18:44 +01002913 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002914 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002915 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002916 dx = 0;
2917
2918 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002919 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002920 else
2921 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002922
2923 x = target_output->x + dx;
2924 y = target_output->y + dy;
2925
2926 weston_surface_set_position (surface, x, y);
2927}
2928
2929static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002930map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002931 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002932{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002933 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002934 struct shell_surface *shsurf = get_shell_surface(surface);
2935 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002936 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002937 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002938 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002939 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002940
Pekka Paalanen60921e52012-01-25 15:55:43 +02002941 surface->geometry.width = width;
2942 surface->geometry.height = height;
2943 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002944
2945 /* initial positioning, see also configure() */
2946 switch (surface_type) {
2947 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002948 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002949 break;
Alex Wu4539b082012-03-01 12:57:46 +08002950 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002951 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002952 shell_map_fullscreen(shsurf);
2953 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002954 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002955 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002956 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002957 weston_surface_set_position(surface, shsurf->output->x,
2958 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002959 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002960 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002961 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00002962 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002963 case SHELL_SURFACE_NONE:
2964 weston_surface_set_position(surface,
2965 surface->geometry.x + sx,
2966 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002967 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002968 default:
2969 ;
2970 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002971
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002972 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002973 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002974 case SHELL_SURFACE_POPUP:
2975 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002976 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002977 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2978 break;
Alex Wu4539b082012-03-01 12:57:46 +08002979 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002980 case SHELL_SURFACE_NONE:
2981 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002982 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002983 ws = get_current_workspace(shell);
2984 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002985 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002986 }
2987
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002988 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002989 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002990 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2991 surface->output = shsurf->output;
2992 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002993
Juan Zhao7bb92f02011-12-15 11:31:51 -05002994 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002995 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002996 if (shsurf->transient.flags ==
2997 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2998 break;
2999 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003000 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003001 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003002 if (!shell->locked) {
3003 wl_list_for_each(seat, &compositor->seat_list, link)
3004 activate(shell, surface, seat);
3005 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003006 break;
3007 default:
3008 break;
3009 }
3010
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003011 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003012 {
3013 switch (shell->win_animation_type) {
3014 case ANIMATION_FADE:
3015 weston_fade_run(surface, NULL, NULL);
3016 break;
3017 case ANIMATION_ZOOM:
3018 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3019 break;
3020 default:
3021 break;
3022 }
3023 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003024}
3025
3026static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003027configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003028 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003029{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003030 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3031 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003032
Pekka Paalanen77346a62011-11-30 16:26:35 +02003033 shsurf = get_shell_surface(surface);
3034 if (shsurf)
3035 surface_type = shsurf->type;
3036
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003037 surface->geometry.x = x;
3038 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02003039 surface->geometry.width = width;
3040 surface->geometry.height = height;
3041 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003042
3043 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003044 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003045 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003046 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003047 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003048 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003049 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003050 surface->geometry.x = surface->output->x;
3051 surface->geometry.y = surface->output->y +
3052 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003053 break;
Alex Wu4539b082012-03-01 12:57:46 +08003054 case SHELL_SURFACE_TOPLEVEL:
3055 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003056 default:
3057 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003058 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003059
Alex Wu4539b082012-03-01 12:57:46 +08003060 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003061 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003062 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003063
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003064 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003065 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003066 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003067}
3068
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003069static void
3070shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3071{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003072 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003073 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003074 int32_t width = weston_surface_buffer_width(es);
3075 int32_t height = weston_surface_buffer_height(es);
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003076 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003077
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003078 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003079 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003080 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003081 type_changed = 1;
3082 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003083
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003084 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003085 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003086 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003087 es->geometry.width != width ||
3088 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003089 float from_x, from_y;
3090 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003091
3092 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3093 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3094 configure(shell, es,
3095 es->geometry.x + to_x - from_x,
3096 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003097 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003098 }
3099}
3100
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003101static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003102
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003103static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003104desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003105{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003106 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003107 struct desktop_shell *shell =
3108 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003109
3110 shell->child.process.pid = 0;
3111 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003112
3113 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3114 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003115 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003116 shell->child.deathstamp = time;
3117 shell->child.deathcount = 0;
3118 }
3119
3120 shell->child.deathcount++;
3121 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003122 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003123 return;
3124 }
3125
Martin Minarik6d118362012-06-07 18:01:59 +02003126 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003127 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003128}
3129
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003130static void
3131launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003132{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003133 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003134 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003135
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003136 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003137 &shell->child.process,
3138 shell_exe,
3139 desktop_shell_sigchld);
3140
3141 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003142 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003143}
3144
3145static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003146bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3147{
Tiago Vignattibe143262012-04-16 17:31:41 +03003148 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003149
3150 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003151 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003152}
3153
Kristian Høgsberg75840622011-09-06 13:48:16 -04003154static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003155unbind_desktop_shell(struct wl_resource *resource)
3156{
Tiago Vignattibe143262012-04-16 17:31:41 +03003157 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003158
3159 if (shell->locked)
3160 resume_desktop(shell);
3161
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003162 shell->child.desktop_shell = NULL;
3163 shell->prepare_event_sent = false;
3164 free(resource);
3165}
3166
3167static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003168bind_desktop_shell(struct wl_client *client,
3169 void *data, uint32_t version, uint32_t id)
3170{
Tiago Vignattibe143262012-04-16 17:31:41 +03003171 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003172 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003173
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003174 resource = wl_client_add_object(client, &desktop_shell_interface,
3175 &desktop_shell_implementation,
3176 id, shell);
3177
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003178 if (client == shell->child.client) {
3179 resource->destroy = unbind_desktop_shell;
3180 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003181 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003182 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003183
3184 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3185 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003186 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003187}
3188
Pekka Paalanen6e168112011-11-24 11:34:05 +02003189static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003190screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3191{
3192 struct desktop_shell *shell = surface->private;
3193
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003194 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003195 if (!shell->locked)
3196 return;
3197
3198 center_on_output(surface, surface->output);
3199
3200 if (wl_list_empty(&surface->layer_link)) {
3201 wl_list_insert(shell->lock_layer.surface_list.prev,
3202 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003203 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003204 shell->compositor->idle_time = shell->screensaver.duration;
3205 weston_compositor_wake(shell->compositor);
3206 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3207 }
3208}
3209
3210static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003211screensaver_set_surface(struct wl_client *client,
3212 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003213 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003214 struct wl_resource *output_resource)
3215{
Tiago Vignattibe143262012-04-16 17:31:41 +03003216 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003217 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003218 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003219
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003220 surface->configure = screensaver_configure;
3221 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003222 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003223}
3224
3225static const struct screensaver_interface screensaver_implementation = {
3226 screensaver_set_surface
3227};
3228
3229static void
3230unbind_screensaver(struct wl_resource *resource)
3231{
Tiago Vignattibe143262012-04-16 17:31:41 +03003232 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003233
Pekka Paalanen77346a62011-11-30 16:26:35 +02003234 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003235 free(resource);
3236}
3237
3238static void
3239bind_screensaver(struct wl_client *client,
3240 void *data, uint32_t version, uint32_t id)
3241{
Tiago Vignattibe143262012-04-16 17:31:41 +03003242 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003243 struct wl_resource *resource;
3244
3245 resource = wl_client_add_object(client, &screensaver_interface,
3246 &screensaver_implementation,
3247 id, shell);
3248
Pekka Paalanen77346a62011-11-30 16:26:35 +02003249 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003250 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003251 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003252 return;
3253 }
3254
3255 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3256 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003257 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003258}
3259
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003260static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003261input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3262{
3263 struct weston_mode *mode = surface->output->current;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003264 int32_t width = weston_surface_buffer_width(surface);
3265 int32_t height = weston_surface_buffer_height(surface);
3266 float x = (mode->width - width) / 2;
3267 float y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003268
3269 /* Don't map the input panel here, wait for
3270 * show_input_panels signal. */
3271
3272 weston_surface_configure(surface,
3273 surface->output->x + x,
3274 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003275 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003276}
3277
3278static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003279destroy_input_panel_surface(struct wl_listener *listener,
3280 void *data)
3281{
3282 struct input_panel_surface *input_panel_surface =
3283 container_of(listener, struct input_panel_surface, listener);
3284
3285 wl_list_remove(&listener->link);
3286 wl_list_remove(&input_panel_surface->link);
3287
3288 free(input_panel_surface);
3289}
3290
3291static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003292input_panel_set_surface(struct wl_client *client,
3293 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003294 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003295 struct wl_resource *output_resource)
3296{
3297 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003298 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003299 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003300 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003301
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003302 surface->configure = input_panel_configure;
3303 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003304 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003305
3306 input_panel_surface = malloc(sizeof *input_panel_surface);
3307 if (!input_panel_surface) {
3308 wl_resource_post_no_memory(resource);
3309 return;
3310 }
3311
3312 input_panel_surface->surface = surface;
3313 input_panel_surface->listener.notify = destroy_input_panel_surface;
3314
3315 wl_signal_add(&surface_resource->destroy_signal,
3316 &input_panel_surface->listener);
3317
3318 wl_list_insert(&shell->input_panel.surfaces,
3319 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003320}
3321
3322static const struct input_panel_interface input_panel_implementation = {
3323 input_panel_set_surface
3324};
3325
3326static void
3327unbind_input_panel(struct wl_resource *resource)
3328{
3329 struct desktop_shell *shell = resource->data;
3330
3331 shell->input_panel.binding = NULL;
3332 free(resource);
3333}
3334
3335static void
3336bind_input_panel(struct wl_client *client,
3337 void *data, uint32_t version, uint32_t id)
3338{
3339 struct desktop_shell *shell = data;
3340 struct wl_resource *resource;
3341
3342 resource = wl_client_add_object(client, &input_panel_interface,
3343 &input_panel_implementation,
3344 id, shell);
3345
3346 if (shell->input_panel.binding == NULL) {
3347 resource->destroy = unbind_input_panel;
3348 shell->input_panel.binding = resource;
3349 return;
3350 }
3351
3352 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3353 "interface object already bound");
3354 wl_resource_destroy(resource);
3355}
3356
Kristian Høgsberg07045392012-02-19 18:52:44 -05003357struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003358 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003359 struct weston_surface *current;
3360 struct wl_listener listener;
3361 struct wl_keyboard_grab grab;
3362};
3363
3364static void
3365switcher_next(struct switcher *switcher)
3366{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003367 struct weston_surface *surface;
3368 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003369 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003370 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003371
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003372 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003373 switch (get_shell_surface_type(surface)) {
3374 case SHELL_SURFACE_TOPLEVEL:
3375 case SHELL_SURFACE_FULLSCREEN:
3376 case SHELL_SURFACE_MAXIMIZED:
3377 if (first == NULL)
3378 first = surface;
3379 if (prev == switcher->current)
3380 next = surface;
3381 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003382 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003383 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003384 weston_surface_damage(surface);
3385 break;
3386 default:
3387 break;
3388 }
Alex Wu1659daa2012-04-01 20:13:09 +08003389
3390 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003391 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003392 surface->geometry.dirty = 1;
3393 weston_surface_damage(surface);
3394 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003395 }
3396
3397 if (next == NULL)
3398 next = first;
3399
Alex Wu07b26062012-03-12 16:06:01 +08003400 if (next == NULL)
3401 return;
3402
Kristian Høgsberg07045392012-02-19 18:52:44 -05003403 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003404 wl_signal_add(&next->surface.resource.destroy_signal,
3405 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003406
3407 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003408 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003409
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003410 shsurf = get_shell_surface(switcher->current);
3411 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003412 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003413}
3414
3415static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003416switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003417{
3418 struct switcher *switcher =
3419 container_of(listener, struct switcher, listener);
3420
3421 switcher_next(switcher);
3422}
3423
3424static void
Daniel Stone351eb612012-05-31 15:27:47 -04003425switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003426{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003427 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003428 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003429 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003430
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003431 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003432 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003433 weston_surface_damage(surface);
3434 }
3435
Alex Wu07b26062012-03-12 16:06:01 +08003436 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003437 activate(switcher->shell, switcher->current,
3438 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003439 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003440 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003441 free(switcher);
3442}
3443
3444static void
3445switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003446 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003447{
3448 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003449 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003450
Daniel Stonec9785ea2012-05-30 16:31:52 +01003451 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003452 switcher_next(switcher);
3453}
3454
3455static void
3456switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3457 uint32_t mods_depressed, uint32_t mods_latched,
3458 uint32_t mods_locked, uint32_t group)
3459{
3460 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003461 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003462
Daniel Stone351eb612012-05-31 15:27:47 -04003463 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3464 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003465}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003466
3467static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003468 switcher_key,
3469 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003470};
3471
3472static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003473switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3474 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003475{
Tiago Vignattibe143262012-04-16 17:31:41 +03003476 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003477 struct switcher *switcher;
3478
3479 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003480 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003481 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003482 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003483 wl_list_init(&switcher->listener.link);
3484
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003485 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003486 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003487 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3488 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003489 switcher_next(switcher);
3490}
3491
Pekka Paalanen3c647232011-12-22 13:43:43 +02003492static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003493backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3494 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003495{
3496 struct weston_compositor *compositor = data;
3497 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003498 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003499
3500 /* TODO: we're limiting to simple use cases, where we assume just
3501 * control on the primary display. We'd have to extend later if we
3502 * ever get support for setting backlights on random desktop LCD
3503 * panels though */
3504 output = get_default_output(compositor);
3505 if (!output)
3506 return;
3507
3508 if (!output->set_backlight)
3509 return;
3510
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003511 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3512 backlight_new = output->backlight_current - 25;
3513 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3514 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003515
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003516 if (backlight_new < 5)
3517 backlight_new = 5;
3518 if (backlight_new > 255)
3519 backlight_new = 255;
3520
3521 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003522 output->set_backlight(output, output->backlight_current);
3523}
3524
3525static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003526fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3527 void *data)
3528{
3529 struct desktop_shell *shell = data;
3530 struct weston_compositor *compositor = shell->compositor;
3531 compositor->fan_debug = !compositor->fan_debug;
3532 weston_compositor_damage_all(compositor);
3533}
3534
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003535struct debug_binding_grab {
3536 struct wl_keyboard_grab grab;
3537 struct weston_seat *seat;
3538 uint32_t key[2];
3539 int key_released[2];
3540};
3541
3542static void
3543debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3544 uint32_t key, uint32_t state)
3545{
3546 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3547 struct wl_resource *resource;
3548 struct wl_display *display;
3549 uint32_t serial;
3550 int send = 0, terminate = 0;
3551 int check_binding = 1;
3552 int i;
3553
3554 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3555 /* Do not run bindings on key releases */
3556 check_binding = 0;
3557
3558 for (i = 0; i < 2; i++)
3559 if (key == db->key[i])
3560 db->key_released[i] = 1;
3561
3562 if (db->key_released[0] && db->key_released[1]) {
3563 /* All key releases been swalled so end the grab */
3564 terminate = 1;
3565 } else if (key != db->key[0] && key != db->key[1]) {
3566 /* Should not swallow release of other keys */
3567 send = 1;
3568 }
3569 } else if (key == db->key[0] && !db->key_released[0]) {
3570 /* Do not check bindings for the first press of the binding
3571 * key. This allows it to be used as a debug shortcut.
3572 * We still need to swallow this event. */
3573 check_binding = 0;
3574 } else if (db->key[1]) {
3575 /* If we already ran a binding don't process another one since
3576 * we can't keep track of all the binding keys that were
3577 * pressed in order to swallow the release events. */
3578 send = 1;
3579 check_binding = 0;
3580 }
3581
3582 if (check_binding) {
3583 struct weston_compositor *ec = db->seat->compositor;
3584
3585 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3586 key, state)) {
3587 /* We ran a binding so swallow the press and keep the
3588 * grab to swallow the released too. */
3589 send = 0;
3590 terminate = 0;
3591 db->key[1] = key;
3592 } else {
3593 /* Terminate the grab since the key pressed is not a
3594 * debug binding key. */
3595 send = 1;
3596 terminate = 1;
3597 }
3598 }
3599
3600 if (send) {
3601 resource = grab->keyboard->focus_resource;
3602
3603 if (resource) {
3604 display = wl_client_get_display(resource->client);
3605 serial = wl_display_next_serial(display);
3606 wl_keyboard_send_key(resource, serial, time, key, state);
3607 }
3608 }
3609
3610 if (terminate) {
3611 wl_keyboard_end_grab(grab->keyboard);
3612 free(db);
3613 }
3614}
3615
3616static void
3617debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3618 uint32_t mods_depressed, uint32_t mods_latched,
3619 uint32_t mods_locked, uint32_t group)
3620{
3621 struct wl_resource *resource;
3622
3623 resource = grab->keyboard->focus_resource;
3624 if (!resource)
3625 return;
3626
3627 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3628 mods_latched, mods_locked, group);
3629}
3630
3631struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3632 debug_binding_key,
3633 debug_binding_modifiers
3634};
3635
3636static void
3637debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3638{
3639 struct debug_binding_grab *grab;
3640
3641 grab = calloc(1, sizeof *grab);
3642 if (!grab)
3643 return;
3644
3645 grab->seat = (struct weston_seat *) seat;
3646 grab->key[0] = key;
3647 grab->grab.interface = &debug_binding_keyboard_grab;
3648 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3649}
3650
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003651static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003652force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3653 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003654{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003655 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003656 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003657 struct desktop_shell *shell = data;
3658 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003659 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003660
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003661 focus_surface = seat->keyboard->focus;
3662 if (!focus_surface)
3663 return;
3664
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003665 wl_signal_emit(&compositor->kill_signal, focus_surface);
3666
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003667 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003668 wl_client_get_credentials(client, &pid, NULL, NULL);
3669
3670 /* Skip clients that we launched ourselves (the credentials of
3671 * the socketpair is ours) */
3672 if (pid == getpid())
3673 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003674
Daniel Stone325fc2d2012-05-30 16:31:58 +01003675 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003676}
3677
3678static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003679workspace_up_binding(struct wl_seat *seat, uint32_t time,
3680 uint32_t key, void *data)
3681{
3682 struct desktop_shell *shell = data;
3683 unsigned int new_index = shell->workspaces.current;
3684
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003685 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003686 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003687 if (new_index != 0)
3688 new_index--;
3689
3690 change_workspace(shell, new_index);
3691}
3692
3693static void
3694workspace_down_binding(struct wl_seat *seat, uint32_t time,
3695 uint32_t key, void *data)
3696{
3697 struct desktop_shell *shell = data;
3698 unsigned int new_index = shell->workspaces.current;
3699
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003700 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003701 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003702 if (new_index < shell->workspaces.num - 1)
3703 new_index++;
3704
3705 change_workspace(shell, new_index);
3706}
3707
3708static void
3709workspace_f_binding(struct wl_seat *seat, uint32_t time,
3710 uint32_t key, void *data)
3711{
3712 struct desktop_shell *shell = data;
3713 unsigned int new_index;
3714
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003715 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003716 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003717 new_index = key - KEY_F1;
3718 if (new_index >= shell->workspaces.num)
3719 new_index = shell->workspaces.num - 1;
3720
3721 change_workspace(shell, new_index);
3722}
3723
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003724static void
3725workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3726 uint32_t key, void *data)
3727{
3728 struct desktop_shell *shell = data;
3729 unsigned int new_index = shell->workspaces.current;
3730
3731 if (shell->locked)
3732 return;
3733
3734 if (new_index != 0)
3735 new_index--;
3736
3737 take_surface_to_workspace_by_seat(shell, seat, new_index);
3738}
3739
3740static void
3741workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3742 uint32_t key, void *data)
3743{
3744 struct desktop_shell *shell = data;
3745 unsigned int new_index = shell->workspaces.current;
3746
3747 if (shell->locked)
3748 return;
3749
3750 if (new_index < shell->workspaces.num - 1)
3751 new_index++;
3752
3753 take_surface_to_workspace_by_seat(shell, seat, new_index);
3754}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003755
3756static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003757shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003758{
Tiago Vignattibe143262012-04-16 17:31:41 +03003759 struct desktop_shell *shell =
3760 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003761 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003762
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003763 if (shell->child.client)
3764 wl_client_destroy(shell->child.client);
3765
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003766 wl_list_remove(&shell->lock_listener.link);
3767 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003768 wl_list_remove(&shell->show_input_panel_listener.link);
3769 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003770
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003771 wl_array_for_each(ws, &shell->workspaces.array)
3772 workspace_destroy(*ws);
3773 wl_array_release(&shell->workspaces.array);
3774
Pekka Paalanen3c647232011-12-22 13:43:43 +02003775 free(shell->screensaver.path);
3776 free(shell);
3777}
3778
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003779static void
3780shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3781{
3782 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003783 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003784
3785 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003786 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3787 MODIFIER_CTRL | MODIFIER_ALT,
3788 terminate_binding, ec);
3789 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3790 click_to_activate_binding,
3791 shell);
3792 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3793 MODIFIER_SUPER | MODIFIER_ALT,
3794 surface_opacity_binding, NULL);
3795 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3796 MODIFIER_SUPER, zoom_axis_binding,
3797 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003798
3799 /* configurable bindings */
3800 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003801 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3802 zoom_key_binding, NULL);
3803 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3804 zoom_key_binding, NULL);
3805 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3806 shell);
3807 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3808 resize_binding, shell);
3809 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3810 rotate_binding, NULL);
3811 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3812 shell);
3813 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3814 ec);
3815 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3816 backlight_binding, ec);
3817 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3818 ec);
3819 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3820 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003821 weston_compositor_add_key_binding(ec, KEY_K, mod,
3822 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003823 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3824 workspace_up_binding, shell);
3825 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3826 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003827 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3828 workspace_move_surface_up_binding,
3829 shell);
3830 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3831 workspace_move_surface_down_binding,
3832 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003833
3834 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3835 if (shell->workspaces.num > 1) {
3836 num_workspace_bindings = shell->workspaces.num;
3837 if (num_workspace_bindings > 6)
3838 num_workspace_bindings = 6;
3839 for (i = 0; i < num_workspace_bindings; i++)
3840 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3841 workspace_f_binding,
3842 shell);
3843 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003844
3845 /* Debug bindings */
3846 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3847 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003848 weston_compositor_add_debug_binding(ec, KEY_F,
3849 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003850}
3851
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003852WL_EXPORT int
Kristian Høgsbergb00a9d32012-09-11 14:06:27 -04003853module_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003854{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003855 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003856 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003857 struct workspace **pws;
3858 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003859 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003860
3861 shell = malloc(sizeof *shell);
3862 if (shell == NULL)
3863 return -1;
3864
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003865 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003866 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003867
3868 shell->destroy_listener.notify = shell_destroy;
3869 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3870 shell->lock_listener.notify = lock;
3871 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3872 shell->unlock_listener.notify = unlock;
3873 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003874 shell->show_input_panel_listener.notify = show_input_panels;
3875 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3876 shell->hide_input_panel_listener.notify = hide_input_panels;
3877 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003878 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003879 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003880 ec->shell_interface.create_shell_surface = create_shell_surface;
3881 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003882 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003883 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003884 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003885 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003886
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003887 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003888
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003889 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3890 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003891 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3892 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003893 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003894
3895 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02003896 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003897
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003898 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003899
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003900 for (i = 0; i < shell->workspaces.num; i++) {
3901 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3902 if (pws == NULL)
3903 return -1;
3904
3905 *pws = workspace_create();
3906 if (*pws == NULL)
3907 return -1;
3908 }
3909 activate_workspace(shell, 0);
3910
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003911 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003912 wl_list_init(&shell->workspaces.animation.link);
3913 shell->workspaces.animation.frame = animate_workspace_change_frame;
3914
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003915 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3916 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003917 return -1;
3918
Kristian Høgsberg75840622011-09-06 13:48:16 -04003919 if (wl_display_add_global(ec->wl_display,
3920 &desktop_shell_interface,
3921 shell, bind_desktop_shell) == NULL)
3922 return -1;
3923
Pekka Paalanen6e168112011-11-24 11:34:05 +02003924 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3925 shell, bind_screensaver) == NULL)
3926 return -1;
3927
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003928 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3929 shell, bind_input_panel) == NULL)
3930 return -1;
3931
Jonas Ådahle9d22502012-08-29 22:13:01 +02003932 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3933 shell, bind_workspace_manager) == NULL)
3934 return -1;
3935
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003936 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003937
3938 loop = wl_display_get_event_loop(ec->wl_display);
3939 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003940
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003941 wl_list_for_each(seat, &ec->seat_list, link)
3942 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003943
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003944 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003945
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003946 return 0;
3947}