blob: 39987d5895b9d43a50b07d37a0ab39a5fcbb1b5a [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020038#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020039#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020042#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043
Juan Zhaoe10d2792012-04-25 19:09:52 +080044enum animation_type {
45 ANIMATION_NONE,
46
47 ANIMATION_ZOOM,
48 ANIMATION_FADE
49};
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040053 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020054 struct weston_surface *keyboard_focus;
55 struct wl_list link;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
58};
59
Jonas Ådahle3cddce2012-06-13 00:01:22 +020060struct workspace {
61 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020062
63 struct wl_list focus_list;
64 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020065};
66
Philipp Brüschweiler88013572012-08-06 13:44:42 +020067struct input_panel_surface {
68 struct wl_list link;
69 struct weston_surface *surface;
70 struct wl_listener listener;
71};
72
Tiago Vignattibe143262012-04-16 17:31:41 +030073struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050074 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040075
76 struct wl_listener lock_listener;
77 struct wl_listener unlock_listener;
78 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020079 struct wl_listener show_input_panel_listener;
80 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020081
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050082 struct weston_layer fullscreen_layer;
83 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050084 struct weston_layer background_layer;
85 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +020086 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050087
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040088 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030089 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040090
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020091 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050092 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020093 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020094 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020095
96 unsigned deathcount;
97 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020098 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020099
100 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200101 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200102 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200103
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400104 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500105 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100106
Pekka Paalanen77346a62011-11-30 16:26:35 +0200107 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200108 struct wl_array array;
109 unsigned int current;
110 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200111
Jonas Ådahle9d22502012-08-29 22:13:01 +0200112 struct wl_list client_list;
113
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200114 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200115 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200116 int anim_dir;
117 uint32_t anim_timestamp;
118 double anim_current;
119 struct workspace *anim_from;
120 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200121 } workspaces;
122
123 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200124 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200125 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200126 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500127 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200128 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500129
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200130 struct {
131 struct wl_resource *binding;
132 struct wl_list surfaces;
133 } input_panel;
134
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300135 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800136 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400137};
138
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500139enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200140 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500141 SHELL_SURFACE_TOPLEVEL,
142 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800144 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200146};
147
Scott Moreauff1db4a2012-04-17 19:06:18 -0600148struct ping_timer {
149 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600150 uint32_t serial;
151};
152
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200153struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200154 struct wl_resource resource;
155
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200157 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400158 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300159 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200160
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400161 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400162 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500163 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800164 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800165 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600166 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100167
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500168 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200169 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500170 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200171 } rotation;
172
173 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700174 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500175 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200176 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500177 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100178 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400179 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500180 } popup;
181
Alex Wu4539b082012-03-01 12:57:46 +0800182 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300183 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400184 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300185 } transient;
186
187 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800188 enum wl_shell_surface_fullscreen_method type;
189 struct weston_transform transform; /* matrix from x, y */
190 uint32_t framerate;
191 struct weston_surface *black_surface;
192 } fullscreen;
193
Scott Moreauff1db4a2012-04-17 19:06:18 -0600194 struct ping_timer *ping_timer;
195
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200196 struct weston_transform workspace_transform;
197
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500198 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500199 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100200 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400201
202 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200203};
204
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700206 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207 struct shell_surface *shsurf;
208 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300209 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210};
211
212struct weston_move_grab {
213 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100214 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500215};
216
Pekka Paalanen460099f2012-01-20 16:48:25 +0200217struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300218 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500219 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200220 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200221 float x;
222 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200223 } center;
224};
225
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400226static void
227activate(struct desktop_shell *shell, struct weston_surface *es,
228 struct weston_seat *seat);
229
230static struct workspace *
231get_current_workspace(struct desktop_shell *shell);
232
Alex Wubd3354b2012-04-17 17:20:49 +0800233static struct shell_surface *
234get_shell_surface(struct weston_surface *surface);
235
236static struct desktop_shell *
237shell_surface_get_shell(struct shell_surface *shsurf);
238
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500239static void
240surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
241
Alex Wubd3354b2012-04-17 17:20:49 +0800242static bool
243shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
244{
245 struct desktop_shell *shell;
246 struct weston_surface *top_fs_es;
247
248 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100249
Alex Wubd3354b2012-04-17 17:20:49 +0800250 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
251 return false;
252
253 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100254 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800255 layer_link);
256 return (shsurf == get_shell_surface(top_fs_es));
257}
258
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500259static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400260destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300261{
262 struct shell_grab *grab;
263
264 grab = container_of(listener, struct shell_grab,
265 shsurf_destroy_listener);
266
267 grab->shsurf = NULL;
268}
269
270static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400271popup_grab_end(struct wl_pointer *pointer);
272
273static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300274shell_grab_start(struct shell_grab *grab,
275 const struct wl_pointer_grab_interface *interface,
276 struct shell_surface *shsurf,
277 struct wl_pointer *pointer,
278 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300280 struct desktop_shell *shell = shsurf->shell;
281
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400282 popup_grab_end(pointer);
283
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284 grab->grab.interface = interface;
285 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400286 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
287 wl_signal_add(&shsurf->resource.destroy_signal,
288 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300289
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300290 grab->pointer = pointer;
291 grab->grab.focus = &shsurf->surface->surface;
292
293 wl_pointer_start_grab(pointer, &grab->grab);
294 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
295 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
296 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300297}
298
299static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300300shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300301{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400302 if (grab->shsurf)
303 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300304
305 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306}
307
308static void
Alex Wu4539b082012-03-01 12:57:46 +0800309center_on_output(struct weston_surface *surface,
310 struct weston_output *output);
311
Daniel Stone496ca172012-05-30 16:31:42 +0100312static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300313get_modifier(char *modifier)
314{
315 if (!modifier)
316 return MODIFIER_SUPER;
317
318 if (!strcmp("ctrl", modifier))
319 return MODIFIER_CTRL;
320 else if (!strcmp("alt", modifier))
321 return MODIFIER_ALT;
322 else if (!strcmp("super", modifier))
323 return MODIFIER_SUPER;
324 else
325 return MODIFIER_SUPER;
326}
327
Juan Zhaoe10d2792012-04-25 19:09:52 +0800328static enum animation_type
329get_animation_type(char *animation)
330{
331 if (!animation)
332 return ANIMATION_NONE;
333
334 if (!strcmp("zoom", animation))
335 return ANIMATION_ZOOM;
336 else if (!strcmp("fade", animation))
337 return ANIMATION_FADE;
338 else
339 return ANIMATION_NONE;
340}
341
Alex Wu4539b082012-03-01 12:57:46 +0800342static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300343shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200344{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200345 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200346 char *path = NULL;
347 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200348 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300349 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800350 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200351
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400352 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300353 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800354 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200355 { "num-workspaces",
356 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200357 };
358
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400359 struct config_key saver_keys[] = {
360 { "path", CONFIG_KEY_STRING, &path },
361 { "duration", CONFIG_KEY_INTEGER, &duration },
362 };
363
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200364 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400365 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200366 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
367 };
368
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200369 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500370 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200371 free(config_file);
372
Pekka Paalanen7296e792011-12-07 16:22:00 +0200373 shell->screensaver.path = path;
374 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300375 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800376 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200377 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
378}
379
380static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200381focus_state_destroy(struct focus_state *state)
382{
383 wl_list_remove(&state->seat_destroy_listener.link);
384 wl_list_remove(&state->surface_destroy_listener.link);
385 free(state);
386}
387
388static void
389focus_state_seat_destroy(struct wl_listener *listener, void *data)
390{
391 struct focus_state *state = container_of(listener,
392 struct focus_state,
393 seat_destroy_listener);
394
395 wl_list_remove(&state->link);
396 focus_state_destroy(state);
397}
398
399static void
400focus_state_surface_destroy(struct wl_listener *listener, void *data)
401{
402 struct focus_state *state = container_of(listener,
403 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400404 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400405 struct desktop_shell *shell;
406 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200407
Kristian Høgsberge3778222012-07-31 17:29:30 -0400408 next = NULL;
409 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
410 if (surface == state->keyboard_focus)
411 continue;
412
413 next = surface;
414 break;
415 }
416
417 if (next) {
418 shell = state->seat->compositor->shell_interface.shell;
419 activate(shell, next, state->seat);
420 } else {
421 wl_list_remove(&state->link);
422 focus_state_destroy(state);
423 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200424}
425
426static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400427focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200428{
Jonas Ådahl04769742012-06-13 00:01:24 +0200429 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200430
431 state = malloc(sizeof *state);
432 if (state == NULL)
433 return NULL;
434
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400435 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200436 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400437 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200438
439 state->seat_destroy_listener.notify = focus_state_seat_destroy;
440 state->surface_destroy_listener.notify = focus_state_surface_destroy;
441 wl_signal_add(&seat->seat.destroy_signal,
442 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400443 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200444
445 return state;
446}
447
Jonas Ådahl8538b222012-08-29 22:13:03 +0200448static struct focus_state *
449ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
450{
451 struct workspace *ws = get_current_workspace(shell);
452 struct focus_state *state;
453
454 wl_list_for_each(state, &ws->focus_list, link)
455 if (state->seat == seat)
456 break;
457
458 if (&state->link == &ws->focus_list)
459 state = focus_state_create(seat, ws);
460
461 return state;
462}
463
Jonas Ådahl04769742012-06-13 00:01:24 +0200464static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400465restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200466{
467 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200468 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200469
470 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200471 surface = state->keyboard_focus ?
472 &state->keyboard_focus->surface : NULL;
473
474 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200475 }
476}
477
478static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200479replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
480 struct weston_seat *seat)
481{
482 struct focus_state *state;
483 struct wl_surface *surface;
484
485 wl_list_for_each(state, &ws->focus_list, link) {
486 if (state->seat == seat) {
487 surface = seat->seat.keyboard->focus;
488 state->keyboard_focus =
489 (struct weston_surface *) surface;
490 return;
491 }
492 }
493}
494
495static void
496drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
497 struct weston_surface *surface)
498{
499 struct focus_state *state;
500
501 wl_list_for_each(state, &ws->focus_list, link)
502 if (state->keyboard_focus == surface)
503 state->keyboard_focus = NULL;
504}
505
506static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200507workspace_destroy(struct workspace *ws)
508{
Jonas Ådahl04769742012-06-13 00:01:24 +0200509 struct focus_state *state, *next;
510
511 wl_list_for_each_safe(state, next, &ws->focus_list, link)
512 focus_state_destroy(state);
513
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200514 free(ws);
515}
516
Jonas Ådahl04769742012-06-13 00:01:24 +0200517static void
518seat_destroyed(struct wl_listener *listener, void *data)
519{
520 struct weston_seat *seat = data;
521 struct focus_state *state, *next;
522 struct workspace *ws = container_of(listener,
523 struct workspace,
524 seat_destroyed_listener);
525
526 wl_list_for_each_safe(state, next, &ws->focus_list, link)
527 if (state->seat == seat)
528 wl_list_remove(&state->link);
529}
530
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200531static struct workspace *
532workspace_create(void)
533{
534 struct workspace *ws = malloc(sizeof *ws);
535 if (ws == NULL)
536 return NULL;
537
538 weston_layer_init(&ws->layer, NULL);
539
Jonas Ådahl04769742012-06-13 00:01:24 +0200540 wl_list_init(&ws->focus_list);
541 wl_list_init(&ws->seat_destroyed_listener.link);
542 ws->seat_destroyed_listener.notify = seat_destroyed;
543
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200544 return ws;
545}
546
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200547static int
548workspace_is_empty(struct workspace *ws)
549{
550 return wl_list_empty(&ws->layer.surface_list);
551}
552
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200553static struct workspace *
554get_workspace(struct desktop_shell *shell, unsigned int index)
555{
556 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200557 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200558 pws += index;
559 return *pws;
560}
561
562static struct workspace *
563get_current_workspace(struct desktop_shell *shell)
564{
565 return get_workspace(shell, shell->workspaces.current);
566}
567
568static void
569activate_workspace(struct desktop_shell *shell, unsigned int index)
570{
571 struct workspace *ws;
572
573 ws = get_workspace(shell, index);
574 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
575
576 shell->workspaces.current = index;
577}
578
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200579static unsigned int
580get_output_height(struct weston_output *output)
581{
582 return abs(output->region.extents.y1 - output->region.extents.y2);
583}
584
585static void
586surface_translate(struct weston_surface *surface, double d)
587{
588 struct shell_surface *shsurf = get_shell_surface(surface);
589 struct weston_transform *transform;
590
591 transform = &shsurf->workspace_transform;
592 if (wl_list_empty(&transform->link))
593 wl_list_insert(surface->geometry.transformation_list.prev,
594 &shsurf->workspace_transform.link);
595
596 weston_matrix_init(&shsurf->workspace_transform.matrix);
597 weston_matrix_translate(&shsurf->workspace_transform.matrix,
598 0.0, d, 0.0);
599 surface->geometry.dirty = 1;
600}
601
602static void
603workspace_translate_out(struct workspace *ws, double fraction)
604{
605 struct weston_surface *surface;
606 unsigned int height;
607 double d;
608
609 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
610 height = get_output_height(surface->output);
611 d = height * fraction;
612
613 surface_translate(surface, d);
614 }
615}
616
617static void
618workspace_translate_in(struct workspace *ws, double fraction)
619{
620 struct weston_surface *surface;
621 unsigned int height;
622 double d;
623
624 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
625 height = get_output_height(surface->output);
626
627 if (fraction > 0)
628 d = -(height - height * fraction);
629 else
630 d = height + height * fraction;
631
632 surface_translate(surface, d);
633 }
634}
635
636static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200637broadcast_current_workspace_state(struct desktop_shell *shell)
638{
639 struct wl_resource *resource;
640
641 wl_list_for_each(resource, &shell->workspaces.client_list, link)
642 workspace_manager_send_state(resource,
643 shell->workspaces.current,
644 shell->workspaces.num);
645}
646
647static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200648reverse_workspace_change_animation(struct desktop_shell *shell,
649 unsigned int index,
650 struct workspace *from,
651 struct workspace *to)
652{
653 shell->workspaces.current = index;
654
655 shell->workspaces.anim_to = to;
656 shell->workspaces.anim_from = from;
657 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
658 shell->workspaces.anim_timestamp = 0;
659
Scott Moreau4272e632012-08-13 09:58:41 -0600660 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200661}
662
663static void
664workspace_deactivate_transforms(struct workspace *ws)
665{
666 struct weston_surface *surface;
667 struct shell_surface *shsurf;
668
669 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
670 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200671 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
672 wl_list_remove(&shsurf->workspace_transform.link);
673 wl_list_init(&shsurf->workspace_transform.link);
674 }
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200675 shsurf->surface->geometry.dirty = 1;
676 }
677}
678
679static void
680finish_workspace_change_animation(struct desktop_shell *shell,
681 struct workspace *from,
682 struct workspace *to)
683{
Scott Moreau4272e632012-08-13 09:58:41 -0600684 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200685
686 wl_list_remove(&shell->workspaces.animation.link);
687 workspace_deactivate_transforms(from);
688 workspace_deactivate_transforms(to);
689 shell->workspaces.anim_to = NULL;
690
691 wl_list_remove(&shell->workspaces.anim_from->layer.link);
692}
693
694static void
695animate_workspace_change_frame(struct weston_animation *animation,
696 struct weston_output *output, uint32_t msecs)
697{
698 struct desktop_shell *shell =
699 container_of(animation, struct desktop_shell,
700 workspaces.animation);
701 struct workspace *from = shell->workspaces.anim_from;
702 struct workspace *to = shell->workspaces.anim_to;
703 uint32_t t;
704 double x, y;
705
706 if (workspace_is_empty(from) && workspace_is_empty(to)) {
707 finish_workspace_change_animation(shell, from, to);
708 return;
709 }
710
711 if (shell->workspaces.anim_timestamp == 0) {
712 if (shell->workspaces.anim_current == 0.0)
713 shell->workspaces.anim_timestamp = msecs;
714 else
715 shell->workspaces.anim_timestamp =
716 msecs -
717 /* Invers of movement function 'y' below. */
718 (asin(1.0 - shell->workspaces.anim_current) *
719 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
720 M_2_PI);
721 }
722
723 t = msecs - shell->workspaces.anim_timestamp;
724
725 /*
726 * x = [0, π/2]
727 * y(x) = sin(x)
728 */
729 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
730 y = sin(x);
731
732 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600733 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200734
735 workspace_translate_out(from, shell->workspaces.anim_dir * y);
736 workspace_translate_in(to, shell->workspaces.anim_dir * y);
737 shell->workspaces.anim_current = y;
738
Scott Moreau4272e632012-08-13 09:58:41 -0600739 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200740 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200741 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200742 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200743}
744
745static void
746animate_workspace_change(struct desktop_shell *shell,
747 unsigned int index,
748 struct workspace *from,
749 struct workspace *to)
750{
751 struct weston_output *output;
752
753 int dir;
754
755 if (index > shell->workspaces.current)
756 dir = -1;
757 else
758 dir = 1;
759
760 shell->workspaces.current = index;
761
762 shell->workspaces.anim_dir = dir;
763 shell->workspaces.anim_from = from;
764 shell->workspaces.anim_to = to;
765 shell->workspaces.anim_current = 0.0;
766 shell->workspaces.anim_timestamp = 0;
767
768 output = container_of(shell->compositor->output_list.next,
769 struct weston_output, link);
770 wl_list_insert(&output->animation_list,
771 &shell->workspaces.animation.link);
772
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200773 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200774
775 workspace_translate_in(to, 0);
776
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400777 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200778
Scott Moreau4272e632012-08-13 09:58:41 -0600779 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200780}
781
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200782static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200783update_workspace(struct desktop_shell *shell, unsigned int index,
784 struct workspace *from, struct workspace *to)
785{
786 shell->workspaces.current = index;
787 wl_list_insert(&from->layer.link, &to->layer.link);
788 wl_list_remove(&from->layer.link);
789}
790
791static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200792change_workspace(struct desktop_shell *shell, unsigned int index)
793{
794 struct workspace *from;
795 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200796
797 if (index == shell->workspaces.current)
798 return;
799
800 /* Don't change workspace when there is any fullscreen surfaces. */
801 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
802 return;
803
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200804 from = get_current_workspace(shell);
805 to = get_workspace(shell, index);
806
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200807 if (shell->workspaces.anim_from == to &&
808 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200809 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200810 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200811 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200812 return;
813 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200814
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200815 if (shell->workspaces.anim_to != NULL)
816 finish_workspace_change_animation(shell,
817 shell->workspaces.anim_from,
818 shell->workspaces.anim_to);
819
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200820 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200821
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200822 if (workspace_is_empty(to) && workspace_is_empty(from))
823 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200824 else
825 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200826
827 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200828}
829
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200830static bool
831workspace_has_only(struct workspace *ws, struct weston_surface *surface)
832{
833 struct wl_list *list = &ws->layer.surface_list;
834 struct wl_list *e;
835
836 if (wl_list_empty(list))
837 return false;
838
839 e = list->next;
840
841 if (e->next != list)
842 return false;
843
844 return container_of(e, struct weston_surface, layer_link) == surface;
845}
846
847static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200848move_surface_to_workspace(struct desktop_shell *shell,
849 struct weston_surface *surface,
850 uint32_t workspace)
851{
852 struct workspace *from;
853 struct workspace *to;
854 struct weston_seat *seat;
855
856 if (workspace == shell->workspaces.current)
857 return;
858
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200859 if (workspace >= shell->workspaces.num)
860 workspace = shell->workspaces.num - 1;
861
Jonas Ådahle9d22502012-08-29 22:13:01 +0200862 from = get_current_workspace(shell);
863 to = get_workspace(shell, workspace);
864
865 wl_list_remove(&surface->layer_link);
866 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
867
868 drop_focus_state(shell, from, surface);
869 wl_list_for_each(seat, &shell->compositor->seat_list, link)
870 if (seat->has_keyboard &&
871 seat->keyboard.focus == &surface->surface)
872 wl_keyboard_set_focus(&seat->keyboard, NULL);
873
874 weston_surface_damage_below(surface);
875}
876
877static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200878take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200879 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200880 unsigned int index)
881{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200882 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200883 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200884 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200885 struct shell_surface *shsurf;
886 struct workspace *from;
887 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200888 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200889
890 if (surface == NULL ||
891 index == shell->workspaces.current)
892 return;
893
894 from = get_current_workspace(shell);
895 to = get_workspace(shell, index);
896
897 wl_list_remove(&surface->layer_link);
898 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
899
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200901 drop_focus_state(shell, from, surface);
902
903 if (shell->workspaces.anim_from == to &&
904 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200905 wl_list_remove(&to->layer.link);
906 wl_list_insert(from->layer.link.prev, &to->layer.link);
907
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200908 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200909 broadcast_current_workspace_state(shell);
910
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200911 return;
912 }
913
914 if (shell->workspaces.anim_to != NULL)
915 finish_workspace_change_animation(shell,
916 shell->workspaces.anim_from,
917 shell->workspaces.anim_to);
918
919 if (workspace_is_empty(from) &&
920 workspace_has_only(to, surface))
921 update_workspace(shell, index, from, to);
922 else {
923 shsurf = get_shell_surface(surface);
924 if (wl_list_empty(&shsurf->workspace_transform.link))
925 wl_list_insert(&shell->workspaces.anim_sticky_list,
926 &shsurf->workspace_transform.link);
927
928 animate_workspace_change(shell, index, from, to);
929 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200930
931 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200932
933 state = ensure_focus_state(shell, seat);
934 if (state != NULL)
935 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200936}
937
938static void
939workspace_manager_move_surface(struct wl_client *client,
940 struct wl_resource *resource,
941 struct wl_resource *surface_resource,
942 uint32_t workspace)
943{
944 struct desktop_shell *shell = resource->data;
945 struct weston_surface *surface =
946 (struct weston_surface *) surface_resource;
947
948 move_surface_to_workspace(shell, surface, workspace);
949}
950
951static const struct workspace_manager_interface workspace_manager_implementation = {
952 workspace_manager_move_surface,
953};
954
955static void
956unbind_resource(struct wl_resource *resource)
957{
958 wl_list_remove(&resource->link);
959 free(resource);
960}
961
962static void
963bind_workspace_manager(struct wl_client *client,
964 void *data, uint32_t version, uint32_t id)
965{
966 struct desktop_shell *shell = data;
967 struct wl_resource *resource;
968
969 resource = wl_client_add_object(client, &workspace_manager_interface,
970 &workspace_manager_implementation,
971 id, shell);
972
973 if (resource == NULL) {
974 weston_log("couldn't add workspace manager object");
975 return;
976 }
977
978 resource->destroy = unbind_resource;
979 wl_list_insert(&shell->workspaces.client_list, &resource->link);
980
981 workspace_manager_send_state(resource,
982 shell->workspaces.current,
983 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200984}
985
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200986static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400987noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100988 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500989{
990 grab->focus = NULL;
991}
992
993static void
Scott Moreau447013d2012-02-18 05:05:29 -0700994move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100995 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500996{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500997 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100998 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300999 struct shell_surface *shsurf = move->base.shsurf;
1000 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001001 int dx = wl_fixed_to_int(pointer->x + move->dx);
1002 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001003
1004 if (!shsurf)
1005 return;
1006
1007 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001008
Daniel Stone103db7f2012-05-08 17:17:55 +01001009 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001010 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001011
1012 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001013}
1014
1015static void
Scott Moreau447013d2012-02-18 05:05:29 -07001016move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001017 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001018{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001019 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1020 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001021 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001022 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023
Daniel Stone4dbadb12012-05-30 16:31:51 +01001024 if (pointer->button_count == 0 &&
1025 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001026 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001027 free(grab);
1028 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001029}
1030
Scott Moreau447013d2012-02-18 05:05:29 -07001031static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001032 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001033 move_grab_motion,
1034 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001035};
1036
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001037static int
1038surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1039{
1040 struct weston_move_grab *move;
1041
1042 if (!shsurf)
1043 return -1;
1044
1045 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1046 return 0;
1047
1048 move = malloc(sizeof *move);
1049 if (!move)
1050 return -1;
1051
1052 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1053 ws->seat.pointer->grab_x;
1054 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1055 ws->seat.pointer->grab_y;
1056
1057 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1058 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1059
1060 return 0;
1061}
1062
1063static void
1064shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1065 struct wl_resource *seat_resource, uint32_t serial)
1066{
1067 struct weston_seat *ws = seat_resource->data;
1068 struct shell_surface *shsurf = resource->data;
1069
1070 if (ws->seat.pointer->button_count == 0 ||
1071 ws->seat.pointer->grab_serial != serial ||
1072 ws->seat.pointer->focus != &shsurf->surface->surface)
1073 return;
1074
1075 if (surface_move(shsurf, ws) < 0)
1076 wl_resource_post_no_memory(resource);
1077}
1078
1079struct weston_resize_grab {
1080 struct shell_grab base;
1081 uint32_t edges;
1082 int32_t width, height;
1083};
1084
1085static void
1086resize_grab_motion(struct wl_pointer_grab *grab,
1087 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1088{
1089 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1090 struct wl_pointer *pointer = grab->pointer;
1091 struct shell_surface *shsurf = resize->base.shsurf;
1092 int32_t width, height;
1093 wl_fixed_t from_x, from_y;
1094 wl_fixed_t to_x, to_y;
1095
1096 if (!shsurf)
1097 return;
1098
1099 weston_surface_from_global_fixed(shsurf->surface,
1100 pointer->grab_x, pointer->grab_y,
1101 &from_x, &from_y);
1102 weston_surface_from_global_fixed(shsurf->surface,
1103 pointer->x, pointer->y, &to_x, &to_y);
1104
1105 width = resize->width;
1106 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1107 width += wl_fixed_to_int(from_x - to_x);
1108 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1109 width += wl_fixed_to_int(to_x - from_x);
1110 }
1111
1112 height = resize->height;
1113 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1114 height += wl_fixed_to_int(from_y - to_y);
1115 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1116 height += wl_fixed_to_int(to_y - from_y);
1117 }
1118
1119 shsurf->client->send_configure(shsurf->surface,
1120 resize->edges, width, height);
1121}
1122
1123static void
1124send_configure(struct weston_surface *surface,
1125 uint32_t edges, int32_t width, int32_t height)
1126{
1127 struct shell_surface *shsurf = get_shell_surface(surface);
1128
1129 wl_shell_surface_send_configure(&shsurf->resource,
1130 edges, width, height);
1131}
1132
1133static const struct weston_shell_client shell_client = {
1134 send_configure
1135};
1136
1137static void
1138resize_grab_button(struct wl_pointer_grab *grab,
1139 uint32_t time, uint32_t button, uint32_t state_w)
1140{
1141 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1142 struct wl_pointer *pointer = grab->pointer;
1143 enum wl_pointer_button_state state = state_w;
1144
1145 if (pointer->button_count == 0 &&
1146 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1147 shell_grab_end(&resize->base);
1148 free(grab);
1149 }
1150}
1151
1152static const struct wl_pointer_grab_interface resize_grab_interface = {
1153 noop_grab_focus,
1154 resize_grab_motion,
1155 resize_grab_button,
1156};
1157
1158static int
1159surface_resize(struct shell_surface *shsurf,
1160 struct weston_seat *ws, uint32_t edges)
1161{
1162 struct weston_resize_grab *resize;
1163
1164 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1165 return 0;
1166
1167 if (edges == 0 || edges > 15 ||
1168 (edges & 3) == 3 || (edges & 12) == 12)
1169 return 0;
1170
1171 resize = malloc(sizeof *resize);
1172 if (!resize)
1173 return -1;
1174
1175 resize->edges = edges;
1176 resize->width = shsurf->surface->geometry.width;
1177 resize->height = shsurf->surface->geometry.height;
1178
1179 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1180 ws->seat.pointer, edges);
1181
1182 return 0;
1183}
1184
1185static void
1186shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1187 struct wl_resource *seat_resource, uint32_t serial,
1188 uint32_t edges)
1189{
1190 struct weston_seat *ws = seat_resource->data;
1191 struct shell_surface *shsurf = resource->data;
1192
1193 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1194 return;
1195
1196 if (ws->seat.pointer->button_count == 0 ||
1197 ws->seat.pointer->grab_serial != serial ||
1198 ws->seat.pointer->focus != &shsurf->surface->surface)
1199 return;
1200
1201 if (surface_resize(shsurf, ws, edges) < 0)
1202 wl_resource_post_no_memory(resource);
1203}
1204
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001205static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001206busy_cursor_grab_focus(struct wl_pointer_grab *base,
1207 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001208{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001209 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001210
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001211 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001212 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001213 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001214 }
1215}
1216
1217static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001218busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1219 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001220{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001221}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001222
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001223static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001224busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001225 uint32_t time, uint32_t button, uint32_t state)
1226{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001227 struct shell_grab *grab = (struct shell_grab *) base;
1228 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001229 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001230 (struct weston_surface *) grab->grab.pointer->current;
1231 struct weston_seat *seat =
1232 (struct weston_seat *) grab->grab.pointer->seat;
1233
1234 shsurf = get_shell_surface(surface);
1235 if (shsurf && button == BTN_LEFT && state) {
1236 activate(shsurf->shell, shsurf->surface, seat);
1237 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001238 } else if (shsurf && button == BTN_RIGHT && state) {
1239 activate(shsurf->shell, shsurf->surface, seat);
1240 surface_rotate(shsurf, &seat->seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001241 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001242}
1243
1244static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1245 busy_cursor_grab_focus,
1246 busy_cursor_grab_motion,
1247 busy_cursor_grab_button,
1248};
1249
1250static void
1251set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1252{
1253 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001254
1255 grab = malloc(sizeof *grab);
1256 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001257 return;
1258
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001259 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1260 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001261}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001262
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001263static void
1264end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1265{
1266 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1267
1268 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001269 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001270 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001271 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001272}
1273
Scott Moreau9521d5e2012-04-19 13:06:17 -06001274static void
1275ping_timer_destroy(struct shell_surface *shsurf)
1276{
1277 if (!shsurf || !shsurf->ping_timer)
1278 return;
1279
1280 if (shsurf->ping_timer->source)
1281 wl_event_source_remove(shsurf->ping_timer->source);
1282
1283 free(shsurf->ping_timer);
1284 shsurf->ping_timer = NULL;
1285}
1286
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001287static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001288ping_timeout_handler(void *data)
1289{
1290 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001291 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001292
Scott Moreau9521d5e2012-04-19 13:06:17 -06001293 /* Client is not responding */
1294 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001295
1296 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1297 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1298 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001299
1300 return 1;
1301}
1302
1303static void
1304ping_handler(struct weston_surface *surface, uint32_t serial)
1305{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001306 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001307 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001308 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001309
1310 if (!shsurf)
1311 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001312 if (!shsurf->resource.client)
1313 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001314
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001315 if (shsurf->surface == shsurf->shell->grab_surface)
1316 return;
1317
Scott Moreauff1db4a2012-04-17 19:06:18 -06001318 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001319 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001320 if (!shsurf->ping_timer)
1321 return;
1322
1323 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001324 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1325 shsurf->ping_timer->source =
1326 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1327 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1328
1329 wl_shell_surface_send_ping(&shsurf->resource, serial);
1330 }
1331}
1332
1333static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001334handle_pointer_focus(struct wl_listener *listener, void *data)
1335{
1336 struct wl_pointer *pointer = data;
1337 struct weston_surface *surface =
1338 (struct weston_surface *) pointer->focus;
1339 struct weston_compositor *compositor;
1340 struct shell_surface *shsurf;
1341 uint32_t serial;
1342
1343 if (!surface)
1344 return;
1345
1346 compositor = surface->compositor;
1347 shsurf = get_shell_surface(surface);
1348
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001349 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001350 set_busy_cursor(shsurf, pointer);
1351 } else {
1352 serial = wl_display_next_serial(compositor->wl_display);
1353 ping_handler(surface, serial);
1354 }
1355}
1356
1357static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001358create_pointer_focus_listener(struct weston_seat *seat)
1359{
1360 struct wl_listener *listener;
1361
1362 if (!seat->seat.pointer)
1363 return;
1364
1365 listener = malloc(sizeof *listener);
1366 listener->notify = handle_pointer_focus;
1367 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1368}
1369
1370static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001371shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1372 uint32_t serial)
1373{
1374 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001375 struct desktop_shell *shell = shsurf->shell;
1376 struct weston_seat *seat;
1377 struct weston_compositor *ec = shsurf->surface->compositor;
1378 struct wl_pointer *pointer;
1379 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001380
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001381 if (shsurf->ping_timer == NULL)
1382 /* Just ignore unsolicited pong. */
1383 return;
1384
Scott Moreauff1db4a2012-04-17 19:06:18 -06001385 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001386 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001387 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001388 if (was_unresponsive) {
1389 /* Received pong from previously unresponsive client */
1390 wl_list_for_each(seat, &ec->seat_list, link) {
1391 pointer = seat->seat.pointer;
1392 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001393 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001394 pointer->current ==
1395 &shsurf->surface->surface)
1396 end_busy_cursor(shsurf, pointer);
1397 }
1398 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001399 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001400 }
1401}
1402
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001403static void
1404shell_surface_set_title(struct wl_client *client,
1405 struct wl_resource *resource, const char *title)
1406{
1407 struct shell_surface *shsurf = resource->data;
1408
1409 free(shsurf->title);
1410 shsurf->title = strdup(title);
1411}
1412
1413static void
1414shell_surface_set_class(struct wl_client *client,
1415 struct wl_resource *resource, const char *class)
1416{
1417 struct shell_surface *shsurf = resource->data;
1418
1419 free(shsurf->class);
1420 shsurf->class = strdup(class);
1421}
1422
Juan Zhao96879df2012-02-07 08:45:41 +08001423static struct weston_output *
1424get_default_output(struct weston_compositor *compositor)
1425{
1426 return container_of(compositor->output_list.next,
1427 struct weston_output, link);
1428}
1429
Alex Wu4539b082012-03-01 12:57:46 +08001430static void
1431shell_unset_fullscreen(struct shell_surface *shsurf)
1432{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001433 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001434 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001435 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1436 shell_surface_is_top_fullscreen(shsurf)) {
1437 weston_output_switch_mode(shsurf->fullscreen_output,
1438 shsurf->fullscreen_output->origin);
1439 }
Alex Wu4539b082012-03-01 12:57:46 +08001440 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1441 shsurf->fullscreen.framerate = 0;
1442 wl_list_remove(&shsurf->fullscreen.transform.link);
1443 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001444 if (shsurf->fullscreen.black_surface)
1445 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001446 shsurf->fullscreen.black_surface = NULL;
1447 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001448 weston_surface_set_position(shsurf->surface,
1449 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001450 if (shsurf->saved_rotation_valid) {
1451 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1452 &shsurf->rotation.transform.link);
1453 shsurf->saved_rotation_valid = false;
1454 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001455
1456 ws = get_current_workspace(shsurf->shell);
1457 wl_list_remove(&shsurf->surface->layer_link);
1458 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001459}
1460
Pekka Paalanen98262232011-12-01 10:42:22 +02001461static int
1462reset_shell_surface_type(struct shell_surface *surface)
1463{
1464 switch (surface->type) {
1465 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001466 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001467 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001468 case SHELL_SURFACE_MAXIMIZED:
1469 surface->output = get_default_output(surface->surface->compositor);
1470 weston_surface_set_position(surface->surface,
1471 surface->saved_x,
1472 surface->saved_y);
1473 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001474 case SHELL_SURFACE_NONE:
1475 case SHELL_SURFACE_TOPLEVEL:
1476 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001477 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001478 break;
1479 }
1480
1481 surface->type = SHELL_SURFACE_NONE;
1482 return 0;
1483}
1484
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001485static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001486set_surface_type(struct shell_surface *shsurf)
1487{
1488 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001489 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001490
1491 reset_shell_surface_type(shsurf);
1492
1493 shsurf->type = shsurf->next_type;
1494 shsurf->next_type = SHELL_SURFACE_NONE;
1495
1496 switch (shsurf->type) {
1497 case SHELL_SURFACE_TOPLEVEL:
1498 break;
1499 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001500 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001501 pes->geometry.x + shsurf->transient.x,
1502 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001503 break;
1504
1505 case SHELL_SURFACE_MAXIMIZED:
1506 shsurf->saved_x = surface->geometry.x;
1507 shsurf->saved_y = surface->geometry.y;
1508 shsurf->saved_position_valid = true;
1509 break;
1510
1511 case SHELL_SURFACE_FULLSCREEN:
1512 shsurf->saved_x = surface->geometry.x;
1513 shsurf->saved_y = surface->geometry.y;
1514 shsurf->saved_position_valid = true;
1515
1516 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1517 wl_list_remove(&shsurf->rotation.transform.link);
1518 wl_list_init(&shsurf->rotation.transform.link);
1519 shsurf->surface->geometry.dirty = 1;
1520 shsurf->saved_rotation_valid = true;
1521 }
1522 break;
1523
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001524 default:
1525 break;
1526 }
1527}
1528
1529static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001530set_toplevel(struct shell_surface *shsurf)
1531{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001532 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001533}
1534
1535static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001536shell_surface_set_toplevel(struct wl_client *client,
1537 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001538{
Pekka Paalanen98262232011-12-01 10:42:22 +02001539 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001540
Tiago Vignattibc052c92012-04-19 16:18:18 +03001541 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001542}
1543
1544static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001545set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001546 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001547{
1548 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001549 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001550 shsurf->transient.x = x;
1551 shsurf->transient.y = y;
1552 shsurf->transient.flags = flags;
1553 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1554}
1555
1556static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001557shell_surface_set_transient(struct wl_client *client,
1558 struct wl_resource *resource,
1559 struct wl_resource *parent_resource,
1560 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001561{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001562 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001563 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001564
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001565 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001566}
1567
Tiago Vignattibe143262012-04-16 17:31:41 +03001568static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001569shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001570{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001571 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001572}
1573
1574static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001575get_output_panel_height(struct desktop_shell *shell,
1576 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001577{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001578 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001579 int panel_height = 0;
1580
1581 if (!output)
1582 return 0;
1583
Juan Zhao4ab94682012-07-09 22:24:09 -07001584 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001585 if (surface->output == output) {
1586 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001587 break;
1588 }
1589 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001590
Juan Zhao96879df2012-02-07 08:45:41 +08001591 return panel_height;
1592}
1593
1594static void
1595shell_surface_set_maximized(struct wl_client *client,
1596 struct wl_resource *resource,
1597 struct wl_resource *output_resource )
1598{
1599 struct shell_surface *shsurf = resource->data;
1600 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001601 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001602 uint32_t edges = 0, panel_height = 0;
1603
1604 /* get the default output, if the client set it as NULL
1605 check whether the ouput is available */
1606 if (output_resource)
1607 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001608 else if (es->output)
1609 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001610 else
1611 shsurf->output = get_default_output(es->compositor);
1612
Tiago Vignattibe143262012-04-16 17:31:41 +03001613 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001614 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001615 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001616
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001617 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001618 shsurf->output->width,
1619 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001620
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001621 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001622}
1623
Alex Wu21858432012-04-01 20:13:08 +08001624static void
1625black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1626
Alex Wu4539b082012-03-01 12:57:46 +08001627static struct weston_surface *
1628create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001629 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001630 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001631{
1632 struct weston_surface *surface = NULL;
1633
1634 surface = weston_surface_create(ec);
1635 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001636 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001637 return NULL;
1638 }
1639
Alex Wu21858432012-04-01 20:13:08 +08001640 surface->configure = black_surface_configure;
1641 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001642 weston_surface_configure(surface, x, y, w, h);
1643 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001644 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001645 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001646 pixman_region32_fini(&surface->input);
1647 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001648
Alex Wu4539b082012-03-01 12:57:46 +08001649 return surface;
1650}
1651
1652/* Create black surface and append it to the associated fullscreen surface.
1653 * Handle size dismatch and positioning according to the method. */
1654static void
1655shell_configure_fullscreen(struct shell_surface *shsurf)
1656{
1657 struct weston_output *output = shsurf->fullscreen_output;
1658 struct weston_surface *surface = shsurf->surface;
1659 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001660 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001661
Alex Wu4539b082012-03-01 12:57:46 +08001662 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001663 shsurf->fullscreen.black_surface =
1664 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001665 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001666 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001667 output->width,
1668 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001669
1670 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1671 wl_list_insert(&surface->layer_link,
1672 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001673 shsurf->fullscreen.black_surface->output = output;
1674
1675 switch (shsurf->fullscreen.type) {
1676 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001677 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001678 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001679 break;
1680 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001681 /* 1:1 mapping between surface and output dimensions */
1682 if (output->width == surface->geometry.width &&
1683 output->height == surface->geometry.height) {
1684 weston_surface_set_position(surface, output->x, output->y);
1685 break;
1686 }
1687
Alex Wu4539b082012-03-01 12:57:46 +08001688 matrix = &shsurf->fullscreen.transform.matrix;
1689 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001690
Scott Moreau1bad5db2012-08-18 01:04:05 -06001691 output_aspect = (float) output->width /
1692 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001693 surface_aspect = (float) surface->geometry.width /
1694 (float) surface->geometry.height;
1695 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001696 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001697 (float) surface->geometry.width;
1698 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001699 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001700 (float) surface->geometry.height;
1701
Alex Wu4539b082012-03-01 12:57:46 +08001702 weston_matrix_scale(matrix, scale, scale, 1);
1703 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001704 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001705 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001706 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1707 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001708 weston_surface_set_position(surface, x, y);
1709
Alex Wu4539b082012-03-01 12:57:46 +08001710 break;
1711 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001712 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001713 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001714 surface->geometry.width,
1715 surface->geometry.height,
1716 shsurf->fullscreen.framerate};
1717
1718 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001719 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001720 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001721 output->width,
1722 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001723 weston_surface_set_position(surface, output->x, output->y);
1724 break;
1725 }
1726 }
Alex Wu4539b082012-03-01 12:57:46 +08001727 break;
1728 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1729 break;
1730 default:
1731 break;
1732 }
1733}
1734
1735/* make the fullscreen and black surface at the top */
1736static void
1737shell_stack_fullscreen(struct shell_surface *shsurf)
1738{
Alex Wubd3354b2012-04-17 17:20:49 +08001739 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001740 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001741 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001742
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001743 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001744 wl_list_insert(&shell->fullscreen_layer.surface_list,
1745 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001746 weston_surface_damage(surface);
1747
1748 if (!shsurf->fullscreen.black_surface)
1749 shsurf->fullscreen.black_surface =
1750 create_black_surface(surface->compositor,
1751 surface,
1752 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001753 output->width,
1754 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001755
1756 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001757 wl_list_insert(&surface->layer_link,
1758 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001759 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001760}
1761
1762static void
1763shell_map_fullscreen(struct shell_surface *shsurf)
1764{
Alex Wu4539b082012-03-01 12:57:46 +08001765 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001766 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001767}
1768
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001769static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001770set_fullscreen(struct shell_surface *shsurf,
1771 uint32_t method,
1772 uint32_t framerate,
1773 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001774{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001775 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001776
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001777 if (output)
1778 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001779 else if (es->output)
1780 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001781 else
1782 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001783
Alex Wu4539b082012-03-01 12:57:46 +08001784 shsurf->fullscreen_output = shsurf->output;
1785 shsurf->fullscreen.type = method;
1786 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001787 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001788
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001789 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001790 shsurf->output->width,
1791 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001792}
1793
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001794static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001795shell_surface_set_fullscreen(struct wl_client *client,
1796 struct wl_resource *resource,
1797 uint32_t method,
1798 uint32_t framerate,
1799 struct wl_resource *output_resource)
1800{
1801 struct shell_surface *shsurf = resource->data;
1802 struct weston_output *output;
1803
1804 if (output_resource)
1805 output = output_resource->data;
1806 else
1807 output = NULL;
1808
1809 set_fullscreen(shsurf, method, framerate, output);
1810}
1811
1812static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001813popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001814 struct wl_surface *surface,
1815 wl_fixed_t x,
1816 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001817{
Daniel Stone37816df2012-05-16 18:45:18 +01001818 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001819 struct shell_surface *priv =
1820 container_of(grab, struct shell_surface, popup.grab);
1821 struct wl_client *client = priv->surface->surface.resource.client;
1822
Pekka Paalanencb108432012-01-19 16:25:40 +02001823 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001824 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001825 grab->focus = surface;
1826 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001827 wl_pointer_set_focus(pointer, NULL,
1828 wl_fixed_from_int(0),
1829 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001830 grab->focus = NULL;
1831 }
1832}
1833
1834static void
Scott Moreau447013d2012-02-18 05:05:29 -07001835popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001836 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001837{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001838 struct wl_resource *resource;
1839
Daniel Stone37816df2012-05-16 18:45:18 +01001840 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001841 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001842 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001843}
1844
1845static void
Scott Moreau447013d2012-02-18 05:05:29 -07001846popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001847 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001848{
1849 struct wl_resource *resource;
1850 struct shell_surface *shsurf =
1851 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001852 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001853 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001854 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001855
Daniel Stone37816df2012-05-16 18:45:18 +01001856 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001857 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001858 display = wl_client_get_display(resource->client);
1859 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001860 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001861 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001862 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001863 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001864 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001865 }
1866
Daniel Stone4dbadb12012-05-30 16:31:51 +01001867 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001868 shsurf->popup.initial_up = 1;
1869}
1870
Scott Moreau447013d2012-02-18 05:05:29 -07001871static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001872 popup_grab_focus,
1873 popup_grab_motion,
1874 popup_grab_button,
1875};
1876
1877static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001878popup_grab_end(struct wl_pointer *pointer)
1879{
1880 struct wl_pointer_grab *grab = pointer->grab;
1881 struct shell_surface *shsurf =
1882 container_of(grab, struct shell_surface, popup.grab);
1883
1884 if (pointer->grab->interface == &popup_grab_interface) {
1885 wl_shell_surface_send_popup_done(&shsurf->resource);
1886 wl_pointer_end_grab(grab->pointer);
1887 shsurf->popup.grab.pointer = NULL;
1888 }
1889}
1890
1891static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001892shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001893{
Daniel Stone37816df2012-05-16 18:45:18 +01001894 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001895 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001896 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001897
1898 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001899 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001900
Pekka Paalanen938269a2012-02-07 14:19:01 +02001901 weston_surface_update_transform(parent);
1902 if (parent->transform.enabled) {
1903 shsurf->popup.parent_transform.matrix =
1904 parent->transform.matrix;
1905 } else {
1906 /* construct x, y translation matrix */
1907 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03001908 shsurf->popup.parent_transform.matrix.type =
1909 WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen938269a2012-02-07 14:19:01 +02001910 shsurf->popup.parent_transform.matrix.d[12] =
1911 parent->geometry.x;
1912 shsurf->popup.parent_transform.matrix.d[13] =
1913 parent->geometry.y;
1914 }
1915 wl_list_insert(es->geometry.transformation_list.prev,
1916 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001917
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001918 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001919 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1920 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001921
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001922 /* We don't require the grab to still be active, but if another
1923 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001924 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1925 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001926 } else {
1927 wl_shell_surface_send_popup_done(&shsurf->resource);
1928 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001929}
1930
1931static void
1932shell_surface_set_popup(struct wl_client *client,
1933 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001934 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001935 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001936 struct wl_resource *parent_resource,
1937 int32_t x, int32_t y, uint32_t flags)
1938{
1939 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001940
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001941 shsurf->type = SHELL_SURFACE_POPUP;
1942 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001943 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001944 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001945 shsurf->popup.x = x;
1946 shsurf->popup.y = y;
1947}
1948
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001949static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001950 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001951 shell_surface_move,
1952 shell_surface_resize,
1953 shell_surface_set_toplevel,
1954 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001955 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001956 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001957 shell_surface_set_maximized,
1958 shell_surface_set_title,
1959 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001960};
1961
1962static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001963destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001964{
Daniel Stone37816df2012-05-16 18:45:18 +01001965 if (shsurf->popup.grab.pointer)
1966 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001967
Alex Wubd3354b2012-04-17 17:20:49 +08001968 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1969 shell_surface_is_top_fullscreen(shsurf)) {
1970 weston_output_switch_mode(shsurf->fullscreen_output,
1971 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001972 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001973
Alex Wuaa08e2d2012-03-05 11:01:40 +08001974 if (shsurf->fullscreen.black_surface)
1975 weston_surface_destroy(shsurf->fullscreen.black_surface);
1976
Alex Wubd3354b2012-04-17 17:20:49 +08001977 /* As destroy_resource() use wl_list_for_each_safe(),
1978 * we can always remove the listener.
1979 */
1980 wl_list_remove(&shsurf->surface_destroy_listener.link);
1981 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001982 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001983
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001984 wl_list_remove(&shsurf->link);
1985 free(shsurf);
1986}
1987
1988static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001989shell_destroy_shell_surface(struct wl_resource *resource)
1990{
1991 struct shell_surface *shsurf = resource->data;
1992
1993 destroy_shell_surface(shsurf);
1994}
1995
1996static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001997shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001998{
1999 struct shell_surface *shsurf = container_of(listener,
2000 struct shell_surface,
2001 surface_destroy_listener);
2002
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002003 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002004 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002005 } else {
2006 wl_signal_emit(&shsurf->resource.destroy_signal,
2007 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002008 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002009 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002010}
2011
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002012static void
2013shell_surface_configure(struct weston_surface *, int32_t, int32_t);
2014
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002015static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002016get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002017{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002018 if (surface->configure == shell_surface_configure)
2019 return surface->private;
2020 else
2021 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002022}
2023
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002024static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002025create_shell_surface(void *shell, struct weston_surface *surface,
2026 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002027{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002028 struct shell_surface *shsurf;
2029
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002030 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002031 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002032 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002033 }
2034
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002035 shsurf = calloc(1, sizeof *shsurf);
2036 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002037 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002038 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002039 }
2040
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002041 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002042 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002043
Tiago Vignattibc052c92012-04-19 16:18:18 +03002044 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002045 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002046 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002047 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002048 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002049 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2050 shsurf->fullscreen.framerate = 0;
2051 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002052 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002053 wl_list_init(&shsurf->fullscreen.transform.link);
2054
Tiago Vignattibc052c92012-04-19 16:18:18 +03002055 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002056 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2057 wl_signal_add(&surface->surface.resource.destroy_signal,
2058 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002059
2060 /* init link so its safe to always remove it in destroy_shell_surface */
2061 wl_list_init(&shsurf->link);
2062
Pekka Paalanen460099f2012-01-20 16:48:25 +02002063 /* empty when not in use */
2064 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002065 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002066
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002067 wl_list_init(&shsurf->workspace_transform.link);
2068
Pekka Paalanen98262232011-12-01 10:42:22 +02002069 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002070 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002071
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002072 shsurf->client = client;
2073
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002074 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002075}
2076
2077static void
2078shell_get_shell_surface(struct wl_client *client,
2079 struct wl_resource *resource,
2080 uint32_t id,
2081 struct wl_resource *surface_resource)
2082{
2083 struct weston_surface *surface = surface_resource->data;
2084 struct desktop_shell *shell = resource->data;
2085 struct shell_surface *shsurf;
2086
2087 if (get_shell_surface(surface)) {
2088 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002089 WL_DISPLAY_ERROR_INVALID_OBJECT,
2090 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002091 return;
2092 }
2093
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002094 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002095 if (!shsurf) {
2096 wl_resource_post_error(surface_resource,
2097 WL_DISPLAY_ERROR_INVALID_OBJECT,
2098 "surface->configure already set");
2099 return;
2100 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002101
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002102 shsurf->resource.destroy = shell_destroy_shell_surface;
2103 shsurf->resource.object.id = id;
2104 shsurf->resource.object.interface = &wl_shell_surface_interface;
2105 shsurf->resource.object.implementation =
2106 (void (**)(void)) &shell_surface_implementation;
2107 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002108
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002109 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002110}
2111
2112static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002113 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002114};
2115
Kristian Høgsberg07937562011-04-12 17:25:42 -04002116static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002117handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002118{
2119 proc->pid = 0;
2120}
2121
2122static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002123launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002124{
2125 if (shell->screensaver.binding)
2126 return;
2127
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002128 if (!shell->screensaver.path)
2129 return;
2130
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002131 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002132 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002133 return;
2134 }
2135
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002136 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002137 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002138 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002139 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002140}
2141
2142static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002143terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002144{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002145 if (shell->screensaver.process.pid == 0)
2146 return;
2147
2148 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002149}
2150
2151static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002152configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002153{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002154 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002155
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002156 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2157 if (s->output == es->output && s != es) {
2158 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002159 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002160 }
2161 }
2162
2163 weston_surface_configure(es, es->output->x, es->output->y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002164 weston_surface_buffer_width(es),
2165 weston_surface_buffer_height(es));
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002166
2167 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002168 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002169 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002170 }
2171}
2172
2173static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002174background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2175{
2176 struct desktop_shell *shell = es->private;
2177
2178 configure_static_surface(es, &shell->background_layer);
2179}
2180
2181static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002182desktop_shell_set_background(struct wl_client *client,
2183 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002184 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002185 struct wl_resource *surface_resource)
2186{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002187 struct desktop_shell *shell = resource->data;
2188 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002189
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002190 if (surface->configure) {
2191 wl_resource_post_error(surface_resource,
2192 WL_DISPLAY_ERROR_INVALID_OBJECT,
2193 "surface role already assigned");
2194 return;
2195 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002196
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002197 surface->configure = background_configure;
2198 surface->private = shell;
2199 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002200 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002201 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002202 surface->output->width,
2203 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002204}
2205
2206static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002207panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2208{
2209 struct desktop_shell *shell = es->private;
2210
2211 configure_static_surface(es, &shell->panel_layer);
2212}
2213
2214static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002215desktop_shell_set_panel(struct wl_client *client,
2216 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002217 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002218 struct wl_resource *surface_resource)
2219{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002220 struct desktop_shell *shell = resource->data;
2221 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002222
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002223 if (surface->configure) {
2224 wl_resource_post_error(surface_resource,
2225 WL_DISPLAY_ERROR_INVALID_OBJECT,
2226 "surface role already assigned");
2227 return;
2228 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002229
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002230 surface->configure = panel_configure;
2231 surface->private = shell;
2232 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002233 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002234 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002235 surface->output->width,
2236 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002237}
2238
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002239static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002240lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2241{
2242 struct desktop_shell *shell = surface->private;
2243
2244 center_on_output(surface, get_default_output(shell->compositor));
2245
2246 if (!weston_surface_is_mapped(surface)) {
2247 wl_list_insert(&shell->lock_layer.surface_list,
2248 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002249 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002250 weston_compositor_wake(shell->compositor);
2251 }
2252}
2253
2254static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002255handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002256{
Tiago Vignattibe143262012-04-16 17:31:41 +03002257 struct desktop_shell *shell =
2258 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002259
Martin Minarik6d118362012-06-07 18:01:59 +02002260 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002261 shell->lock_surface = NULL;
2262}
2263
2264static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002265desktop_shell_set_lock_surface(struct wl_client *client,
2266 struct wl_resource *resource,
2267 struct wl_resource *surface_resource)
2268{
Tiago Vignattibe143262012-04-16 17:31:41 +03002269 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002270 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002271
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002272 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002273
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002274 if (!shell->locked)
2275 return;
2276
Pekka Paalanen98262232011-12-01 10:42:22 +02002277 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002278
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002279 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2280 wl_signal_add(&surface_resource->destroy_signal,
2281 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002282
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002283 surface->configure = lock_surface_configure;
2284 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002285}
2286
2287static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002288resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002289{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002290 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002291
Pekka Paalanen77346a62011-11-30 16:26:35 +02002292 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002293
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002294 wl_list_remove(&shell->lock_layer.link);
2295 wl_list_insert(&shell->compositor->cursor_layer.link,
2296 &shell->fullscreen_layer.link);
2297 wl_list_insert(&shell->fullscreen_layer.link,
2298 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002299 if (shell->showing_input_panels) {
2300 wl_list_insert(&shell->panel_layer.link,
2301 &shell->input_panel_layer.link);
2302 wl_list_insert(&shell->input_panel_layer.link,
2303 &ws->layer.link);
2304 } else {
2305 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2306 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002307
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002308 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002309
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002310 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002311 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002312 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002313 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002314}
2315
2316static void
2317desktop_shell_unlock(struct wl_client *client,
2318 struct wl_resource *resource)
2319{
Tiago Vignattibe143262012-04-16 17:31:41 +03002320 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002321
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002322 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002323
2324 if (shell->locked)
2325 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002326}
2327
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002328static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002329desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002330 struct wl_resource *resource,
2331 struct wl_resource *surface_resource)
2332{
2333 struct desktop_shell *shell = resource->data;
2334
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002335 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002336}
2337
Kristian Høgsberg75840622011-09-06 13:48:16 -04002338static const struct desktop_shell_interface desktop_shell_implementation = {
2339 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002340 desktop_shell_set_panel,
2341 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002342 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002343 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002344};
2345
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002346static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002347get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002348{
2349 struct shell_surface *shsurf;
2350
2351 shsurf = get_shell_surface(surface);
2352 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002353 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002354 return shsurf->type;
2355}
2356
Kristian Høgsberg75840622011-09-06 13:48:16 -04002357static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002358move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002359{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002360 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002361 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002362 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002363
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002364 if (surface == NULL)
2365 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002366
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002367 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002368 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002369 return;
2370
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002371 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002372}
2373
2374static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002375resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002376{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002377 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002378 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002379 uint32_t edges = 0;
2380 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002381 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002382
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002383 if (surface == NULL)
2384 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002385
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002386 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002387 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002388 return;
2389
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002390 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002391 wl_fixed_to_int(seat->pointer->grab_x),
2392 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002393 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002394
Pekka Paalanen60921e52012-01-25 15:55:43 +02002395 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002396 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002397 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002398 edges |= 0;
2399 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002400 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002401
Pekka Paalanen60921e52012-01-25 15:55:43 +02002402 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002403 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002404 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002405 edges |= 0;
2406 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002407 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002408
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002409 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002410}
2411
2412static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002413surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002414 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002415{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002416 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002417 struct shell_surface *shsurf;
2418 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002419 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002420
2421 if (surface == NULL)
2422 return;
2423
2424 shsurf = get_shell_surface(surface);
2425 if (!shsurf)
2426 return;
2427
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002428 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002429
Scott Moreau02709af2012-05-22 01:54:10 -06002430 if (surface->alpha > 1.0)
2431 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002432 if (surface->alpha < step)
2433 surface->alpha = step;
2434
2435 surface->geometry.dirty = 1;
2436 weston_surface_damage(surface);
2437}
2438
2439static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002440do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002441 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002442{
Daniel Stone37816df2012-05-16 18:45:18 +01002443 struct weston_seat *ws = (struct weston_seat *) seat;
2444 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002445 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002446 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002447
2448 wl_list_for_each(output, &compositor->output_list, link) {
2449 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002450 wl_fixed_to_double(seat->pointer->x),
2451 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002452 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002453 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002454 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002455 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002456 increment = -output->zoom.increment;
2457 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002458 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002459 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002460 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002461 else
2462 increment = 0;
2463
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002464 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002465
Scott Moreaue6603982012-06-11 13:07:51 -06002466 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002467 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002468 else if (output->zoom.level > output->zoom.max_level)
2469 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002470 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002471 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002472 output->disable_planes++;
2473 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002474
Scott Moreaue6603982012-06-11 13:07:51 -06002475 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002476
Scott Moreau8dacaab2012-06-17 18:10:58 -06002477 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002478 }
2479 }
2480}
2481
Scott Moreauccbf29d2012-02-22 14:21:41 -07002482static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002483zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002484 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002485{
2486 do_zoom(seat, time, 0, axis, value);
2487}
2488
2489static void
2490zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2491 void *data)
2492{
2493 do_zoom(seat, time, key, 0, 0);
2494}
2495
2496static void
2497terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2498 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002499{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002500 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002501
Daniel Stone325fc2d2012-05-30 16:31:58 +01002502 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002503}
2504
2505static void
Scott Moreau447013d2012-02-18 05:05:29 -07002506rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002507 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002508{
2509 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002510 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002511 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002512 struct shell_surface *shsurf = rotate->base.shsurf;
2513 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002514 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002515
2516 if (!shsurf)
2517 return;
2518
2519 surface = shsurf->surface;
2520
2521 cx = 0.5f * surface->geometry.width;
2522 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002523
Daniel Stone37816df2012-05-16 18:45:18 +01002524 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2525 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002526 r = sqrtf(dx * dx + dy * dy);
2527
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002528 wl_list_remove(&shsurf->rotation.transform.link);
2529 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002530
2531 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002532 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002533 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002534
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002535 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002536 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002537
2538 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002539 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002540 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002541 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002542 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002543
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002544 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002545 &shsurf->surface->geometry.transformation_list,
2546 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002547 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002548 wl_list_init(&shsurf->rotation.transform.link);
2549 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002550 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002551 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002552
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002553 /* We need to adjust the position of the surface
2554 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002555 cposx = surface->geometry.x + cx;
2556 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002557 dposx = rotate->center.x - cposx;
2558 dposy = rotate->center.y - cposy;
2559 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002560 weston_surface_set_position(surface,
2561 surface->geometry.x + dposx,
2562 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002563 }
2564
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002565 /* Repaint implies weston_surface_update_transform(), which
2566 * lazily applies the damage due to rotation update.
2567 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002568 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002569}
2570
2571static void
Scott Moreau447013d2012-02-18 05:05:29 -07002572rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002573 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002574{
2575 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002576 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002577 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002578 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002579 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002580
Daniel Stone4dbadb12012-05-30 16:31:51 +01002581 if (pointer->button_count == 0 &&
2582 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002583 if (shsurf)
2584 weston_matrix_multiply(&shsurf->rotation.rotation,
2585 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002586 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002587 free(rotate);
2588 }
2589}
2590
Scott Moreau447013d2012-02-18 05:05:29 -07002591static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002592 noop_grab_focus,
2593 rotate_grab_motion,
2594 rotate_grab_button,
2595};
2596
2597static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002598surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002599{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002600 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002601 float dx, dy;
2602 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002603
Pekka Paalanen460099f2012-01-20 16:48:25 +02002604 rotate = malloc(sizeof *rotate);
2605 if (!rotate)
2606 return;
2607
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002608 weston_surface_to_global_float(surface->surface,
2609 surface->surface->geometry.width / 2,
2610 surface->surface->geometry.height / 2,
2611 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002612
Daniel Stone37816df2012-05-16 18:45:18 +01002613 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2614 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002615 r = sqrtf(dx * dx + dy * dy);
2616 if (r > 20.0f) {
2617 struct weston_matrix inverse;
2618
2619 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002620 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002621 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002622
2623 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002624 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002625 } else {
2626 weston_matrix_init(&surface->rotation.rotation);
2627 weston_matrix_init(&rotate->rotation);
2628 }
2629
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002630 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2631 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002632}
2633
2634static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002635rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2636 void *data)
2637{
2638 struct weston_surface *base_surface =
2639 (struct weston_surface *) seat->pointer->focus;
2640 struct shell_surface *surface;
2641
2642 if (base_surface == NULL)
2643 return;
2644
2645 surface = get_shell_surface(base_surface);
2646 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2647 return;
2648
2649 surface_rotate(surface, seat);
2650}
2651
2652static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002653lower_fullscreen_layer(struct desktop_shell *shell)
2654{
2655 struct workspace *ws;
2656 struct weston_surface *surface, *prev;
2657
2658 ws = get_current_workspace(shell);
2659 wl_list_for_each_reverse_safe(surface, prev,
2660 &shell->fullscreen_layer.surface_list,
2661 layer_link)
2662 weston_surface_restack(surface, &ws->layer.surface_list);
2663}
2664
2665static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002666activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002667 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002668{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002669 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002670 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002671
Daniel Stone37816df2012-05-16 18:45:18 +01002672 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002673
Jonas Ådahl8538b222012-08-29 22:13:03 +02002674 state = ensure_focus_state(shell, seat);
2675 if (state == NULL)
2676 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002677
2678 state->keyboard_focus = es;
2679 wl_list_remove(&state->surface_destroy_listener.link);
2680 wl_signal_add(&es->surface.resource.destroy_signal,
2681 &state->surface_destroy_listener);
2682
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002683 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002684 case SHELL_SURFACE_FULLSCREEN:
2685 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002686 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002687 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002688 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002689 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002690 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002691 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002692 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002693 }
2694}
2695
Alex Wu21858432012-04-01 20:13:08 +08002696/* no-op func for checking black surface */
2697static void
2698black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2699{
2700}
2701
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002702static bool
Alex Wu21858432012-04-01 20:13:08 +08002703is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2704{
2705 if (es->configure == black_surface_configure) {
2706 if (fs_surface)
2707 *fs_surface = (struct weston_surface *)es->private;
2708 return true;
2709 }
2710 return false;
2711}
2712
Kristian Høgsberg75840622011-09-06 13:48:16 -04002713static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002714click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002715 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002716{
Daniel Stone37816df2012-05-16 18:45:18 +01002717 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002718 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002719 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002720 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002721
Daniel Stone37816df2012-05-16 18:45:18 +01002722 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002723 if (!focus)
2724 return;
2725
Alex Wu21858432012-04-01 20:13:08 +08002726 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002727 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002728
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002729 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2730 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002731
Daniel Stone325fc2d2012-05-30 16:31:58 +01002732 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002733 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002734}
2735
2736static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002737lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002738{
Tiago Vignattibe143262012-04-16 17:31:41 +03002739 struct desktop_shell *shell =
2740 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002741 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002742 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002743
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002744 if (shell->locked) {
2745 wl_list_for_each(output, &shell->compositor->output_list, link)
2746 /* TODO: find a way to jump to other DPMS levels */
2747 if (output->set_dpms)
2748 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002749 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002750 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002751
2752 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002753
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002754 /* Hide all surfaces by removing the fullscreen, panel and
2755 * toplevel layers. This way nothing else can show or receive
2756 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002757
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002758 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002759 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002760 if (shell->showing_input_panels)
2761 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002762 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002763 wl_list_insert(&shell->compositor->cursor_layer.link,
2764 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002765
Pekka Paalanen77346a62011-11-30 16:26:35 +02002766 launch_screensaver(shell);
2767
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002768 /* TODO: disable bindings that should not work while locked. */
2769
2770 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002771}
2772
2773static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002774unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002775{
Tiago Vignattibe143262012-04-16 17:31:41 +03002776 struct desktop_shell *shell =
2777 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002778
Pekka Paalanend81c2162011-11-16 13:47:34 +02002779 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002780 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002781 return;
2782 }
2783
2784 /* If desktop-shell client has gone away, unlock immediately. */
2785 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002786 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002787 return;
2788 }
2789
2790 if (shell->prepare_event_sent)
2791 return;
2792
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002793 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002794 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002795}
2796
2797static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002798show_input_panels(struct wl_listener *listener, void *data)
2799{
2800 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002801 container_of(listener, struct desktop_shell,
2802 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002803 struct input_panel_surface *surface, *next;
2804 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002805
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002806 shell->showing_input_panels = true;
2807
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002808 if (!shell->locked)
2809 wl_list_insert(&shell->panel_layer.link,
2810 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002811
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002812 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002813 &shell->input_panel.surfaces, link) {
2814 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002815 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002816 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002817 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002818 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002819 weston_surface_damage(ws);
2820 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002821 }
2822}
2823
2824static void
2825hide_input_panels(struct wl_listener *listener, void *data)
2826{
2827 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002828 container_of(listener, struct desktop_shell,
2829 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002830 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002831
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002832 shell->showing_input_panels = false;
2833
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002834 if (!shell->locked)
2835 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002836
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002837 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002838 &shell->input_panel_layer.surface_list, layer_link)
2839 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002840}
2841
2842static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002843center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002844{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002845 int32_t width = weston_surface_buffer_width(surface);
2846 int32_t height = weston_surface_buffer_height(surface);
2847 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002848
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002849 x = output->x + (output->width - width) / 2;
2850 y = output->y + (output->height - height) / 2;
2851
2852 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002853}
2854
2855static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002856weston_surface_set_initial_position (struct weston_surface *surface,
2857 struct desktop_shell *shell)
2858{
2859 struct weston_compositor *compositor = shell->compositor;
2860 int ix = 0, iy = 0;
2861 int range_x, range_y;
2862 int dx, dy, x, y, panel_height;
2863 struct weston_output *output, *target_output = NULL;
2864 struct weston_seat *seat;
2865
2866 /* As a heuristic place the new window on the same output as the
2867 * pointer. Falling back to the output containing 0, 0.
2868 *
2869 * TODO: Do something clever for touch too?
2870 */
2871 wl_list_for_each(seat, &compositor->seat_list, link) {
2872 if (seat->has_pointer) {
2873 ix = wl_fixed_to_int(seat->pointer.x);
2874 iy = wl_fixed_to_int(seat->pointer.y);
2875 break;
2876 }
2877 }
2878
2879 wl_list_for_each(output, &compositor->output_list, link) {
2880 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2881 target_output = output;
2882 break;
2883 }
2884 }
2885
2886 if (!target_output) {
2887 weston_surface_set_position(surface, 10 + random() % 400,
2888 10 + random() % 400);
2889 return;
2890 }
2891
2892 /* Valid range within output where the surface will still be onscreen.
2893 * If this is negative it means that the surface is bigger than
2894 * output.
2895 */
2896 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002897 range_x = target_output->width - surface->geometry.width;
2898 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002899 surface->geometry.height;
2900
Rob Bradford4cb88c72012-08-13 15:18:44 +01002901 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002902 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002903 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002904 dx = 0;
2905
2906 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002907 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002908 else
2909 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002910
2911 x = target_output->x + dx;
2912 y = target_output->y + dy;
2913
2914 weston_surface_set_position (surface, x, y);
2915}
2916
2917static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002918map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002919 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002920{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002921 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002922 struct shell_surface *shsurf = get_shell_surface(surface);
2923 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002924 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002925 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002926 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002927 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002928
Pekka Paalanen60921e52012-01-25 15:55:43 +02002929 surface->geometry.width = width;
2930 surface->geometry.height = height;
2931 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002932
2933 /* initial positioning, see also configure() */
2934 switch (surface_type) {
2935 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002936 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002937 break;
Alex Wu4539b082012-03-01 12:57:46 +08002938 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002939 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002940 shell_map_fullscreen(shsurf);
2941 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002942 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002943 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002944 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002945 weston_surface_set_position(surface, shsurf->output->x,
2946 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002947 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002948 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002949 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00002950 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002951 case SHELL_SURFACE_NONE:
2952 weston_surface_set_position(surface,
2953 surface->geometry.x + sx,
2954 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002955 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002956 default:
2957 ;
2958 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002959
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002960 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002961 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002962 case SHELL_SURFACE_POPUP:
2963 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002964 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002965 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2966 break;
Alex Wu4539b082012-03-01 12:57:46 +08002967 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002968 case SHELL_SURFACE_NONE:
2969 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002970 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002971 ws = get_current_workspace(shell);
2972 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002973 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002974 }
2975
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002976 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002977 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002978 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2979 surface->output = shsurf->output;
2980 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002981
Juan Zhao7bb92f02011-12-15 11:31:51 -05002982 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002983 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002984 if (shsurf->transient.flags ==
2985 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2986 break;
2987 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002988 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002989 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002990 if (!shell->locked) {
2991 wl_list_for_each(seat, &compositor->seat_list, link)
2992 activate(shell, surface, seat);
2993 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002994 break;
2995 default:
2996 break;
2997 }
2998
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002999 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003000 {
3001 switch (shell->win_animation_type) {
3002 case ANIMATION_FADE:
3003 weston_fade_run(surface, NULL, NULL);
3004 break;
3005 case ANIMATION_ZOOM:
3006 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3007 break;
3008 default:
3009 break;
3010 }
3011 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003012}
3013
3014static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003015configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003016 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003017{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003018 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3019 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003020
Pekka Paalanen77346a62011-11-30 16:26:35 +02003021 shsurf = get_shell_surface(surface);
3022 if (shsurf)
3023 surface_type = shsurf->type;
3024
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003025 surface->geometry.x = x;
3026 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02003027 surface->geometry.width = width;
3028 surface->geometry.height = height;
3029 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003030
3031 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003032 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003033 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003034 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003035 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003036 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003037 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003038 surface->geometry.x = surface->output->x;
3039 surface->geometry.y = surface->output->y +
3040 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003041 break;
Alex Wu4539b082012-03-01 12:57:46 +08003042 case SHELL_SURFACE_TOPLEVEL:
3043 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003044 default:
3045 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003046 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003047
Alex Wu4539b082012-03-01 12:57:46 +08003048 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003049 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003050 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003051
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003052 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003053 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003054 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003055}
3056
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003057static void
3058shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3059{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003060 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003061 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003062 int32_t width = weston_surface_buffer_width(es);
3063 int32_t height = weston_surface_buffer_height(es);
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003064 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003065
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003066 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003067 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003068 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003069 type_changed = 1;
3070 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003071
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003072 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003073 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003074 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003075 es->geometry.width != width ||
3076 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003077 float from_x, from_y;
3078 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003079
3080 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3081 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3082 configure(shell, es,
3083 es->geometry.x + to_x - from_x,
3084 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003085 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003086 }
3087}
3088
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003089static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003090
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003091static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003092desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003093{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003094 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003095 struct desktop_shell *shell =
3096 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003097
3098 shell->child.process.pid = 0;
3099 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003100
3101 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3102 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003103 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003104 shell->child.deathstamp = time;
3105 shell->child.deathcount = 0;
3106 }
3107
3108 shell->child.deathcount++;
3109 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003110 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003111 return;
3112 }
3113
Martin Minarik6d118362012-06-07 18:01:59 +02003114 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003115 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003116}
3117
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003118static void
3119launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003120{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003121 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003122 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003123
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003124 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003125 &shell->child.process,
3126 shell_exe,
3127 desktop_shell_sigchld);
3128
3129 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003130 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003131}
3132
3133static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003134bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3135{
Tiago Vignattibe143262012-04-16 17:31:41 +03003136 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003137
3138 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003139 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003140}
3141
Kristian Høgsberg75840622011-09-06 13:48:16 -04003142static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003143unbind_desktop_shell(struct wl_resource *resource)
3144{
Tiago Vignattibe143262012-04-16 17:31:41 +03003145 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003146
3147 if (shell->locked)
3148 resume_desktop(shell);
3149
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003150 shell->child.desktop_shell = NULL;
3151 shell->prepare_event_sent = false;
3152 free(resource);
3153}
3154
3155static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003156bind_desktop_shell(struct wl_client *client,
3157 void *data, uint32_t version, uint32_t id)
3158{
Tiago Vignattibe143262012-04-16 17:31:41 +03003159 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003160 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003161
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003162 resource = wl_client_add_object(client, &desktop_shell_interface,
3163 &desktop_shell_implementation,
3164 id, shell);
3165
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003166 if (client == shell->child.client) {
3167 resource->destroy = unbind_desktop_shell;
3168 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003169 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003170 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003171
3172 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3173 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003174 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003175}
3176
Pekka Paalanen6e168112011-11-24 11:34:05 +02003177static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003178screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3179{
3180 struct desktop_shell *shell = surface->private;
3181
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003182 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003183 if (!shell->locked)
3184 return;
3185
3186 center_on_output(surface, surface->output);
3187
3188 if (wl_list_empty(&surface->layer_link)) {
3189 wl_list_insert(shell->lock_layer.surface_list.prev,
3190 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003191 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003192 shell->compositor->idle_time = shell->screensaver.duration;
3193 weston_compositor_wake(shell->compositor);
3194 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3195 }
3196}
3197
3198static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003199screensaver_set_surface(struct wl_client *client,
3200 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003201 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003202 struct wl_resource *output_resource)
3203{
Tiago Vignattibe143262012-04-16 17:31:41 +03003204 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003205 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003206 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003207
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003208 surface->configure = screensaver_configure;
3209 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003210 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003211}
3212
3213static const struct screensaver_interface screensaver_implementation = {
3214 screensaver_set_surface
3215};
3216
3217static void
3218unbind_screensaver(struct wl_resource *resource)
3219{
Tiago Vignattibe143262012-04-16 17:31:41 +03003220 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003221
Pekka Paalanen77346a62011-11-30 16:26:35 +02003222 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003223 free(resource);
3224}
3225
3226static void
3227bind_screensaver(struct wl_client *client,
3228 void *data, uint32_t version, uint32_t id)
3229{
Tiago Vignattibe143262012-04-16 17:31:41 +03003230 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003231 struct wl_resource *resource;
3232
3233 resource = wl_client_add_object(client, &screensaver_interface,
3234 &screensaver_implementation,
3235 id, shell);
3236
Pekka Paalanen77346a62011-11-30 16:26:35 +02003237 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003238 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003239 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003240 return;
3241 }
3242
3243 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3244 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003245 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003246}
3247
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003248static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003249input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3250{
3251 struct weston_mode *mode = surface->output->current;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003252 int32_t width = weston_surface_buffer_width(surface);
3253 int32_t height = weston_surface_buffer_height(surface);
3254 float x = (mode->width - width) / 2;
3255 float y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003256
3257 /* Don't map the input panel here, wait for
3258 * show_input_panels signal. */
3259
3260 weston_surface_configure(surface,
3261 surface->output->x + x,
3262 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003263 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003264}
3265
3266static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003267destroy_input_panel_surface(struct wl_listener *listener,
3268 void *data)
3269{
3270 struct input_panel_surface *input_panel_surface =
3271 container_of(listener, struct input_panel_surface, listener);
3272
3273 wl_list_remove(&listener->link);
3274 wl_list_remove(&input_panel_surface->link);
3275
3276 free(input_panel_surface);
3277}
3278
3279static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003280input_panel_set_surface(struct wl_client *client,
3281 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003282 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003283 struct wl_resource *output_resource)
3284{
3285 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003286 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003287 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003288 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003289
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003290 surface->configure = input_panel_configure;
3291 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003292 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003293
3294 input_panel_surface = malloc(sizeof *input_panel_surface);
3295 if (!input_panel_surface) {
3296 wl_resource_post_no_memory(resource);
3297 return;
3298 }
3299
3300 input_panel_surface->surface = surface;
3301 input_panel_surface->listener.notify = destroy_input_panel_surface;
3302
3303 wl_signal_add(&surface_resource->destroy_signal,
3304 &input_panel_surface->listener);
3305
3306 wl_list_insert(&shell->input_panel.surfaces,
3307 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003308}
3309
3310static const struct input_panel_interface input_panel_implementation = {
3311 input_panel_set_surface
3312};
3313
3314static void
3315unbind_input_panel(struct wl_resource *resource)
3316{
3317 struct desktop_shell *shell = resource->data;
3318
3319 shell->input_panel.binding = NULL;
3320 free(resource);
3321}
3322
3323static void
3324bind_input_panel(struct wl_client *client,
3325 void *data, uint32_t version, uint32_t id)
3326{
3327 struct desktop_shell *shell = data;
3328 struct wl_resource *resource;
3329
3330 resource = wl_client_add_object(client, &input_panel_interface,
3331 &input_panel_implementation,
3332 id, shell);
3333
3334 if (shell->input_panel.binding == NULL) {
3335 resource->destroy = unbind_input_panel;
3336 shell->input_panel.binding = resource;
3337 return;
3338 }
3339
3340 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3341 "interface object already bound");
3342 wl_resource_destroy(resource);
3343}
3344
Kristian Høgsberg07045392012-02-19 18:52:44 -05003345struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003346 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003347 struct weston_surface *current;
3348 struct wl_listener listener;
3349 struct wl_keyboard_grab grab;
3350};
3351
3352static void
3353switcher_next(struct switcher *switcher)
3354{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003355 struct weston_surface *surface;
3356 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003357 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003358 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003359
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003360 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003361 switch (get_shell_surface_type(surface)) {
3362 case SHELL_SURFACE_TOPLEVEL:
3363 case SHELL_SURFACE_FULLSCREEN:
3364 case SHELL_SURFACE_MAXIMIZED:
3365 if (first == NULL)
3366 first = surface;
3367 if (prev == switcher->current)
3368 next = surface;
3369 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003370 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003371 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003372 weston_surface_damage(surface);
3373 break;
3374 default:
3375 break;
3376 }
Alex Wu1659daa2012-04-01 20:13:09 +08003377
3378 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003379 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003380 surface->geometry.dirty = 1;
3381 weston_surface_damage(surface);
3382 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003383 }
3384
3385 if (next == NULL)
3386 next = first;
3387
Alex Wu07b26062012-03-12 16:06:01 +08003388 if (next == NULL)
3389 return;
3390
Kristian Høgsberg07045392012-02-19 18:52:44 -05003391 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003392 wl_signal_add(&next->surface.resource.destroy_signal,
3393 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003394
3395 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003396 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003397
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003398 shsurf = get_shell_surface(switcher->current);
3399 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003400 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003401}
3402
3403static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003404switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003405{
3406 struct switcher *switcher =
3407 container_of(listener, struct switcher, listener);
3408
3409 switcher_next(switcher);
3410}
3411
3412static void
Daniel Stone351eb612012-05-31 15:27:47 -04003413switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003414{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003415 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003416 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003417 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003418
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003419 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003420 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003421 weston_surface_damage(surface);
3422 }
3423
Alex Wu07b26062012-03-12 16:06:01 +08003424 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003425 activate(switcher->shell, switcher->current,
3426 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003427 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003428 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003429 free(switcher);
3430}
3431
3432static void
3433switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003434 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003435{
3436 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003437 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003438
Daniel Stonec9785ea2012-05-30 16:31:52 +01003439 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003440 switcher_next(switcher);
3441}
3442
3443static void
3444switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3445 uint32_t mods_depressed, uint32_t mods_latched,
3446 uint32_t mods_locked, uint32_t group)
3447{
3448 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003449 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003450
Daniel Stone351eb612012-05-31 15:27:47 -04003451 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3452 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003453}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003454
3455static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003456 switcher_key,
3457 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003458};
3459
3460static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003461switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3462 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003463{
Tiago Vignattibe143262012-04-16 17:31:41 +03003464 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003465 struct switcher *switcher;
3466
3467 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003468 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003469 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003470 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003471 wl_list_init(&switcher->listener.link);
3472
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003473 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003474 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003475 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3476 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003477 switcher_next(switcher);
3478}
3479
Pekka Paalanen3c647232011-12-22 13:43:43 +02003480static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003481backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3482 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003483{
3484 struct weston_compositor *compositor = data;
3485 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003486 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003487
3488 /* TODO: we're limiting to simple use cases, where we assume just
3489 * control on the primary display. We'd have to extend later if we
3490 * ever get support for setting backlights on random desktop LCD
3491 * panels though */
3492 output = get_default_output(compositor);
3493 if (!output)
3494 return;
3495
3496 if (!output->set_backlight)
3497 return;
3498
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003499 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3500 backlight_new = output->backlight_current - 25;
3501 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3502 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003503
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003504 if (backlight_new < 5)
3505 backlight_new = 5;
3506 if (backlight_new > 255)
3507 backlight_new = 255;
3508
3509 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003510 output->set_backlight(output, output->backlight_current);
3511}
3512
3513static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003514fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3515 void *data)
3516{
3517 struct desktop_shell *shell = data;
3518 struct weston_compositor *compositor = shell->compositor;
3519 compositor->fan_debug = !compositor->fan_debug;
3520 weston_compositor_damage_all(compositor);
3521}
3522
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003523struct debug_binding_grab {
3524 struct wl_keyboard_grab grab;
3525 struct weston_seat *seat;
3526 uint32_t key[2];
3527 int key_released[2];
3528};
3529
3530static void
3531debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3532 uint32_t key, uint32_t state)
3533{
3534 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3535 struct wl_resource *resource;
3536 struct wl_display *display;
3537 uint32_t serial;
3538 int send = 0, terminate = 0;
3539 int check_binding = 1;
3540 int i;
3541
3542 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3543 /* Do not run bindings on key releases */
3544 check_binding = 0;
3545
3546 for (i = 0; i < 2; i++)
3547 if (key == db->key[i])
3548 db->key_released[i] = 1;
3549
3550 if (db->key_released[0] && db->key_released[1]) {
3551 /* All key releases been swalled so end the grab */
3552 terminate = 1;
3553 } else if (key != db->key[0] && key != db->key[1]) {
3554 /* Should not swallow release of other keys */
3555 send = 1;
3556 }
3557 } else if (key == db->key[0] && !db->key_released[0]) {
3558 /* Do not check bindings for the first press of the binding
3559 * key. This allows it to be used as a debug shortcut.
3560 * We still need to swallow this event. */
3561 check_binding = 0;
3562 } else if (db->key[1]) {
3563 /* If we already ran a binding don't process another one since
3564 * we can't keep track of all the binding keys that were
3565 * pressed in order to swallow the release events. */
3566 send = 1;
3567 check_binding = 0;
3568 }
3569
3570 if (check_binding) {
3571 struct weston_compositor *ec = db->seat->compositor;
3572
3573 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3574 key, state)) {
3575 /* We ran a binding so swallow the press and keep the
3576 * grab to swallow the released too. */
3577 send = 0;
3578 terminate = 0;
3579 db->key[1] = key;
3580 } else {
3581 /* Terminate the grab since the key pressed is not a
3582 * debug binding key. */
3583 send = 1;
3584 terminate = 1;
3585 }
3586 }
3587
3588 if (send) {
3589 resource = grab->keyboard->focus_resource;
3590
3591 if (resource) {
3592 display = wl_client_get_display(resource->client);
3593 serial = wl_display_next_serial(display);
3594 wl_keyboard_send_key(resource, serial, time, key, state);
3595 }
3596 }
3597
3598 if (terminate) {
3599 wl_keyboard_end_grab(grab->keyboard);
3600 free(db);
3601 }
3602}
3603
3604static void
3605debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3606 uint32_t mods_depressed, uint32_t mods_latched,
3607 uint32_t mods_locked, uint32_t group)
3608{
3609 struct wl_resource *resource;
3610
3611 resource = grab->keyboard->focus_resource;
3612 if (!resource)
3613 return;
3614
3615 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3616 mods_latched, mods_locked, group);
3617}
3618
3619struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3620 debug_binding_key,
3621 debug_binding_modifiers
3622};
3623
3624static void
3625debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3626{
3627 struct debug_binding_grab *grab;
3628
3629 grab = calloc(1, sizeof *grab);
3630 if (!grab)
3631 return;
3632
3633 grab->seat = (struct weston_seat *) seat;
3634 grab->key[0] = key;
3635 grab->grab.interface = &debug_binding_keyboard_grab;
3636 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3637}
3638
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003639static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003640force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3641 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003642{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003643 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003644 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003645 struct desktop_shell *shell = data;
3646 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003647 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003648
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003649 focus_surface = seat->keyboard->focus;
3650 if (!focus_surface)
3651 return;
3652
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003653 wl_signal_emit(&compositor->kill_signal, focus_surface);
3654
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003655 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003656 wl_client_get_credentials(client, &pid, NULL, NULL);
3657
3658 /* Skip clients that we launched ourselves (the credentials of
3659 * the socketpair is ours) */
3660 if (pid == getpid())
3661 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003662
Daniel Stone325fc2d2012-05-30 16:31:58 +01003663 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003664}
3665
3666static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003667workspace_up_binding(struct wl_seat *seat, uint32_t time,
3668 uint32_t key, void *data)
3669{
3670 struct desktop_shell *shell = data;
3671 unsigned int new_index = shell->workspaces.current;
3672
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003673 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003674 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003675 if (new_index != 0)
3676 new_index--;
3677
3678 change_workspace(shell, new_index);
3679}
3680
3681static void
3682workspace_down_binding(struct wl_seat *seat, uint32_t time,
3683 uint32_t key, void *data)
3684{
3685 struct desktop_shell *shell = data;
3686 unsigned int new_index = shell->workspaces.current;
3687
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003688 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003689 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003690 if (new_index < shell->workspaces.num - 1)
3691 new_index++;
3692
3693 change_workspace(shell, new_index);
3694}
3695
3696static void
3697workspace_f_binding(struct wl_seat *seat, uint32_t time,
3698 uint32_t key, void *data)
3699{
3700 struct desktop_shell *shell = data;
3701 unsigned int new_index;
3702
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003703 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003704 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003705 new_index = key - KEY_F1;
3706 if (new_index >= shell->workspaces.num)
3707 new_index = shell->workspaces.num - 1;
3708
3709 change_workspace(shell, new_index);
3710}
3711
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003712static void
3713workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3714 uint32_t key, void *data)
3715{
3716 struct desktop_shell *shell = data;
3717 unsigned int new_index = shell->workspaces.current;
3718
3719 if (shell->locked)
3720 return;
3721
3722 if (new_index != 0)
3723 new_index--;
3724
3725 take_surface_to_workspace_by_seat(shell, seat, new_index);
3726}
3727
3728static void
3729workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3730 uint32_t key, void *data)
3731{
3732 struct desktop_shell *shell = data;
3733 unsigned int new_index = shell->workspaces.current;
3734
3735 if (shell->locked)
3736 return;
3737
3738 if (new_index < shell->workspaces.num - 1)
3739 new_index++;
3740
3741 take_surface_to_workspace_by_seat(shell, seat, new_index);
3742}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003743
3744static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003745shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003746{
Tiago Vignattibe143262012-04-16 17:31:41 +03003747 struct desktop_shell *shell =
3748 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003749 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003750
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003751 if (shell->child.client)
3752 wl_client_destroy(shell->child.client);
3753
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003754 wl_list_remove(&shell->lock_listener.link);
3755 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003756 wl_list_remove(&shell->show_input_panel_listener.link);
3757 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003758
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003759 wl_array_for_each(ws, &shell->workspaces.array)
3760 workspace_destroy(*ws);
3761 wl_array_release(&shell->workspaces.array);
3762
Pekka Paalanen3c647232011-12-22 13:43:43 +02003763 free(shell->screensaver.path);
3764 free(shell);
3765}
3766
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003767static void
3768shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3769{
3770 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003771 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003772
3773 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003774 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3775 MODIFIER_CTRL | MODIFIER_ALT,
3776 terminate_binding, ec);
3777 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3778 click_to_activate_binding,
3779 shell);
3780 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3781 MODIFIER_SUPER | MODIFIER_ALT,
3782 surface_opacity_binding, NULL);
3783 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3784 MODIFIER_SUPER, zoom_axis_binding,
3785 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003786
3787 /* configurable bindings */
3788 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003789 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3790 zoom_key_binding, NULL);
3791 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3792 zoom_key_binding, NULL);
3793 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3794 shell);
3795 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3796 resize_binding, shell);
3797 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3798 rotate_binding, NULL);
3799 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3800 shell);
3801 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3802 ec);
3803 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3804 backlight_binding, ec);
3805 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3806 ec);
3807 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3808 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003809 weston_compositor_add_key_binding(ec, KEY_K, mod,
3810 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003811 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3812 workspace_up_binding, shell);
3813 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3814 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003815 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3816 workspace_move_surface_up_binding,
3817 shell);
3818 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3819 workspace_move_surface_down_binding,
3820 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003821
3822 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3823 if (shell->workspaces.num > 1) {
3824 num_workspace_bindings = shell->workspaces.num;
3825 if (num_workspace_bindings > 6)
3826 num_workspace_bindings = 6;
3827 for (i = 0; i < num_workspace_bindings; i++)
3828 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3829 workspace_f_binding,
3830 shell);
3831 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003832
3833 /* Debug bindings */
3834 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3835 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003836 weston_compositor_add_debug_binding(ec, KEY_F,
3837 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003838}
3839
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003840WL_EXPORT int
Kristian Høgsbergb00a9d32012-09-11 14:06:27 -04003841module_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003842{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003843 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003844 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003845 struct workspace **pws;
3846 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003847 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003848
3849 shell = malloc(sizeof *shell);
3850 if (shell == NULL)
3851 return -1;
3852
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003853 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003854 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003855
3856 shell->destroy_listener.notify = shell_destroy;
3857 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3858 shell->lock_listener.notify = lock;
3859 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3860 shell->unlock_listener.notify = unlock;
3861 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003862 shell->show_input_panel_listener.notify = show_input_panels;
3863 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3864 shell->hide_input_panel_listener.notify = hide_input_panels;
3865 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003866 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003867 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003868 ec->shell_interface.create_shell_surface = create_shell_surface;
3869 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003870 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003871 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003872 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003873 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003874
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003875 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003876
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003877 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3878 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003879 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3880 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003881 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003882
3883 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02003884 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003885
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003886 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003887
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003888 for (i = 0; i < shell->workspaces.num; i++) {
3889 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3890 if (pws == NULL)
3891 return -1;
3892
3893 *pws = workspace_create();
3894 if (*pws == NULL)
3895 return -1;
3896 }
3897 activate_workspace(shell, 0);
3898
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003899 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003900 wl_list_init(&shell->workspaces.animation.link);
3901 shell->workspaces.animation.frame = animate_workspace_change_frame;
3902
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003903 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3904 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003905 return -1;
3906
Kristian Høgsberg75840622011-09-06 13:48:16 -04003907 if (wl_display_add_global(ec->wl_display,
3908 &desktop_shell_interface,
3909 shell, bind_desktop_shell) == NULL)
3910 return -1;
3911
Pekka Paalanen6e168112011-11-24 11:34:05 +02003912 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3913 shell, bind_screensaver) == NULL)
3914 return -1;
3915
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003916 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3917 shell, bind_input_panel) == NULL)
3918 return -1;
3919
Jonas Ådahle9d22502012-08-29 22:13:01 +02003920 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3921 shell, bind_workspace_manager) == NULL)
3922 return -1;
3923
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003924 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003925
3926 loop = wl_display_get_event_loop(ec->wl_display);
3927 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003928
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003929 wl_list_for_each(seat, &ec->seat_list, link)
3930 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003931
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003932 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003933
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003934 return 0;
3935}