blob: 368fa5b72f1ff84686b8d71d344812580aceee24 [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
239static bool
240shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
241{
242 struct desktop_shell *shell;
243 struct weston_surface *top_fs_es;
244
245 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100246
Alex Wubd3354b2012-04-17 17:20:49 +0800247 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
248 return false;
249
250 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100251 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800252 layer_link);
253 return (shsurf == get_shell_surface(top_fs_es));
254}
255
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500256static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400257destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300258{
259 struct shell_grab *grab;
260
261 grab = container_of(listener, struct shell_grab,
262 shsurf_destroy_listener);
263
264 grab->shsurf = NULL;
265}
266
267static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400268popup_grab_end(struct wl_pointer *pointer);
269
270static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300271shell_grab_start(struct shell_grab *grab,
272 const struct wl_pointer_grab_interface *interface,
273 struct shell_surface *shsurf,
274 struct wl_pointer *pointer,
275 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300276{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300277 struct desktop_shell *shell = shsurf->shell;
278
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400279 popup_grab_end(pointer);
280
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300281 grab->grab.interface = interface;
282 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400283 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
284 wl_signal_add(&shsurf->resource.destroy_signal,
285 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300286
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300287 grab->pointer = pointer;
288 grab->grab.focus = &shsurf->surface->surface;
289
290 wl_pointer_start_grab(pointer, &grab->grab);
291 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
292 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
293 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300294}
295
296static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300297shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300298{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400299 if (grab->shsurf)
300 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300301
302 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300303}
304
305static void
Alex Wu4539b082012-03-01 12:57:46 +0800306center_on_output(struct weston_surface *surface,
307 struct weston_output *output);
308
Daniel Stone496ca172012-05-30 16:31:42 +0100309static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300310get_modifier(char *modifier)
311{
312 if (!modifier)
313 return MODIFIER_SUPER;
314
315 if (!strcmp("ctrl", modifier))
316 return MODIFIER_CTRL;
317 else if (!strcmp("alt", modifier))
318 return MODIFIER_ALT;
319 else if (!strcmp("super", modifier))
320 return MODIFIER_SUPER;
321 else
322 return MODIFIER_SUPER;
323}
324
Juan Zhaoe10d2792012-04-25 19:09:52 +0800325static enum animation_type
326get_animation_type(char *animation)
327{
328 if (!animation)
329 return ANIMATION_NONE;
330
331 if (!strcmp("zoom", animation))
332 return ANIMATION_ZOOM;
333 else if (!strcmp("fade", animation))
334 return ANIMATION_FADE;
335 else
336 return ANIMATION_NONE;
337}
338
Alex Wu4539b082012-03-01 12:57:46 +0800339static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300340shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200341{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200342 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200343 char *path = NULL;
344 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200345 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300346 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800347 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200348
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400349 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300350 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800351 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200352 { "num-workspaces",
353 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200354 };
355
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400356 struct config_key saver_keys[] = {
357 { "path", CONFIG_KEY_STRING, &path },
358 { "duration", CONFIG_KEY_INTEGER, &duration },
359 };
360
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200361 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400362 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200363 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
364 };
365
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200366 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500367 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200368 free(config_file);
369
Pekka Paalanen7296e792011-12-07 16:22:00 +0200370 shell->screensaver.path = path;
371 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300372 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800373 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200374 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
375}
376
377static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200378focus_state_destroy(struct focus_state *state)
379{
380 wl_list_remove(&state->seat_destroy_listener.link);
381 wl_list_remove(&state->surface_destroy_listener.link);
382 free(state);
383}
384
385static void
386focus_state_seat_destroy(struct wl_listener *listener, void *data)
387{
388 struct focus_state *state = container_of(listener,
389 struct focus_state,
390 seat_destroy_listener);
391
392 wl_list_remove(&state->link);
393 focus_state_destroy(state);
394}
395
396static void
397focus_state_surface_destroy(struct wl_listener *listener, void *data)
398{
399 struct focus_state *state = container_of(listener,
400 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400401 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400402 struct desktop_shell *shell;
403 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200404
Kristian Høgsberge3778222012-07-31 17:29:30 -0400405 next = NULL;
406 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
407 if (surface == state->keyboard_focus)
408 continue;
409
410 next = surface;
411 break;
412 }
413
414 if (next) {
415 shell = state->seat->compositor->shell_interface.shell;
416 activate(shell, next, state->seat);
417 } else {
418 wl_list_remove(&state->link);
419 focus_state_destroy(state);
420 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200421}
422
423static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400424focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200425{
Jonas Ådahl04769742012-06-13 00:01:24 +0200426 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200427
428 state = malloc(sizeof *state);
429 if (state == NULL)
430 return NULL;
431
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400432 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200433 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400434 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200435
436 state->seat_destroy_listener.notify = focus_state_seat_destroy;
437 state->surface_destroy_listener.notify = focus_state_surface_destroy;
438 wl_signal_add(&seat->seat.destroy_signal,
439 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400440 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200441
442 return state;
443}
444
Jonas Ådahl8538b222012-08-29 22:13:03 +0200445static struct focus_state *
446ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
447{
448 struct workspace *ws = get_current_workspace(shell);
449 struct focus_state *state;
450
451 wl_list_for_each(state, &ws->focus_list, link)
452 if (state->seat == seat)
453 break;
454
455 if (&state->link == &ws->focus_list)
456 state = focus_state_create(seat, ws);
457
458 return state;
459}
460
Jonas Ådahl04769742012-06-13 00:01:24 +0200461static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400462restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200463{
464 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200465 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200466
467 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200468 surface = state->keyboard_focus ?
469 &state->keyboard_focus->surface : NULL;
470
471 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200472 }
473}
474
475static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200476replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
477 struct weston_seat *seat)
478{
479 struct focus_state *state;
480 struct wl_surface *surface;
481
482 wl_list_for_each(state, &ws->focus_list, link) {
483 if (state->seat == seat) {
484 surface = seat->seat.keyboard->focus;
485 state->keyboard_focus =
486 (struct weston_surface *) surface;
487 return;
488 }
489 }
490}
491
492static void
493drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
494 struct weston_surface *surface)
495{
496 struct focus_state *state;
497
498 wl_list_for_each(state, &ws->focus_list, link)
499 if (state->keyboard_focus == surface)
500 state->keyboard_focus = NULL;
501}
502
503static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200504workspace_destroy(struct workspace *ws)
505{
Jonas Ådahl04769742012-06-13 00:01:24 +0200506 struct focus_state *state, *next;
507
508 wl_list_for_each_safe(state, next, &ws->focus_list, link)
509 focus_state_destroy(state);
510
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200511 free(ws);
512}
513
Jonas Ådahl04769742012-06-13 00:01:24 +0200514static void
515seat_destroyed(struct wl_listener *listener, void *data)
516{
517 struct weston_seat *seat = data;
518 struct focus_state *state, *next;
519 struct workspace *ws = container_of(listener,
520 struct workspace,
521 seat_destroyed_listener);
522
523 wl_list_for_each_safe(state, next, &ws->focus_list, link)
524 if (state->seat == seat)
525 wl_list_remove(&state->link);
526}
527
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200528static struct workspace *
529workspace_create(void)
530{
531 struct workspace *ws = malloc(sizeof *ws);
532 if (ws == NULL)
533 return NULL;
534
535 weston_layer_init(&ws->layer, NULL);
536
Jonas Ådahl04769742012-06-13 00:01:24 +0200537 wl_list_init(&ws->focus_list);
538 wl_list_init(&ws->seat_destroyed_listener.link);
539 ws->seat_destroyed_listener.notify = seat_destroyed;
540
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200541 return ws;
542}
543
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200544static int
545workspace_is_empty(struct workspace *ws)
546{
547 return wl_list_empty(&ws->layer.surface_list);
548}
549
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200550static struct workspace *
551get_workspace(struct desktop_shell *shell, unsigned int index)
552{
553 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200554 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200555 pws += index;
556 return *pws;
557}
558
559static struct workspace *
560get_current_workspace(struct desktop_shell *shell)
561{
562 return get_workspace(shell, shell->workspaces.current);
563}
564
565static void
566activate_workspace(struct desktop_shell *shell, unsigned int index)
567{
568 struct workspace *ws;
569
570 ws = get_workspace(shell, index);
571 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
572
573 shell->workspaces.current = index;
574}
575
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200576static unsigned int
577get_output_height(struct weston_output *output)
578{
579 return abs(output->region.extents.y1 - output->region.extents.y2);
580}
581
582static void
583surface_translate(struct weston_surface *surface, double d)
584{
585 struct shell_surface *shsurf = get_shell_surface(surface);
586 struct weston_transform *transform;
587
588 transform = &shsurf->workspace_transform;
589 if (wl_list_empty(&transform->link))
590 wl_list_insert(surface->geometry.transformation_list.prev,
591 &shsurf->workspace_transform.link);
592
593 weston_matrix_init(&shsurf->workspace_transform.matrix);
594 weston_matrix_translate(&shsurf->workspace_transform.matrix,
595 0.0, d, 0.0);
596 surface->geometry.dirty = 1;
597}
598
599static void
600workspace_translate_out(struct workspace *ws, double fraction)
601{
602 struct weston_surface *surface;
603 unsigned int height;
604 double d;
605
606 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
607 height = get_output_height(surface->output);
608 d = height * fraction;
609
610 surface_translate(surface, d);
611 }
612}
613
614static void
615workspace_translate_in(struct workspace *ws, double fraction)
616{
617 struct weston_surface *surface;
618 unsigned int height;
619 double d;
620
621 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
622 height = get_output_height(surface->output);
623
624 if (fraction > 0)
625 d = -(height - height * fraction);
626 else
627 d = height + height * fraction;
628
629 surface_translate(surface, d);
630 }
631}
632
633static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200634broadcast_current_workspace_state(struct desktop_shell *shell)
635{
636 struct wl_resource *resource;
637
638 wl_list_for_each(resource, &shell->workspaces.client_list, link)
639 workspace_manager_send_state(resource,
640 shell->workspaces.current,
641 shell->workspaces.num);
642}
643
644static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200645reverse_workspace_change_animation(struct desktop_shell *shell,
646 unsigned int index,
647 struct workspace *from,
648 struct workspace *to)
649{
650 shell->workspaces.current = index;
651
652 shell->workspaces.anim_to = to;
653 shell->workspaces.anim_from = from;
654 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
655 shell->workspaces.anim_timestamp = 0;
656
Scott Moreau4272e632012-08-13 09:58:41 -0600657 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200658}
659
660static void
661workspace_deactivate_transforms(struct workspace *ws)
662{
663 struct weston_surface *surface;
664 struct shell_surface *shsurf;
665
666 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
667 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200668 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
669 wl_list_remove(&shsurf->workspace_transform.link);
670 wl_list_init(&shsurf->workspace_transform.link);
671 }
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200672 shsurf->surface->geometry.dirty = 1;
673 }
674}
675
676static void
677finish_workspace_change_animation(struct desktop_shell *shell,
678 struct workspace *from,
679 struct workspace *to)
680{
Scott Moreau4272e632012-08-13 09:58:41 -0600681 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200682
683 wl_list_remove(&shell->workspaces.animation.link);
684 workspace_deactivate_transforms(from);
685 workspace_deactivate_transforms(to);
686 shell->workspaces.anim_to = NULL;
687
688 wl_list_remove(&shell->workspaces.anim_from->layer.link);
689}
690
691static void
692animate_workspace_change_frame(struct weston_animation *animation,
693 struct weston_output *output, uint32_t msecs)
694{
695 struct desktop_shell *shell =
696 container_of(animation, struct desktop_shell,
697 workspaces.animation);
698 struct workspace *from = shell->workspaces.anim_from;
699 struct workspace *to = shell->workspaces.anim_to;
700 uint32_t t;
701 double x, y;
702
703 if (workspace_is_empty(from) && workspace_is_empty(to)) {
704 finish_workspace_change_animation(shell, from, to);
705 return;
706 }
707
708 if (shell->workspaces.anim_timestamp == 0) {
709 if (shell->workspaces.anim_current == 0.0)
710 shell->workspaces.anim_timestamp = msecs;
711 else
712 shell->workspaces.anim_timestamp =
713 msecs -
714 /* Invers of movement function 'y' below. */
715 (asin(1.0 - shell->workspaces.anim_current) *
716 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
717 M_2_PI);
718 }
719
720 t = msecs - shell->workspaces.anim_timestamp;
721
722 /*
723 * x = [0, π/2]
724 * y(x) = sin(x)
725 */
726 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
727 y = sin(x);
728
729 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600730 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200731
732 workspace_translate_out(from, shell->workspaces.anim_dir * y);
733 workspace_translate_in(to, shell->workspaces.anim_dir * y);
734 shell->workspaces.anim_current = y;
735
Scott Moreau4272e632012-08-13 09:58:41 -0600736 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200737 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200738 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200739 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200740}
741
742static void
743animate_workspace_change(struct desktop_shell *shell,
744 unsigned int index,
745 struct workspace *from,
746 struct workspace *to)
747{
748 struct weston_output *output;
749
750 int dir;
751
752 if (index > shell->workspaces.current)
753 dir = -1;
754 else
755 dir = 1;
756
757 shell->workspaces.current = index;
758
759 shell->workspaces.anim_dir = dir;
760 shell->workspaces.anim_from = from;
761 shell->workspaces.anim_to = to;
762 shell->workspaces.anim_current = 0.0;
763 shell->workspaces.anim_timestamp = 0;
764
765 output = container_of(shell->compositor->output_list.next,
766 struct weston_output, link);
767 wl_list_insert(&output->animation_list,
768 &shell->workspaces.animation.link);
769
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200770 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200771
772 workspace_translate_in(to, 0);
773
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400774 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200775
Scott Moreau4272e632012-08-13 09:58:41 -0600776 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200777}
778
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200779static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200780update_workspace(struct desktop_shell *shell, unsigned int index,
781 struct workspace *from, struct workspace *to)
782{
783 shell->workspaces.current = index;
784 wl_list_insert(&from->layer.link, &to->layer.link);
785 wl_list_remove(&from->layer.link);
786}
787
788static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200789change_workspace(struct desktop_shell *shell, unsigned int index)
790{
791 struct workspace *from;
792 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200793
794 if (index == shell->workspaces.current)
795 return;
796
797 /* Don't change workspace when there is any fullscreen surfaces. */
798 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
799 return;
800
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200801 from = get_current_workspace(shell);
802 to = get_workspace(shell, index);
803
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200804 if (shell->workspaces.anim_from == to &&
805 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200806 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200807 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200808 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200809 return;
810 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200811
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200812 if (shell->workspaces.anim_to != NULL)
813 finish_workspace_change_animation(shell,
814 shell->workspaces.anim_from,
815 shell->workspaces.anim_to);
816
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200817 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200818
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200819 if (workspace_is_empty(to) && workspace_is_empty(from))
820 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200821 else
822 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200823
824 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200825}
826
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200827static bool
828workspace_has_only(struct workspace *ws, struct weston_surface *surface)
829{
830 struct wl_list *list = &ws->layer.surface_list;
831 struct wl_list *e;
832
833 if (wl_list_empty(list))
834 return false;
835
836 e = list->next;
837
838 if (e->next != list)
839 return false;
840
841 return container_of(e, struct weston_surface, layer_link) == surface;
842}
843
844static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200845move_surface_to_workspace(struct desktop_shell *shell,
846 struct weston_surface *surface,
847 uint32_t workspace)
848{
849 struct workspace *from;
850 struct workspace *to;
851 struct weston_seat *seat;
852
853 if (workspace == shell->workspaces.current)
854 return;
855
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200856 if (workspace >= shell->workspaces.num)
857 workspace = shell->workspaces.num - 1;
858
Jonas Ådahle9d22502012-08-29 22:13:01 +0200859 from = get_current_workspace(shell);
860 to = get_workspace(shell, workspace);
861
862 wl_list_remove(&surface->layer_link);
863 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
864
865 drop_focus_state(shell, from, surface);
866 wl_list_for_each(seat, &shell->compositor->seat_list, link)
867 if (seat->has_keyboard &&
868 seat->keyboard.focus == &surface->surface)
869 wl_keyboard_set_focus(&seat->keyboard, NULL);
870
871 weston_surface_damage_below(surface);
872}
873
874static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200875take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200876 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200877 unsigned int index)
878{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200879 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200880 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200881 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200882 struct shell_surface *shsurf;
883 struct workspace *from;
884 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200885 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200886
887 if (surface == NULL ||
888 index == shell->workspaces.current)
889 return;
890
891 from = get_current_workspace(shell);
892 to = get_workspace(shell, index);
893
894 wl_list_remove(&surface->layer_link);
895 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
896
Jonas Ådahle9d22502012-08-29 22:13:01 +0200897 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200898 drop_focus_state(shell, from, surface);
899
900 if (shell->workspaces.anim_from == to &&
901 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200902 wl_list_remove(&to->layer.link);
903 wl_list_insert(from->layer.link.prev, &to->layer.link);
904
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200905 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200906 broadcast_current_workspace_state(shell);
907
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200908 return;
909 }
910
911 if (shell->workspaces.anim_to != NULL)
912 finish_workspace_change_animation(shell,
913 shell->workspaces.anim_from,
914 shell->workspaces.anim_to);
915
916 if (workspace_is_empty(from) &&
917 workspace_has_only(to, surface))
918 update_workspace(shell, index, from, to);
919 else {
920 shsurf = get_shell_surface(surface);
921 if (wl_list_empty(&shsurf->workspace_transform.link))
922 wl_list_insert(&shell->workspaces.anim_sticky_list,
923 &shsurf->workspace_transform.link);
924
925 animate_workspace_change(shell, index, from, to);
926 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200927
928 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200929
930 state = ensure_focus_state(shell, seat);
931 if (state != NULL)
932 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200933}
934
935static void
936workspace_manager_move_surface(struct wl_client *client,
937 struct wl_resource *resource,
938 struct wl_resource *surface_resource,
939 uint32_t workspace)
940{
941 struct desktop_shell *shell = resource->data;
942 struct weston_surface *surface =
943 (struct weston_surface *) surface_resource;
944
945 move_surface_to_workspace(shell, surface, workspace);
946}
947
948static const struct workspace_manager_interface workspace_manager_implementation = {
949 workspace_manager_move_surface,
950};
951
952static void
953unbind_resource(struct wl_resource *resource)
954{
955 wl_list_remove(&resource->link);
956 free(resource);
957}
958
959static void
960bind_workspace_manager(struct wl_client *client,
961 void *data, uint32_t version, uint32_t id)
962{
963 struct desktop_shell *shell = data;
964 struct wl_resource *resource;
965
966 resource = wl_client_add_object(client, &workspace_manager_interface,
967 &workspace_manager_implementation,
968 id, shell);
969
970 if (resource == NULL) {
971 weston_log("couldn't add workspace manager object");
972 return;
973 }
974
975 resource->destroy = unbind_resource;
976 wl_list_insert(&shell->workspaces.client_list, &resource->link);
977
978 workspace_manager_send_state(resource,
979 shell->workspaces.current,
980 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200981}
982
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200983static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400984noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100985 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500986{
987 grab->focus = NULL;
988}
989
990static void
Scott Moreau447013d2012-02-18 05:05:29 -0700991move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100992 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500993{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500994 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100995 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300996 struct shell_surface *shsurf = move->base.shsurf;
997 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100998 int dx = wl_fixed_to_int(pointer->x + move->dx);
999 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001000
1001 if (!shsurf)
1002 return;
1003
1004 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001005
Daniel Stone103db7f2012-05-08 17:17:55 +01001006 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001007 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001008
1009 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001010}
1011
1012static void
Scott Moreau447013d2012-02-18 05:05:29 -07001013move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001014 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001015{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001016 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1017 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001018 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001019 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001020
Daniel Stone4dbadb12012-05-30 16:31:51 +01001021 if (pointer->button_count == 0 &&
1022 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001023 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001024 free(grab);
1025 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001026}
1027
Scott Moreau447013d2012-02-18 05:05:29 -07001028static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001029 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001030 move_grab_motion,
1031 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001032};
1033
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001034static int
1035surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1036{
1037 struct weston_move_grab *move;
1038
1039 if (!shsurf)
1040 return -1;
1041
1042 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1043 return 0;
1044
1045 move = malloc(sizeof *move);
1046 if (!move)
1047 return -1;
1048
1049 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1050 ws->seat.pointer->grab_x;
1051 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1052 ws->seat.pointer->grab_y;
1053
1054 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1055 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1056
1057 return 0;
1058}
1059
1060static void
1061shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1062 struct wl_resource *seat_resource, uint32_t serial)
1063{
1064 struct weston_seat *ws = seat_resource->data;
1065 struct shell_surface *shsurf = resource->data;
1066
1067 if (ws->seat.pointer->button_count == 0 ||
1068 ws->seat.pointer->grab_serial != serial ||
1069 ws->seat.pointer->focus != &shsurf->surface->surface)
1070 return;
1071
1072 if (surface_move(shsurf, ws) < 0)
1073 wl_resource_post_no_memory(resource);
1074}
1075
1076struct weston_resize_grab {
1077 struct shell_grab base;
1078 uint32_t edges;
1079 int32_t width, height;
1080};
1081
1082static void
1083resize_grab_motion(struct wl_pointer_grab *grab,
1084 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1085{
1086 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1087 struct wl_pointer *pointer = grab->pointer;
1088 struct shell_surface *shsurf = resize->base.shsurf;
1089 int32_t width, height;
1090 wl_fixed_t from_x, from_y;
1091 wl_fixed_t to_x, to_y;
1092
1093 if (!shsurf)
1094 return;
1095
1096 weston_surface_from_global_fixed(shsurf->surface,
1097 pointer->grab_x, pointer->grab_y,
1098 &from_x, &from_y);
1099 weston_surface_from_global_fixed(shsurf->surface,
1100 pointer->x, pointer->y, &to_x, &to_y);
1101
1102 width = resize->width;
1103 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1104 width += wl_fixed_to_int(from_x - to_x);
1105 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1106 width += wl_fixed_to_int(to_x - from_x);
1107 }
1108
1109 height = resize->height;
1110 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1111 height += wl_fixed_to_int(from_y - to_y);
1112 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1113 height += wl_fixed_to_int(to_y - from_y);
1114 }
1115
1116 shsurf->client->send_configure(shsurf->surface,
1117 resize->edges, width, height);
1118}
1119
1120static void
1121send_configure(struct weston_surface *surface,
1122 uint32_t edges, int32_t width, int32_t height)
1123{
1124 struct shell_surface *shsurf = get_shell_surface(surface);
1125
1126 wl_shell_surface_send_configure(&shsurf->resource,
1127 edges, width, height);
1128}
1129
1130static const struct weston_shell_client shell_client = {
1131 send_configure
1132};
1133
1134static void
1135resize_grab_button(struct wl_pointer_grab *grab,
1136 uint32_t time, uint32_t button, uint32_t state_w)
1137{
1138 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1139 struct wl_pointer *pointer = grab->pointer;
1140 enum wl_pointer_button_state state = state_w;
1141
1142 if (pointer->button_count == 0 &&
1143 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1144 shell_grab_end(&resize->base);
1145 free(grab);
1146 }
1147}
1148
1149static const struct wl_pointer_grab_interface resize_grab_interface = {
1150 noop_grab_focus,
1151 resize_grab_motion,
1152 resize_grab_button,
1153};
1154
1155static int
1156surface_resize(struct shell_surface *shsurf,
1157 struct weston_seat *ws, uint32_t edges)
1158{
1159 struct weston_resize_grab *resize;
1160
1161 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1162 return 0;
1163
1164 if (edges == 0 || edges > 15 ||
1165 (edges & 3) == 3 || (edges & 12) == 12)
1166 return 0;
1167
1168 resize = malloc(sizeof *resize);
1169 if (!resize)
1170 return -1;
1171
1172 resize->edges = edges;
1173 resize->width = shsurf->surface->geometry.width;
1174 resize->height = shsurf->surface->geometry.height;
1175
1176 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1177 ws->seat.pointer, edges);
1178
1179 return 0;
1180}
1181
1182static void
1183shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1184 struct wl_resource *seat_resource, uint32_t serial,
1185 uint32_t edges)
1186{
1187 struct weston_seat *ws = seat_resource->data;
1188 struct shell_surface *shsurf = resource->data;
1189
1190 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1191 return;
1192
1193 if (ws->seat.pointer->button_count == 0 ||
1194 ws->seat.pointer->grab_serial != serial ||
1195 ws->seat.pointer->focus != &shsurf->surface->surface)
1196 return;
1197
1198 if (surface_resize(shsurf, ws, edges) < 0)
1199 wl_resource_post_no_memory(resource);
1200}
1201
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001202static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001203busy_cursor_grab_focus(struct wl_pointer_grab *base,
1204 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001205{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001206 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001207
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001208 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001209 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001210 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001211 }
1212}
1213
1214static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001215busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1216 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001217{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001218}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001219
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001220static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001221busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001222 uint32_t time, uint32_t button, uint32_t state)
1223{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001224 struct shell_grab *grab = (struct shell_grab *) base;
1225 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001226 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001227 (struct weston_surface *) grab->grab.pointer->current;
1228 struct weston_seat *seat =
1229 (struct weston_seat *) grab->grab.pointer->seat;
1230
1231 shsurf = get_shell_surface(surface);
1232 if (shsurf && button == BTN_LEFT && state) {
1233 activate(shsurf->shell, shsurf->surface, seat);
1234 surface_move(shsurf, seat);
1235 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001236}
1237
1238static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1239 busy_cursor_grab_focus,
1240 busy_cursor_grab_motion,
1241 busy_cursor_grab_button,
1242};
1243
1244static void
1245set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1246{
1247 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001248
1249 grab = malloc(sizeof *grab);
1250 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001251 return;
1252
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001253 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1254 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001255}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001256
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001257static void
1258end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1259{
1260 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1261
1262 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001263 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001264 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001265 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001266}
1267
Scott Moreau9521d5e2012-04-19 13:06:17 -06001268static void
1269ping_timer_destroy(struct shell_surface *shsurf)
1270{
1271 if (!shsurf || !shsurf->ping_timer)
1272 return;
1273
1274 if (shsurf->ping_timer->source)
1275 wl_event_source_remove(shsurf->ping_timer->source);
1276
1277 free(shsurf->ping_timer);
1278 shsurf->ping_timer = NULL;
1279}
1280
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001281static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001282ping_timeout_handler(void *data)
1283{
1284 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001285 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001286
Scott Moreau9521d5e2012-04-19 13:06:17 -06001287 /* Client is not responding */
1288 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001289
1290 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1291 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1292 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001293
1294 return 1;
1295}
1296
1297static void
1298ping_handler(struct weston_surface *surface, uint32_t serial)
1299{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001300 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001301 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001302 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001303
1304 if (!shsurf)
1305 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001306 if (!shsurf->resource.client)
1307 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001308
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001309 if (shsurf->surface == shsurf->shell->grab_surface)
1310 return;
1311
Scott Moreauff1db4a2012-04-17 19:06:18 -06001312 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001313 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001314 if (!shsurf->ping_timer)
1315 return;
1316
1317 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001318 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1319 shsurf->ping_timer->source =
1320 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1321 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1322
1323 wl_shell_surface_send_ping(&shsurf->resource, serial);
1324 }
1325}
1326
1327static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001328handle_pointer_focus(struct wl_listener *listener, void *data)
1329{
1330 struct wl_pointer *pointer = data;
1331 struct weston_surface *surface =
1332 (struct weston_surface *) pointer->focus;
1333 struct weston_compositor *compositor;
1334 struct shell_surface *shsurf;
1335 uint32_t serial;
1336
1337 if (!surface)
1338 return;
1339
1340 compositor = surface->compositor;
1341 shsurf = get_shell_surface(surface);
1342
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001343 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001344 set_busy_cursor(shsurf, pointer);
1345 } else {
1346 serial = wl_display_next_serial(compositor->wl_display);
1347 ping_handler(surface, serial);
1348 }
1349}
1350
1351static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001352create_pointer_focus_listener(struct weston_seat *seat)
1353{
1354 struct wl_listener *listener;
1355
1356 if (!seat->seat.pointer)
1357 return;
1358
1359 listener = malloc(sizeof *listener);
1360 listener->notify = handle_pointer_focus;
1361 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1362}
1363
1364static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001365shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1366 uint32_t serial)
1367{
1368 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001369 struct desktop_shell *shell = shsurf->shell;
1370 struct weston_seat *seat;
1371 struct weston_compositor *ec = shsurf->surface->compositor;
1372 struct wl_pointer *pointer;
1373 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001374
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001375 if (shsurf->ping_timer == NULL)
1376 /* Just ignore unsolicited pong. */
1377 return;
1378
Scott Moreauff1db4a2012-04-17 19:06:18 -06001379 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001380 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001381 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001382 if (was_unresponsive) {
1383 /* Received pong from previously unresponsive client */
1384 wl_list_for_each(seat, &ec->seat_list, link) {
1385 pointer = seat->seat.pointer;
1386 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001387 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001388 pointer->current ==
1389 &shsurf->surface->surface)
1390 end_busy_cursor(shsurf, pointer);
1391 }
1392 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001393 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001394 }
1395}
1396
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001397static void
1398shell_surface_set_title(struct wl_client *client,
1399 struct wl_resource *resource, const char *title)
1400{
1401 struct shell_surface *shsurf = resource->data;
1402
1403 free(shsurf->title);
1404 shsurf->title = strdup(title);
1405}
1406
1407static void
1408shell_surface_set_class(struct wl_client *client,
1409 struct wl_resource *resource, const char *class)
1410{
1411 struct shell_surface *shsurf = resource->data;
1412
1413 free(shsurf->class);
1414 shsurf->class = strdup(class);
1415}
1416
Juan Zhao96879df2012-02-07 08:45:41 +08001417static struct weston_output *
1418get_default_output(struct weston_compositor *compositor)
1419{
1420 return container_of(compositor->output_list.next,
1421 struct weston_output, link);
1422}
1423
Alex Wu4539b082012-03-01 12:57:46 +08001424static void
1425shell_unset_fullscreen(struct shell_surface *shsurf)
1426{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001427 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001428 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001429 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1430 shell_surface_is_top_fullscreen(shsurf)) {
1431 weston_output_switch_mode(shsurf->fullscreen_output,
1432 shsurf->fullscreen_output->origin);
1433 }
Alex Wu4539b082012-03-01 12:57:46 +08001434 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1435 shsurf->fullscreen.framerate = 0;
1436 wl_list_remove(&shsurf->fullscreen.transform.link);
1437 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001438 if (shsurf->fullscreen.black_surface)
1439 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001440 shsurf->fullscreen.black_surface = NULL;
1441 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001442 weston_surface_set_position(shsurf->surface,
1443 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001444 if (shsurf->saved_rotation_valid) {
1445 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1446 &shsurf->rotation.transform.link);
1447 shsurf->saved_rotation_valid = false;
1448 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001449
1450 ws = get_current_workspace(shsurf->shell);
1451 wl_list_remove(&shsurf->surface->layer_link);
1452 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001453}
1454
Pekka Paalanen98262232011-12-01 10:42:22 +02001455static int
1456reset_shell_surface_type(struct shell_surface *surface)
1457{
1458 switch (surface->type) {
1459 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001460 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001461 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001462 case SHELL_SURFACE_MAXIMIZED:
1463 surface->output = get_default_output(surface->surface->compositor);
1464 weston_surface_set_position(surface->surface,
1465 surface->saved_x,
1466 surface->saved_y);
1467 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001468 case SHELL_SURFACE_NONE:
1469 case SHELL_SURFACE_TOPLEVEL:
1470 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001471 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001472 break;
1473 }
1474
1475 surface->type = SHELL_SURFACE_NONE;
1476 return 0;
1477}
1478
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001479static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001480set_surface_type(struct shell_surface *shsurf)
1481{
1482 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001483 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001484
1485 reset_shell_surface_type(shsurf);
1486
1487 shsurf->type = shsurf->next_type;
1488 shsurf->next_type = SHELL_SURFACE_NONE;
1489
1490 switch (shsurf->type) {
1491 case SHELL_SURFACE_TOPLEVEL:
1492 break;
1493 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001494 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001495 pes->geometry.x + shsurf->transient.x,
1496 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001497 break;
1498
1499 case SHELL_SURFACE_MAXIMIZED:
1500 shsurf->saved_x = surface->geometry.x;
1501 shsurf->saved_y = surface->geometry.y;
1502 shsurf->saved_position_valid = true;
1503 break;
1504
1505 case SHELL_SURFACE_FULLSCREEN:
1506 shsurf->saved_x = surface->geometry.x;
1507 shsurf->saved_y = surface->geometry.y;
1508 shsurf->saved_position_valid = true;
1509
1510 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1511 wl_list_remove(&shsurf->rotation.transform.link);
1512 wl_list_init(&shsurf->rotation.transform.link);
1513 shsurf->surface->geometry.dirty = 1;
1514 shsurf->saved_rotation_valid = true;
1515 }
1516 break;
1517
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001518 default:
1519 break;
1520 }
1521}
1522
1523static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001524set_toplevel(struct shell_surface *shsurf)
1525{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001526 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001527}
1528
1529static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001530shell_surface_set_toplevel(struct wl_client *client,
1531 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001532{
Pekka Paalanen98262232011-12-01 10:42:22 +02001533 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001534
Tiago Vignattibc052c92012-04-19 16:18:18 +03001535 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001536}
1537
1538static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001539set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001540 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001541{
1542 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001543 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001544 shsurf->transient.x = x;
1545 shsurf->transient.y = y;
1546 shsurf->transient.flags = flags;
1547 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1548}
1549
1550static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001551shell_surface_set_transient(struct wl_client *client,
1552 struct wl_resource *resource,
1553 struct wl_resource *parent_resource,
1554 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001555{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001556 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001557 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001558
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001559 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001560}
1561
Tiago Vignattibe143262012-04-16 17:31:41 +03001562static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001563shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001564{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001565 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001566}
1567
1568static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001569get_output_panel_height(struct desktop_shell *shell,
1570 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001571{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001572 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001573 int panel_height = 0;
1574
1575 if (!output)
1576 return 0;
1577
Juan Zhao4ab94682012-07-09 22:24:09 -07001578 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001579 if (surface->output == output) {
1580 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001581 break;
1582 }
1583 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001584
Juan Zhao96879df2012-02-07 08:45:41 +08001585 return panel_height;
1586}
1587
1588static void
1589shell_surface_set_maximized(struct wl_client *client,
1590 struct wl_resource *resource,
1591 struct wl_resource *output_resource )
1592{
1593 struct shell_surface *shsurf = resource->data;
1594 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001595 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001596 uint32_t edges = 0, panel_height = 0;
1597
1598 /* get the default output, if the client set it as NULL
1599 check whether the ouput is available */
1600 if (output_resource)
1601 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001602 else if (es->output)
1603 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001604 else
1605 shsurf->output = get_default_output(es->compositor);
1606
Tiago Vignattibe143262012-04-16 17:31:41 +03001607 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001608 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001609 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001610
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001611 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001612 shsurf->output->width,
1613 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001614
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001615 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001616}
1617
Alex Wu21858432012-04-01 20:13:08 +08001618static void
1619black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1620
Alex Wu4539b082012-03-01 12:57:46 +08001621static struct weston_surface *
1622create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001623 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001624 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001625{
1626 struct weston_surface *surface = NULL;
1627
1628 surface = weston_surface_create(ec);
1629 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001630 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001631 return NULL;
1632 }
1633
Alex Wu21858432012-04-01 20:13:08 +08001634 surface->configure = black_surface_configure;
1635 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001636 weston_surface_configure(surface, x, y, w, h);
1637 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001638 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001639 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001640 pixman_region32_fini(&surface->input);
1641 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001642
Alex Wu4539b082012-03-01 12:57:46 +08001643 return surface;
1644}
1645
1646/* Create black surface and append it to the associated fullscreen surface.
1647 * Handle size dismatch and positioning according to the method. */
1648static void
1649shell_configure_fullscreen(struct shell_surface *shsurf)
1650{
1651 struct weston_output *output = shsurf->fullscreen_output;
1652 struct weston_surface *surface = shsurf->surface;
1653 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001654 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001655
Alex Wu4539b082012-03-01 12:57:46 +08001656 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001657 shsurf->fullscreen.black_surface =
1658 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001659 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001660 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001661 output->width,
1662 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001663
1664 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1665 wl_list_insert(&surface->layer_link,
1666 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001667 shsurf->fullscreen.black_surface->output = output;
1668
1669 switch (shsurf->fullscreen.type) {
1670 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001671 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001672 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001673 break;
1674 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1675 matrix = &shsurf->fullscreen.transform.matrix;
1676 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001677
Scott Moreau1bad5db2012-08-18 01:04:05 -06001678 output_aspect = (float) output->width /
1679 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001680 surface_aspect = (float) surface->geometry.width /
1681 (float) surface->geometry.height;
1682 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001683 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001684 (float) surface->geometry.width;
1685 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001686 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001687 (float) surface->geometry.height;
1688
Alex Wu4539b082012-03-01 12:57:46 +08001689 weston_matrix_scale(matrix, scale, scale, 1);
1690 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001691 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001692 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001693 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1694 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001695 weston_surface_set_position(surface, x, y);
1696
Alex Wu4539b082012-03-01 12:57:46 +08001697 break;
1698 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001699 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001700 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001701 surface->geometry.width,
1702 surface->geometry.height,
1703 shsurf->fullscreen.framerate};
1704
1705 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001706 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001707 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001708 output->width,
1709 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001710 weston_surface_set_position(surface, output->x, output->y);
1711 break;
1712 }
1713 }
Alex Wu4539b082012-03-01 12:57:46 +08001714 break;
1715 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1716 break;
1717 default:
1718 break;
1719 }
1720}
1721
1722/* make the fullscreen and black surface at the top */
1723static void
1724shell_stack_fullscreen(struct shell_surface *shsurf)
1725{
Alex Wubd3354b2012-04-17 17:20:49 +08001726 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001727 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001728 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001729
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001730 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001731 wl_list_insert(&shell->fullscreen_layer.surface_list,
1732 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001733 weston_surface_damage(surface);
1734
1735 if (!shsurf->fullscreen.black_surface)
1736 shsurf->fullscreen.black_surface =
1737 create_black_surface(surface->compositor,
1738 surface,
1739 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001740 output->width,
1741 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001742
1743 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001744 wl_list_insert(&surface->layer_link,
1745 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001746 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001747}
1748
1749static void
1750shell_map_fullscreen(struct shell_surface *shsurf)
1751{
Alex Wu4539b082012-03-01 12:57:46 +08001752 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001753 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001754}
1755
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001756static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001757shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001758 struct wl_resource *resource,
1759 uint32_t method,
1760 uint32_t framerate,
1761 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001762{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001763 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001764 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001765
1766 if (output_resource)
1767 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001768 else if (es->output)
1769 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001770 else
1771 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001772
Alex Wu4539b082012-03-01 12:57:46 +08001773 shsurf->fullscreen_output = shsurf->output;
1774 shsurf->fullscreen.type = method;
1775 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001776 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001777
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001778 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001779 shsurf->output->width,
1780 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001781}
1782
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001783static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001784popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001785 struct wl_surface *surface,
1786 wl_fixed_t x,
1787 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001788{
Daniel Stone37816df2012-05-16 18:45:18 +01001789 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001790 struct shell_surface *priv =
1791 container_of(grab, struct shell_surface, popup.grab);
1792 struct wl_client *client = priv->surface->surface.resource.client;
1793
Pekka Paalanencb108432012-01-19 16:25:40 +02001794 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001795 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001796 grab->focus = surface;
1797 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001798 wl_pointer_set_focus(pointer, NULL,
1799 wl_fixed_from_int(0),
1800 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001801 grab->focus = NULL;
1802 }
1803}
1804
1805static void
Scott Moreau447013d2012-02-18 05:05:29 -07001806popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001807 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001808{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001809 struct wl_resource *resource;
1810
Daniel Stone37816df2012-05-16 18:45:18 +01001811 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001812 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001813 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001814}
1815
1816static void
Scott Moreau447013d2012-02-18 05:05:29 -07001817popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001818 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001819{
1820 struct wl_resource *resource;
1821 struct shell_surface *shsurf =
1822 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001823 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001824 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001825 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001826
Daniel Stone37816df2012-05-16 18:45:18 +01001827 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001828 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001829 display = wl_client_get_display(resource->client);
1830 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001831 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001832 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001833 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001834 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001835 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001836 }
1837
Daniel Stone4dbadb12012-05-30 16:31:51 +01001838 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001839 shsurf->popup.initial_up = 1;
1840}
1841
Scott Moreau447013d2012-02-18 05:05:29 -07001842static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001843 popup_grab_focus,
1844 popup_grab_motion,
1845 popup_grab_button,
1846};
1847
1848static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001849popup_grab_end(struct wl_pointer *pointer)
1850{
1851 struct wl_pointer_grab *grab = pointer->grab;
1852 struct shell_surface *shsurf =
1853 container_of(grab, struct shell_surface, popup.grab);
1854
1855 if (pointer->grab->interface == &popup_grab_interface) {
1856 wl_shell_surface_send_popup_done(&shsurf->resource);
1857 wl_pointer_end_grab(grab->pointer);
1858 shsurf->popup.grab.pointer = NULL;
1859 }
1860}
1861
1862static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001863shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001864{
Daniel Stone37816df2012-05-16 18:45:18 +01001865 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001866 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001867 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001868
1869 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001870 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001871
Pekka Paalanen938269a2012-02-07 14:19:01 +02001872 weston_surface_update_transform(parent);
1873 if (parent->transform.enabled) {
1874 shsurf->popup.parent_transform.matrix =
1875 parent->transform.matrix;
1876 } else {
1877 /* construct x, y translation matrix */
1878 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03001879 shsurf->popup.parent_transform.matrix.type =
1880 WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen938269a2012-02-07 14:19:01 +02001881 shsurf->popup.parent_transform.matrix.d[12] =
1882 parent->geometry.x;
1883 shsurf->popup.parent_transform.matrix.d[13] =
1884 parent->geometry.y;
1885 }
1886 wl_list_insert(es->geometry.transformation_list.prev,
1887 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001888
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001889 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001890 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1891 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001892
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001893 /* We don't require the grab to still be active, but if another
1894 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001895 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1896 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001897 } else {
1898 wl_shell_surface_send_popup_done(&shsurf->resource);
1899 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001900}
1901
1902static void
1903shell_surface_set_popup(struct wl_client *client,
1904 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001905 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001906 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001907 struct wl_resource *parent_resource,
1908 int32_t x, int32_t y, uint32_t flags)
1909{
1910 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001911
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001912 shsurf->type = SHELL_SURFACE_POPUP;
1913 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001914 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001915 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001916 shsurf->popup.x = x;
1917 shsurf->popup.y = y;
1918}
1919
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001920static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001921 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001922 shell_surface_move,
1923 shell_surface_resize,
1924 shell_surface_set_toplevel,
1925 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001926 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001927 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001928 shell_surface_set_maximized,
1929 shell_surface_set_title,
1930 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001931};
1932
1933static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001934destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001935{
Daniel Stone37816df2012-05-16 18:45:18 +01001936 if (shsurf->popup.grab.pointer)
1937 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001938
Alex Wubd3354b2012-04-17 17:20:49 +08001939 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1940 shell_surface_is_top_fullscreen(shsurf)) {
1941 weston_output_switch_mode(shsurf->fullscreen_output,
1942 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001943 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001944
Alex Wuaa08e2d2012-03-05 11:01:40 +08001945 if (shsurf->fullscreen.black_surface)
1946 weston_surface_destroy(shsurf->fullscreen.black_surface);
1947
Alex Wubd3354b2012-04-17 17:20:49 +08001948 /* As destroy_resource() use wl_list_for_each_safe(),
1949 * we can always remove the listener.
1950 */
1951 wl_list_remove(&shsurf->surface_destroy_listener.link);
1952 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001953 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001954
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001955 wl_list_remove(&shsurf->link);
1956 free(shsurf);
1957}
1958
1959static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001960shell_destroy_shell_surface(struct wl_resource *resource)
1961{
1962 struct shell_surface *shsurf = resource->data;
1963
1964 destroy_shell_surface(shsurf);
1965}
1966
1967static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001968shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001969{
1970 struct shell_surface *shsurf = container_of(listener,
1971 struct shell_surface,
1972 surface_destroy_listener);
1973
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001974 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001975 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001976 } else {
1977 wl_signal_emit(&shsurf->resource.destroy_signal,
1978 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001979 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001980 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001981}
1982
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001983static void
1984shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1985
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001986static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001987get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001988{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001989 if (surface->configure == shell_surface_configure)
1990 return surface->private;
1991 else
1992 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001993}
1994
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001995static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001996create_shell_surface(void *shell, struct weston_surface *surface,
1997 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001998{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001999 struct shell_surface *shsurf;
2000
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002001 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002002 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002003 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002004 }
2005
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002006 shsurf = calloc(1, sizeof *shsurf);
2007 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002008 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002009 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002010 }
2011
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002012 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002013 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002014
Tiago Vignattibc052c92012-04-19 16:18:18 +03002015 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002016 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002017 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002018 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002019 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002020 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2021 shsurf->fullscreen.framerate = 0;
2022 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002023 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002024 wl_list_init(&shsurf->fullscreen.transform.link);
2025
Tiago Vignattibc052c92012-04-19 16:18:18 +03002026 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002027 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2028 wl_signal_add(&surface->surface.resource.destroy_signal,
2029 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002030
2031 /* init link so its safe to always remove it in destroy_shell_surface */
2032 wl_list_init(&shsurf->link);
2033
Pekka Paalanen460099f2012-01-20 16:48:25 +02002034 /* empty when not in use */
2035 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002036 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002037
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002038 wl_list_init(&shsurf->workspace_transform.link);
2039
Pekka Paalanen98262232011-12-01 10:42:22 +02002040 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002041 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002042
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002043 shsurf->client = client;
2044
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002045 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002046}
2047
2048static void
2049shell_get_shell_surface(struct wl_client *client,
2050 struct wl_resource *resource,
2051 uint32_t id,
2052 struct wl_resource *surface_resource)
2053{
2054 struct weston_surface *surface = surface_resource->data;
2055 struct desktop_shell *shell = resource->data;
2056 struct shell_surface *shsurf;
2057
2058 if (get_shell_surface(surface)) {
2059 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002060 WL_DISPLAY_ERROR_INVALID_OBJECT,
2061 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002062 return;
2063 }
2064
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002065 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002066 if (!shsurf) {
2067 wl_resource_post_error(surface_resource,
2068 WL_DISPLAY_ERROR_INVALID_OBJECT,
2069 "surface->configure already set");
2070 return;
2071 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002072
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002073 shsurf->resource.destroy = shell_destroy_shell_surface;
2074 shsurf->resource.object.id = id;
2075 shsurf->resource.object.interface = &wl_shell_surface_interface;
2076 shsurf->resource.object.implementation =
2077 (void (**)(void)) &shell_surface_implementation;
2078 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002079
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002080 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002081}
2082
2083static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002084 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002085};
2086
Kristian Høgsberg07937562011-04-12 17:25:42 -04002087static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002088handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002089{
2090 proc->pid = 0;
2091}
2092
2093static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002094launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002095{
2096 if (shell->screensaver.binding)
2097 return;
2098
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002099 if (!shell->screensaver.path)
2100 return;
2101
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002102 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002103 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002104 return;
2105 }
2106
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002107 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002108 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002109 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002110 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002111}
2112
2113static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002114terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002115{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002116 if (shell->screensaver.process.pid == 0)
2117 return;
2118
2119 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002120}
2121
2122static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002123configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002124{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002125 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002126
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002127 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2128 if (s->output == es->output && s != es) {
2129 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002130 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002131 }
2132 }
2133
2134 weston_surface_configure(es, es->output->x, es->output->y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002135 weston_surface_buffer_width(es),
2136 weston_surface_buffer_height(es));
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002137
2138 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002139 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002140 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002141 }
2142}
2143
2144static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002145background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2146{
2147 struct desktop_shell *shell = es->private;
2148
2149 configure_static_surface(es, &shell->background_layer);
2150}
2151
2152static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002153desktop_shell_set_background(struct wl_client *client,
2154 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002155 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002156 struct wl_resource *surface_resource)
2157{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002158 struct desktop_shell *shell = resource->data;
2159 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002160
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002161 if (surface->configure) {
2162 wl_resource_post_error(surface_resource,
2163 WL_DISPLAY_ERROR_INVALID_OBJECT,
2164 "surface role already assigned");
2165 return;
2166 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002167
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002168 surface->configure = background_configure;
2169 surface->private = shell;
2170 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002171 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002172 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002173 surface->output->width,
2174 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002175}
2176
2177static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002178panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2179{
2180 struct desktop_shell *shell = es->private;
2181
2182 configure_static_surface(es, &shell->panel_layer);
2183}
2184
2185static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002186desktop_shell_set_panel(struct wl_client *client,
2187 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002188 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002189 struct wl_resource *surface_resource)
2190{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002191 struct desktop_shell *shell = resource->data;
2192 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002193
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002194 if (surface->configure) {
2195 wl_resource_post_error(surface_resource,
2196 WL_DISPLAY_ERROR_INVALID_OBJECT,
2197 "surface role already assigned");
2198 return;
2199 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002200
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002201 surface->configure = panel_configure;
2202 surface->private = shell;
2203 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002204 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002205 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002206 surface->output->width,
2207 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002208}
2209
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002210static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002211lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2212{
2213 struct desktop_shell *shell = surface->private;
2214
2215 center_on_output(surface, get_default_output(shell->compositor));
2216
2217 if (!weston_surface_is_mapped(surface)) {
2218 wl_list_insert(&shell->lock_layer.surface_list,
2219 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002220 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002221 weston_compositor_wake(shell->compositor);
2222 }
2223}
2224
2225static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002226handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002227{
Tiago Vignattibe143262012-04-16 17:31:41 +03002228 struct desktop_shell *shell =
2229 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002230
Martin Minarik6d118362012-06-07 18:01:59 +02002231 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002232 shell->lock_surface = NULL;
2233}
2234
2235static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002236desktop_shell_set_lock_surface(struct wl_client *client,
2237 struct wl_resource *resource,
2238 struct wl_resource *surface_resource)
2239{
Tiago Vignattibe143262012-04-16 17:31:41 +03002240 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002241 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002242
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002243 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002244
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002245 if (!shell->locked)
2246 return;
2247
Pekka Paalanen98262232011-12-01 10:42:22 +02002248 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002249
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002250 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2251 wl_signal_add(&surface_resource->destroy_signal,
2252 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002253
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002254 surface->configure = lock_surface_configure;
2255 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002256}
2257
2258static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002259resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002260{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002261 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002262
Pekka Paalanen77346a62011-11-30 16:26:35 +02002263 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002264
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002265 wl_list_remove(&shell->lock_layer.link);
2266 wl_list_insert(&shell->compositor->cursor_layer.link,
2267 &shell->fullscreen_layer.link);
2268 wl_list_insert(&shell->fullscreen_layer.link,
2269 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002270 if (shell->showing_input_panels) {
2271 wl_list_insert(&shell->panel_layer.link,
2272 &shell->input_panel_layer.link);
2273 wl_list_insert(&shell->input_panel_layer.link,
2274 &ws->layer.link);
2275 } else {
2276 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2277 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002278
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002279 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002280
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002281 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002282 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002283 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002284 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002285}
2286
2287static void
2288desktop_shell_unlock(struct wl_client *client,
2289 struct wl_resource *resource)
2290{
Tiago Vignattibe143262012-04-16 17:31:41 +03002291 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002292
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002293 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002294
2295 if (shell->locked)
2296 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002297}
2298
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002299static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002300desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002301 struct wl_resource *resource,
2302 struct wl_resource *surface_resource)
2303{
2304 struct desktop_shell *shell = resource->data;
2305
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002306 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002307}
2308
Kristian Høgsberg75840622011-09-06 13:48:16 -04002309static const struct desktop_shell_interface desktop_shell_implementation = {
2310 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002311 desktop_shell_set_panel,
2312 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002313 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002314 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002315};
2316
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002317static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002318get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002319{
2320 struct shell_surface *shsurf;
2321
2322 shsurf = get_shell_surface(surface);
2323 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002324 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002325 return shsurf->type;
2326}
2327
Kristian Høgsberg75840622011-09-06 13:48:16 -04002328static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002329move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002330{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002331 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002332 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002333 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002334
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002335 if (surface == NULL)
2336 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002337
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002338 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002339 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002340 return;
2341
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002342 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002343}
2344
2345static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002346resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002347{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002348 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002349 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002350 uint32_t edges = 0;
2351 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002352 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002353
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002354 if (surface == NULL)
2355 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002356
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002357 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002358 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002359 return;
2360
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002361 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002362 wl_fixed_to_int(seat->pointer->grab_x),
2363 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002364 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002365
Pekka Paalanen60921e52012-01-25 15:55:43 +02002366 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002367 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002368 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002369 edges |= 0;
2370 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002371 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002372
Pekka Paalanen60921e52012-01-25 15:55:43 +02002373 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002374 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002375 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002376 edges |= 0;
2377 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002378 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002379
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002380 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002381}
2382
2383static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002384surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002385 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002386{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002387 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002388 struct shell_surface *shsurf;
2389 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002390 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002391
2392 if (surface == NULL)
2393 return;
2394
2395 shsurf = get_shell_surface(surface);
2396 if (!shsurf)
2397 return;
2398
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002399 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002400
Scott Moreau02709af2012-05-22 01:54:10 -06002401 if (surface->alpha > 1.0)
2402 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002403 if (surface->alpha < step)
2404 surface->alpha = step;
2405
2406 surface->geometry.dirty = 1;
2407 weston_surface_damage(surface);
2408}
2409
2410static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002411do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002412 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002413{
Daniel Stone37816df2012-05-16 18:45:18 +01002414 struct weston_seat *ws = (struct weston_seat *) seat;
2415 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002416 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002417 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002418
2419 wl_list_for_each(output, &compositor->output_list, link) {
2420 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002421 wl_fixed_to_double(seat->pointer->x),
2422 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002423 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002424 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002425 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002426 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002427 increment = -output->zoom.increment;
2428 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002429 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002430 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002431 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002432 else
2433 increment = 0;
2434
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002435 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002436
Scott Moreaue6603982012-06-11 13:07:51 -06002437 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002438 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002439 else if (output->zoom.level > output->zoom.max_level)
2440 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002441 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002442 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002443 output->disable_planes++;
2444 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002445
Scott Moreaue6603982012-06-11 13:07:51 -06002446 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002447
Scott Moreau8dacaab2012-06-17 18:10:58 -06002448 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002449 }
2450 }
2451}
2452
Scott Moreauccbf29d2012-02-22 14:21:41 -07002453static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002454zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002455 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002456{
2457 do_zoom(seat, time, 0, axis, value);
2458}
2459
2460static void
2461zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2462 void *data)
2463{
2464 do_zoom(seat, time, key, 0, 0);
2465}
2466
2467static void
2468terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2469 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002470{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002471 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002472
Daniel Stone325fc2d2012-05-30 16:31:58 +01002473 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002474}
2475
2476static void
Scott Moreau447013d2012-02-18 05:05:29 -07002477rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002478 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002479{
2480 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002481 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002482 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002483 struct shell_surface *shsurf = rotate->base.shsurf;
2484 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002485 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002486
2487 if (!shsurf)
2488 return;
2489
2490 surface = shsurf->surface;
2491
2492 cx = 0.5f * surface->geometry.width;
2493 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002494
Daniel Stone37816df2012-05-16 18:45:18 +01002495 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2496 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002497 r = sqrtf(dx * dx + dy * dy);
2498
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002499 wl_list_remove(&shsurf->rotation.transform.link);
2500 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002501
2502 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002503 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002504 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002505
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002506 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002507 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002508
2509 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002510 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002511 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002512 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002513 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002514
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002515 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002516 &shsurf->surface->geometry.transformation_list,
2517 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002518 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002519 wl_list_init(&shsurf->rotation.transform.link);
2520 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002521 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002522 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002523
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002524 /* We need to adjust the position of the surface
2525 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002526 cposx = surface->geometry.x + cx;
2527 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002528 dposx = rotate->center.x - cposx;
2529 dposy = rotate->center.y - cposy;
2530 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002531 weston_surface_set_position(surface,
2532 surface->geometry.x + dposx,
2533 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002534 }
2535
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002536 /* Repaint implies weston_surface_update_transform(), which
2537 * lazily applies the damage due to rotation update.
2538 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002539 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002540}
2541
2542static void
Scott Moreau447013d2012-02-18 05:05:29 -07002543rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002544 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002545{
2546 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002547 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002548 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002549 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002550 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002551
Daniel Stone4dbadb12012-05-30 16:31:51 +01002552 if (pointer->button_count == 0 &&
2553 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002554 if (shsurf)
2555 weston_matrix_multiply(&shsurf->rotation.rotation,
2556 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002557 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002558 free(rotate);
2559 }
2560}
2561
Scott Moreau447013d2012-02-18 05:05:29 -07002562static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002563 noop_grab_focus,
2564 rotate_grab_motion,
2565 rotate_grab_button,
2566};
2567
2568static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002569rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002570 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002571{
2572 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002573 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002574 struct shell_surface *surface;
2575 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002576 float dx, dy;
2577 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002578
2579 if (base_surface == NULL)
2580 return;
2581
2582 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002583 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002584 return;
2585
Pekka Paalanen460099f2012-01-20 16:48:25 +02002586 rotate = malloc(sizeof *rotate);
2587 if (!rotate)
2588 return;
2589
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002590 weston_surface_to_global_float(surface->surface,
2591 surface->surface->geometry.width / 2,
2592 surface->surface->geometry.height / 2,
2593 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002594
Daniel Stone37816df2012-05-16 18:45:18 +01002595 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2596 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002597 r = sqrtf(dx * dx + dy * dy);
2598 if (r > 20.0f) {
2599 struct weston_matrix inverse;
2600
2601 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002602 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002603 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002604
2605 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002606 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002607 } else {
2608 weston_matrix_init(&surface->rotation.rotation);
2609 weston_matrix_init(&rotate->rotation);
2610 }
2611
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002612 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2613 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002614}
2615
2616static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002617lower_fullscreen_layer(struct desktop_shell *shell)
2618{
2619 struct workspace *ws;
2620 struct weston_surface *surface, *prev;
2621
2622 ws = get_current_workspace(shell);
2623 wl_list_for_each_reverse_safe(surface, prev,
2624 &shell->fullscreen_layer.surface_list,
2625 layer_link)
2626 weston_surface_restack(surface, &ws->layer.surface_list);
2627}
2628
2629static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002630activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002631 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002632{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002633 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002634 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002635
Daniel Stone37816df2012-05-16 18:45:18 +01002636 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002637
Jonas Ådahl8538b222012-08-29 22:13:03 +02002638 state = ensure_focus_state(shell, seat);
2639 if (state == NULL)
2640 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002641
2642 state->keyboard_focus = es;
2643 wl_list_remove(&state->surface_destroy_listener.link);
2644 wl_signal_add(&es->surface.resource.destroy_signal,
2645 &state->surface_destroy_listener);
2646
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002647 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002648 case SHELL_SURFACE_FULLSCREEN:
2649 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002650 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002651 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002652 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002653 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002654 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002655 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002656 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002657 }
2658}
2659
Alex Wu21858432012-04-01 20:13:08 +08002660/* no-op func for checking black surface */
2661static void
2662black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2663{
2664}
2665
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002666static bool
Alex Wu21858432012-04-01 20:13:08 +08002667is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2668{
2669 if (es->configure == black_surface_configure) {
2670 if (fs_surface)
2671 *fs_surface = (struct weston_surface *)es->private;
2672 return true;
2673 }
2674 return false;
2675}
2676
Kristian Høgsberg75840622011-09-06 13:48:16 -04002677static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002678click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002679 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002680{
Daniel Stone37816df2012-05-16 18:45:18 +01002681 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002682 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002683 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002684 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002685
Daniel Stone37816df2012-05-16 18:45:18 +01002686 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002687 if (!focus)
2688 return;
2689
Alex Wu21858432012-04-01 20:13:08 +08002690 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002691 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002692
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002693 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2694 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002695
Daniel Stone325fc2d2012-05-30 16:31:58 +01002696 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002697 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002698}
2699
2700static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002701lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002702{
Tiago Vignattibe143262012-04-16 17:31:41 +03002703 struct desktop_shell *shell =
2704 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002705 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002706 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002707
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002708 if (shell->locked) {
2709 wl_list_for_each(output, &shell->compositor->output_list, link)
2710 /* TODO: find a way to jump to other DPMS levels */
2711 if (output->set_dpms)
2712 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002713 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002714 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002715
2716 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002717
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002718 /* Hide all surfaces by removing the fullscreen, panel and
2719 * toplevel layers. This way nothing else can show or receive
2720 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002721
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002722 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002723 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002724 if (shell->showing_input_panels)
2725 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002726 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002727 wl_list_insert(&shell->compositor->cursor_layer.link,
2728 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002729
Pekka Paalanen77346a62011-11-30 16:26:35 +02002730 launch_screensaver(shell);
2731
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002732 /* TODO: disable bindings that should not work while locked. */
2733
2734 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002735}
2736
2737static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002738unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002739{
Tiago Vignattibe143262012-04-16 17:31:41 +03002740 struct desktop_shell *shell =
2741 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002742
Pekka Paalanend81c2162011-11-16 13:47:34 +02002743 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002744 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002745 return;
2746 }
2747
2748 /* If desktop-shell client has gone away, unlock immediately. */
2749 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002750 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002751 return;
2752 }
2753
2754 if (shell->prepare_event_sent)
2755 return;
2756
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002757 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002758 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002759}
2760
2761static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002762show_input_panels(struct wl_listener *listener, void *data)
2763{
2764 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002765 container_of(listener, struct desktop_shell,
2766 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002767 struct input_panel_surface *surface, *next;
2768 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002769
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002770 shell->showing_input_panels = true;
2771
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002772 if (!shell->locked)
2773 wl_list_insert(&shell->panel_layer.link,
2774 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002775
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002776 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002777 &shell->input_panel.surfaces, link) {
2778 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002779 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002780 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002781 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002782 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002783 weston_surface_damage(ws);
2784 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002785 }
2786}
2787
2788static void
2789hide_input_panels(struct wl_listener *listener, void *data)
2790{
2791 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002792 container_of(listener, struct desktop_shell,
2793 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002794 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002795
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002796 shell->showing_input_panels = false;
2797
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002798 if (!shell->locked)
2799 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002800
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002801 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002802 &shell->input_panel_layer.surface_list, layer_link)
2803 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002804}
2805
2806static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002807center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002808{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002809 int32_t width = weston_surface_buffer_width(surface);
2810 int32_t height = weston_surface_buffer_height(surface);
2811 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002812
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002813 x = output->x + (output->width - width) / 2;
2814 y = output->y + (output->height - height) / 2;
2815
2816 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002817}
2818
2819static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002820weston_surface_set_initial_position (struct weston_surface *surface,
2821 struct desktop_shell *shell)
2822{
2823 struct weston_compositor *compositor = shell->compositor;
2824 int ix = 0, iy = 0;
2825 int range_x, range_y;
2826 int dx, dy, x, y, panel_height;
2827 struct weston_output *output, *target_output = NULL;
2828 struct weston_seat *seat;
2829
2830 /* As a heuristic place the new window on the same output as the
2831 * pointer. Falling back to the output containing 0, 0.
2832 *
2833 * TODO: Do something clever for touch too?
2834 */
2835 wl_list_for_each(seat, &compositor->seat_list, link) {
2836 if (seat->has_pointer) {
2837 ix = wl_fixed_to_int(seat->pointer.x);
2838 iy = wl_fixed_to_int(seat->pointer.y);
2839 break;
2840 }
2841 }
2842
2843 wl_list_for_each(output, &compositor->output_list, link) {
2844 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2845 target_output = output;
2846 break;
2847 }
2848 }
2849
2850 if (!target_output) {
2851 weston_surface_set_position(surface, 10 + random() % 400,
2852 10 + random() % 400);
2853 return;
2854 }
2855
2856 /* Valid range within output where the surface will still be onscreen.
2857 * If this is negative it means that the surface is bigger than
2858 * output.
2859 */
2860 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002861 range_x = target_output->width - surface->geometry.width;
2862 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002863 surface->geometry.height;
2864
Rob Bradford4cb88c72012-08-13 15:18:44 +01002865 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002866 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002867 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002868 dx = 0;
2869
2870 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002871 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002872 else
2873 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002874
2875 x = target_output->x + dx;
2876 y = target_output->y + dy;
2877
2878 weston_surface_set_position (surface, x, y);
2879}
2880
2881static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002882map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002883 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002884{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002885 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002886 struct shell_surface *shsurf = get_shell_surface(surface);
2887 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002888 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002889 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002890 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002891 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002892
Pekka Paalanen60921e52012-01-25 15:55:43 +02002893 surface->geometry.width = width;
2894 surface->geometry.height = height;
2895 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002896
2897 /* initial positioning, see also configure() */
2898 switch (surface_type) {
2899 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002900 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002901 break;
Alex Wu4539b082012-03-01 12:57:46 +08002902 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002903 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002904 shell_map_fullscreen(shsurf);
2905 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002906 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002907 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002908 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002909 weston_surface_set_position(surface, shsurf->output->x,
2910 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002911 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002912 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002913 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00002914 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002915 case SHELL_SURFACE_NONE:
2916 weston_surface_set_position(surface,
2917 surface->geometry.x + sx,
2918 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002919 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002920 default:
2921 ;
2922 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002923
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002924 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002925 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002926 case SHELL_SURFACE_POPUP:
2927 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002928 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002929 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2930 break;
Alex Wu4539b082012-03-01 12:57:46 +08002931 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002932 case SHELL_SURFACE_NONE:
2933 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002934 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002935 ws = get_current_workspace(shell);
2936 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002937 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002938 }
2939
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002940 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002941 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002942 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2943 surface->output = shsurf->output;
2944 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002945
Juan Zhao7bb92f02011-12-15 11:31:51 -05002946 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002947 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002948 if (shsurf->transient.flags ==
2949 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2950 break;
2951 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002952 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002953 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002954 if (!shell->locked) {
2955 wl_list_for_each(seat, &compositor->seat_list, link)
2956 activate(shell, surface, seat);
2957 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002958 break;
2959 default:
2960 break;
2961 }
2962
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002963 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002964 {
2965 switch (shell->win_animation_type) {
2966 case ANIMATION_FADE:
2967 weston_fade_run(surface, NULL, NULL);
2968 break;
2969 case ANIMATION_ZOOM:
2970 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2971 break;
2972 default:
2973 break;
2974 }
2975 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002976}
2977
2978static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002979configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002980 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002981{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002982 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2983 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002984
Pekka Paalanen77346a62011-11-30 16:26:35 +02002985 shsurf = get_shell_surface(surface);
2986 if (shsurf)
2987 surface_type = shsurf->type;
2988
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002989 surface->geometry.x = x;
2990 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002991 surface->geometry.width = width;
2992 surface->geometry.height = height;
2993 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002994
2995 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002996 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002997 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002998 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002999 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003000 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003001 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003002 surface->geometry.x = surface->output->x;
3003 surface->geometry.y = surface->output->y +
3004 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003005 break;
Alex Wu4539b082012-03-01 12:57:46 +08003006 case SHELL_SURFACE_TOPLEVEL:
3007 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003008 default:
3009 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003010 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003011
Alex Wu4539b082012-03-01 12:57:46 +08003012 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003013 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003014 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003015
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003016 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003017 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003018 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003019}
3020
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003021static void
3022shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3023{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003024 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003025 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003026 int32_t width = weston_surface_buffer_width(es);
3027 int32_t height = weston_surface_buffer_height(es);
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003028 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003029
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003030 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003031 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003032 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003033 type_changed = 1;
3034 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003035
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003036 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003037 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003038 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003039 es->geometry.width != width ||
3040 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003041 float from_x, from_y;
3042 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003043
3044 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3045 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3046 configure(shell, es,
3047 es->geometry.x + to_x - from_x,
3048 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003049 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003050 }
3051}
3052
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003053static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003054
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003055static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003056desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003057{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003058 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003059 struct desktop_shell *shell =
3060 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003061
3062 shell->child.process.pid = 0;
3063 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003064
3065 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3066 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003067 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003068 shell->child.deathstamp = time;
3069 shell->child.deathcount = 0;
3070 }
3071
3072 shell->child.deathcount++;
3073 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003074 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003075 return;
3076 }
3077
Martin Minarik6d118362012-06-07 18:01:59 +02003078 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003079 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003080}
3081
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003082static void
3083launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003084{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003085 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003086 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003087
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003088 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003089 &shell->child.process,
3090 shell_exe,
3091 desktop_shell_sigchld);
3092
3093 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003094 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003095}
3096
3097static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003098bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3099{
Tiago Vignattibe143262012-04-16 17:31:41 +03003100 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003101
3102 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003103 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003104}
3105
Kristian Høgsberg75840622011-09-06 13:48:16 -04003106static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003107unbind_desktop_shell(struct wl_resource *resource)
3108{
Tiago Vignattibe143262012-04-16 17:31:41 +03003109 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003110
3111 if (shell->locked)
3112 resume_desktop(shell);
3113
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003114 shell->child.desktop_shell = NULL;
3115 shell->prepare_event_sent = false;
3116 free(resource);
3117}
3118
3119static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003120bind_desktop_shell(struct wl_client *client,
3121 void *data, uint32_t version, uint32_t id)
3122{
Tiago Vignattibe143262012-04-16 17:31:41 +03003123 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003124 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003125
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003126 resource = wl_client_add_object(client, &desktop_shell_interface,
3127 &desktop_shell_implementation,
3128 id, shell);
3129
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003130 if (client == shell->child.client) {
3131 resource->destroy = unbind_desktop_shell;
3132 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003133 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003134 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003135
3136 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3137 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003138 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003139}
3140
Pekka Paalanen6e168112011-11-24 11:34:05 +02003141static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003142screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3143{
3144 struct desktop_shell *shell = surface->private;
3145
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003146 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003147 if (!shell->locked)
3148 return;
3149
3150 center_on_output(surface, surface->output);
3151
3152 if (wl_list_empty(&surface->layer_link)) {
3153 wl_list_insert(shell->lock_layer.surface_list.prev,
3154 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003155 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003156 shell->compositor->idle_time = shell->screensaver.duration;
3157 weston_compositor_wake(shell->compositor);
3158 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3159 }
3160}
3161
3162static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003163screensaver_set_surface(struct wl_client *client,
3164 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003165 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003166 struct wl_resource *output_resource)
3167{
Tiago Vignattibe143262012-04-16 17:31:41 +03003168 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003169 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003170 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003171
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003172 surface->configure = screensaver_configure;
3173 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003174 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003175}
3176
3177static const struct screensaver_interface screensaver_implementation = {
3178 screensaver_set_surface
3179};
3180
3181static void
3182unbind_screensaver(struct wl_resource *resource)
3183{
Tiago Vignattibe143262012-04-16 17:31:41 +03003184 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003185
Pekka Paalanen77346a62011-11-30 16:26:35 +02003186 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003187 free(resource);
3188}
3189
3190static void
3191bind_screensaver(struct wl_client *client,
3192 void *data, uint32_t version, uint32_t id)
3193{
Tiago Vignattibe143262012-04-16 17:31:41 +03003194 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003195 struct wl_resource *resource;
3196
3197 resource = wl_client_add_object(client, &screensaver_interface,
3198 &screensaver_implementation,
3199 id, shell);
3200
Pekka Paalanen77346a62011-11-30 16:26:35 +02003201 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003202 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003203 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003204 return;
3205 }
3206
3207 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3208 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003209 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003210}
3211
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003212static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003213input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3214{
3215 struct weston_mode *mode = surface->output->current;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003216 int32_t width = weston_surface_buffer_width(surface);
3217 int32_t height = weston_surface_buffer_height(surface);
3218 float x = (mode->width - width) / 2;
3219 float y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003220
3221 /* Don't map the input panel here, wait for
3222 * show_input_panels signal. */
3223
3224 weston_surface_configure(surface,
3225 surface->output->x + x,
3226 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003227 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003228}
3229
3230static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003231destroy_input_panel_surface(struct wl_listener *listener,
3232 void *data)
3233{
3234 struct input_panel_surface *input_panel_surface =
3235 container_of(listener, struct input_panel_surface, listener);
3236
3237 wl_list_remove(&listener->link);
3238 wl_list_remove(&input_panel_surface->link);
3239
3240 free(input_panel_surface);
3241}
3242
3243static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003244input_panel_set_surface(struct wl_client *client,
3245 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003246 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003247 struct wl_resource *output_resource)
3248{
3249 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003250 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003251 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003252 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003253
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003254 surface->configure = input_panel_configure;
3255 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003256 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003257
3258 input_panel_surface = malloc(sizeof *input_panel_surface);
3259 if (!input_panel_surface) {
3260 wl_resource_post_no_memory(resource);
3261 return;
3262 }
3263
3264 input_panel_surface->surface = surface;
3265 input_panel_surface->listener.notify = destroy_input_panel_surface;
3266
3267 wl_signal_add(&surface_resource->destroy_signal,
3268 &input_panel_surface->listener);
3269
3270 wl_list_insert(&shell->input_panel.surfaces,
3271 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003272}
3273
3274static const struct input_panel_interface input_panel_implementation = {
3275 input_panel_set_surface
3276};
3277
3278static void
3279unbind_input_panel(struct wl_resource *resource)
3280{
3281 struct desktop_shell *shell = resource->data;
3282
3283 shell->input_panel.binding = NULL;
3284 free(resource);
3285}
3286
3287static void
3288bind_input_panel(struct wl_client *client,
3289 void *data, uint32_t version, uint32_t id)
3290{
3291 struct desktop_shell *shell = data;
3292 struct wl_resource *resource;
3293
3294 resource = wl_client_add_object(client, &input_panel_interface,
3295 &input_panel_implementation,
3296 id, shell);
3297
3298 if (shell->input_panel.binding == NULL) {
3299 resource->destroy = unbind_input_panel;
3300 shell->input_panel.binding = resource;
3301 return;
3302 }
3303
3304 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3305 "interface object already bound");
3306 wl_resource_destroy(resource);
3307}
3308
Kristian Høgsberg07045392012-02-19 18:52:44 -05003309struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003310 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003311 struct weston_surface *current;
3312 struct wl_listener listener;
3313 struct wl_keyboard_grab grab;
3314};
3315
3316static void
3317switcher_next(struct switcher *switcher)
3318{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003319 struct weston_surface *surface;
3320 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003321 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003322 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003323
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003324 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003325 switch (get_shell_surface_type(surface)) {
3326 case SHELL_SURFACE_TOPLEVEL:
3327 case SHELL_SURFACE_FULLSCREEN:
3328 case SHELL_SURFACE_MAXIMIZED:
3329 if (first == NULL)
3330 first = surface;
3331 if (prev == switcher->current)
3332 next = surface;
3333 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003334 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003335 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003336 weston_surface_damage(surface);
3337 break;
3338 default:
3339 break;
3340 }
Alex Wu1659daa2012-04-01 20:13:09 +08003341
3342 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003343 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003344 surface->geometry.dirty = 1;
3345 weston_surface_damage(surface);
3346 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003347 }
3348
3349 if (next == NULL)
3350 next = first;
3351
Alex Wu07b26062012-03-12 16:06:01 +08003352 if (next == NULL)
3353 return;
3354
Kristian Høgsberg07045392012-02-19 18:52:44 -05003355 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003356 wl_signal_add(&next->surface.resource.destroy_signal,
3357 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003358
3359 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003360 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003361
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003362 shsurf = get_shell_surface(switcher->current);
3363 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003364 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003365}
3366
3367static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003368switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003369{
3370 struct switcher *switcher =
3371 container_of(listener, struct switcher, listener);
3372
3373 switcher_next(switcher);
3374}
3375
3376static void
Daniel Stone351eb612012-05-31 15:27:47 -04003377switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003378{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003379 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003380 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003381 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003382
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003383 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003384 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003385 weston_surface_damage(surface);
3386 }
3387
Alex Wu07b26062012-03-12 16:06:01 +08003388 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003389 activate(switcher->shell, switcher->current,
3390 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003391 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003392 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003393 free(switcher);
3394}
3395
3396static void
3397switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003398 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003399{
3400 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003401 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003402
Daniel Stonec9785ea2012-05-30 16:31:52 +01003403 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003404 switcher_next(switcher);
3405}
3406
3407static void
3408switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3409 uint32_t mods_depressed, uint32_t mods_latched,
3410 uint32_t mods_locked, uint32_t group)
3411{
3412 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003413 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003414
Daniel Stone351eb612012-05-31 15:27:47 -04003415 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3416 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003417}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003418
3419static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003420 switcher_key,
3421 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003422};
3423
3424static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003425switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3426 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003427{
Tiago Vignattibe143262012-04-16 17:31:41 +03003428 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003429 struct switcher *switcher;
3430
3431 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003432 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003433 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003434 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003435 wl_list_init(&switcher->listener.link);
3436
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003437 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003438 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003439 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3440 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003441 switcher_next(switcher);
3442}
3443
Pekka Paalanen3c647232011-12-22 13:43:43 +02003444static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003445backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3446 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003447{
3448 struct weston_compositor *compositor = data;
3449 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003450 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003451
3452 /* TODO: we're limiting to simple use cases, where we assume just
3453 * control on the primary display. We'd have to extend later if we
3454 * ever get support for setting backlights on random desktop LCD
3455 * panels though */
3456 output = get_default_output(compositor);
3457 if (!output)
3458 return;
3459
3460 if (!output->set_backlight)
3461 return;
3462
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003463 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3464 backlight_new = output->backlight_current - 25;
3465 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3466 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003467
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003468 if (backlight_new < 5)
3469 backlight_new = 5;
3470 if (backlight_new > 255)
3471 backlight_new = 255;
3472
3473 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003474 output->set_backlight(output, output->backlight_current);
3475}
3476
3477static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003478fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3479 void *data)
3480{
3481 struct desktop_shell *shell = data;
3482 struct weston_compositor *compositor = shell->compositor;
3483 compositor->fan_debug = !compositor->fan_debug;
3484 weston_compositor_damage_all(compositor);
3485}
3486
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003487struct debug_binding_grab {
3488 struct wl_keyboard_grab grab;
3489 struct weston_seat *seat;
3490 uint32_t key[2];
3491 int key_released[2];
3492};
3493
3494static void
3495debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3496 uint32_t key, uint32_t state)
3497{
3498 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3499 struct wl_resource *resource;
3500 struct wl_display *display;
3501 uint32_t serial;
3502 int send = 0, terminate = 0;
3503 int check_binding = 1;
3504 int i;
3505
3506 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3507 /* Do not run bindings on key releases */
3508 check_binding = 0;
3509
3510 for (i = 0; i < 2; i++)
3511 if (key == db->key[i])
3512 db->key_released[i] = 1;
3513
3514 if (db->key_released[0] && db->key_released[1]) {
3515 /* All key releases been swalled so end the grab */
3516 terminate = 1;
3517 } else if (key != db->key[0] && key != db->key[1]) {
3518 /* Should not swallow release of other keys */
3519 send = 1;
3520 }
3521 } else if (key == db->key[0] && !db->key_released[0]) {
3522 /* Do not check bindings for the first press of the binding
3523 * key. This allows it to be used as a debug shortcut.
3524 * We still need to swallow this event. */
3525 check_binding = 0;
3526 } else if (db->key[1]) {
3527 /* If we already ran a binding don't process another one since
3528 * we can't keep track of all the binding keys that were
3529 * pressed in order to swallow the release events. */
3530 send = 1;
3531 check_binding = 0;
3532 }
3533
3534 if (check_binding) {
3535 struct weston_compositor *ec = db->seat->compositor;
3536
3537 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3538 key, state)) {
3539 /* We ran a binding so swallow the press and keep the
3540 * grab to swallow the released too. */
3541 send = 0;
3542 terminate = 0;
3543 db->key[1] = key;
3544 } else {
3545 /* Terminate the grab since the key pressed is not a
3546 * debug binding key. */
3547 send = 1;
3548 terminate = 1;
3549 }
3550 }
3551
3552 if (send) {
3553 resource = grab->keyboard->focus_resource;
3554
3555 if (resource) {
3556 display = wl_client_get_display(resource->client);
3557 serial = wl_display_next_serial(display);
3558 wl_keyboard_send_key(resource, serial, time, key, state);
3559 }
3560 }
3561
3562 if (terminate) {
3563 wl_keyboard_end_grab(grab->keyboard);
3564 free(db);
3565 }
3566}
3567
3568static void
3569debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3570 uint32_t mods_depressed, uint32_t mods_latched,
3571 uint32_t mods_locked, uint32_t group)
3572{
3573 struct wl_resource *resource;
3574
3575 resource = grab->keyboard->focus_resource;
3576 if (!resource)
3577 return;
3578
3579 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3580 mods_latched, mods_locked, group);
3581}
3582
3583struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3584 debug_binding_key,
3585 debug_binding_modifiers
3586};
3587
3588static void
3589debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3590{
3591 struct debug_binding_grab *grab;
3592
3593 grab = calloc(1, sizeof *grab);
3594 if (!grab)
3595 return;
3596
3597 grab->seat = (struct weston_seat *) seat;
3598 grab->key[0] = key;
3599 grab->grab.interface = &debug_binding_keyboard_grab;
3600 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3601}
3602
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003603static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003604force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3605 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003606{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003607 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003608 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003609 struct desktop_shell *shell = data;
3610 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003611 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003612
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003613 focus_surface = seat->keyboard->focus;
3614 if (!focus_surface)
3615 return;
3616
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003617 wl_signal_emit(&compositor->kill_signal, focus_surface);
3618
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003619 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003620 wl_client_get_credentials(client, &pid, NULL, NULL);
3621
3622 /* Skip clients that we launched ourselves (the credentials of
3623 * the socketpair is ours) */
3624 if (pid == getpid())
3625 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003626
Daniel Stone325fc2d2012-05-30 16:31:58 +01003627 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003628}
3629
3630static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003631workspace_up_binding(struct wl_seat *seat, uint32_t time,
3632 uint32_t key, void *data)
3633{
3634 struct desktop_shell *shell = data;
3635 unsigned int new_index = shell->workspaces.current;
3636
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003637 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003638 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003639 if (new_index != 0)
3640 new_index--;
3641
3642 change_workspace(shell, new_index);
3643}
3644
3645static void
3646workspace_down_binding(struct wl_seat *seat, uint32_t time,
3647 uint32_t key, void *data)
3648{
3649 struct desktop_shell *shell = data;
3650 unsigned int new_index = shell->workspaces.current;
3651
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003652 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003653 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003654 if (new_index < shell->workspaces.num - 1)
3655 new_index++;
3656
3657 change_workspace(shell, new_index);
3658}
3659
3660static void
3661workspace_f_binding(struct wl_seat *seat, uint32_t time,
3662 uint32_t key, void *data)
3663{
3664 struct desktop_shell *shell = data;
3665 unsigned int new_index;
3666
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003667 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003668 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003669 new_index = key - KEY_F1;
3670 if (new_index >= shell->workspaces.num)
3671 new_index = shell->workspaces.num - 1;
3672
3673 change_workspace(shell, new_index);
3674}
3675
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003676static void
3677workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3678 uint32_t key, void *data)
3679{
3680 struct desktop_shell *shell = data;
3681 unsigned int new_index = shell->workspaces.current;
3682
3683 if (shell->locked)
3684 return;
3685
3686 if (new_index != 0)
3687 new_index--;
3688
3689 take_surface_to_workspace_by_seat(shell, seat, new_index);
3690}
3691
3692static void
3693workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3694 uint32_t key, void *data)
3695{
3696 struct desktop_shell *shell = data;
3697 unsigned int new_index = shell->workspaces.current;
3698
3699 if (shell->locked)
3700 return;
3701
3702 if (new_index < shell->workspaces.num - 1)
3703 new_index++;
3704
3705 take_surface_to_workspace_by_seat(shell, seat, new_index);
3706}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003707
3708static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003709shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003710{
Tiago Vignattibe143262012-04-16 17:31:41 +03003711 struct desktop_shell *shell =
3712 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003713 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003714
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003715 if (shell->child.client)
3716 wl_client_destroy(shell->child.client);
3717
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003718 wl_list_remove(&shell->lock_listener.link);
3719 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003720 wl_list_remove(&shell->show_input_panel_listener.link);
3721 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003722
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003723 wl_array_for_each(ws, &shell->workspaces.array)
3724 workspace_destroy(*ws);
3725 wl_array_release(&shell->workspaces.array);
3726
Pekka Paalanen3c647232011-12-22 13:43:43 +02003727 free(shell->screensaver.path);
3728 free(shell);
3729}
3730
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003731static void
3732shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3733{
3734 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003735 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003736
3737 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003738 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3739 MODIFIER_CTRL | MODIFIER_ALT,
3740 terminate_binding, ec);
3741 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3742 click_to_activate_binding,
3743 shell);
3744 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3745 MODIFIER_SUPER | MODIFIER_ALT,
3746 surface_opacity_binding, NULL);
3747 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3748 MODIFIER_SUPER, zoom_axis_binding,
3749 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003750
3751 /* configurable bindings */
3752 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003753 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3754 zoom_key_binding, NULL);
3755 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3756 zoom_key_binding, NULL);
3757 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3758 shell);
3759 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3760 resize_binding, shell);
3761 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3762 rotate_binding, NULL);
3763 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3764 shell);
3765 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3766 ec);
3767 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3768 backlight_binding, ec);
3769 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3770 ec);
3771 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3772 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003773 weston_compositor_add_key_binding(ec, KEY_K, mod,
3774 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003775 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3776 workspace_up_binding, shell);
3777 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3778 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003779 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3780 workspace_move_surface_up_binding,
3781 shell);
3782 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3783 workspace_move_surface_down_binding,
3784 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003785
3786 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3787 if (shell->workspaces.num > 1) {
3788 num_workspace_bindings = shell->workspaces.num;
3789 if (num_workspace_bindings > 6)
3790 num_workspace_bindings = 6;
3791 for (i = 0; i < num_workspace_bindings; i++)
3792 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3793 workspace_f_binding,
3794 shell);
3795 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003796
3797 /* Debug bindings */
3798 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3799 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003800 weston_compositor_add_debug_binding(ec, KEY_F,
3801 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003802}
3803
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003804WL_EXPORT int
Kristian Høgsbergb00a9d32012-09-11 14:06:27 -04003805module_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003806{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003807 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003808 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003809 struct workspace **pws;
3810 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003811 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003812
3813 shell = malloc(sizeof *shell);
3814 if (shell == NULL)
3815 return -1;
3816
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003817 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003818 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003819
3820 shell->destroy_listener.notify = shell_destroy;
3821 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3822 shell->lock_listener.notify = lock;
3823 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3824 shell->unlock_listener.notify = unlock;
3825 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003826 shell->show_input_panel_listener.notify = show_input_panels;
3827 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3828 shell->hide_input_panel_listener.notify = hide_input_panels;
3829 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003830 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003831 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003832 ec->shell_interface.create_shell_surface = create_shell_surface;
3833 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003834 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003835 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003836 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003837
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003838 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003839
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003840 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3841 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003842 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3843 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003844 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003845
3846 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02003847 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003848
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003849 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003850
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003851 for (i = 0; i < shell->workspaces.num; i++) {
3852 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3853 if (pws == NULL)
3854 return -1;
3855
3856 *pws = workspace_create();
3857 if (*pws == NULL)
3858 return -1;
3859 }
3860 activate_workspace(shell, 0);
3861
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003862 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003863 wl_list_init(&shell->workspaces.animation.link);
3864 shell->workspaces.animation.frame = animate_workspace_change_frame;
3865
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003866 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3867 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003868 return -1;
3869
Kristian Høgsberg75840622011-09-06 13:48:16 -04003870 if (wl_display_add_global(ec->wl_display,
3871 &desktop_shell_interface,
3872 shell, bind_desktop_shell) == NULL)
3873 return -1;
3874
Pekka Paalanen6e168112011-11-24 11:34:05 +02003875 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3876 shell, bind_screensaver) == NULL)
3877 return -1;
3878
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003879 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3880 shell, bind_input_panel) == NULL)
3881 return -1;
3882
Jonas Ådahle9d22502012-08-29 22:13:01 +02003883 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3884 shell, bind_workspace_manager) == NULL)
3885 return -1;
3886
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003887 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003888
3889 loop = wl_display_get_event_loop(ec->wl_display);
3890 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003891
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003892 wl_list_for_each(seat, &ec->seat_list, link)
3893 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003894
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003895 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003896
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003897 return 0;
3898}