blob: 6c810ff99631241ae57b153b6bca32a8ee8dcd67 [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
Kristian Høgsbergbbe98392012-08-01 00:20:21 -0400996busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400997 uint32_t time, uint32_t button, uint32_t state)
998{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -0400999 struct shell_grab *grab = (struct shell_grab *) base;
1000 struct shell_surface *shsurf;
1001 struct weston_surface *surface =
1002 (struct weston_surface *) grab->grab.pointer->current;
1003 struct weston_seat *seat =
1004 (struct weston_seat *) grab->grab.pointer->seat;
1005
1006 shsurf = get_shell_surface(surface);
1007 if (shsurf && button == BTN_LEFT && state) {
1008 activate(shsurf->shell, shsurf->surface, seat);
1009 surface_move(shsurf, seat);
1010 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001011}
1012
1013static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1014 busy_cursor_grab_focus,
1015 busy_cursor_grab_motion,
1016 busy_cursor_grab_button,
1017};
1018
1019static void
1020set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1021{
1022 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001023
1024 grab = malloc(sizeof *grab);
1025 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001026 return;
1027
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001028 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1029 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001030}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001031
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001032static void
1033end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1034{
1035 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1036
1037 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001038 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001039 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001040 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001041}
1042
Scott Moreau9521d5e2012-04-19 13:06:17 -06001043static void
1044ping_timer_destroy(struct shell_surface *shsurf)
1045{
1046 if (!shsurf || !shsurf->ping_timer)
1047 return;
1048
1049 if (shsurf->ping_timer->source)
1050 wl_event_source_remove(shsurf->ping_timer->source);
1051
1052 free(shsurf->ping_timer);
1053 shsurf->ping_timer = NULL;
1054}
1055
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001056static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001057ping_timeout_handler(void *data)
1058{
1059 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001060 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001061
Scott Moreau9521d5e2012-04-19 13:06:17 -06001062 /* Client is not responding */
1063 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001064
1065 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1066 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1067 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001068
1069 return 1;
1070}
1071
1072static void
1073ping_handler(struct weston_surface *surface, uint32_t serial)
1074{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001075 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001076 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001077 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001078
1079 if (!shsurf)
1080 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001081 if (!shsurf->resource.client)
1082 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001083
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001084 if (shsurf->surface == shsurf->shell->grab_surface)
1085 return;
1086
Scott Moreauff1db4a2012-04-17 19:06:18 -06001087 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001088 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001089 if (!shsurf->ping_timer)
1090 return;
1091
1092 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001093 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1094 shsurf->ping_timer->source =
1095 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1096 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1097
1098 wl_shell_surface_send_ping(&shsurf->resource, serial);
1099 }
1100}
1101
1102static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001103handle_pointer_focus(struct wl_listener *listener, void *data)
1104{
1105 struct wl_pointer *pointer = data;
1106 struct weston_surface *surface =
1107 (struct weston_surface *) pointer->focus;
1108 struct weston_compositor *compositor;
1109 struct shell_surface *shsurf;
1110 uint32_t serial;
1111
1112 if (!surface)
1113 return;
1114
1115 compositor = surface->compositor;
1116 shsurf = get_shell_surface(surface);
1117
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001118 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001119 set_busy_cursor(shsurf, pointer);
1120 } else {
1121 serial = wl_display_next_serial(compositor->wl_display);
1122 ping_handler(surface, serial);
1123 }
1124}
1125
1126static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001127shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1128 uint32_t serial)
1129{
1130 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001131 struct desktop_shell *shell = shsurf->shell;
1132 struct weston_seat *seat;
1133 struct weston_compositor *ec = shsurf->surface->compositor;
1134 struct wl_pointer *pointer;
1135 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001136
Scott Moreauff1db4a2012-04-17 19:06:18 -06001137 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001138 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001139 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001140 if (was_unresponsive) {
1141 /* Received pong from previously unresponsive client */
1142 wl_list_for_each(seat, &ec->seat_list, link) {
1143 pointer = seat->seat.pointer;
1144 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001145 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001146 pointer->current ==
1147 &shsurf->surface->surface)
1148 end_busy_cursor(shsurf, pointer);
1149 }
1150 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001151 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001152 }
1153}
1154
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001155static void
1156shell_surface_set_title(struct wl_client *client,
1157 struct wl_resource *resource, const char *title)
1158{
1159 struct shell_surface *shsurf = resource->data;
1160
1161 free(shsurf->title);
1162 shsurf->title = strdup(title);
1163}
1164
1165static void
1166shell_surface_set_class(struct wl_client *client,
1167 struct wl_resource *resource, const char *class)
1168{
1169 struct shell_surface *shsurf = resource->data;
1170
1171 free(shsurf->class);
1172 shsurf->class = strdup(class);
1173}
1174
Juan Zhao96879df2012-02-07 08:45:41 +08001175static struct weston_output *
1176get_default_output(struct weston_compositor *compositor)
1177{
1178 return container_of(compositor->output_list.next,
1179 struct weston_output, link);
1180}
1181
Alex Wu4539b082012-03-01 12:57:46 +08001182static void
1183shell_unset_fullscreen(struct shell_surface *shsurf)
1184{
1185 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001186 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1187 shell_surface_is_top_fullscreen(shsurf)) {
1188 weston_output_switch_mode(shsurf->fullscreen_output,
1189 shsurf->fullscreen_output->origin);
1190 }
Alex Wu4539b082012-03-01 12:57:46 +08001191 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1192 shsurf->fullscreen.framerate = 0;
1193 wl_list_remove(&shsurf->fullscreen.transform.link);
1194 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001195 if (shsurf->fullscreen.black_surface)
1196 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001197 shsurf->fullscreen.black_surface = NULL;
1198 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001199 weston_surface_set_position(shsurf->surface,
1200 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001201 if (shsurf->saved_rotation_valid) {
1202 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1203 &shsurf->rotation.transform.link);
1204 shsurf->saved_rotation_valid = false;
1205 }
Alex Wu4539b082012-03-01 12:57:46 +08001206}
1207
Pekka Paalanen98262232011-12-01 10:42:22 +02001208static int
1209reset_shell_surface_type(struct shell_surface *surface)
1210{
1211 switch (surface->type) {
1212 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001213 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001214 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001215 case SHELL_SURFACE_MAXIMIZED:
1216 surface->output = get_default_output(surface->surface->compositor);
1217 weston_surface_set_position(surface->surface,
1218 surface->saved_x,
1219 surface->saved_y);
1220 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001221 case SHELL_SURFACE_NONE:
1222 case SHELL_SURFACE_TOPLEVEL:
1223 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001224 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001225 break;
1226 }
1227
1228 surface->type = SHELL_SURFACE_NONE;
1229 return 0;
1230}
1231
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001232static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001233set_surface_type(struct shell_surface *shsurf)
1234{
1235 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001236 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001237
1238 reset_shell_surface_type(shsurf);
1239
1240 shsurf->type = shsurf->next_type;
1241 shsurf->next_type = SHELL_SURFACE_NONE;
1242
1243 switch (shsurf->type) {
1244 case SHELL_SURFACE_TOPLEVEL:
1245 break;
1246 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001247 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001248 pes->geometry.x + shsurf->transient.x,
1249 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001250 break;
1251
1252 case SHELL_SURFACE_MAXIMIZED:
1253 shsurf->saved_x = surface->geometry.x;
1254 shsurf->saved_y = surface->geometry.y;
1255 shsurf->saved_position_valid = true;
1256 break;
1257
1258 case SHELL_SURFACE_FULLSCREEN:
1259 shsurf->saved_x = surface->geometry.x;
1260 shsurf->saved_y = surface->geometry.y;
1261 shsurf->saved_position_valid = true;
1262
1263 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1264 wl_list_remove(&shsurf->rotation.transform.link);
1265 wl_list_init(&shsurf->rotation.transform.link);
1266 shsurf->surface->geometry.dirty = 1;
1267 shsurf->saved_rotation_valid = true;
1268 }
1269 break;
1270
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001271 default:
1272 break;
1273 }
1274}
1275
1276static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001277set_toplevel(struct shell_surface *shsurf)
1278{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001279 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001280}
1281
1282static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001283shell_surface_set_toplevel(struct wl_client *client,
1284 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001285{
Pekka Paalanen98262232011-12-01 10:42:22 +02001286 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001287
Tiago Vignattibc052c92012-04-19 16:18:18 +03001288 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001289}
1290
1291static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001292set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001293 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001294{
1295 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001296 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001297 shsurf->transient.x = x;
1298 shsurf->transient.y = y;
1299 shsurf->transient.flags = flags;
1300 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1301}
1302
1303static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001304shell_surface_set_transient(struct wl_client *client,
1305 struct wl_resource *resource,
1306 struct wl_resource *parent_resource,
1307 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001308{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001309 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001310 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001311
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001312 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001313}
1314
Tiago Vignattibe143262012-04-16 17:31:41 +03001315static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001316shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001317{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001318 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001319}
1320
1321static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001322get_output_panel_height(struct desktop_shell *shell,
1323 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001324{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001325 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001326 int panel_height = 0;
1327
1328 if (!output)
1329 return 0;
1330
Juan Zhao4ab94682012-07-09 22:24:09 -07001331 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001332 if (surface->output == output) {
1333 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001334 break;
1335 }
1336 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001337
Juan Zhao96879df2012-02-07 08:45:41 +08001338 return panel_height;
1339}
1340
1341static void
1342shell_surface_set_maximized(struct wl_client *client,
1343 struct wl_resource *resource,
1344 struct wl_resource *output_resource )
1345{
1346 struct shell_surface *shsurf = resource->data;
1347 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001348 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001349 uint32_t edges = 0, panel_height = 0;
1350
1351 /* get the default output, if the client set it as NULL
1352 check whether the ouput is available */
1353 if (output_resource)
1354 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001355 else if (es->output)
1356 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001357 else
1358 shsurf->output = get_default_output(es->compositor);
1359
Tiago Vignattibe143262012-04-16 17:31:41 +03001360 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001361 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001362 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001363
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001364 shsurf->client->send_configure(shsurf->surface, edges,
Rob Bradford31b68622012-07-02 19:00:19 +01001365 shsurf->output->current->width,
1366 shsurf->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001367
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001368 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001369}
1370
Alex Wu21858432012-04-01 20:13:08 +08001371static void
1372black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1373
Alex Wu4539b082012-03-01 12:57:46 +08001374static struct weston_surface *
1375create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001376 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001377 GLfloat x, GLfloat y, int w, int h)
1378{
1379 struct weston_surface *surface = NULL;
1380
1381 surface = weston_surface_create(ec);
1382 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001383 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001384 return NULL;
1385 }
1386
Alex Wu21858432012-04-01 20:13:08 +08001387 surface->configure = black_surface_configure;
1388 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001389 weston_surface_configure(surface, x, y, w, h);
1390 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001391 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1392
Alex Wu4539b082012-03-01 12:57:46 +08001393 return surface;
1394}
1395
1396/* Create black surface and append it to the associated fullscreen surface.
1397 * Handle size dismatch and positioning according to the method. */
1398static void
1399shell_configure_fullscreen(struct shell_surface *shsurf)
1400{
1401 struct weston_output *output = shsurf->fullscreen_output;
1402 struct weston_surface *surface = shsurf->surface;
1403 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001404 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001405
Alex Wu4539b082012-03-01 12:57:46 +08001406 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001407 shsurf->fullscreen.black_surface =
1408 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001409 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001410 output->x, output->y,
1411 output->current->width,
1412 output->current->height);
1413
1414 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1415 wl_list_insert(&surface->layer_link,
1416 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001417 shsurf->fullscreen.black_surface->output = output;
1418
1419 switch (shsurf->fullscreen.type) {
1420 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001421 if (surface->buffer)
1422 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001423 break;
1424 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1425 matrix = &shsurf->fullscreen.transform.matrix;
1426 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001427
1428 output_aspect = (float) output->current->width /
1429 (float) output->current->height;
1430 surface_aspect = (float) surface->geometry.width /
1431 (float) surface->geometry.height;
1432 if (output_aspect < surface_aspect)
1433 scale = (float) output->current->width /
1434 (float) surface->geometry.width;
1435 else
1436 scale = (float) output->current->height /
1437 (float) surface->geometry.height;
1438
Alex Wu4539b082012-03-01 12:57:46 +08001439 weston_matrix_scale(matrix, scale, scale, 1);
1440 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001441 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001442 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001443 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1444 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1445 weston_surface_set_position(surface, x, y);
1446
Alex Wu4539b082012-03-01 12:57:46 +08001447 break;
1448 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001449 if (shell_surface_is_top_fullscreen(shsurf)) {
1450 struct weston_mode mode = {0,
1451 surface->geometry.width,
1452 surface->geometry.height,
1453 shsurf->fullscreen.framerate};
1454
1455 if (weston_output_switch_mode(output, &mode) == 0) {
1456 weston_surface_configure(shsurf->fullscreen.black_surface,
1457 output->x, output->y,
1458 output->current->width,
1459 output->current->height);
1460 weston_surface_set_position(surface, output->x, output->y);
1461 break;
1462 }
1463 }
Alex Wu4539b082012-03-01 12:57:46 +08001464 break;
1465 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1466 break;
1467 default:
1468 break;
1469 }
1470}
1471
1472/* make the fullscreen and black surface at the top */
1473static void
1474shell_stack_fullscreen(struct shell_surface *shsurf)
1475{
Alex Wubd3354b2012-04-17 17:20:49 +08001476 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001477 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001478 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001479
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001480 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001481 wl_list_insert(&shell->fullscreen_layer.surface_list,
1482 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001483 weston_surface_damage(surface);
1484
1485 if (!shsurf->fullscreen.black_surface)
1486 shsurf->fullscreen.black_surface =
1487 create_black_surface(surface->compositor,
1488 surface,
1489 output->x, output->y,
1490 output->current->width,
1491 output->current->height);
1492
1493 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001494 wl_list_insert(&surface->layer_link,
1495 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001496 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001497}
1498
1499static void
1500shell_map_fullscreen(struct shell_surface *shsurf)
1501{
Alex Wu4539b082012-03-01 12:57:46 +08001502 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001503 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001504}
1505
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001506static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001507shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001508 struct wl_resource *resource,
1509 uint32_t method,
1510 uint32_t framerate,
1511 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001512{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001513 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001514 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001515
1516 if (output_resource)
1517 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001518 else if (es->output)
1519 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001520 else
1521 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001522
Alex Wu4539b082012-03-01 12:57:46 +08001523 shsurf->fullscreen_output = shsurf->output;
1524 shsurf->fullscreen.type = method;
1525 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001526 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001527
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001528 shsurf->client->send_configure(shsurf->surface, 0,
1529 shsurf->output->current->width,
1530 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001531}
1532
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001533static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001534popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001535 struct wl_surface *surface,
1536 wl_fixed_t x,
1537 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001538{
Daniel Stone37816df2012-05-16 18:45:18 +01001539 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001540 struct shell_surface *priv =
1541 container_of(grab, struct shell_surface, popup.grab);
1542 struct wl_client *client = priv->surface->surface.resource.client;
1543
Pekka Paalanencb108432012-01-19 16:25:40 +02001544 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001545 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001546 grab->focus = surface;
1547 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001548 wl_pointer_set_focus(pointer, NULL,
1549 wl_fixed_from_int(0),
1550 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001551 grab->focus = NULL;
1552 }
1553}
1554
1555static void
Scott Moreau447013d2012-02-18 05:05:29 -07001556popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001557 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001558{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001559 struct wl_resource *resource;
1560
Daniel Stone37816df2012-05-16 18:45:18 +01001561 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001562 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001563 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564}
1565
1566static void
Scott Moreau447013d2012-02-18 05:05:29 -07001567popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001568 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001569{
1570 struct wl_resource *resource;
1571 struct shell_surface *shsurf =
1572 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001573 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001574 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001575 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001576
Daniel Stone37816df2012-05-16 18:45:18 +01001577 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001578 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001579 display = wl_client_get_display(resource->client);
1580 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001581 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001582 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001583 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001584 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001585 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001586 wl_pointer_end_grab(grab->pointer);
1587 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001588 }
1589
Daniel Stone4dbadb12012-05-30 16:31:51 +01001590 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001591 shsurf->popup.initial_up = 1;
1592}
1593
Scott Moreau447013d2012-02-18 05:05:29 -07001594static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001595 popup_grab_focus,
1596 popup_grab_motion,
1597 popup_grab_button,
1598};
1599
1600static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001601shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001602{
Daniel Stone37816df2012-05-16 18:45:18 +01001603 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001605 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606
1607 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001608 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001609
Pekka Paalanen938269a2012-02-07 14:19:01 +02001610 weston_surface_update_transform(parent);
1611 if (parent->transform.enabled) {
1612 shsurf->popup.parent_transform.matrix =
1613 parent->transform.matrix;
1614 } else {
1615 /* construct x, y translation matrix */
1616 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1617 shsurf->popup.parent_transform.matrix.d[12] =
1618 parent->geometry.x;
1619 shsurf->popup.parent_transform.matrix.d[13] =
1620 parent->geometry.y;
1621 }
1622 wl_list_insert(es->geometry.transformation_list.prev,
1623 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001624 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001625
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001626 shsurf->popup.initial_up = 0;
1627
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001628 /* We don't require the grab to still be active, but if another
1629 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001630 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1631 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001632 } else {
1633 wl_shell_surface_send_popup_done(&shsurf->resource);
1634 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001635}
1636
1637static void
1638shell_surface_set_popup(struct wl_client *client,
1639 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001640 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001641 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001642 struct wl_resource *parent_resource,
1643 int32_t x, int32_t y, uint32_t flags)
1644{
1645 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001646
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001647 shsurf->type = SHELL_SURFACE_POPUP;
1648 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001649 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001650 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001651 shsurf->popup.x = x;
1652 shsurf->popup.y = y;
1653}
1654
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001655static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001656 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001657 shell_surface_move,
1658 shell_surface_resize,
1659 shell_surface_set_toplevel,
1660 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001661 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001662 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001663 shell_surface_set_maximized,
1664 shell_surface_set_title,
1665 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001666};
1667
1668static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001669destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001670{
Daniel Stone37816df2012-05-16 18:45:18 +01001671 if (shsurf->popup.grab.pointer)
1672 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001673
Alex Wubd3354b2012-04-17 17:20:49 +08001674 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1675 shell_surface_is_top_fullscreen(shsurf)) {
1676 weston_output_switch_mode(shsurf->fullscreen_output,
1677 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001678 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001679
Alex Wuaa08e2d2012-03-05 11:01:40 +08001680 if (shsurf->fullscreen.black_surface)
1681 weston_surface_destroy(shsurf->fullscreen.black_surface);
1682
Alex Wubd3354b2012-04-17 17:20:49 +08001683 /* As destroy_resource() use wl_list_for_each_safe(),
1684 * we can always remove the listener.
1685 */
1686 wl_list_remove(&shsurf->surface_destroy_listener.link);
1687 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001688 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001689
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001690 wl_list_remove(&shsurf->link);
1691 free(shsurf);
1692}
1693
1694static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001695shell_destroy_shell_surface(struct wl_resource *resource)
1696{
1697 struct shell_surface *shsurf = resource->data;
1698
1699 destroy_shell_surface(shsurf);
1700}
1701
1702static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001703shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001704{
1705 struct shell_surface *shsurf = container_of(listener,
1706 struct shell_surface,
1707 surface_destroy_listener);
1708
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001709 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001710 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001711 } else {
1712 wl_signal_emit(&shsurf->resource.destroy_signal,
1713 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001714 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001715 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001716}
1717
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001718static void
1719shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1720
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001721static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001722get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001723{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001724 if (surface->configure == shell_surface_configure)
1725 return surface->private;
1726 else
1727 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001728}
1729
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001730static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001731create_shell_surface(void *shell, struct weston_surface *surface,
1732 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001733{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001734 struct shell_surface *shsurf;
1735
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001736 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001737 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001738 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001739 }
1740
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001741 shsurf = calloc(1, sizeof *shsurf);
1742 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001743 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001744 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001745 }
1746
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001747 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001748 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001749
Tiago Vignattibc052c92012-04-19 16:18:18 +03001750 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001751 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001752 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001753 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001754 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001755 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1756 shsurf->fullscreen.framerate = 0;
1757 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001758 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001759 wl_list_init(&shsurf->fullscreen.transform.link);
1760
Tiago Vignattibc052c92012-04-19 16:18:18 +03001761 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001762 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1763 wl_signal_add(&surface->surface.resource.destroy_signal,
1764 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001765
1766 /* init link so its safe to always remove it in destroy_shell_surface */
1767 wl_list_init(&shsurf->link);
1768
Pekka Paalanen460099f2012-01-20 16:48:25 +02001769 /* empty when not in use */
1770 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001771 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001772
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001773 wl_list_init(&shsurf->workspace_transform.link);
1774
Pekka Paalanen98262232011-12-01 10:42:22 +02001775 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001776 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001777
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001778 shsurf->client = client;
1779
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001780 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001781}
1782
1783static void
1784shell_get_shell_surface(struct wl_client *client,
1785 struct wl_resource *resource,
1786 uint32_t id,
1787 struct wl_resource *surface_resource)
1788{
1789 struct weston_surface *surface = surface_resource->data;
1790 struct desktop_shell *shell = resource->data;
1791 struct shell_surface *shsurf;
1792
1793 if (get_shell_surface(surface)) {
1794 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001795 WL_DISPLAY_ERROR_INVALID_OBJECT,
1796 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001797 return;
1798 }
1799
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001800 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001801 if (!shsurf) {
1802 wl_resource_post_error(surface_resource,
1803 WL_DISPLAY_ERROR_INVALID_OBJECT,
1804 "surface->configure already set");
1805 return;
1806 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001807
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001808 shsurf->resource.destroy = shell_destroy_shell_surface;
1809 shsurf->resource.object.id = id;
1810 shsurf->resource.object.interface = &wl_shell_surface_interface;
1811 shsurf->resource.object.implementation =
1812 (void (**)(void)) &shell_surface_implementation;
1813 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001814
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001815 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001816}
1817
1818static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001819 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001820};
1821
Kristian Høgsberg07937562011-04-12 17:25:42 -04001822static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001823handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001824{
1825 proc->pid = 0;
1826}
1827
1828static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001829launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001830{
1831 if (shell->screensaver.binding)
1832 return;
1833
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001834 if (!shell->screensaver.path)
1835 return;
1836
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001837 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001838 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001839 return;
1840 }
1841
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001842 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001843 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001844 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001845 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001846}
1847
1848static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001849terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001850{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001851 if (shell->screensaver.process.pid == 0)
1852 return;
1853
1854 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001855}
1856
1857static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001858configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001859{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001860 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001861
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001862 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1863 if (s->output == es->output && s != es) {
1864 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001865 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001866 }
1867 }
1868
1869 weston_surface_configure(es, es->output->x, es->output->y,
1870 es->buffer->width, es->buffer->height);
1871
1872 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001873 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001874 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001875 }
1876}
1877
1878static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001879background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1880{
1881 struct desktop_shell *shell = es->private;
1882
1883 configure_static_surface(es, &shell->background_layer);
1884}
1885
1886static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001887desktop_shell_set_background(struct wl_client *client,
1888 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001889 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001890 struct wl_resource *surface_resource)
1891{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001892 struct desktop_shell *shell = resource->data;
1893 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001894
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001895 if (surface->configure) {
1896 wl_resource_post_error(surface_resource,
1897 WL_DISPLAY_ERROR_INVALID_OBJECT,
1898 "surface role already assigned");
1899 return;
1900 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001901
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001902 surface->configure = background_configure;
1903 surface->private = shell;
1904 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001905 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001906 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001907 surface->output->current->width,
1908 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001909}
1910
1911static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001912panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1913{
1914 struct desktop_shell *shell = es->private;
1915
1916 configure_static_surface(es, &shell->panel_layer);
1917}
1918
1919static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001920desktop_shell_set_panel(struct wl_client *client,
1921 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001922 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001923 struct wl_resource *surface_resource)
1924{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001925 struct desktop_shell *shell = resource->data;
1926 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001927
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001928 if (surface->configure) {
1929 wl_resource_post_error(surface_resource,
1930 WL_DISPLAY_ERROR_INVALID_OBJECT,
1931 "surface role already assigned");
1932 return;
1933 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001934
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001935 surface->configure = panel_configure;
1936 surface->private = shell;
1937 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001938 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001939 surface_resource,
1940 surface->output->current->width,
1941 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001942}
1943
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001944static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001945lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1946{
1947 struct desktop_shell *shell = surface->private;
1948
1949 center_on_output(surface, get_default_output(shell->compositor));
1950
1951 if (!weston_surface_is_mapped(surface)) {
1952 wl_list_insert(&shell->lock_layer.surface_list,
1953 &surface->layer_link);
1954 weston_surface_assign_output(surface);
1955 weston_compositor_wake(shell->compositor);
1956 }
1957}
1958
1959static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001960handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001961{
Tiago Vignattibe143262012-04-16 17:31:41 +03001962 struct desktop_shell *shell =
1963 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001964
Martin Minarik6d118362012-06-07 18:01:59 +02001965 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001966 shell->lock_surface = NULL;
1967}
1968
1969static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001970desktop_shell_set_lock_surface(struct wl_client *client,
1971 struct wl_resource *resource,
1972 struct wl_resource *surface_resource)
1973{
Tiago Vignattibe143262012-04-16 17:31:41 +03001974 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001975 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001976
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001977 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001978
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001979 if (!shell->locked)
1980 return;
1981
Pekka Paalanen98262232011-12-01 10:42:22 +02001982 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001983
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001984 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1985 wl_signal_add(&surface_resource->destroy_signal,
1986 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001987
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001988 surface->configure = lock_surface_configure;
1989 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001990}
1991
1992static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001993resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001994{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001995 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001996 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001997
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001998 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1999 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002000
2001 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002002
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002003 wl_list_remove(&shell->lock_layer.link);
2004 wl_list_insert(&shell->compositor->cursor_layer.link,
2005 &shell->fullscreen_layer.link);
2006 wl_list_insert(&shell->fullscreen_layer.link,
2007 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002008 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002009
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002010 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002011
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002012 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002013 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002014 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002015 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002016}
2017
2018static void
2019desktop_shell_unlock(struct wl_client *client,
2020 struct wl_resource *resource)
2021{
Tiago Vignattibe143262012-04-16 17:31:41 +03002022 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002023
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002024 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002025
2026 if (shell->locked)
2027 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002028}
2029
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002030static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002031desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002032 struct wl_resource *resource,
2033 struct wl_resource *surface_resource)
2034{
2035 struct desktop_shell *shell = resource->data;
2036
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002037 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002038}
2039
Kristian Høgsberg75840622011-09-06 13:48:16 -04002040static const struct desktop_shell_interface desktop_shell_implementation = {
2041 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002042 desktop_shell_set_panel,
2043 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002044 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002045 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002046};
2047
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002048static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002049get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002050{
2051 struct shell_surface *shsurf;
2052
2053 shsurf = get_shell_surface(surface);
2054 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002055 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002056 return shsurf->type;
2057}
2058
Kristian Høgsberg75840622011-09-06 13:48:16 -04002059static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002060move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002061{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002062 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002063 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002064 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002065
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002066 if (surface == NULL)
2067 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002068
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002069 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002070 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002071 return;
2072
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002073 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002074}
2075
2076static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002077resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002078{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002079 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002080 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002081 uint32_t edges = 0;
2082 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002083 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002084
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002085 if (surface == NULL)
2086 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002087
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002088 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002089 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002090 return;
2091
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002092 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002093 wl_fixed_to_int(seat->pointer->grab_x),
2094 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002095 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002096
Pekka Paalanen60921e52012-01-25 15:55:43 +02002097 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002098 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002099 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002100 edges |= 0;
2101 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002102 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002103
Pekka Paalanen60921e52012-01-25 15:55:43 +02002104 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002105 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002106 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002107 edges |= 0;
2108 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002109 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002110
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002111 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002112}
2113
2114static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002115surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002116 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002117{
Scott Moreau02709af2012-05-22 01:54:10 -06002118 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002119 struct shell_surface *shsurf;
2120 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002121 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002122
2123 if (surface == NULL)
2124 return;
2125
2126 shsurf = get_shell_surface(surface);
2127 if (!shsurf)
2128 return;
2129
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002130 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002131
Scott Moreau02709af2012-05-22 01:54:10 -06002132 if (surface->alpha > 1.0)
2133 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002134 if (surface->alpha < step)
2135 surface->alpha = step;
2136
2137 surface->geometry.dirty = 1;
2138 weston_surface_damage(surface);
2139}
2140
2141static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002142do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002143 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002144{
Daniel Stone37816df2012-05-16 18:45:18 +01002145 struct weston_seat *ws = (struct weston_seat *) seat;
2146 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002147 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002148 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002149
2150 wl_list_for_each(output, &compositor->output_list, link) {
2151 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002152 wl_fixed_to_double(seat->pointer->x),
2153 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002154 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002155 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002156 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002157 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002158 increment = -output->zoom.increment;
2159 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002160 increment = output->zoom.increment *
2161 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002162 else
2163 increment = 0;
2164
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002165 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002166
Scott Moreaue6603982012-06-11 13:07:51 -06002167 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002168 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002169 else if (output->zoom.level > output->zoom.max_level)
2170 output->zoom.level = output->zoom.max_level;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002171 else {
Scott Moreaue6603982012-06-11 13:07:51 -06002172 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002173 output->disable_planes++;
2174 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002175
Scott Moreaue6603982012-06-11 13:07:51 -06002176 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002177
Scott Moreau8dacaab2012-06-17 18:10:58 -06002178 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002179 }
2180 }
2181}
2182
Scott Moreauccbf29d2012-02-22 14:21:41 -07002183static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002184zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002185 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002186{
2187 do_zoom(seat, time, 0, axis, value);
2188}
2189
2190static void
2191zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2192 void *data)
2193{
2194 do_zoom(seat, time, key, 0, 0);
2195}
2196
2197static void
2198terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2199 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002200{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002201 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002202
Daniel Stone325fc2d2012-05-30 16:31:58 +01002203 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002204}
2205
2206static void
Scott Moreau447013d2012-02-18 05:05:29 -07002207rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002208 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002209{
2210 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002211 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002212 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002213 struct shell_surface *shsurf = rotate->base.shsurf;
2214 struct weston_surface *surface;
2215 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2216
2217 if (!shsurf)
2218 return;
2219
2220 surface = shsurf->surface;
2221
2222 cx = 0.5f * surface->geometry.width;
2223 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002224
Daniel Stone37816df2012-05-16 18:45:18 +01002225 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2226 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002227 r = sqrtf(dx * dx + dy * dy);
2228
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002229 wl_list_remove(&shsurf->rotation.transform.link);
2230 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002231
2232 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002233 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002234 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002235
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002236 weston_matrix_init(&rotate->rotation);
2237 rotate->rotation.d[0] = dx / r;
2238 rotate->rotation.d[4] = -dy / r;
2239 rotate->rotation.d[1] = -rotate->rotation.d[4];
2240 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002241
2242 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002243 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002244 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002245 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002246 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002247
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002248 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002249 &shsurf->surface->geometry.transformation_list,
2250 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002251 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002252 wl_list_init(&shsurf->rotation.transform.link);
2253 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002254 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002255 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002256
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002257 /* We need to adjust the position of the surface
2258 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002259 cposx = surface->geometry.x + cx;
2260 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002261 dposx = rotate->center.x - cposx;
2262 dposy = rotate->center.y - cposy;
2263 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002264 weston_surface_set_position(surface,
2265 surface->geometry.x + dposx,
2266 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002267 }
2268
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002269 /* Repaint implies weston_surface_update_transform(), which
2270 * lazily applies the damage due to rotation update.
2271 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002272 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002273}
2274
2275static void
Scott Moreau447013d2012-02-18 05:05:29 -07002276rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002277 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002278{
2279 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002280 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002281 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002282 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002283 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002284
Daniel Stone4dbadb12012-05-30 16:31:51 +01002285 if (pointer->button_count == 0 &&
2286 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002287 if (shsurf)
2288 weston_matrix_multiply(&shsurf->rotation.rotation,
2289 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002290 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002291 free(rotate);
2292 }
2293}
2294
Scott Moreau447013d2012-02-18 05:05:29 -07002295static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002296 noop_grab_focus,
2297 rotate_grab_motion,
2298 rotate_grab_button,
2299};
2300
2301static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002302rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002303 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002304{
2305 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002306 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002307 struct shell_surface *surface;
2308 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002309 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002310 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002311
2312 if (base_surface == NULL)
2313 return;
2314
2315 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002316 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002317 return;
2318
Pekka Paalanen460099f2012-01-20 16:48:25 +02002319 rotate = malloc(sizeof *rotate);
2320 if (!rotate)
2321 return;
2322
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002323 weston_surface_to_global_float(surface->surface,
2324 surface->surface->geometry.width / 2,
2325 surface->surface->geometry.height / 2,
2326 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002327
Daniel Stone37816df2012-05-16 18:45:18 +01002328 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2329 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002330 r = sqrtf(dx * dx + dy * dy);
2331 if (r > 20.0f) {
2332 struct weston_matrix inverse;
2333
2334 weston_matrix_init(&inverse);
2335 inverse.d[0] = dx / r;
2336 inverse.d[4] = dy / r;
2337 inverse.d[1] = -inverse.d[4];
2338 inverse.d[5] = inverse.d[0];
2339 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002340
2341 weston_matrix_init(&rotate->rotation);
2342 rotate->rotation.d[0] = dx / r;
2343 rotate->rotation.d[4] = -dy / r;
2344 rotate->rotation.d[1] = -rotate->rotation.d[4];
2345 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002346 } else {
2347 weston_matrix_init(&surface->rotation.rotation);
2348 weston_matrix_init(&rotate->rotation);
2349 }
2350
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002351 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2352 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002353}
2354
2355static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002356lower_fullscreen_layer(struct desktop_shell *shell)
2357{
2358 struct workspace *ws;
2359 struct weston_surface *surface, *prev;
2360
2361 ws = get_current_workspace(shell);
2362 wl_list_for_each_reverse_safe(surface, prev,
2363 &shell->fullscreen_layer.surface_list,
2364 layer_link)
2365 weston_surface_restack(surface, &ws->layer.surface_list);
2366}
2367
2368static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002369activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002370 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002371{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002372 struct workspace *ws = get_current_workspace(shell);
2373 struct focus_state *state;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002374
Daniel Stone37816df2012-05-16 18:45:18 +01002375 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002376
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002377 wl_list_for_each(state, &ws->focus_list, link)
2378 if (state->seat == seat)
2379 break;
2380
2381 if (&state->link == &ws->focus_list) {
2382 state = focus_state_create(seat, ws);
2383 if (state == NULL)
2384 return;
2385 }
2386
2387 state->keyboard_focus = es;
2388 wl_list_remove(&state->surface_destroy_listener.link);
2389 wl_signal_add(&es->surface.resource.destroy_signal,
2390 &state->surface_destroy_listener);
2391
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002392 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002393 case SHELL_SURFACE_FULLSCREEN:
2394 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002395 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002396 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002397 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002398 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002399 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002400 lower_fullscreen_layer(shell);
2401 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002402 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002403 }
2404}
2405
Alex Wu21858432012-04-01 20:13:08 +08002406/* no-op func for checking black surface */
2407static void
2408black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2409{
2410}
2411
2412static bool
2413is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2414{
2415 if (es->configure == black_surface_configure) {
2416 if (fs_surface)
2417 *fs_surface = (struct weston_surface *)es->private;
2418 return true;
2419 }
2420 return false;
2421}
2422
Kristian Høgsberg75840622011-09-06 13:48:16 -04002423static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002424click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002425 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002426{
Daniel Stone37816df2012-05-16 18:45:18 +01002427 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002428 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002429 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002430 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002431
Daniel Stone37816df2012-05-16 18:45:18 +01002432 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002433 if (!focus)
2434 return;
2435
Alex Wu21858432012-04-01 20:13:08 +08002436 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002437 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002438
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002439 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2440 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002441
Daniel Stone325fc2d2012-05-30 16:31:58 +01002442 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002443 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002444}
2445
2446static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002447lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002448{
Tiago Vignattibe143262012-04-16 17:31:41 +03002449 struct desktop_shell *shell =
2450 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002451 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002452 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002453
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002454 if (shell->locked) {
2455 wl_list_for_each(output, &shell->compositor->output_list, link)
2456 /* TODO: find a way to jump to other DPMS levels */
2457 if (output->set_dpms)
2458 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002459 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002460 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002461
2462 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002463
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002464 /* Hide all surfaces by removing the fullscreen, panel and
2465 * toplevel layers. This way nothing else can show or receive
2466 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002467
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002468 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002469 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002470 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002471 wl_list_insert(&shell->compositor->cursor_layer.link,
2472 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002473
Pekka Paalanen77346a62011-11-30 16:26:35 +02002474 launch_screensaver(shell);
2475
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002476 /* TODO: disable bindings that should not work while locked. */
2477
2478 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002479}
2480
2481static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002482unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002483{
Tiago Vignattibe143262012-04-16 17:31:41 +03002484 struct desktop_shell *shell =
2485 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002486
Pekka Paalanend81c2162011-11-16 13:47:34 +02002487 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002488 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002489 return;
2490 }
2491
2492 /* If desktop-shell client has gone away, unlock immediately. */
2493 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002494 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002495 return;
2496 }
2497
2498 if (shell->prepare_event_sent)
2499 return;
2500
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002501 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002502 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002503}
2504
2505static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002506show_input_panels(struct wl_listener *listener, void *data)
2507{
2508 struct desktop_shell *shell =
2509 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002510 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002511
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002512 wl_list_for_each_safe(surface, next,
2513 &shell->input_panel.surfaces, layer_link) {
2514 wl_list_remove(&surface->layer_link);
2515 wl_list_insert(&shell->panel_layer.surface_list,
2516 &surface->layer_link);
2517 weston_surface_assign_output(surface);
2518 weston_surface_damage(surface);
2519 weston_slide_run(surface,
2520 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002521 }
2522}
2523
2524static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002525input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2526
2527static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002528hide_input_panels(struct wl_listener *listener, void *data)
2529{
2530 struct desktop_shell *shell =
2531 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002532 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002533
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002534 wl_list_for_each_safe(surface, next,
2535 &shell->panel_layer.surface_list, layer_link)
2536 if (surface->configure == input_panel_configure) {
2537 weston_surface_unmap(surface);
2538 wl_list_insert(&shell->input_panel.surfaces,
2539 &surface->layer_link);
2540 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002541}
2542
2543static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002544center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002545{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002546 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002547 GLfloat x = (mode->width - surface->buffer->width) / 2;
2548 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002549
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002550 weston_surface_configure(surface, output->x + x, output->y + y,
2551 surface->buffer->width,
2552 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002553}
2554
2555static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002556map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002557 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002558{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002559 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002560 struct shell_surface *shsurf = get_shell_surface(surface);
2561 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002562 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002563 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002564 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002565 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002566
Pekka Paalanen60921e52012-01-25 15:55:43 +02002567 surface->geometry.width = width;
2568 surface->geometry.height = height;
2569 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002570
2571 /* initial positioning, see also configure() */
2572 switch (surface_type) {
2573 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002574 weston_surface_set_position(surface, 10 + random() % 400,
2575 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002576 break;
Alex Wu4539b082012-03-01 12:57:46 +08002577 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002578 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002579 shell_map_fullscreen(shsurf);
2580 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002581 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002582 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002583 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002584 weston_surface_set_position(surface, shsurf->output->x,
2585 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002586 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002587 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002588 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002589 case SHELL_SURFACE_NONE:
2590 weston_surface_set_position(surface,
2591 surface->geometry.x + sx,
2592 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002593 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002594 default:
2595 ;
2596 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002597
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002598 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002599 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002600 case SHELL_SURFACE_POPUP:
2601 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002602 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002603 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2604 break;
Alex Wu4539b082012-03-01 12:57:46 +08002605 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002606 case SHELL_SURFACE_NONE:
2607 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002608 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002609 ws = get_current_workspace(shell);
2610 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002611 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002612 }
2613
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002614 if (surface_type != SHELL_SURFACE_NONE) {
2615 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002616 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2617 surface->output = shsurf->output;
2618 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002619
Juan Zhao7bb92f02011-12-15 11:31:51 -05002620 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002621 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002622 if (shsurf->transient.flags ==
2623 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2624 break;
2625 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002626 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002627 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002628 if (!shell->locked) {
2629 wl_list_for_each(seat, &compositor->seat_list, link)
2630 activate(shell, surface, seat);
2631 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002632 break;
2633 default:
2634 break;
2635 }
2636
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002637 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002638 {
2639 switch (shell->win_animation_type) {
2640 case ANIMATION_FADE:
2641 weston_fade_run(surface, NULL, NULL);
2642 break;
2643 case ANIMATION_ZOOM:
2644 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2645 break;
2646 default:
2647 break;
2648 }
2649 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002650}
2651
2652static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002653configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002654 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002655{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002656 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2657 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002658
Pekka Paalanen77346a62011-11-30 16:26:35 +02002659 shsurf = get_shell_surface(surface);
2660 if (shsurf)
2661 surface_type = shsurf->type;
2662
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002663 surface->geometry.x = x;
2664 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002665 surface->geometry.width = width;
2666 surface->geometry.height = height;
2667 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002668
2669 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002670 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002671 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002672 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002673 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002674 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002675 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002676 surface->geometry.x = surface->output->x;
2677 surface->geometry.y = surface->output->y +
2678 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002679 break;
Alex Wu4539b082012-03-01 12:57:46 +08002680 case SHELL_SURFACE_TOPLEVEL:
2681 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002682 default:
2683 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002684 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002685
Alex Wu4539b082012-03-01 12:57:46 +08002686 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002687 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002688 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002689
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002690 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002691 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002692 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002693}
2694
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002695static void
2696shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2697{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002698 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002699 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002700 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002701
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002702 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002703 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002704 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002705 type_changed = 1;
2706 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002707
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002708 if (!weston_surface_is_mapped(es)) {
2709 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002710 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002711 es->geometry.width != es->buffer->width ||
2712 es->geometry.height != es->buffer->height) {
2713 GLfloat from_x, from_y;
2714 GLfloat to_x, to_y;
2715
2716 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2717 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2718 configure(shell, es,
2719 es->geometry.x + to_x - from_x,
2720 es->geometry.y + to_y - from_y,
2721 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002722 }
2723}
2724
Tiago Vignattibe143262012-04-16 17:31:41 +03002725static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002726
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002727static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002728desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002729{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002730 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002731 struct desktop_shell *shell =
2732 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002733
2734 shell->child.process.pid = 0;
2735 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002736
2737 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2738 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002739 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002740 shell->child.deathstamp = time;
2741 shell->child.deathcount = 0;
2742 }
2743
2744 shell->child.deathcount++;
2745 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002746 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002747 return;
2748 }
2749
Martin Minarik6d118362012-06-07 18:01:59 +02002750 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002751 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002752}
2753
2754static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002755launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002756{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002757 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002758
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002759 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002760 &shell->child.process,
2761 shell_exe,
2762 desktop_shell_sigchld);
2763
2764 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002765 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002766 return 0;
2767}
2768
2769static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002770bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2771{
Tiago Vignattibe143262012-04-16 17:31:41 +03002772 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002773
2774 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002775 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002776}
2777
Kristian Høgsberg75840622011-09-06 13:48:16 -04002778static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002779unbind_desktop_shell(struct wl_resource *resource)
2780{
Tiago Vignattibe143262012-04-16 17:31:41 +03002781 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002782
2783 if (shell->locked)
2784 resume_desktop(shell);
2785
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002786 shell->child.desktop_shell = NULL;
2787 shell->prepare_event_sent = false;
2788 free(resource);
2789}
2790
2791static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002792bind_desktop_shell(struct wl_client *client,
2793 void *data, uint32_t version, uint32_t id)
2794{
Tiago Vignattibe143262012-04-16 17:31:41 +03002795 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002796 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002797
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002798 resource = wl_client_add_object(client, &desktop_shell_interface,
2799 &desktop_shell_implementation,
2800 id, shell);
2801
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002802 if (client == shell->child.client) {
2803 resource->destroy = unbind_desktop_shell;
2804 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002805 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002806 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002807
2808 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2809 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002810 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002811}
2812
Pekka Paalanen6e168112011-11-24 11:34:05 +02002813static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002814screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2815{
2816 struct desktop_shell *shell = surface->private;
2817
2818 if (!shell->locked)
2819 return;
2820
2821 center_on_output(surface, surface->output);
2822
2823 if (wl_list_empty(&surface->layer_link)) {
2824 wl_list_insert(shell->lock_layer.surface_list.prev,
2825 &surface->layer_link);
2826 weston_surface_assign_output(surface);
2827 shell->compositor->idle_time = shell->screensaver.duration;
2828 weston_compositor_wake(shell->compositor);
2829 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2830 }
2831}
2832
2833static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002834screensaver_set_surface(struct wl_client *client,
2835 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002836 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002837 struct wl_resource *output_resource)
2838{
Tiago Vignattibe143262012-04-16 17:31:41 +03002839 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002840 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002841 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002842
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002843 surface->configure = screensaver_configure;
2844 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002845 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002846}
2847
2848static const struct screensaver_interface screensaver_implementation = {
2849 screensaver_set_surface
2850};
2851
2852static void
2853unbind_screensaver(struct wl_resource *resource)
2854{
Tiago Vignattibe143262012-04-16 17:31:41 +03002855 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002856
Pekka Paalanen77346a62011-11-30 16:26:35 +02002857 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002858 free(resource);
2859}
2860
2861static void
2862bind_screensaver(struct wl_client *client,
2863 void *data, uint32_t version, uint32_t id)
2864{
Tiago Vignattibe143262012-04-16 17:31:41 +03002865 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002866 struct wl_resource *resource;
2867
2868 resource = wl_client_add_object(client, &screensaver_interface,
2869 &screensaver_implementation,
2870 id, shell);
2871
Pekka Paalanen77346a62011-11-30 16:26:35 +02002872 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002873 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002874 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002875 return;
2876 }
2877
2878 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2879 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002880 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002881}
2882
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002883static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002884input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2885{
2886 struct weston_mode *mode = surface->output->current;
2887 GLfloat x = (mode->width - surface->buffer->width) / 2;
2888 GLfloat y = mode->height - surface->buffer->height;
2889
2890 /* Don't map the input panel here, wait for
2891 * show_input_panels signal. */
2892
2893 weston_surface_configure(surface,
2894 surface->output->x + x,
2895 surface->output->y + y,
2896 surface->buffer->width,
2897 surface->buffer->height);
2898}
2899
2900static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002901input_panel_set_surface(struct wl_client *client,
2902 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002903 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002904 struct wl_resource *output_resource)
2905{
2906 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002907 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002908 struct weston_output *output = output_resource->data;
2909
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002910 surface->configure = input_panel_configure;
2911 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002912 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002913 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002914}
2915
2916static const struct input_panel_interface input_panel_implementation = {
2917 input_panel_set_surface
2918};
2919
2920static void
2921unbind_input_panel(struct wl_resource *resource)
2922{
2923 struct desktop_shell *shell = resource->data;
2924
2925 shell->input_panel.binding = NULL;
2926 free(resource);
2927}
2928
2929static void
2930bind_input_panel(struct wl_client *client,
2931 void *data, uint32_t version, uint32_t id)
2932{
2933 struct desktop_shell *shell = data;
2934 struct wl_resource *resource;
2935
2936 resource = wl_client_add_object(client, &input_panel_interface,
2937 &input_panel_implementation,
2938 id, shell);
2939
2940 if (shell->input_panel.binding == NULL) {
2941 resource->destroy = unbind_input_panel;
2942 shell->input_panel.binding = resource;
2943 return;
2944 }
2945
2946 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2947 "interface object already bound");
2948 wl_resource_destroy(resource);
2949}
2950
Kristian Høgsberg07045392012-02-19 18:52:44 -05002951struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002952 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002953 struct weston_surface *current;
2954 struct wl_listener listener;
2955 struct wl_keyboard_grab grab;
2956};
2957
2958static void
2959switcher_next(struct switcher *switcher)
2960{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002961 struct weston_surface *surface;
2962 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002963 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002964 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002965
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002966 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002967 switch (get_shell_surface_type(surface)) {
2968 case SHELL_SURFACE_TOPLEVEL:
2969 case SHELL_SURFACE_FULLSCREEN:
2970 case SHELL_SURFACE_MAXIMIZED:
2971 if (first == NULL)
2972 first = surface;
2973 if (prev == switcher->current)
2974 next = surface;
2975 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002976 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002977 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002978 weston_surface_damage(surface);
2979 break;
2980 default:
2981 break;
2982 }
Alex Wu1659daa2012-04-01 20:13:09 +08002983
2984 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002985 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002986 surface->geometry.dirty = 1;
2987 weston_surface_damage(surface);
2988 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002989 }
2990
2991 if (next == NULL)
2992 next = first;
2993
Alex Wu07b26062012-03-12 16:06:01 +08002994 if (next == NULL)
2995 return;
2996
Kristian Høgsberg07045392012-02-19 18:52:44 -05002997 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002998 wl_signal_add(&next->surface.resource.destroy_signal,
2999 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003000
3001 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003002 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003003
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003004 shsurf = get_shell_surface(switcher->current);
3005 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003006 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003007}
3008
3009static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003010switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003011{
3012 struct switcher *switcher =
3013 container_of(listener, struct switcher, listener);
3014
3015 switcher_next(switcher);
3016}
3017
3018static void
Daniel Stone351eb612012-05-31 15:27:47 -04003019switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003020{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003021 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003022 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003023 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003024
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003025 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003026 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003027 weston_surface_damage(surface);
3028 }
3029
Alex Wu07b26062012-03-12 16:06:01 +08003030 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003031 activate(switcher->shell, switcher->current,
3032 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003033 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003034 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003035 free(switcher);
3036}
3037
3038static void
3039switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003040 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003041{
3042 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003043 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003044
Daniel Stonec9785ea2012-05-30 16:31:52 +01003045 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003046 switcher_next(switcher);
3047}
3048
3049static void
3050switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3051 uint32_t mods_depressed, uint32_t mods_latched,
3052 uint32_t mods_locked, uint32_t group)
3053{
3054 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003055 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003056
Daniel Stone351eb612012-05-31 15:27:47 -04003057 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3058 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003059}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003060
3061static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003062 switcher_key,
3063 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003064};
3065
3066static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003067switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3068 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003069{
Tiago Vignattibe143262012-04-16 17:31:41 +03003070 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003071 struct switcher *switcher;
3072
3073 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003074 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003075 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003076 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003077 wl_list_init(&switcher->listener.link);
3078
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003079 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003080 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003081 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3082 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003083 switcher_next(switcher);
3084}
3085
Pekka Paalanen3c647232011-12-22 13:43:43 +02003086static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003087backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3088 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003089{
3090 struct weston_compositor *compositor = data;
3091 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003092 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003093
3094 /* TODO: we're limiting to simple use cases, where we assume just
3095 * control on the primary display. We'd have to extend later if we
3096 * ever get support for setting backlights on random desktop LCD
3097 * panels though */
3098 output = get_default_output(compositor);
3099 if (!output)
3100 return;
3101
3102 if (!output->set_backlight)
3103 return;
3104
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003105 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3106 backlight_new = output->backlight_current - 25;
3107 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3108 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003109
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003110 if (backlight_new < 5)
3111 backlight_new = 5;
3112 if (backlight_new > 255)
3113 backlight_new = 255;
3114
3115 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003116 output->set_backlight(output, output->backlight_current);
3117}
3118
3119static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003120debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3121 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003122{
Tiago Vignattibe143262012-04-16 17:31:41 +03003123 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003124 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003125 struct weston_surface *surface;
3126
3127 if (shell->debug_repaint_surface) {
3128 weston_surface_destroy(shell->debug_repaint_surface);
3129 shell->debug_repaint_surface = NULL;
3130 } else {
3131 surface = weston_surface_create(compositor);
3132 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3133 weston_surface_configure(surface, 0, 0, 8192, 8192);
3134 wl_list_insert(&compositor->fade_layer.surface_list,
3135 &surface->layer_link);
3136 weston_surface_assign_output(surface);
3137 pixman_region32_init(&surface->input);
3138
3139 /* Here's the dirty little trick that makes the
3140 * repaint debugging work: we force an
3141 * update_transform first to update dependent state
3142 * and clear the geometry.dirty bit. Then we clear
3143 * the surface damage so it only gets repainted
3144 * piecewise as we repaint other things. */
3145
3146 weston_surface_update_transform(surface);
3147 pixman_region32_fini(&surface->damage);
3148 pixman_region32_init(&surface->damage);
3149 shell->debug_repaint_surface = surface;
3150 }
3151}
3152
3153static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003154force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3155 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003156{
3157 struct wl_client *client;
3158 pid_t pid;
3159 uid_t uid;
3160 gid_t gid;
3161
Daniel Stone325fc2d2012-05-30 16:31:58 +01003162 client = seat->keyboard->focus->resource.client;
3163 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003164
Daniel Stone325fc2d2012-05-30 16:31:58 +01003165 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003166}
3167
3168static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003169workspace_up_binding(struct wl_seat *seat, uint32_t time,
3170 uint32_t key, void *data)
3171{
3172 struct desktop_shell *shell = data;
3173 unsigned int new_index = shell->workspaces.current;
3174
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003175 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003176 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003177 if (new_index != 0)
3178 new_index--;
3179
3180 change_workspace(shell, new_index);
3181}
3182
3183static void
3184workspace_down_binding(struct wl_seat *seat, uint32_t time,
3185 uint32_t key, void *data)
3186{
3187 struct desktop_shell *shell = data;
3188 unsigned int new_index = shell->workspaces.current;
3189
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003190 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003191 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003192 if (new_index < shell->workspaces.num - 1)
3193 new_index++;
3194
3195 change_workspace(shell, new_index);
3196}
3197
3198static void
3199workspace_f_binding(struct wl_seat *seat, uint32_t time,
3200 uint32_t key, void *data)
3201{
3202 struct desktop_shell *shell = data;
3203 unsigned int new_index;
3204
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003205 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003206 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003207 new_index = key - KEY_F1;
3208 if (new_index >= shell->workspaces.num)
3209 new_index = shell->workspaces.num - 1;
3210
3211 change_workspace(shell, new_index);
3212}
3213
3214
3215static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003216shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003217{
Tiago Vignattibe143262012-04-16 17:31:41 +03003218 struct desktop_shell *shell =
3219 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003220 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003221
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003222 if (shell->child.client)
3223 wl_client_destroy(shell->child.client);
3224
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003225 wl_list_remove(&shell->lock_listener.link);
3226 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003227 wl_list_remove(&shell->show_input_panel_listener.link);
3228 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003229
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003230 wl_array_for_each(ws, &shell->workspaces.array)
3231 workspace_destroy(*ws);
3232 wl_array_release(&shell->workspaces.array);
3233
Pekka Paalanen3c647232011-12-22 13:43:43 +02003234 free(shell->screensaver.path);
3235 free(shell);
3236}
3237
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003238static void
3239shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3240{
3241 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003242 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003243
3244 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003245 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3246 MODIFIER_CTRL | MODIFIER_ALT,
3247 terminate_binding, ec);
3248 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3249 click_to_activate_binding,
3250 shell);
3251 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3252 MODIFIER_SUPER | MODIFIER_ALT,
3253 surface_opacity_binding, NULL);
3254 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3255 MODIFIER_SUPER, zoom_axis_binding,
3256 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003257
3258 /* configurable bindings */
3259 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003260 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3261 zoom_key_binding, NULL);
3262 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3263 zoom_key_binding, NULL);
3264 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3265 shell);
3266 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3267 resize_binding, shell);
3268 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3269 rotate_binding, NULL);
3270 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3271 shell);
3272 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3273 ec);
3274 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3275 backlight_binding, ec);
3276 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3277 ec);
3278 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3279 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003280 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003281 debug_repaint_binding, shell);
3282 weston_compositor_add_key_binding(ec, KEY_K, mod,
3283 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003284 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3285 workspace_up_binding, shell);
3286 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3287 workspace_down_binding, shell);
3288
3289 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3290 if (shell->workspaces.num > 1) {
3291 num_workspace_bindings = shell->workspaces.num;
3292 if (num_workspace_bindings > 6)
3293 num_workspace_bindings = 6;
3294 for (i = 0; i < num_workspace_bindings; i++)
3295 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3296 workspace_f_binding,
3297 shell);
3298 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003299}
3300
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003301int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003302shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003303
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003304WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003305shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003306{
Tiago Vignattibe143262012-04-16 17:31:41 +03003307 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003308 struct workspace **pws;
3309 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003310
3311 shell = malloc(sizeof *shell);
3312 if (shell == NULL)
3313 return -1;
3314
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003315 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003316 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003317
3318 shell->destroy_listener.notify = shell_destroy;
3319 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3320 shell->lock_listener.notify = lock;
3321 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3322 shell->unlock_listener.notify = unlock;
3323 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003324 shell->show_input_panel_listener.notify = show_input_panels;
3325 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3326 shell->hide_input_panel_listener.notify = hide_input_panels;
3327 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003328 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003329 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003330 ec->shell_interface.create_shell_surface = create_shell_surface;
3331 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003332 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003333 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003334 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003335
Pekka Paalanen77346a62011-11-30 16:26:35 +02003336 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003337 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003338
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003339 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3340 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003341 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3342 weston_layer_init(&shell->lock_layer, NULL);
3343
3344 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003345
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003346 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003347
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003348 for (i = 0; i < shell->workspaces.num; i++) {
3349 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3350 if (pws == NULL)
3351 return -1;
3352
3353 *pws = workspace_create();
3354 if (*pws == NULL)
3355 return -1;
3356 }
3357 activate_workspace(shell, 0);
3358
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003359 wl_list_init(&shell->workspaces.animation.link);
3360 shell->workspaces.animation.frame = animate_workspace_change_frame;
3361
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003362 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3363 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003364 return -1;
3365
Kristian Høgsberg75840622011-09-06 13:48:16 -04003366 if (wl_display_add_global(ec->wl_display,
3367 &desktop_shell_interface,
3368 shell, bind_desktop_shell) == NULL)
3369 return -1;
3370
Pekka Paalanen6e168112011-11-24 11:34:05 +02003371 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3372 shell, bind_screensaver) == NULL)
3373 return -1;
3374
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003375 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3376 shell, bind_input_panel) == NULL)
3377 return -1;
3378
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003379 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003380 if (launch_desktop_shell_process(shell) != 0)
3381 return -1;
3382
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003383 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003384 if (ec->seat->seat.pointer)
3385 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3386 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003387
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003388 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003389
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003390 return 0;
3391}