blob: a0d208891f39f773c9fd9c259cb1ebbb1fd3318d [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"
Martin Minarik6d118362012-06-07 18:01:59 +020039#include "log.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020042#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043
Juan Zhaoe10d2792012-04-25 19:09:52 +080044enum animation_type {
45 ANIMATION_NONE,
46
47 ANIMATION_ZOOM,
48 ANIMATION_FADE
49};
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
53 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
Jonas Ådahl04769742012-06-13 00:01:24 +0200188 struct focus_state *focus_state;
189
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500190 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100192 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400193
194 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200195};
196
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300197struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700198 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199 struct shell_surface *shsurf;
200 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300201 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300202};
203
204struct weston_move_grab {
205 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100206 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500207};
208
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500211 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400213 GLfloat x;
214 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215 } center;
216};
217
Alex Wubd3354b2012-04-17 17:20:49 +0800218static struct shell_surface *
219get_shell_surface(struct weston_surface *surface);
220
221static struct desktop_shell *
222shell_surface_get_shell(struct shell_surface *shsurf);
223
224static bool
225shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
226{
227 struct desktop_shell *shell;
228 struct weston_surface *top_fs_es;
229
230 shell = shell_surface_get_shell(shsurf);
231
232 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
233 return false;
234
235 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
236 struct weston_surface,
237 layer_link);
238 return (shsurf == get_shell_surface(top_fs_es));
239}
240
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500241static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400242destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300243{
244 struct shell_grab *grab;
245
246 grab = container_of(listener, struct shell_grab,
247 shsurf_destroy_listener);
248
249 grab->shsurf = NULL;
250}
251
252static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300253shell_grab_start(struct shell_grab *grab,
254 const struct wl_pointer_grab_interface *interface,
255 struct shell_surface *shsurf,
256 struct wl_pointer *pointer,
257 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300258{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300259 struct desktop_shell *shell = shsurf->shell;
260
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300261 grab->grab.interface = interface;
262 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400263 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
264 wl_signal_add(&shsurf->resource.destroy_signal,
265 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300266
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300267 grab->pointer = pointer;
268 grab->grab.focus = &shsurf->surface->surface;
269
270 wl_pointer_start_grab(pointer, &grab->grab);
271 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
272 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
273 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274}
275
276static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300277shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300278{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400279 if (grab->shsurf)
280 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300281
282 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283}
284
285static void
Alex Wu4539b082012-03-01 12:57:46 +0800286center_on_output(struct weston_surface *surface,
287 struct weston_output *output);
288
Daniel Stone496ca172012-05-30 16:31:42 +0100289static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300290get_modifier(char *modifier)
291{
292 if (!modifier)
293 return MODIFIER_SUPER;
294
295 if (!strcmp("ctrl", modifier))
296 return MODIFIER_CTRL;
297 else if (!strcmp("alt", modifier))
298 return MODIFIER_ALT;
299 else if (!strcmp("super", modifier))
300 return MODIFIER_SUPER;
301 else
302 return MODIFIER_SUPER;
303}
304
Juan Zhaoe10d2792012-04-25 19:09:52 +0800305static enum animation_type
306get_animation_type(char *animation)
307{
308 if (!animation)
309 return ANIMATION_NONE;
310
311 if (!strcmp("zoom", animation))
312 return ANIMATION_ZOOM;
313 else if (!strcmp("fade", animation))
314 return ANIMATION_FADE;
315 else
316 return ANIMATION_NONE;
317}
318
Alex Wu4539b082012-03-01 12:57:46 +0800319static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300320shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200321{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200322 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200323 char *path = NULL;
324 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200325 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300326 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800327 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200328
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400329 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300330 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800331 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200332 { "num-workspaces",
333 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200334 };
335
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400336 struct config_key saver_keys[] = {
337 { "path", CONFIG_KEY_STRING, &path },
338 { "duration", CONFIG_KEY_INTEGER, &duration },
339 };
340
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200341 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400342 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200343 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
344 };
345
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200346 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500347 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200348 free(config_file);
349
Pekka Paalanen7296e792011-12-07 16:22:00 +0200350 shell->screensaver.path = path;
351 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300352 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800353 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200354 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
355}
356
357static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200358focus_state_destroy(struct focus_state *state)
359{
360 wl_list_remove(&state->seat_destroy_listener.link);
361 wl_list_remove(&state->surface_destroy_listener.link);
362 free(state);
363}
364
365static void
366focus_state_seat_destroy(struct wl_listener *listener, void *data)
367{
368 struct focus_state *state = container_of(listener,
369 struct focus_state,
370 seat_destroy_listener);
371
372 wl_list_remove(&state->link);
373 focus_state_destroy(state);
374}
375
376static void
377focus_state_surface_destroy(struct wl_listener *listener, void *data)
378{
379 struct focus_state *state = container_of(listener,
380 struct focus_state,
381 seat_destroy_listener);
382
383 wl_list_remove(&state->link);
384 focus_state_destroy(state);
385}
386
387static struct focus_state *
388focus_state_create(struct weston_seat *seat)
389{
390 struct wl_keyboard *keyboard = seat->seat.keyboard;
391 struct focus_state *state;
392 struct wl_surface *surface;
393 struct shell_surface *shsurf;
394
395 state = malloc(sizeof *state);
396 if (state == NULL)
397 return NULL;
398
399 surface = keyboard->focus;
400 shsurf = get_shell_surface((struct weston_surface *)keyboard->focus);
401 shsurf->focus_state = state;
402
403 state->seat = seat;
404 state->keyboard_focus = shsurf->surface;
405 wl_list_init(&state->link);
406
407 state->seat_destroy_listener.notify = focus_state_seat_destroy;
408 state->surface_destroy_listener.notify = focus_state_surface_destroy;
409 wl_signal_add(&seat->seat.destroy_signal,
410 &state->seat_destroy_listener);
411 wl_signal_add(&surface->resource.destroy_signal,
412 &state->surface_destroy_listener);
413
414 return state;
415}
416
417static void
418pop_focus_state(struct desktop_shell *shell, struct workspace *ws)
419{
420 struct focus_state *state, *next;
421
422 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
423 if (state->keyboard_focus)
424 wl_keyboard_set_focus(state->seat->seat.keyboard,
425 &state->keyboard_focus->surface);
426
427 focus_state_destroy(state);
428 }
429 wl_list_init(&ws->focus_list);
430}
431
432static void
433push_focus_state(struct desktop_shell *shell, struct workspace *ws)
434{
435 struct weston_seat *seat;
436 struct focus_state *state;
437 struct wl_keyboard *keyboard;
438
439 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
440 keyboard = seat->seat.keyboard;
441 if (keyboard && keyboard->focus) {
442 state = focus_state_create(seat);
443 if (state == NULL)
444 return;
445
446 wl_list_insert(&ws->focus_list, &state->link);
447
448 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
449 }
450 }
451}
452
453static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200454workspace_destroy(struct workspace *ws)
455{
Jonas Ådahl04769742012-06-13 00:01:24 +0200456 struct focus_state *state, *next;
457
458 wl_list_for_each_safe(state, next, &ws->focus_list, link)
459 focus_state_destroy(state);
460
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200461 free(ws);
462}
463
Jonas Ådahl04769742012-06-13 00:01:24 +0200464static void
465seat_destroyed(struct wl_listener *listener, void *data)
466{
467 struct weston_seat *seat = data;
468 struct focus_state *state, *next;
469 struct workspace *ws = container_of(listener,
470 struct workspace,
471 seat_destroyed_listener);
472
473 wl_list_for_each_safe(state, next, &ws->focus_list, link)
474 if (state->seat == seat)
475 wl_list_remove(&state->link);
476}
477
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200478static struct workspace *
479workspace_create(void)
480{
481 struct workspace *ws = malloc(sizeof *ws);
482 if (ws == NULL)
483 return NULL;
484
485 weston_layer_init(&ws->layer, NULL);
486
Jonas Ådahl04769742012-06-13 00:01:24 +0200487 wl_list_init(&ws->focus_list);
488 wl_list_init(&ws->seat_destroyed_listener.link);
489 ws->seat_destroyed_listener.notify = seat_destroyed;
490
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200491 return ws;
492}
493
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200494static int
495workspace_is_empty(struct workspace *ws)
496{
497 return wl_list_empty(&ws->layer.surface_list);
498}
499
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200500static struct workspace *
501get_workspace(struct desktop_shell *shell, unsigned int index)
502{
503 struct workspace **pws = shell->workspaces.array.data;
504 pws += index;
505 return *pws;
506}
507
508static struct workspace *
509get_current_workspace(struct desktop_shell *shell)
510{
511 return get_workspace(shell, shell->workspaces.current);
512}
513
514static void
515activate_workspace(struct desktop_shell *shell, unsigned int index)
516{
517 struct workspace *ws;
518
519 ws = get_workspace(shell, index);
520 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
521
522 shell->workspaces.current = index;
523}
524
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200525static unsigned int
526get_output_height(struct weston_output *output)
527{
528 return abs(output->region.extents.y1 - output->region.extents.y2);
529}
530
531static void
532surface_translate(struct weston_surface *surface, double d)
533{
534 struct shell_surface *shsurf = get_shell_surface(surface);
535 struct weston_transform *transform;
536
537 transform = &shsurf->workspace_transform;
538 if (wl_list_empty(&transform->link))
539 wl_list_insert(surface->geometry.transformation_list.prev,
540 &shsurf->workspace_transform.link);
541
542 weston_matrix_init(&shsurf->workspace_transform.matrix);
543 weston_matrix_translate(&shsurf->workspace_transform.matrix,
544 0.0, d, 0.0);
545 surface->geometry.dirty = 1;
546}
547
548static void
549workspace_translate_out(struct workspace *ws, double fraction)
550{
551 struct weston_surface *surface;
552 unsigned int height;
553 double d;
554
555 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
556 height = get_output_height(surface->output);
557 d = height * fraction;
558
559 surface_translate(surface, d);
560 }
561}
562
563static void
564workspace_translate_in(struct workspace *ws, double fraction)
565{
566 struct weston_surface *surface;
567 unsigned int height;
568 double d;
569
570 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
571 height = get_output_height(surface->output);
572
573 if (fraction > 0)
574 d = -(height - height * fraction);
575 else
576 d = height + height * fraction;
577
578 surface_translate(surface, d);
579 }
580}
581
582static void
583workspace_damage_all_surfaces(struct workspace *ws)
584{
585 struct weston_surface *surface;
586
587 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
588 weston_surface_damage(surface);
589}
590
591static void
592reverse_workspace_change_animation(struct desktop_shell *shell,
593 unsigned int index,
594 struct workspace *from,
595 struct workspace *to)
596{
597 shell->workspaces.current = index;
598
599 shell->workspaces.anim_to = to;
600 shell->workspaces.anim_from = from;
601 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
602 shell->workspaces.anim_timestamp = 0;
603
Jonas Ådahl04769742012-06-13 00:01:24 +0200604 push_focus_state(shell, from);
605 pop_focus_state(shell, to);
606
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200607 workspace_damage_all_surfaces(from);
608 workspace_damage_all_surfaces(to);
609}
610
611static void
612workspace_deactivate_transforms(struct workspace *ws)
613{
614 struct weston_surface *surface;
615 struct shell_surface *shsurf;
616
617 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
618 shsurf = get_shell_surface(surface);
619 wl_list_remove(&shsurf->workspace_transform.link);
620 wl_list_init(&shsurf->workspace_transform.link);
621 shsurf->surface->geometry.dirty = 1;
622 }
623}
624
625static void
626finish_workspace_change_animation(struct desktop_shell *shell,
627 struct workspace *from,
628 struct workspace *to)
629{
630 workspace_damage_all_surfaces(from);
631 workspace_damage_all_surfaces(to);
632
633 wl_list_remove(&shell->workspaces.animation.link);
634 workspace_deactivate_transforms(from);
635 workspace_deactivate_transforms(to);
636 shell->workspaces.anim_to = NULL;
637
638 wl_list_remove(&shell->workspaces.anim_from->layer.link);
639}
640
641static void
642animate_workspace_change_frame(struct weston_animation *animation,
643 struct weston_output *output, uint32_t msecs)
644{
645 struct desktop_shell *shell =
646 container_of(animation, struct desktop_shell,
647 workspaces.animation);
648 struct workspace *from = shell->workspaces.anim_from;
649 struct workspace *to = shell->workspaces.anim_to;
650 uint32_t t;
651 double x, y;
652
653 if (workspace_is_empty(from) && workspace_is_empty(to)) {
654 finish_workspace_change_animation(shell, from, to);
655 return;
656 }
657
658 if (shell->workspaces.anim_timestamp == 0) {
659 if (shell->workspaces.anim_current == 0.0)
660 shell->workspaces.anim_timestamp = msecs;
661 else
662 shell->workspaces.anim_timestamp =
663 msecs -
664 /* Invers of movement function 'y' below. */
665 (asin(1.0 - shell->workspaces.anim_current) *
666 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
667 M_2_PI);
668 }
669
670 t = msecs - shell->workspaces.anim_timestamp;
671
672 /*
673 * x = [0, π/2]
674 * y(x) = sin(x)
675 */
676 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
677 y = sin(x);
678
679 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
680 workspace_damage_all_surfaces(from);
681 workspace_damage_all_surfaces(to);
682
683 workspace_translate_out(from, shell->workspaces.anim_dir * y);
684 workspace_translate_in(to, shell->workspaces.anim_dir * y);
685 shell->workspaces.anim_current = y;
686
687 workspace_damage_all_surfaces(from);
688 workspace_damage_all_surfaces(to);
689 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200690 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200691 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200692}
693
694static void
695animate_workspace_change(struct desktop_shell *shell,
696 unsigned int index,
697 struct workspace *from,
698 struct workspace *to)
699{
700 struct weston_output *output;
701
702 int dir;
703
704 if (index > shell->workspaces.current)
705 dir = -1;
706 else
707 dir = 1;
708
709 shell->workspaces.current = index;
710
711 shell->workspaces.anim_dir = dir;
712 shell->workspaces.anim_from = from;
713 shell->workspaces.anim_to = to;
714 shell->workspaces.anim_current = 0.0;
715 shell->workspaces.anim_timestamp = 0;
716
717 output = container_of(shell->compositor->output_list.next,
718 struct weston_output, link);
719 wl_list_insert(&output->animation_list,
720 &shell->workspaces.animation.link);
721
722 wl_list_insert(&from->layer.link, &to->layer.link);
723
724 workspace_translate_in(to, 0);
725
Jonas Ådahl04769742012-06-13 00:01:24 +0200726 push_focus_state(shell, from);
727 pop_focus_state(shell, to);
728
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200729 workspace_damage_all_surfaces(from);
730 workspace_damage_all_surfaces(to);
731}
732
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200733static void
734change_workspace(struct desktop_shell *shell, unsigned int index)
735{
736 struct workspace *from;
737 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200738
739 if (index == shell->workspaces.current)
740 return;
741
742 /* Don't change workspace when there is any fullscreen surfaces. */
743 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
744 return;
745
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200746 from = get_current_workspace(shell);
747 to = get_workspace(shell, index);
748
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200749 if (shell->workspaces.anim_from == to &&
750 shell->workspaces.anim_to == from) {
751 reverse_workspace_change_animation(shell, index, from, to);
752 return;
753 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200754
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200755 if (shell->workspaces.anim_to != NULL)
756 finish_workspace_change_animation(shell,
757 shell->workspaces.anim_from,
758 shell->workspaces.anim_to);
759
760 if (workspace_is_empty(to) && workspace_is_empty(from)) {
761 shell->workspaces.current = index;
762 wl_list_insert(&from->layer.link, &to->layer.link);
763 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200764
765 push_focus_state(shell, from);
766 pop_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200767 }
768 else
769 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200770}
771
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200772static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400773noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100774 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500775{
776 grab->focus = NULL;
777}
778
779static void
Scott Moreau447013d2012-02-18 05:05:29 -0700780move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100781 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500782{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500783 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100784 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300785 struct shell_surface *shsurf = move->base.shsurf;
786 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100787 int dx = wl_fixed_to_int(pointer->x + move->dx);
788 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300789
790 if (!shsurf)
791 return;
792
793 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500794
Daniel Stone103db7f2012-05-08 17:17:55 +0100795 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200796 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400797
798 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500799}
800
801static void
Scott Moreau447013d2012-02-18 05:05:29 -0700802move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100803 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500804{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300805 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
806 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100807 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100808 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500809
Daniel Stone4dbadb12012-05-30 16:31:51 +0100810 if (pointer->button_count == 0 &&
811 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300812 shell_grab_end(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100813 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500814 free(grab);
815 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500816}
817
Scott Moreau447013d2012-02-18 05:05:29 -0700818static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500819 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500820 move_grab_motion,
821 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500822};
823
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600824static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400825busy_cursor_grab_focus(struct wl_pointer_grab *base,
826 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600827{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400828 struct shell_grab *grab = (struct shell_grab *) base;
829 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600830
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400831 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300832 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400833 wl_pointer_end_grab(pointer);
834 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600835 }
836}
837
838static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400839busy_cursor_grab_motion(struct wl_pointer_grab *grab,
840 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600841{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400842}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600843
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400844static void
845busy_cursor_grab_button(struct wl_pointer_grab *grab,
846 uint32_t time, uint32_t button, uint32_t state)
847{
848}
849
850static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
851 busy_cursor_grab_focus,
852 busy_cursor_grab_motion,
853 busy_cursor_grab_button,
854};
855
856static void
857set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
858{
859 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400860
861 grab = malloc(sizeof *grab);
862 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600863 return;
864
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300865 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
866 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400867}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600868
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400869static void
870end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
871{
872 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
873
874 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300875 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400876 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600877 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600878}
879
Scott Moreau9521d5e2012-04-19 13:06:17 -0600880static void
881ping_timer_destroy(struct shell_surface *shsurf)
882{
883 if (!shsurf || !shsurf->ping_timer)
884 return;
885
886 if (shsurf->ping_timer->source)
887 wl_event_source_remove(shsurf->ping_timer->source);
888
889 free(shsurf->ping_timer);
890 shsurf->ping_timer = NULL;
891}
892
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400893static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600894ping_timeout_handler(void *data)
895{
896 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400897 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600898
Scott Moreau9521d5e2012-04-19 13:06:17 -0600899 /* Client is not responding */
900 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400901
902 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
903 if (seat->seat.pointer->focus == &shsurf->surface->surface)
904 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600905
906 return 1;
907}
908
909static void
910ping_handler(struct weston_surface *surface, uint32_t serial)
911{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400912 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600913 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400914 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600915
916 if (!shsurf)
917 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400918 if (!shsurf->resource.client)
919 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600920
921 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300922 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600923 if (!shsurf->ping_timer)
924 return;
925
926 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600927 loop = wl_display_get_event_loop(surface->compositor->wl_display);
928 shsurf->ping_timer->source =
929 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
930 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
931
932 wl_shell_surface_send_ping(&shsurf->resource, serial);
933 }
934}
935
936static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400937handle_pointer_focus(struct wl_listener *listener, void *data)
938{
939 struct wl_pointer *pointer = data;
940 struct weston_surface *surface =
941 (struct weston_surface *) pointer->focus;
942 struct weston_compositor *compositor;
943 struct shell_surface *shsurf;
944 uint32_t serial;
945
946 if (!surface)
947 return;
948
949 compositor = surface->compositor;
950 shsurf = get_shell_surface(surface);
951
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300952 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400953 set_busy_cursor(shsurf, pointer);
954 } else {
955 serial = wl_display_next_serial(compositor->wl_display);
956 ping_handler(surface, serial);
957 }
958}
959
960static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600961shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
962 uint32_t serial)
963{
964 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400965 struct desktop_shell *shell = shsurf->shell;
966 struct weston_seat *seat;
967 struct weston_compositor *ec = shsurf->surface->compositor;
968 struct wl_pointer *pointer;
969 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600970
Scott Moreauff1db4a2012-04-17 19:06:18 -0600971 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400972 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600973 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400974 if (was_unresponsive) {
975 /* Received pong from previously unresponsive client */
976 wl_list_for_each(seat, &ec->seat_list, link) {
977 pointer = seat->seat.pointer;
978 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300979 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400980 pointer->current ==
981 &shsurf->surface->surface)
982 end_busy_cursor(shsurf, pointer);
983 }
984 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600985 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600986 }
987}
988
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400989static void
990shell_surface_set_title(struct wl_client *client,
991 struct wl_resource *resource, const char *title)
992{
993 struct shell_surface *shsurf = resource->data;
994
995 free(shsurf->title);
996 shsurf->title = strdup(title);
997}
998
999static void
1000shell_surface_set_class(struct wl_client *client,
1001 struct wl_resource *resource, const char *class)
1002{
1003 struct shell_surface *shsurf = resource->data;
1004
1005 free(shsurf->class);
1006 shsurf->class = strdup(class);
1007}
1008
Scott Moreauff1db4a2012-04-17 19:06:18 -06001009static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001010surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001011{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001012 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001013
1014 if (!shsurf)
1015 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001016
Ander Conselvan de Oliveira00d17bb2012-06-28 18:08:06 +03001017 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1018 return 0;
1019
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001020 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001021 if (!move)
1022 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001024 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001025 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001026 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001027 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001028
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001029 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1030 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001031
1032 return 0;
1033}
1034
1035static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001036shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001037 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001038{
Daniel Stone37816df2012-05-16 18:45:18 +01001039 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001040 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001041
Daniel Stone37816df2012-05-16 18:45:18 +01001042 if (ws->seat.pointer->button_count == 0 ||
1043 ws->seat.pointer->grab_serial != serial ||
1044 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001045 return;
1046
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001047 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001048 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001049}
1050
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001051struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001052 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001053 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001054 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001055};
1056
1057static void
Scott Moreau447013d2012-02-18 05:05:29 -07001058resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001059 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001060{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001061 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001062 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001063 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001064 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001065 wl_fixed_t from_x, from_y;
1066 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001067
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001068 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001069 return;
1070
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001071 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001072 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001073 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001074 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001075 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001076
Daniel Stone103db7f2012-05-08 17:17:55 +01001077 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001078 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001079 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001080 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001081 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001082 }
1083
Daniel Stone103db7f2012-05-08 17:17:55 +01001084 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001085 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001086 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001087 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001088 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001089 }
1090
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001091 shsurf->client->send_configure(shsurf->surface,
1092 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001093}
1094
1095static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001096send_configure(struct weston_surface *surface,
1097 uint32_t edges, int32_t width, int32_t height)
1098{
1099 struct shell_surface *shsurf = get_shell_surface(surface);
1100
1101 wl_shell_surface_send_configure(&shsurf->resource,
1102 edges, width, height);
1103}
1104
1105static const struct weston_shell_client shell_client = {
1106 send_configure
1107};
1108
1109static void
Scott Moreau447013d2012-02-18 05:05:29 -07001110resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001111 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001112{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001113 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001114 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001115 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001116
Daniel Stone4dbadb12012-05-30 16:31:51 +01001117 if (pointer->button_count == 0 &&
1118 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001119 shell_grab_end(&resize->base);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001120 free(grab);
1121 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001122}
1123
Scott Moreau447013d2012-02-18 05:05:29 -07001124static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001125 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001126 resize_grab_motion,
1127 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001128};
1129
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001130static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001131surface_resize(struct shell_surface *shsurf,
1132 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001133{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001134 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001135
Alex Wu4539b082012-03-01 12:57:46 +08001136 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1137 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001138
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001139 if (edges == 0 || edges > 15 ||
1140 (edges & 3) == 3 || (edges & 12) == 12)
1141 return 0;
1142
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001143 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001144 if (!resize)
1145 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001146
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001147 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001148 resize->width = shsurf->surface->geometry.width;
1149 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001150
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001151 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1152 ws->seat.pointer, edges);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001153
1154 return 0;
1155}
1156
1157static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001158shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001159 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001160 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001161{
Daniel Stone37816df2012-05-16 18:45:18 +01001162 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001164
Alex Wu4539b082012-03-01 12:57:46 +08001165 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1166 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001167
Daniel Stone37816df2012-05-16 18:45:18 +01001168 if (ws->seat.pointer->button_count == 0 ||
1169 ws->seat.pointer->grab_serial != serial ||
1170 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001171 return;
1172
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001173 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001174 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001175}
1176
Juan Zhao96879df2012-02-07 08:45:41 +08001177static struct weston_output *
1178get_default_output(struct weston_compositor *compositor)
1179{
1180 return container_of(compositor->output_list.next,
1181 struct weston_output, link);
1182}
1183
Alex Wu4539b082012-03-01 12:57:46 +08001184static void
1185shell_unset_fullscreen(struct shell_surface *shsurf)
1186{
1187 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001188 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1189 shell_surface_is_top_fullscreen(shsurf)) {
1190 weston_output_switch_mode(shsurf->fullscreen_output,
1191 shsurf->fullscreen_output->origin);
1192 }
Alex Wu4539b082012-03-01 12:57:46 +08001193 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1194 shsurf->fullscreen.framerate = 0;
1195 wl_list_remove(&shsurf->fullscreen.transform.link);
1196 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001197 if (shsurf->fullscreen.black_surface)
1198 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001199 shsurf->fullscreen.black_surface = NULL;
1200 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001201 weston_surface_set_position(shsurf->surface,
1202 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001203 if (shsurf->saved_rotation_valid) {
1204 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1205 &shsurf->rotation.transform.link);
1206 shsurf->saved_rotation_valid = false;
1207 }
Alex Wu4539b082012-03-01 12:57:46 +08001208}
1209
Pekka Paalanen98262232011-12-01 10:42:22 +02001210static int
1211reset_shell_surface_type(struct shell_surface *surface)
1212{
1213 switch (surface->type) {
1214 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001215 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001216 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001217 case SHELL_SURFACE_MAXIMIZED:
1218 surface->output = get_default_output(surface->surface->compositor);
1219 weston_surface_set_position(surface->surface,
1220 surface->saved_x,
1221 surface->saved_y);
1222 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001223 case SHELL_SURFACE_NONE:
1224 case SHELL_SURFACE_TOPLEVEL:
1225 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001226 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001227 break;
1228 }
1229
1230 surface->type = SHELL_SURFACE_NONE;
1231 return 0;
1232}
1233
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001234static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001235set_surface_type(struct shell_surface *shsurf)
1236{
1237 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001238 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001239
1240 reset_shell_surface_type(shsurf);
1241
1242 shsurf->type = shsurf->next_type;
1243 shsurf->next_type = SHELL_SURFACE_NONE;
1244
1245 switch (shsurf->type) {
1246 case SHELL_SURFACE_TOPLEVEL:
1247 break;
1248 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001249 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001250 pes->geometry.x + shsurf->transient.x,
1251 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001252 break;
1253
1254 case SHELL_SURFACE_MAXIMIZED:
1255 shsurf->saved_x = surface->geometry.x;
1256 shsurf->saved_y = surface->geometry.y;
1257 shsurf->saved_position_valid = true;
1258 break;
1259
1260 case SHELL_SURFACE_FULLSCREEN:
1261 shsurf->saved_x = surface->geometry.x;
1262 shsurf->saved_y = surface->geometry.y;
1263 shsurf->saved_position_valid = true;
1264
1265 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1266 wl_list_remove(&shsurf->rotation.transform.link);
1267 wl_list_init(&shsurf->rotation.transform.link);
1268 shsurf->surface->geometry.dirty = 1;
1269 shsurf->saved_rotation_valid = true;
1270 }
1271 break;
1272
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001273 default:
1274 break;
1275 }
1276}
1277
1278static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001279set_toplevel(struct shell_surface *shsurf)
1280{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001281 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001282}
1283
1284static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001285shell_surface_set_toplevel(struct wl_client *client,
1286 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001287{
Pekka Paalanen98262232011-12-01 10:42:22 +02001288 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001289
Tiago Vignattibc052c92012-04-19 16:18:18 +03001290 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001291}
1292
1293static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001294set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001295 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001296{
1297 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001298 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001299 shsurf->transient.x = x;
1300 shsurf->transient.y = y;
1301 shsurf->transient.flags = flags;
1302 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1303}
1304
1305static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001306shell_surface_set_transient(struct wl_client *client,
1307 struct wl_resource *resource,
1308 struct wl_resource *parent_resource,
1309 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001310{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001311 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001312 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001313
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001314 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001315}
1316
Tiago Vignattibe143262012-04-16 17:31:41 +03001317static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001318shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001319{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001320 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001321}
1322
1323static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001324get_output_panel_height(struct desktop_shell *shell,
1325 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001326{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001327 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001328 int panel_height = 0;
1329
1330 if (!output)
1331 return 0;
1332
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001333 wl_list_for_each(surface, &shell->panel_layer.surface_list, link) {
1334 if (surface->output == output) {
1335 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001336 break;
1337 }
1338 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001339
Juan Zhao96879df2012-02-07 08:45:41 +08001340 return panel_height;
1341}
1342
1343static void
1344shell_surface_set_maximized(struct wl_client *client,
1345 struct wl_resource *resource,
1346 struct wl_resource *output_resource )
1347{
1348 struct shell_surface *shsurf = resource->data;
1349 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001350 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001351 uint32_t edges = 0, panel_height = 0;
1352
1353 /* get the default output, if the client set it as NULL
1354 check whether the ouput is available */
1355 if (output_resource)
1356 shsurf->output = output_resource->data;
1357 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;
1402 float scale;
1403
1404 center_on_output(surface, output);
1405
1406 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001407 shsurf->fullscreen.black_surface =
1408 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001409 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001410 output->x, output->y,
1411 output->current->width,
1412 output->current->height);
1413
1414 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1415 wl_list_insert(&surface->layer_link,
1416 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001417 shsurf->fullscreen.black_surface->output = output;
1418
1419 switch (shsurf->fullscreen.type) {
1420 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1421 break;
1422 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1423 matrix = &shsurf->fullscreen.transform.matrix;
1424 weston_matrix_init(matrix);
1425 scale = (float)output->current->width/(float)surface->geometry.width;
1426 weston_matrix_scale(matrix, scale, scale, 1);
1427 wl_list_remove(&shsurf->fullscreen.transform.link);
1428 wl_list_insert(surface->geometry.transformation_list.prev,
1429 &shsurf->fullscreen.transform.link);
1430 weston_surface_set_position(surface, output->x, output->y);
1431 break;
1432 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001433 if (shell_surface_is_top_fullscreen(shsurf)) {
1434 struct weston_mode mode = {0,
1435 surface->geometry.width,
1436 surface->geometry.height,
1437 shsurf->fullscreen.framerate};
1438
1439 if (weston_output_switch_mode(output, &mode) == 0) {
1440 weston_surface_configure(shsurf->fullscreen.black_surface,
1441 output->x, output->y,
1442 output->current->width,
1443 output->current->height);
1444 weston_surface_set_position(surface, output->x, output->y);
1445 break;
1446 }
1447 }
Alex Wu4539b082012-03-01 12:57:46 +08001448 break;
1449 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1450 break;
1451 default:
1452 break;
1453 }
1454}
1455
1456/* make the fullscreen and black surface at the top */
1457static void
1458shell_stack_fullscreen(struct shell_surface *shsurf)
1459{
Alex Wubd3354b2012-04-17 17:20:49 +08001460 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001461 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001462 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001463
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001464 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001465 wl_list_insert(&shell->fullscreen_layer.surface_list,
1466 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001467 weston_surface_damage(surface);
1468
1469 if (!shsurf->fullscreen.black_surface)
1470 shsurf->fullscreen.black_surface =
1471 create_black_surface(surface->compositor,
1472 surface,
1473 output->x, output->y,
1474 output->current->width,
1475 output->current->height);
1476
1477 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001478 wl_list_insert(&surface->layer_link,
1479 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001480 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001481}
1482
1483static void
1484shell_map_fullscreen(struct shell_surface *shsurf)
1485{
Alex Wu4539b082012-03-01 12:57:46 +08001486 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001487 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001488}
1489
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001490static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001491shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001492 struct wl_resource *resource,
1493 uint32_t method,
1494 uint32_t framerate,
1495 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001496{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001497 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001498 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001499
1500 if (output_resource)
1501 shsurf->output = output_resource->data;
1502 else
1503 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001504
Alex Wu4539b082012-03-01 12:57:46 +08001505 shsurf->fullscreen_output = shsurf->output;
1506 shsurf->fullscreen.type = method;
1507 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001508 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001509
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001510 shsurf->client->send_configure(shsurf->surface, 0,
1511 shsurf->output->current->width,
1512 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001513}
1514
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001515static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001516popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001517 struct wl_surface *surface,
1518 wl_fixed_t x,
1519 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001520{
Daniel Stone37816df2012-05-16 18:45:18 +01001521 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001522 struct shell_surface *priv =
1523 container_of(grab, struct shell_surface, popup.grab);
1524 struct wl_client *client = priv->surface->surface.resource.client;
1525
Pekka Paalanencb108432012-01-19 16:25:40 +02001526 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001527 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001528 grab->focus = surface;
1529 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001530 wl_pointer_set_focus(pointer, NULL,
1531 wl_fixed_from_int(0),
1532 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001533 grab->focus = NULL;
1534 }
1535}
1536
1537static void
Scott Moreau447013d2012-02-18 05:05:29 -07001538popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001539 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001540{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001541 struct wl_resource *resource;
1542
Daniel Stone37816df2012-05-16 18:45:18 +01001543 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001544 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001545 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001546}
1547
1548static void
Scott Moreau447013d2012-02-18 05:05:29 -07001549popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001550 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001551{
1552 struct wl_resource *resource;
1553 struct shell_surface *shsurf =
1554 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001555 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001556 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001557 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001558
Daniel Stone37816df2012-05-16 18:45:18 +01001559 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001560 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001561 display = wl_client_get_display(resource->client);
1562 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001563 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001564 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001565 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001566 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001567 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001568 wl_pointer_end_grab(grab->pointer);
1569 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001570 }
1571
Daniel Stone4dbadb12012-05-30 16:31:51 +01001572 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001573 shsurf->popup.initial_up = 1;
1574}
1575
Scott Moreau447013d2012-02-18 05:05:29 -07001576static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577 popup_grab_focus,
1578 popup_grab_motion,
1579 popup_grab_button,
1580};
1581
1582static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001583shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001584{
Daniel Stone37816df2012-05-16 18:45:18 +01001585 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001586 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001587 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001588
1589 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001591
Pekka Paalanen938269a2012-02-07 14:19:01 +02001592 weston_surface_update_transform(parent);
1593 if (parent->transform.enabled) {
1594 shsurf->popup.parent_transform.matrix =
1595 parent->transform.matrix;
1596 } else {
1597 /* construct x, y translation matrix */
1598 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1599 shsurf->popup.parent_transform.matrix.d[12] =
1600 parent->geometry.x;
1601 shsurf->popup.parent_transform.matrix.d[13] =
1602 parent->geometry.y;
1603 }
1604 wl_list_insert(es->geometry.transformation_list.prev,
1605 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001606 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001607
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001608 shsurf->popup.initial_up = 0;
1609
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001610 /* We don't require the grab to still be active, but if another
1611 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001612 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1613 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001614 } else {
1615 wl_shell_surface_send_popup_done(&shsurf->resource);
1616 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001617}
1618
1619static void
1620shell_surface_set_popup(struct wl_client *client,
1621 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001622 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001623 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001624 struct wl_resource *parent_resource,
1625 int32_t x, int32_t y, uint32_t flags)
1626{
1627 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001628
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001629 shsurf->type = SHELL_SURFACE_POPUP;
1630 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001631 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001632 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001633 shsurf->popup.x = x;
1634 shsurf->popup.y = y;
1635}
1636
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001637static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001638 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001639 shell_surface_move,
1640 shell_surface_resize,
1641 shell_surface_set_toplevel,
1642 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001643 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001644 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001645 shell_surface_set_maximized,
1646 shell_surface_set_title,
1647 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001648};
1649
1650static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001651destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001652{
Daniel Stone37816df2012-05-16 18:45:18 +01001653 if (shsurf->popup.grab.pointer)
1654 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001655
Alex Wubd3354b2012-04-17 17:20:49 +08001656 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1657 shell_surface_is_top_fullscreen(shsurf)) {
1658 weston_output_switch_mode(shsurf->fullscreen_output,
1659 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001660 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001661
Alex Wuaa08e2d2012-03-05 11:01:40 +08001662 if (shsurf->fullscreen.black_surface)
1663 weston_surface_destroy(shsurf->fullscreen.black_surface);
1664
Alex Wubd3354b2012-04-17 17:20:49 +08001665 /* As destroy_resource() use wl_list_for_each_safe(),
1666 * we can always remove the listener.
1667 */
1668 wl_list_remove(&shsurf->surface_destroy_listener.link);
1669 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001670 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001671
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001672 wl_list_remove(&shsurf->link);
1673 free(shsurf);
1674}
1675
1676static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001677shell_destroy_shell_surface(struct wl_resource *resource)
1678{
1679 struct shell_surface *shsurf = resource->data;
1680
1681 destroy_shell_surface(shsurf);
1682}
1683
1684static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001685shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001686{
1687 struct shell_surface *shsurf = container_of(listener,
1688 struct shell_surface,
1689 surface_destroy_listener);
1690
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001691 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001692 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001693 } else {
1694 wl_signal_emit(&shsurf->resource.destroy_signal,
1695 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001696 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001697 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001698}
1699
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001700static void
1701shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1702
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001703static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001704get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001705{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001706 if (surface->configure == shell_surface_configure)
1707 return surface->private;
1708 else
1709 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001710}
1711
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001712static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001713create_shell_surface(void *shell, struct weston_surface *surface,
1714 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001715{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001716 struct shell_surface *shsurf;
1717
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001718 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001719 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001720 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001721 }
1722
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001723 shsurf = calloc(1, sizeof *shsurf);
1724 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001725 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001726 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001727 }
1728
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001729 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001730 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001731 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001732
Tiago Vignattibc052c92012-04-19 16:18:18 +03001733 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001734 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001735 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001736 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001737 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001738 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1739 shsurf->fullscreen.framerate = 0;
1740 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001741 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001742 wl_list_init(&shsurf->fullscreen.transform.link);
1743
Tiago Vignattibc052c92012-04-19 16:18:18 +03001744 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001745 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1746 wl_signal_add(&surface->surface.resource.destroy_signal,
1747 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001748
1749 /* init link so its safe to always remove it in destroy_shell_surface */
1750 wl_list_init(&shsurf->link);
1751
Pekka Paalanen460099f2012-01-20 16:48:25 +02001752 /* empty when not in use */
1753 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001754 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001755
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001756 wl_list_init(&shsurf->workspace_transform.link);
1757
Pekka Paalanen98262232011-12-01 10:42:22 +02001758 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001759 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001760
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001761 shsurf->client = client;
1762
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001763 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001764}
1765
1766static void
1767shell_get_shell_surface(struct wl_client *client,
1768 struct wl_resource *resource,
1769 uint32_t id,
1770 struct wl_resource *surface_resource)
1771{
1772 struct weston_surface *surface = surface_resource->data;
1773 struct desktop_shell *shell = resource->data;
1774 struct shell_surface *shsurf;
1775
1776 if (get_shell_surface(surface)) {
1777 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001778 WL_DISPLAY_ERROR_INVALID_OBJECT,
1779 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001780 return;
1781 }
1782
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001783 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001784 if (!shsurf) {
1785 wl_resource_post_error(surface_resource,
1786 WL_DISPLAY_ERROR_INVALID_OBJECT,
1787 "surface->configure already set");
1788 return;
1789 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001790
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001791 shsurf->resource.destroy = shell_destroy_shell_surface;
1792 shsurf->resource.object.id = id;
1793 shsurf->resource.object.interface = &wl_shell_surface_interface;
1794 shsurf->resource.object.implementation =
1795 (void (**)(void)) &shell_surface_implementation;
1796 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001797
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001798 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001799}
1800
1801static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001802 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001803};
1804
Kristian Høgsberg07937562011-04-12 17:25:42 -04001805static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001806handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001807{
1808 proc->pid = 0;
1809}
1810
1811static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001812launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001813{
1814 if (shell->screensaver.binding)
1815 return;
1816
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001817 if (!shell->screensaver.path)
1818 return;
1819
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001820 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001821 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001822 return;
1823 }
1824
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001825 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001826 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001827 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001828 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001829}
1830
1831static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001832terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001833{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001834 if (shell->screensaver.process.pid == 0)
1835 return;
1836
1837 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001838}
1839
1840static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001841configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001842{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001843 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001844
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001845 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1846 if (s->output == es->output && s != es) {
1847 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001848 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001849 }
1850 }
1851
1852 weston_surface_configure(es, es->output->x, es->output->y,
1853 es->buffer->width, es->buffer->height);
1854
1855 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001856 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001857 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001858 }
1859}
1860
1861static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001862background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1863{
1864 struct desktop_shell *shell = es->private;
1865
1866 configure_static_surface(es, &shell->background_layer);
1867}
1868
1869static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001870desktop_shell_set_background(struct wl_client *client,
1871 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001872 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001873 struct wl_resource *surface_resource)
1874{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001875 struct desktop_shell *shell = resource->data;
1876 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001877
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001878 if (surface->configure) {
1879 wl_resource_post_error(surface_resource,
1880 WL_DISPLAY_ERROR_INVALID_OBJECT,
1881 "surface role already assigned");
1882 return;
1883 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001884
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001885 surface->configure = background_configure;
1886 surface->private = shell;
1887 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001888 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001889 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001890 surface->output->current->width,
1891 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001892}
1893
1894static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001895panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1896{
1897 struct desktop_shell *shell = es->private;
1898
1899 configure_static_surface(es, &shell->panel_layer);
1900}
1901
1902static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001903desktop_shell_set_panel(struct wl_client *client,
1904 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001905 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001906 struct wl_resource *surface_resource)
1907{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001908 struct desktop_shell *shell = resource->data;
1909 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001910
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001911 if (surface->configure) {
1912 wl_resource_post_error(surface_resource,
1913 WL_DISPLAY_ERROR_INVALID_OBJECT,
1914 "surface role already assigned");
1915 return;
1916 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001917
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001918 surface->configure = panel_configure;
1919 surface->private = shell;
1920 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001921 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001922 surface_resource,
1923 surface->output->current->width,
1924 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001925}
1926
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001927static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001928lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1929{
1930 struct desktop_shell *shell = surface->private;
1931
1932 center_on_output(surface, get_default_output(shell->compositor));
1933
1934 if (!weston_surface_is_mapped(surface)) {
1935 wl_list_insert(&shell->lock_layer.surface_list,
1936 &surface->layer_link);
1937 weston_surface_assign_output(surface);
1938 weston_compositor_wake(shell->compositor);
1939 }
1940}
1941
1942static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001943handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001944{
Tiago Vignattibe143262012-04-16 17:31:41 +03001945 struct desktop_shell *shell =
1946 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001947
Martin Minarik6d118362012-06-07 18:01:59 +02001948 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001949 shell->lock_surface = NULL;
1950}
1951
1952static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001953desktop_shell_set_lock_surface(struct wl_client *client,
1954 struct wl_resource *resource,
1955 struct wl_resource *surface_resource)
1956{
Tiago Vignattibe143262012-04-16 17:31:41 +03001957 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001958 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001959
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001960 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001961
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001962 if (!shell->locked)
1963 return;
1964
Pekka Paalanen98262232011-12-01 10:42:22 +02001965 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001966
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001967 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1968 wl_signal_add(&surface_resource->destroy_signal,
1969 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001970
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001971 surface->configure = lock_surface_configure;
1972 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001973}
1974
1975static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001976resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001977{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001978 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001979 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001980
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001981 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1982 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001983
1984 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001985
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001986 wl_list_remove(&shell->lock_layer.link);
1987 wl_list_insert(&shell->compositor->cursor_layer.link,
1988 &shell->fullscreen_layer.link);
1989 wl_list_insert(&shell->fullscreen_layer.link,
1990 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001991 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001992
Jonas Ådahl04769742012-06-13 00:01:24 +02001993 pop_focus_state(shell, get_current_workspace(shell));
1994
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001995 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001996 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001997 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001998 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001999}
2000
2001static void
2002desktop_shell_unlock(struct wl_client *client,
2003 struct wl_resource *resource)
2004{
Tiago Vignattibe143262012-04-16 17:31:41 +03002005 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002006
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002007 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002008
2009 if (shell->locked)
2010 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002011}
2012
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002013static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002014desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002015 struct wl_resource *resource,
2016 struct wl_resource *surface_resource)
2017{
2018 struct desktop_shell *shell = resource->data;
2019
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002020 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002021}
2022
Kristian Høgsberg75840622011-09-06 13:48:16 -04002023static const struct desktop_shell_interface desktop_shell_implementation = {
2024 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002025 desktop_shell_set_panel,
2026 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002027 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002028 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002029};
2030
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002031static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002032get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002033{
2034 struct shell_surface *shsurf;
2035
2036 shsurf = get_shell_surface(surface);
2037 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002038 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002039 return shsurf->type;
2040}
2041
Kristian Høgsberg75840622011-09-06 13:48:16 -04002042static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002043move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002044{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002045 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002046 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002047 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002048
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002049 if (surface == NULL)
2050 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002051
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002052 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002053 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002054 return;
2055
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002056 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002057}
2058
2059static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002060resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002061{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002062 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002063 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002064 uint32_t edges = 0;
2065 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002066 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002067
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002068 if (surface == NULL)
2069 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002070
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002071 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002072 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002073 return;
2074
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002075 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002076 wl_fixed_to_int(seat->pointer->grab_x),
2077 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002078 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002079
Pekka Paalanen60921e52012-01-25 15:55:43 +02002080 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002081 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002082 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002083 edges |= 0;
2084 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002085 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002086
Pekka Paalanen60921e52012-01-25 15:55:43 +02002087 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002088 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002089 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002090 edges |= 0;
2091 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002092 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002093
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002094 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002095}
2096
2097static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002098surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002099 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002100{
Scott Moreau02709af2012-05-22 01:54:10 -06002101 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002102 struct shell_surface *shsurf;
2103 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002104 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002105
2106 if (surface == NULL)
2107 return;
2108
2109 shsurf = get_shell_surface(surface);
2110 if (!shsurf)
2111 return;
2112
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002113 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002114
Scott Moreau02709af2012-05-22 01:54:10 -06002115 if (surface->alpha > 1.0)
2116 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002117 if (surface->alpha < step)
2118 surface->alpha = step;
2119
2120 surface->geometry.dirty = 1;
2121 weston_surface_damage(surface);
2122}
2123
2124static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002125do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002126 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002127{
Daniel Stone37816df2012-05-16 18:45:18 +01002128 struct weston_seat *ws = (struct weston_seat *) seat;
2129 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002130 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002131 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002132
2133 wl_list_for_each(output, &compositor->output_list, link) {
2134 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002135 wl_fixed_to_double(seat->pointer->x),
2136 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002137 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002138 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002139 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002140 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002141 increment = -output->zoom.increment;
2142 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002143 increment = output->zoom.increment *
2144 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002145 else
2146 increment = 0;
2147
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002148 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002149
Scott Moreaue6603982012-06-11 13:07:51 -06002150 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002151 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002152 else if (output->zoom.level > output->zoom.max_level)
2153 output->zoom.level = output->zoom.max_level;
2154 else
2155 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002156
Scott Moreaue6603982012-06-11 13:07:51 -06002157 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002158
Scott Moreau8dacaab2012-06-17 18:10:58 -06002159 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002160 }
2161 }
2162}
2163
Scott Moreauccbf29d2012-02-22 14:21:41 -07002164static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002165zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002166 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002167{
2168 do_zoom(seat, time, 0, axis, value);
2169}
2170
2171static void
2172zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2173 void *data)
2174{
2175 do_zoom(seat, time, key, 0, 0);
2176}
2177
2178static void
2179terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2180 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002181{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002182 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002183
Daniel Stone325fc2d2012-05-30 16:31:58 +01002184 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002185}
2186
2187static void
Scott Moreau447013d2012-02-18 05:05:29 -07002188rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002189 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002190{
2191 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002192 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002193 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002194 struct shell_surface *shsurf = rotate->base.shsurf;
2195 struct weston_surface *surface;
2196 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2197
2198 if (!shsurf)
2199 return;
2200
2201 surface = shsurf->surface;
2202
2203 cx = 0.5f * surface->geometry.width;
2204 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002205
Daniel Stone37816df2012-05-16 18:45:18 +01002206 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2207 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002208 r = sqrtf(dx * dx + dy * dy);
2209
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002210 wl_list_remove(&shsurf->rotation.transform.link);
2211 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002212
2213 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002214 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002215 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002216
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002217 weston_matrix_init(&rotate->rotation);
2218 rotate->rotation.d[0] = dx / r;
2219 rotate->rotation.d[4] = -dy / r;
2220 rotate->rotation.d[1] = -rotate->rotation.d[4];
2221 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002222
2223 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002224 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002225 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002226 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002227 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002228
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002229 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002230 &shsurf->surface->geometry.transformation_list,
2231 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002232 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002233 wl_list_init(&shsurf->rotation.transform.link);
2234 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002235 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002236 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002237
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002238 /* We need to adjust the position of the surface
2239 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002240 cposx = surface->geometry.x + cx;
2241 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002242 dposx = rotate->center.x - cposx;
2243 dposy = rotate->center.y - cposy;
2244 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002245 weston_surface_set_position(surface,
2246 surface->geometry.x + dposx,
2247 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002248 }
2249
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002250 /* Repaint implies weston_surface_update_transform(), which
2251 * lazily applies the damage due to rotation update.
2252 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002253 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002254}
2255
2256static void
Scott Moreau447013d2012-02-18 05:05:29 -07002257rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002258 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002259{
2260 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002261 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002262 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002263 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002264 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002265
Daniel Stone4dbadb12012-05-30 16:31:51 +01002266 if (pointer->button_count == 0 &&
2267 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002268 if (shsurf)
2269 weston_matrix_multiply(&shsurf->rotation.rotation,
2270 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002271 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002272 free(rotate);
2273 }
2274}
2275
Scott Moreau447013d2012-02-18 05:05:29 -07002276static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002277 noop_grab_focus,
2278 rotate_grab_motion,
2279 rotate_grab_button,
2280};
2281
2282static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002283rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002284 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002285{
2286 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002287 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002288 struct shell_surface *surface;
2289 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002290 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002291 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002292
2293 if (base_surface == NULL)
2294 return;
2295
2296 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002297 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002298 return;
2299
Pekka Paalanen460099f2012-01-20 16:48:25 +02002300 rotate = malloc(sizeof *rotate);
2301 if (!rotate)
2302 return;
2303
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002304 weston_surface_to_global_float(surface->surface,
2305 surface->surface->geometry.width / 2,
2306 surface->surface->geometry.height / 2,
2307 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002308
Daniel Stone37816df2012-05-16 18:45:18 +01002309 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2310 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002311 r = sqrtf(dx * dx + dy * dy);
2312 if (r > 20.0f) {
2313 struct weston_matrix inverse;
2314
2315 weston_matrix_init(&inverse);
2316 inverse.d[0] = dx / r;
2317 inverse.d[4] = dy / r;
2318 inverse.d[1] = -inverse.d[4];
2319 inverse.d[5] = inverse.d[0];
2320 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002321
2322 weston_matrix_init(&rotate->rotation);
2323 rotate->rotation.d[0] = dx / r;
2324 rotate->rotation.d[4] = -dy / r;
2325 rotate->rotation.d[1] = -rotate->rotation.d[4];
2326 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002327 } else {
2328 weston_matrix_init(&surface->rotation.rotation);
2329 weston_matrix_init(&rotate->rotation);
2330 }
2331
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002332 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2333 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002334}
2335
2336static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002337lower_fullscreen_layer(struct desktop_shell *shell)
2338{
2339 struct workspace *ws;
2340 struct weston_surface *surface, *prev;
2341
2342 ws = get_current_workspace(shell);
2343 wl_list_for_each_reverse_safe(surface, prev,
2344 &shell->fullscreen_layer.surface_list,
2345 layer_link)
2346 weston_surface_restack(surface, &ws->layer.surface_list);
2347}
2348
2349static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002350activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002351 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002352{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002353 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002354
Daniel Stone37816df2012-05-16 18:45:18 +01002355 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002356
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002357 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002358 case SHELL_SURFACE_FULLSCREEN:
2359 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002360 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002361 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002362 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002363 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002364 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002365 lower_fullscreen_layer(shell);
2366 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002367 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002368 }
2369}
2370
Alex Wu21858432012-04-01 20:13:08 +08002371/* no-op func for checking black surface */
2372static void
2373black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2374{
2375}
2376
2377static bool
2378is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2379{
2380 if (es->configure == black_surface_configure) {
2381 if (fs_surface)
2382 *fs_surface = (struct weston_surface *)es->private;
2383 return true;
2384 }
2385 return false;
2386}
2387
Kristian Høgsberg75840622011-09-06 13:48:16 -04002388static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002389click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002390 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002391{
Daniel Stone37816df2012-05-16 18:45:18 +01002392 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002393 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002394 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002395 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002396
Daniel Stone37816df2012-05-16 18:45:18 +01002397 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002398 if (!focus)
2399 return;
2400
Alex Wu21858432012-04-01 20:13:08 +08002401 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002402 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002403
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002404 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2405 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002406
Daniel Stone325fc2d2012-05-30 16:31:58 +01002407 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002408 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002409}
2410
2411static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002412lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002413{
Tiago Vignattibe143262012-04-16 17:31:41 +03002414 struct desktop_shell *shell =
2415 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002416 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002417 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002418
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002419 if (shell->locked) {
2420 wl_list_for_each(output, &shell->compositor->output_list, link)
2421 /* TODO: find a way to jump to other DPMS levels */
2422 if (output->set_dpms)
2423 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002424 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002425 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002426
2427 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002428
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002429 /* Hide all surfaces by removing the fullscreen, panel and
2430 * toplevel layers. This way nothing else can show or receive
2431 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002432
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002433 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002434 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002435 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002436 wl_list_insert(&shell->compositor->cursor_layer.link,
2437 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002438
Pekka Paalanen77346a62011-11-30 16:26:35 +02002439 launch_screensaver(shell);
2440
Jonas Ådahl04769742012-06-13 00:01:24 +02002441 /* stash keyboard foci in current workspace */
2442 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002443
2444 /* TODO: disable bindings that should not work while locked. */
2445
2446 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002447}
2448
2449static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002450unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002451{
Tiago Vignattibe143262012-04-16 17:31:41 +03002452 struct desktop_shell *shell =
2453 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002454
Pekka Paalanend81c2162011-11-16 13:47:34 +02002455 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002456 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002457 return;
2458 }
2459
2460 /* If desktop-shell client has gone away, unlock immediately. */
2461 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002462 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002463 return;
2464 }
2465
2466 if (shell->prepare_event_sent)
2467 return;
2468
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002469 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002470 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002471}
2472
2473static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002474show_input_panels(struct wl_listener *listener, void *data)
2475{
2476 struct desktop_shell *shell =
2477 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002478 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002479
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002480 wl_list_for_each_safe(surface, next,
2481 &shell->input_panel.surfaces, layer_link) {
2482 wl_list_remove(&surface->layer_link);
2483 wl_list_insert(&shell->panel_layer.surface_list,
2484 &surface->layer_link);
2485 weston_surface_assign_output(surface);
2486 weston_surface_damage(surface);
2487 weston_slide_run(surface,
2488 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002489 }
2490}
2491
2492static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002493input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2494
2495static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002496hide_input_panels(struct wl_listener *listener, void *data)
2497{
2498 struct desktop_shell *shell =
2499 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002500 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002501
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002502 wl_list_for_each_safe(surface, next,
2503 &shell->panel_layer.surface_list, layer_link)
2504 if (surface->configure == input_panel_configure) {
2505 weston_surface_unmap(surface);
2506 wl_list_insert(&shell->input_panel.surfaces,
2507 &surface->layer_link);
2508 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002509}
2510
2511static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002512center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002513{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002514 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002515 GLfloat x = (mode->width - surface->buffer->width) / 2;
2516 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002517
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002518 weston_surface_configure(surface, output->x + x, output->y + y,
2519 surface->buffer->width,
2520 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002521}
2522
2523static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002524map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002525 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002526{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002527 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002528 struct shell_surface *shsurf = get_shell_surface(surface);
2529 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002530 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002531 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002532 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002533 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002534
Pekka Paalanen60921e52012-01-25 15:55:43 +02002535 surface->geometry.width = width;
2536 surface->geometry.height = height;
2537 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002538
2539 /* initial positioning, see also configure() */
2540 switch (surface_type) {
2541 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002542 weston_surface_set_position(surface, 10 + random() % 400,
2543 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002544 break;
Alex Wu4539b082012-03-01 12:57:46 +08002545 case SHELL_SURFACE_FULLSCREEN:
2546 shell_map_fullscreen(shsurf);
2547 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002548 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002549 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002550 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002551 weston_surface_set_position(surface, shsurf->output->x,
2552 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002553 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002554 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002555 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002556 case SHELL_SURFACE_NONE:
2557 weston_surface_set_position(surface,
2558 surface->geometry.x + sx,
2559 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002560 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002561 default:
2562 ;
2563 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002564
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002565 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002566 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002567 case SHELL_SURFACE_POPUP:
2568 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002569 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002570 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2571 break;
Alex Wu4539b082012-03-01 12:57:46 +08002572 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002573 case SHELL_SURFACE_NONE:
2574 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002575 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002576 ws = get_current_workspace(shell);
2577 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002578 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002579 }
2580
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002581 if (surface_type != SHELL_SURFACE_NONE) {
2582 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002583 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2584 surface->output = shsurf->output;
2585 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002586
Juan Zhao7bb92f02011-12-15 11:31:51 -05002587 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002588 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002589 if (shsurf->transient.flags ==
2590 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2591 break;
2592 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002593 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002594 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002595 if (!shell->locked) {
2596 wl_list_for_each(seat, &compositor->seat_list, link)
2597 activate(shell, surface, seat);
2598 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002599 break;
2600 default:
2601 break;
2602 }
2603
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002604 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002605 {
2606 switch (shell->win_animation_type) {
2607 case ANIMATION_FADE:
2608 weston_fade_run(surface, NULL, NULL);
2609 break;
2610 case ANIMATION_ZOOM:
2611 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2612 break;
2613 default:
2614 break;
2615 }
2616 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002617}
2618
2619static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002620configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002621 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002622{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002623 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2624 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002625
Pekka Paalanen77346a62011-11-30 16:26:35 +02002626 shsurf = get_shell_surface(surface);
2627 if (shsurf)
2628 surface_type = shsurf->type;
2629
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002630 surface->geometry.x = x;
2631 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002632 surface->geometry.width = width;
2633 surface->geometry.height = height;
2634 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002635
2636 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002637 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002638 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002639 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002640 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002641 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002642 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002643 surface->geometry.x = surface->output->x;
2644 surface->geometry.y = surface->output->y +
2645 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002646 break;
Alex Wu4539b082012-03-01 12:57:46 +08002647 case SHELL_SURFACE_TOPLEVEL:
2648 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002649 default:
2650 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002651 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002652
Alex Wu4539b082012-03-01 12:57:46 +08002653 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002654 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002655 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002656
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002657 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002658 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002659 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002660}
2661
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002662static void
2663shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2664{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002665 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002666 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002667 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002668
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002669 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002670 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002671 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002672 type_changed = 1;
2673 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002674
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002675 if (!weston_surface_is_mapped(es)) {
2676 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002677 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002678 es->geometry.width != es->buffer->width ||
2679 es->geometry.height != es->buffer->height) {
2680 GLfloat from_x, from_y;
2681 GLfloat to_x, to_y;
2682
2683 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2684 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2685 configure(shell, es,
2686 es->geometry.x + to_x - from_x,
2687 es->geometry.y + to_y - from_y,
2688 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002689 }
2690}
2691
Tiago Vignattibe143262012-04-16 17:31:41 +03002692static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002693
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002694static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002695desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002696{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002697 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002698 struct desktop_shell *shell =
2699 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002700
2701 shell->child.process.pid = 0;
2702 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002703
2704 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2705 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002706 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002707 shell->child.deathstamp = time;
2708 shell->child.deathcount = 0;
2709 }
2710
2711 shell->child.deathcount++;
2712 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002713 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002714 return;
2715 }
2716
Martin Minarik6d118362012-06-07 18:01:59 +02002717 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002718 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002719}
2720
2721static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002722launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002723{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002724 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002725
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002726 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002727 &shell->child.process,
2728 shell_exe,
2729 desktop_shell_sigchld);
2730
2731 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002732 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002733 return 0;
2734}
2735
2736static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002737bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2738{
Tiago Vignattibe143262012-04-16 17:31:41 +03002739 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002740
2741 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002742 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002743}
2744
Kristian Høgsberg75840622011-09-06 13:48:16 -04002745static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002746unbind_desktop_shell(struct wl_resource *resource)
2747{
Tiago Vignattibe143262012-04-16 17:31:41 +03002748 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002749
2750 if (shell->locked)
2751 resume_desktop(shell);
2752
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002753 shell->child.desktop_shell = NULL;
2754 shell->prepare_event_sent = false;
2755 free(resource);
2756}
2757
2758static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002759bind_desktop_shell(struct wl_client *client,
2760 void *data, uint32_t version, uint32_t id)
2761{
Tiago Vignattibe143262012-04-16 17:31:41 +03002762 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002763 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002764
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002765 resource = wl_client_add_object(client, &desktop_shell_interface,
2766 &desktop_shell_implementation,
2767 id, shell);
2768
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002769 if (client == shell->child.client) {
2770 resource->destroy = unbind_desktop_shell;
2771 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002772 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002773 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002774
2775 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2776 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002777 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002778}
2779
Pekka Paalanen6e168112011-11-24 11:34:05 +02002780static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002781screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2782{
2783 struct desktop_shell *shell = surface->private;
2784
2785 if (!shell->locked)
2786 return;
2787
2788 center_on_output(surface, surface->output);
2789
2790 if (wl_list_empty(&surface->layer_link)) {
2791 wl_list_insert(shell->lock_layer.surface_list.prev,
2792 &surface->layer_link);
2793 weston_surface_assign_output(surface);
2794 shell->compositor->idle_time = shell->screensaver.duration;
2795 weston_compositor_wake(shell->compositor);
2796 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2797 }
2798}
2799
2800static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002801screensaver_set_surface(struct wl_client *client,
2802 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002803 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002804 struct wl_resource *output_resource)
2805{
Tiago Vignattibe143262012-04-16 17:31:41 +03002806 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002807 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002808 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002809
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002810 surface->configure = screensaver_configure;
2811 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002812 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002813}
2814
2815static const struct screensaver_interface screensaver_implementation = {
2816 screensaver_set_surface
2817};
2818
2819static void
2820unbind_screensaver(struct wl_resource *resource)
2821{
Tiago Vignattibe143262012-04-16 17:31:41 +03002822 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002823
Pekka Paalanen77346a62011-11-30 16:26:35 +02002824 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002825 free(resource);
2826}
2827
2828static void
2829bind_screensaver(struct wl_client *client,
2830 void *data, uint32_t version, uint32_t id)
2831{
Tiago Vignattibe143262012-04-16 17:31:41 +03002832 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002833 struct wl_resource *resource;
2834
2835 resource = wl_client_add_object(client, &screensaver_interface,
2836 &screensaver_implementation,
2837 id, shell);
2838
Pekka Paalanen77346a62011-11-30 16:26:35 +02002839 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002840 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002841 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002842 return;
2843 }
2844
2845 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2846 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002847 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002848}
2849
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002850static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002851input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2852{
2853 struct weston_mode *mode = surface->output->current;
2854 GLfloat x = (mode->width - surface->buffer->width) / 2;
2855 GLfloat y = mode->height - surface->buffer->height;
2856
2857 /* Don't map the input panel here, wait for
2858 * show_input_panels signal. */
2859
2860 weston_surface_configure(surface,
2861 surface->output->x + x,
2862 surface->output->y + y,
2863 surface->buffer->width,
2864 surface->buffer->height);
2865}
2866
2867static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002868input_panel_set_surface(struct wl_client *client,
2869 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002870 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002871 struct wl_resource *output_resource)
2872{
2873 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002874 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002875 struct weston_output *output = output_resource->data;
2876
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002877 surface->configure = input_panel_configure;
2878 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002879 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002880 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002881}
2882
2883static const struct input_panel_interface input_panel_implementation = {
2884 input_panel_set_surface
2885};
2886
2887static void
2888unbind_input_panel(struct wl_resource *resource)
2889{
2890 struct desktop_shell *shell = resource->data;
2891
2892 shell->input_panel.binding = NULL;
2893 free(resource);
2894}
2895
2896static void
2897bind_input_panel(struct wl_client *client,
2898 void *data, uint32_t version, uint32_t id)
2899{
2900 struct desktop_shell *shell = data;
2901 struct wl_resource *resource;
2902
2903 resource = wl_client_add_object(client, &input_panel_interface,
2904 &input_panel_implementation,
2905 id, shell);
2906
2907 if (shell->input_panel.binding == NULL) {
2908 resource->destroy = unbind_input_panel;
2909 shell->input_panel.binding = resource;
2910 return;
2911 }
2912
2913 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2914 "interface object already bound");
2915 wl_resource_destroy(resource);
2916}
2917
Kristian Høgsberg07045392012-02-19 18:52:44 -05002918struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002919 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002920 struct weston_surface *current;
2921 struct wl_listener listener;
2922 struct wl_keyboard_grab grab;
2923};
2924
2925static void
2926switcher_next(struct switcher *switcher)
2927{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002928 struct weston_surface *surface;
2929 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002930 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002931 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002932
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002933 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002934 switch (get_shell_surface_type(surface)) {
2935 case SHELL_SURFACE_TOPLEVEL:
2936 case SHELL_SURFACE_FULLSCREEN:
2937 case SHELL_SURFACE_MAXIMIZED:
2938 if (first == NULL)
2939 first = surface;
2940 if (prev == switcher->current)
2941 next = surface;
2942 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002943 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002944 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002945 weston_surface_damage(surface);
2946 break;
2947 default:
2948 break;
2949 }
Alex Wu1659daa2012-04-01 20:13:09 +08002950
2951 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002952 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002953 surface->geometry.dirty = 1;
2954 weston_surface_damage(surface);
2955 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002956 }
2957
2958 if (next == NULL)
2959 next = first;
2960
Alex Wu07b26062012-03-12 16:06:01 +08002961 if (next == NULL)
2962 return;
2963
Kristian Høgsberg07045392012-02-19 18:52:44 -05002964 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002965 wl_signal_add(&next->surface.resource.destroy_signal,
2966 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002967
2968 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002969 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002970
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002971 shsurf = get_shell_surface(switcher->current);
2972 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002973 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002974}
2975
2976static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002977switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002978{
2979 struct switcher *switcher =
2980 container_of(listener, struct switcher, listener);
2981
2982 switcher_next(switcher);
2983}
2984
2985static void
Daniel Stone351eb612012-05-31 15:27:47 -04002986switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002987{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002988 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002989 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002990 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002991
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002992 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002993 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002994 weston_surface_damage(surface);
2995 }
2996
Alex Wu07b26062012-03-12 16:06:01 +08002997 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002998 activate(switcher->shell, switcher->current,
2999 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003000 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003001 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003002 free(switcher);
3003}
3004
3005static void
3006switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003007 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003008{
3009 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003010 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003011
Daniel Stonec9785ea2012-05-30 16:31:52 +01003012 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003013 switcher_next(switcher);
3014}
3015
3016static void
3017switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3018 uint32_t mods_depressed, uint32_t mods_latched,
3019 uint32_t mods_locked, uint32_t group)
3020{
3021 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003022 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003023
Daniel Stone351eb612012-05-31 15:27:47 -04003024 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3025 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003026}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003027
3028static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003029 switcher_key,
3030 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003031};
3032
3033static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003034switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3035 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003036{
Tiago Vignattibe143262012-04-16 17:31:41 +03003037 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003038 struct switcher *switcher;
3039
3040 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003041 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003042 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003043 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003044 wl_list_init(&switcher->listener.link);
3045
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003046 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003047 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003048 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3049 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003050 switcher_next(switcher);
3051}
3052
Pekka Paalanen3c647232011-12-22 13:43:43 +02003053static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003054backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3055 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003056{
3057 struct weston_compositor *compositor = data;
3058 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003059 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003060
3061 /* TODO: we're limiting to simple use cases, where we assume just
3062 * control on the primary display. We'd have to extend later if we
3063 * ever get support for setting backlights on random desktop LCD
3064 * panels though */
3065 output = get_default_output(compositor);
3066 if (!output)
3067 return;
3068
3069 if (!output->set_backlight)
3070 return;
3071
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003072 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3073 backlight_new = output->backlight_current - 25;
3074 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3075 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003076
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003077 if (backlight_new < 5)
3078 backlight_new = 5;
3079 if (backlight_new > 255)
3080 backlight_new = 255;
3081
3082 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003083 output->set_backlight(output, output->backlight_current);
3084}
3085
3086static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003087debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3088 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003089{
Tiago Vignattibe143262012-04-16 17:31:41 +03003090 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003091 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003092 struct weston_surface *surface;
3093
3094 if (shell->debug_repaint_surface) {
3095 weston_surface_destroy(shell->debug_repaint_surface);
3096 shell->debug_repaint_surface = NULL;
3097 } else {
3098 surface = weston_surface_create(compositor);
3099 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3100 weston_surface_configure(surface, 0, 0, 8192, 8192);
3101 wl_list_insert(&compositor->fade_layer.surface_list,
3102 &surface->layer_link);
3103 weston_surface_assign_output(surface);
3104 pixman_region32_init(&surface->input);
3105
3106 /* Here's the dirty little trick that makes the
3107 * repaint debugging work: we force an
3108 * update_transform first to update dependent state
3109 * and clear the geometry.dirty bit. Then we clear
3110 * the surface damage so it only gets repainted
3111 * piecewise as we repaint other things. */
3112
3113 weston_surface_update_transform(surface);
3114 pixman_region32_fini(&surface->damage);
3115 pixman_region32_init(&surface->damage);
3116 shell->debug_repaint_surface = surface;
3117 }
3118}
3119
3120static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003121force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3122 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003123{
3124 struct wl_client *client;
3125 pid_t pid;
3126 uid_t uid;
3127 gid_t gid;
3128
Daniel Stone325fc2d2012-05-30 16:31:58 +01003129 client = seat->keyboard->focus->resource.client;
3130 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003131
Daniel Stone325fc2d2012-05-30 16:31:58 +01003132 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003133}
3134
3135static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003136workspace_up_binding(struct wl_seat *seat, uint32_t time,
3137 uint32_t key, void *data)
3138{
3139 struct desktop_shell *shell = data;
3140 unsigned int new_index = shell->workspaces.current;
3141
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003142 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003143 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003144 if (new_index != 0)
3145 new_index--;
3146
3147 change_workspace(shell, new_index);
3148}
3149
3150static void
3151workspace_down_binding(struct wl_seat *seat, uint32_t time,
3152 uint32_t key, void *data)
3153{
3154 struct desktop_shell *shell = data;
3155 unsigned int new_index = shell->workspaces.current;
3156
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003157 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003158 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003159 if (new_index < shell->workspaces.num - 1)
3160 new_index++;
3161
3162 change_workspace(shell, new_index);
3163}
3164
3165static void
3166workspace_f_binding(struct wl_seat *seat, uint32_t time,
3167 uint32_t key, void *data)
3168{
3169 struct desktop_shell *shell = data;
3170 unsigned int new_index;
3171
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003172 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003173 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003174 new_index = key - KEY_F1;
3175 if (new_index >= shell->workspaces.num)
3176 new_index = shell->workspaces.num - 1;
3177
3178 change_workspace(shell, new_index);
3179}
3180
3181
3182static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003183shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003184{
Tiago Vignattibe143262012-04-16 17:31:41 +03003185 struct desktop_shell *shell =
3186 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003187 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003188
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003189 if (shell->child.client)
3190 wl_client_destroy(shell->child.client);
3191
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003192 wl_list_remove(&shell->lock_listener.link);
3193 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003194 wl_list_remove(&shell->show_input_panel_listener.link);
3195 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003196
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003197 wl_array_for_each(ws, &shell->workspaces.array)
3198 workspace_destroy(*ws);
3199 wl_array_release(&shell->workspaces.array);
3200
Pekka Paalanen3c647232011-12-22 13:43:43 +02003201 free(shell->screensaver.path);
3202 free(shell);
3203}
3204
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003205static void
3206shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3207{
3208 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003209 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003210
3211 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003212 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3213 MODIFIER_CTRL | MODIFIER_ALT,
3214 terminate_binding, ec);
3215 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3216 click_to_activate_binding,
3217 shell);
3218 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3219 MODIFIER_SUPER | MODIFIER_ALT,
3220 surface_opacity_binding, NULL);
3221 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3222 MODIFIER_SUPER, zoom_axis_binding,
3223 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003224
3225 /* configurable bindings */
3226 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003227 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3228 zoom_key_binding, NULL);
3229 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3230 zoom_key_binding, NULL);
3231 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3232 shell);
3233 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3234 resize_binding, shell);
3235 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3236 rotate_binding, NULL);
3237 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3238 shell);
3239 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3240 ec);
3241 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3242 backlight_binding, ec);
3243 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3244 ec);
3245 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3246 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003247 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003248 debug_repaint_binding, shell);
3249 weston_compositor_add_key_binding(ec, KEY_K, mod,
3250 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003251 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3252 workspace_up_binding, shell);
3253 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3254 workspace_down_binding, shell);
3255
3256 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3257 if (shell->workspaces.num > 1) {
3258 num_workspace_bindings = shell->workspaces.num;
3259 if (num_workspace_bindings > 6)
3260 num_workspace_bindings = 6;
3261 for (i = 0; i < num_workspace_bindings; i++)
3262 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3263 workspace_f_binding,
3264 shell);
3265 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003266}
3267
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003268int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003269shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003270
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003271WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003272shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003273{
Tiago Vignattibe143262012-04-16 17:31:41 +03003274 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003275 struct workspace **pws;
3276 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003277
3278 shell = malloc(sizeof *shell);
3279 if (shell == NULL)
3280 return -1;
3281
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003282 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003283 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003284
3285 shell->destroy_listener.notify = shell_destroy;
3286 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3287 shell->lock_listener.notify = lock;
3288 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3289 shell->unlock_listener.notify = unlock;
3290 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003291 shell->show_input_panel_listener.notify = show_input_panels;
3292 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3293 shell->hide_input_panel_listener.notify = hide_input_panels;
3294 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003295 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003296 ec->shell_interface.create_shell_surface = create_shell_surface;
3297 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003298 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003299 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003300 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003301
Pekka Paalanen77346a62011-11-30 16:26:35 +02003302 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003303 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003304
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003305 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3306 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003307 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3308 weston_layer_init(&shell->lock_layer, NULL);
3309
3310 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003311
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003312 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003313
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003314 for (i = 0; i < shell->workspaces.num; i++) {
3315 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3316 if (pws == NULL)
3317 return -1;
3318
3319 *pws = workspace_create();
3320 if (*pws == NULL)
3321 return -1;
3322 }
3323 activate_workspace(shell, 0);
3324
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003325 wl_list_init(&shell->workspaces.animation.link);
3326 shell->workspaces.animation.frame = animate_workspace_change_frame;
3327
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003328 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3329 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003330 return -1;
3331
Kristian Høgsberg75840622011-09-06 13:48:16 -04003332 if (wl_display_add_global(ec->wl_display,
3333 &desktop_shell_interface,
3334 shell, bind_desktop_shell) == NULL)
3335 return -1;
3336
Pekka Paalanen6e168112011-11-24 11:34:05 +02003337 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3338 shell, bind_screensaver) == NULL)
3339 return -1;
3340
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003341 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3342 shell, bind_input_panel) == NULL)
3343 return -1;
3344
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003345 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003346 if (launch_desktop_shell_process(shell) != 0)
3347 return -1;
3348
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003349 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003350 if (ec->seat->seat.pointer)
3351 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3352 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003353
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003354 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003355
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003356 return 0;
3357}