blob: 545689c4f401509875d57bbc647942c2f16488ad [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);
1391 return surface;
1392}
1393
1394/* Create black surface and append it to the associated fullscreen surface.
1395 * Handle size dismatch and positioning according to the method. */
1396static void
1397shell_configure_fullscreen(struct shell_surface *shsurf)
1398{
1399 struct weston_output *output = shsurf->fullscreen_output;
1400 struct weston_surface *surface = shsurf->surface;
1401 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001402 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001403
Alex Wu4539b082012-03-01 12:57:46 +08001404 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001405 shsurf->fullscreen.black_surface =
1406 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001407 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001408 output->x, output->y,
1409 output->current->width,
1410 output->current->height);
1411
1412 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1413 wl_list_insert(&surface->layer_link,
1414 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001415 shsurf->fullscreen.black_surface->output = output;
1416
1417 switch (shsurf->fullscreen.type) {
1418 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001419 if (surface->buffer)
1420 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001421 break;
1422 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1423 matrix = &shsurf->fullscreen.transform.matrix;
1424 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001425
1426 output_aspect = (float) output->current->width /
1427 (float) output->current->height;
1428 surface_aspect = (float) surface->geometry.width /
1429 (float) surface->geometry.height;
1430 if (output_aspect < surface_aspect)
1431 scale = (float) output->current->width /
1432 (float) surface->geometry.width;
1433 else
1434 scale = (float) output->current->height /
1435 (float) surface->geometry.height;
1436
Alex Wu4539b082012-03-01 12:57:46 +08001437 weston_matrix_scale(matrix, scale, scale, 1);
1438 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001439 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001440 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001441 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1442 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1443 weston_surface_set_position(surface, x, y);
1444
Alex Wu4539b082012-03-01 12:57:46 +08001445 break;
1446 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001447 if (shell_surface_is_top_fullscreen(shsurf)) {
1448 struct weston_mode mode = {0,
1449 surface->geometry.width,
1450 surface->geometry.height,
1451 shsurf->fullscreen.framerate};
1452
1453 if (weston_output_switch_mode(output, &mode) == 0) {
1454 weston_surface_configure(shsurf->fullscreen.black_surface,
1455 output->x, output->y,
1456 output->current->width,
1457 output->current->height);
1458 weston_surface_set_position(surface, output->x, output->y);
1459 break;
1460 }
1461 }
Alex Wu4539b082012-03-01 12:57:46 +08001462 break;
1463 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1464 break;
1465 default:
1466 break;
1467 }
1468}
1469
1470/* make the fullscreen and black surface at the top */
1471static void
1472shell_stack_fullscreen(struct shell_surface *shsurf)
1473{
Alex Wubd3354b2012-04-17 17:20:49 +08001474 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001475 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001476 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001477
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001478 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001479 wl_list_insert(&shell->fullscreen_layer.surface_list,
1480 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001481 weston_surface_damage(surface);
1482
1483 if (!shsurf->fullscreen.black_surface)
1484 shsurf->fullscreen.black_surface =
1485 create_black_surface(surface->compositor,
1486 surface,
1487 output->x, output->y,
1488 output->current->width,
1489 output->current->height);
1490
1491 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001492 wl_list_insert(&surface->layer_link,
1493 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001494 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001495}
1496
1497static void
1498shell_map_fullscreen(struct shell_surface *shsurf)
1499{
Alex Wu4539b082012-03-01 12:57:46 +08001500 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001501 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001502}
1503
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001504static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001505shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001506 struct wl_resource *resource,
1507 uint32_t method,
1508 uint32_t framerate,
1509 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001510{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001511 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001512 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001513
1514 if (output_resource)
1515 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001516 else if (es->output)
1517 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001518 else
1519 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001520
Alex Wu4539b082012-03-01 12:57:46 +08001521 shsurf->fullscreen_output = shsurf->output;
1522 shsurf->fullscreen.type = method;
1523 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001524 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001525
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001526 shsurf->client->send_configure(shsurf->surface, 0,
1527 shsurf->output->current->width,
1528 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001529}
1530
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001531static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001532popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001533 struct wl_surface *surface,
1534 wl_fixed_t x,
1535 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001536{
Daniel Stone37816df2012-05-16 18:45:18 +01001537 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001538 struct shell_surface *priv =
1539 container_of(grab, struct shell_surface, popup.grab);
1540 struct wl_client *client = priv->surface->surface.resource.client;
1541
Pekka Paalanencb108432012-01-19 16:25:40 +02001542 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001543 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001544 grab->focus = surface;
1545 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001546 wl_pointer_set_focus(pointer, NULL,
1547 wl_fixed_from_int(0),
1548 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001549 grab->focus = NULL;
1550 }
1551}
1552
1553static void
Scott Moreau447013d2012-02-18 05:05:29 -07001554popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001555 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001556{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001557 struct wl_resource *resource;
1558
Daniel Stone37816df2012-05-16 18:45:18 +01001559 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001560 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001561 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001562}
1563
1564static void
Scott Moreau447013d2012-02-18 05:05:29 -07001565popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001566 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001567{
1568 struct wl_resource *resource;
1569 struct shell_surface *shsurf =
1570 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001571 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001572 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001573 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001574
Daniel Stone37816df2012-05-16 18:45:18 +01001575 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001576 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001577 display = wl_client_get_display(resource->client);
1578 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001579 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001580 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001581 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001582 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001583 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001584 wl_pointer_end_grab(grab->pointer);
1585 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001586 }
1587
Daniel Stone4dbadb12012-05-30 16:31:51 +01001588 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001589 shsurf->popup.initial_up = 1;
1590}
1591
Scott Moreau447013d2012-02-18 05:05:29 -07001592static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001593 popup_grab_focus,
1594 popup_grab_motion,
1595 popup_grab_button,
1596};
1597
1598static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001599shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001600{
Daniel Stone37816df2012-05-16 18:45:18 +01001601 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001602 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001603 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604
1605 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001607
Pekka Paalanen938269a2012-02-07 14:19:01 +02001608 weston_surface_update_transform(parent);
1609 if (parent->transform.enabled) {
1610 shsurf->popup.parent_transform.matrix =
1611 parent->transform.matrix;
1612 } else {
1613 /* construct x, y translation matrix */
1614 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1615 shsurf->popup.parent_transform.matrix.d[12] =
1616 parent->geometry.x;
1617 shsurf->popup.parent_transform.matrix.d[13] =
1618 parent->geometry.y;
1619 }
1620 wl_list_insert(es->geometry.transformation_list.prev,
1621 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001622 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001623
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001624 shsurf->popup.initial_up = 0;
1625
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001626 /* We don't require the grab to still be active, but if another
1627 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001628 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1629 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001630 } else {
1631 wl_shell_surface_send_popup_done(&shsurf->resource);
1632 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001633}
1634
1635static void
1636shell_surface_set_popup(struct wl_client *client,
1637 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001638 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001639 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001640 struct wl_resource *parent_resource,
1641 int32_t x, int32_t y, uint32_t flags)
1642{
1643 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001644
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001645 shsurf->type = SHELL_SURFACE_POPUP;
1646 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001647 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001648 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001649 shsurf->popup.x = x;
1650 shsurf->popup.y = y;
1651}
1652
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001653static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001654 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001655 shell_surface_move,
1656 shell_surface_resize,
1657 shell_surface_set_toplevel,
1658 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001659 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001660 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001661 shell_surface_set_maximized,
1662 shell_surface_set_title,
1663 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001664};
1665
1666static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001667destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001668{
Daniel Stone37816df2012-05-16 18:45:18 +01001669 if (shsurf->popup.grab.pointer)
1670 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001671
Alex Wubd3354b2012-04-17 17:20:49 +08001672 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1673 shell_surface_is_top_fullscreen(shsurf)) {
1674 weston_output_switch_mode(shsurf->fullscreen_output,
1675 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001676 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001677
Alex Wuaa08e2d2012-03-05 11:01:40 +08001678 if (shsurf->fullscreen.black_surface)
1679 weston_surface_destroy(shsurf->fullscreen.black_surface);
1680
Alex Wubd3354b2012-04-17 17:20:49 +08001681 /* As destroy_resource() use wl_list_for_each_safe(),
1682 * we can always remove the listener.
1683 */
1684 wl_list_remove(&shsurf->surface_destroy_listener.link);
1685 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001686 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001687
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001688 wl_list_remove(&shsurf->link);
1689 free(shsurf);
1690}
1691
1692static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001693shell_destroy_shell_surface(struct wl_resource *resource)
1694{
1695 struct shell_surface *shsurf = resource->data;
1696
1697 destroy_shell_surface(shsurf);
1698}
1699
1700static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001701shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001702{
1703 struct shell_surface *shsurf = container_of(listener,
1704 struct shell_surface,
1705 surface_destroy_listener);
1706
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001707 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001708 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001709 } else {
1710 wl_signal_emit(&shsurf->resource.destroy_signal,
1711 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001712 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001713 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001714}
1715
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001716static void
1717shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1718
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001719static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001720get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001721{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001722 if (surface->configure == shell_surface_configure)
1723 return surface->private;
1724 else
1725 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001726}
1727
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001728static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001729create_shell_surface(void *shell, struct weston_surface *surface,
1730 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001731{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001732 struct shell_surface *shsurf;
1733
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001734 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001735 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001736 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001737 }
1738
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001739 shsurf = calloc(1, sizeof *shsurf);
1740 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001741 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001742 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001743 }
1744
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001745 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001746 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001747
Tiago Vignattibc052c92012-04-19 16:18:18 +03001748 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001749 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001750 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001751 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001752 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001753 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1754 shsurf->fullscreen.framerate = 0;
1755 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001756 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001757 wl_list_init(&shsurf->fullscreen.transform.link);
1758
Tiago Vignattibc052c92012-04-19 16:18:18 +03001759 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001760 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1761 wl_signal_add(&surface->surface.resource.destroy_signal,
1762 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001763
1764 /* init link so its safe to always remove it in destroy_shell_surface */
1765 wl_list_init(&shsurf->link);
1766
Pekka Paalanen460099f2012-01-20 16:48:25 +02001767 /* empty when not in use */
1768 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001769 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001770
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001771 wl_list_init(&shsurf->workspace_transform.link);
1772
Pekka Paalanen98262232011-12-01 10:42:22 +02001773 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001774 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001775
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001776 shsurf->client = client;
1777
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001778 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001779}
1780
1781static void
1782shell_get_shell_surface(struct wl_client *client,
1783 struct wl_resource *resource,
1784 uint32_t id,
1785 struct wl_resource *surface_resource)
1786{
1787 struct weston_surface *surface = surface_resource->data;
1788 struct desktop_shell *shell = resource->data;
1789 struct shell_surface *shsurf;
1790
1791 if (get_shell_surface(surface)) {
1792 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001793 WL_DISPLAY_ERROR_INVALID_OBJECT,
1794 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001795 return;
1796 }
1797
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001798 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001799 if (!shsurf) {
1800 wl_resource_post_error(surface_resource,
1801 WL_DISPLAY_ERROR_INVALID_OBJECT,
1802 "surface->configure already set");
1803 return;
1804 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001805
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001806 shsurf->resource.destroy = shell_destroy_shell_surface;
1807 shsurf->resource.object.id = id;
1808 shsurf->resource.object.interface = &wl_shell_surface_interface;
1809 shsurf->resource.object.implementation =
1810 (void (**)(void)) &shell_surface_implementation;
1811 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001812
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001813 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001814}
1815
1816static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001817 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001818};
1819
Kristian Høgsberg07937562011-04-12 17:25:42 -04001820static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001821handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001822{
1823 proc->pid = 0;
1824}
1825
1826static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001827launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001828{
1829 if (shell->screensaver.binding)
1830 return;
1831
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001832 if (!shell->screensaver.path)
1833 return;
1834
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001835 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001836 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001837 return;
1838 }
1839
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001840 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001841 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001842 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001843 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001844}
1845
1846static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001847terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001848{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001849 if (shell->screensaver.process.pid == 0)
1850 return;
1851
1852 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001853}
1854
1855static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001856configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001857{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001858 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001859
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001860 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1861 if (s->output == es->output && s != es) {
1862 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001863 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001864 }
1865 }
1866
1867 weston_surface_configure(es, es->output->x, es->output->y,
1868 es->buffer->width, es->buffer->height);
1869
1870 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001871 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001872 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001873 }
1874}
1875
1876static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001877background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1878{
1879 struct desktop_shell *shell = es->private;
1880
1881 configure_static_surface(es, &shell->background_layer);
1882}
1883
1884static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001885desktop_shell_set_background(struct wl_client *client,
1886 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001887 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001888 struct wl_resource *surface_resource)
1889{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001890 struct desktop_shell *shell = resource->data;
1891 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001892
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001893 if (surface->configure) {
1894 wl_resource_post_error(surface_resource,
1895 WL_DISPLAY_ERROR_INVALID_OBJECT,
1896 "surface role already assigned");
1897 return;
1898 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001899
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001900 surface->configure = background_configure;
1901 surface->private = shell;
1902 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001903 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001904 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001905 surface->output->current->width,
1906 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001907}
1908
1909static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001910panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1911{
1912 struct desktop_shell *shell = es->private;
1913
1914 configure_static_surface(es, &shell->panel_layer);
1915}
1916
1917static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001918desktop_shell_set_panel(struct wl_client *client,
1919 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001920 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001921 struct wl_resource *surface_resource)
1922{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001923 struct desktop_shell *shell = resource->data;
1924 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001925
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001926 if (surface->configure) {
1927 wl_resource_post_error(surface_resource,
1928 WL_DISPLAY_ERROR_INVALID_OBJECT,
1929 "surface role already assigned");
1930 return;
1931 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001932
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001933 surface->configure = panel_configure;
1934 surface->private = shell;
1935 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001936 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001937 surface_resource,
1938 surface->output->current->width,
1939 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001940}
1941
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001942static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001943lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1944{
1945 struct desktop_shell *shell = surface->private;
1946
1947 center_on_output(surface, get_default_output(shell->compositor));
1948
1949 if (!weston_surface_is_mapped(surface)) {
1950 wl_list_insert(&shell->lock_layer.surface_list,
1951 &surface->layer_link);
1952 weston_surface_assign_output(surface);
1953 weston_compositor_wake(shell->compositor);
1954 }
1955}
1956
1957static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001958handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001959{
Tiago Vignattibe143262012-04-16 17:31:41 +03001960 struct desktop_shell *shell =
1961 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001962
Martin Minarik6d118362012-06-07 18:01:59 +02001963 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001964 shell->lock_surface = NULL;
1965}
1966
1967static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001968desktop_shell_set_lock_surface(struct wl_client *client,
1969 struct wl_resource *resource,
1970 struct wl_resource *surface_resource)
1971{
Tiago Vignattibe143262012-04-16 17:31:41 +03001972 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001973 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001974
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001975 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001976
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001977 if (!shell->locked)
1978 return;
1979
Pekka Paalanen98262232011-12-01 10:42:22 +02001980 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001981
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001982 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1983 wl_signal_add(&surface_resource->destroy_signal,
1984 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001985
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001986 surface->configure = lock_surface_configure;
1987 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001988}
1989
1990static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001991resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001992{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001993 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001994 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001995
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001996 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1997 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001998
1999 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002000
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002001 wl_list_remove(&shell->lock_layer.link);
2002 wl_list_insert(&shell->compositor->cursor_layer.link,
2003 &shell->fullscreen_layer.link);
2004 wl_list_insert(&shell->fullscreen_layer.link,
2005 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002006 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002007
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002008 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002009
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002010 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002011 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002012 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002013 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002014}
2015
2016static void
2017desktop_shell_unlock(struct wl_client *client,
2018 struct wl_resource *resource)
2019{
Tiago Vignattibe143262012-04-16 17:31:41 +03002020 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002021
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002022 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002023
2024 if (shell->locked)
2025 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002026}
2027
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002028static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002029desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002030 struct wl_resource *resource,
2031 struct wl_resource *surface_resource)
2032{
2033 struct desktop_shell *shell = resource->data;
2034
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002035 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036}
2037
Kristian Høgsberg75840622011-09-06 13:48:16 -04002038static const struct desktop_shell_interface desktop_shell_implementation = {
2039 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002040 desktop_shell_set_panel,
2041 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002042 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002043 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002044};
2045
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002046static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002047get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002048{
2049 struct shell_surface *shsurf;
2050
2051 shsurf = get_shell_surface(surface);
2052 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002053 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002054 return shsurf->type;
2055}
2056
Kristian Høgsberg75840622011-09-06 13:48:16 -04002057static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002058move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002059{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002060 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002061 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002062 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002063
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002064 if (surface == NULL)
2065 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002066
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002067 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002068 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002069 return;
2070
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002071 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002072}
2073
2074static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002075resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002076{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002077 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002078 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002079 uint32_t edges = 0;
2080 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002081 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002082
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002083 if (surface == NULL)
2084 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002085
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002086 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002087 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002088 return;
2089
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002090 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002091 wl_fixed_to_int(seat->pointer->grab_x),
2092 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002093 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002094
Pekka Paalanen60921e52012-01-25 15:55:43 +02002095 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002096 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002097 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002098 edges |= 0;
2099 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002100 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002101
Pekka Paalanen60921e52012-01-25 15:55:43 +02002102 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002103 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002104 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002105 edges |= 0;
2106 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002107 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002108
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002109 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002110}
2111
2112static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002113surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002114 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002115{
Scott Moreau02709af2012-05-22 01:54:10 -06002116 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002117 struct shell_surface *shsurf;
2118 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002119 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002120
2121 if (surface == NULL)
2122 return;
2123
2124 shsurf = get_shell_surface(surface);
2125 if (!shsurf)
2126 return;
2127
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002128 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002129
Scott Moreau02709af2012-05-22 01:54:10 -06002130 if (surface->alpha > 1.0)
2131 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002132 if (surface->alpha < step)
2133 surface->alpha = step;
2134
2135 surface->geometry.dirty = 1;
2136 weston_surface_damage(surface);
2137}
2138
2139static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002140do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002141 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002142{
Daniel Stone37816df2012-05-16 18:45:18 +01002143 struct weston_seat *ws = (struct weston_seat *) seat;
2144 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002145 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002146 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002147
2148 wl_list_for_each(output, &compositor->output_list, link) {
2149 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002150 wl_fixed_to_double(seat->pointer->x),
2151 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002152 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002153 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002154 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002155 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002156 increment = -output->zoom.increment;
2157 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002158 increment = output->zoom.increment *
2159 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002160 else
2161 increment = 0;
2162
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002163 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002164
Scott Moreaue6603982012-06-11 13:07:51 -06002165 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002166 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002167 else if (output->zoom.level > output->zoom.max_level)
2168 output->zoom.level = output->zoom.max_level;
2169 else
2170 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002171
Scott Moreaue6603982012-06-11 13:07:51 -06002172 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002173
Scott Moreau8dacaab2012-06-17 18:10:58 -06002174 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002175 }
2176 }
2177}
2178
Scott Moreauccbf29d2012-02-22 14:21:41 -07002179static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002180zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002181 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002182{
2183 do_zoom(seat, time, 0, axis, value);
2184}
2185
2186static void
2187zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2188 void *data)
2189{
2190 do_zoom(seat, time, key, 0, 0);
2191}
2192
2193static void
2194terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2195 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002196{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002197 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002198
Daniel Stone325fc2d2012-05-30 16:31:58 +01002199 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002200}
2201
2202static void
Scott Moreau447013d2012-02-18 05:05:29 -07002203rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002204 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002205{
2206 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002207 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002208 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002209 struct shell_surface *shsurf = rotate->base.shsurf;
2210 struct weston_surface *surface;
2211 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2212
2213 if (!shsurf)
2214 return;
2215
2216 surface = shsurf->surface;
2217
2218 cx = 0.5f * surface->geometry.width;
2219 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002220
Daniel Stone37816df2012-05-16 18:45:18 +01002221 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2222 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002223 r = sqrtf(dx * dx + dy * dy);
2224
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002225 wl_list_remove(&shsurf->rotation.transform.link);
2226 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002227
2228 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002229 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002230 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002231
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002232 weston_matrix_init(&rotate->rotation);
2233 rotate->rotation.d[0] = dx / r;
2234 rotate->rotation.d[4] = -dy / r;
2235 rotate->rotation.d[1] = -rotate->rotation.d[4];
2236 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002237
2238 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002239 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002240 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002241 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002242 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002243
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002244 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002245 &shsurf->surface->geometry.transformation_list,
2246 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002247 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002248 wl_list_init(&shsurf->rotation.transform.link);
2249 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002250 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002251 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002252
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002253 /* We need to adjust the position of the surface
2254 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002255 cposx = surface->geometry.x + cx;
2256 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002257 dposx = rotate->center.x - cposx;
2258 dposy = rotate->center.y - cposy;
2259 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002260 weston_surface_set_position(surface,
2261 surface->geometry.x + dposx,
2262 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002263 }
2264
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002265 /* Repaint implies weston_surface_update_transform(), which
2266 * lazily applies the damage due to rotation update.
2267 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002268 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002269}
2270
2271static void
Scott Moreau447013d2012-02-18 05:05:29 -07002272rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002273 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002274{
2275 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002276 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002277 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002278 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002279 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002280
Daniel Stone4dbadb12012-05-30 16:31:51 +01002281 if (pointer->button_count == 0 &&
2282 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002283 if (shsurf)
2284 weston_matrix_multiply(&shsurf->rotation.rotation,
2285 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002286 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002287 free(rotate);
2288 }
2289}
2290
Scott Moreau447013d2012-02-18 05:05:29 -07002291static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002292 noop_grab_focus,
2293 rotate_grab_motion,
2294 rotate_grab_button,
2295};
2296
2297static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002298rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002299 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002300{
2301 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002302 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002303 struct shell_surface *surface;
2304 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002305 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002306 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002307
2308 if (base_surface == NULL)
2309 return;
2310
2311 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002312 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002313 return;
2314
Pekka Paalanen460099f2012-01-20 16:48:25 +02002315 rotate = malloc(sizeof *rotate);
2316 if (!rotate)
2317 return;
2318
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002319 weston_surface_to_global_float(surface->surface,
2320 surface->surface->geometry.width / 2,
2321 surface->surface->geometry.height / 2,
2322 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002323
Daniel Stone37816df2012-05-16 18:45:18 +01002324 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2325 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002326 r = sqrtf(dx * dx + dy * dy);
2327 if (r > 20.0f) {
2328 struct weston_matrix inverse;
2329
2330 weston_matrix_init(&inverse);
2331 inverse.d[0] = dx / r;
2332 inverse.d[4] = dy / r;
2333 inverse.d[1] = -inverse.d[4];
2334 inverse.d[5] = inverse.d[0];
2335 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002336
2337 weston_matrix_init(&rotate->rotation);
2338 rotate->rotation.d[0] = dx / r;
2339 rotate->rotation.d[4] = -dy / r;
2340 rotate->rotation.d[1] = -rotate->rotation.d[4];
2341 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002342 } else {
2343 weston_matrix_init(&surface->rotation.rotation);
2344 weston_matrix_init(&rotate->rotation);
2345 }
2346
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002347 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2348 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002349}
2350
2351static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002352lower_fullscreen_layer(struct desktop_shell *shell)
2353{
2354 struct workspace *ws;
2355 struct weston_surface *surface, *prev;
2356
2357 ws = get_current_workspace(shell);
2358 wl_list_for_each_reverse_safe(surface, prev,
2359 &shell->fullscreen_layer.surface_list,
2360 layer_link)
2361 weston_surface_restack(surface, &ws->layer.surface_list);
2362}
2363
2364static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002365activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002366 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002367{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002368 struct workspace *ws = get_current_workspace(shell);
2369 struct focus_state *state;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002370
Daniel Stone37816df2012-05-16 18:45:18 +01002371 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002372
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002373 wl_list_for_each(state, &ws->focus_list, link)
2374 if (state->seat == seat)
2375 break;
2376
2377 if (&state->link == &ws->focus_list) {
2378 state = focus_state_create(seat, ws);
2379 if (state == NULL)
2380 return;
2381 }
2382
2383 state->keyboard_focus = es;
2384 wl_list_remove(&state->surface_destroy_listener.link);
2385 wl_signal_add(&es->surface.resource.destroy_signal,
2386 &state->surface_destroy_listener);
2387
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002388 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002389 case SHELL_SURFACE_FULLSCREEN:
2390 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002391 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002392 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002393 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002394 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002395 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002396 lower_fullscreen_layer(shell);
2397 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002398 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002399 }
2400}
2401
Alex Wu21858432012-04-01 20:13:08 +08002402/* no-op func for checking black surface */
2403static void
2404black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2405{
2406}
2407
2408static bool
2409is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2410{
2411 if (es->configure == black_surface_configure) {
2412 if (fs_surface)
2413 *fs_surface = (struct weston_surface *)es->private;
2414 return true;
2415 }
2416 return false;
2417}
2418
Kristian Høgsberg75840622011-09-06 13:48:16 -04002419static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002420click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002421 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002422{
Daniel Stone37816df2012-05-16 18:45:18 +01002423 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002424 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002425 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002426 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002427
Daniel Stone37816df2012-05-16 18:45:18 +01002428 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002429 if (!focus)
2430 return;
2431
Alex Wu21858432012-04-01 20:13:08 +08002432 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002433 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002434
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002435 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2436 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002437
Daniel Stone325fc2d2012-05-30 16:31:58 +01002438 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002439 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002440}
2441
2442static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002443lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002444{
Tiago Vignattibe143262012-04-16 17:31:41 +03002445 struct desktop_shell *shell =
2446 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002447 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002448 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002449
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002450 if (shell->locked) {
2451 wl_list_for_each(output, &shell->compositor->output_list, link)
2452 /* TODO: find a way to jump to other DPMS levels */
2453 if (output->set_dpms)
2454 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002455 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002456 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002457
2458 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002459
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002460 /* Hide all surfaces by removing the fullscreen, panel and
2461 * toplevel layers. This way nothing else can show or receive
2462 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002463
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002464 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002465 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002466 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002467 wl_list_insert(&shell->compositor->cursor_layer.link,
2468 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002469
Pekka Paalanen77346a62011-11-30 16:26:35 +02002470 launch_screensaver(shell);
2471
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002472 /* TODO: disable bindings that should not work while locked. */
2473
2474 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002475}
2476
2477static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002478unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002479{
Tiago Vignattibe143262012-04-16 17:31:41 +03002480 struct desktop_shell *shell =
2481 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002482
Pekka Paalanend81c2162011-11-16 13:47:34 +02002483 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002484 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002485 return;
2486 }
2487
2488 /* If desktop-shell client has gone away, unlock immediately. */
2489 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002490 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002491 return;
2492 }
2493
2494 if (shell->prepare_event_sent)
2495 return;
2496
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002497 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002498 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002499}
2500
2501static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002502show_input_panels(struct wl_listener *listener, void *data)
2503{
2504 struct desktop_shell *shell =
2505 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002506 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002507
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002508 wl_list_for_each_safe(surface, next,
2509 &shell->input_panel.surfaces, layer_link) {
2510 wl_list_remove(&surface->layer_link);
2511 wl_list_insert(&shell->panel_layer.surface_list,
2512 &surface->layer_link);
2513 weston_surface_assign_output(surface);
2514 weston_surface_damage(surface);
2515 weston_slide_run(surface,
2516 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002517 }
2518}
2519
2520static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002521input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2522
2523static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002524hide_input_panels(struct wl_listener *listener, void *data)
2525{
2526 struct desktop_shell *shell =
2527 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002528 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002529
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002530 wl_list_for_each_safe(surface, next,
2531 &shell->panel_layer.surface_list, layer_link)
2532 if (surface->configure == input_panel_configure) {
2533 weston_surface_unmap(surface);
2534 wl_list_insert(&shell->input_panel.surfaces,
2535 &surface->layer_link);
2536 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002537}
2538
2539static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002540center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002541{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002542 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002543 GLfloat x = (mode->width - surface->buffer->width) / 2;
2544 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002545
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002546 weston_surface_configure(surface, output->x + x, output->y + y,
2547 surface->buffer->width,
2548 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002549}
2550
2551static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002552map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002553 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002554{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002555 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002556 struct shell_surface *shsurf = get_shell_surface(surface);
2557 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002558 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002559 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002560 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002561 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002562
Pekka Paalanen60921e52012-01-25 15:55:43 +02002563 surface->geometry.width = width;
2564 surface->geometry.height = height;
2565 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002566
2567 /* initial positioning, see also configure() */
2568 switch (surface_type) {
2569 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002570 weston_surface_set_position(surface, 10 + random() % 400,
2571 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002572 break;
Alex Wu4539b082012-03-01 12:57:46 +08002573 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002574 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002575 shell_map_fullscreen(shsurf);
2576 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002577 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002578 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002579 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002580 weston_surface_set_position(surface, shsurf->output->x,
2581 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002582 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002583 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002584 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002585 case SHELL_SURFACE_NONE:
2586 weston_surface_set_position(surface,
2587 surface->geometry.x + sx,
2588 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002589 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002590 default:
2591 ;
2592 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002593
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002594 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002595 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002596 case SHELL_SURFACE_POPUP:
2597 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002598 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002599 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2600 break;
Alex Wu4539b082012-03-01 12:57:46 +08002601 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002602 case SHELL_SURFACE_NONE:
2603 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002604 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002605 ws = get_current_workspace(shell);
2606 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002607 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002608 }
2609
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002610 if (surface_type != SHELL_SURFACE_NONE) {
2611 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002612 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2613 surface->output = shsurf->output;
2614 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002615
Juan Zhao7bb92f02011-12-15 11:31:51 -05002616 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002617 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002618 if (shsurf->transient.flags ==
2619 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2620 break;
2621 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002622 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002623 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002624 if (!shell->locked) {
2625 wl_list_for_each(seat, &compositor->seat_list, link)
2626 activate(shell, surface, seat);
2627 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002628 break;
2629 default:
2630 break;
2631 }
2632
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002633 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002634 {
2635 switch (shell->win_animation_type) {
2636 case ANIMATION_FADE:
2637 weston_fade_run(surface, NULL, NULL);
2638 break;
2639 case ANIMATION_ZOOM:
2640 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2641 break;
2642 default:
2643 break;
2644 }
2645 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002646}
2647
2648static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002649configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002650 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002651{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002652 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2653 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002654
Pekka Paalanen77346a62011-11-30 16:26:35 +02002655 shsurf = get_shell_surface(surface);
2656 if (shsurf)
2657 surface_type = shsurf->type;
2658
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002659 surface->geometry.x = x;
2660 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002661 surface->geometry.width = width;
2662 surface->geometry.height = height;
2663 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002664
2665 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002666 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002667 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002668 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002669 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002670 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002671 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002672 surface->geometry.x = surface->output->x;
2673 surface->geometry.y = surface->output->y +
2674 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002675 break;
Alex Wu4539b082012-03-01 12:57:46 +08002676 case SHELL_SURFACE_TOPLEVEL:
2677 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002678 default:
2679 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002680 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002681
Alex Wu4539b082012-03-01 12:57:46 +08002682 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002683 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002684 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002685
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002686 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002687 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002688 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002689}
2690
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002691static void
2692shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2693{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002694 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002695 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002696 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002697
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002698 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002699 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002700 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002701 type_changed = 1;
2702 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002703
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002704 if (!weston_surface_is_mapped(es)) {
2705 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002706 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002707 es->geometry.width != es->buffer->width ||
2708 es->geometry.height != es->buffer->height) {
2709 GLfloat from_x, from_y;
2710 GLfloat to_x, to_y;
2711
2712 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2713 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2714 configure(shell, es,
2715 es->geometry.x + to_x - from_x,
2716 es->geometry.y + to_y - from_y,
2717 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002718 }
2719}
2720
Tiago Vignattibe143262012-04-16 17:31:41 +03002721static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002722
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002723static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002724desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002725{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002726 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002727 struct desktop_shell *shell =
2728 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002729
2730 shell->child.process.pid = 0;
2731 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002732
2733 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2734 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002735 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002736 shell->child.deathstamp = time;
2737 shell->child.deathcount = 0;
2738 }
2739
2740 shell->child.deathcount++;
2741 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002742 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002743 return;
2744 }
2745
Martin Minarik6d118362012-06-07 18:01:59 +02002746 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002747 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002748}
2749
2750static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002751launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002752{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002753 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002754
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002755 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002756 &shell->child.process,
2757 shell_exe,
2758 desktop_shell_sigchld);
2759
2760 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002761 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002762 return 0;
2763}
2764
2765static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002766bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2767{
Tiago Vignattibe143262012-04-16 17:31:41 +03002768 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002769
2770 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002771 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002772}
2773
Kristian Høgsberg75840622011-09-06 13:48:16 -04002774static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002775unbind_desktop_shell(struct wl_resource *resource)
2776{
Tiago Vignattibe143262012-04-16 17:31:41 +03002777 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002778
2779 if (shell->locked)
2780 resume_desktop(shell);
2781
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002782 shell->child.desktop_shell = NULL;
2783 shell->prepare_event_sent = false;
2784 free(resource);
2785}
2786
2787static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002788bind_desktop_shell(struct wl_client *client,
2789 void *data, uint32_t version, uint32_t id)
2790{
Tiago Vignattibe143262012-04-16 17:31:41 +03002791 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002792 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002793
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002794 resource = wl_client_add_object(client, &desktop_shell_interface,
2795 &desktop_shell_implementation,
2796 id, shell);
2797
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002798 if (client == shell->child.client) {
2799 resource->destroy = unbind_desktop_shell;
2800 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002801 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002802 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002803
2804 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2805 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002806 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002807}
2808
Pekka Paalanen6e168112011-11-24 11:34:05 +02002809static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002810screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2811{
2812 struct desktop_shell *shell = surface->private;
2813
2814 if (!shell->locked)
2815 return;
2816
2817 center_on_output(surface, surface->output);
2818
2819 if (wl_list_empty(&surface->layer_link)) {
2820 wl_list_insert(shell->lock_layer.surface_list.prev,
2821 &surface->layer_link);
2822 weston_surface_assign_output(surface);
2823 shell->compositor->idle_time = shell->screensaver.duration;
2824 weston_compositor_wake(shell->compositor);
2825 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2826 }
2827}
2828
2829static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002830screensaver_set_surface(struct wl_client *client,
2831 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002832 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002833 struct wl_resource *output_resource)
2834{
Tiago Vignattibe143262012-04-16 17:31:41 +03002835 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002836 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002837 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002838
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002839 surface->configure = screensaver_configure;
2840 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002841 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002842}
2843
2844static const struct screensaver_interface screensaver_implementation = {
2845 screensaver_set_surface
2846};
2847
2848static void
2849unbind_screensaver(struct wl_resource *resource)
2850{
Tiago Vignattibe143262012-04-16 17:31:41 +03002851 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002852
Pekka Paalanen77346a62011-11-30 16:26:35 +02002853 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002854 free(resource);
2855}
2856
2857static void
2858bind_screensaver(struct wl_client *client,
2859 void *data, uint32_t version, uint32_t id)
2860{
Tiago Vignattibe143262012-04-16 17:31:41 +03002861 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002862 struct wl_resource *resource;
2863
2864 resource = wl_client_add_object(client, &screensaver_interface,
2865 &screensaver_implementation,
2866 id, shell);
2867
Pekka Paalanen77346a62011-11-30 16:26:35 +02002868 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002869 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002870 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002871 return;
2872 }
2873
2874 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2875 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002876 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002877}
2878
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002879static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002880input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2881{
2882 struct weston_mode *mode = surface->output->current;
2883 GLfloat x = (mode->width - surface->buffer->width) / 2;
2884 GLfloat y = mode->height - surface->buffer->height;
2885
2886 /* Don't map the input panel here, wait for
2887 * show_input_panels signal. */
2888
2889 weston_surface_configure(surface,
2890 surface->output->x + x,
2891 surface->output->y + y,
2892 surface->buffer->width,
2893 surface->buffer->height);
2894}
2895
2896static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002897input_panel_set_surface(struct wl_client *client,
2898 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002899 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002900 struct wl_resource *output_resource)
2901{
2902 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002903 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002904 struct weston_output *output = output_resource->data;
2905
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002906 surface->configure = input_panel_configure;
2907 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002908 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002909 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002910}
2911
2912static const struct input_panel_interface input_panel_implementation = {
2913 input_panel_set_surface
2914};
2915
2916static void
2917unbind_input_panel(struct wl_resource *resource)
2918{
2919 struct desktop_shell *shell = resource->data;
2920
2921 shell->input_panel.binding = NULL;
2922 free(resource);
2923}
2924
2925static void
2926bind_input_panel(struct wl_client *client,
2927 void *data, uint32_t version, uint32_t id)
2928{
2929 struct desktop_shell *shell = data;
2930 struct wl_resource *resource;
2931
2932 resource = wl_client_add_object(client, &input_panel_interface,
2933 &input_panel_implementation,
2934 id, shell);
2935
2936 if (shell->input_panel.binding == NULL) {
2937 resource->destroy = unbind_input_panel;
2938 shell->input_panel.binding = resource;
2939 return;
2940 }
2941
2942 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2943 "interface object already bound");
2944 wl_resource_destroy(resource);
2945}
2946
Kristian Høgsberg07045392012-02-19 18:52:44 -05002947struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002948 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002949 struct weston_surface *current;
2950 struct wl_listener listener;
2951 struct wl_keyboard_grab grab;
2952};
2953
2954static void
2955switcher_next(struct switcher *switcher)
2956{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002957 struct weston_surface *surface;
2958 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002959 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002960 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002961
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002962 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002963 switch (get_shell_surface_type(surface)) {
2964 case SHELL_SURFACE_TOPLEVEL:
2965 case SHELL_SURFACE_FULLSCREEN:
2966 case SHELL_SURFACE_MAXIMIZED:
2967 if (first == NULL)
2968 first = surface;
2969 if (prev == switcher->current)
2970 next = surface;
2971 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002972 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002973 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002974 weston_surface_damage(surface);
2975 break;
2976 default:
2977 break;
2978 }
Alex Wu1659daa2012-04-01 20:13:09 +08002979
2980 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002981 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002982 surface->geometry.dirty = 1;
2983 weston_surface_damage(surface);
2984 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002985 }
2986
2987 if (next == NULL)
2988 next = first;
2989
Alex Wu07b26062012-03-12 16:06:01 +08002990 if (next == NULL)
2991 return;
2992
Kristian Høgsberg07045392012-02-19 18:52:44 -05002993 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002994 wl_signal_add(&next->surface.resource.destroy_signal,
2995 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002996
2997 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002998 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002999
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003000 shsurf = get_shell_surface(switcher->current);
3001 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003002 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003003}
3004
3005static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003006switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003007{
3008 struct switcher *switcher =
3009 container_of(listener, struct switcher, listener);
3010
3011 switcher_next(switcher);
3012}
3013
3014static void
Daniel Stone351eb612012-05-31 15:27:47 -04003015switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003016{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003017 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003018 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003019 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003020
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003021 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003022 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003023 weston_surface_damage(surface);
3024 }
3025
Alex Wu07b26062012-03-12 16:06:01 +08003026 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003027 activate(switcher->shell, switcher->current,
3028 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003029 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003030 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003031 free(switcher);
3032}
3033
3034static void
3035switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003036 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003037{
3038 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003039 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003040
Daniel Stonec9785ea2012-05-30 16:31:52 +01003041 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003042 switcher_next(switcher);
3043}
3044
3045static void
3046switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3047 uint32_t mods_depressed, uint32_t mods_latched,
3048 uint32_t mods_locked, uint32_t group)
3049{
3050 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003051 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003052
Daniel Stone351eb612012-05-31 15:27:47 -04003053 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3054 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003055}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003056
3057static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003058 switcher_key,
3059 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003060};
3061
3062static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003063switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3064 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003065{
Tiago Vignattibe143262012-04-16 17:31:41 +03003066 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003067 struct switcher *switcher;
3068
3069 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003070 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003071 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003072 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003073 wl_list_init(&switcher->listener.link);
3074
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003075 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003076 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003077 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3078 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003079 switcher_next(switcher);
3080}
3081
Pekka Paalanen3c647232011-12-22 13:43:43 +02003082static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003083backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3084 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003085{
3086 struct weston_compositor *compositor = data;
3087 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003088 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003089
3090 /* TODO: we're limiting to simple use cases, where we assume just
3091 * control on the primary display. We'd have to extend later if we
3092 * ever get support for setting backlights on random desktop LCD
3093 * panels though */
3094 output = get_default_output(compositor);
3095 if (!output)
3096 return;
3097
3098 if (!output->set_backlight)
3099 return;
3100
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003101 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3102 backlight_new = output->backlight_current - 25;
3103 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3104 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003105
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003106 if (backlight_new < 5)
3107 backlight_new = 5;
3108 if (backlight_new > 255)
3109 backlight_new = 255;
3110
3111 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003112 output->set_backlight(output, output->backlight_current);
3113}
3114
3115static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003116debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3117 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003118{
Tiago Vignattibe143262012-04-16 17:31:41 +03003119 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003120 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003121 struct weston_surface *surface;
3122
3123 if (shell->debug_repaint_surface) {
3124 weston_surface_destroy(shell->debug_repaint_surface);
3125 shell->debug_repaint_surface = NULL;
3126 } else {
3127 surface = weston_surface_create(compositor);
3128 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3129 weston_surface_configure(surface, 0, 0, 8192, 8192);
3130 wl_list_insert(&compositor->fade_layer.surface_list,
3131 &surface->layer_link);
3132 weston_surface_assign_output(surface);
3133 pixman_region32_init(&surface->input);
3134
3135 /* Here's the dirty little trick that makes the
3136 * repaint debugging work: we force an
3137 * update_transform first to update dependent state
3138 * and clear the geometry.dirty bit. Then we clear
3139 * the surface damage so it only gets repainted
3140 * piecewise as we repaint other things. */
3141
3142 weston_surface_update_transform(surface);
3143 pixman_region32_fini(&surface->damage);
3144 pixman_region32_init(&surface->damage);
3145 shell->debug_repaint_surface = surface;
3146 }
3147}
3148
3149static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003150force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3151 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003152{
3153 struct wl_client *client;
3154 pid_t pid;
3155 uid_t uid;
3156 gid_t gid;
3157
Daniel Stone325fc2d2012-05-30 16:31:58 +01003158 client = seat->keyboard->focus->resource.client;
3159 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003160
Daniel Stone325fc2d2012-05-30 16:31:58 +01003161 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003162}
3163
3164static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003165workspace_up_binding(struct wl_seat *seat, uint32_t time,
3166 uint32_t key, void *data)
3167{
3168 struct desktop_shell *shell = data;
3169 unsigned int new_index = shell->workspaces.current;
3170
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003171 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003172 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003173 if (new_index != 0)
3174 new_index--;
3175
3176 change_workspace(shell, new_index);
3177}
3178
3179static void
3180workspace_down_binding(struct wl_seat *seat, uint32_t time,
3181 uint32_t key, void *data)
3182{
3183 struct desktop_shell *shell = data;
3184 unsigned int new_index = shell->workspaces.current;
3185
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003186 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003187 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003188 if (new_index < shell->workspaces.num - 1)
3189 new_index++;
3190
3191 change_workspace(shell, new_index);
3192}
3193
3194static void
3195workspace_f_binding(struct wl_seat *seat, uint32_t time,
3196 uint32_t key, void *data)
3197{
3198 struct desktop_shell *shell = data;
3199 unsigned int new_index;
3200
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003201 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003202 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003203 new_index = key - KEY_F1;
3204 if (new_index >= shell->workspaces.num)
3205 new_index = shell->workspaces.num - 1;
3206
3207 change_workspace(shell, new_index);
3208}
3209
3210
3211static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003212shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003213{
Tiago Vignattibe143262012-04-16 17:31:41 +03003214 struct desktop_shell *shell =
3215 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003216 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003217
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003218 if (shell->child.client)
3219 wl_client_destroy(shell->child.client);
3220
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003221 wl_list_remove(&shell->lock_listener.link);
3222 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003223 wl_list_remove(&shell->show_input_panel_listener.link);
3224 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003225
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003226 wl_array_for_each(ws, &shell->workspaces.array)
3227 workspace_destroy(*ws);
3228 wl_array_release(&shell->workspaces.array);
3229
Pekka Paalanen3c647232011-12-22 13:43:43 +02003230 free(shell->screensaver.path);
3231 free(shell);
3232}
3233
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003234static void
3235shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3236{
3237 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003238 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003239
3240 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003241 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3242 MODIFIER_CTRL | MODIFIER_ALT,
3243 terminate_binding, ec);
3244 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3245 click_to_activate_binding,
3246 shell);
3247 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3248 MODIFIER_SUPER | MODIFIER_ALT,
3249 surface_opacity_binding, NULL);
3250 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3251 MODIFIER_SUPER, zoom_axis_binding,
3252 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003253
3254 /* configurable bindings */
3255 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003256 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3257 zoom_key_binding, NULL);
3258 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3259 zoom_key_binding, NULL);
3260 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3261 shell);
3262 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3263 resize_binding, shell);
3264 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3265 rotate_binding, NULL);
3266 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3267 shell);
3268 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3269 ec);
3270 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3271 backlight_binding, ec);
3272 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3273 ec);
3274 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3275 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003276 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003277 debug_repaint_binding, shell);
3278 weston_compositor_add_key_binding(ec, KEY_K, mod,
3279 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003280 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3281 workspace_up_binding, shell);
3282 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3283 workspace_down_binding, shell);
3284
3285 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3286 if (shell->workspaces.num > 1) {
3287 num_workspace_bindings = shell->workspaces.num;
3288 if (num_workspace_bindings > 6)
3289 num_workspace_bindings = 6;
3290 for (i = 0; i < num_workspace_bindings; i++)
3291 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3292 workspace_f_binding,
3293 shell);
3294 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003295}
3296
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003297int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003298shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003299
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003300WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003301shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003302{
Tiago Vignattibe143262012-04-16 17:31:41 +03003303 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003304 struct workspace **pws;
3305 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003306
3307 shell = malloc(sizeof *shell);
3308 if (shell == NULL)
3309 return -1;
3310
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003311 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003312 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003313
3314 shell->destroy_listener.notify = shell_destroy;
3315 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3316 shell->lock_listener.notify = lock;
3317 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3318 shell->unlock_listener.notify = unlock;
3319 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003320 shell->show_input_panel_listener.notify = show_input_panels;
3321 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3322 shell->hide_input_panel_listener.notify = hide_input_panels;
3323 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003324 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003325 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003326 ec->shell_interface.create_shell_surface = create_shell_surface;
3327 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003328 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003329 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003330 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003331
Pekka Paalanen77346a62011-11-30 16:26:35 +02003332 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003333 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003334
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003335 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3336 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003337 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3338 weston_layer_init(&shell->lock_layer, NULL);
3339
3340 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003341
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003342 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003343
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003344 for (i = 0; i < shell->workspaces.num; i++) {
3345 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3346 if (pws == NULL)
3347 return -1;
3348
3349 *pws = workspace_create();
3350 if (*pws == NULL)
3351 return -1;
3352 }
3353 activate_workspace(shell, 0);
3354
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003355 wl_list_init(&shell->workspaces.animation.link);
3356 shell->workspaces.animation.frame = animate_workspace_change_frame;
3357
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003358 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3359 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003360 return -1;
3361
Kristian Høgsberg75840622011-09-06 13:48:16 -04003362 if (wl_display_add_global(ec->wl_display,
3363 &desktop_shell_interface,
3364 shell, bind_desktop_shell) == NULL)
3365 return -1;
3366
Pekka Paalanen6e168112011-11-24 11:34:05 +02003367 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3368 shell, bind_screensaver) == NULL)
3369 return -1;
3370
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003371 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3372 shell, bind_input_panel) == NULL)
3373 return -1;
3374
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003375 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003376 if (launch_desktop_shell_process(shell) != 0)
3377 return -1;
3378
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003379 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003380 if (ec->seat->seat.pointer)
3381 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3382 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003383
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003384 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003385
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003386 return 0;
3387}