blob: cef4011d7b8802bee3e4b0b0c9d5208e0db7792a [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;
127 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500128 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200129 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500130
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200131 struct {
132 struct wl_resource *binding;
133 struct wl_list surfaces;
134 } input_panel;
135
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300136 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800137 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500138 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400139};
140
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500141enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200142 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500143 SHELL_SURFACE_TOPLEVEL,
144 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800146 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500147 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200148};
149
Scott Moreauff1db4a2012-04-17 19:06:18 -0600150struct ping_timer {
151 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600152 uint32_t serial;
153};
154
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200155struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200156 struct wl_resource resource;
157
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500158 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200159 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400160 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300161 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200162
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400163 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400164 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500165 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800166 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800167 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600168 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100169
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500170 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200171 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500172 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200173 } rotation;
174
175 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700176 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500177 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200178 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500179 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100180 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400181 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500182 } popup;
183
Alex Wu4539b082012-03-01 12:57:46 +0800184 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300185 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400186 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300187 } transient;
188
189 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800190 enum wl_shell_surface_fullscreen_method type;
191 struct weston_transform transform; /* matrix from x, y */
192 uint32_t framerate;
193 struct weston_surface *black_surface;
194 } fullscreen;
195
Scott Moreauff1db4a2012-04-17 19:06:18 -0600196 struct ping_timer *ping_timer;
197
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200198 struct weston_transform workspace_transform;
199
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500200 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500201 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100202 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400203
204 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200205};
206
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700208 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300209 struct shell_surface *shsurf;
210 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300211 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300212};
213
214struct weston_move_grab {
215 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100216 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500217};
218
Pekka Paalanen460099f2012-01-20 16:48:25 +0200219struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300220 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500221 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200222 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200223 float x;
224 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200225 } center;
226};
227
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400228static void
229activate(struct desktop_shell *shell, struct weston_surface *es,
230 struct weston_seat *seat);
231
232static struct workspace *
233get_current_workspace(struct desktop_shell *shell);
234
Alex Wubd3354b2012-04-17 17:20:49 +0800235static struct shell_surface *
236get_shell_surface(struct weston_surface *surface);
237
238static struct desktop_shell *
239shell_surface_get_shell(struct shell_surface *shsurf);
240
241static bool
242shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
243{
244 struct desktop_shell *shell;
245 struct weston_surface *top_fs_es;
246
247 shell = shell_surface_get_shell(shsurf);
248
249 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
250 return false;
251
252 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
253 struct weston_surface,
254 layer_link);
255 return (shsurf == get_shell_surface(top_fs_es));
256}
257
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500258static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400259destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300260{
261 struct shell_grab *grab;
262
263 grab = container_of(listener, struct shell_grab,
264 shsurf_destroy_listener);
265
266 grab->shsurf = NULL;
267}
268
269static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300270shell_grab_start(struct shell_grab *grab,
271 const struct wl_pointer_grab_interface *interface,
272 struct shell_surface *shsurf,
273 struct wl_pointer *pointer,
274 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300275{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300276 struct desktop_shell *shell = shsurf->shell;
277
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300278 grab->grab.interface = interface;
279 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400280 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
281 wl_signal_add(&shsurf->resource.destroy_signal,
282 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300284 grab->pointer = pointer;
285 grab->grab.focus = &shsurf->surface->surface;
286
287 wl_pointer_start_grab(pointer, &grab->grab);
288 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
289 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
290 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300291}
292
293static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300294shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300295{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400296 if (grab->shsurf)
297 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300298
299 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300300}
301
302static void
Alex Wu4539b082012-03-01 12:57:46 +0800303center_on_output(struct weston_surface *surface,
304 struct weston_output *output);
305
Daniel Stone496ca172012-05-30 16:31:42 +0100306static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300307get_modifier(char *modifier)
308{
309 if (!modifier)
310 return MODIFIER_SUPER;
311
312 if (!strcmp("ctrl", modifier))
313 return MODIFIER_CTRL;
314 else if (!strcmp("alt", modifier))
315 return MODIFIER_ALT;
316 else if (!strcmp("super", modifier))
317 return MODIFIER_SUPER;
318 else
319 return MODIFIER_SUPER;
320}
321
Juan Zhaoe10d2792012-04-25 19:09:52 +0800322static enum animation_type
323get_animation_type(char *animation)
324{
325 if (!animation)
326 return ANIMATION_NONE;
327
328 if (!strcmp("zoom", animation))
329 return ANIMATION_ZOOM;
330 else if (!strcmp("fade", animation))
331 return ANIMATION_FADE;
332 else
333 return ANIMATION_NONE;
334}
335
Alex Wu4539b082012-03-01 12:57:46 +0800336static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300337shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200338{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200339 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200340 char *path = NULL;
341 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200342 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300343 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800344 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200345
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400346 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300347 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800348 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200349 { "num-workspaces",
350 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200351 };
352
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400353 struct config_key saver_keys[] = {
354 { "path", CONFIG_KEY_STRING, &path },
355 { "duration", CONFIG_KEY_INTEGER, &duration },
356 };
357
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200358 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400359 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200360 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
361 };
362
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200363 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500364 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200365 free(config_file);
366
Pekka Paalanen7296e792011-12-07 16:22:00 +0200367 shell->screensaver.path = path;
368 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300369 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800370 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200371 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
372}
373
374static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200375focus_state_destroy(struct focus_state *state)
376{
377 wl_list_remove(&state->seat_destroy_listener.link);
378 wl_list_remove(&state->surface_destroy_listener.link);
379 free(state);
380}
381
382static void
383focus_state_seat_destroy(struct wl_listener *listener, void *data)
384{
385 struct focus_state *state = container_of(listener,
386 struct focus_state,
387 seat_destroy_listener);
388
389 wl_list_remove(&state->link);
390 focus_state_destroy(state);
391}
392
393static void
394focus_state_surface_destroy(struct wl_listener *listener, void *data)
395{
396 struct focus_state *state = container_of(listener,
397 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400398 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400399 struct desktop_shell *shell;
400 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200401
Kristian Høgsberge3778222012-07-31 17:29:30 -0400402 next = NULL;
403 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
404 if (surface == state->keyboard_focus)
405 continue;
406
407 next = surface;
408 break;
409 }
410
411 if (next) {
412 shell = state->seat->compositor->shell_interface.shell;
413 activate(shell, next, state->seat);
414 } else {
415 wl_list_remove(&state->link);
416 focus_state_destroy(state);
417 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200418}
419
420static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400421focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200422{
Jonas Ådahl04769742012-06-13 00:01:24 +0200423 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200424
425 state = malloc(sizeof *state);
426 if (state == NULL)
427 return NULL;
428
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400429 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200430 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400431 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200432
433 state->seat_destroy_listener.notify = focus_state_seat_destroy;
434 state->surface_destroy_listener.notify = focus_state_surface_destroy;
435 wl_signal_add(&seat->seat.destroy_signal,
436 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400437 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200438
439 return state;
440}
441
Jonas Ådahl8538b222012-08-29 22:13:03 +0200442static struct focus_state *
443ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
444{
445 struct workspace *ws = get_current_workspace(shell);
446 struct focus_state *state;
447
448 wl_list_for_each(state, &ws->focus_list, link)
449 if (state->seat == seat)
450 break;
451
452 if (&state->link == &ws->focus_list)
453 state = focus_state_create(seat, ws);
454
455 return state;
456}
457
Jonas Ådahl04769742012-06-13 00:01:24 +0200458static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400459restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200460{
461 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200462 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200463
464 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200465 surface = state->keyboard_focus ?
466 &state->keyboard_focus->surface : NULL;
467
468 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200469 }
470}
471
472static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200473replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
474 struct weston_seat *seat)
475{
476 struct focus_state *state;
477 struct wl_surface *surface;
478
479 wl_list_for_each(state, &ws->focus_list, link) {
480 if (state->seat == seat) {
481 surface = seat->seat.keyboard->focus;
482 state->keyboard_focus =
483 (struct weston_surface *) surface;
484 return;
485 }
486 }
487}
488
489static void
490drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
491 struct weston_surface *surface)
492{
493 struct focus_state *state;
494
495 wl_list_for_each(state, &ws->focus_list, link)
496 if (state->keyboard_focus == surface)
497 state->keyboard_focus = NULL;
498}
499
500static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200501workspace_destroy(struct workspace *ws)
502{
Jonas Ådahl04769742012-06-13 00:01:24 +0200503 struct focus_state *state, *next;
504
505 wl_list_for_each_safe(state, next, &ws->focus_list, link)
506 focus_state_destroy(state);
507
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200508 free(ws);
509}
510
Jonas Ådahl04769742012-06-13 00:01:24 +0200511static void
512seat_destroyed(struct wl_listener *listener, void *data)
513{
514 struct weston_seat *seat = data;
515 struct focus_state *state, *next;
516 struct workspace *ws = container_of(listener,
517 struct workspace,
518 seat_destroyed_listener);
519
520 wl_list_for_each_safe(state, next, &ws->focus_list, link)
521 if (state->seat == seat)
522 wl_list_remove(&state->link);
523}
524
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200525static struct workspace *
526workspace_create(void)
527{
528 struct workspace *ws = malloc(sizeof *ws);
529 if (ws == NULL)
530 return NULL;
531
532 weston_layer_init(&ws->layer, NULL);
533
Jonas Ådahl04769742012-06-13 00:01:24 +0200534 wl_list_init(&ws->focus_list);
535 wl_list_init(&ws->seat_destroyed_listener.link);
536 ws->seat_destroyed_listener.notify = seat_destroyed;
537
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200538 return ws;
539}
540
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200541static int
542workspace_is_empty(struct workspace *ws)
543{
544 return wl_list_empty(&ws->layer.surface_list);
545}
546
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200547static struct workspace *
548get_workspace(struct desktop_shell *shell, unsigned int index)
549{
550 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200551 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200552 pws += index;
553 return *pws;
554}
555
556static struct workspace *
557get_current_workspace(struct desktop_shell *shell)
558{
559 return get_workspace(shell, shell->workspaces.current);
560}
561
562static void
563activate_workspace(struct desktop_shell *shell, unsigned int index)
564{
565 struct workspace *ws;
566
567 ws = get_workspace(shell, index);
568 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
569
570 shell->workspaces.current = index;
571}
572
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200573static unsigned int
574get_output_height(struct weston_output *output)
575{
576 return abs(output->region.extents.y1 - output->region.extents.y2);
577}
578
579static void
580surface_translate(struct weston_surface *surface, double d)
581{
582 struct shell_surface *shsurf = get_shell_surface(surface);
583 struct weston_transform *transform;
584
585 transform = &shsurf->workspace_transform;
586 if (wl_list_empty(&transform->link))
587 wl_list_insert(surface->geometry.transformation_list.prev,
588 &shsurf->workspace_transform.link);
589
590 weston_matrix_init(&shsurf->workspace_transform.matrix);
591 weston_matrix_translate(&shsurf->workspace_transform.matrix,
592 0.0, d, 0.0);
593 surface->geometry.dirty = 1;
594}
595
596static void
597workspace_translate_out(struct workspace *ws, double fraction)
598{
599 struct weston_surface *surface;
600 unsigned int height;
601 double d;
602
603 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
604 height = get_output_height(surface->output);
605 d = height * fraction;
606
607 surface_translate(surface, d);
608 }
609}
610
611static void
612workspace_translate_in(struct workspace *ws, double fraction)
613{
614 struct weston_surface *surface;
615 unsigned int height;
616 double d;
617
618 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
619 height = get_output_height(surface->output);
620
621 if (fraction > 0)
622 d = -(height - height * fraction);
623 else
624 d = height + height * fraction;
625
626 surface_translate(surface, d);
627 }
628}
629
630static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200631broadcast_current_workspace_state(struct desktop_shell *shell)
632{
633 struct wl_resource *resource;
634
635 wl_list_for_each(resource, &shell->workspaces.client_list, link)
636 workspace_manager_send_state(resource,
637 shell->workspaces.current,
638 shell->workspaces.num);
639}
640
641static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200642reverse_workspace_change_animation(struct desktop_shell *shell,
643 unsigned int index,
644 struct workspace *from,
645 struct workspace *to)
646{
647 shell->workspaces.current = index;
648
649 shell->workspaces.anim_to = to;
650 shell->workspaces.anim_from = from;
651 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
652 shell->workspaces.anim_timestamp = 0;
653
Scott Moreau4272e632012-08-13 09:58:41 -0600654 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200655}
656
657static void
658workspace_deactivate_transforms(struct workspace *ws)
659{
660 struct weston_surface *surface;
661 struct shell_surface *shsurf;
662
663 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
664 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200665 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
666 wl_list_remove(&shsurf->workspace_transform.link);
667 wl_list_init(&shsurf->workspace_transform.link);
668 }
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200669 shsurf->surface->geometry.dirty = 1;
670 }
671}
672
673static void
674finish_workspace_change_animation(struct desktop_shell *shell,
675 struct workspace *from,
676 struct workspace *to)
677{
Scott Moreau4272e632012-08-13 09:58:41 -0600678 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200679
680 wl_list_remove(&shell->workspaces.animation.link);
681 workspace_deactivate_transforms(from);
682 workspace_deactivate_transforms(to);
683 shell->workspaces.anim_to = NULL;
684
685 wl_list_remove(&shell->workspaces.anim_from->layer.link);
686}
687
688static void
689animate_workspace_change_frame(struct weston_animation *animation,
690 struct weston_output *output, uint32_t msecs)
691{
692 struct desktop_shell *shell =
693 container_of(animation, struct desktop_shell,
694 workspaces.animation);
695 struct workspace *from = shell->workspaces.anim_from;
696 struct workspace *to = shell->workspaces.anim_to;
697 uint32_t t;
698 double x, y;
699
700 if (workspace_is_empty(from) && workspace_is_empty(to)) {
701 finish_workspace_change_animation(shell, from, to);
702 return;
703 }
704
705 if (shell->workspaces.anim_timestamp == 0) {
706 if (shell->workspaces.anim_current == 0.0)
707 shell->workspaces.anim_timestamp = msecs;
708 else
709 shell->workspaces.anim_timestamp =
710 msecs -
711 /* Invers of movement function 'y' below. */
712 (asin(1.0 - shell->workspaces.anim_current) *
713 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
714 M_2_PI);
715 }
716
717 t = msecs - shell->workspaces.anim_timestamp;
718
719 /*
720 * x = [0, π/2]
721 * y(x) = sin(x)
722 */
723 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
724 y = sin(x);
725
726 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600727 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200728
729 workspace_translate_out(from, shell->workspaces.anim_dir * y);
730 workspace_translate_in(to, shell->workspaces.anim_dir * y);
731 shell->workspaces.anim_current = y;
732
Scott Moreau4272e632012-08-13 09:58:41 -0600733 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200734 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200735 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200736 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200737}
738
739static void
740animate_workspace_change(struct desktop_shell *shell,
741 unsigned int index,
742 struct workspace *from,
743 struct workspace *to)
744{
745 struct weston_output *output;
746
747 int dir;
748
749 if (index > shell->workspaces.current)
750 dir = -1;
751 else
752 dir = 1;
753
754 shell->workspaces.current = index;
755
756 shell->workspaces.anim_dir = dir;
757 shell->workspaces.anim_from = from;
758 shell->workspaces.anim_to = to;
759 shell->workspaces.anim_current = 0.0;
760 shell->workspaces.anim_timestamp = 0;
761
762 output = container_of(shell->compositor->output_list.next,
763 struct weston_output, link);
764 wl_list_insert(&output->animation_list,
765 &shell->workspaces.animation.link);
766
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200767 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200768
769 workspace_translate_in(to, 0);
770
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400771 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200772
Scott Moreau4272e632012-08-13 09:58:41 -0600773 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200774}
775
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200776static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200777update_workspace(struct desktop_shell *shell, unsigned int index,
778 struct workspace *from, struct workspace *to)
779{
780 shell->workspaces.current = index;
781 wl_list_insert(&from->layer.link, &to->layer.link);
782 wl_list_remove(&from->layer.link);
783}
784
785static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200786change_workspace(struct desktop_shell *shell, unsigned int index)
787{
788 struct workspace *from;
789 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200790
791 if (index == shell->workspaces.current)
792 return;
793
794 /* Don't change workspace when there is any fullscreen surfaces. */
795 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
796 return;
797
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200798 from = get_current_workspace(shell);
799 to = get_workspace(shell, index);
800
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200801 if (shell->workspaces.anim_from == to &&
802 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200803 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200804 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200805 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200806 return;
807 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200808
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200809 if (shell->workspaces.anim_to != NULL)
810 finish_workspace_change_animation(shell,
811 shell->workspaces.anim_from,
812 shell->workspaces.anim_to);
813
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200814 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200815
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200816 if (workspace_is_empty(to) && workspace_is_empty(from))
817 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200818 else
819 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200820
821 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200822}
823
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200824static bool
825workspace_has_only(struct workspace *ws, struct weston_surface *surface)
826{
827 struct wl_list *list = &ws->layer.surface_list;
828 struct wl_list *e;
829
830 if (wl_list_empty(list))
831 return false;
832
833 e = list->next;
834
835 if (e->next != list)
836 return false;
837
838 return container_of(e, struct weston_surface, layer_link) == surface;
839}
840
841static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200842move_surface_to_workspace(struct desktop_shell *shell,
843 struct weston_surface *surface,
844 uint32_t workspace)
845{
846 struct workspace *from;
847 struct workspace *to;
848 struct weston_seat *seat;
849
850 if (workspace == shell->workspaces.current)
851 return;
852
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200853 if (workspace >= shell->workspaces.num)
854 workspace = shell->workspaces.num - 1;
855
Jonas Ådahle9d22502012-08-29 22:13:01 +0200856 from = get_current_workspace(shell);
857 to = get_workspace(shell, workspace);
858
859 wl_list_remove(&surface->layer_link);
860 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
861
862 drop_focus_state(shell, from, surface);
863 wl_list_for_each(seat, &shell->compositor->seat_list, link)
864 if (seat->has_keyboard &&
865 seat->keyboard.focus == &surface->surface)
866 wl_keyboard_set_focus(&seat->keyboard, NULL);
867
868 weston_surface_damage_below(surface);
869}
870
871static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200872take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200873 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200874 unsigned int index)
875{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200876 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200877 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200878 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200879 struct shell_surface *shsurf;
880 struct workspace *from;
881 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200882 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200883
884 if (surface == NULL ||
885 index == shell->workspaces.current)
886 return;
887
888 from = get_current_workspace(shell);
889 to = get_workspace(shell, index);
890
891 wl_list_remove(&surface->layer_link);
892 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
893
Jonas Ådahle9d22502012-08-29 22:13:01 +0200894 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200895 drop_focus_state(shell, from, surface);
896
897 if (shell->workspaces.anim_from == to &&
898 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200899 wl_list_remove(&to->layer.link);
900 wl_list_insert(from->layer.link.prev, &to->layer.link);
901
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200902 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200903 broadcast_current_workspace_state(shell);
904
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200905 return;
906 }
907
908 if (shell->workspaces.anim_to != NULL)
909 finish_workspace_change_animation(shell,
910 shell->workspaces.anim_from,
911 shell->workspaces.anim_to);
912
913 if (workspace_is_empty(from) &&
914 workspace_has_only(to, surface))
915 update_workspace(shell, index, from, to);
916 else {
917 shsurf = get_shell_surface(surface);
918 if (wl_list_empty(&shsurf->workspace_transform.link))
919 wl_list_insert(&shell->workspaces.anim_sticky_list,
920 &shsurf->workspace_transform.link);
921
922 animate_workspace_change(shell, index, from, to);
923 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200924
925 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200926
927 state = ensure_focus_state(shell, seat);
928 if (state != NULL)
929 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200930}
931
932static void
933workspace_manager_move_surface(struct wl_client *client,
934 struct wl_resource *resource,
935 struct wl_resource *surface_resource,
936 uint32_t workspace)
937{
938 struct desktop_shell *shell = resource->data;
939 struct weston_surface *surface =
940 (struct weston_surface *) surface_resource;
941
942 move_surface_to_workspace(shell, surface, workspace);
943}
944
945static const struct workspace_manager_interface workspace_manager_implementation = {
946 workspace_manager_move_surface,
947};
948
949static void
950unbind_resource(struct wl_resource *resource)
951{
952 wl_list_remove(&resource->link);
953 free(resource);
954}
955
956static void
957bind_workspace_manager(struct wl_client *client,
958 void *data, uint32_t version, uint32_t id)
959{
960 struct desktop_shell *shell = data;
961 struct wl_resource *resource;
962
963 resource = wl_client_add_object(client, &workspace_manager_interface,
964 &workspace_manager_implementation,
965 id, shell);
966
967 if (resource == NULL) {
968 weston_log("couldn't add workspace manager object");
969 return;
970 }
971
972 resource->destroy = unbind_resource;
973 wl_list_insert(&shell->workspaces.client_list, &resource->link);
974
975 workspace_manager_send_state(resource,
976 shell->workspaces.current,
977 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200978}
979
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200980static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400981noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100982 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500983{
984 grab->focus = NULL;
985}
986
987static void
Scott Moreau447013d2012-02-18 05:05:29 -0700988move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100989 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500990{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500991 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100992 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300993 struct shell_surface *shsurf = move->base.shsurf;
994 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100995 int dx = wl_fixed_to_int(pointer->x + move->dx);
996 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300997
998 if (!shsurf)
999 return;
1000
1001 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001002
Daniel Stone103db7f2012-05-08 17:17:55 +01001003 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001004 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001005
1006 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001007}
1008
1009static void
Scott Moreau447013d2012-02-18 05:05:29 -07001010move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001011 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001012{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001013 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1014 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001015 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001016 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001017
Daniel Stone4dbadb12012-05-30 16:31:51 +01001018 if (pointer->button_count == 0 &&
1019 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001020 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001021 free(grab);
1022 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023}
1024
Scott Moreau447013d2012-02-18 05:05:29 -07001025static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001026 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001027 move_grab_motion,
1028 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001029};
1030
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001031static int
1032surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1033{
1034 struct weston_move_grab *move;
1035
1036 if (!shsurf)
1037 return -1;
1038
1039 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1040 return 0;
1041
1042 move = malloc(sizeof *move);
1043 if (!move)
1044 return -1;
1045
1046 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1047 ws->seat.pointer->grab_x;
1048 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1049 ws->seat.pointer->grab_y;
1050
1051 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1052 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1053
1054 return 0;
1055}
1056
1057static void
1058shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1059 struct wl_resource *seat_resource, uint32_t serial)
1060{
1061 struct weston_seat *ws = seat_resource->data;
1062 struct shell_surface *shsurf = resource->data;
1063
1064 if (ws->seat.pointer->button_count == 0 ||
1065 ws->seat.pointer->grab_serial != serial ||
1066 ws->seat.pointer->focus != &shsurf->surface->surface)
1067 return;
1068
1069 if (surface_move(shsurf, ws) < 0)
1070 wl_resource_post_no_memory(resource);
1071}
1072
1073struct weston_resize_grab {
1074 struct shell_grab base;
1075 uint32_t edges;
1076 int32_t width, height;
1077};
1078
1079static void
1080resize_grab_motion(struct wl_pointer_grab *grab,
1081 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1082{
1083 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1084 struct wl_pointer *pointer = grab->pointer;
1085 struct shell_surface *shsurf = resize->base.shsurf;
1086 int32_t width, height;
1087 wl_fixed_t from_x, from_y;
1088 wl_fixed_t to_x, to_y;
1089
1090 if (!shsurf)
1091 return;
1092
1093 weston_surface_from_global_fixed(shsurf->surface,
1094 pointer->grab_x, pointer->grab_y,
1095 &from_x, &from_y);
1096 weston_surface_from_global_fixed(shsurf->surface,
1097 pointer->x, pointer->y, &to_x, &to_y);
1098
1099 width = resize->width;
1100 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1101 width += wl_fixed_to_int(from_x - to_x);
1102 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1103 width += wl_fixed_to_int(to_x - from_x);
1104 }
1105
1106 height = resize->height;
1107 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1108 height += wl_fixed_to_int(from_y - to_y);
1109 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1110 height += wl_fixed_to_int(to_y - from_y);
1111 }
1112
1113 shsurf->client->send_configure(shsurf->surface,
1114 resize->edges, width, height);
1115}
1116
1117static void
1118send_configure(struct weston_surface *surface,
1119 uint32_t edges, int32_t width, int32_t height)
1120{
1121 struct shell_surface *shsurf = get_shell_surface(surface);
1122
1123 wl_shell_surface_send_configure(&shsurf->resource,
1124 edges, width, height);
1125}
1126
1127static const struct weston_shell_client shell_client = {
1128 send_configure
1129};
1130
1131static void
1132resize_grab_button(struct wl_pointer_grab *grab,
1133 uint32_t time, uint32_t button, uint32_t state_w)
1134{
1135 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1136 struct wl_pointer *pointer = grab->pointer;
1137 enum wl_pointer_button_state state = state_w;
1138
1139 if (pointer->button_count == 0 &&
1140 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1141 shell_grab_end(&resize->base);
1142 free(grab);
1143 }
1144}
1145
1146static const struct wl_pointer_grab_interface resize_grab_interface = {
1147 noop_grab_focus,
1148 resize_grab_motion,
1149 resize_grab_button,
1150};
1151
1152static int
1153surface_resize(struct shell_surface *shsurf,
1154 struct weston_seat *ws, uint32_t edges)
1155{
1156 struct weston_resize_grab *resize;
1157
1158 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1159 return 0;
1160
1161 if (edges == 0 || edges > 15 ||
1162 (edges & 3) == 3 || (edges & 12) == 12)
1163 return 0;
1164
1165 resize = malloc(sizeof *resize);
1166 if (!resize)
1167 return -1;
1168
1169 resize->edges = edges;
1170 resize->width = shsurf->surface->geometry.width;
1171 resize->height = shsurf->surface->geometry.height;
1172
1173 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1174 ws->seat.pointer, edges);
1175
1176 return 0;
1177}
1178
1179static void
1180shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1181 struct wl_resource *seat_resource, uint32_t serial,
1182 uint32_t edges)
1183{
1184 struct weston_seat *ws = seat_resource->data;
1185 struct shell_surface *shsurf = resource->data;
1186
1187 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1188 return;
1189
1190 if (ws->seat.pointer->button_count == 0 ||
1191 ws->seat.pointer->grab_serial != serial ||
1192 ws->seat.pointer->focus != &shsurf->surface->surface)
1193 return;
1194
1195 if (surface_resize(shsurf, ws, edges) < 0)
1196 wl_resource_post_no_memory(resource);
1197}
1198
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001199static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001200busy_cursor_grab_focus(struct wl_pointer_grab *base,
1201 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001202{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001203 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001204
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001205 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001206 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001207 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001208 }
1209}
1210
1211static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001212busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1213 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001214{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001215}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001216
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001217static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001218busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001219 uint32_t time, uint32_t button, uint32_t state)
1220{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001221 struct shell_grab *grab = (struct shell_grab *) base;
1222 struct shell_surface *shsurf;
1223 struct weston_surface *surface =
1224 (struct weston_surface *) grab->grab.pointer->current;
1225 struct weston_seat *seat =
1226 (struct weston_seat *) grab->grab.pointer->seat;
1227
1228 shsurf = get_shell_surface(surface);
1229 if (shsurf && button == BTN_LEFT && state) {
1230 activate(shsurf->shell, shsurf->surface, seat);
1231 surface_move(shsurf, seat);
1232 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001233}
1234
1235static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1236 busy_cursor_grab_focus,
1237 busy_cursor_grab_motion,
1238 busy_cursor_grab_button,
1239};
1240
1241static void
1242set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1243{
1244 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001245
1246 grab = malloc(sizeof *grab);
1247 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001248 return;
1249
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001250 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1251 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001252}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001253
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001254static void
1255end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1256{
1257 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1258
1259 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001260 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001261 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001262 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001263}
1264
Scott Moreau9521d5e2012-04-19 13:06:17 -06001265static void
1266ping_timer_destroy(struct shell_surface *shsurf)
1267{
1268 if (!shsurf || !shsurf->ping_timer)
1269 return;
1270
1271 if (shsurf->ping_timer->source)
1272 wl_event_source_remove(shsurf->ping_timer->source);
1273
1274 free(shsurf->ping_timer);
1275 shsurf->ping_timer = NULL;
1276}
1277
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001278static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001279ping_timeout_handler(void *data)
1280{
1281 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001282 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001283
Scott Moreau9521d5e2012-04-19 13:06:17 -06001284 /* Client is not responding */
1285 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001286
1287 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1288 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1289 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001290
1291 return 1;
1292}
1293
1294static void
1295ping_handler(struct weston_surface *surface, uint32_t serial)
1296{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001297 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001298 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001299 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001300
1301 if (!shsurf)
1302 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001303 if (!shsurf->resource.client)
1304 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001305
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001306 if (shsurf->surface == shsurf->shell->grab_surface)
1307 return;
1308
Scott Moreauff1db4a2012-04-17 19:06:18 -06001309 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001310 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001311 if (!shsurf->ping_timer)
1312 return;
1313
1314 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001315 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1316 shsurf->ping_timer->source =
1317 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1318 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1319
1320 wl_shell_surface_send_ping(&shsurf->resource, serial);
1321 }
1322}
1323
1324static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001325handle_pointer_focus(struct wl_listener *listener, void *data)
1326{
1327 struct wl_pointer *pointer = data;
1328 struct weston_surface *surface =
1329 (struct weston_surface *) pointer->focus;
1330 struct weston_compositor *compositor;
1331 struct shell_surface *shsurf;
1332 uint32_t serial;
1333
1334 if (!surface)
1335 return;
1336
1337 compositor = surface->compositor;
1338 shsurf = get_shell_surface(surface);
1339
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001340 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001341 set_busy_cursor(shsurf, pointer);
1342 } else {
1343 serial = wl_display_next_serial(compositor->wl_display);
1344 ping_handler(surface, serial);
1345 }
1346}
1347
1348static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001349create_pointer_focus_listener(struct weston_seat *seat)
1350{
1351 struct wl_listener *listener;
1352
1353 if (!seat->seat.pointer)
1354 return;
1355
1356 listener = malloc(sizeof *listener);
1357 listener->notify = handle_pointer_focus;
1358 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1359}
1360
1361static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001362shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1363 uint32_t serial)
1364{
1365 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001366 struct desktop_shell *shell = shsurf->shell;
1367 struct weston_seat *seat;
1368 struct weston_compositor *ec = shsurf->surface->compositor;
1369 struct wl_pointer *pointer;
1370 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001371
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001372 if (shsurf->ping_timer == NULL)
1373 /* Just ignore unsolicited pong. */
1374 return;
1375
Scott Moreauff1db4a2012-04-17 19:06:18 -06001376 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001377 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001378 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001379 if (was_unresponsive) {
1380 /* Received pong from previously unresponsive client */
1381 wl_list_for_each(seat, &ec->seat_list, link) {
1382 pointer = seat->seat.pointer;
1383 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001384 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001385 pointer->current ==
1386 &shsurf->surface->surface)
1387 end_busy_cursor(shsurf, pointer);
1388 }
1389 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001390 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001391 }
1392}
1393
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001394static void
1395shell_surface_set_title(struct wl_client *client,
1396 struct wl_resource *resource, const char *title)
1397{
1398 struct shell_surface *shsurf = resource->data;
1399
1400 free(shsurf->title);
1401 shsurf->title = strdup(title);
1402}
1403
1404static void
1405shell_surface_set_class(struct wl_client *client,
1406 struct wl_resource *resource, const char *class)
1407{
1408 struct shell_surface *shsurf = resource->data;
1409
1410 free(shsurf->class);
1411 shsurf->class = strdup(class);
1412}
1413
Juan Zhao96879df2012-02-07 08:45:41 +08001414static struct weston_output *
1415get_default_output(struct weston_compositor *compositor)
1416{
1417 return container_of(compositor->output_list.next,
1418 struct weston_output, link);
1419}
1420
Alex Wu4539b082012-03-01 12:57:46 +08001421static void
1422shell_unset_fullscreen(struct shell_surface *shsurf)
1423{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001424 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001425 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001426 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1427 shell_surface_is_top_fullscreen(shsurf)) {
1428 weston_output_switch_mode(shsurf->fullscreen_output,
1429 shsurf->fullscreen_output->origin);
1430 }
Alex Wu4539b082012-03-01 12:57:46 +08001431 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1432 shsurf->fullscreen.framerate = 0;
1433 wl_list_remove(&shsurf->fullscreen.transform.link);
1434 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001435 if (shsurf->fullscreen.black_surface)
1436 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001437 shsurf->fullscreen.black_surface = NULL;
1438 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001439 weston_surface_set_position(shsurf->surface,
1440 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001441 if (shsurf->saved_rotation_valid) {
1442 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1443 &shsurf->rotation.transform.link);
1444 shsurf->saved_rotation_valid = false;
1445 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001446
1447 ws = get_current_workspace(shsurf->shell);
1448 wl_list_remove(&shsurf->surface->layer_link);
1449 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001450}
1451
Pekka Paalanen98262232011-12-01 10:42:22 +02001452static int
1453reset_shell_surface_type(struct shell_surface *surface)
1454{
1455 switch (surface->type) {
1456 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001457 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001458 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001459 case SHELL_SURFACE_MAXIMIZED:
1460 surface->output = get_default_output(surface->surface->compositor);
1461 weston_surface_set_position(surface->surface,
1462 surface->saved_x,
1463 surface->saved_y);
1464 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001465 case SHELL_SURFACE_NONE:
1466 case SHELL_SURFACE_TOPLEVEL:
1467 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001468 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001469 break;
1470 }
1471
1472 surface->type = SHELL_SURFACE_NONE;
1473 return 0;
1474}
1475
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001476static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001477set_surface_type(struct shell_surface *shsurf)
1478{
1479 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001480 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001481
1482 reset_shell_surface_type(shsurf);
1483
1484 shsurf->type = shsurf->next_type;
1485 shsurf->next_type = SHELL_SURFACE_NONE;
1486
1487 switch (shsurf->type) {
1488 case SHELL_SURFACE_TOPLEVEL:
1489 break;
1490 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001491 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001492 pes->geometry.x + shsurf->transient.x,
1493 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001494 break;
1495
1496 case SHELL_SURFACE_MAXIMIZED:
1497 shsurf->saved_x = surface->geometry.x;
1498 shsurf->saved_y = surface->geometry.y;
1499 shsurf->saved_position_valid = true;
1500 break;
1501
1502 case SHELL_SURFACE_FULLSCREEN:
1503 shsurf->saved_x = surface->geometry.x;
1504 shsurf->saved_y = surface->geometry.y;
1505 shsurf->saved_position_valid = true;
1506
1507 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1508 wl_list_remove(&shsurf->rotation.transform.link);
1509 wl_list_init(&shsurf->rotation.transform.link);
1510 shsurf->surface->geometry.dirty = 1;
1511 shsurf->saved_rotation_valid = true;
1512 }
1513 break;
1514
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001515 default:
1516 break;
1517 }
1518}
1519
1520static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001521set_toplevel(struct shell_surface *shsurf)
1522{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001523 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001524}
1525
1526static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001527shell_surface_set_toplevel(struct wl_client *client,
1528 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001529{
Pekka Paalanen98262232011-12-01 10:42:22 +02001530 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001531
Tiago Vignattibc052c92012-04-19 16:18:18 +03001532 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001533}
1534
1535static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001536set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001537 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001538{
1539 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001540 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001541 shsurf->transient.x = x;
1542 shsurf->transient.y = y;
1543 shsurf->transient.flags = flags;
1544 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1545}
1546
1547static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001548shell_surface_set_transient(struct wl_client *client,
1549 struct wl_resource *resource,
1550 struct wl_resource *parent_resource,
1551 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001552{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001553 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001554 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001555
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001556 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001557}
1558
Tiago Vignattibe143262012-04-16 17:31:41 +03001559static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001560shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001561{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001562 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001563}
1564
1565static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001566get_output_panel_height(struct desktop_shell *shell,
1567 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001568{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001569 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001570 int panel_height = 0;
1571
1572 if (!output)
1573 return 0;
1574
Juan Zhao4ab94682012-07-09 22:24:09 -07001575 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001576 if (surface->output == output) {
1577 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001578 break;
1579 }
1580 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001581
Juan Zhao96879df2012-02-07 08:45:41 +08001582 return panel_height;
1583}
1584
1585static void
1586shell_surface_set_maximized(struct wl_client *client,
1587 struct wl_resource *resource,
1588 struct wl_resource *output_resource )
1589{
1590 struct shell_surface *shsurf = resource->data;
1591 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001592 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001593 uint32_t edges = 0, panel_height = 0;
1594
1595 /* get the default output, if the client set it as NULL
1596 check whether the ouput is available */
1597 if (output_resource)
1598 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001599 else if (es->output)
1600 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001601 else
1602 shsurf->output = get_default_output(es->compositor);
1603
Tiago Vignattibe143262012-04-16 17:31:41 +03001604 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001605 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001606 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001607
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001608 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001609 shsurf->output->width,
1610 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001611
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001612 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001613}
1614
Alex Wu21858432012-04-01 20:13:08 +08001615static void
1616black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1617
Alex Wu4539b082012-03-01 12:57:46 +08001618static struct weston_surface *
1619create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001620 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001621 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001622{
1623 struct weston_surface *surface = NULL;
1624
1625 surface = weston_surface_create(ec);
1626 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001627 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001628 return NULL;
1629 }
1630
Alex Wu21858432012-04-01 20:13:08 +08001631 surface->configure = black_surface_configure;
1632 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001633 weston_surface_configure(surface, x, y, w, h);
1634 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001635 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001636 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1637
Alex Wu4539b082012-03-01 12:57:46 +08001638 return surface;
1639}
1640
1641/* Create black surface and append it to the associated fullscreen surface.
1642 * Handle size dismatch and positioning according to the method. */
1643static void
1644shell_configure_fullscreen(struct shell_surface *shsurf)
1645{
1646 struct weston_output *output = shsurf->fullscreen_output;
1647 struct weston_surface *surface = shsurf->surface;
1648 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001649 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001650
Alex Wu4539b082012-03-01 12:57:46 +08001651 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001652 shsurf->fullscreen.black_surface =
1653 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001654 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001655 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001656 output->width,
1657 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001658
1659 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1660 wl_list_insert(&surface->layer_link,
1661 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001662 shsurf->fullscreen.black_surface->output = output;
1663
1664 switch (shsurf->fullscreen.type) {
1665 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001666 if (surface->buffer)
1667 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001668 break;
1669 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1670 matrix = &shsurf->fullscreen.transform.matrix;
1671 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001672
Scott Moreau1bad5db2012-08-18 01:04:05 -06001673 output_aspect = (float) output->width /
1674 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001675 surface_aspect = (float) surface->geometry.width /
1676 (float) surface->geometry.height;
1677 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001678 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001679 (float) surface->geometry.width;
1680 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001681 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001682 (float) surface->geometry.height;
1683
Alex Wu4539b082012-03-01 12:57:46 +08001684 weston_matrix_scale(matrix, scale, scale, 1);
1685 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001686 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001687 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001688 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1689 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001690 weston_surface_set_position(surface, x, y);
1691
Alex Wu4539b082012-03-01 12:57:46 +08001692 break;
1693 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001694 if (shell_surface_is_top_fullscreen(shsurf)) {
1695 struct weston_mode mode = {0,
1696 surface->geometry.width,
1697 surface->geometry.height,
1698 shsurf->fullscreen.framerate};
1699
1700 if (weston_output_switch_mode(output, &mode) == 0) {
1701 weston_surface_configure(shsurf->fullscreen.black_surface,
1702 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001703 output->width,
1704 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001705 weston_surface_set_position(surface, output->x, output->y);
1706 break;
1707 }
1708 }
Alex Wu4539b082012-03-01 12:57:46 +08001709 break;
1710 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1711 break;
1712 default:
1713 break;
1714 }
1715}
1716
1717/* make the fullscreen and black surface at the top */
1718static void
1719shell_stack_fullscreen(struct shell_surface *shsurf)
1720{
Alex Wubd3354b2012-04-17 17:20:49 +08001721 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001722 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001723 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001724
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001725 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001726 wl_list_insert(&shell->fullscreen_layer.surface_list,
1727 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001728 weston_surface_damage(surface);
1729
1730 if (!shsurf->fullscreen.black_surface)
1731 shsurf->fullscreen.black_surface =
1732 create_black_surface(surface->compositor,
1733 surface,
1734 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001735 output->width,
1736 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001737
1738 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001739 wl_list_insert(&surface->layer_link,
1740 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001741 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001742}
1743
1744static void
1745shell_map_fullscreen(struct shell_surface *shsurf)
1746{
Alex Wu4539b082012-03-01 12:57:46 +08001747 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001748 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001749}
1750
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001751static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001752shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001753 struct wl_resource *resource,
1754 uint32_t method,
1755 uint32_t framerate,
1756 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001757{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001758 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001759 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001760
1761 if (output_resource)
1762 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001763 else if (es->output)
1764 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001765 else
1766 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001767
Alex Wu4539b082012-03-01 12:57:46 +08001768 shsurf->fullscreen_output = shsurf->output;
1769 shsurf->fullscreen.type = method;
1770 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001771 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001772
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001773 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001774 shsurf->output->width,
1775 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001776}
1777
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001778static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001779popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001780 struct wl_surface *surface,
1781 wl_fixed_t x,
1782 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001783{
Daniel Stone37816df2012-05-16 18:45:18 +01001784 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001785 struct shell_surface *priv =
1786 container_of(grab, struct shell_surface, popup.grab);
1787 struct wl_client *client = priv->surface->surface.resource.client;
1788
Pekka Paalanencb108432012-01-19 16:25:40 +02001789 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001790 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001791 grab->focus = surface;
1792 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001793 wl_pointer_set_focus(pointer, NULL,
1794 wl_fixed_from_int(0),
1795 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001796 grab->focus = NULL;
1797 }
1798}
1799
1800static void
Scott Moreau447013d2012-02-18 05:05:29 -07001801popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001802 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001803{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001804 struct wl_resource *resource;
1805
Daniel Stone37816df2012-05-16 18:45:18 +01001806 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001807 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001808 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001809}
1810
1811static void
Scott Moreau447013d2012-02-18 05:05:29 -07001812popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001813 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001814{
1815 struct wl_resource *resource;
1816 struct shell_surface *shsurf =
1817 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001818 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001819 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001820 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001821
Daniel Stone37816df2012-05-16 18:45:18 +01001822 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001823 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001824 display = wl_client_get_display(resource->client);
1825 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001826 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001827 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001828 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001829 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001830 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001831 wl_pointer_end_grab(grab->pointer);
1832 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001833 }
1834
Daniel Stone4dbadb12012-05-30 16:31:51 +01001835 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001836 shsurf->popup.initial_up = 1;
1837}
1838
Scott Moreau447013d2012-02-18 05:05:29 -07001839static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001840 popup_grab_focus,
1841 popup_grab_motion,
1842 popup_grab_button,
1843};
1844
1845static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001846shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001847{
Daniel Stone37816df2012-05-16 18:45:18 +01001848 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001849 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001850 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001851
1852 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001853 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001854
Pekka Paalanen938269a2012-02-07 14:19:01 +02001855 weston_surface_update_transform(parent);
1856 if (parent->transform.enabled) {
1857 shsurf->popup.parent_transform.matrix =
1858 parent->transform.matrix;
1859 } else {
1860 /* construct x, y translation matrix */
1861 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1862 shsurf->popup.parent_transform.matrix.d[12] =
1863 parent->geometry.x;
1864 shsurf->popup.parent_transform.matrix.d[13] =
1865 parent->geometry.y;
1866 }
1867 wl_list_insert(es->geometry.transformation_list.prev,
1868 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001869
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001870 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001871 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1872 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001873
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001874 /* We don't require the grab to still be active, but if another
1875 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001876 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1877 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001878 } else {
1879 wl_shell_surface_send_popup_done(&shsurf->resource);
1880 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001881}
1882
1883static void
1884shell_surface_set_popup(struct wl_client *client,
1885 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001886 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001887 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001888 struct wl_resource *parent_resource,
1889 int32_t x, int32_t y, uint32_t flags)
1890{
1891 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001892
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001893 shsurf->type = SHELL_SURFACE_POPUP;
1894 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001895 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001896 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001897 shsurf->popup.x = x;
1898 shsurf->popup.y = y;
1899}
1900
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001901static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001902 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001903 shell_surface_move,
1904 shell_surface_resize,
1905 shell_surface_set_toplevel,
1906 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001907 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001908 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001909 shell_surface_set_maximized,
1910 shell_surface_set_title,
1911 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001912};
1913
1914static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001915destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001916{
Daniel Stone37816df2012-05-16 18:45:18 +01001917 if (shsurf->popup.grab.pointer)
1918 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001919
Alex Wubd3354b2012-04-17 17:20:49 +08001920 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1921 shell_surface_is_top_fullscreen(shsurf)) {
1922 weston_output_switch_mode(shsurf->fullscreen_output,
1923 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001924 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001925
Alex Wuaa08e2d2012-03-05 11:01:40 +08001926 if (shsurf->fullscreen.black_surface)
1927 weston_surface_destroy(shsurf->fullscreen.black_surface);
1928
Alex Wubd3354b2012-04-17 17:20:49 +08001929 /* As destroy_resource() use wl_list_for_each_safe(),
1930 * we can always remove the listener.
1931 */
1932 wl_list_remove(&shsurf->surface_destroy_listener.link);
1933 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001934 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001935
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001936 wl_list_remove(&shsurf->link);
1937 free(shsurf);
1938}
1939
1940static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001941shell_destroy_shell_surface(struct wl_resource *resource)
1942{
1943 struct shell_surface *shsurf = resource->data;
1944
1945 destroy_shell_surface(shsurf);
1946}
1947
1948static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001949shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001950{
1951 struct shell_surface *shsurf = container_of(listener,
1952 struct shell_surface,
1953 surface_destroy_listener);
1954
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001955 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001956 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001957 } else {
1958 wl_signal_emit(&shsurf->resource.destroy_signal,
1959 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001960 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001961 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001962}
1963
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001964static void
1965shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1966
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001967static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001968get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001969{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001970 if (surface->configure == shell_surface_configure)
1971 return surface->private;
1972 else
1973 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001974}
1975
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001976static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001977create_shell_surface(void *shell, struct weston_surface *surface,
1978 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001979{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001980 struct shell_surface *shsurf;
1981
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001982 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001983 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001984 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001985 }
1986
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001987 shsurf = calloc(1, sizeof *shsurf);
1988 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001989 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001990 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001991 }
1992
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001993 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001994 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001995
Tiago Vignattibc052c92012-04-19 16:18:18 +03001996 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001997 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001998 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001999 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002000 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002001 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2002 shsurf->fullscreen.framerate = 0;
2003 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002004 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002005 wl_list_init(&shsurf->fullscreen.transform.link);
2006
Tiago Vignattibc052c92012-04-19 16:18:18 +03002007 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002008 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2009 wl_signal_add(&surface->surface.resource.destroy_signal,
2010 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002011
2012 /* init link so its safe to always remove it in destroy_shell_surface */
2013 wl_list_init(&shsurf->link);
2014
Pekka Paalanen460099f2012-01-20 16:48:25 +02002015 /* empty when not in use */
2016 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002017 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002018
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002019 wl_list_init(&shsurf->workspace_transform.link);
2020
Pekka Paalanen98262232011-12-01 10:42:22 +02002021 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002022 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002023
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002024 shsurf->client = client;
2025
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002026 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002027}
2028
2029static void
2030shell_get_shell_surface(struct wl_client *client,
2031 struct wl_resource *resource,
2032 uint32_t id,
2033 struct wl_resource *surface_resource)
2034{
2035 struct weston_surface *surface = surface_resource->data;
2036 struct desktop_shell *shell = resource->data;
2037 struct shell_surface *shsurf;
2038
2039 if (get_shell_surface(surface)) {
2040 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002041 WL_DISPLAY_ERROR_INVALID_OBJECT,
2042 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002043 return;
2044 }
2045
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002046 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002047 if (!shsurf) {
2048 wl_resource_post_error(surface_resource,
2049 WL_DISPLAY_ERROR_INVALID_OBJECT,
2050 "surface->configure already set");
2051 return;
2052 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002053
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002054 shsurf->resource.destroy = shell_destroy_shell_surface;
2055 shsurf->resource.object.id = id;
2056 shsurf->resource.object.interface = &wl_shell_surface_interface;
2057 shsurf->resource.object.implementation =
2058 (void (**)(void)) &shell_surface_implementation;
2059 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002060
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002061 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002062}
2063
2064static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002065 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002066};
2067
Kristian Høgsberg07937562011-04-12 17:25:42 -04002068static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002069handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002070{
2071 proc->pid = 0;
2072}
2073
2074static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002075launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002076{
2077 if (shell->screensaver.binding)
2078 return;
2079
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002080 if (!shell->screensaver.path)
2081 return;
2082
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002083 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002084 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002085 return;
2086 }
2087
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002088 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002089 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002090 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002091 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002092}
2093
2094static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002095terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002096{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002097 if (shell->screensaver.process.pid == 0)
2098 return;
2099
2100 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002101}
2102
2103static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002104configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002105{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002106 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002107
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002108 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2109 if (s->output == es->output && s != es) {
2110 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002111 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002112 }
2113 }
2114
2115 weston_surface_configure(es, es->output->x, es->output->y,
2116 es->buffer->width, es->buffer->height);
2117
2118 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002119 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002120 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002121 }
2122}
2123
2124static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002125background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2126{
2127 struct desktop_shell *shell = es->private;
2128
2129 configure_static_surface(es, &shell->background_layer);
2130}
2131
2132static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002133desktop_shell_set_background(struct wl_client *client,
2134 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002135 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002136 struct wl_resource *surface_resource)
2137{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002138 struct desktop_shell *shell = resource->data;
2139 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002140
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002141 if (surface->configure) {
2142 wl_resource_post_error(surface_resource,
2143 WL_DISPLAY_ERROR_INVALID_OBJECT,
2144 "surface role already assigned");
2145 return;
2146 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002147
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002148 surface->configure = background_configure;
2149 surface->private = shell;
2150 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002151 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002152 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002153 surface->output->width,
2154 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002155}
2156
2157static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002158panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2159{
2160 struct desktop_shell *shell = es->private;
2161
2162 configure_static_surface(es, &shell->panel_layer);
2163}
2164
2165static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002166desktop_shell_set_panel(struct wl_client *client,
2167 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002168 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002169 struct wl_resource *surface_resource)
2170{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002171 struct desktop_shell *shell = resource->data;
2172 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002173
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002174 if (surface->configure) {
2175 wl_resource_post_error(surface_resource,
2176 WL_DISPLAY_ERROR_INVALID_OBJECT,
2177 "surface role already assigned");
2178 return;
2179 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002180
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002181 surface->configure = panel_configure;
2182 surface->private = shell;
2183 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002184 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002185 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002186 surface->output->width,
2187 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002188}
2189
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002190static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002191lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2192{
2193 struct desktop_shell *shell = surface->private;
2194
2195 center_on_output(surface, get_default_output(shell->compositor));
2196
2197 if (!weston_surface_is_mapped(surface)) {
2198 wl_list_insert(&shell->lock_layer.surface_list,
2199 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002200 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002201 weston_compositor_wake(shell->compositor);
2202 }
2203}
2204
2205static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002206handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002207{
Tiago Vignattibe143262012-04-16 17:31:41 +03002208 struct desktop_shell *shell =
2209 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002210
Martin Minarik6d118362012-06-07 18:01:59 +02002211 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002212 shell->lock_surface = NULL;
2213}
2214
2215static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002216desktop_shell_set_lock_surface(struct wl_client *client,
2217 struct wl_resource *resource,
2218 struct wl_resource *surface_resource)
2219{
Tiago Vignattibe143262012-04-16 17:31:41 +03002220 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002221 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002222
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002223 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002224
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002225 if (!shell->locked)
2226 return;
2227
Pekka Paalanen98262232011-12-01 10:42:22 +02002228 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002229
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002230 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2231 wl_signal_add(&surface_resource->destroy_signal,
2232 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002233
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002234 surface->configure = lock_surface_configure;
2235 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002236}
2237
2238static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002239resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002240{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002241 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002242 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002243
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002244 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2245 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002246
2247 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002248
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002249 wl_list_remove(&shell->lock_layer.link);
2250 wl_list_insert(&shell->compositor->cursor_layer.link,
2251 &shell->fullscreen_layer.link);
2252 wl_list_insert(&shell->fullscreen_layer.link,
2253 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002254 if (shell->showing_input_panels) {
2255 wl_list_insert(&shell->panel_layer.link,
2256 &shell->input_panel_layer.link);
2257 wl_list_insert(&shell->input_panel_layer.link,
2258 &ws->layer.link);
2259 } else {
2260 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2261 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002262
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002263 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002264
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002265 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002266 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002267 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002268 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002269}
2270
2271static void
2272desktop_shell_unlock(struct wl_client *client,
2273 struct wl_resource *resource)
2274{
Tiago Vignattibe143262012-04-16 17:31:41 +03002275 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002276
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002277 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002278
2279 if (shell->locked)
2280 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002281}
2282
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002283static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002284desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002285 struct wl_resource *resource,
2286 struct wl_resource *surface_resource)
2287{
2288 struct desktop_shell *shell = resource->data;
2289
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002290 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002291}
2292
Kristian Høgsberg75840622011-09-06 13:48:16 -04002293static const struct desktop_shell_interface desktop_shell_implementation = {
2294 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002295 desktop_shell_set_panel,
2296 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002297 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002298 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002299};
2300
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002301static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002302get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002303{
2304 struct shell_surface *shsurf;
2305
2306 shsurf = get_shell_surface(surface);
2307 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002308 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002309 return shsurf->type;
2310}
2311
Kristian Høgsberg75840622011-09-06 13:48:16 -04002312static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002313move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002314{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002315 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002316 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002317 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002318
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002319 if (surface == NULL)
2320 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002321
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002322 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002323 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002324 return;
2325
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002326 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002327}
2328
2329static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002330resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002331{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002332 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002333 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002334 uint32_t edges = 0;
2335 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002336 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002337
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002338 if (surface == NULL)
2339 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002340
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002341 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002342 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002343 return;
2344
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002345 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002346 wl_fixed_to_int(seat->pointer->grab_x),
2347 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002348 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002349
Pekka Paalanen60921e52012-01-25 15:55:43 +02002350 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002351 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002352 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002353 edges |= 0;
2354 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002355 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002356
Pekka Paalanen60921e52012-01-25 15:55:43 +02002357 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002358 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002359 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002360 edges |= 0;
2361 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002362 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002363
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002364 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002365}
2366
2367static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002368surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002369 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002370{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002371 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002372 struct shell_surface *shsurf;
2373 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002374 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002375
2376 if (surface == NULL)
2377 return;
2378
2379 shsurf = get_shell_surface(surface);
2380 if (!shsurf)
2381 return;
2382
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002383 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002384
Scott Moreau02709af2012-05-22 01:54:10 -06002385 if (surface->alpha > 1.0)
2386 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002387 if (surface->alpha < step)
2388 surface->alpha = step;
2389
2390 surface->geometry.dirty = 1;
2391 weston_surface_damage(surface);
2392}
2393
2394static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002395do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002396 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002397{
Daniel Stone37816df2012-05-16 18:45:18 +01002398 struct weston_seat *ws = (struct weston_seat *) seat;
2399 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002400 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002401 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002402
2403 wl_list_for_each(output, &compositor->output_list, link) {
2404 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002405 wl_fixed_to_double(seat->pointer->x),
2406 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002407 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002408 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002409 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002410 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002411 increment = -output->zoom.increment;
2412 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002413 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002414 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002415 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002416 else
2417 increment = 0;
2418
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002419 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002420
Scott Moreaue6603982012-06-11 13:07:51 -06002421 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002422 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002423 else if (output->zoom.level > output->zoom.max_level)
2424 output->zoom.level = output->zoom.max_level;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002425 else {
Scott Moreaue6603982012-06-11 13:07:51 -06002426 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002427 output->disable_planes++;
2428 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002429
Scott Moreaue6603982012-06-11 13:07:51 -06002430 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002431
Scott Moreau8dacaab2012-06-17 18:10:58 -06002432 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002433 }
2434 }
2435}
2436
Scott Moreauccbf29d2012-02-22 14:21:41 -07002437static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002438zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002439 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002440{
2441 do_zoom(seat, time, 0, axis, value);
2442}
2443
2444static void
2445zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2446 void *data)
2447{
2448 do_zoom(seat, time, key, 0, 0);
2449}
2450
2451static void
2452terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2453 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002454{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002455 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002456
Daniel Stone325fc2d2012-05-30 16:31:58 +01002457 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002458}
2459
2460static void
Scott Moreau447013d2012-02-18 05:05:29 -07002461rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002462 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002463{
2464 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002465 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002466 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002467 struct shell_surface *shsurf = rotate->base.shsurf;
2468 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002469 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002470
2471 if (!shsurf)
2472 return;
2473
2474 surface = shsurf->surface;
2475
2476 cx = 0.5f * surface->geometry.width;
2477 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002478
Daniel Stone37816df2012-05-16 18:45:18 +01002479 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2480 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002481 r = sqrtf(dx * dx + dy * dy);
2482
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002483 wl_list_remove(&shsurf->rotation.transform.link);
2484 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002485
2486 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002487 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002488 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002489
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002490 weston_matrix_init(&rotate->rotation);
2491 rotate->rotation.d[0] = dx / r;
2492 rotate->rotation.d[4] = -dy / r;
2493 rotate->rotation.d[1] = -rotate->rotation.d[4];
2494 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002495
2496 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002497 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002498 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002499 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002500 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002501
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002502 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002503 &shsurf->surface->geometry.transformation_list,
2504 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002505 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002506 wl_list_init(&shsurf->rotation.transform.link);
2507 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002508 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002509 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002510
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002511 /* We need to adjust the position of the surface
2512 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002513 cposx = surface->geometry.x + cx;
2514 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002515 dposx = rotate->center.x - cposx;
2516 dposy = rotate->center.y - cposy;
2517 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002518 weston_surface_set_position(surface,
2519 surface->geometry.x + dposx,
2520 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002521 }
2522
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002523 /* Repaint implies weston_surface_update_transform(), which
2524 * lazily applies the damage due to rotation update.
2525 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002526 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002527}
2528
2529static void
Scott Moreau447013d2012-02-18 05:05:29 -07002530rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002531 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002532{
2533 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002534 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002535 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002536 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002537 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002538
Daniel Stone4dbadb12012-05-30 16:31:51 +01002539 if (pointer->button_count == 0 &&
2540 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002541 if (shsurf)
2542 weston_matrix_multiply(&shsurf->rotation.rotation,
2543 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002544 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002545 free(rotate);
2546 }
2547}
2548
Scott Moreau447013d2012-02-18 05:05:29 -07002549static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002550 noop_grab_focus,
2551 rotate_grab_motion,
2552 rotate_grab_button,
2553};
2554
2555static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002556rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002557 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002558{
2559 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002560 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002561 struct shell_surface *surface;
2562 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002563 float dx, dy;
2564 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002565
2566 if (base_surface == NULL)
2567 return;
2568
2569 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002570 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002571 return;
2572
Pekka Paalanen460099f2012-01-20 16:48:25 +02002573 rotate = malloc(sizeof *rotate);
2574 if (!rotate)
2575 return;
2576
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002577 weston_surface_to_global_float(surface->surface,
2578 surface->surface->geometry.width / 2,
2579 surface->surface->geometry.height / 2,
2580 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002581
Daniel Stone37816df2012-05-16 18:45:18 +01002582 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2583 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002584 r = sqrtf(dx * dx + dy * dy);
2585 if (r > 20.0f) {
2586 struct weston_matrix inverse;
2587
2588 weston_matrix_init(&inverse);
2589 inverse.d[0] = dx / r;
2590 inverse.d[4] = dy / r;
2591 inverse.d[1] = -inverse.d[4];
2592 inverse.d[5] = inverse.d[0];
2593 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002594
2595 weston_matrix_init(&rotate->rotation);
2596 rotate->rotation.d[0] = dx / r;
2597 rotate->rotation.d[4] = -dy / r;
2598 rotate->rotation.d[1] = -rotate->rotation.d[4];
2599 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002600 } else {
2601 weston_matrix_init(&surface->rotation.rotation);
2602 weston_matrix_init(&rotate->rotation);
2603 }
2604
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002605 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2606 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002607}
2608
2609static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002610lower_fullscreen_layer(struct desktop_shell *shell)
2611{
2612 struct workspace *ws;
2613 struct weston_surface *surface, *prev;
2614
2615 ws = get_current_workspace(shell);
2616 wl_list_for_each_reverse_safe(surface, prev,
2617 &shell->fullscreen_layer.surface_list,
2618 layer_link)
2619 weston_surface_restack(surface, &ws->layer.surface_list);
2620}
2621
2622static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002623activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002624 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002625{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002626 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002627 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002628
Daniel Stone37816df2012-05-16 18:45:18 +01002629 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002630
Jonas Ådahl8538b222012-08-29 22:13:03 +02002631 state = ensure_focus_state(shell, seat);
2632 if (state == NULL)
2633 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002634
2635 state->keyboard_focus = es;
2636 wl_list_remove(&state->surface_destroy_listener.link);
2637 wl_signal_add(&es->surface.resource.destroy_signal,
2638 &state->surface_destroy_listener);
2639
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002640 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002641 case SHELL_SURFACE_FULLSCREEN:
2642 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002643 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002644 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002645 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002646 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002647 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002648 lower_fullscreen_layer(shell);
2649 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002650 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002651 }
2652}
2653
Alex Wu21858432012-04-01 20:13:08 +08002654/* no-op func for checking black surface */
2655static void
2656black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2657{
2658}
2659
2660static bool
2661is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2662{
2663 if (es->configure == black_surface_configure) {
2664 if (fs_surface)
2665 *fs_surface = (struct weston_surface *)es->private;
2666 return true;
2667 }
2668 return false;
2669}
2670
Kristian Høgsberg75840622011-09-06 13:48:16 -04002671static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002672click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002673 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002674{
Daniel Stone37816df2012-05-16 18:45:18 +01002675 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002676 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002677 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002678 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002679
Daniel Stone37816df2012-05-16 18:45:18 +01002680 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002681 if (!focus)
2682 return;
2683
Alex Wu21858432012-04-01 20:13:08 +08002684 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002685 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002686
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002687 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2688 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002689
Daniel Stone325fc2d2012-05-30 16:31:58 +01002690 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002691 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002692}
2693
2694static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002695lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002696{
Tiago Vignattibe143262012-04-16 17:31:41 +03002697 struct desktop_shell *shell =
2698 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002699 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002700 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002701
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002702 if (shell->locked) {
2703 wl_list_for_each(output, &shell->compositor->output_list, link)
2704 /* TODO: find a way to jump to other DPMS levels */
2705 if (output->set_dpms)
2706 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002707 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002708 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002709
2710 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002711
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002712 /* Hide all surfaces by removing the fullscreen, panel and
2713 * toplevel layers. This way nothing else can show or receive
2714 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002715
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002716 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002717 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002718 if (shell->showing_input_panels)
2719 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002720 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002721 wl_list_insert(&shell->compositor->cursor_layer.link,
2722 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002723
Pekka Paalanen77346a62011-11-30 16:26:35 +02002724 launch_screensaver(shell);
2725
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002726 /* TODO: disable bindings that should not work while locked. */
2727
2728 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002729}
2730
2731static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002732unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002733{
Tiago Vignattibe143262012-04-16 17:31:41 +03002734 struct desktop_shell *shell =
2735 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002736
Pekka Paalanend81c2162011-11-16 13:47:34 +02002737 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002738 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002739 return;
2740 }
2741
2742 /* If desktop-shell client has gone away, unlock immediately. */
2743 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002744 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002745 return;
2746 }
2747
2748 if (shell->prepare_event_sent)
2749 return;
2750
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002751 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002752 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002753}
2754
2755static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002756show_input_panels(struct wl_listener *listener, void *data)
2757{
2758 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002759 container_of(listener, struct desktop_shell,
2760 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002761 struct input_panel_surface *surface, *next;
2762 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002763
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002764 shell->showing_input_panels = true;
2765
2766 wl_list_insert(&shell->panel_layer.link,
2767 &shell->input_panel_layer.link);
2768
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002769 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002770 &shell->input_panel.surfaces, link) {
2771 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002772 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002773 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002774 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002775 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002776 weston_surface_damage(ws);
2777 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002778 }
2779}
2780
2781static void
2782hide_input_panels(struct wl_listener *listener, void *data)
2783{
2784 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002785 container_of(listener, struct desktop_shell,
2786 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002787 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002788
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002789 shell->showing_input_panels = false;
2790
2791 wl_list_remove(&shell->input_panel_layer.link);
2792
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002793 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002794 &shell->input_panel_layer.surface_list, layer_link)
2795 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002796}
2797
2798static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002799center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002800{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002801 float x = (output->width - surface->buffer->width) / 2;
2802 float y = (output->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002803
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002804 weston_surface_configure(surface, output->x + x, output->y + y,
2805 surface->buffer->width,
2806 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002807}
2808
2809static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002810weston_surface_set_initial_position (struct weston_surface *surface,
2811 struct desktop_shell *shell)
2812{
2813 struct weston_compositor *compositor = shell->compositor;
2814 int ix = 0, iy = 0;
2815 int range_x, range_y;
2816 int dx, dy, x, y, panel_height;
2817 struct weston_output *output, *target_output = NULL;
2818 struct weston_seat *seat;
2819
2820 /* As a heuristic place the new window on the same output as the
2821 * pointer. Falling back to the output containing 0, 0.
2822 *
2823 * TODO: Do something clever for touch too?
2824 */
2825 wl_list_for_each(seat, &compositor->seat_list, link) {
2826 if (seat->has_pointer) {
2827 ix = wl_fixed_to_int(seat->pointer.x);
2828 iy = wl_fixed_to_int(seat->pointer.y);
2829 break;
2830 }
2831 }
2832
2833 wl_list_for_each(output, &compositor->output_list, link) {
2834 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2835 target_output = output;
2836 break;
2837 }
2838 }
2839
2840 if (!target_output) {
2841 weston_surface_set_position(surface, 10 + random() % 400,
2842 10 + random() % 400);
2843 return;
2844 }
2845
2846 /* Valid range within output where the surface will still be onscreen.
2847 * If this is negative it means that the surface is bigger than
2848 * output.
2849 */
2850 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002851 range_x = target_output->width - surface->geometry.width;
2852 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002853 surface->geometry.height;
2854
Rob Bradford4cb88c72012-08-13 15:18:44 +01002855 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002856 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002857 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002858 dx = 0;
2859
2860 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002861 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002862 else
2863 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002864
2865 x = target_output->x + dx;
2866 y = target_output->y + dy;
2867
2868 weston_surface_set_position (surface, x, y);
2869}
2870
2871static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002872map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002873 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002874{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002875 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002876 struct shell_surface *shsurf = get_shell_surface(surface);
2877 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002878 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002879 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002880 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002881 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002882
Pekka Paalanen60921e52012-01-25 15:55:43 +02002883 surface->geometry.width = width;
2884 surface->geometry.height = height;
2885 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002886
2887 /* initial positioning, see also configure() */
2888 switch (surface_type) {
2889 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002890 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002891 break;
Alex Wu4539b082012-03-01 12:57:46 +08002892 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002893 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002894 shell_map_fullscreen(shsurf);
2895 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002896 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002897 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002898 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002899 weston_surface_set_position(surface, shsurf->output->x,
2900 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002901 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002902 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002903 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002904 case SHELL_SURFACE_NONE:
2905 weston_surface_set_position(surface,
2906 surface->geometry.x + sx,
2907 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002908 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002909 default:
2910 ;
2911 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002912
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002913 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002914 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002915 case SHELL_SURFACE_POPUP:
2916 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002917 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002918 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2919 break;
Alex Wu4539b082012-03-01 12:57:46 +08002920 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002921 case SHELL_SURFACE_NONE:
2922 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002923 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002924 ws = get_current_workspace(shell);
2925 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002926 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002927 }
2928
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002929 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002930 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002931 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2932 surface->output = shsurf->output;
2933 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002934
Juan Zhao7bb92f02011-12-15 11:31:51 -05002935 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002936 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002937 if (shsurf->transient.flags ==
2938 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2939 break;
2940 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002941 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002942 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002943 if (!shell->locked) {
2944 wl_list_for_each(seat, &compositor->seat_list, link)
2945 activate(shell, surface, seat);
2946 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002947 break;
2948 default:
2949 break;
2950 }
2951
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002952 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002953 {
2954 switch (shell->win_animation_type) {
2955 case ANIMATION_FADE:
2956 weston_fade_run(surface, NULL, NULL);
2957 break;
2958 case ANIMATION_ZOOM:
2959 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2960 break;
2961 default:
2962 break;
2963 }
2964 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002965}
2966
2967static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002968configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002969 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002970{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002971 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2972 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002973
Pekka Paalanen77346a62011-11-30 16:26:35 +02002974 shsurf = get_shell_surface(surface);
2975 if (shsurf)
2976 surface_type = shsurf->type;
2977
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002978 surface->geometry.x = x;
2979 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002980 surface->geometry.width = width;
2981 surface->geometry.height = height;
2982 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002983
2984 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002985 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002986 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002987 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002988 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002989 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002990 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002991 surface->geometry.x = surface->output->x;
2992 surface->geometry.y = surface->output->y +
2993 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002994 break;
Alex Wu4539b082012-03-01 12:57:46 +08002995 case SHELL_SURFACE_TOPLEVEL:
2996 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002997 default:
2998 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002999 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003000
Alex Wu4539b082012-03-01 12:57:46 +08003001 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003002 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003003 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003004
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003005 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003006 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003007 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003008}
3009
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003010static void
3011shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3012{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003013 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003014 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003015 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003016
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003017 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003018 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003019 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003020 type_changed = 1;
3021 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003022
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003023 if (!weston_surface_is_mapped(es)) {
3024 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003025 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003026 es->geometry.width != es->buffer->width ||
3027 es->geometry.height != es->buffer->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003028 float from_x, from_y;
3029 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003030
3031 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3032 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3033 configure(shell, es,
3034 es->geometry.x + to_x - from_x,
3035 es->geometry.y + to_y - from_y,
3036 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003037 }
3038}
3039
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003040static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003041
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003042static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003043desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003044{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003045 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003046 struct desktop_shell *shell =
3047 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003048
3049 shell->child.process.pid = 0;
3050 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003051
3052 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3053 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003054 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003055 shell->child.deathstamp = time;
3056 shell->child.deathcount = 0;
3057 }
3058
3059 shell->child.deathcount++;
3060 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003061 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003062 return;
3063 }
3064
Martin Minarik6d118362012-06-07 18:01:59 +02003065 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003066 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003067}
3068
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003069static void
3070launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003071{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003072 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003073 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003074
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003075 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003076 &shell->child.process,
3077 shell_exe,
3078 desktop_shell_sigchld);
3079
3080 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003081 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003082}
3083
3084static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003085bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3086{
Tiago Vignattibe143262012-04-16 17:31:41 +03003087 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003088
3089 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003090 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003091}
3092
Kristian Høgsberg75840622011-09-06 13:48:16 -04003093static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003094unbind_desktop_shell(struct wl_resource *resource)
3095{
Tiago Vignattibe143262012-04-16 17:31:41 +03003096 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003097
3098 if (shell->locked)
3099 resume_desktop(shell);
3100
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003101 shell->child.desktop_shell = NULL;
3102 shell->prepare_event_sent = false;
3103 free(resource);
3104}
3105
3106static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003107bind_desktop_shell(struct wl_client *client,
3108 void *data, uint32_t version, uint32_t id)
3109{
Tiago Vignattibe143262012-04-16 17:31:41 +03003110 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003111 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003112
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003113 resource = wl_client_add_object(client, &desktop_shell_interface,
3114 &desktop_shell_implementation,
3115 id, shell);
3116
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003117 if (client == shell->child.client) {
3118 resource->destroy = unbind_desktop_shell;
3119 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003120 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003121 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003122
3123 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3124 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003125 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003126}
3127
Pekka Paalanen6e168112011-11-24 11:34:05 +02003128static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003129screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3130{
3131 struct desktop_shell *shell = surface->private;
3132
3133 if (!shell->locked)
3134 return;
3135
3136 center_on_output(surface, surface->output);
3137
3138 if (wl_list_empty(&surface->layer_link)) {
3139 wl_list_insert(shell->lock_layer.surface_list.prev,
3140 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003141 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003142 shell->compositor->idle_time = shell->screensaver.duration;
3143 weston_compositor_wake(shell->compositor);
3144 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3145 }
3146}
3147
3148static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003149screensaver_set_surface(struct wl_client *client,
3150 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003151 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003152 struct wl_resource *output_resource)
3153{
Tiago Vignattibe143262012-04-16 17:31:41 +03003154 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003155 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003156 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003157
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003158 surface->configure = screensaver_configure;
3159 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003160 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003161}
3162
3163static const struct screensaver_interface screensaver_implementation = {
3164 screensaver_set_surface
3165};
3166
3167static void
3168unbind_screensaver(struct wl_resource *resource)
3169{
Tiago Vignattibe143262012-04-16 17:31:41 +03003170 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003171
Pekka Paalanen77346a62011-11-30 16:26:35 +02003172 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003173 free(resource);
3174}
3175
3176static void
3177bind_screensaver(struct wl_client *client,
3178 void *data, uint32_t version, uint32_t id)
3179{
Tiago Vignattibe143262012-04-16 17:31:41 +03003180 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003181 struct wl_resource *resource;
3182
3183 resource = wl_client_add_object(client, &screensaver_interface,
3184 &screensaver_implementation,
3185 id, shell);
3186
Pekka Paalanen77346a62011-11-30 16:26:35 +02003187 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003188 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003189 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003190 return;
3191 }
3192
3193 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3194 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003195 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003196}
3197
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003198static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003199input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3200{
3201 struct weston_mode *mode = surface->output->current;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003202 float x = (mode->width - surface->buffer->width) / 2;
3203 float y = mode->height - surface->buffer->height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003204
3205 /* Don't map the input panel here, wait for
3206 * show_input_panels signal. */
3207
3208 weston_surface_configure(surface,
3209 surface->output->x + x,
3210 surface->output->y + y,
3211 surface->buffer->width,
3212 surface->buffer->height);
3213}
3214
3215static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003216destroy_input_panel_surface(struct wl_listener *listener,
3217 void *data)
3218{
3219 struct input_panel_surface *input_panel_surface =
3220 container_of(listener, struct input_panel_surface, listener);
3221
3222 wl_list_remove(&listener->link);
3223 wl_list_remove(&input_panel_surface->link);
3224
3225 free(input_panel_surface);
3226}
3227
3228static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003229input_panel_set_surface(struct wl_client *client,
3230 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003231 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003232 struct wl_resource *output_resource)
3233{
3234 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003235 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003236 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003237 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003238
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003239 surface->configure = input_panel_configure;
3240 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003241 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003242
3243 input_panel_surface = malloc(sizeof *input_panel_surface);
3244 if (!input_panel_surface) {
3245 wl_resource_post_no_memory(resource);
3246 return;
3247 }
3248
3249 input_panel_surface->surface = surface;
3250 input_panel_surface->listener.notify = destroy_input_panel_surface;
3251
3252 wl_signal_add(&surface_resource->destroy_signal,
3253 &input_panel_surface->listener);
3254
3255 wl_list_insert(&shell->input_panel.surfaces,
3256 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003257}
3258
3259static const struct input_panel_interface input_panel_implementation = {
3260 input_panel_set_surface
3261};
3262
3263static void
3264unbind_input_panel(struct wl_resource *resource)
3265{
3266 struct desktop_shell *shell = resource->data;
3267
3268 shell->input_panel.binding = NULL;
3269 free(resource);
3270}
3271
3272static void
3273bind_input_panel(struct wl_client *client,
3274 void *data, uint32_t version, uint32_t id)
3275{
3276 struct desktop_shell *shell = data;
3277 struct wl_resource *resource;
3278
3279 resource = wl_client_add_object(client, &input_panel_interface,
3280 &input_panel_implementation,
3281 id, shell);
3282
3283 if (shell->input_panel.binding == NULL) {
3284 resource->destroy = unbind_input_panel;
3285 shell->input_panel.binding = resource;
3286 return;
3287 }
3288
3289 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3290 "interface object already bound");
3291 wl_resource_destroy(resource);
3292}
3293
Kristian Høgsberg07045392012-02-19 18:52:44 -05003294struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003295 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003296 struct weston_surface *current;
3297 struct wl_listener listener;
3298 struct wl_keyboard_grab grab;
3299};
3300
3301static void
3302switcher_next(struct switcher *switcher)
3303{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003304 struct weston_surface *surface;
3305 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003306 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003307 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003308
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003309 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003310 switch (get_shell_surface_type(surface)) {
3311 case SHELL_SURFACE_TOPLEVEL:
3312 case SHELL_SURFACE_FULLSCREEN:
3313 case SHELL_SURFACE_MAXIMIZED:
3314 if (first == NULL)
3315 first = surface;
3316 if (prev == switcher->current)
3317 next = surface;
3318 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003319 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003320 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003321 weston_surface_damage(surface);
3322 break;
3323 default:
3324 break;
3325 }
Alex Wu1659daa2012-04-01 20:13:09 +08003326
3327 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003328 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003329 surface->geometry.dirty = 1;
3330 weston_surface_damage(surface);
3331 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003332 }
3333
3334 if (next == NULL)
3335 next = first;
3336
Alex Wu07b26062012-03-12 16:06:01 +08003337 if (next == NULL)
3338 return;
3339
Kristian Høgsberg07045392012-02-19 18:52:44 -05003340 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003341 wl_signal_add(&next->surface.resource.destroy_signal,
3342 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003343
3344 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003345 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003346
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003347 shsurf = get_shell_surface(switcher->current);
3348 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003349 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003350}
3351
3352static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003353switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003354{
3355 struct switcher *switcher =
3356 container_of(listener, struct switcher, listener);
3357
3358 switcher_next(switcher);
3359}
3360
3361static void
Daniel Stone351eb612012-05-31 15:27:47 -04003362switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003363{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003364 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003365 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003366 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003367
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003368 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003369 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003370 weston_surface_damage(surface);
3371 }
3372
Alex Wu07b26062012-03-12 16:06:01 +08003373 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003374 activate(switcher->shell, switcher->current,
3375 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003376 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003377 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003378 free(switcher);
3379}
3380
3381static void
3382switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003383 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003384{
3385 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003386 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003387
Daniel Stonec9785ea2012-05-30 16:31:52 +01003388 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003389 switcher_next(switcher);
3390}
3391
3392static void
3393switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3394 uint32_t mods_depressed, uint32_t mods_latched,
3395 uint32_t mods_locked, uint32_t group)
3396{
3397 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003398 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003399
Daniel Stone351eb612012-05-31 15:27:47 -04003400 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3401 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003402}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003403
3404static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003405 switcher_key,
3406 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003407};
3408
3409static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003410switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3411 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003412{
Tiago Vignattibe143262012-04-16 17:31:41 +03003413 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003414 struct switcher *switcher;
3415
3416 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003417 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003418 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003419 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003420 wl_list_init(&switcher->listener.link);
3421
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003422 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003423 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003424 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3425 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003426 switcher_next(switcher);
3427}
3428
Pekka Paalanen3c647232011-12-22 13:43:43 +02003429static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003430backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3431 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003432{
3433 struct weston_compositor *compositor = data;
3434 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003435 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003436
3437 /* TODO: we're limiting to simple use cases, where we assume just
3438 * control on the primary display. We'd have to extend later if we
3439 * ever get support for setting backlights on random desktop LCD
3440 * panels though */
3441 output = get_default_output(compositor);
3442 if (!output)
3443 return;
3444
3445 if (!output->set_backlight)
3446 return;
3447
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003448 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3449 backlight_new = output->backlight_current - 25;
3450 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3451 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003452
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003453 if (backlight_new < 5)
3454 backlight_new = 5;
3455 if (backlight_new > 255)
3456 backlight_new = 255;
3457
3458 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003459 output->set_backlight(output, output->backlight_current);
3460}
3461
3462static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003463debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3464 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003465{
Tiago Vignattibe143262012-04-16 17:31:41 +03003466 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003467 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003468 struct weston_surface *surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003469 struct weston_plane plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003470
3471 if (shell->debug_repaint_surface) {
3472 weston_surface_destroy(shell->debug_repaint_surface);
3473 shell->debug_repaint_surface = NULL;
3474 } else {
3475 surface = weston_surface_create(compositor);
3476 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3477 weston_surface_configure(surface, 0, 0, 8192, 8192);
3478 wl_list_insert(&compositor->fade_layer.surface_list,
3479 &surface->layer_link);
Pekka Paalanenb89b0042012-10-10 12:49:29 +03003480 pixman_region32_fini(&surface->input);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003481 pixman_region32_init(&surface->input);
3482
3483 /* Here's the dirty little trick that makes the
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003484 * repaint debugging work: we move the surface to a
3485 * different plane and force an update_transform to
3486 * update dependent state and clear the
3487 * geometry.dirty bit. This way the call to
3488 * damage_below() in update_transform() does not
3489 * add damage to the primary plane. */
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003490
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003491 weston_plane_init(&plane, 0, 0);
3492 surface->plane = &plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003493 weston_surface_update_transform(surface);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003494 shell->debug_repaint_surface = surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003495 surface->plane = &compositor->primary_plane;
3496 weston_plane_release(&plane);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003497 }
3498}
3499
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003500
3501static void
3502fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3503 void *data)
3504{
3505 struct desktop_shell *shell = data;
3506 struct weston_compositor *compositor = shell->compositor;
3507 compositor->fan_debug = !compositor->fan_debug;
3508 weston_compositor_damage_all(compositor);
3509}
3510
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003511static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003512force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3513 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003514{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003515 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003516 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003517 struct desktop_shell *shell = data;
3518 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003519 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003520
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003521 focus_surface = seat->keyboard->focus;
3522 if (!focus_surface)
3523 return;
3524
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003525 wl_signal_emit(&compositor->kill_signal, focus_surface);
3526
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003527 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003528 wl_client_get_credentials(client, &pid, NULL, NULL);
3529
3530 /* Skip clients that we launched ourselves (the credentials of
3531 * the socketpair is ours) */
3532 if (pid == getpid())
3533 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003534
Daniel Stone325fc2d2012-05-30 16:31:58 +01003535 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003536}
3537
3538static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003539workspace_up_binding(struct wl_seat *seat, uint32_t time,
3540 uint32_t key, void *data)
3541{
3542 struct desktop_shell *shell = data;
3543 unsigned int new_index = shell->workspaces.current;
3544
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003545 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003546 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003547 if (new_index != 0)
3548 new_index--;
3549
3550 change_workspace(shell, new_index);
3551}
3552
3553static void
3554workspace_down_binding(struct wl_seat *seat, uint32_t time,
3555 uint32_t key, void *data)
3556{
3557 struct desktop_shell *shell = data;
3558 unsigned int new_index = shell->workspaces.current;
3559
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003560 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003561 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003562 if (new_index < shell->workspaces.num - 1)
3563 new_index++;
3564
3565 change_workspace(shell, new_index);
3566}
3567
3568static void
3569workspace_f_binding(struct wl_seat *seat, uint32_t time,
3570 uint32_t key, void *data)
3571{
3572 struct desktop_shell *shell = data;
3573 unsigned int new_index;
3574
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003575 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003576 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003577 new_index = key - KEY_F1;
3578 if (new_index >= shell->workspaces.num)
3579 new_index = shell->workspaces.num - 1;
3580
3581 change_workspace(shell, new_index);
3582}
3583
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003584static void
3585workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3586 uint32_t key, void *data)
3587{
3588 struct desktop_shell *shell = data;
3589 unsigned int new_index = shell->workspaces.current;
3590
3591 if (shell->locked)
3592 return;
3593
3594 if (new_index != 0)
3595 new_index--;
3596
3597 take_surface_to_workspace_by_seat(shell, seat, new_index);
3598}
3599
3600static void
3601workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3602 uint32_t key, void *data)
3603{
3604 struct desktop_shell *shell = data;
3605 unsigned int new_index = shell->workspaces.current;
3606
3607 if (shell->locked)
3608 return;
3609
3610 if (new_index < shell->workspaces.num - 1)
3611 new_index++;
3612
3613 take_surface_to_workspace_by_seat(shell, seat, new_index);
3614}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003615
3616static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003617shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003618{
Tiago Vignattibe143262012-04-16 17:31:41 +03003619 struct desktop_shell *shell =
3620 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003621 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003622
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003623 if (shell->child.client)
3624 wl_client_destroy(shell->child.client);
3625
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003626 wl_list_remove(&shell->lock_listener.link);
3627 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003628 wl_list_remove(&shell->show_input_panel_listener.link);
3629 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003630
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003631 wl_array_for_each(ws, &shell->workspaces.array)
3632 workspace_destroy(*ws);
3633 wl_array_release(&shell->workspaces.array);
3634
Pekka Paalanen3c647232011-12-22 13:43:43 +02003635 free(shell->screensaver.path);
3636 free(shell);
3637}
3638
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003639static void
3640shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3641{
3642 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003643 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003644
3645 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003646 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3647 MODIFIER_CTRL | MODIFIER_ALT,
3648 terminate_binding, ec);
3649 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3650 click_to_activate_binding,
3651 shell);
3652 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3653 MODIFIER_SUPER | MODIFIER_ALT,
3654 surface_opacity_binding, NULL);
3655 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3656 MODIFIER_SUPER, zoom_axis_binding,
3657 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003658
3659 /* configurable bindings */
3660 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003661 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3662 zoom_key_binding, NULL);
3663 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3664 zoom_key_binding, NULL);
3665 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3666 shell);
3667 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3668 resize_binding, shell);
3669 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3670 rotate_binding, NULL);
3671 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3672 shell);
3673 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3674 ec);
3675 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3676 backlight_binding, ec);
3677 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3678 ec);
3679 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3680 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003681 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003682 debug_repaint_binding, shell);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003683 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_ALT,
3684 fan_debug_repaint_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003685 weston_compositor_add_key_binding(ec, KEY_K, mod,
3686 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003687 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3688 workspace_up_binding, shell);
3689 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3690 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003691 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3692 workspace_move_surface_up_binding,
3693 shell);
3694 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3695 workspace_move_surface_down_binding,
3696 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003697
3698 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3699 if (shell->workspaces.num > 1) {
3700 num_workspace_bindings = shell->workspaces.num;
3701 if (num_workspace_bindings > 6)
3702 num_workspace_bindings = 6;
3703 for (i = 0; i < num_workspace_bindings; i++)
3704 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3705 workspace_f_binding,
3706 shell);
3707 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003708}
3709
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003710WL_EXPORT int
Kristian Høgsbergb00a9d32012-09-11 14:06:27 -04003711module_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003712{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003713 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003714 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003715 struct workspace **pws;
3716 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003717 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003718
3719 shell = malloc(sizeof *shell);
3720 if (shell == NULL)
3721 return -1;
3722
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003723 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003724 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003725
3726 shell->destroy_listener.notify = shell_destroy;
3727 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3728 shell->lock_listener.notify = lock;
3729 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3730 shell->unlock_listener.notify = unlock;
3731 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003732 shell->show_input_panel_listener.notify = show_input_panels;
3733 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3734 shell->hide_input_panel_listener.notify = hide_input_panels;
3735 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003736 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003737 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003738 ec->shell_interface.create_shell_surface = create_shell_surface;
3739 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003740 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003741 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003742 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003743
Pekka Paalanen77346a62011-11-30 16:26:35 +02003744 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003745 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003746
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003747 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3748 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003749 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3750 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003751 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003752
3753 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02003754 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003755
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003756 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003757
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003758 for (i = 0; i < shell->workspaces.num; i++) {
3759 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3760 if (pws == NULL)
3761 return -1;
3762
3763 *pws = workspace_create();
3764 if (*pws == NULL)
3765 return -1;
3766 }
3767 activate_workspace(shell, 0);
3768
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003769 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003770 wl_list_init(&shell->workspaces.animation.link);
3771 shell->workspaces.animation.frame = animate_workspace_change_frame;
3772
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003773 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3774 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003775 return -1;
3776
Kristian Høgsberg75840622011-09-06 13:48:16 -04003777 if (wl_display_add_global(ec->wl_display,
3778 &desktop_shell_interface,
3779 shell, bind_desktop_shell) == NULL)
3780 return -1;
3781
Pekka Paalanen6e168112011-11-24 11:34:05 +02003782 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3783 shell, bind_screensaver) == NULL)
3784 return -1;
3785
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003786 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3787 shell, bind_input_panel) == NULL)
3788 return -1;
3789
Jonas Ådahle9d22502012-08-29 22:13:01 +02003790 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3791 shell, bind_workspace_manager) == NULL)
3792 return -1;
3793
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003794 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003795
3796 loop = wl_display_get_event_loop(ec->wl_display);
3797 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003798
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003799 wl_list_for_each(seat, &ec->seat_list, link)
3800 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003801
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003802 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003803
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003804 return 0;
3805}