blob: bec539709ee8fbfd2eea4dc95526a1b6e395f466 [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"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020038#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050039
Jonas Ådahle3cddce2012-06-13 00:01:22 +020040#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020041#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020042
Juan Zhaoe10d2792012-04-25 19:09:52 +080043enum animation_type {
44 ANIMATION_NONE,
45
46 ANIMATION_ZOOM,
47 ANIMATION_FADE
48};
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Jonas Ådahle3cddce2012-06-13 00:01:22 +020059struct workspace {
60 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020061
62 struct wl_list focus_list;
63 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020064};
65
Tiago Vignattibe143262012-04-16 17:31:41 +030066struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040068
69 struct wl_listener lock_listener;
70 struct wl_listener unlock_listener;
71 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020072 struct wl_listener show_input_panel_listener;
73 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020074
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050075 struct weston_layer fullscreen_layer;
76 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050077 struct weston_layer background_layer;
78 struct weston_layer lock_layer;
79
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040080 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030081 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040082
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020083 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020085 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020086 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020087
88 unsigned deathcount;
89 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020090 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020091
92 bool locked;
93 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020094
Kristian Høgsberg730c94d2012-06-26 21:44:35 -040095 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050096 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010097
Pekka Paalanen77346a62011-11-30 16:26:35 +020098 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +020099 struct wl_array array;
100 unsigned int current;
101 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200102
103 struct weston_animation animation;
104 int anim_dir;
105 uint32_t anim_timestamp;
106 double anim_current;
107 struct workspace *anim_from;
108 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200109 } workspaces;
110
111 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200112 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200113 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200114 struct wl_resource *binding;
115 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200117 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500118
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200119 struct {
120 struct wl_resource *binding;
121 struct wl_list surfaces;
122 } input_panel;
123
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300124 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800125 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500126 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400127};
128
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500129enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200130 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131 SHELL_SURFACE_TOPLEVEL,
132 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500133 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800134 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200136};
137
Scott Moreauff1db4a2012-04-17 19:06:18 -0600138struct ping_timer {
139 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600140 uint32_t serial;
141};
142
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200143struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200144 struct wl_resource resource;
145
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500146 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200147 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400148 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300149 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200150
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400151 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400152 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500153 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800154 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800155 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600156 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100157
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500158 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200159 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500160 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200161 } rotation;
162
163 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700164 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500165 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200166 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500167 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100168 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400169 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500170 } popup;
171
Alex Wu4539b082012-03-01 12:57:46 +0800172 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300173 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400174 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300175 } transient;
176
177 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800178 enum wl_shell_surface_fullscreen_method type;
179 struct weston_transform transform; /* matrix from x, y */
180 uint32_t framerate;
181 struct weston_surface *black_surface;
182 } fullscreen;
183
Scott Moreauff1db4a2012-04-17 19:06:18 -0600184 struct ping_timer *ping_timer;
185
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200186 struct weston_transform workspace_transform;
187
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500188 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500189 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100190 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400191
192 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200193};
194
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300195struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700196 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300197 struct shell_surface *shsurf;
198 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300199 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300200};
201
202struct weston_move_grab {
203 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100204 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500205};
206
Pekka Paalanen460099f2012-01-20 16:48:25 +0200207struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300208 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500209 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200210 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400211 GLfloat x;
212 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200213 } center;
214};
215
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400216static void
217activate(struct desktop_shell *shell, struct weston_surface *es,
218 struct weston_seat *seat);
219
220static struct workspace *
221get_current_workspace(struct desktop_shell *shell);
222
Alex Wubd3354b2012-04-17 17:20:49 +0800223static struct shell_surface *
224get_shell_surface(struct weston_surface *surface);
225
226static struct desktop_shell *
227shell_surface_get_shell(struct shell_surface *shsurf);
228
229static bool
230shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
231{
232 struct desktop_shell *shell;
233 struct weston_surface *top_fs_es;
234
235 shell = shell_surface_get_shell(shsurf);
236
237 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
238 return false;
239
240 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
241 struct weston_surface,
242 layer_link);
243 return (shsurf == get_shell_surface(top_fs_es));
244}
245
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500246static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400247destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300248{
249 struct shell_grab *grab;
250
251 grab = container_of(listener, struct shell_grab,
252 shsurf_destroy_listener);
253
254 grab->shsurf = NULL;
255}
256
257static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300258shell_grab_start(struct shell_grab *grab,
259 const struct wl_pointer_grab_interface *interface,
260 struct shell_surface *shsurf,
261 struct wl_pointer *pointer,
262 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300263{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300264 struct desktop_shell *shell = shsurf->shell;
265
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300266 grab->grab.interface = interface;
267 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400268 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
269 wl_signal_add(&shsurf->resource.destroy_signal,
270 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300271
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300272 grab->pointer = pointer;
273 grab->grab.focus = &shsurf->surface->surface;
274
275 wl_pointer_start_grab(pointer, &grab->grab);
276 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
277 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
278 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279}
280
281static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300282shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400284 if (grab->shsurf)
285 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300286
287 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300288}
289
290static void
Alex Wu4539b082012-03-01 12:57:46 +0800291center_on_output(struct weston_surface *surface,
292 struct weston_output *output);
293
Daniel Stone496ca172012-05-30 16:31:42 +0100294static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300295get_modifier(char *modifier)
296{
297 if (!modifier)
298 return MODIFIER_SUPER;
299
300 if (!strcmp("ctrl", modifier))
301 return MODIFIER_CTRL;
302 else if (!strcmp("alt", modifier))
303 return MODIFIER_ALT;
304 else if (!strcmp("super", modifier))
305 return MODIFIER_SUPER;
306 else
307 return MODIFIER_SUPER;
308}
309
Juan Zhaoe10d2792012-04-25 19:09:52 +0800310static enum animation_type
311get_animation_type(char *animation)
312{
313 if (!animation)
314 return ANIMATION_NONE;
315
316 if (!strcmp("zoom", animation))
317 return ANIMATION_ZOOM;
318 else if (!strcmp("fade", animation))
319 return ANIMATION_FADE;
320 else
321 return ANIMATION_NONE;
322}
323
Alex Wu4539b082012-03-01 12:57:46 +0800324static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300325shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200326{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200327 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200328 char *path = NULL;
329 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200330 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300331 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800332 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200333
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400334 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300335 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800336 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200337 { "num-workspaces",
338 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200339 };
340
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400341 struct config_key saver_keys[] = {
342 { "path", CONFIG_KEY_STRING, &path },
343 { "duration", CONFIG_KEY_INTEGER, &duration },
344 };
345
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200346 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400347 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200348 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
349 };
350
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200351 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500352 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200353 free(config_file);
354
Pekka Paalanen7296e792011-12-07 16:22:00 +0200355 shell->screensaver.path = path;
356 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300357 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800358 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200359 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
360}
361
362static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200363focus_state_destroy(struct focus_state *state)
364{
365 wl_list_remove(&state->seat_destroy_listener.link);
366 wl_list_remove(&state->surface_destroy_listener.link);
367 free(state);
368}
369
370static void
371focus_state_seat_destroy(struct wl_listener *listener, void *data)
372{
373 struct focus_state *state = container_of(listener,
374 struct focus_state,
375 seat_destroy_listener);
376
377 wl_list_remove(&state->link);
378 focus_state_destroy(state);
379}
380
381static void
382focus_state_surface_destroy(struct wl_listener *listener, void *data)
383{
384 struct focus_state *state = container_of(listener,
385 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400386 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400387 struct desktop_shell *shell;
388 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200389
Kristian Høgsberge3778222012-07-31 17:29:30 -0400390 next = NULL;
391 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
392 if (surface == state->keyboard_focus)
393 continue;
394
395 next = surface;
396 break;
397 }
398
399 if (next) {
400 shell = state->seat->compositor->shell_interface.shell;
401 activate(shell, next, state->seat);
402 } else {
403 wl_list_remove(&state->link);
404 focus_state_destroy(state);
405 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200406}
407
408static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400409focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200410{
Jonas Ådahl04769742012-06-13 00:01:24 +0200411 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200412
413 state = malloc(sizeof *state);
414 if (state == NULL)
415 return NULL;
416
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400417 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200418 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400419 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200420
421 state->seat_destroy_listener.notify = focus_state_seat_destroy;
422 state->surface_destroy_listener.notify = focus_state_surface_destroy;
423 wl_signal_add(&seat->seat.destroy_signal,
424 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400425 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200426
427 return state;
428}
429
430static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400431restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200432{
433 struct focus_state *state, *next;
434
435 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
436 if (state->keyboard_focus)
437 wl_keyboard_set_focus(state->seat->seat.keyboard,
438 &state->keyboard_focus->surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200439 }
440}
441
442static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200443workspace_destroy(struct workspace *ws)
444{
Jonas Ådahl04769742012-06-13 00:01:24 +0200445 struct focus_state *state, *next;
446
447 wl_list_for_each_safe(state, next, &ws->focus_list, link)
448 focus_state_destroy(state);
449
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200450 free(ws);
451}
452
Jonas Ådahl04769742012-06-13 00:01:24 +0200453static void
454seat_destroyed(struct wl_listener *listener, void *data)
455{
456 struct weston_seat *seat = data;
457 struct focus_state *state, *next;
458 struct workspace *ws = container_of(listener,
459 struct workspace,
460 seat_destroyed_listener);
461
462 wl_list_for_each_safe(state, next, &ws->focus_list, link)
463 if (state->seat == seat)
464 wl_list_remove(&state->link);
465}
466
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200467static struct workspace *
468workspace_create(void)
469{
470 struct workspace *ws = malloc(sizeof *ws);
471 if (ws == NULL)
472 return NULL;
473
474 weston_layer_init(&ws->layer, NULL);
475
Jonas Ådahl04769742012-06-13 00:01:24 +0200476 wl_list_init(&ws->focus_list);
477 wl_list_init(&ws->seat_destroyed_listener.link);
478 ws->seat_destroyed_listener.notify = seat_destroyed;
479
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200480 return ws;
481}
482
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200483static int
484workspace_is_empty(struct workspace *ws)
485{
486 return wl_list_empty(&ws->layer.surface_list);
487}
488
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200489static struct workspace *
490get_workspace(struct desktop_shell *shell, unsigned int index)
491{
492 struct workspace **pws = shell->workspaces.array.data;
493 pws += index;
494 return *pws;
495}
496
497static struct workspace *
498get_current_workspace(struct desktop_shell *shell)
499{
500 return get_workspace(shell, shell->workspaces.current);
501}
502
503static void
504activate_workspace(struct desktop_shell *shell, unsigned int index)
505{
506 struct workspace *ws;
507
508 ws = get_workspace(shell, index);
509 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
510
511 shell->workspaces.current = index;
512}
513
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200514static unsigned int
515get_output_height(struct weston_output *output)
516{
517 return abs(output->region.extents.y1 - output->region.extents.y2);
518}
519
520static void
521surface_translate(struct weston_surface *surface, double d)
522{
523 struct shell_surface *shsurf = get_shell_surface(surface);
524 struct weston_transform *transform;
525
526 transform = &shsurf->workspace_transform;
527 if (wl_list_empty(&transform->link))
528 wl_list_insert(surface->geometry.transformation_list.prev,
529 &shsurf->workspace_transform.link);
530
531 weston_matrix_init(&shsurf->workspace_transform.matrix);
532 weston_matrix_translate(&shsurf->workspace_transform.matrix,
533 0.0, d, 0.0);
534 surface->geometry.dirty = 1;
535}
536
537static void
538workspace_translate_out(struct workspace *ws, double fraction)
539{
540 struct weston_surface *surface;
541 unsigned int height;
542 double d;
543
544 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
545 height = get_output_height(surface->output);
546 d = height * fraction;
547
548 surface_translate(surface, d);
549 }
550}
551
552static void
553workspace_translate_in(struct workspace *ws, double fraction)
554{
555 struct weston_surface *surface;
556 unsigned int height;
557 double d;
558
559 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
560 height = get_output_height(surface->output);
561
562 if (fraction > 0)
563 d = -(height - height * fraction);
564 else
565 d = height + height * fraction;
566
567 surface_translate(surface, d);
568 }
569}
570
571static void
572workspace_damage_all_surfaces(struct workspace *ws)
573{
574 struct weston_surface *surface;
575
576 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
577 weston_surface_damage(surface);
578}
579
580static void
581reverse_workspace_change_animation(struct desktop_shell *shell,
582 unsigned int index,
583 struct workspace *from,
584 struct workspace *to)
585{
586 shell->workspaces.current = index;
587
588 shell->workspaces.anim_to = to;
589 shell->workspaces.anim_from = from;
590 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
591 shell->workspaces.anim_timestamp = 0;
592
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400593 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200594
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200595 workspace_damage_all_surfaces(from);
596 workspace_damage_all_surfaces(to);
597}
598
599static void
600workspace_deactivate_transforms(struct workspace *ws)
601{
602 struct weston_surface *surface;
603 struct shell_surface *shsurf;
604
605 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
606 shsurf = get_shell_surface(surface);
607 wl_list_remove(&shsurf->workspace_transform.link);
608 wl_list_init(&shsurf->workspace_transform.link);
609 shsurf->surface->geometry.dirty = 1;
610 }
611}
612
613static void
614finish_workspace_change_animation(struct desktop_shell *shell,
615 struct workspace *from,
616 struct workspace *to)
617{
618 workspace_damage_all_surfaces(from);
619 workspace_damage_all_surfaces(to);
620
621 wl_list_remove(&shell->workspaces.animation.link);
622 workspace_deactivate_transforms(from);
623 workspace_deactivate_transforms(to);
624 shell->workspaces.anim_to = NULL;
625
626 wl_list_remove(&shell->workspaces.anim_from->layer.link);
627}
628
629static void
630animate_workspace_change_frame(struct weston_animation *animation,
631 struct weston_output *output, uint32_t msecs)
632{
633 struct desktop_shell *shell =
634 container_of(animation, struct desktop_shell,
635 workspaces.animation);
636 struct workspace *from = shell->workspaces.anim_from;
637 struct workspace *to = shell->workspaces.anim_to;
638 uint32_t t;
639 double x, y;
640
641 if (workspace_is_empty(from) && workspace_is_empty(to)) {
642 finish_workspace_change_animation(shell, from, to);
643 return;
644 }
645
646 if (shell->workspaces.anim_timestamp == 0) {
647 if (shell->workspaces.anim_current == 0.0)
648 shell->workspaces.anim_timestamp = msecs;
649 else
650 shell->workspaces.anim_timestamp =
651 msecs -
652 /* Invers of movement function 'y' below. */
653 (asin(1.0 - shell->workspaces.anim_current) *
654 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
655 M_2_PI);
656 }
657
658 t = msecs - shell->workspaces.anim_timestamp;
659
660 /*
661 * x = [0, π/2]
662 * y(x) = sin(x)
663 */
664 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
665 y = sin(x);
666
667 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
668 workspace_damage_all_surfaces(from);
669 workspace_damage_all_surfaces(to);
670
671 workspace_translate_out(from, shell->workspaces.anim_dir * y);
672 workspace_translate_in(to, shell->workspaces.anim_dir * y);
673 shell->workspaces.anim_current = y;
674
675 workspace_damage_all_surfaces(from);
676 workspace_damage_all_surfaces(to);
677 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200678 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200679 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200680}
681
682static void
683animate_workspace_change(struct desktop_shell *shell,
684 unsigned int index,
685 struct workspace *from,
686 struct workspace *to)
687{
688 struct weston_output *output;
689
690 int dir;
691
692 if (index > shell->workspaces.current)
693 dir = -1;
694 else
695 dir = 1;
696
697 shell->workspaces.current = index;
698
699 shell->workspaces.anim_dir = dir;
700 shell->workspaces.anim_from = from;
701 shell->workspaces.anim_to = to;
702 shell->workspaces.anim_current = 0.0;
703 shell->workspaces.anim_timestamp = 0;
704
705 output = container_of(shell->compositor->output_list.next,
706 struct weston_output, link);
707 wl_list_insert(&output->animation_list,
708 &shell->workspaces.animation.link);
709
710 wl_list_insert(&from->layer.link, &to->layer.link);
711
712 workspace_translate_in(to, 0);
713
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400714 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200715
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200716 workspace_damage_all_surfaces(from);
717 workspace_damage_all_surfaces(to);
718}
719
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200720static void
721change_workspace(struct desktop_shell *shell, unsigned int index)
722{
723 struct workspace *from;
724 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200725
726 if (index == shell->workspaces.current)
727 return;
728
729 /* Don't change workspace when there is any fullscreen surfaces. */
730 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
731 return;
732
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200733 from = get_current_workspace(shell);
734 to = get_workspace(shell, index);
735
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200736 if (shell->workspaces.anim_from == to &&
737 shell->workspaces.anim_to == from) {
738 reverse_workspace_change_animation(shell, index, from, to);
739 return;
740 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200741
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200742 if (shell->workspaces.anim_to != NULL)
743 finish_workspace_change_animation(shell,
744 shell->workspaces.anim_from,
745 shell->workspaces.anim_to);
746
747 if (workspace_is_empty(to) && workspace_is_empty(from)) {
748 shell->workspaces.current = index;
749 wl_list_insert(&from->layer.link, &to->layer.link);
750 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200751
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400752 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200753 }
754 else
755 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200756}
757
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200758static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400759noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100760 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500761{
762 grab->focus = NULL;
763}
764
765static void
Scott Moreau447013d2012-02-18 05:05:29 -0700766move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100767 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500768{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500769 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100770 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300771 struct shell_surface *shsurf = move->base.shsurf;
772 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100773 int dx = wl_fixed_to_int(pointer->x + move->dx);
774 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300775
776 if (!shsurf)
777 return;
778
779 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500780
Daniel Stone103db7f2012-05-08 17:17:55 +0100781 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200782 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400783
784 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500785}
786
787static void
Scott Moreau447013d2012-02-18 05:05:29 -0700788move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100789 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500790{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300791 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
792 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100793 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100794 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500795
Daniel Stone4dbadb12012-05-30 16:31:51 +0100796 if (pointer->button_count == 0 &&
797 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300798 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500799 free(grab);
800 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500801}
802
Scott Moreau447013d2012-02-18 05:05:29 -0700803static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500804 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500805 move_grab_motion,
806 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500807};
808
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -0400809static int
810surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
811{
812 struct weston_move_grab *move;
813
814 if (!shsurf)
815 return -1;
816
817 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
818 return 0;
819
820 move = malloc(sizeof *move);
821 if (!move)
822 return -1;
823
824 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
825 ws->seat.pointer->grab_x;
826 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
827 ws->seat.pointer->grab_y;
828
829 shell_grab_start(&move->base, &move_grab_interface, shsurf,
830 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
831
832 return 0;
833}
834
835static void
836shell_surface_move(struct wl_client *client, struct wl_resource *resource,
837 struct wl_resource *seat_resource, uint32_t serial)
838{
839 struct weston_seat *ws = seat_resource->data;
840 struct shell_surface *shsurf = resource->data;
841
842 if (ws->seat.pointer->button_count == 0 ||
843 ws->seat.pointer->grab_serial != serial ||
844 ws->seat.pointer->focus != &shsurf->surface->surface)
845 return;
846
847 if (surface_move(shsurf, ws) < 0)
848 wl_resource_post_no_memory(resource);
849}
850
851struct weston_resize_grab {
852 struct shell_grab base;
853 uint32_t edges;
854 int32_t width, height;
855};
856
857static void
858resize_grab_motion(struct wl_pointer_grab *grab,
859 uint32_t time, wl_fixed_t x, wl_fixed_t y)
860{
861 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
862 struct wl_pointer *pointer = grab->pointer;
863 struct shell_surface *shsurf = resize->base.shsurf;
864 int32_t width, height;
865 wl_fixed_t from_x, from_y;
866 wl_fixed_t to_x, to_y;
867
868 if (!shsurf)
869 return;
870
871 weston_surface_from_global_fixed(shsurf->surface,
872 pointer->grab_x, pointer->grab_y,
873 &from_x, &from_y);
874 weston_surface_from_global_fixed(shsurf->surface,
875 pointer->x, pointer->y, &to_x, &to_y);
876
877 width = resize->width;
878 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
879 width += wl_fixed_to_int(from_x - to_x);
880 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
881 width += wl_fixed_to_int(to_x - from_x);
882 }
883
884 height = resize->height;
885 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
886 height += wl_fixed_to_int(from_y - to_y);
887 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
888 height += wl_fixed_to_int(to_y - from_y);
889 }
890
891 shsurf->client->send_configure(shsurf->surface,
892 resize->edges, width, height);
893}
894
895static void
896send_configure(struct weston_surface *surface,
897 uint32_t edges, int32_t width, int32_t height)
898{
899 struct shell_surface *shsurf = get_shell_surface(surface);
900
901 wl_shell_surface_send_configure(&shsurf->resource,
902 edges, width, height);
903}
904
905static const struct weston_shell_client shell_client = {
906 send_configure
907};
908
909static void
910resize_grab_button(struct wl_pointer_grab *grab,
911 uint32_t time, uint32_t button, uint32_t state_w)
912{
913 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
914 struct wl_pointer *pointer = grab->pointer;
915 enum wl_pointer_button_state state = state_w;
916
917 if (pointer->button_count == 0 &&
918 state == WL_POINTER_BUTTON_STATE_RELEASED) {
919 shell_grab_end(&resize->base);
920 free(grab);
921 }
922}
923
924static const struct wl_pointer_grab_interface resize_grab_interface = {
925 noop_grab_focus,
926 resize_grab_motion,
927 resize_grab_button,
928};
929
930static int
931surface_resize(struct shell_surface *shsurf,
932 struct weston_seat *ws, uint32_t edges)
933{
934 struct weston_resize_grab *resize;
935
936 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
937 return 0;
938
939 if (edges == 0 || edges > 15 ||
940 (edges & 3) == 3 || (edges & 12) == 12)
941 return 0;
942
943 resize = malloc(sizeof *resize);
944 if (!resize)
945 return -1;
946
947 resize->edges = edges;
948 resize->width = shsurf->surface->geometry.width;
949 resize->height = shsurf->surface->geometry.height;
950
951 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
952 ws->seat.pointer, edges);
953
954 return 0;
955}
956
957static void
958shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
959 struct wl_resource *seat_resource, uint32_t serial,
960 uint32_t edges)
961{
962 struct weston_seat *ws = seat_resource->data;
963 struct shell_surface *shsurf = resource->data;
964
965 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
966 return;
967
968 if (ws->seat.pointer->button_count == 0 ||
969 ws->seat.pointer->grab_serial != serial ||
970 ws->seat.pointer->focus != &shsurf->surface->surface)
971 return;
972
973 if (surface_resize(shsurf, ws, edges) < 0)
974 wl_resource_post_no_memory(resource);
975}
976
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600977static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400978busy_cursor_grab_focus(struct wl_pointer_grab *base,
979 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600980{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400981 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600982
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400983 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300984 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400985 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600986 }
987}
988
989static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400990busy_cursor_grab_motion(struct wl_pointer_grab *grab,
991 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600992{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400993}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600994
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400995static void
996busy_cursor_grab_button(struct wl_pointer_grab *grab,
997 uint32_t time, uint32_t button, uint32_t state)
998{
999}
1000
1001static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1002 busy_cursor_grab_focus,
1003 busy_cursor_grab_motion,
1004 busy_cursor_grab_button,
1005};
1006
1007static void
1008set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1009{
1010 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001011
1012 grab = malloc(sizeof *grab);
1013 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001014 return;
1015
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001016 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1017 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001018}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001019
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001020static void
1021end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1022{
1023 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1024
1025 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001026 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001027 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001028 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001029}
1030
Scott Moreau9521d5e2012-04-19 13:06:17 -06001031static void
1032ping_timer_destroy(struct shell_surface *shsurf)
1033{
1034 if (!shsurf || !shsurf->ping_timer)
1035 return;
1036
1037 if (shsurf->ping_timer->source)
1038 wl_event_source_remove(shsurf->ping_timer->source);
1039
1040 free(shsurf->ping_timer);
1041 shsurf->ping_timer = NULL;
1042}
1043
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001044static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001045ping_timeout_handler(void *data)
1046{
1047 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001048 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001049
Scott Moreau9521d5e2012-04-19 13:06:17 -06001050 /* Client is not responding */
1051 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001052
1053 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1054 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1055 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001056
1057 return 1;
1058}
1059
1060static void
1061ping_handler(struct weston_surface *surface, uint32_t serial)
1062{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001063 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001064 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001065 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001066
1067 if (!shsurf)
1068 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001069 if (!shsurf->resource.client)
1070 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001071
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001072 if (shsurf->surface == shsurf->shell->grab_surface)
1073 return;
1074
Scott Moreauff1db4a2012-04-17 19:06:18 -06001075 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001076 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001077 if (!shsurf->ping_timer)
1078 return;
1079
1080 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001081 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1082 shsurf->ping_timer->source =
1083 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1084 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1085
1086 wl_shell_surface_send_ping(&shsurf->resource, serial);
1087 }
1088}
1089
1090static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001091handle_pointer_focus(struct wl_listener *listener, void *data)
1092{
1093 struct wl_pointer *pointer = data;
1094 struct weston_surface *surface =
1095 (struct weston_surface *) pointer->focus;
1096 struct weston_compositor *compositor;
1097 struct shell_surface *shsurf;
1098 uint32_t serial;
1099
1100 if (!surface)
1101 return;
1102
1103 compositor = surface->compositor;
1104 shsurf = get_shell_surface(surface);
1105
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001106 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001107 set_busy_cursor(shsurf, pointer);
1108 } else {
1109 serial = wl_display_next_serial(compositor->wl_display);
1110 ping_handler(surface, serial);
1111 }
1112}
1113
1114static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001115shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1116 uint32_t serial)
1117{
1118 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001119 struct desktop_shell *shell = shsurf->shell;
1120 struct weston_seat *seat;
1121 struct weston_compositor *ec = shsurf->surface->compositor;
1122 struct wl_pointer *pointer;
1123 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001124
Scott Moreauff1db4a2012-04-17 19:06:18 -06001125 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001126 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001127 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001128 if (was_unresponsive) {
1129 /* Received pong from previously unresponsive client */
1130 wl_list_for_each(seat, &ec->seat_list, link) {
1131 pointer = seat->seat.pointer;
1132 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001133 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001134 pointer->current ==
1135 &shsurf->surface->surface)
1136 end_busy_cursor(shsurf, pointer);
1137 }
1138 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001139 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001140 }
1141}
1142
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001143static void
1144shell_surface_set_title(struct wl_client *client,
1145 struct wl_resource *resource, const char *title)
1146{
1147 struct shell_surface *shsurf = resource->data;
1148
1149 free(shsurf->title);
1150 shsurf->title = strdup(title);
1151}
1152
1153static void
1154shell_surface_set_class(struct wl_client *client,
1155 struct wl_resource *resource, const char *class)
1156{
1157 struct shell_surface *shsurf = resource->data;
1158
1159 free(shsurf->class);
1160 shsurf->class = strdup(class);
1161}
1162
Juan Zhao96879df2012-02-07 08:45:41 +08001163static struct weston_output *
1164get_default_output(struct weston_compositor *compositor)
1165{
1166 return container_of(compositor->output_list.next,
1167 struct weston_output, link);
1168}
1169
Alex Wu4539b082012-03-01 12:57:46 +08001170static void
1171shell_unset_fullscreen(struct shell_surface *shsurf)
1172{
1173 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001174 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1175 shell_surface_is_top_fullscreen(shsurf)) {
1176 weston_output_switch_mode(shsurf->fullscreen_output,
1177 shsurf->fullscreen_output->origin);
1178 }
Alex Wu4539b082012-03-01 12:57:46 +08001179 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1180 shsurf->fullscreen.framerate = 0;
1181 wl_list_remove(&shsurf->fullscreen.transform.link);
1182 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001183 if (shsurf->fullscreen.black_surface)
1184 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001185 shsurf->fullscreen.black_surface = NULL;
1186 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001187 weston_surface_set_position(shsurf->surface,
1188 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001189 if (shsurf->saved_rotation_valid) {
1190 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1191 &shsurf->rotation.transform.link);
1192 shsurf->saved_rotation_valid = false;
1193 }
Alex Wu4539b082012-03-01 12:57:46 +08001194}
1195
Pekka Paalanen98262232011-12-01 10:42:22 +02001196static int
1197reset_shell_surface_type(struct shell_surface *surface)
1198{
1199 switch (surface->type) {
1200 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001201 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001202 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001203 case SHELL_SURFACE_MAXIMIZED:
1204 surface->output = get_default_output(surface->surface->compositor);
1205 weston_surface_set_position(surface->surface,
1206 surface->saved_x,
1207 surface->saved_y);
1208 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001209 case SHELL_SURFACE_NONE:
1210 case SHELL_SURFACE_TOPLEVEL:
1211 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001212 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001213 break;
1214 }
1215
1216 surface->type = SHELL_SURFACE_NONE;
1217 return 0;
1218}
1219
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001220static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001221set_surface_type(struct shell_surface *shsurf)
1222{
1223 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001224 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001225
1226 reset_shell_surface_type(shsurf);
1227
1228 shsurf->type = shsurf->next_type;
1229 shsurf->next_type = SHELL_SURFACE_NONE;
1230
1231 switch (shsurf->type) {
1232 case SHELL_SURFACE_TOPLEVEL:
1233 break;
1234 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001235 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001236 pes->geometry.x + shsurf->transient.x,
1237 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001238 break;
1239
1240 case SHELL_SURFACE_MAXIMIZED:
1241 shsurf->saved_x = surface->geometry.x;
1242 shsurf->saved_y = surface->geometry.y;
1243 shsurf->saved_position_valid = true;
1244 break;
1245
1246 case SHELL_SURFACE_FULLSCREEN:
1247 shsurf->saved_x = surface->geometry.x;
1248 shsurf->saved_y = surface->geometry.y;
1249 shsurf->saved_position_valid = true;
1250
1251 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1252 wl_list_remove(&shsurf->rotation.transform.link);
1253 wl_list_init(&shsurf->rotation.transform.link);
1254 shsurf->surface->geometry.dirty = 1;
1255 shsurf->saved_rotation_valid = true;
1256 }
1257 break;
1258
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001259 default:
1260 break;
1261 }
1262}
1263
1264static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001265set_toplevel(struct shell_surface *shsurf)
1266{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001267 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001268}
1269
1270static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001271shell_surface_set_toplevel(struct wl_client *client,
1272 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001273{
Pekka Paalanen98262232011-12-01 10:42:22 +02001274 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001275
Tiago Vignattibc052c92012-04-19 16:18:18 +03001276 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001277}
1278
1279static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001280set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001281 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001282{
1283 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001284 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001285 shsurf->transient.x = x;
1286 shsurf->transient.y = y;
1287 shsurf->transient.flags = flags;
1288 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1289}
1290
1291static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001292shell_surface_set_transient(struct wl_client *client,
1293 struct wl_resource *resource,
1294 struct wl_resource *parent_resource,
1295 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001296{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001297 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001298 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001299
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001300 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001301}
1302
Tiago Vignattibe143262012-04-16 17:31:41 +03001303static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001304shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001305{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001306 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001307}
1308
1309static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001310get_output_panel_height(struct desktop_shell *shell,
1311 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001312{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001313 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001314 int panel_height = 0;
1315
1316 if (!output)
1317 return 0;
1318
Juan Zhao4ab94682012-07-09 22:24:09 -07001319 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001320 if (surface->output == output) {
1321 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001322 break;
1323 }
1324 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001325
Juan Zhao96879df2012-02-07 08:45:41 +08001326 return panel_height;
1327}
1328
1329static void
1330shell_surface_set_maximized(struct wl_client *client,
1331 struct wl_resource *resource,
1332 struct wl_resource *output_resource )
1333{
1334 struct shell_surface *shsurf = resource->data;
1335 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001336 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001337 uint32_t edges = 0, panel_height = 0;
1338
1339 /* get the default output, if the client set it as NULL
1340 check whether the ouput is available */
1341 if (output_resource)
1342 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001343 else if (es->output)
1344 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001345 else
1346 shsurf->output = get_default_output(es->compositor);
1347
Tiago Vignattibe143262012-04-16 17:31:41 +03001348 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001349 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001350 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001351
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001352 shsurf->client->send_configure(shsurf->surface, edges,
Rob Bradford31b68622012-07-02 19:00:19 +01001353 shsurf->output->current->width,
1354 shsurf->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001355
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001356 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001357}
1358
Alex Wu21858432012-04-01 20:13:08 +08001359static void
1360black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1361
Alex Wu4539b082012-03-01 12:57:46 +08001362static struct weston_surface *
1363create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001364 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001365 GLfloat x, GLfloat y, int w, int h)
1366{
1367 struct weston_surface *surface = NULL;
1368
1369 surface = weston_surface_create(ec);
1370 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001371 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001372 return NULL;
1373 }
1374
Alex Wu21858432012-04-01 20:13:08 +08001375 surface->configure = black_surface_configure;
1376 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001377 weston_surface_configure(surface, x, y, w, h);
1378 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1379 return surface;
1380}
1381
1382/* Create black surface and append it to the associated fullscreen surface.
1383 * Handle size dismatch and positioning according to the method. */
1384static void
1385shell_configure_fullscreen(struct shell_surface *shsurf)
1386{
1387 struct weston_output *output = shsurf->fullscreen_output;
1388 struct weston_surface *surface = shsurf->surface;
1389 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001390 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001391
Alex Wu4539b082012-03-01 12:57:46 +08001392 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001393 shsurf->fullscreen.black_surface =
1394 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001395 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001396 output->x, output->y,
1397 output->current->width,
1398 output->current->height);
1399
1400 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1401 wl_list_insert(&surface->layer_link,
1402 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001403 shsurf->fullscreen.black_surface->output = output;
1404
1405 switch (shsurf->fullscreen.type) {
1406 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001407 if (surface->buffer)
1408 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001409 break;
1410 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1411 matrix = &shsurf->fullscreen.transform.matrix;
1412 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001413
1414 output_aspect = (float) output->current->width /
1415 (float) output->current->height;
1416 surface_aspect = (float) surface->geometry.width /
1417 (float) surface->geometry.height;
1418 if (output_aspect < surface_aspect)
1419 scale = (float) output->current->width /
1420 (float) surface->geometry.width;
1421 else
1422 scale = (float) output->current->height /
1423 (float) surface->geometry.height;
1424
Alex Wu4539b082012-03-01 12:57:46 +08001425 weston_matrix_scale(matrix, scale, scale, 1);
1426 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001427 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001428 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001429 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1430 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1431 weston_surface_set_position(surface, x, y);
1432
Alex Wu4539b082012-03-01 12:57:46 +08001433 break;
1434 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001435 if (shell_surface_is_top_fullscreen(shsurf)) {
1436 struct weston_mode mode = {0,
1437 surface->geometry.width,
1438 surface->geometry.height,
1439 shsurf->fullscreen.framerate};
1440
1441 if (weston_output_switch_mode(output, &mode) == 0) {
1442 weston_surface_configure(shsurf->fullscreen.black_surface,
1443 output->x, output->y,
1444 output->current->width,
1445 output->current->height);
1446 weston_surface_set_position(surface, output->x, output->y);
1447 break;
1448 }
1449 }
Alex Wu4539b082012-03-01 12:57:46 +08001450 break;
1451 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1452 break;
1453 default:
1454 break;
1455 }
1456}
1457
1458/* make the fullscreen and black surface at the top */
1459static void
1460shell_stack_fullscreen(struct shell_surface *shsurf)
1461{
Alex Wubd3354b2012-04-17 17:20:49 +08001462 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001463 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001464 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001465
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001466 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001467 wl_list_insert(&shell->fullscreen_layer.surface_list,
1468 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001469 weston_surface_damage(surface);
1470
1471 if (!shsurf->fullscreen.black_surface)
1472 shsurf->fullscreen.black_surface =
1473 create_black_surface(surface->compositor,
1474 surface,
1475 output->x, output->y,
1476 output->current->width,
1477 output->current->height);
1478
1479 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001480 wl_list_insert(&surface->layer_link,
1481 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001482 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001483}
1484
1485static void
1486shell_map_fullscreen(struct shell_surface *shsurf)
1487{
Alex Wu4539b082012-03-01 12:57:46 +08001488 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001489 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001490}
1491
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001492static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001493shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001494 struct wl_resource *resource,
1495 uint32_t method,
1496 uint32_t framerate,
1497 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001498{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001499 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001500 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001501
1502 if (output_resource)
1503 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001504 else if (es->output)
1505 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001506 else
1507 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001508
Alex Wu4539b082012-03-01 12:57:46 +08001509 shsurf->fullscreen_output = shsurf->output;
1510 shsurf->fullscreen.type = method;
1511 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001512 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001513
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001514 shsurf->client->send_configure(shsurf->surface, 0,
1515 shsurf->output->current->width,
1516 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001517}
1518
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001519static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001520popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001521 struct wl_surface *surface,
1522 wl_fixed_t x,
1523 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001524{
Daniel Stone37816df2012-05-16 18:45:18 +01001525 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001526 struct shell_surface *priv =
1527 container_of(grab, struct shell_surface, popup.grab);
1528 struct wl_client *client = priv->surface->surface.resource.client;
1529
Pekka Paalanencb108432012-01-19 16:25:40 +02001530 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001531 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001532 grab->focus = surface;
1533 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001534 wl_pointer_set_focus(pointer, NULL,
1535 wl_fixed_from_int(0),
1536 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001537 grab->focus = NULL;
1538 }
1539}
1540
1541static void
Scott Moreau447013d2012-02-18 05:05:29 -07001542popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001543 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001544{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001545 struct wl_resource *resource;
1546
Daniel Stone37816df2012-05-16 18:45:18 +01001547 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001548 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001549 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001550}
1551
1552static void
Scott Moreau447013d2012-02-18 05:05:29 -07001553popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001554 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001555{
1556 struct wl_resource *resource;
1557 struct shell_surface *shsurf =
1558 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001559 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001560 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001561 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001562
Daniel Stone37816df2012-05-16 18:45:18 +01001563 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001565 display = wl_client_get_display(resource->client);
1566 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001567 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001568 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001569 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001570 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001571 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001572 wl_pointer_end_grab(grab->pointer);
1573 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001574 }
1575
Daniel Stone4dbadb12012-05-30 16:31:51 +01001576 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577 shsurf->popup.initial_up = 1;
1578}
1579
Scott Moreau447013d2012-02-18 05:05:29 -07001580static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001581 popup_grab_focus,
1582 popup_grab_motion,
1583 popup_grab_button,
1584};
1585
1586static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001587shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001588{
Daniel Stone37816df2012-05-16 18:45:18 +01001589 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001591 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001592
1593 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001594 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001595
Pekka Paalanen938269a2012-02-07 14:19:01 +02001596 weston_surface_update_transform(parent);
1597 if (parent->transform.enabled) {
1598 shsurf->popup.parent_transform.matrix =
1599 parent->transform.matrix;
1600 } else {
1601 /* construct x, y translation matrix */
1602 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1603 shsurf->popup.parent_transform.matrix.d[12] =
1604 parent->geometry.x;
1605 shsurf->popup.parent_transform.matrix.d[13] =
1606 parent->geometry.y;
1607 }
1608 wl_list_insert(es->geometry.transformation_list.prev,
1609 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001610 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001611
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001612 shsurf->popup.initial_up = 0;
1613
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001614 /* We don't require the grab to still be active, but if another
1615 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001616 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1617 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001618 } else {
1619 wl_shell_surface_send_popup_done(&shsurf->resource);
1620 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001621}
1622
1623static void
1624shell_surface_set_popup(struct wl_client *client,
1625 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001626 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001627 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001628 struct wl_resource *parent_resource,
1629 int32_t x, int32_t y, uint32_t flags)
1630{
1631 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001632
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001633 shsurf->type = SHELL_SURFACE_POPUP;
1634 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001635 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001636 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001637 shsurf->popup.x = x;
1638 shsurf->popup.y = y;
1639}
1640
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001641static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001642 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001643 shell_surface_move,
1644 shell_surface_resize,
1645 shell_surface_set_toplevel,
1646 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001647 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001648 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001649 shell_surface_set_maximized,
1650 shell_surface_set_title,
1651 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001652};
1653
1654static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001655destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001656{
Daniel Stone37816df2012-05-16 18:45:18 +01001657 if (shsurf->popup.grab.pointer)
1658 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001659
Alex Wubd3354b2012-04-17 17:20:49 +08001660 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1661 shell_surface_is_top_fullscreen(shsurf)) {
1662 weston_output_switch_mode(shsurf->fullscreen_output,
1663 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001664 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001665
Alex Wuaa08e2d2012-03-05 11:01:40 +08001666 if (shsurf->fullscreen.black_surface)
1667 weston_surface_destroy(shsurf->fullscreen.black_surface);
1668
Alex Wubd3354b2012-04-17 17:20:49 +08001669 /* As destroy_resource() use wl_list_for_each_safe(),
1670 * we can always remove the listener.
1671 */
1672 wl_list_remove(&shsurf->surface_destroy_listener.link);
1673 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001674 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001675
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001676 wl_list_remove(&shsurf->link);
1677 free(shsurf);
1678}
1679
1680static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001681shell_destroy_shell_surface(struct wl_resource *resource)
1682{
1683 struct shell_surface *shsurf = resource->data;
1684
1685 destroy_shell_surface(shsurf);
1686}
1687
1688static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001689shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001690{
1691 struct shell_surface *shsurf = container_of(listener,
1692 struct shell_surface,
1693 surface_destroy_listener);
1694
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001695 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001696 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001697 } else {
1698 wl_signal_emit(&shsurf->resource.destroy_signal,
1699 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001700 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001701 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001702}
1703
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001704static void
1705shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1706
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001707static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001708get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001709{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001710 if (surface->configure == shell_surface_configure)
1711 return surface->private;
1712 else
1713 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001714}
1715
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001716static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001717create_shell_surface(void *shell, struct weston_surface *surface,
1718 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001719{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001720 struct shell_surface *shsurf;
1721
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001722 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001723 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001724 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001725 }
1726
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001727 shsurf = calloc(1, sizeof *shsurf);
1728 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001729 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001730 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001731 }
1732
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001733 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001734 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001735
Tiago Vignattibc052c92012-04-19 16:18:18 +03001736 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001737 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001738 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001739 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001740 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001741 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1742 shsurf->fullscreen.framerate = 0;
1743 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001744 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001745 wl_list_init(&shsurf->fullscreen.transform.link);
1746
Tiago Vignattibc052c92012-04-19 16:18:18 +03001747 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001748 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1749 wl_signal_add(&surface->surface.resource.destroy_signal,
1750 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001751
1752 /* init link so its safe to always remove it in destroy_shell_surface */
1753 wl_list_init(&shsurf->link);
1754
Pekka Paalanen460099f2012-01-20 16:48:25 +02001755 /* empty when not in use */
1756 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001757 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001758
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001759 wl_list_init(&shsurf->workspace_transform.link);
1760
Pekka Paalanen98262232011-12-01 10:42:22 +02001761 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001762 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001763
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001764 shsurf->client = client;
1765
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001766 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001767}
1768
1769static void
1770shell_get_shell_surface(struct wl_client *client,
1771 struct wl_resource *resource,
1772 uint32_t id,
1773 struct wl_resource *surface_resource)
1774{
1775 struct weston_surface *surface = surface_resource->data;
1776 struct desktop_shell *shell = resource->data;
1777 struct shell_surface *shsurf;
1778
1779 if (get_shell_surface(surface)) {
1780 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001781 WL_DISPLAY_ERROR_INVALID_OBJECT,
1782 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001783 return;
1784 }
1785
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001786 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001787 if (!shsurf) {
1788 wl_resource_post_error(surface_resource,
1789 WL_DISPLAY_ERROR_INVALID_OBJECT,
1790 "surface->configure already set");
1791 return;
1792 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001793
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001794 shsurf->resource.destroy = shell_destroy_shell_surface;
1795 shsurf->resource.object.id = id;
1796 shsurf->resource.object.interface = &wl_shell_surface_interface;
1797 shsurf->resource.object.implementation =
1798 (void (**)(void)) &shell_surface_implementation;
1799 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001800
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001801 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001802}
1803
1804static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001805 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001806};
1807
Kristian Høgsberg07937562011-04-12 17:25:42 -04001808static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001809handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001810{
1811 proc->pid = 0;
1812}
1813
1814static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001815launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001816{
1817 if (shell->screensaver.binding)
1818 return;
1819
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001820 if (!shell->screensaver.path)
1821 return;
1822
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001823 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001824 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001825 return;
1826 }
1827
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001828 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001829 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001830 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001831 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001832}
1833
1834static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001835terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001836{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001837 if (shell->screensaver.process.pid == 0)
1838 return;
1839
1840 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001841}
1842
1843static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001844configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001845{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001846 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001847
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001848 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1849 if (s->output == es->output && s != es) {
1850 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001851 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001852 }
1853 }
1854
1855 weston_surface_configure(es, es->output->x, es->output->y,
1856 es->buffer->width, es->buffer->height);
1857
1858 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001859 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001860 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001861 }
1862}
1863
1864static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001865background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1866{
1867 struct desktop_shell *shell = es->private;
1868
1869 configure_static_surface(es, &shell->background_layer);
1870}
1871
1872static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001873desktop_shell_set_background(struct wl_client *client,
1874 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001875 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001876 struct wl_resource *surface_resource)
1877{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001878 struct desktop_shell *shell = resource->data;
1879 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001880
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001881 if (surface->configure) {
1882 wl_resource_post_error(surface_resource,
1883 WL_DISPLAY_ERROR_INVALID_OBJECT,
1884 "surface role already assigned");
1885 return;
1886 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001887
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001888 surface->configure = background_configure;
1889 surface->private = shell;
1890 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001891 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001892 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001893 surface->output->current->width,
1894 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001895}
1896
1897static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001898panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1899{
1900 struct desktop_shell *shell = es->private;
1901
1902 configure_static_surface(es, &shell->panel_layer);
1903}
1904
1905static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001906desktop_shell_set_panel(struct wl_client *client,
1907 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001908 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001909 struct wl_resource *surface_resource)
1910{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001911 struct desktop_shell *shell = resource->data;
1912 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001913
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001914 if (surface->configure) {
1915 wl_resource_post_error(surface_resource,
1916 WL_DISPLAY_ERROR_INVALID_OBJECT,
1917 "surface role already assigned");
1918 return;
1919 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001920
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001921 surface->configure = panel_configure;
1922 surface->private = shell;
1923 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001924 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001925 surface_resource,
1926 surface->output->current->width,
1927 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001928}
1929
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001930static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001931lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1932{
1933 struct desktop_shell *shell = surface->private;
1934
1935 center_on_output(surface, get_default_output(shell->compositor));
1936
1937 if (!weston_surface_is_mapped(surface)) {
1938 wl_list_insert(&shell->lock_layer.surface_list,
1939 &surface->layer_link);
1940 weston_surface_assign_output(surface);
1941 weston_compositor_wake(shell->compositor);
1942 }
1943}
1944
1945static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001946handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001947{
Tiago Vignattibe143262012-04-16 17:31:41 +03001948 struct desktop_shell *shell =
1949 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001950
Martin Minarik6d118362012-06-07 18:01:59 +02001951 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001952 shell->lock_surface = NULL;
1953}
1954
1955static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001956desktop_shell_set_lock_surface(struct wl_client *client,
1957 struct wl_resource *resource,
1958 struct wl_resource *surface_resource)
1959{
Tiago Vignattibe143262012-04-16 17:31:41 +03001960 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001961 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001962
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001963 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001964
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001965 if (!shell->locked)
1966 return;
1967
Pekka Paalanen98262232011-12-01 10:42:22 +02001968 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001969
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001970 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1971 wl_signal_add(&surface_resource->destroy_signal,
1972 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001973
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001974 surface->configure = lock_surface_configure;
1975 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001976}
1977
1978static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001979resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001980{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001981 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001982 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001983
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001984 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1985 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001986
1987 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001988
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001989 wl_list_remove(&shell->lock_layer.link);
1990 wl_list_insert(&shell->compositor->cursor_layer.link,
1991 &shell->fullscreen_layer.link);
1992 wl_list_insert(&shell->fullscreen_layer.link,
1993 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001994 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001995
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001996 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02001997
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001998 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001999 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002000 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002001 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002002}
2003
2004static void
2005desktop_shell_unlock(struct wl_client *client,
2006 struct wl_resource *resource)
2007{
Tiago Vignattibe143262012-04-16 17:31:41 +03002008 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002009
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002010 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002011
2012 if (shell->locked)
2013 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002014}
2015
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002016static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002017desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002018 struct wl_resource *resource,
2019 struct wl_resource *surface_resource)
2020{
2021 struct desktop_shell *shell = resource->data;
2022
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002023 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002024}
2025
Kristian Høgsberg75840622011-09-06 13:48:16 -04002026static const struct desktop_shell_interface desktop_shell_implementation = {
2027 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002028 desktop_shell_set_panel,
2029 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002030 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002031 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002032};
2033
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002034static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002035get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002036{
2037 struct shell_surface *shsurf;
2038
2039 shsurf = get_shell_surface(surface);
2040 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002041 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002042 return shsurf->type;
2043}
2044
Kristian Høgsberg75840622011-09-06 13:48:16 -04002045static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002046move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002047{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002048 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002049 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002050 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002051
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002052 if (surface == NULL)
2053 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002054
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002055 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002056 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002057 return;
2058
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002059 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002060}
2061
2062static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002063resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002064{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002065 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002066 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002067 uint32_t edges = 0;
2068 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002069 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002070
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002071 if (surface == NULL)
2072 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002073
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002074 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002075 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002076 return;
2077
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002078 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002079 wl_fixed_to_int(seat->pointer->grab_x),
2080 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002081 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002082
Pekka Paalanen60921e52012-01-25 15:55:43 +02002083 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002084 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002085 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002086 edges |= 0;
2087 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002088 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002089
Pekka Paalanen60921e52012-01-25 15:55:43 +02002090 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002091 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002092 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002093 edges |= 0;
2094 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002095 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002096
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002097 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002098}
2099
2100static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002101surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002102 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002103{
Scott Moreau02709af2012-05-22 01:54:10 -06002104 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002105 struct shell_surface *shsurf;
2106 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002107 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002108
2109 if (surface == NULL)
2110 return;
2111
2112 shsurf = get_shell_surface(surface);
2113 if (!shsurf)
2114 return;
2115
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002116 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002117
Scott Moreau02709af2012-05-22 01:54:10 -06002118 if (surface->alpha > 1.0)
2119 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002120 if (surface->alpha < step)
2121 surface->alpha = step;
2122
2123 surface->geometry.dirty = 1;
2124 weston_surface_damage(surface);
2125}
2126
2127static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002128do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002129 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002130{
Daniel Stone37816df2012-05-16 18:45:18 +01002131 struct weston_seat *ws = (struct weston_seat *) seat;
2132 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002133 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002134 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002135
2136 wl_list_for_each(output, &compositor->output_list, link) {
2137 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002138 wl_fixed_to_double(seat->pointer->x),
2139 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002140 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002141 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002142 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002143 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002144 increment = -output->zoom.increment;
2145 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002146 increment = output->zoom.increment *
2147 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002148 else
2149 increment = 0;
2150
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002151 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002152
Scott Moreaue6603982012-06-11 13:07:51 -06002153 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002154 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002155 else if (output->zoom.level > output->zoom.max_level)
2156 output->zoom.level = output->zoom.max_level;
2157 else
2158 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002159
Scott Moreaue6603982012-06-11 13:07:51 -06002160 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002161
Scott Moreau8dacaab2012-06-17 18:10:58 -06002162 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002163 }
2164 }
2165}
2166
Scott Moreauccbf29d2012-02-22 14:21:41 -07002167static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002168zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002169 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002170{
2171 do_zoom(seat, time, 0, axis, value);
2172}
2173
2174static void
2175zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2176 void *data)
2177{
2178 do_zoom(seat, time, key, 0, 0);
2179}
2180
2181static void
2182terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2183 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002184{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002185 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002186
Daniel Stone325fc2d2012-05-30 16:31:58 +01002187 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002188}
2189
2190static void
Scott Moreau447013d2012-02-18 05:05:29 -07002191rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002192 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002193{
2194 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002195 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002196 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002197 struct shell_surface *shsurf = rotate->base.shsurf;
2198 struct weston_surface *surface;
2199 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2200
2201 if (!shsurf)
2202 return;
2203
2204 surface = shsurf->surface;
2205
2206 cx = 0.5f * surface->geometry.width;
2207 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002208
Daniel Stone37816df2012-05-16 18:45:18 +01002209 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2210 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002211 r = sqrtf(dx * dx + dy * dy);
2212
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002213 wl_list_remove(&shsurf->rotation.transform.link);
2214 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002215
2216 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002217 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002218 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002219
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002220 weston_matrix_init(&rotate->rotation);
2221 rotate->rotation.d[0] = dx / r;
2222 rotate->rotation.d[4] = -dy / r;
2223 rotate->rotation.d[1] = -rotate->rotation.d[4];
2224 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002225
2226 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002227 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002228 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002229 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002230 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002231
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002232 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002233 &shsurf->surface->geometry.transformation_list,
2234 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002235 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002236 wl_list_init(&shsurf->rotation.transform.link);
2237 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002238 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002239 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002240
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002241 /* We need to adjust the position of the surface
2242 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002243 cposx = surface->geometry.x + cx;
2244 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002245 dposx = rotate->center.x - cposx;
2246 dposy = rotate->center.y - cposy;
2247 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002248 weston_surface_set_position(surface,
2249 surface->geometry.x + dposx,
2250 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002251 }
2252
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002253 /* Repaint implies weston_surface_update_transform(), which
2254 * lazily applies the damage due to rotation update.
2255 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002256 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002257}
2258
2259static void
Scott Moreau447013d2012-02-18 05:05:29 -07002260rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002261 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002262{
2263 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002264 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002265 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002266 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002267 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002268
Daniel Stone4dbadb12012-05-30 16:31:51 +01002269 if (pointer->button_count == 0 &&
2270 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002271 if (shsurf)
2272 weston_matrix_multiply(&shsurf->rotation.rotation,
2273 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002274 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002275 free(rotate);
2276 }
2277}
2278
Scott Moreau447013d2012-02-18 05:05:29 -07002279static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002280 noop_grab_focus,
2281 rotate_grab_motion,
2282 rotate_grab_button,
2283};
2284
2285static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002286rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002287 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002288{
2289 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002290 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002291 struct shell_surface *surface;
2292 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002293 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002294 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002295
2296 if (base_surface == NULL)
2297 return;
2298
2299 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002300 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002301 return;
2302
Pekka Paalanen460099f2012-01-20 16:48:25 +02002303 rotate = malloc(sizeof *rotate);
2304 if (!rotate)
2305 return;
2306
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002307 weston_surface_to_global_float(surface->surface,
2308 surface->surface->geometry.width / 2,
2309 surface->surface->geometry.height / 2,
2310 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002311
Daniel Stone37816df2012-05-16 18:45:18 +01002312 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2313 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002314 r = sqrtf(dx * dx + dy * dy);
2315 if (r > 20.0f) {
2316 struct weston_matrix inverse;
2317
2318 weston_matrix_init(&inverse);
2319 inverse.d[0] = dx / r;
2320 inverse.d[4] = dy / r;
2321 inverse.d[1] = -inverse.d[4];
2322 inverse.d[5] = inverse.d[0];
2323 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002324
2325 weston_matrix_init(&rotate->rotation);
2326 rotate->rotation.d[0] = dx / r;
2327 rotate->rotation.d[4] = -dy / r;
2328 rotate->rotation.d[1] = -rotate->rotation.d[4];
2329 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002330 } else {
2331 weston_matrix_init(&surface->rotation.rotation);
2332 weston_matrix_init(&rotate->rotation);
2333 }
2334
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002335 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2336 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002337}
2338
2339static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002340lower_fullscreen_layer(struct desktop_shell *shell)
2341{
2342 struct workspace *ws;
2343 struct weston_surface *surface, *prev;
2344
2345 ws = get_current_workspace(shell);
2346 wl_list_for_each_reverse_safe(surface, prev,
2347 &shell->fullscreen_layer.surface_list,
2348 layer_link)
2349 weston_surface_restack(surface, &ws->layer.surface_list);
2350}
2351
2352static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002353activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002354 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002355{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002356 struct workspace *ws = get_current_workspace(shell);
2357 struct focus_state *state;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002358
Daniel Stone37816df2012-05-16 18:45:18 +01002359 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002360
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002361 wl_list_for_each(state, &ws->focus_list, link)
2362 if (state->seat == seat)
2363 break;
2364
2365 if (&state->link == &ws->focus_list) {
2366 state = focus_state_create(seat, ws);
2367 if (state == NULL)
2368 return;
2369 }
2370
2371 state->keyboard_focus = es;
2372 wl_list_remove(&state->surface_destroy_listener.link);
2373 wl_signal_add(&es->surface.resource.destroy_signal,
2374 &state->surface_destroy_listener);
2375
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002376 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002377 case SHELL_SURFACE_FULLSCREEN:
2378 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002379 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002380 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002381 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002382 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002383 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002384 lower_fullscreen_layer(shell);
2385 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002386 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002387 }
2388}
2389
Alex Wu21858432012-04-01 20:13:08 +08002390/* no-op func for checking black surface */
2391static void
2392black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2393{
2394}
2395
2396static bool
2397is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2398{
2399 if (es->configure == black_surface_configure) {
2400 if (fs_surface)
2401 *fs_surface = (struct weston_surface *)es->private;
2402 return true;
2403 }
2404 return false;
2405}
2406
Kristian Høgsberg75840622011-09-06 13:48:16 -04002407static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002408click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002409 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002410{
Daniel Stone37816df2012-05-16 18:45:18 +01002411 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002412 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002413 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002414 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002415
Daniel Stone37816df2012-05-16 18:45:18 +01002416 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002417 if (!focus)
2418 return;
2419
Alex Wu21858432012-04-01 20:13:08 +08002420 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002421 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002422
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002423 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2424 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002425
Daniel Stone325fc2d2012-05-30 16:31:58 +01002426 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002427 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002428}
2429
2430static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002431lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002432{
Tiago Vignattibe143262012-04-16 17:31:41 +03002433 struct desktop_shell *shell =
2434 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002435 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002436 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002437
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002438 if (shell->locked) {
2439 wl_list_for_each(output, &shell->compositor->output_list, link)
2440 /* TODO: find a way to jump to other DPMS levels */
2441 if (output->set_dpms)
2442 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002443 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002444 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002445
2446 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002447
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002448 /* Hide all surfaces by removing the fullscreen, panel and
2449 * toplevel layers. This way nothing else can show or receive
2450 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002451
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002452 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002453 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002454 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002455 wl_list_insert(&shell->compositor->cursor_layer.link,
2456 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002457
Pekka Paalanen77346a62011-11-30 16:26:35 +02002458 launch_screensaver(shell);
2459
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002460 /* TODO: disable bindings that should not work while locked. */
2461
2462 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002463}
2464
2465static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002466unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002467{
Tiago Vignattibe143262012-04-16 17:31:41 +03002468 struct desktop_shell *shell =
2469 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002470
Pekka Paalanend81c2162011-11-16 13:47:34 +02002471 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002472 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002473 return;
2474 }
2475
2476 /* If desktop-shell client has gone away, unlock immediately. */
2477 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002478 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002479 return;
2480 }
2481
2482 if (shell->prepare_event_sent)
2483 return;
2484
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002485 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002486 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002487}
2488
2489static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002490show_input_panels(struct wl_listener *listener, void *data)
2491{
2492 struct desktop_shell *shell =
2493 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002494 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002495
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002496 wl_list_for_each_safe(surface, next,
2497 &shell->input_panel.surfaces, layer_link) {
2498 wl_list_remove(&surface->layer_link);
2499 wl_list_insert(&shell->panel_layer.surface_list,
2500 &surface->layer_link);
2501 weston_surface_assign_output(surface);
2502 weston_surface_damage(surface);
2503 weston_slide_run(surface,
2504 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002505 }
2506}
2507
2508static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002509input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2510
2511static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002512hide_input_panels(struct wl_listener *listener, void *data)
2513{
2514 struct desktop_shell *shell =
2515 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002516 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002517
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002518 wl_list_for_each_safe(surface, next,
2519 &shell->panel_layer.surface_list, layer_link)
2520 if (surface->configure == input_panel_configure) {
2521 weston_surface_unmap(surface);
2522 wl_list_insert(&shell->input_panel.surfaces,
2523 &surface->layer_link);
2524 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002525}
2526
2527static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002528center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002529{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002530 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002531 GLfloat x = (mode->width - surface->buffer->width) / 2;
2532 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002533
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002534 weston_surface_configure(surface, output->x + x, output->y + y,
2535 surface->buffer->width,
2536 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002537}
2538
2539static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002540map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002541 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002542{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002543 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002544 struct shell_surface *shsurf = get_shell_surface(surface);
2545 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002546 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002547 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002548 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002549 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002550
Pekka Paalanen60921e52012-01-25 15:55:43 +02002551 surface->geometry.width = width;
2552 surface->geometry.height = height;
2553 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002554
2555 /* initial positioning, see also configure() */
2556 switch (surface_type) {
2557 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002558 weston_surface_set_position(surface, 10 + random() % 400,
2559 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002560 break;
Alex Wu4539b082012-03-01 12:57:46 +08002561 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002562 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002563 shell_map_fullscreen(shsurf);
2564 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002565 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002566 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002567 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002568 weston_surface_set_position(surface, shsurf->output->x,
2569 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002570 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002571 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002572 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002573 case SHELL_SURFACE_NONE:
2574 weston_surface_set_position(surface,
2575 surface->geometry.x + sx,
2576 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002577 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002578 default:
2579 ;
2580 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002581
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002582 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002583 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002584 case SHELL_SURFACE_POPUP:
2585 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002586 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002587 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2588 break;
Alex Wu4539b082012-03-01 12:57:46 +08002589 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002590 case SHELL_SURFACE_NONE:
2591 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002592 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002593 ws = get_current_workspace(shell);
2594 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002595 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002596 }
2597
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002598 if (surface_type != SHELL_SURFACE_NONE) {
2599 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002600 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2601 surface->output = shsurf->output;
2602 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002603
Juan Zhao7bb92f02011-12-15 11:31:51 -05002604 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002605 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002606 if (shsurf->transient.flags ==
2607 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2608 break;
2609 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002610 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002611 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002612 if (!shell->locked) {
2613 wl_list_for_each(seat, &compositor->seat_list, link)
2614 activate(shell, surface, seat);
2615 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002616 break;
2617 default:
2618 break;
2619 }
2620
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002621 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002622 {
2623 switch (shell->win_animation_type) {
2624 case ANIMATION_FADE:
2625 weston_fade_run(surface, NULL, NULL);
2626 break;
2627 case ANIMATION_ZOOM:
2628 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2629 break;
2630 default:
2631 break;
2632 }
2633 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002634}
2635
2636static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002637configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002638 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002639{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002640 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2641 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002642
Pekka Paalanen77346a62011-11-30 16:26:35 +02002643 shsurf = get_shell_surface(surface);
2644 if (shsurf)
2645 surface_type = shsurf->type;
2646
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002647 surface->geometry.x = x;
2648 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002649 surface->geometry.width = width;
2650 surface->geometry.height = height;
2651 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002652
2653 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002654 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002655 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002656 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002657 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002658 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002659 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002660 surface->geometry.x = surface->output->x;
2661 surface->geometry.y = surface->output->y +
2662 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002663 break;
Alex Wu4539b082012-03-01 12:57:46 +08002664 case SHELL_SURFACE_TOPLEVEL:
2665 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002666 default:
2667 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002668 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002669
Alex Wu4539b082012-03-01 12:57:46 +08002670 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002671 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002672 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002673
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002674 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002675 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002676 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002677}
2678
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002679static void
2680shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2681{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002682 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002683 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002684 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002685
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002686 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002687 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002688 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002689 type_changed = 1;
2690 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002691
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002692 if (!weston_surface_is_mapped(es)) {
2693 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002694 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002695 es->geometry.width != es->buffer->width ||
2696 es->geometry.height != es->buffer->height) {
2697 GLfloat from_x, from_y;
2698 GLfloat to_x, to_y;
2699
2700 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2701 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2702 configure(shell, es,
2703 es->geometry.x + to_x - from_x,
2704 es->geometry.y + to_y - from_y,
2705 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002706 }
2707}
2708
Tiago Vignattibe143262012-04-16 17:31:41 +03002709static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002710
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002711static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002712desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002713{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002714 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002715 struct desktop_shell *shell =
2716 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002717
2718 shell->child.process.pid = 0;
2719 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002720
2721 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2722 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002723 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002724 shell->child.deathstamp = time;
2725 shell->child.deathcount = 0;
2726 }
2727
2728 shell->child.deathcount++;
2729 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002730 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002731 return;
2732 }
2733
Martin Minarik6d118362012-06-07 18:01:59 +02002734 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002735 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002736}
2737
2738static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002739launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002740{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002741 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002742
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002743 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002744 &shell->child.process,
2745 shell_exe,
2746 desktop_shell_sigchld);
2747
2748 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002749 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002750 return 0;
2751}
2752
2753static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002754bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2755{
Tiago Vignattibe143262012-04-16 17:31:41 +03002756 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002757
2758 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002759 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002760}
2761
Kristian Høgsberg75840622011-09-06 13:48:16 -04002762static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002763unbind_desktop_shell(struct wl_resource *resource)
2764{
Tiago Vignattibe143262012-04-16 17:31:41 +03002765 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002766
2767 if (shell->locked)
2768 resume_desktop(shell);
2769
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002770 shell->child.desktop_shell = NULL;
2771 shell->prepare_event_sent = false;
2772 free(resource);
2773}
2774
2775static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002776bind_desktop_shell(struct wl_client *client,
2777 void *data, uint32_t version, uint32_t id)
2778{
Tiago Vignattibe143262012-04-16 17:31:41 +03002779 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002780 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002781
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002782 resource = wl_client_add_object(client, &desktop_shell_interface,
2783 &desktop_shell_implementation,
2784 id, shell);
2785
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002786 if (client == shell->child.client) {
2787 resource->destroy = unbind_desktop_shell;
2788 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002789 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002790 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002791
2792 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2793 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002794 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002795}
2796
Pekka Paalanen6e168112011-11-24 11:34:05 +02002797static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002798screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2799{
2800 struct desktop_shell *shell = surface->private;
2801
2802 if (!shell->locked)
2803 return;
2804
2805 center_on_output(surface, surface->output);
2806
2807 if (wl_list_empty(&surface->layer_link)) {
2808 wl_list_insert(shell->lock_layer.surface_list.prev,
2809 &surface->layer_link);
2810 weston_surface_assign_output(surface);
2811 shell->compositor->idle_time = shell->screensaver.duration;
2812 weston_compositor_wake(shell->compositor);
2813 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2814 }
2815}
2816
2817static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002818screensaver_set_surface(struct wl_client *client,
2819 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002820 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002821 struct wl_resource *output_resource)
2822{
Tiago Vignattibe143262012-04-16 17:31:41 +03002823 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002824 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002825 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002826
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002827 surface->configure = screensaver_configure;
2828 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002829 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002830}
2831
2832static const struct screensaver_interface screensaver_implementation = {
2833 screensaver_set_surface
2834};
2835
2836static void
2837unbind_screensaver(struct wl_resource *resource)
2838{
Tiago Vignattibe143262012-04-16 17:31:41 +03002839 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002840
Pekka Paalanen77346a62011-11-30 16:26:35 +02002841 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002842 free(resource);
2843}
2844
2845static void
2846bind_screensaver(struct wl_client *client,
2847 void *data, uint32_t version, uint32_t id)
2848{
Tiago Vignattibe143262012-04-16 17:31:41 +03002849 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002850 struct wl_resource *resource;
2851
2852 resource = wl_client_add_object(client, &screensaver_interface,
2853 &screensaver_implementation,
2854 id, shell);
2855
Pekka Paalanen77346a62011-11-30 16:26:35 +02002856 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002857 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002858 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002859 return;
2860 }
2861
2862 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2863 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002864 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002865}
2866
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002867static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002868input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2869{
2870 struct weston_mode *mode = surface->output->current;
2871 GLfloat x = (mode->width - surface->buffer->width) / 2;
2872 GLfloat y = mode->height - surface->buffer->height;
2873
2874 /* Don't map the input panel here, wait for
2875 * show_input_panels signal. */
2876
2877 weston_surface_configure(surface,
2878 surface->output->x + x,
2879 surface->output->y + y,
2880 surface->buffer->width,
2881 surface->buffer->height);
2882}
2883
2884static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002885input_panel_set_surface(struct wl_client *client,
2886 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002887 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002888 struct wl_resource *output_resource)
2889{
2890 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002891 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002892 struct weston_output *output = output_resource->data;
2893
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002894 surface->configure = input_panel_configure;
2895 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002896 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002897 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002898}
2899
2900static const struct input_panel_interface input_panel_implementation = {
2901 input_panel_set_surface
2902};
2903
2904static void
2905unbind_input_panel(struct wl_resource *resource)
2906{
2907 struct desktop_shell *shell = resource->data;
2908
2909 shell->input_panel.binding = NULL;
2910 free(resource);
2911}
2912
2913static void
2914bind_input_panel(struct wl_client *client,
2915 void *data, uint32_t version, uint32_t id)
2916{
2917 struct desktop_shell *shell = data;
2918 struct wl_resource *resource;
2919
2920 resource = wl_client_add_object(client, &input_panel_interface,
2921 &input_panel_implementation,
2922 id, shell);
2923
2924 if (shell->input_panel.binding == NULL) {
2925 resource->destroy = unbind_input_panel;
2926 shell->input_panel.binding = resource;
2927 return;
2928 }
2929
2930 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2931 "interface object already bound");
2932 wl_resource_destroy(resource);
2933}
2934
Kristian Høgsberg07045392012-02-19 18:52:44 -05002935struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002936 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002937 struct weston_surface *current;
2938 struct wl_listener listener;
2939 struct wl_keyboard_grab grab;
2940};
2941
2942static void
2943switcher_next(struct switcher *switcher)
2944{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002945 struct weston_surface *surface;
2946 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002947 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002948 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002949
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002950 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002951 switch (get_shell_surface_type(surface)) {
2952 case SHELL_SURFACE_TOPLEVEL:
2953 case SHELL_SURFACE_FULLSCREEN:
2954 case SHELL_SURFACE_MAXIMIZED:
2955 if (first == NULL)
2956 first = surface;
2957 if (prev == switcher->current)
2958 next = surface;
2959 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002960 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002961 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002962 weston_surface_damage(surface);
2963 break;
2964 default:
2965 break;
2966 }
Alex Wu1659daa2012-04-01 20:13:09 +08002967
2968 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002969 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002970 surface->geometry.dirty = 1;
2971 weston_surface_damage(surface);
2972 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002973 }
2974
2975 if (next == NULL)
2976 next = first;
2977
Alex Wu07b26062012-03-12 16:06:01 +08002978 if (next == NULL)
2979 return;
2980
Kristian Høgsberg07045392012-02-19 18:52:44 -05002981 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002982 wl_signal_add(&next->surface.resource.destroy_signal,
2983 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002984
2985 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002986 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002987
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002988 shsurf = get_shell_surface(switcher->current);
2989 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002990 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002991}
2992
2993static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002994switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002995{
2996 struct switcher *switcher =
2997 container_of(listener, struct switcher, listener);
2998
2999 switcher_next(switcher);
3000}
3001
3002static void
Daniel Stone351eb612012-05-31 15:27:47 -04003003switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003004{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003005 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003006 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003007 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003008
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003009 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003010 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003011 weston_surface_damage(surface);
3012 }
3013
Alex Wu07b26062012-03-12 16:06:01 +08003014 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003015 activate(switcher->shell, switcher->current,
3016 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003017 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003018 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003019 free(switcher);
3020}
3021
3022static void
3023switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003024 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003025{
3026 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003027 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003028
Daniel Stonec9785ea2012-05-30 16:31:52 +01003029 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003030 switcher_next(switcher);
3031}
3032
3033static void
3034switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3035 uint32_t mods_depressed, uint32_t mods_latched,
3036 uint32_t mods_locked, uint32_t group)
3037{
3038 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003039 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003040
Daniel Stone351eb612012-05-31 15:27:47 -04003041 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3042 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003043}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003044
3045static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003046 switcher_key,
3047 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003048};
3049
3050static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003051switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3052 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003053{
Tiago Vignattibe143262012-04-16 17:31:41 +03003054 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003055 struct switcher *switcher;
3056
3057 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003058 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003059 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003060 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003061 wl_list_init(&switcher->listener.link);
3062
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003063 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003064 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003065 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3066 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003067 switcher_next(switcher);
3068}
3069
Pekka Paalanen3c647232011-12-22 13:43:43 +02003070static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003071backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3072 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003073{
3074 struct weston_compositor *compositor = data;
3075 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003076 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003077
3078 /* TODO: we're limiting to simple use cases, where we assume just
3079 * control on the primary display. We'd have to extend later if we
3080 * ever get support for setting backlights on random desktop LCD
3081 * panels though */
3082 output = get_default_output(compositor);
3083 if (!output)
3084 return;
3085
3086 if (!output->set_backlight)
3087 return;
3088
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003089 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3090 backlight_new = output->backlight_current - 25;
3091 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3092 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003093
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003094 if (backlight_new < 5)
3095 backlight_new = 5;
3096 if (backlight_new > 255)
3097 backlight_new = 255;
3098
3099 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003100 output->set_backlight(output, output->backlight_current);
3101}
3102
3103static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003104debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3105 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003106{
Tiago Vignattibe143262012-04-16 17:31:41 +03003107 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003108 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003109 struct weston_surface *surface;
3110
3111 if (shell->debug_repaint_surface) {
3112 weston_surface_destroy(shell->debug_repaint_surface);
3113 shell->debug_repaint_surface = NULL;
3114 } else {
3115 surface = weston_surface_create(compositor);
3116 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3117 weston_surface_configure(surface, 0, 0, 8192, 8192);
3118 wl_list_insert(&compositor->fade_layer.surface_list,
3119 &surface->layer_link);
3120 weston_surface_assign_output(surface);
3121 pixman_region32_init(&surface->input);
3122
3123 /* Here's the dirty little trick that makes the
3124 * repaint debugging work: we force an
3125 * update_transform first to update dependent state
3126 * and clear the geometry.dirty bit. Then we clear
3127 * the surface damage so it only gets repainted
3128 * piecewise as we repaint other things. */
3129
3130 weston_surface_update_transform(surface);
3131 pixman_region32_fini(&surface->damage);
3132 pixman_region32_init(&surface->damage);
3133 shell->debug_repaint_surface = surface;
3134 }
3135}
3136
3137static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003138force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3139 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003140{
3141 struct wl_client *client;
3142 pid_t pid;
3143 uid_t uid;
3144 gid_t gid;
3145
Daniel Stone325fc2d2012-05-30 16:31:58 +01003146 client = seat->keyboard->focus->resource.client;
3147 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003148
Daniel Stone325fc2d2012-05-30 16:31:58 +01003149 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003150}
3151
3152static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003153workspace_up_binding(struct wl_seat *seat, uint32_t time,
3154 uint32_t key, void *data)
3155{
3156 struct desktop_shell *shell = data;
3157 unsigned int new_index = shell->workspaces.current;
3158
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003159 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003160 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003161 if (new_index != 0)
3162 new_index--;
3163
3164 change_workspace(shell, new_index);
3165}
3166
3167static void
3168workspace_down_binding(struct wl_seat *seat, uint32_t time,
3169 uint32_t key, void *data)
3170{
3171 struct desktop_shell *shell = data;
3172 unsigned int new_index = shell->workspaces.current;
3173
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003174 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003175 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003176 if (new_index < shell->workspaces.num - 1)
3177 new_index++;
3178
3179 change_workspace(shell, new_index);
3180}
3181
3182static void
3183workspace_f_binding(struct wl_seat *seat, uint32_t time,
3184 uint32_t key, void *data)
3185{
3186 struct desktop_shell *shell = data;
3187 unsigned int new_index;
3188
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003189 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003190 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003191 new_index = key - KEY_F1;
3192 if (new_index >= shell->workspaces.num)
3193 new_index = shell->workspaces.num - 1;
3194
3195 change_workspace(shell, new_index);
3196}
3197
3198
3199static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003200shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003201{
Tiago Vignattibe143262012-04-16 17:31:41 +03003202 struct desktop_shell *shell =
3203 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003204 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003205
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003206 if (shell->child.client)
3207 wl_client_destroy(shell->child.client);
3208
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003209 wl_list_remove(&shell->lock_listener.link);
3210 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003211 wl_list_remove(&shell->show_input_panel_listener.link);
3212 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003213
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003214 wl_array_for_each(ws, &shell->workspaces.array)
3215 workspace_destroy(*ws);
3216 wl_array_release(&shell->workspaces.array);
3217
Pekka Paalanen3c647232011-12-22 13:43:43 +02003218 free(shell->screensaver.path);
3219 free(shell);
3220}
3221
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003222static void
3223shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3224{
3225 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003226 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003227
3228 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003229 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3230 MODIFIER_CTRL | MODIFIER_ALT,
3231 terminate_binding, ec);
3232 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3233 click_to_activate_binding,
3234 shell);
3235 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3236 MODIFIER_SUPER | MODIFIER_ALT,
3237 surface_opacity_binding, NULL);
3238 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3239 MODIFIER_SUPER, zoom_axis_binding,
3240 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003241
3242 /* configurable bindings */
3243 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003244 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3245 zoom_key_binding, NULL);
3246 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3247 zoom_key_binding, NULL);
3248 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3249 shell);
3250 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3251 resize_binding, shell);
3252 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3253 rotate_binding, NULL);
3254 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3255 shell);
3256 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3257 ec);
3258 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3259 backlight_binding, ec);
3260 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3261 ec);
3262 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3263 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003264 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003265 debug_repaint_binding, shell);
3266 weston_compositor_add_key_binding(ec, KEY_K, mod,
3267 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003268 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3269 workspace_up_binding, shell);
3270 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3271 workspace_down_binding, shell);
3272
3273 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3274 if (shell->workspaces.num > 1) {
3275 num_workspace_bindings = shell->workspaces.num;
3276 if (num_workspace_bindings > 6)
3277 num_workspace_bindings = 6;
3278 for (i = 0; i < num_workspace_bindings; i++)
3279 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3280 workspace_f_binding,
3281 shell);
3282 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003283}
3284
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003285int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003286shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003287
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003288WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003289shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003290{
Tiago Vignattibe143262012-04-16 17:31:41 +03003291 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003292 struct workspace **pws;
3293 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003294
3295 shell = malloc(sizeof *shell);
3296 if (shell == NULL)
3297 return -1;
3298
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003299 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003300 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003301
3302 shell->destroy_listener.notify = shell_destroy;
3303 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3304 shell->lock_listener.notify = lock;
3305 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3306 shell->unlock_listener.notify = unlock;
3307 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003308 shell->show_input_panel_listener.notify = show_input_panels;
3309 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3310 shell->hide_input_panel_listener.notify = hide_input_panels;
3311 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003312 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003313 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003314 ec->shell_interface.create_shell_surface = create_shell_surface;
3315 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003316 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003317 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003318 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003319
Pekka Paalanen77346a62011-11-30 16:26:35 +02003320 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003321 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003322
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003323 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3324 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003325 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3326 weston_layer_init(&shell->lock_layer, NULL);
3327
3328 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003329
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003330 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003331
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003332 for (i = 0; i < shell->workspaces.num; i++) {
3333 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3334 if (pws == NULL)
3335 return -1;
3336
3337 *pws = workspace_create();
3338 if (*pws == NULL)
3339 return -1;
3340 }
3341 activate_workspace(shell, 0);
3342
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003343 wl_list_init(&shell->workspaces.animation.link);
3344 shell->workspaces.animation.frame = animate_workspace_change_frame;
3345
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003346 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3347 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003348 return -1;
3349
Kristian Høgsberg75840622011-09-06 13:48:16 -04003350 if (wl_display_add_global(ec->wl_display,
3351 &desktop_shell_interface,
3352 shell, bind_desktop_shell) == NULL)
3353 return -1;
3354
Pekka Paalanen6e168112011-11-24 11:34:05 +02003355 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3356 shell, bind_screensaver) == NULL)
3357 return -1;
3358
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003359 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3360 shell, bind_input_panel) == NULL)
3361 return -1;
3362
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003363 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003364 if (launch_desktop_shell_process(shell) != 0)
3365 return -1;
3366
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003367 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003368 if (ec->seat->seat.pointer)
3369 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3370 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003371
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003372 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003373
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003374 return 0;
3375}