blob: 6b0f5745f2dfb9aca0e05b167c6578b484c328f3 [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:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001675 /* 1:1 mapping between surface and output dimensions */
1676 if (output->width == surface->geometry.width &&
1677 output->height == surface->geometry.height) {
1678 weston_surface_set_position(surface, output->x, output->y);
1679 break;
1680 }
1681
Alex Wu4539b082012-03-01 12:57:46 +08001682 matrix = &shsurf->fullscreen.transform.matrix;
1683 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001684
Scott Moreau1bad5db2012-08-18 01:04:05 -06001685 output_aspect = (float) output->width /
1686 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001687 surface_aspect = (float) surface->geometry.width /
1688 (float) surface->geometry.height;
1689 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001690 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001691 (float) surface->geometry.width;
1692 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001693 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001694 (float) surface->geometry.height;
1695
Alex Wu4539b082012-03-01 12:57:46 +08001696 weston_matrix_scale(matrix, scale, scale, 1);
1697 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001698 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001699 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001700 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1701 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001702 weston_surface_set_position(surface, x, y);
1703
Alex Wu4539b082012-03-01 12:57:46 +08001704 break;
1705 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001706 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001707 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001708 surface->geometry.width,
1709 surface->geometry.height,
1710 shsurf->fullscreen.framerate};
1711
1712 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001713 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001714 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001715 output->width,
1716 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001717 weston_surface_set_position(surface, output->x, output->y);
1718 break;
1719 }
1720 }
Alex Wu4539b082012-03-01 12:57:46 +08001721 break;
1722 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1723 break;
1724 default:
1725 break;
1726 }
1727}
1728
1729/* make the fullscreen and black surface at the top */
1730static void
1731shell_stack_fullscreen(struct shell_surface *shsurf)
1732{
Alex Wubd3354b2012-04-17 17:20:49 +08001733 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001734 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001735 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001736
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001737 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001738 wl_list_insert(&shell->fullscreen_layer.surface_list,
1739 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001740 weston_surface_damage(surface);
1741
1742 if (!shsurf->fullscreen.black_surface)
1743 shsurf->fullscreen.black_surface =
1744 create_black_surface(surface->compositor,
1745 surface,
1746 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001747 output->width,
1748 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001749
1750 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001751 wl_list_insert(&surface->layer_link,
1752 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001753 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001754}
1755
1756static void
1757shell_map_fullscreen(struct shell_surface *shsurf)
1758{
Alex Wu4539b082012-03-01 12:57:46 +08001759 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001760 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001761}
1762
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001763static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001764set_fullscreen(struct shell_surface *shsurf,
1765 uint32_t method,
1766 uint32_t framerate,
1767 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001768{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001769 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001770
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001771 if (output)
1772 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001773 else if (es->output)
1774 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001775 else
1776 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001777
Alex Wu4539b082012-03-01 12:57:46 +08001778 shsurf->fullscreen_output = shsurf->output;
1779 shsurf->fullscreen.type = method;
1780 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001781 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001782
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001783 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001784 shsurf->output->width,
1785 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001786}
1787
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001788static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001789shell_surface_set_fullscreen(struct wl_client *client,
1790 struct wl_resource *resource,
1791 uint32_t method,
1792 uint32_t framerate,
1793 struct wl_resource *output_resource)
1794{
1795 struct shell_surface *shsurf = resource->data;
1796 struct weston_output *output;
1797
1798 if (output_resource)
1799 output = output_resource->data;
1800 else
1801 output = NULL;
1802
1803 set_fullscreen(shsurf, method, framerate, output);
1804}
1805
1806static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001807popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001808 struct wl_surface *surface,
1809 wl_fixed_t x,
1810 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001811{
Daniel Stone37816df2012-05-16 18:45:18 +01001812 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001813 struct shell_surface *priv =
1814 container_of(grab, struct shell_surface, popup.grab);
1815 struct wl_client *client = priv->surface->surface.resource.client;
1816
Pekka Paalanencb108432012-01-19 16:25:40 +02001817 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001818 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001819 grab->focus = surface;
1820 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001821 wl_pointer_set_focus(pointer, NULL,
1822 wl_fixed_from_int(0),
1823 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001824 grab->focus = NULL;
1825 }
1826}
1827
1828static void
Scott Moreau447013d2012-02-18 05:05:29 -07001829popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001830 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001831{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001832 struct wl_resource *resource;
1833
Daniel Stone37816df2012-05-16 18:45:18 +01001834 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001835 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001836 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001837}
1838
1839static void
Scott Moreau447013d2012-02-18 05:05:29 -07001840popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001841 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001842{
1843 struct wl_resource *resource;
1844 struct shell_surface *shsurf =
1845 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001846 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001847 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001848 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001849
Daniel Stone37816df2012-05-16 18:45:18 +01001850 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001851 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001852 display = wl_client_get_display(resource->client);
1853 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001854 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001855 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001856 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001857 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001858 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001859 }
1860
Daniel Stone4dbadb12012-05-30 16:31:51 +01001861 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001862 shsurf->popup.initial_up = 1;
1863}
1864
Scott Moreau447013d2012-02-18 05:05:29 -07001865static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001866 popup_grab_focus,
1867 popup_grab_motion,
1868 popup_grab_button,
1869};
1870
1871static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001872popup_grab_end(struct wl_pointer *pointer)
1873{
1874 struct wl_pointer_grab *grab = pointer->grab;
1875 struct shell_surface *shsurf =
1876 container_of(grab, struct shell_surface, popup.grab);
1877
1878 if (pointer->grab->interface == &popup_grab_interface) {
1879 wl_shell_surface_send_popup_done(&shsurf->resource);
1880 wl_pointer_end_grab(grab->pointer);
1881 shsurf->popup.grab.pointer = NULL;
1882 }
1883}
1884
1885static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001886shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001887{
Daniel Stone37816df2012-05-16 18:45:18 +01001888 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001889 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001890 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001891
1892 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001893 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001894
Pekka Paalanen938269a2012-02-07 14:19:01 +02001895 weston_surface_update_transform(parent);
1896 if (parent->transform.enabled) {
1897 shsurf->popup.parent_transform.matrix =
1898 parent->transform.matrix;
1899 } else {
1900 /* construct x, y translation matrix */
1901 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03001902 shsurf->popup.parent_transform.matrix.type =
1903 WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen938269a2012-02-07 14:19:01 +02001904 shsurf->popup.parent_transform.matrix.d[12] =
1905 parent->geometry.x;
1906 shsurf->popup.parent_transform.matrix.d[13] =
1907 parent->geometry.y;
1908 }
1909 wl_list_insert(es->geometry.transformation_list.prev,
1910 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001911
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001912 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001913 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1914 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001915
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001916 /* We don't require the grab to still be active, but if another
1917 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001918 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1919 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001920 } else {
1921 wl_shell_surface_send_popup_done(&shsurf->resource);
1922 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001923}
1924
1925static void
1926shell_surface_set_popup(struct wl_client *client,
1927 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001928 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001929 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001930 struct wl_resource *parent_resource,
1931 int32_t x, int32_t y, uint32_t flags)
1932{
1933 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001934
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001935 shsurf->type = SHELL_SURFACE_POPUP;
1936 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001937 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001938 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001939 shsurf->popup.x = x;
1940 shsurf->popup.y = y;
1941}
1942
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001943static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001944 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001945 shell_surface_move,
1946 shell_surface_resize,
1947 shell_surface_set_toplevel,
1948 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001949 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001950 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001951 shell_surface_set_maximized,
1952 shell_surface_set_title,
1953 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001954};
1955
1956static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001957destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001958{
Daniel Stone37816df2012-05-16 18:45:18 +01001959 if (shsurf->popup.grab.pointer)
1960 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001961
Alex Wubd3354b2012-04-17 17:20:49 +08001962 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1963 shell_surface_is_top_fullscreen(shsurf)) {
1964 weston_output_switch_mode(shsurf->fullscreen_output,
1965 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001966 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001967
Alex Wuaa08e2d2012-03-05 11:01:40 +08001968 if (shsurf->fullscreen.black_surface)
1969 weston_surface_destroy(shsurf->fullscreen.black_surface);
1970
Alex Wubd3354b2012-04-17 17:20:49 +08001971 /* As destroy_resource() use wl_list_for_each_safe(),
1972 * we can always remove the listener.
1973 */
1974 wl_list_remove(&shsurf->surface_destroy_listener.link);
1975 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001976 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001977
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001978 wl_list_remove(&shsurf->link);
1979 free(shsurf);
1980}
1981
1982static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001983shell_destroy_shell_surface(struct wl_resource *resource)
1984{
1985 struct shell_surface *shsurf = resource->data;
1986
1987 destroy_shell_surface(shsurf);
1988}
1989
1990static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001991shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001992{
1993 struct shell_surface *shsurf = container_of(listener,
1994 struct shell_surface,
1995 surface_destroy_listener);
1996
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001997 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001998 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001999 } else {
2000 wl_signal_emit(&shsurf->resource.destroy_signal,
2001 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002002 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002003 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002004}
2005
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002006static void
2007shell_surface_configure(struct weston_surface *, int32_t, int32_t);
2008
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002009static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002010get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002011{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002012 if (surface->configure == shell_surface_configure)
2013 return surface->private;
2014 else
2015 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002016}
2017
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002018static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002019create_shell_surface(void *shell, struct weston_surface *surface,
2020 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002021{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002022 struct shell_surface *shsurf;
2023
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002024 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002025 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002026 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002027 }
2028
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002029 shsurf = calloc(1, sizeof *shsurf);
2030 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002031 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002032 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002033 }
2034
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002035 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002036 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002037
Tiago Vignattibc052c92012-04-19 16:18:18 +03002038 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002039 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002040 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002041 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002042 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002043 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2044 shsurf->fullscreen.framerate = 0;
2045 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002046 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002047 wl_list_init(&shsurf->fullscreen.transform.link);
2048
Tiago Vignattibc052c92012-04-19 16:18:18 +03002049 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002050 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2051 wl_signal_add(&surface->surface.resource.destroy_signal,
2052 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002053
2054 /* init link so its safe to always remove it in destroy_shell_surface */
2055 wl_list_init(&shsurf->link);
2056
Pekka Paalanen460099f2012-01-20 16:48:25 +02002057 /* empty when not in use */
2058 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002059 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002060
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002061 wl_list_init(&shsurf->workspace_transform.link);
2062
Pekka Paalanen98262232011-12-01 10:42:22 +02002063 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002064 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002065
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002066 shsurf->client = client;
2067
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002068 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002069}
2070
2071static void
2072shell_get_shell_surface(struct wl_client *client,
2073 struct wl_resource *resource,
2074 uint32_t id,
2075 struct wl_resource *surface_resource)
2076{
2077 struct weston_surface *surface = surface_resource->data;
2078 struct desktop_shell *shell = resource->data;
2079 struct shell_surface *shsurf;
2080
2081 if (get_shell_surface(surface)) {
2082 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002083 WL_DISPLAY_ERROR_INVALID_OBJECT,
2084 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002085 return;
2086 }
2087
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002088 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002089 if (!shsurf) {
2090 wl_resource_post_error(surface_resource,
2091 WL_DISPLAY_ERROR_INVALID_OBJECT,
2092 "surface->configure already set");
2093 return;
2094 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002095
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002096 shsurf->resource.destroy = shell_destroy_shell_surface;
2097 shsurf->resource.object.id = id;
2098 shsurf->resource.object.interface = &wl_shell_surface_interface;
2099 shsurf->resource.object.implementation =
2100 (void (**)(void)) &shell_surface_implementation;
2101 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002102
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002103 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002104}
2105
2106static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002107 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002108};
2109
Kristian Høgsberg07937562011-04-12 17:25:42 -04002110static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002111handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002112{
2113 proc->pid = 0;
2114}
2115
2116static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002117launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002118{
2119 if (shell->screensaver.binding)
2120 return;
2121
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002122 if (!shell->screensaver.path)
2123 return;
2124
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002125 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002126 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002127 return;
2128 }
2129
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002130 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002131 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002132 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002133 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002134}
2135
2136static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002137terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002138{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002139 if (shell->screensaver.process.pid == 0)
2140 return;
2141
2142 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002143}
2144
2145static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002146configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002147{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002148 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002149
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002150 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2151 if (s->output == es->output && s != es) {
2152 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002153 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002154 }
2155 }
2156
2157 weston_surface_configure(es, es->output->x, es->output->y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002158 weston_surface_buffer_width(es),
2159 weston_surface_buffer_height(es));
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002160
2161 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002162 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002163 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002164 }
2165}
2166
2167static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002168background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2169{
2170 struct desktop_shell *shell = es->private;
2171
2172 configure_static_surface(es, &shell->background_layer);
2173}
2174
2175static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002176desktop_shell_set_background(struct wl_client *client,
2177 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002178 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002179 struct wl_resource *surface_resource)
2180{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002181 struct desktop_shell *shell = resource->data;
2182 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002183
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002184 if (surface->configure) {
2185 wl_resource_post_error(surface_resource,
2186 WL_DISPLAY_ERROR_INVALID_OBJECT,
2187 "surface role already assigned");
2188 return;
2189 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002190
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002191 surface->configure = background_configure;
2192 surface->private = shell;
2193 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002194 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002195 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002196 surface->output->width,
2197 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002198}
2199
2200static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002201panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2202{
2203 struct desktop_shell *shell = es->private;
2204
2205 configure_static_surface(es, &shell->panel_layer);
2206}
2207
2208static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002209desktop_shell_set_panel(struct wl_client *client,
2210 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002211 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002212 struct wl_resource *surface_resource)
2213{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002214 struct desktop_shell *shell = resource->data;
2215 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002216
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002217 if (surface->configure) {
2218 wl_resource_post_error(surface_resource,
2219 WL_DISPLAY_ERROR_INVALID_OBJECT,
2220 "surface role already assigned");
2221 return;
2222 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002223
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002224 surface->configure = panel_configure;
2225 surface->private = shell;
2226 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002227 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002228 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002229 surface->output->width,
2230 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002231}
2232
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002233static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002234lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2235{
2236 struct desktop_shell *shell = surface->private;
2237
2238 center_on_output(surface, get_default_output(shell->compositor));
2239
2240 if (!weston_surface_is_mapped(surface)) {
2241 wl_list_insert(&shell->lock_layer.surface_list,
2242 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002243 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002244 weston_compositor_wake(shell->compositor);
2245 }
2246}
2247
2248static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002249handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002250{
Tiago Vignattibe143262012-04-16 17:31:41 +03002251 struct desktop_shell *shell =
2252 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002253
Martin Minarik6d118362012-06-07 18:01:59 +02002254 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002255 shell->lock_surface = NULL;
2256}
2257
2258static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002259desktop_shell_set_lock_surface(struct wl_client *client,
2260 struct wl_resource *resource,
2261 struct wl_resource *surface_resource)
2262{
Tiago Vignattibe143262012-04-16 17:31:41 +03002263 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002264 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002265
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002266 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002267
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002268 if (!shell->locked)
2269 return;
2270
Pekka Paalanen98262232011-12-01 10:42:22 +02002271 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002272
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002273 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2274 wl_signal_add(&surface_resource->destroy_signal,
2275 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002276
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002277 surface->configure = lock_surface_configure;
2278 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002279}
2280
2281static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002282resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002283{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002284 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002285
Pekka Paalanen77346a62011-11-30 16:26:35 +02002286 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002287
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002288 wl_list_remove(&shell->lock_layer.link);
2289 wl_list_insert(&shell->compositor->cursor_layer.link,
2290 &shell->fullscreen_layer.link);
2291 wl_list_insert(&shell->fullscreen_layer.link,
2292 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002293 if (shell->showing_input_panels) {
2294 wl_list_insert(&shell->panel_layer.link,
2295 &shell->input_panel_layer.link);
2296 wl_list_insert(&shell->input_panel_layer.link,
2297 &ws->layer.link);
2298 } else {
2299 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2300 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002301
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002302 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002303
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002304 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002305 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002306 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002307 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002308}
2309
2310static void
2311desktop_shell_unlock(struct wl_client *client,
2312 struct wl_resource *resource)
2313{
Tiago Vignattibe143262012-04-16 17:31:41 +03002314 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002315
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002316 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002317
2318 if (shell->locked)
2319 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002320}
2321
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002322static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002323desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002324 struct wl_resource *resource,
2325 struct wl_resource *surface_resource)
2326{
2327 struct desktop_shell *shell = resource->data;
2328
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002329 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002330}
2331
Kristian Høgsberg75840622011-09-06 13:48:16 -04002332static const struct desktop_shell_interface desktop_shell_implementation = {
2333 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002334 desktop_shell_set_panel,
2335 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002336 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002337 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002338};
2339
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002340static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002341get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002342{
2343 struct shell_surface *shsurf;
2344
2345 shsurf = get_shell_surface(surface);
2346 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002347 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002348 return shsurf->type;
2349}
2350
Kristian Høgsberg75840622011-09-06 13:48:16 -04002351static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002352move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002353{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002354 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002355 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002356 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002357
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002358 if (surface == NULL)
2359 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002360
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002361 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002362 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002363 return;
2364
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002365 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002366}
2367
2368static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002369resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002370{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002371 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002372 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002373 uint32_t edges = 0;
2374 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002375 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002376
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002377 if (surface == NULL)
2378 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002379
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002380 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002381 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002382 return;
2383
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002384 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002385 wl_fixed_to_int(seat->pointer->grab_x),
2386 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002387 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002388
Pekka Paalanen60921e52012-01-25 15:55:43 +02002389 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002390 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002391 else if (x < 2 * surface->geometry.width / 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_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002395
Pekka Paalanen60921e52012-01-25 15:55:43 +02002396 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002397 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002398 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002399 edges |= 0;
2400 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002401 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002402
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002403 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002404}
2405
2406static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002407surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002408 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002409{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002410 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002411 struct shell_surface *shsurf;
2412 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002413 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002414
2415 if (surface == NULL)
2416 return;
2417
2418 shsurf = get_shell_surface(surface);
2419 if (!shsurf)
2420 return;
2421
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002422 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002423
Scott Moreau02709af2012-05-22 01:54:10 -06002424 if (surface->alpha > 1.0)
2425 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002426 if (surface->alpha < step)
2427 surface->alpha = step;
2428
2429 surface->geometry.dirty = 1;
2430 weston_surface_damage(surface);
2431}
2432
2433static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002434do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002435 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002436{
Daniel Stone37816df2012-05-16 18:45:18 +01002437 struct weston_seat *ws = (struct weston_seat *) seat;
2438 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002439 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002440 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002441
2442 wl_list_for_each(output, &compositor->output_list, link) {
2443 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002444 wl_fixed_to_double(seat->pointer->x),
2445 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002446 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002447 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002448 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002449 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002450 increment = -output->zoom.increment;
2451 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002452 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002453 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002454 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002455 else
2456 increment = 0;
2457
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002458 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002459
Scott Moreaue6603982012-06-11 13:07:51 -06002460 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002461 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002462 else if (output->zoom.level > output->zoom.max_level)
2463 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002464 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002465 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002466 output->disable_planes++;
2467 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002468
Scott Moreaue6603982012-06-11 13:07:51 -06002469 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002470
Scott Moreau8dacaab2012-06-17 18:10:58 -06002471 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002472 }
2473 }
2474}
2475
Scott Moreauccbf29d2012-02-22 14:21:41 -07002476static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002477zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002478 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002479{
2480 do_zoom(seat, time, 0, axis, value);
2481}
2482
2483static void
2484zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2485 void *data)
2486{
2487 do_zoom(seat, time, key, 0, 0);
2488}
2489
2490static void
2491terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2492 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002493{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002494 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002495
Daniel Stone325fc2d2012-05-30 16:31:58 +01002496 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002497}
2498
2499static void
Scott Moreau447013d2012-02-18 05:05:29 -07002500rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002501 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002502{
2503 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002504 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002505 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002506 struct shell_surface *shsurf = rotate->base.shsurf;
2507 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002508 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002509
2510 if (!shsurf)
2511 return;
2512
2513 surface = shsurf->surface;
2514
2515 cx = 0.5f * surface->geometry.width;
2516 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002517
Daniel Stone37816df2012-05-16 18:45:18 +01002518 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2519 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002520 r = sqrtf(dx * dx + dy * dy);
2521
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002522 wl_list_remove(&shsurf->rotation.transform.link);
2523 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002524
2525 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002526 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002527 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002528
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002529 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002530 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002531
2532 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002533 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002534 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002535 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002536 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002537
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002538 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002539 &shsurf->surface->geometry.transformation_list,
2540 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002541 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002542 wl_list_init(&shsurf->rotation.transform.link);
2543 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002544 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002545 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002546
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002547 /* We need to adjust the position of the surface
2548 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002549 cposx = surface->geometry.x + cx;
2550 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002551 dposx = rotate->center.x - cposx;
2552 dposy = rotate->center.y - cposy;
2553 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002554 weston_surface_set_position(surface,
2555 surface->geometry.x + dposx,
2556 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002557 }
2558
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002559 /* Repaint implies weston_surface_update_transform(), which
2560 * lazily applies the damage due to rotation update.
2561 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002562 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002563}
2564
2565static void
Scott Moreau447013d2012-02-18 05:05:29 -07002566rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002567 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002568{
2569 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002570 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002571 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002572 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002573 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002574
Daniel Stone4dbadb12012-05-30 16:31:51 +01002575 if (pointer->button_count == 0 &&
2576 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002577 if (shsurf)
2578 weston_matrix_multiply(&shsurf->rotation.rotation,
2579 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002580 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002581 free(rotate);
2582 }
2583}
2584
Scott Moreau447013d2012-02-18 05:05:29 -07002585static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002586 noop_grab_focus,
2587 rotate_grab_motion,
2588 rotate_grab_button,
2589};
2590
2591static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002592rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002593 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002594{
2595 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002596 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002597 struct shell_surface *surface;
2598 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002599 float dx, dy;
2600 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002601
2602 if (base_surface == NULL)
2603 return;
2604
2605 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002606 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002607 return;
2608
Pekka Paalanen460099f2012-01-20 16:48:25 +02002609 rotate = malloc(sizeof *rotate);
2610 if (!rotate)
2611 return;
2612
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002613 weston_surface_to_global_float(surface->surface,
2614 surface->surface->geometry.width / 2,
2615 surface->surface->geometry.height / 2,
2616 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002617
Daniel Stone37816df2012-05-16 18:45:18 +01002618 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2619 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002620 r = sqrtf(dx * dx + dy * dy);
2621 if (r > 20.0f) {
2622 struct weston_matrix inverse;
2623
2624 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002625 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002626 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002627
2628 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002629 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002630 } else {
2631 weston_matrix_init(&surface->rotation.rotation);
2632 weston_matrix_init(&rotate->rotation);
2633 }
2634
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002635 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2636 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002637}
2638
2639static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002640lower_fullscreen_layer(struct desktop_shell *shell)
2641{
2642 struct workspace *ws;
2643 struct weston_surface *surface, *prev;
2644
2645 ws = get_current_workspace(shell);
2646 wl_list_for_each_reverse_safe(surface, prev,
2647 &shell->fullscreen_layer.surface_list,
2648 layer_link)
2649 weston_surface_restack(surface, &ws->layer.surface_list);
2650}
2651
2652static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002653activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002654 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002655{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002656 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002657 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002658
Daniel Stone37816df2012-05-16 18:45:18 +01002659 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002660
Jonas Ådahl8538b222012-08-29 22:13:03 +02002661 state = ensure_focus_state(shell, seat);
2662 if (state == NULL)
2663 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002664
2665 state->keyboard_focus = es;
2666 wl_list_remove(&state->surface_destroy_listener.link);
2667 wl_signal_add(&es->surface.resource.destroy_signal,
2668 &state->surface_destroy_listener);
2669
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002670 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002671 case SHELL_SURFACE_FULLSCREEN:
2672 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002673 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002674 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002675 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002676 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002677 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002678 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002679 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002680 }
2681}
2682
Alex Wu21858432012-04-01 20:13:08 +08002683/* no-op func for checking black surface */
2684static void
2685black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2686{
2687}
2688
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002689static bool
Alex Wu21858432012-04-01 20:13:08 +08002690is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2691{
2692 if (es->configure == black_surface_configure) {
2693 if (fs_surface)
2694 *fs_surface = (struct weston_surface *)es->private;
2695 return true;
2696 }
2697 return false;
2698}
2699
Kristian Høgsberg75840622011-09-06 13:48:16 -04002700static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002701click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002702 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002703{
Daniel Stone37816df2012-05-16 18:45:18 +01002704 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002705 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002706 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002707 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002708
Daniel Stone37816df2012-05-16 18:45:18 +01002709 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002710 if (!focus)
2711 return;
2712
Alex Wu21858432012-04-01 20:13:08 +08002713 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002714 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002715
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002716 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2717 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002718
Daniel Stone325fc2d2012-05-30 16:31:58 +01002719 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002720 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002721}
2722
2723static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002724lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002725{
Tiago Vignattibe143262012-04-16 17:31:41 +03002726 struct desktop_shell *shell =
2727 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002728 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002729 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002730
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002731 if (shell->locked) {
2732 wl_list_for_each(output, &shell->compositor->output_list, link)
2733 /* TODO: find a way to jump to other DPMS levels */
2734 if (output->set_dpms)
2735 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002736 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002737 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002738
2739 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002740
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002741 /* Hide all surfaces by removing the fullscreen, panel and
2742 * toplevel layers. This way nothing else can show or receive
2743 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002744
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002745 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002746 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002747 if (shell->showing_input_panels)
2748 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002749 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002750 wl_list_insert(&shell->compositor->cursor_layer.link,
2751 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002752
Pekka Paalanen77346a62011-11-30 16:26:35 +02002753 launch_screensaver(shell);
2754
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002755 /* TODO: disable bindings that should not work while locked. */
2756
2757 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002758}
2759
2760static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002761unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002762{
Tiago Vignattibe143262012-04-16 17:31:41 +03002763 struct desktop_shell *shell =
2764 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002765
Pekka Paalanend81c2162011-11-16 13:47:34 +02002766 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002767 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002768 return;
2769 }
2770
2771 /* If desktop-shell client has gone away, unlock immediately. */
2772 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002773 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002774 return;
2775 }
2776
2777 if (shell->prepare_event_sent)
2778 return;
2779
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002780 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002781 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002782}
2783
2784static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002785show_input_panels(struct wl_listener *listener, void *data)
2786{
2787 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002788 container_of(listener, struct desktop_shell,
2789 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002790 struct input_panel_surface *surface, *next;
2791 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002792
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002793 shell->showing_input_panels = true;
2794
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002795 if (!shell->locked)
2796 wl_list_insert(&shell->panel_layer.link,
2797 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002798
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002799 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002800 &shell->input_panel.surfaces, link) {
2801 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002802 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002803 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002804 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002805 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002806 weston_surface_damage(ws);
2807 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002808 }
2809}
2810
2811static void
2812hide_input_panels(struct wl_listener *listener, void *data)
2813{
2814 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002815 container_of(listener, struct desktop_shell,
2816 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002817 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002818
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002819 shell->showing_input_panels = false;
2820
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002821 if (!shell->locked)
2822 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002823
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002824 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002825 &shell->input_panel_layer.surface_list, layer_link)
2826 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002827}
2828
2829static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002830center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002831{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002832 int32_t width = weston_surface_buffer_width(surface);
2833 int32_t height = weston_surface_buffer_height(surface);
2834 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002835
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002836 x = output->x + (output->width - width) / 2;
2837 y = output->y + (output->height - height) / 2;
2838
2839 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002840}
2841
2842static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002843weston_surface_set_initial_position (struct weston_surface *surface,
2844 struct desktop_shell *shell)
2845{
2846 struct weston_compositor *compositor = shell->compositor;
2847 int ix = 0, iy = 0;
2848 int range_x, range_y;
2849 int dx, dy, x, y, panel_height;
2850 struct weston_output *output, *target_output = NULL;
2851 struct weston_seat *seat;
2852
2853 /* As a heuristic place the new window on the same output as the
2854 * pointer. Falling back to the output containing 0, 0.
2855 *
2856 * TODO: Do something clever for touch too?
2857 */
2858 wl_list_for_each(seat, &compositor->seat_list, link) {
2859 if (seat->has_pointer) {
2860 ix = wl_fixed_to_int(seat->pointer.x);
2861 iy = wl_fixed_to_int(seat->pointer.y);
2862 break;
2863 }
2864 }
2865
2866 wl_list_for_each(output, &compositor->output_list, link) {
2867 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2868 target_output = output;
2869 break;
2870 }
2871 }
2872
2873 if (!target_output) {
2874 weston_surface_set_position(surface, 10 + random() % 400,
2875 10 + random() % 400);
2876 return;
2877 }
2878
2879 /* Valid range within output where the surface will still be onscreen.
2880 * If this is negative it means that the surface is bigger than
2881 * output.
2882 */
2883 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002884 range_x = target_output->width - surface->geometry.width;
2885 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002886 surface->geometry.height;
2887
Rob Bradford4cb88c72012-08-13 15:18:44 +01002888 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002889 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002890 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002891 dx = 0;
2892
2893 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002894 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002895 else
2896 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002897
2898 x = target_output->x + dx;
2899 y = target_output->y + dy;
2900
2901 weston_surface_set_position (surface, x, y);
2902}
2903
2904static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002905map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002906 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002907{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002908 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002909 struct shell_surface *shsurf = get_shell_surface(surface);
2910 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002911 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002912 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002913 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002914 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002915
Pekka Paalanen60921e52012-01-25 15:55:43 +02002916 surface->geometry.width = width;
2917 surface->geometry.height = height;
2918 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002919
2920 /* initial positioning, see also configure() */
2921 switch (surface_type) {
2922 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002923 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002924 break;
Alex Wu4539b082012-03-01 12:57:46 +08002925 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002926 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002927 shell_map_fullscreen(shsurf);
2928 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002929 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002930 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002931 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002932 weston_surface_set_position(surface, shsurf->output->x,
2933 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002934 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002935 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002936 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00002937 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002938 case SHELL_SURFACE_NONE:
2939 weston_surface_set_position(surface,
2940 surface->geometry.x + sx,
2941 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002942 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002943 default:
2944 ;
2945 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002946
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002947 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002948 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002949 case SHELL_SURFACE_POPUP:
2950 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002951 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002952 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2953 break;
Alex Wu4539b082012-03-01 12:57:46 +08002954 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002955 case SHELL_SURFACE_NONE:
2956 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002957 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002958 ws = get_current_workspace(shell);
2959 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002960 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002961 }
2962
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002963 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002964 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002965 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2966 surface->output = shsurf->output;
2967 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002968
Juan Zhao7bb92f02011-12-15 11:31:51 -05002969 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002970 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002971 if (shsurf->transient.flags ==
2972 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2973 break;
2974 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002975 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002976 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002977 if (!shell->locked) {
2978 wl_list_for_each(seat, &compositor->seat_list, link)
2979 activate(shell, surface, seat);
2980 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002981 break;
2982 default:
2983 break;
2984 }
2985
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002986 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002987 {
2988 switch (shell->win_animation_type) {
2989 case ANIMATION_FADE:
2990 weston_fade_run(surface, NULL, NULL);
2991 break;
2992 case ANIMATION_ZOOM:
2993 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2994 break;
2995 default:
2996 break;
2997 }
2998 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002999}
3000
3001static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003002configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003003 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003004{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003005 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3006 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003007
Pekka Paalanen77346a62011-11-30 16:26:35 +02003008 shsurf = get_shell_surface(surface);
3009 if (shsurf)
3010 surface_type = shsurf->type;
3011
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003012 surface->geometry.x = x;
3013 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02003014 surface->geometry.width = width;
3015 surface->geometry.height = height;
3016 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003017
3018 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003019 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003020 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003021 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003022 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003023 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003024 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003025 surface->geometry.x = surface->output->x;
3026 surface->geometry.y = surface->output->y +
3027 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003028 break;
Alex Wu4539b082012-03-01 12:57:46 +08003029 case SHELL_SURFACE_TOPLEVEL:
3030 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003031 default:
3032 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003033 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003034
Alex Wu4539b082012-03-01 12:57:46 +08003035 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003036 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003037 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003038
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003039 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003040 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003041 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003042}
3043
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003044static void
3045shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3046{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003047 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003048 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003049 int32_t width = weston_surface_buffer_width(es);
3050 int32_t height = weston_surface_buffer_height(es);
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003051 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003052
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003053 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003054 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003055 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003056 type_changed = 1;
3057 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003058
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003059 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003060 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003061 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003062 es->geometry.width != width ||
3063 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003064 float from_x, from_y;
3065 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003066
3067 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3068 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3069 configure(shell, es,
3070 es->geometry.x + to_x - from_x,
3071 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003072 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003073 }
3074}
3075
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003076static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003077
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003078static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003079desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003080{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003081 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003082 struct desktop_shell *shell =
3083 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003084
3085 shell->child.process.pid = 0;
3086 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003087
3088 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3089 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003090 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003091 shell->child.deathstamp = time;
3092 shell->child.deathcount = 0;
3093 }
3094
3095 shell->child.deathcount++;
3096 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003097 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003098 return;
3099 }
3100
Martin Minarik6d118362012-06-07 18:01:59 +02003101 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003102 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003103}
3104
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003105static void
3106launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003107{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003108 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003109 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003111 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003112 &shell->child.process,
3113 shell_exe,
3114 desktop_shell_sigchld);
3115
3116 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003117 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003118}
3119
3120static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003121bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3122{
Tiago Vignattibe143262012-04-16 17:31:41 +03003123 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003124
3125 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003126 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003127}
3128
Kristian Høgsberg75840622011-09-06 13:48:16 -04003129static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003130unbind_desktop_shell(struct wl_resource *resource)
3131{
Tiago Vignattibe143262012-04-16 17:31:41 +03003132 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003133
3134 if (shell->locked)
3135 resume_desktop(shell);
3136
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003137 shell->child.desktop_shell = NULL;
3138 shell->prepare_event_sent = false;
3139 free(resource);
3140}
3141
3142static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003143bind_desktop_shell(struct wl_client *client,
3144 void *data, uint32_t version, uint32_t id)
3145{
Tiago Vignattibe143262012-04-16 17:31:41 +03003146 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003147 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003148
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003149 resource = wl_client_add_object(client, &desktop_shell_interface,
3150 &desktop_shell_implementation,
3151 id, shell);
3152
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003153 if (client == shell->child.client) {
3154 resource->destroy = unbind_desktop_shell;
3155 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003156 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003157 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003158
3159 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3160 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003161 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003162}
3163
Pekka Paalanen6e168112011-11-24 11:34:05 +02003164static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003165screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3166{
3167 struct desktop_shell *shell = surface->private;
3168
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003169 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003170 if (!shell->locked)
3171 return;
3172
3173 center_on_output(surface, surface->output);
3174
3175 if (wl_list_empty(&surface->layer_link)) {
3176 wl_list_insert(shell->lock_layer.surface_list.prev,
3177 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003178 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003179 shell->compositor->idle_time = shell->screensaver.duration;
3180 weston_compositor_wake(shell->compositor);
3181 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3182 }
3183}
3184
3185static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003186screensaver_set_surface(struct wl_client *client,
3187 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003188 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003189 struct wl_resource *output_resource)
3190{
Tiago Vignattibe143262012-04-16 17:31:41 +03003191 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003192 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003193 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003194
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003195 surface->configure = screensaver_configure;
3196 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003197 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003198}
3199
3200static const struct screensaver_interface screensaver_implementation = {
3201 screensaver_set_surface
3202};
3203
3204static void
3205unbind_screensaver(struct wl_resource *resource)
3206{
Tiago Vignattibe143262012-04-16 17:31:41 +03003207 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003208
Pekka Paalanen77346a62011-11-30 16:26:35 +02003209 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003210 free(resource);
3211}
3212
3213static void
3214bind_screensaver(struct wl_client *client,
3215 void *data, uint32_t version, uint32_t id)
3216{
Tiago Vignattibe143262012-04-16 17:31:41 +03003217 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003218 struct wl_resource *resource;
3219
3220 resource = wl_client_add_object(client, &screensaver_interface,
3221 &screensaver_implementation,
3222 id, shell);
3223
Pekka Paalanen77346a62011-11-30 16:26:35 +02003224 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003225 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003226 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003227 return;
3228 }
3229
3230 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3231 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003232 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003233}
3234
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003235static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003236input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3237{
3238 struct weston_mode *mode = surface->output->current;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003239 int32_t width = weston_surface_buffer_width(surface);
3240 int32_t height = weston_surface_buffer_height(surface);
3241 float x = (mode->width - width) / 2;
3242 float y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003243
3244 /* Don't map the input panel here, wait for
3245 * show_input_panels signal. */
3246
3247 weston_surface_configure(surface,
3248 surface->output->x + x,
3249 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003250 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003251}
3252
3253static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003254destroy_input_panel_surface(struct wl_listener *listener,
3255 void *data)
3256{
3257 struct input_panel_surface *input_panel_surface =
3258 container_of(listener, struct input_panel_surface, listener);
3259
3260 wl_list_remove(&listener->link);
3261 wl_list_remove(&input_panel_surface->link);
3262
3263 free(input_panel_surface);
3264}
3265
3266static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003267input_panel_set_surface(struct wl_client *client,
3268 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003269 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003270 struct wl_resource *output_resource)
3271{
3272 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003273 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003274 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003275 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003276
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003277 surface->configure = input_panel_configure;
3278 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003279 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003280
3281 input_panel_surface = malloc(sizeof *input_panel_surface);
3282 if (!input_panel_surface) {
3283 wl_resource_post_no_memory(resource);
3284 return;
3285 }
3286
3287 input_panel_surface->surface = surface;
3288 input_panel_surface->listener.notify = destroy_input_panel_surface;
3289
3290 wl_signal_add(&surface_resource->destroy_signal,
3291 &input_panel_surface->listener);
3292
3293 wl_list_insert(&shell->input_panel.surfaces,
3294 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003295}
3296
3297static const struct input_panel_interface input_panel_implementation = {
3298 input_panel_set_surface
3299};
3300
3301static void
3302unbind_input_panel(struct wl_resource *resource)
3303{
3304 struct desktop_shell *shell = resource->data;
3305
3306 shell->input_panel.binding = NULL;
3307 free(resource);
3308}
3309
3310static void
3311bind_input_panel(struct wl_client *client,
3312 void *data, uint32_t version, uint32_t id)
3313{
3314 struct desktop_shell *shell = data;
3315 struct wl_resource *resource;
3316
3317 resource = wl_client_add_object(client, &input_panel_interface,
3318 &input_panel_implementation,
3319 id, shell);
3320
3321 if (shell->input_panel.binding == NULL) {
3322 resource->destroy = unbind_input_panel;
3323 shell->input_panel.binding = resource;
3324 return;
3325 }
3326
3327 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3328 "interface object already bound");
3329 wl_resource_destroy(resource);
3330}
3331
Kristian Høgsberg07045392012-02-19 18:52:44 -05003332struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003333 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003334 struct weston_surface *current;
3335 struct wl_listener listener;
3336 struct wl_keyboard_grab grab;
3337};
3338
3339static void
3340switcher_next(struct switcher *switcher)
3341{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003342 struct weston_surface *surface;
3343 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003344 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003345 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003346
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003347 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003348 switch (get_shell_surface_type(surface)) {
3349 case SHELL_SURFACE_TOPLEVEL:
3350 case SHELL_SURFACE_FULLSCREEN:
3351 case SHELL_SURFACE_MAXIMIZED:
3352 if (first == NULL)
3353 first = surface;
3354 if (prev == switcher->current)
3355 next = surface;
3356 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003357 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003358 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003359 weston_surface_damage(surface);
3360 break;
3361 default:
3362 break;
3363 }
Alex Wu1659daa2012-04-01 20:13:09 +08003364
3365 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003366 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003367 surface->geometry.dirty = 1;
3368 weston_surface_damage(surface);
3369 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003370 }
3371
3372 if (next == NULL)
3373 next = first;
3374
Alex Wu07b26062012-03-12 16:06:01 +08003375 if (next == NULL)
3376 return;
3377
Kristian Høgsberg07045392012-02-19 18:52:44 -05003378 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003379 wl_signal_add(&next->surface.resource.destroy_signal,
3380 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003381
3382 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003383 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003384
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003385 shsurf = get_shell_surface(switcher->current);
3386 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003387 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003388}
3389
3390static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003391switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003392{
3393 struct switcher *switcher =
3394 container_of(listener, struct switcher, listener);
3395
3396 switcher_next(switcher);
3397}
3398
3399static void
Daniel Stone351eb612012-05-31 15:27:47 -04003400switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003401{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003402 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003403 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003404 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003405
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003406 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003407 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003408 weston_surface_damage(surface);
3409 }
3410
Alex Wu07b26062012-03-12 16:06:01 +08003411 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003412 activate(switcher->shell, switcher->current,
3413 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003414 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003415 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003416 free(switcher);
3417}
3418
3419static void
3420switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003421 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003422{
3423 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003424 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003425
Daniel Stonec9785ea2012-05-30 16:31:52 +01003426 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003427 switcher_next(switcher);
3428}
3429
3430static void
3431switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3432 uint32_t mods_depressed, uint32_t mods_latched,
3433 uint32_t mods_locked, uint32_t group)
3434{
3435 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003436 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003437
Daniel Stone351eb612012-05-31 15:27:47 -04003438 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3439 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003440}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003441
3442static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003443 switcher_key,
3444 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003445};
3446
3447static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003448switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3449 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003450{
Tiago Vignattibe143262012-04-16 17:31:41 +03003451 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003452 struct switcher *switcher;
3453
3454 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003455 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003456 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003457 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003458 wl_list_init(&switcher->listener.link);
3459
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003460 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003461 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003462 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3463 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003464 switcher_next(switcher);
3465}
3466
Pekka Paalanen3c647232011-12-22 13:43:43 +02003467static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003468backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3469 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003470{
3471 struct weston_compositor *compositor = data;
3472 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003473 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003474
3475 /* TODO: we're limiting to simple use cases, where we assume just
3476 * control on the primary display. We'd have to extend later if we
3477 * ever get support for setting backlights on random desktop LCD
3478 * panels though */
3479 output = get_default_output(compositor);
3480 if (!output)
3481 return;
3482
3483 if (!output->set_backlight)
3484 return;
3485
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003486 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3487 backlight_new = output->backlight_current - 25;
3488 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3489 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003490
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003491 if (backlight_new < 5)
3492 backlight_new = 5;
3493 if (backlight_new > 255)
3494 backlight_new = 255;
3495
3496 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003497 output->set_backlight(output, output->backlight_current);
3498}
3499
3500static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003501fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3502 void *data)
3503{
3504 struct desktop_shell *shell = data;
3505 struct weston_compositor *compositor = shell->compositor;
3506 compositor->fan_debug = !compositor->fan_debug;
3507 weston_compositor_damage_all(compositor);
3508}
3509
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003510struct debug_binding_grab {
3511 struct wl_keyboard_grab grab;
3512 struct weston_seat *seat;
3513 uint32_t key[2];
3514 int key_released[2];
3515};
3516
3517static void
3518debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3519 uint32_t key, uint32_t state)
3520{
3521 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3522 struct wl_resource *resource;
3523 struct wl_display *display;
3524 uint32_t serial;
3525 int send = 0, terminate = 0;
3526 int check_binding = 1;
3527 int i;
3528
3529 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3530 /* Do not run bindings on key releases */
3531 check_binding = 0;
3532
3533 for (i = 0; i < 2; i++)
3534 if (key == db->key[i])
3535 db->key_released[i] = 1;
3536
3537 if (db->key_released[0] && db->key_released[1]) {
3538 /* All key releases been swalled so end the grab */
3539 terminate = 1;
3540 } else if (key != db->key[0] && key != db->key[1]) {
3541 /* Should not swallow release of other keys */
3542 send = 1;
3543 }
3544 } else if (key == db->key[0] && !db->key_released[0]) {
3545 /* Do not check bindings for the first press of the binding
3546 * key. This allows it to be used as a debug shortcut.
3547 * We still need to swallow this event. */
3548 check_binding = 0;
3549 } else if (db->key[1]) {
3550 /* If we already ran a binding don't process another one since
3551 * we can't keep track of all the binding keys that were
3552 * pressed in order to swallow the release events. */
3553 send = 1;
3554 check_binding = 0;
3555 }
3556
3557 if (check_binding) {
3558 struct weston_compositor *ec = db->seat->compositor;
3559
3560 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3561 key, state)) {
3562 /* We ran a binding so swallow the press and keep the
3563 * grab to swallow the released too. */
3564 send = 0;
3565 terminate = 0;
3566 db->key[1] = key;
3567 } else {
3568 /* Terminate the grab since the key pressed is not a
3569 * debug binding key. */
3570 send = 1;
3571 terminate = 1;
3572 }
3573 }
3574
3575 if (send) {
3576 resource = grab->keyboard->focus_resource;
3577
3578 if (resource) {
3579 display = wl_client_get_display(resource->client);
3580 serial = wl_display_next_serial(display);
3581 wl_keyboard_send_key(resource, serial, time, key, state);
3582 }
3583 }
3584
3585 if (terminate) {
3586 wl_keyboard_end_grab(grab->keyboard);
3587 free(db);
3588 }
3589}
3590
3591static void
3592debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3593 uint32_t mods_depressed, uint32_t mods_latched,
3594 uint32_t mods_locked, uint32_t group)
3595{
3596 struct wl_resource *resource;
3597
3598 resource = grab->keyboard->focus_resource;
3599 if (!resource)
3600 return;
3601
3602 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3603 mods_latched, mods_locked, group);
3604}
3605
3606struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3607 debug_binding_key,
3608 debug_binding_modifiers
3609};
3610
3611static void
3612debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3613{
3614 struct debug_binding_grab *grab;
3615
3616 grab = calloc(1, sizeof *grab);
3617 if (!grab)
3618 return;
3619
3620 grab->seat = (struct weston_seat *) seat;
3621 grab->key[0] = key;
3622 grab->grab.interface = &debug_binding_keyboard_grab;
3623 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3624}
3625
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003626static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003627force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3628 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003629{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003630 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003631 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003632 struct desktop_shell *shell = data;
3633 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003634 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003635
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003636 focus_surface = seat->keyboard->focus;
3637 if (!focus_surface)
3638 return;
3639
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003640 wl_signal_emit(&compositor->kill_signal, focus_surface);
3641
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003642 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003643 wl_client_get_credentials(client, &pid, NULL, NULL);
3644
3645 /* Skip clients that we launched ourselves (the credentials of
3646 * the socketpair is ours) */
3647 if (pid == getpid())
3648 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003649
Daniel Stone325fc2d2012-05-30 16:31:58 +01003650 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003651}
3652
3653static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003654workspace_up_binding(struct wl_seat *seat, uint32_t time,
3655 uint32_t key, void *data)
3656{
3657 struct desktop_shell *shell = data;
3658 unsigned int new_index = shell->workspaces.current;
3659
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003660 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003661 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003662 if (new_index != 0)
3663 new_index--;
3664
3665 change_workspace(shell, new_index);
3666}
3667
3668static void
3669workspace_down_binding(struct wl_seat *seat, uint32_t time,
3670 uint32_t key, void *data)
3671{
3672 struct desktop_shell *shell = data;
3673 unsigned int new_index = shell->workspaces.current;
3674
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003675 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003676 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003677 if (new_index < shell->workspaces.num - 1)
3678 new_index++;
3679
3680 change_workspace(shell, new_index);
3681}
3682
3683static void
3684workspace_f_binding(struct wl_seat *seat, uint32_t time,
3685 uint32_t key, void *data)
3686{
3687 struct desktop_shell *shell = data;
3688 unsigned int new_index;
3689
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003690 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003691 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003692 new_index = key - KEY_F1;
3693 if (new_index >= shell->workspaces.num)
3694 new_index = shell->workspaces.num - 1;
3695
3696 change_workspace(shell, new_index);
3697}
3698
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003699static void
3700workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3701 uint32_t key, void *data)
3702{
3703 struct desktop_shell *shell = data;
3704 unsigned int new_index = shell->workspaces.current;
3705
3706 if (shell->locked)
3707 return;
3708
3709 if (new_index != 0)
3710 new_index--;
3711
3712 take_surface_to_workspace_by_seat(shell, seat, new_index);
3713}
3714
3715static void
3716workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3717 uint32_t key, void *data)
3718{
3719 struct desktop_shell *shell = data;
3720 unsigned int new_index = shell->workspaces.current;
3721
3722 if (shell->locked)
3723 return;
3724
3725 if (new_index < shell->workspaces.num - 1)
3726 new_index++;
3727
3728 take_surface_to_workspace_by_seat(shell, seat, new_index);
3729}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003730
3731static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003732shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003733{
Tiago Vignattibe143262012-04-16 17:31:41 +03003734 struct desktop_shell *shell =
3735 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003736 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003737
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003738 if (shell->child.client)
3739 wl_client_destroy(shell->child.client);
3740
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003741 wl_list_remove(&shell->lock_listener.link);
3742 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003743 wl_list_remove(&shell->show_input_panel_listener.link);
3744 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003745
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003746 wl_array_for_each(ws, &shell->workspaces.array)
3747 workspace_destroy(*ws);
3748 wl_array_release(&shell->workspaces.array);
3749
Pekka Paalanen3c647232011-12-22 13:43:43 +02003750 free(shell->screensaver.path);
3751 free(shell);
3752}
3753
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003754static void
3755shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3756{
3757 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003758 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003759
3760 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003761 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3762 MODIFIER_CTRL | MODIFIER_ALT,
3763 terminate_binding, ec);
3764 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3765 click_to_activate_binding,
3766 shell);
3767 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3768 MODIFIER_SUPER | MODIFIER_ALT,
3769 surface_opacity_binding, NULL);
3770 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3771 MODIFIER_SUPER, zoom_axis_binding,
3772 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003773
3774 /* configurable bindings */
3775 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003776 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3777 zoom_key_binding, NULL);
3778 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3779 zoom_key_binding, NULL);
3780 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3781 shell);
3782 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3783 resize_binding, shell);
3784 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3785 rotate_binding, NULL);
3786 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3787 shell);
3788 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3789 ec);
3790 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3791 backlight_binding, ec);
3792 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3793 ec);
3794 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3795 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003796 weston_compositor_add_key_binding(ec, KEY_K, mod,
3797 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003798 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3799 workspace_up_binding, shell);
3800 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3801 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003802 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3803 workspace_move_surface_up_binding,
3804 shell);
3805 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3806 workspace_move_surface_down_binding,
3807 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003808
3809 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3810 if (shell->workspaces.num > 1) {
3811 num_workspace_bindings = shell->workspaces.num;
3812 if (num_workspace_bindings > 6)
3813 num_workspace_bindings = 6;
3814 for (i = 0; i < num_workspace_bindings; i++)
3815 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3816 workspace_f_binding,
3817 shell);
3818 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003819
3820 /* Debug bindings */
3821 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3822 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003823 weston_compositor_add_debug_binding(ec, KEY_F,
3824 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003825}
3826
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003827WL_EXPORT int
Kristian Høgsbergb00a9d32012-09-11 14:06:27 -04003828module_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003829{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003830 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003831 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003832 struct workspace **pws;
3833 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003834 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003835
3836 shell = malloc(sizeof *shell);
3837 if (shell == NULL)
3838 return -1;
3839
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003840 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003841 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003842
3843 shell->destroy_listener.notify = shell_destroy;
3844 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3845 shell->lock_listener.notify = lock;
3846 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3847 shell->unlock_listener.notify = unlock;
3848 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003849 shell->show_input_panel_listener.notify = show_input_panels;
3850 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3851 shell->hide_input_panel_listener.notify = hide_input_panels;
3852 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003853 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003854 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003855 ec->shell_interface.create_shell_surface = create_shell_surface;
3856 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003857 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003858 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003859 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003860 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003861
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003862 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003863
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003864 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3865 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003866 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3867 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003868 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003869
3870 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02003871 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003872
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003873 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003874
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003875 for (i = 0; i < shell->workspaces.num; i++) {
3876 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3877 if (pws == NULL)
3878 return -1;
3879
3880 *pws = workspace_create();
3881 if (*pws == NULL)
3882 return -1;
3883 }
3884 activate_workspace(shell, 0);
3885
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003886 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003887 wl_list_init(&shell->workspaces.animation.link);
3888 shell->workspaces.animation.frame = animate_workspace_change_frame;
3889
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003890 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3891 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003892 return -1;
3893
Kristian Høgsberg75840622011-09-06 13:48:16 -04003894 if (wl_display_add_global(ec->wl_display,
3895 &desktop_shell_interface,
3896 shell, bind_desktop_shell) == NULL)
3897 return -1;
3898
Pekka Paalanen6e168112011-11-24 11:34:05 +02003899 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3900 shell, bind_screensaver) == NULL)
3901 return -1;
3902
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003903 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3904 shell, bind_input_panel) == NULL)
3905 return -1;
3906
Jonas Ådahle9d22502012-08-29 22:13:01 +02003907 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3908 shell, bind_workspace_manager) == NULL)
3909 return -1;
3910
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003911 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003912
3913 loop = wl_display_get_event_loop(ec->wl_display);
3914 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003915
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003916 wl_list_for_each(seat, &ec->seat_list, link)
3917 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003918
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003919 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003920
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003921 return 0;
3922}