blob: 72d2a73f2ef35f37f4e31741d884e15eff93459c [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
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001757set_fullscreen(struct shell_surface *shsurf,
1758 uint32_t method,
1759 uint32_t framerate,
1760 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001761{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001762 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001763
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001764 if (output)
1765 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001766 else if (es->output)
1767 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001768 else
1769 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001770
Alex Wu4539b082012-03-01 12:57:46 +08001771 shsurf->fullscreen_output = shsurf->output;
1772 shsurf->fullscreen.type = method;
1773 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001774 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001775
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001776 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001777 shsurf->output->width,
1778 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001779}
1780
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001781static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001782shell_surface_set_fullscreen(struct wl_client *client,
1783 struct wl_resource *resource,
1784 uint32_t method,
1785 uint32_t framerate,
1786 struct wl_resource *output_resource)
1787{
1788 struct shell_surface *shsurf = resource->data;
1789 struct weston_output *output;
1790
1791 if (output_resource)
1792 output = output_resource->data;
1793 else
1794 output = NULL;
1795
1796 set_fullscreen(shsurf, method, framerate, output);
1797}
1798
1799static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001800popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001801 struct wl_surface *surface,
1802 wl_fixed_t x,
1803 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001804{
Daniel Stone37816df2012-05-16 18:45:18 +01001805 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001806 struct shell_surface *priv =
1807 container_of(grab, struct shell_surface, popup.grab);
1808 struct wl_client *client = priv->surface->surface.resource.client;
1809
Pekka Paalanencb108432012-01-19 16:25:40 +02001810 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001811 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001812 grab->focus = surface;
1813 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001814 wl_pointer_set_focus(pointer, NULL,
1815 wl_fixed_from_int(0),
1816 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001817 grab->focus = NULL;
1818 }
1819}
1820
1821static void
Scott Moreau447013d2012-02-18 05:05:29 -07001822popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001823 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001824{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001825 struct wl_resource *resource;
1826
Daniel Stone37816df2012-05-16 18:45:18 +01001827 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001828 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001829 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001830}
1831
1832static void
Scott Moreau447013d2012-02-18 05:05:29 -07001833popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001834 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001835{
1836 struct wl_resource *resource;
1837 struct shell_surface *shsurf =
1838 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001839 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001840 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001841 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001842
Daniel Stone37816df2012-05-16 18:45:18 +01001843 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001844 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001845 display = wl_client_get_display(resource->client);
1846 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001847 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001848 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001849 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001850 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001851 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001852 }
1853
Daniel Stone4dbadb12012-05-30 16:31:51 +01001854 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001855 shsurf->popup.initial_up = 1;
1856}
1857
Scott Moreau447013d2012-02-18 05:05:29 -07001858static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001859 popup_grab_focus,
1860 popup_grab_motion,
1861 popup_grab_button,
1862};
1863
1864static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001865popup_grab_end(struct wl_pointer *pointer)
1866{
1867 struct wl_pointer_grab *grab = pointer->grab;
1868 struct shell_surface *shsurf =
1869 container_of(grab, struct shell_surface, popup.grab);
1870
1871 if (pointer->grab->interface == &popup_grab_interface) {
1872 wl_shell_surface_send_popup_done(&shsurf->resource);
1873 wl_pointer_end_grab(grab->pointer);
1874 shsurf->popup.grab.pointer = NULL;
1875 }
1876}
1877
1878static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001879shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001880{
Daniel Stone37816df2012-05-16 18:45:18 +01001881 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001882 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001883 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001884
1885 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001886 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001887
Pekka Paalanen938269a2012-02-07 14:19:01 +02001888 weston_surface_update_transform(parent);
1889 if (parent->transform.enabled) {
1890 shsurf->popup.parent_transform.matrix =
1891 parent->transform.matrix;
1892 } else {
1893 /* construct x, y translation matrix */
1894 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03001895 shsurf->popup.parent_transform.matrix.type =
1896 WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen938269a2012-02-07 14:19:01 +02001897 shsurf->popup.parent_transform.matrix.d[12] =
1898 parent->geometry.x;
1899 shsurf->popup.parent_transform.matrix.d[13] =
1900 parent->geometry.y;
1901 }
1902 wl_list_insert(es->geometry.transformation_list.prev,
1903 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001904
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001905 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001906 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1907 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001908
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001909 /* We don't require the grab to still be active, but if another
1910 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001911 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1912 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001913 } else {
1914 wl_shell_surface_send_popup_done(&shsurf->resource);
1915 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001916}
1917
1918static void
1919shell_surface_set_popup(struct wl_client *client,
1920 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001921 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001922 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001923 struct wl_resource *parent_resource,
1924 int32_t x, int32_t y, uint32_t flags)
1925{
1926 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001927
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001928 shsurf->type = SHELL_SURFACE_POPUP;
1929 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001930 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001931 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001932 shsurf->popup.x = x;
1933 shsurf->popup.y = y;
1934}
1935
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001936static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001937 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001938 shell_surface_move,
1939 shell_surface_resize,
1940 shell_surface_set_toplevel,
1941 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001942 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001943 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001944 shell_surface_set_maximized,
1945 shell_surface_set_title,
1946 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001947};
1948
1949static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001950destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001951{
Daniel Stone37816df2012-05-16 18:45:18 +01001952 if (shsurf->popup.grab.pointer)
1953 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001954
Alex Wubd3354b2012-04-17 17:20:49 +08001955 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1956 shell_surface_is_top_fullscreen(shsurf)) {
1957 weston_output_switch_mode(shsurf->fullscreen_output,
1958 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001959 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001960
Alex Wuaa08e2d2012-03-05 11:01:40 +08001961 if (shsurf->fullscreen.black_surface)
1962 weston_surface_destroy(shsurf->fullscreen.black_surface);
1963
Alex Wubd3354b2012-04-17 17:20:49 +08001964 /* As destroy_resource() use wl_list_for_each_safe(),
1965 * we can always remove the listener.
1966 */
1967 wl_list_remove(&shsurf->surface_destroy_listener.link);
1968 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001969 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001970
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001971 wl_list_remove(&shsurf->link);
1972 free(shsurf);
1973}
1974
1975static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001976shell_destroy_shell_surface(struct wl_resource *resource)
1977{
1978 struct shell_surface *shsurf = resource->data;
1979
1980 destroy_shell_surface(shsurf);
1981}
1982
1983static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001984shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001985{
1986 struct shell_surface *shsurf = container_of(listener,
1987 struct shell_surface,
1988 surface_destroy_listener);
1989
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001990 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001991 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001992 } else {
1993 wl_signal_emit(&shsurf->resource.destroy_signal,
1994 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001995 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001996 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001997}
1998
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001999static void
2000shell_surface_configure(struct weston_surface *, int32_t, int32_t);
2001
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002002static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002003get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002004{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002005 if (surface->configure == shell_surface_configure)
2006 return surface->private;
2007 else
2008 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002009}
2010
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002011static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002012create_shell_surface(void *shell, struct weston_surface *surface,
2013 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002014{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002015 struct shell_surface *shsurf;
2016
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002017 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002018 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002019 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002020 }
2021
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002022 shsurf = calloc(1, sizeof *shsurf);
2023 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002024 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002025 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002026 }
2027
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002028 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002029 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002030
Tiago Vignattibc052c92012-04-19 16:18:18 +03002031 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002032 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002033 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002034 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002035 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002036 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2037 shsurf->fullscreen.framerate = 0;
2038 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002039 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002040 wl_list_init(&shsurf->fullscreen.transform.link);
2041
Tiago Vignattibc052c92012-04-19 16:18:18 +03002042 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002043 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2044 wl_signal_add(&surface->surface.resource.destroy_signal,
2045 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002046
2047 /* init link so its safe to always remove it in destroy_shell_surface */
2048 wl_list_init(&shsurf->link);
2049
Pekka Paalanen460099f2012-01-20 16:48:25 +02002050 /* empty when not in use */
2051 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002052 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002053
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002054 wl_list_init(&shsurf->workspace_transform.link);
2055
Pekka Paalanen98262232011-12-01 10:42:22 +02002056 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002057 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002058
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002059 shsurf->client = client;
2060
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002061 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002062}
2063
2064static void
2065shell_get_shell_surface(struct wl_client *client,
2066 struct wl_resource *resource,
2067 uint32_t id,
2068 struct wl_resource *surface_resource)
2069{
2070 struct weston_surface *surface = surface_resource->data;
2071 struct desktop_shell *shell = resource->data;
2072 struct shell_surface *shsurf;
2073
2074 if (get_shell_surface(surface)) {
2075 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002076 WL_DISPLAY_ERROR_INVALID_OBJECT,
2077 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002078 return;
2079 }
2080
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002081 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002082 if (!shsurf) {
2083 wl_resource_post_error(surface_resource,
2084 WL_DISPLAY_ERROR_INVALID_OBJECT,
2085 "surface->configure already set");
2086 return;
2087 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002088
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002089 shsurf->resource.destroy = shell_destroy_shell_surface;
2090 shsurf->resource.object.id = id;
2091 shsurf->resource.object.interface = &wl_shell_surface_interface;
2092 shsurf->resource.object.implementation =
2093 (void (**)(void)) &shell_surface_implementation;
2094 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002095
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002096 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002097}
2098
2099static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002100 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002101};
2102
Kristian Høgsberg07937562011-04-12 17:25:42 -04002103static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002104handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002105{
2106 proc->pid = 0;
2107}
2108
2109static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002110launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002111{
2112 if (shell->screensaver.binding)
2113 return;
2114
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002115 if (!shell->screensaver.path)
2116 return;
2117
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002118 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002119 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002120 return;
2121 }
2122
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002123 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002124 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002125 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002126 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002127}
2128
2129static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002130terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002131{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002132 if (shell->screensaver.process.pid == 0)
2133 return;
2134
2135 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002136}
2137
2138static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002139configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002140{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002141 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002142
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002143 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2144 if (s->output == es->output && s != es) {
2145 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002146 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002147 }
2148 }
2149
2150 weston_surface_configure(es, es->output->x, es->output->y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002151 weston_surface_buffer_width(es),
2152 weston_surface_buffer_height(es));
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002153
2154 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002155 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002156 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002157 }
2158}
2159
2160static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002161background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2162{
2163 struct desktop_shell *shell = es->private;
2164
2165 configure_static_surface(es, &shell->background_layer);
2166}
2167
2168static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002169desktop_shell_set_background(struct wl_client *client,
2170 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002171 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002172 struct wl_resource *surface_resource)
2173{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002174 struct desktop_shell *shell = resource->data;
2175 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002176
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002177 if (surface->configure) {
2178 wl_resource_post_error(surface_resource,
2179 WL_DISPLAY_ERROR_INVALID_OBJECT,
2180 "surface role already assigned");
2181 return;
2182 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002183
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002184 surface->configure = background_configure;
2185 surface->private = shell;
2186 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002187 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002188 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002189 surface->output->width,
2190 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002191}
2192
2193static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002194panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2195{
2196 struct desktop_shell *shell = es->private;
2197
2198 configure_static_surface(es, &shell->panel_layer);
2199}
2200
2201static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002202desktop_shell_set_panel(struct wl_client *client,
2203 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002204 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002205 struct wl_resource *surface_resource)
2206{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002207 struct desktop_shell *shell = resource->data;
2208 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002209
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002210 if (surface->configure) {
2211 wl_resource_post_error(surface_resource,
2212 WL_DISPLAY_ERROR_INVALID_OBJECT,
2213 "surface role already assigned");
2214 return;
2215 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002216
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002217 surface->configure = panel_configure;
2218 surface->private = shell;
2219 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002220 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002221 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002222 surface->output->width,
2223 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002224}
2225
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002226static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002227lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2228{
2229 struct desktop_shell *shell = surface->private;
2230
2231 center_on_output(surface, get_default_output(shell->compositor));
2232
2233 if (!weston_surface_is_mapped(surface)) {
2234 wl_list_insert(&shell->lock_layer.surface_list,
2235 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002236 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002237 weston_compositor_wake(shell->compositor);
2238 }
2239}
2240
2241static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002242handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002243{
Tiago Vignattibe143262012-04-16 17:31:41 +03002244 struct desktop_shell *shell =
2245 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002246
Martin Minarik6d118362012-06-07 18:01:59 +02002247 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002248 shell->lock_surface = NULL;
2249}
2250
2251static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002252desktop_shell_set_lock_surface(struct wl_client *client,
2253 struct wl_resource *resource,
2254 struct wl_resource *surface_resource)
2255{
Tiago Vignattibe143262012-04-16 17:31:41 +03002256 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002257 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002258
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002259 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002260
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002261 if (!shell->locked)
2262 return;
2263
Pekka Paalanen98262232011-12-01 10:42:22 +02002264 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002265
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002266 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2267 wl_signal_add(&surface_resource->destroy_signal,
2268 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002269
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002270 surface->configure = lock_surface_configure;
2271 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002272}
2273
2274static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002275resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002276{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002277 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002278
Pekka Paalanen77346a62011-11-30 16:26:35 +02002279 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002280
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002281 wl_list_remove(&shell->lock_layer.link);
2282 wl_list_insert(&shell->compositor->cursor_layer.link,
2283 &shell->fullscreen_layer.link);
2284 wl_list_insert(&shell->fullscreen_layer.link,
2285 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002286 if (shell->showing_input_panels) {
2287 wl_list_insert(&shell->panel_layer.link,
2288 &shell->input_panel_layer.link);
2289 wl_list_insert(&shell->input_panel_layer.link,
2290 &ws->layer.link);
2291 } else {
2292 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2293 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002294
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002295 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002296
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002297 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002298 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002299 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002300 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002301}
2302
2303static void
2304desktop_shell_unlock(struct wl_client *client,
2305 struct wl_resource *resource)
2306{
Tiago Vignattibe143262012-04-16 17:31:41 +03002307 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002308
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002309 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002310
2311 if (shell->locked)
2312 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002313}
2314
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002315static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002316desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002317 struct wl_resource *resource,
2318 struct wl_resource *surface_resource)
2319{
2320 struct desktop_shell *shell = resource->data;
2321
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002322 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002323}
2324
Kristian Høgsberg75840622011-09-06 13:48:16 -04002325static const struct desktop_shell_interface desktop_shell_implementation = {
2326 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002327 desktop_shell_set_panel,
2328 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002329 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002330 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002331};
2332
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002333static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002334get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002335{
2336 struct shell_surface *shsurf;
2337
2338 shsurf = get_shell_surface(surface);
2339 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002340 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002341 return shsurf->type;
2342}
2343
Kristian Høgsberg75840622011-09-06 13:48:16 -04002344static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002345move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002346{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002347 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002348 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002349 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002350
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002351 if (surface == NULL)
2352 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002353
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002354 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002355 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002356 return;
2357
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002358 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002359}
2360
2361static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002362resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002363{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002364 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002365 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002366 uint32_t edges = 0;
2367 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002368 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002369
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002370 if (surface == NULL)
2371 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002372
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002373 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002374 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002375 return;
2376
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002377 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002378 wl_fixed_to_int(seat->pointer->grab_x),
2379 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002380 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002381
Pekka Paalanen60921e52012-01-25 15:55:43 +02002382 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002383 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002384 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002385 edges |= 0;
2386 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002387 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002388
Pekka Paalanen60921e52012-01-25 15:55:43 +02002389 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002390 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002391 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002392 edges |= 0;
2393 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002394 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002395
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002396 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002397}
2398
2399static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002400surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002401 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002402{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002403 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002404 struct shell_surface *shsurf;
2405 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002406 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002407
2408 if (surface == NULL)
2409 return;
2410
2411 shsurf = get_shell_surface(surface);
2412 if (!shsurf)
2413 return;
2414
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002415 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002416
Scott Moreau02709af2012-05-22 01:54:10 -06002417 if (surface->alpha > 1.0)
2418 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002419 if (surface->alpha < step)
2420 surface->alpha = step;
2421
2422 surface->geometry.dirty = 1;
2423 weston_surface_damage(surface);
2424}
2425
2426static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002427do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002428 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002429{
Daniel Stone37816df2012-05-16 18:45:18 +01002430 struct weston_seat *ws = (struct weston_seat *) seat;
2431 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002432 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002433 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002434
2435 wl_list_for_each(output, &compositor->output_list, link) {
2436 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002437 wl_fixed_to_double(seat->pointer->x),
2438 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002439 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002440 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002441 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002442 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002443 increment = -output->zoom.increment;
2444 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002445 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002446 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002447 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002448 else
2449 increment = 0;
2450
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002451 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002452
Scott Moreaue6603982012-06-11 13:07:51 -06002453 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002454 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002455 else if (output->zoom.level > output->zoom.max_level)
2456 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002457 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002458 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002459 output->disable_planes++;
2460 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002461
Scott Moreaue6603982012-06-11 13:07:51 -06002462 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002463
Scott Moreau8dacaab2012-06-17 18:10:58 -06002464 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002465 }
2466 }
2467}
2468
Scott Moreauccbf29d2012-02-22 14:21:41 -07002469static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002470zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002471 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002472{
2473 do_zoom(seat, time, 0, axis, value);
2474}
2475
2476static void
2477zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2478 void *data)
2479{
2480 do_zoom(seat, time, key, 0, 0);
2481}
2482
2483static void
2484terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2485 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002486{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002487 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002488
Daniel Stone325fc2d2012-05-30 16:31:58 +01002489 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002490}
2491
2492static void
Scott Moreau447013d2012-02-18 05:05:29 -07002493rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002494 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002495{
2496 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002497 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002498 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002499 struct shell_surface *shsurf = rotate->base.shsurf;
2500 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002501 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002502
2503 if (!shsurf)
2504 return;
2505
2506 surface = shsurf->surface;
2507
2508 cx = 0.5f * surface->geometry.width;
2509 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002510
Daniel Stone37816df2012-05-16 18:45:18 +01002511 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2512 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002513 r = sqrtf(dx * dx + dy * dy);
2514
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002515 wl_list_remove(&shsurf->rotation.transform.link);
2516 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002517
2518 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002519 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002520 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002521
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002522 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002523 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002524
2525 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002526 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002527 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002528 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002529 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002530
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002531 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002532 &shsurf->surface->geometry.transformation_list,
2533 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002534 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002535 wl_list_init(&shsurf->rotation.transform.link);
2536 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002537 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002538 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002539
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002540 /* We need to adjust the position of the surface
2541 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002542 cposx = surface->geometry.x + cx;
2543 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002544 dposx = rotate->center.x - cposx;
2545 dposy = rotate->center.y - cposy;
2546 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002547 weston_surface_set_position(surface,
2548 surface->geometry.x + dposx,
2549 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002550 }
2551
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002552 /* Repaint implies weston_surface_update_transform(), which
2553 * lazily applies the damage due to rotation update.
2554 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002555 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002556}
2557
2558static void
Scott Moreau447013d2012-02-18 05:05:29 -07002559rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002560 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002561{
2562 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002563 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002564 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002565 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002566 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002567
Daniel Stone4dbadb12012-05-30 16:31:51 +01002568 if (pointer->button_count == 0 &&
2569 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002570 if (shsurf)
2571 weston_matrix_multiply(&shsurf->rotation.rotation,
2572 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002573 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002574 free(rotate);
2575 }
2576}
2577
Scott Moreau447013d2012-02-18 05:05:29 -07002578static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002579 noop_grab_focus,
2580 rotate_grab_motion,
2581 rotate_grab_button,
2582};
2583
2584static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002585rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002586 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002587{
2588 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002589 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002590 struct shell_surface *surface;
2591 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002592 float dx, dy;
2593 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002594
2595 if (base_surface == NULL)
2596 return;
2597
2598 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002599 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002600 return;
2601
Pekka Paalanen460099f2012-01-20 16:48:25 +02002602 rotate = malloc(sizeof *rotate);
2603 if (!rotate)
2604 return;
2605
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002606 weston_surface_to_global_float(surface->surface,
2607 surface->surface->geometry.width / 2,
2608 surface->surface->geometry.height / 2,
2609 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002610
Daniel Stone37816df2012-05-16 18:45:18 +01002611 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2612 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002613 r = sqrtf(dx * dx + dy * dy);
2614 if (r > 20.0f) {
2615 struct weston_matrix inverse;
2616
2617 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002618 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002619 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002620
2621 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002622 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002623 } else {
2624 weston_matrix_init(&surface->rotation.rotation);
2625 weston_matrix_init(&rotate->rotation);
2626 }
2627
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002628 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2629 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002630}
2631
2632static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002633lower_fullscreen_layer(struct desktop_shell *shell)
2634{
2635 struct workspace *ws;
2636 struct weston_surface *surface, *prev;
2637
2638 ws = get_current_workspace(shell);
2639 wl_list_for_each_reverse_safe(surface, prev,
2640 &shell->fullscreen_layer.surface_list,
2641 layer_link)
2642 weston_surface_restack(surface, &ws->layer.surface_list);
2643}
2644
2645static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002646activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002647 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002648{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002649 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002650 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002651
Daniel Stone37816df2012-05-16 18:45:18 +01002652 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002653
Jonas Ådahl8538b222012-08-29 22:13:03 +02002654 state = ensure_focus_state(shell, seat);
2655 if (state == NULL)
2656 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002657
2658 state->keyboard_focus = es;
2659 wl_list_remove(&state->surface_destroy_listener.link);
2660 wl_signal_add(&es->surface.resource.destroy_signal,
2661 &state->surface_destroy_listener);
2662
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002663 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002664 case SHELL_SURFACE_FULLSCREEN:
2665 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002666 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002667 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002668 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002669 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002670 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002671 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002672 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002673 }
2674}
2675
Alex Wu21858432012-04-01 20:13:08 +08002676/* no-op func for checking black surface */
2677static void
2678black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2679{
2680}
2681
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002682static bool
Alex Wu21858432012-04-01 20:13:08 +08002683is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2684{
2685 if (es->configure == black_surface_configure) {
2686 if (fs_surface)
2687 *fs_surface = (struct weston_surface *)es->private;
2688 return true;
2689 }
2690 return false;
2691}
2692
Kristian Høgsberg75840622011-09-06 13:48:16 -04002693static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002694click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002695 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002696{
Daniel Stone37816df2012-05-16 18:45:18 +01002697 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002698 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002699 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002700 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002701
Daniel Stone37816df2012-05-16 18:45:18 +01002702 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002703 if (!focus)
2704 return;
2705
Alex Wu21858432012-04-01 20:13:08 +08002706 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002707 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002708
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002709 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2710 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002711
Daniel Stone325fc2d2012-05-30 16:31:58 +01002712 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002713 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002714}
2715
2716static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002717lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002718{
Tiago Vignattibe143262012-04-16 17:31:41 +03002719 struct desktop_shell *shell =
2720 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002721 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002722 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002723
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002724 if (shell->locked) {
2725 wl_list_for_each(output, &shell->compositor->output_list, link)
2726 /* TODO: find a way to jump to other DPMS levels */
2727 if (output->set_dpms)
2728 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002729 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002730 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002731
2732 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002733
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002734 /* Hide all surfaces by removing the fullscreen, panel and
2735 * toplevel layers. This way nothing else can show or receive
2736 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002737
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002738 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002739 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002740 if (shell->showing_input_panels)
2741 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002742 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002743 wl_list_insert(&shell->compositor->cursor_layer.link,
2744 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002745
Pekka Paalanen77346a62011-11-30 16:26:35 +02002746 launch_screensaver(shell);
2747
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002748 /* TODO: disable bindings that should not work while locked. */
2749
2750 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002751}
2752
2753static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002754unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002755{
Tiago Vignattibe143262012-04-16 17:31:41 +03002756 struct desktop_shell *shell =
2757 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002758
Pekka Paalanend81c2162011-11-16 13:47:34 +02002759 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002760 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002761 return;
2762 }
2763
2764 /* If desktop-shell client has gone away, unlock immediately. */
2765 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002766 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002767 return;
2768 }
2769
2770 if (shell->prepare_event_sent)
2771 return;
2772
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002773 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002774 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002775}
2776
2777static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002778show_input_panels(struct wl_listener *listener, void *data)
2779{
2780 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002781 container_of(listener, struct desktop_shell,
2782 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002783 struct input_panel_surface *surface, *next;
2784 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002785
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002786 shell->showing_input_panels = true;
2787
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002788 if (!shell->locked)
2789 wl_list_insert(&shell->panel_layer.link,
2790 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002791
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002792 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002793 &shell->input_panel.surfaces, link) {
2794 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002795 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002796 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002797 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002798 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002799 weston_surface_damage(ws);
2800 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002801 }
2802}
2803
2804static void
2805hide_input_panels(struct wl_listener *listener, void *data)
2806{
2807 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002808 container_of(listener, struct desktop_shell,
2809 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002810 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002811
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002812 shell->showing_input_panels = false;
2813
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002814 if (!shell->locked)
2815 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002816
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002817 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002818 &shell->input_panel_layer.surface_list, layer_link)
2819 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002820}
2821
2822static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002823center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002824{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002825 int32_t width = weston_surface_buffer_width(surface);
2826 int32_t height = weston_surface_buffer_height(surface);
2827 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002828
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002829 x = output->x + (output->width - width) / 2;
2830 y = output->y + (output->height - height) / 2;
2831
2832 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002833}
2834
2835static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002836weston_surface_set_initial_position (struct weston_surface *surface,
2837 struct desktop_shell *shell)
2838{
2839 struct weston_compositor *compositor = shell->compositor;
2840 int ix = 0, iy = 0;
2841 int range_x, range_y;
2842 int dx, dy, x, y, panel_height;
2843 struct weston_output *output, *target_output = NULL;
2844 struct weston_seat *seat;
2845
2846 /* As a heuristic place the new window on the same output as the
2847 * pointer. Falling back to the output containing 0, 0.
2848 *
2849 * TODO: Do something clever for touch too?
2850 */
2851 wl_list_for_each(seat, &compositor->seat_list, link) {
2852 if (seat->has_pointer) {
2853 ix = wl_fixed_to_int(seat->pointer.x);
2854 iy = wl_fixed_to_int(seat->pointer.y);
2855 break;
2856 }
2857 }
2858
2859 wl_list_for_each(output, &compositor->output_list, link) {
2860 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2861 target_output = output;
2862 break;
2863 }
2864 }
2865
2866 if (!target_output) {
2867 weston_surface_set_position(surface, 10 + random() % 400,
2868 10 + random() % 400);
2869 return;
2870 }
2871
2872 /* Valid range within output where the surface will still be onscreen.
2873 * If this is negative it means that the surface is bigger than
2874 * output.
2875 */
2876 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002877 range_x = target_output->width - surface->geometry.width;
2878 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002879 surface->geometry.height;
2880
Rob Bradford4cb88c72012-08-13 15:18:44 +01002881 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002882 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002883 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002884 dx = 0;
2885
2886 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002887 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002888 else
2889 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002890
2891 x = target_output->x + dx;
2892 y = target_output->y + dy;
2893
2894 weston_surface_set_position (surface, x, y);
2895}
2896
2897static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002898map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002899 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002900{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002901 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002902 struct shell_surface *shsurf = get_shell_surface(surface);
2903 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002904 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002905 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002906 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002907 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002908
Pekka Paalanen60921e52012-01-25 15:55:43 +02002909 surface->geometry.width = width;
2910 surface->geometry.height = height;
2911 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002912
2913 /* initial positioning, see also configure() */
2914 switch (surface_type) {
2915 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002916 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002917 break;
Alex Wu4539b082012-03-01 12:57:46 +08002918 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002919 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002920 shell_map_fullscreen(shsurf);
2921 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002922 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002923 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002924 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002925 weston_surface_set_position(surface, shsurf->output->x,
2926 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002927 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002928 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002929 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00002930 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002931 case SHELL_SURFACE_NONE:
2932 weston_surface_set_position(surface,
2933 surface->geometry.x + sx,
2934 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002935 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002936 default:
2937 ;
2938 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002939
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002940 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002941 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002942 case SHELL_SURFACE_POPUP:
2943 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002944 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002945 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2946 break;
Alex Wu4539b082012-03-01 12:57:46 +08002947 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002948 case SHELL_SURFACE_NONE:
2949 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002950 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002951 ws = get_current_workspace(shell);
2952 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002953 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002954 }
2955
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002956 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002957 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002958 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2959 surface->output = shsurf->output;
2960 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002961
Juan Zhao7bb92f02011-12-15 11:31:51 -05002962 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002963 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002964 if (shsurf->transient.flags ==
2965 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2966 break;
2967 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002968 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002969 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002970 if (!shell->locked) {
2971 wl_list_for_each(seat, &compositor->seat_list, link)
2972 activate(shell, surface, seat);
2973 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002974 break;
2975 default:
2976 break;
2977 }
2978
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002979 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002980 {
2981 switch (shell->win_animation_type) {
2982 case ANIMATION_FADE:
2983 weston_fade_run(surface, NULL, NULL);
2984 break;
2985 case ANIMATION_ZOOM:
2986 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2987 break;
2988 default:
2989 break;
2990 }
2991 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002992}
2993
2994static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002995configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002996 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002997{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002998 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2999 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003000
Pekka Paalanen77346a62011-11-30 16:26:35 +02003001 shsurf = get_shell_surface(surface);
3002 if (shsurf)
3003 surface_type = shsurf->type;
3004
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003005 surface->geometry.x = x;
3006 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02003007 surface->geometry.width = width;
3008 surface->geometry.height = height;
3009 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003010
3011 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003012 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003013 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003014 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003015 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003016 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003017 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003018 surface->geometry.x = surface->output->x;
3019 surface->geometry.y = surface->output->y +
3020 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003021 break;
Alex Wu4539b082012-03-01 12:57:46 +08003022 case SHELL_SURFACE_TOPLEVEL:
3023 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003024 default:
3025 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003026 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003027
Alex Wu4539b082012-03-01 12:57:46 +08003028 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003029 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003030 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003031
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003032 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003033 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003034 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003035}
3036
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003037static void
3038shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3039{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003040 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003041 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003042 int32_t width = weston_surface_buffer_width(es);
3043 int32_t height = weston_surface_buffer_height(es);
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003044 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003045
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003046 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003047 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003048 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003049 type_changed = 1;
3050 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003051
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003052 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003053 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003054 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003055 es->geometry.width != width ||
3056 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003057 float from_x, from_y;
3058 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003059
3060 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3061 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3062 configure(shell, es,
3063 es->geometry.x + to_x - from_x,
3064 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003065 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003066 }
3067}
3068
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003069static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003070
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003071static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003072desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003073{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003074 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003075 struct desktop_shell *shell =
3076 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003077
3078 shell->child.process.pid = 0;
3079 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003080
3081 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3082 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003083 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003084 shell->child.deathstamp = time;
3085 shell->child.deathcount = 0;
3086 }
3087
3088 shell->child.deathcount++;
3089 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003090 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003091 return;
3092 }
3093
Martin Minarik6d118362012-06-07 18:01:59 +02003094 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003095 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003096}
3097
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003098static void
3099launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003100{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003101 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003102 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003103
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003104 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003105 &shell->child.process,
3106 shell_exe,
3107 desktop_shell_sigchld);
3108
3109 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003110 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003111}
3112
3113static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003114bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3115{
Tiago Vignattibe143262012-04-16 17:31:41 +03003116 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003117
3118 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003119 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003120}
3121
Kristian Høgsberg75840622011-09-06 13:48:16 -04003122static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003123unbind_desktop_shell(struct wl_resource *resource)
3124{
Tiago Vignattibe143262012-04-16 17:31:41 +03003125 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003126
3127 if (shell->locked)
3128 resume_desktop(shell);
3129
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003130 shell->child.desktop_shell = NULL;
3131 shell->prepare_event_sent = false;
3132 free(resource);
3133}
3134
3135static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003136bind_desktop_shell(struct wl_client *client,
3137 void *data, uint32_t version, uint32_t id)
3138{
Tiago Vignattibe143262012-04-16 17:31:41 +03003139 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003140 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003141
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003142 resource = wl_client_add_object(client, &desktop_shell_interface,
3143 &desktop_shell_implementation,
3144 id, shell);
3145
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003146 if (client == shell->child.client) {
3147 resource->destroy = unbind_desktop_shell;
3148 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003149 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003150 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003151
3152 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3153 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003154 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003155}
3156
Pekka Paalanen6e168112011-11-24 11:34:05 +02003157static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003158screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3159{
3160 struct desktop_shell *shell = surface->private;
3161
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003162 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003163 if (!shell->locked)
3164 return;
3165
3166 center_on_output(surface, surface->output);
3167
3168 if (wl_list_empty(&surface->layer_link)) {
3169 wl_list_insert(shell->lock_layer.surface_list.prev,
3170 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003171 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003172 shell->compositor->idle_time = shell->screensaver.duration;
3173 weston_compositor_wake(shell->compositor);
3174 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3175 }
3176}
3177
3178static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003179screensaver_set_surface(struct wl_client *client,
3180 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003181 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003182 struct wl_resource *output_resource)
3183{
Tiago Vignattibe143262012-04-16 17:31:41 +03003184 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003185 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003186 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003187
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003188 surface->configure = screensaver_configure;
3189 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003190 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003191}
3192
3193static const struct screensaver_interface screensaver_implementation = {
3194 screensaver_set_surface
3195};
3196
3197static void
3198unbind_screensaver(struct wl_resource *resource)
3199{
Tiago Vignattibe143262012-04-16 17:31:41 +03003200 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003201
Pekka Paalanen77346a62011-11-30 16:26:35 +02003202 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003203 free(resource);
3204}
3205
3206static void
3207bind_screensaver(struct wl_client *client,
3208 void *data, uint32_t version, uint32_t id)
3209{
Tiago Vignattibe143262012-04-16 17:31:41 +03003210 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003211 struct wl_resource *resource;
3212
3213 resource = wl_client_add_object(client, &screensaver_interface,
3214 &screensaver_implementation,
3215 id, shell);
3216
Pekka Paalanen77346a62011-11-30 16:26:35 +02003217 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003218 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003219 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003220 return;
3221 }
3222
3223 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3224 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003225 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003226}
3227
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003228static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003229input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3230{
3231 struct weston_mode *mode = surface->output->current;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003232 int32_t width = weston_surface_buffer_width(surface);
3233 int32_t height = weston_surface_buffer_height(surface);
3234 float x = (mode->width - width) / 2;
3235 float y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003236
3237 /* Don't map the input panel here, wait for
3238 * show_input_panels signal. */
3239
3240 weston_surface_configure(surface,
3241 surface->output->x + x,
3242 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003243 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003244}
3245
3246static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003247destroy_input_panel_surface(struct wl_listener *listener,
3248 void *data)
3249{
3250 struct input_panel_surface *input_panel_surface =
3251 container_of(listener, struct input_panel_surface, listener);
3252
3253 wl_list_remove(&listener->link);
3254 wl_list_remove(&input_panel_surface->link);
3255
3256 free(input_panel_surface);
3257}
3258
3259static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003260input_panel_set_surface(struct wl_client *client,
3261 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003262 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003263 struct wl_resource *output_resource)
3264{
3265 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003266 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003267 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003268 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003269
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003270 surface->configure = input_panel_configure;
3271 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003272 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003273
3274 input_panel_surface = malloc(sizeof *input_panel_surface);
3275 if (!input_panel_surface) {
3276 wl_resource_post_no_memory(resource);
3277 return;
3278 }
3279
3280 input_panel_surface->surface = surface;
3281 input_panel_surface->listener.notify = destroy_input_panel_surface;
3282
3283 wl_signal_add(&surface_resource->destroy_signal,
3284 &input_panel_surface->listener);
3285
3286 wl_list_insert(&shell->input_panel.surfaces,
3287 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003288}
3289
3290static const struct input_panel_interface input_panel_implementation = {
3291 input_panel_set_surface
3292};
3293
3294static void
3295unbind_input_panel(struct wl_resource *resource)
3296{
3297 struct desktop_shell *shell = resource->data;
3298
3299 shell->input_panel.binding = NULL;
3300 free(resource);
3301}
3302
3303static void
3304bind_input_panel(struct wl_client *client,
3305 void *data, uint32_t version, uint32_t id)
3306{
3307 struct desktop_shell *shell = data;
3308 struct wl_resource *resource;
3309
3310 resource = wl_client_add_object(client, &input_panel_interface,
3311 &input_panel_implementation,
3312 id, shell);
3313
3314 if (shell->input_panel.binding == NULL) {
3315 resource->destroy = unbind_input_panel;
3316 shell->input_panel.binding = resource;
3317 return;
3318 }
3319
3320 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3321 "interface object already bound");
3322 wl_resource_destroy(resource);
3323}
3324
Kristian Høgsberg07045392012-02-19 18:52:44 -05003325struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003326 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003327 struct weston_surface *current;
3328 struct wl_listener listener;
3329 struct wl_keyboard_grab grab;
3330};
3331
3332static void
3333switcher_next(struct switcher *switcher)
3334{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003335 struct weston_surface *surface;
3336 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003337 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003338 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003339
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003340 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003341 switch (get_shell_surface_type(surface)) {
3342 case SHELL_SURFACE_TOPLEVEL:
3343 case SHELL_SURFACE_FULLSCREEN:
3344 case SHELL_SURFACE_MAXIMIZED:
3345 if (first == NULL)
3346 first = surface;
3347 if (prev == switcher->current)
3348 next = surface;
3349 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003350 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003351 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003352 weston_surface_damage(surface);
3353 break;
3354 default:
3355 break;
3356 }
Alex Wu1659daa2012-04-01 20:13:09 +08003357
3358 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003359 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003360 surface->geometry.dirty = 1;
3361 weston_surface_damage(surface);
3362 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003363 }
3364
3365 if (next == NULL)
3366 next = first;
3367
Alex Wu07b26062012-03-12 16:06:01 +08003368 if (next == NULL)
3369 return;
3370
Kristian Høgsberg07045392012-02-19 18:52:44 -05003371 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003372 wl_signal_add(&next->surface.resource.destroy_signal,
3373 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003374
3375 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003376 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003377
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003378 shsurf = get_shell_surface(switcher->current);
3379 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003380 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003381}
3382
3383static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003384switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003385{
3386 struct switcher *switcher =
3387 container_of(listener, struct switcher, listener);
3388
3389 switcher_next(switcher);
3390}
3391
3392static void
Daniel Stone351eb612012-05-31 15:27:47 -04003393switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003394{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003395 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003396 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003397 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003398
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003399 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003400 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003401 weston_surface_damage(surface);
3402 }
3403
Alex Wu07b26062012-03-12 16:06:01 +08003404 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003405 activate(switcher->shell, switcher->current,
3406 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003407 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003408 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003409 free(switcher);
3410}
3411
3412static void
3413switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003414 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003415{
3416 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003417 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003418
Daniel Stonec9785ea2012-05-30 16:31:52 +01003419 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003420 switcher_next(switcher);
3421}
3422
3423static void
3424switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3425 uint32_t mods_depressed, uint32_t mods_latched,
3426 uint32_t mods_locked, uint32_t group)
3427{
3428 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003429 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003430
Daniel Stone351eb612012-05-31 15:27:47 -04003431 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3432 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003433}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003434
3435static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003436 switcher_key,
3437 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003438};
3439
3440static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003441switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3442 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003443{
Tiago Vignattibe143262012-04-16 17:31:41 +03003444 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003445 struct switcher *switcher;
3446
3447 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003448 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003449 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003450 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003451 wl_list_init(&switcher->listener.link);
3452
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003453 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003454 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003455 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3456 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003457 switcher_next(switcher);
3458}
3459
Pekka Paalanen3c647232011-12-22 13:43:43 +02003460static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003461backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3462 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003463{
3464 struct weston_compositor *compositor = data;
3465 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003466 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003467
3468 /* TODO: we're limiting to simple use cases, where we assume just
3469 * control on the primary display. We'd have to extend later if we
3470 * ever get support for setting backlights on random desktop LCD
3471 * panels though */
3472 output = get_default_output(compositor);
3473 if (!output)
3474 return;
3475
3476 if (!output->set_backlight)
3477 return;
3478
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003479 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3480 backlight_new = output->backlight_current - 25;
3481 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3482 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003483
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003484 if (backlight_new < 5)
3485 backlight_new = 5;
3486 if (backlight_new > 255)
3487 backlight_new = 255;
3488
3489 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003490 output->set_backlight(output, output->backlight_current);
3491}
3492
3493static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003494fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3495 void *data)
3496{
3497 struct desktop_shell *shell = data;
3498 struct weston_compositor *compositor = shell->compositor;
3499 compositor->fan_debug = !compositor->fan_debug;
3500 weston_compositor_damage_all(compositor);
3501}
3502
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003503struct debug_binding_grab {
3504 struct wl_keyboard_grab grab;
3505 struct weston_seat *seat;
3506 uint32_t key[2];
3507 int key_released[2];
3508};
3509
3510static void
3511debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3512 uint32_t key, uint32_t state)
3513{
3514 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3515 struct wl_resource *resource;
3516 struct wl_display *display;
3517 uint32_t serial;
3518 int send = 0, terminate = 0;
3519 int check_binding = 1;
3520 int i;
3521
3522 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3523 /* Do not run bindings on key releases */
3524 check_binding = 0;
3525
3526 for (i = 0; i < 2; i++)
3527 if (key == db->key[i])
3528 db->key_released[i] = 1;
3529
3530 if (db->key_released[0] && db->key_released[1]) {
3531 /* All key releases been swalled so end the grab */
3532 terminate = 1;
3533 } else if (key != db->key[0] && key != db->key[1]) {
3534 /* Should not swallow release of other keys */
3535 send = 1;
3536 }
3537 } else if (key == db->key[0] && !db->key_released[0]) {
3538 /* Do not check bindings for the first press of the binding
3539 * key. This allows it to be used as a debug shortcut.
3540 * We still need to swallow this event. */
3541 check_binding = 0;
3542 } else if (db->key[1]) {
3543 /* If we already ran a binding don't process another one since
3544 * we can't keep track of all the binding keys that were
3545 * pressed in order to swallow the release events. */
3546 send = 1;
3547 check_binding = 0;
3548 }
3549
3550 if (check_binding) {
3551 struct weston_compositor *ec = db->seat->compositor;
3552
3553 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3554 key, state)) {
3555 /* We ran a binding so swallow the press and keep the
3556 * grab to swallow the released too. */
3557 send = 0;
3558 terminate = 0;
3559 db->key[1] = key;
3560 } else {
3561 /* Terminate the grab since the key pressed is not a
3562 * debug binding key. */
3563 send = 1;
3564 terminate = 1;
3565 }
3566 }
3567
3568 if (send) {
3569 resource = grab->keyboard->focus_resource;
3570
3571 if (resource) {
3572 display = wl_client_get_display(resource->client);
3573 serial = wl_display_next_serial(display);
3574 wl_keyboard_send_key(resource, serial, time, key, state);
3575 }
3576 }
3577
3578 if (terminate) {
3579 wl_keyboard_end_grab(grab->keyboard);
3580 free(db);
3581 }
3582}
3583
3584static void
3585debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3586 uint32_t mods_depressed, uint32_t mods_latched,
3587 uint32_t mods_locked, uint32_t group)
3588{
3589 struct wl_resource *resource;
3590
3591 resource = grab->keyboard->focus_resource;
3592 if (!resource)
3593 return;
3594
3595 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3596 mods_latched, mods_locked, group);
3597}
3598
3599struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3600 debug_binding_key,
3601 debug_binding_modifiers
3602};
3603
3604static void
3605debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3606{
3607 struct debug_binding_grab *grab;
3608
3609 grab = calloc(1, sizeof *grab);
3610 if (!grab)
3611 return;
3612
3613 grab->seat = (struct weston_seat *) seat;
3614 grab->key[0] = key;
3615 grab->grab.interface = &debug_binding_keyboard_grab;
3616 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3617}
3618
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003619static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003620force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3621 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003622{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003623 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003624 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003625 struct desktop_shell *shell = data;
3626 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003627 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003628
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003629 focus_surface = seat->keyboard->focus;
3630 if (!focus_surface)
3631 return;
3632
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003633 wl_signal_emit(&compositor->kill_signal, focus_surface);
3634
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003635 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003636 wl_client_get_credentials(client, &pid, NULL, NULL);
3637
3638 /* Skip clients that we launched ourselves (the credentials of
3639 * the socketpair is ours) */
3640 if (pid == getpid())
3641 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003642
Daniel Stone325fc2d2012-05-30 16:31:58 +01003643 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003644}
3645
3646static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003647workspace_up_binding(struct wl_seat *seat, uint32_t time,
3648 uint32_t key, void *data)
3649{
3650 struct desktop_shell *shell = data;
3651 unsigned int new_index = shell->workspaces.current;
3652
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003653 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003654 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003655 if (new_index != 0)
3656 new_index--;
3657
3658 change_workspace(shell, new_index);
3659}
3660
3661static void
3662workspace_down_binding(struct wl_seat *seat, uint32_t time,
3663 uint32_t key, void *data)
3664{
3665 struct desktop_shell *shell = data;
3666 unsigned int new_index = shell->workspaces.current;
3667
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003668 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003669 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003670 if (new_index < shell->workspaces.num - 1)
3671 new_index++;
3672
3673 change_workspace(shell, new_index);
3674}
3675
3676static void
3677workspace_f_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;
3682
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003683 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003684 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003685 new_index = key - KEY_F1;
3686 if (new_index >= shell->workspaces.num)
3687 new_index = shell->workspaces.num - 1;
3688
3689 change_workspace(shell, new_index);
3690}
3691
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003692static void
3693workspace_move_surface_up_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 != 0)
3703 new_index--;
3704
3705 take_surface_to_workspace_by_seat(shell, seat, new_index);
3706}
3707
3708static void
3709workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3710 uint32_t key, void *data)
3711{
3712 struct desktop_shell *shell = data;
3713 unsigned int new_index = shell->workspaces.current;
3714
3715 if (shell->locked)
3716 return;
3717
3718 if (new_index < shell->workspaces.num - 1)
3719 new_index++;
3720
3721 take_surface_to_workspace_by_seat(shell, seat, new_index);
3722}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003723
3724static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003725shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003726{
Tiago Vignattibe143262012-04-16 17:31:41 +03003727 struct desktop_shell *shell =
3728 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003729 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003730
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003731 if (shell->child.client)
3732 wl_client_destroy(shell->child.client);
3733
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003734 wl_list_remove(&shell->lock_listener.link);
3735 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003736 wl_list_remove(&shell->show_input_panel_listener.link);
3737 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003738
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003739 wl_array_for_each(ws, &shell->workspaces.array)
3740 workspace_destroy(*ws);
3741 wl_array_release(&shell->workspaces.array);
3742
Pekka Paalanen3c647232011-12-22 13:43:43 +02003743 free(shell->screensaver.path);
3744 free(shell);
3745}
3746
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003747static void
3748shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3749{
3750 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003751 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003752
3753 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003754 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3755 MODIFIER_CTRL | MODIFIER_ALT,
3756 terminate_binding, ec);
3757 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3758 click_to_activate_binding,
3759 shell);
3760 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3761 MODIFIER_SUPER | MODIFIER_ALT,
3762 surface_opacity_binding, NULL);
3763 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3764 MODIFIER_SUPER, zoom_axis_binding,
3765 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003766
3767 /* configurable bindings */
3768 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003769 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3770 zoom_key_binding, NULL);
3771 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3772 zoom_key_binding, NULL);
3773 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3774 shell);
3775 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3776 resize_binding, shell);
3777 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3778 rotate_binding, NULL);
3779 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3780 shell);
3781 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3782 ec);
3783 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3784 backlight_binding, ec);
3785 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3786 ec);
3787 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3788 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003789 weston_compositor_add_key_binding(ec, KEY_K, mod,
3790 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003791 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3792 workspace_up_binding, shell);
3793 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3794 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003795 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3796 workspace_move_surface_up_binding,
3797 shell);
3798 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3799 workspace_move_surface_down_binding,
3800 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003801
3802 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3803 if (shell->workspaces.num > 1) {
3804 num_workspace_bindings = shell->workspaces.num;
3805 if (num_workspace_bindings > 6)
3806 num_workspace_bindings = 6;
3807 for (i = 0; i < num_workspace_bindings; i++)
3808 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3809 workspace_f_binding,
3810 shell);
3811 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003812
3813 /* Debug bindings */
3814 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3815 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003816 weston_compositor_add_debug_binding(ec, KEY_F,
3817 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003818}
3819
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003820WL_EXPORT int
Kristian Høgsbergb00a9d32012-09-11 14:06:27 -04003821module_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003822{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003823 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003824 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003825 struct workspace **pws;
3826 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003827 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003828
3829 shell = malloc(sizeof *shell);
3830 if (shell == NULL)
3831 return -1;
3832
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003833 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003834 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003835
3836 shell->destroy_listener.notify = shell_destroy;
3837 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3838 shell->lock_listener.notify = lock;
3839 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3840 shell->unlock_listener.notify = unlock;
3841 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003842 shell->show_input_panel_listener.notify = show_input_panels;
3843 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3844 shell->hide_input_panel_listener.notify = hide_input_panels;
3845 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003846 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003847 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003848 ec->shell_interface.create_shell_surface = create_shell_surface;
3849 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003850 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003851 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003852 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003853 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003854
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003855 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003856
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003857 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3858 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003859 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3860 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003861 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003862
3863 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02003864 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003865
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003866 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003867
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003868 for (i = 0; i < shell->workspaces.num; i++) {
3869 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3870 if (pws == NULL)
3871 return -1;
3872
3873 *pws = workspace_create();
3874 if (*pws == NULL)
3875 return -1;
3876 }
3877 activate_workspace(shell, 0);
3878
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003879 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003880 wl_list_init(&shell->workspaces.animation.link);
3881 shell->workspaces.animation.frame = animate_workspace_change_frame;
3882
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003883 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3884 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003885 return -1;
3886
Kristian Høgsberg75840622011-09-06 13:48:16 -04003887 if (wl_display_add_global(ec->wl_display,
3888 &desktop_shell_interface,
3889 shell, bind_desktop_shell) == NULL)
3890 return -1;
3891
Pekka Paalanen6e168112011-11-24 11:34:05 +02003892 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3893 shell, bind_screensaver) == NULL)
3894 return -1;
3895
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003896 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3897 shell, bind_input_panel) == NULL)
3898 return -1;
3899
Jonas Ådahle9d22502012-08-29 22:13:01 +02003900 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3901 shell, bind_workspace_manager) == NULL)
3902 return -1;
3903
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003904 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003905
3906 loop = wl_display_get_event_loop(ec->wl_display);
3907 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003908
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003909 wl_list_for_each(seat, &ec->seat_list, link)
3910 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003911
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003912 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003913
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003914 return 0;
3915}