blob: b477bbe646c978a999d4200bd2094e77c07625ed [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
1017 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001018 if (!move)
1019 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001020
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001021 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001022 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001023 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001024 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001025
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001026 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1027 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001028
1029 return 0;
1030}
1031
1032static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001033shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001034 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001035{
Daniel Stone37816df2012-05-16 18:45:18 +01001036 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001037 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001038
Daniel Stone37816df2012-05-16 18:45:18 +01001039 if (ws->seat.pointer->button_count == 0 ||
1040 ws->seat.pointer->grab_serial != serial ||
1041 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001042 return;
1043
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001044 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001045 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001046}
1047
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001048struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001049 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001050 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001051 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001052};
1053
1054static void
Scott Moreau447013d2012-02-18 05:05:29 -07001055resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001056 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001057{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001058 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001059 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001060 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001061 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001062 wl_fixed_t from_x, from_y;
1063 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001064
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001065 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001066 return;
1067
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001068 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001069 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001070 &from_x, &from_y);
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->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001073
Daniel Stone103db7f2012-05-08 17:17:55 +01001074 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001075 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001076 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001077 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001078 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001079 }
1080
Daniel Stone103db7f2012-05-08 17:17:55 +01001081 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001082 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001083 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001084 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001085 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001086 }
1087
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001088 shsurf->client->send_configure(shsurf->surface,
1089 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001090}
1091
1092static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001093send_configure(struct weston_surface *surface,
1094 uint32_t edges, int32_t width, int32_t height)
1095{
1096 struct shell_surface *shsurf = get_shell_surface(surface);
1097
1098 wl_shell_surface_send_configure(&shsurf->resource,
1099 edges, width, height);
1100}
1101
1102static const struct weston_shell_client shell_client = {
1103 send_configure
1104};
1105
1106static void
Scott Moreau447013d2012-02-18 05:05:29 -07001107resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001108 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001109{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001110 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001111 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001112 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001113
Daniel Stone4dbadb12012-05-30 16:31:51 +01001114 if (pointer->button_count == 0 &&
1115 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001116 shell_grab_end(&resize->base);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001117 free(grab);
1118 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001119}
1120
Scott Moreau447013d2012-02-18 05:05:29 -07001121static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001122 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001123 resize_grab_motion,
1124 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001125};
1126
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001127static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001128surface_resize(struct shell_surface *shsurf,
1129 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001130{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001131 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001132
Alex Wu4539b082012-03-01 12:57:46 +08001133 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1134 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001135
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001136 if (edges == 0 || edges > 15 ||
1137 (edges & 3) == 3 || (edges & 12) == 12)
1138 return 0;
1139
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001140 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001141 if (!resize)
1142 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001143
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001144 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001145 resize->width = shsurf->surface->geometry.width;
1146 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001147
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001148 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1149 ws->seat.pointer, edges);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001150
1151 return 0;
1152}
1153
1154static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001155shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001156 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001157 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001158{
Daniel Stone37816df2012-05-16 18:45:18 +01001159 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001160 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001161
Alex Wu4539b082012-03-01 12:57:46 +08001162 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1163 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001164
Daniel Stone37816df2012-05-16 18:45:18 +01001165 if (ws->seat.pointer->button_count == 0 ||
1166 ws->seat.pointer->grab_serial != serial ||
1167 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001168 return;
1169
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001170 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001171 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001172}
1173
Juan Zhao96879df2012-02-07 08:45:41 +08001174static struct weston_output *
1175get_default_output(struct weston_compositor *compositor)
1176{
1177 return container_of(compositor->output_list.next,
1178 struct weston_output, link);
1179}
1180
Alex Wu4539b082012-03-01 12:57:46 +08001181static void
1182shell_unset_fullscreen(struct shell_surface *shsurf)
1183{
1184 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001185 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1186 shell_surface_is_top_fullscreen(shsurf)) {
1187 weston_output_switch_mode(shsurf->fullscreen_output,
1188 shsurf->fullscreen_output->origin);
1189 }
Alex Wu4539b082012-03-01 12:57:46 +08001190 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1191 shsurf->fullscreen.framerate = 0;
1192 wl_list_remove(&shsurf->fullscreen.transform.link);
1193 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001194 if (shsurf->fullscreen.black_surface)
1195 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001196 shsurf->fullscreen.black_surface = NULL;
1197 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001198 weston_surface_set_position(shsurf->surface,
1199 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001200 if (shsurf->saved_rotation_valid) {
1201 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1202 &shsurf->rotation.transform.link);
1203 shsurf->saved_rotation_valid = false;
1204 }
Alex Wu4539b082012-03-01 12:57:46 +08001205}
1206
Pekka Paalanen98262232011-12-01 10:42:22 +02001207static int
1208reset_shell_surface_type(struct shell_surface *surface)
1209{
1210 switch (surface->type) {
1211 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001212 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001213 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001214 case SHELL_SURFACE_MAXIMIZED:
1215 surface->output = get_default_output(surface->surface->compositor);
1216 weston_surface_set_position(surface->surface,
1217 surface->saved_x,
1218 surface->saved_y);
1219 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001220 case SHELL_SURFACE_NONE:
1221 case SHELL_SURFACE_TOPLEVEL:
1222 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001223 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001224 break;
1225 }
1226
1227 surface->type = SHELL_SURFACE_NONE;
1228 return 0;
1229}
1230
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001231static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001232set_surface_type(struct shell_surface *shsurf)
1233{
1234 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001235 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001236
1237 reset_shell_surface_type(shsurf);
1238
1239 shsurf->type = shsurf->next_type;
1240 shsurf->next_type = SHELL_SURFACE_NONE;
1241
1242 switch (shsurf->type) {
1243 case SHELL_SURFACE_TOPLEVEL:
1244 break;
1245 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001246 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001247 pes->geometry.x + shsurf->transient.x,
1248 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001249 break;
1250
1251 case SHELL_SURFACE_MAXIMIZED:
1252 shsurf->saved_x = surface->geometry.x;
1253 shsurf->saved_y = surface->geometry.y;
1254 shsurf->saved_position_valid = true;
1255 break;
1256
1257 case SHELL_SURFACE_FULLSCREEN:
1258 shsurf->saved_x = surface->geometry.x;
1259 shsurf->saved_y = surface->geometry.y;
1260 shsurf->saved_position_valid = true;
1261
1262 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1263 wl_list_remove(&shsurf->rotation.transform.link);
1264 wl_list_init(&shsurf->rotation.transform.link);
1265 shsurf->surface->geometry.dirty = 1;
1266 shsurf->saved_rotation_valid = true;
1267 }
1268 break;
1269
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001270 default:
1271 break;
1272 }
1273}
1274
1275static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001276set_toplevel(struct shell_surface *shsurf)
1277{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001278 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001279}
1280
1281static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001282shell_surface_set_toplevel(struct wl_client *client,
1283 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001284{
Pekka Paalanen98262232011-12-01 10:42:22 +02001285 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001286
Tiago Vignattibc052c92012-04-19 16:18:18 +03001287 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001288}
1289
1290static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001291set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001292 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001293{
1294 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001295 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001296 shsurf->transient.x = x;
1297 shsurf->transient.y = y;
1298 shsurf->transient.flags = flags;
1299 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1300}
1301
1302static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001303shell_surface_set_transient(struct wl_client *client,
1304 struct wl_resource *resource,
1305 struct wl_resource *parent_resource,
1306 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001307{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001308 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001309 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001310
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001311 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001312}
1313
Tiago Vignattibe143262012-04-16 17:31:41 +03001314static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001315shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001316{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001317 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001318}
1319
1320static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001321get_output_panel_height(struct desktop_shell *shell,
1322 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001323{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001324 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001325 int panel_height = 0;
1326
1327 if (!output)
1328 return 0;
1329
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001330 wl_list_for_each(surface, &shell->panel_layer.surface_list, link) {
1331 if (surface->output == output) {
1332 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001333 break;
1334 }
1335 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001336
Juan Zhao96879df2012-02-07 08:45:41 +08001337 return panel_height;
1338}
1339
1340static void
1341shell_surface_set_maximized(struct wl_client *client,
1342 struct wl_resource *resource,
1343 struct wl_resource *output_resource )
1344{
1345 struct shell_surface *shsurf = resource->data;
1346 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001347 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001348 uint32_t edges = 0, panel_height = 0;
1349
1350 /* get the default output, if the client set it as NULL
1351 check whether the ouput is available */
1352 if (output_resource)
1353 shsurf->output = output_resource->data;
1354 else
1355 shsurf->output = get_default_output(es->compositor);
1356
Tiago Vignattibe143262012-04-16 17:31:41 +03001357 shell = shell_surface_get_shell(shsurf);
1358 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001359 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001360
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001361 shsurf->client->send_configure(shsurf->surface, edges,
1362 es->output->current->width,
1363 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001364
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001365 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001366}
1367
Alex Wu21858432012-04-01 20:13:08 +08001368static void
1369black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1370
Alex Wu4539b082012-03-01 12:57:46 +08001371static struct weston_surface *
1372create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001373 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001374 GLfloat x, GLfloat y, int w, int h)
1375{
1376 struct weston_surface *surface = NULL;
1377
1378 surface = weston_surface_create(ec);
1379 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001380 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001381 return NULL;
1382 }
1383
Alex Wu21858432012-04-01 20:13:08 +08001384 surface->configure = black_surface_configure;
1385 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001386 weston_surface_configure(surface, x, y, w, h);
1387 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1388 return surface;
1389}
1390
1391/* Create black surface and append it to the associated fullscreen surface.
1392 * Handle size dismatch and positioning according to the method. */
1393static void
1394shell_configure_fullscreen(struct shell_surface *shsurf)
1395{
1396 struct weston_output *output = shsurf->fullscreen_output;
1397 struct weston_surface *surface = shsurf->surface;
1398 struct weston_matrix *matrix;
1399 float scale;
1400
1401 center_on_output(surface, output);
1402
1403 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001404 shsurf->fullscreen.black_surface =
1405 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001406 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001407 output->x, output->y,
1408 output->current->width,
1409 output->current->height);
1410
1411 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1412 wl_list_insert(&surface->layer_link,
1413 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001414 shsurf->fullscreen.black_surface->output = output;
1415
1416 switch (shsurf->fullscreen.type) {
1417 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1418 break;
1419 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1420 matrix = &shsurf->fullscreen.transform.matrix;
1421 weston_matrix_init(matrix);
1422 scale = (float)output->current->width/(float)surface->geometry.width;
1423 weston_matrix_scale(matrix, scale, scale, 1);
1424 wl_list_remove(&shsurf->fullscreen.transform.link);
1425 wl_list_insert(surface->geometry.transformation_list.prev,
1426 &shsurf->fullscreen.transform.link);
1427 weston_surface_set_position(surface, output->x, output->y);
1428 break;
1429 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001430 if (shell_surface_is_top_fullscreen(shsurf)) {
1431 struct weston_mode mode = {0,
1432 surface->geometry.width,
1433 surface->geometry.height,
1434 shsurf->fullscreen.framerate};
1435
1436 if (weston_output_switch_mode(output, &mode) == 0) {
1437 weston_surface_configure(shsurf->fullscreen.black_surface,
1438 output->x, output->y,
1439 output->current->width,
1440 output->current->height);
1441 weston_surface_set_position(surface, output->x, output->y);
1442 break;
1443 }
1444 }
Alex Wu4539b082012-03-01 12:57:46 +08001445 break;
1446 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1447 break;
1448 default:
1449 break;
1450 }
1451}
1452
1453/* make the fullscreen and black surface at the top */
1454static void
1455shell_stack_fullscreen(struct shell_surface *shsurf)
1456{
Alex Wubd3354b2012-04-17 17:20:49 +08001457 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001458 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001459 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001460
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001461 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001462 wl_list_insert(&shell->fullscreen_layer.surface_list,
1463 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001464 weston_surface_damage(surface);
1465
1466 if (!shsurf->fullscreen.black_surface)
1467 shsurf->fullscreen.black_surface =
1468 create_black_surface(surface->compositor,
1469 surface,
1470 output->x, output->y,
1471 output->current->width,
1472 output->current->height);
1473
1474 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001475 wl_list_insert(&surface->layer_link,
1476 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001477 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001478}
1479
1480static void
1481shell_map_fullscreen(struct shell_surface *shsurf)
1482{
Alex Wu4539b082012-03-01 12:57:46 +08001483 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001484 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001485}
1486
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001487static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001488shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001489 struct wl_resource *resource,
1490 uint32_t method,
1491 uint32_t framerate,
1492 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001493{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001494 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001495 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001496
1497 if (output_resource)
1498 shsurf->output = output_resource->data;
1499 else
1500 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001501
Alex Wu4539b082012-03-01 12:57:46 +08001502 shsurf->fullscreen_output = shsurf->output;
1503 shsurf->fullscreen.type = method;
1504 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001505 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001506
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001507 shsurf->client->send_configure(shsurf->surface, 0,
1508 shsurf->output->current->width,
1509 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001510}
1511
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001512static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001513popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001514 struct wl_surface *surface,
1515 wl_fixed_t x,
1516 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001517{
Daniel Stone37816df2012-05-16 18:45:18 +01001518 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001519 struct shell_surface *priv =
1520 container_of(grab, struct shell_surface, popup.grab);
1521 struct wl_client *client = priv->surface->surface.resource.client;
1522
Pekka Paalanencb108432012-01-19 16:25:40 +02001523 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001524 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001525 grab->focus = surface;
1526 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001527 wl_pointer_set_focus(pointer, NULL,
1528 wl_fixed_from_int(0),
1529 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001530 grab->focus = NULL;
1531 }
1532}
1533
1534static void
Scott Moreau447013d2012-02-18 05:05:29 -07001535popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001536 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001537{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001538 struct wl_resource *resource;
1539
Daniel Stone37816df2012-05-16 18:45:18 +01001540 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001541 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001542 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001543}
1544
1545static void
Scott Moreau447013d2012-02-18 05:05:29 -07001546popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001547 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001548{
1549 struct wl_resource *resource;
1550 struct shell_surface *shsurf =
1551 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001552 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001553 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001554 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001555
Daniel Stone37816df2012-05-16 18:45:18 +01001556 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001557 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001558 display = wl_client_get_display(resource->client);
1559 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001560 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001561 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001562 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001563 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001564 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001565 wl_pointer_end_grab(grab->pointer);
1566 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001567 }
1568
Daniel Stone4dbadb12012-05-30 16:31:51 +01001569 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001570 shsurf->popup.initial_up = 1;
1571}
1572
Scott Moreau447013d2012-02-18 05:05:29 -07001573static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001574 popup_grab_focus,
1575 popup_grab_motion,
1576 popup_grab_button,
1577};
1578
1579static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001580shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001581{
Daniel Stone37816df2012-05-16 18:45:18 +01001582 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001583 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001584 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001585
1586 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001587 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001588
Pekka Paalanen938269a2012-02-07 14:19:01 +02001589 weston_surface_update_transform(parent);
1590 if (parent->transform.enabled) {
1591 shsurf->popup.parent_transform.matrix =
1592 parent->transform.matrix;
1593 } else {
1594 /* construct x, y translation matrix */
1595 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1596 shsurf->popup.parent_transform.matrix.d[12] =
1597 parent->geometry.x;
1598 shsurf->popup.parent_transform.matrix.d[13] =
1599 parent->geometry.y;
1600 }
1601 wl_list_insert(es->geometry.transformation_list.prev,
1602 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001603 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001605 shsurf->popup.initial_up = 0;
1606
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001607 /* We don't require the grab to still be active, but if another
1608 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001609 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1610 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001611 } else {
1612 wl_shell_surface_send_popup_done(&shsurf->resource);
1613 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001614}
1615
1616static void
1617shell_surface_set_popup(struct wl_client *client,
1618 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001619 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001620 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001621 struct wl_resource *parent_resource,
1622 int32_t x, int32_t y, uint32_t flags)
1623{
1624 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001625
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001626 shsurf->type = SHELL_SURFACE_POPUP;
1627 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001628 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001629 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001630 shsurf->popup.x = x;
1631 shsurf->popup.y = y;
1632}
1633
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001634static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001635 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001636 shell_surface_move,
1637 shell_surface_resize,
1638 shell_surface_set_toplevel,
1639 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001640 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001641 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001642 shell_surface_set_maximized,
1643 shell_surface_set_title,
1644 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001645};
1646
1647static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001648destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001649{
Daniel Stone37816df2012-05-16 18:45:18 +01001650 if (shsurf->popup.grab.pointer)
1651 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001652
Alex Wubd3354b2012-04-17 17:20:49 +08001653 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1654 shell_surface_is_top_fullscreen(shsurf)) {
1655 weston_output_switch_mode(shsurf->fullscreen_output,
1656 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001657 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001658
Alex Wuaa08e2d2012-03-05 11:01:40 +08001659 if (shsurf->fullscreen.black_surface)
1660 weston_surface_destroy(shsurf->fullscreen.black_surface);
1661
Alex Wubd3354b2012-04-17 17:20:49 +08001662 /* As destroy_resource() use wl_list_for_each_safe(),
1663 * we can always remove the listener.
1664 */
1665 wl_list_remove(&shsurf->surface_destroy_listener.link);
1666 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001667 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001668
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001669 wl_list_remove(&shsurf->link);
1670 free(shsurf);
1671}
1672
1673static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001674shell_destroy_shell_surface(struct wl_resource *resource)
1675{
1676 struct shell_surface *shsurf = resource->data;
1677
1678 destroy_shell_surface(shsurf);
1679}
1680
1681static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001682shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001683{
1684 struct shell_surface *shsurf = container_of(listener,
1685 struct shell_surface,
1686 surface_destroy_listener);
1687
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001688 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001689 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001690 } else {
1691 wl_signal_emit(&shsurf->resource.destroy_signal,
1692 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001693 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001694 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001695}
1696
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001697static void
1698shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1699
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001700static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001701get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001702{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001703 if (surface->configure == shell_surface_configure)
1704 return surface->private;
1705 else
1706 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001707}
1708
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001709static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001710create_shell_surface(void *shell, struct weston_surface *surface,
1711 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001712{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001713 struct shell_surface *shsurf;
1714
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001715 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001716 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001717 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001718 }
1719
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001720 shsurf = calloc(1, sizeof *shsurf);
1721 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001722 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001723 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001724 }
1725
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001726 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001727 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001728 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001729
Tiago Vignattibc052c92012-04-19 16:18:18 +03001730 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001731 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001732 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001733 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001734 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001735 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1736 shsurf->fullscreen.framerate = 0;
1737 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001738 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001739 wl_list_init(&shsurf->fullscreen.transform.link);
1740
Tiago Vignattibc052c92012-04-19 16:18:18 +03001741 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001742 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1743 wl_signal_add(&surface->surface.resource.destroy_signal,
1744 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001745
1746 /* init link so its safe to always remove it in destroy_shell_surface */
1747 wl_list_init(&shsurf->link);
1748
Pekka Paalanen460099f2012-01-20 16:48:25 +02001749 /* empty when not in use */
1750 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001751 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001752
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001753 wl_list_init(&shsurf->workspace_transform.link);
1754
Pekka Paalanen98262232011-12-01 10:42:22 +02001755 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001756 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001757
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001758 shsurf->client = client;
1759
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001760 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001761}
1762
1763static void
1764shell_get_shell_surface(struct wl_client *client,
1765 struct wl_resource *resource,
1766 uint32_t id,
1767 struct wl_resource *surface_resource)
1768{
1769 struct weston_surface *surface = surface_resource->data;
1770 struct desktop_shell *shell = resource->data;
1771 struct shell_surface *shsurf;
1772
1773 if (get_shell_surface(surface)) {
1774 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001775 WL_DISPLAY_ERROR_INVALID_OBJECT,
1776 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001777 return;
1778 }
1779
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001780 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001781 if (!shsurf) {
1782 wl_resource_post_error(surface_resource,
1783 WL_DISPLAY_ERROR_INVALID_OBJECT,
1784 "surface->configure already set");
1785 return;
1786 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001787
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001788 shsurf->resource.destroy = shell_destroy_shell_surface;
1789 shsurf->resource.object.id = id;
1790 shsurf->resource.object.interface = &wl_shell_surface_interface;
1791 shsurf->resource.object.implementation =
1792 (void (**)(void)) &shell_surface_implementation;
1793 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001794
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001795 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001796}
1797
1798static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001799 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001800};
1801
Kristian Høgsberg07937562011-04-12 17:25:42 -04001802static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001803handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001804{
1805 proc->pid = 0;
1806}
1807
1808static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001809launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001810{
1811 if (shell->screensaver.binding)
1812 return;
1813
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001814 if (!shell->screensaver.path)
1815 return;
1816
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001817 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001818 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001819 return;
1820 }
1821
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001822 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001823 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001824 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001825 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001826}
1827
1828static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001829terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001830{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001831 if (shell->screensaver.process.pid == 0)
1832 return;
1833
1834 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001835}
1836
1837static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001838configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001839{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001840 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001841
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001842 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1843 if (s->output == es->output && s != es) {
1844 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001845 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001846 }
1847 }
1848
1849 weston_surface_configure(es, es->output->x, es->output->y,
1850 es->buffer->width, es->buffer->height);
1851
1852 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001853 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001854 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001855 }
1856}
1857
1858static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001859background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1860{
1861 struct desktop_shell *shell = es->private;
1862
1863 configure_static_surface(es, &shell->background_layer);
1864}
1865
1866static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001867desktop_shell_set_background(struct wl_client *client,
1868 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001869 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001870 struct wl_resource *surface_resource)
1871{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001872 struct desktop_shell *shell = resource->data;
1873 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001874
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001875 if (surface->configure) {
1876 wl_resource_post_error(surface_resource,
1877 WL_DISPLAY_ERROR_INVALID_OBJECT,
1878 "surface role already assigned");
1879 return;
1880 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001881
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001882 surface->configure = background_configure;
1883 surface->private = shell;
1884 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001885 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001886 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001887 surface->output->current->width,
1888 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001889}
1890
1891static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001892panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1893{
1894 struct desktop_shell *shell = es->private;
1895
1896 configure_static_surface(es, &shell->panel_layer);
1897}
1898
1899static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001900desktop_shell_set_panel(struct wl_client *client,
1901 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001902 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001903 struct wl_resource *surface_resource)
1904{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001905 struct desktop_shell *shell = resource->data;
1906 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001907
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001908 if (surface->configure) {
1909 wl_resource_post_error(surface_resource,
1910 WL_DISPLAY_ERROR_INVALID_OBJECT,
1911 "surface role already assigned");
1912 return;
1913 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001914
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001915 surface->configure = panel_configure;
1916 surface->private = shell;
1917 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001918 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001919 surface_resource,
1920 surface->output->current->width,
1921 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001922}
1923
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001924static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001925lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1926{
1927 struct desktop_shell *shell = surface->private;
1928
1929 center_on_output(surface, get_default_output(shell->compositor));
1930
1931 if (!weston_surface_is_mapped(surface)) {
1932 wl_list_insert(&shell->lock_layer.surface_list,
1933 &surface->layer_link);
1934 weston_surface_assign_output(surface);
1935 weston_compositor_wake(shell->compositor);
1936 }
1937}
1938
1939static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001940handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001941{
Tiago Vignattibe143262012-04-16 17:31:41 +03001942 struct desktop_shell *shell =
1943 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001944
Martin Minarik6d118362012-06-07 18:01:59 +02001945 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001946 shell->lock_surface = NULL;
1947}
1948
1949static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001950desktop_shell_set_lock_surface(struct wl_client *client,
1951 struct wl_resource *resource,
1952 struct wl_resource *surface_resource)
1953{
Tiago Vignattibe143262012-04-16 17:31:41 +03001954 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001955 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001956
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001957 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001958
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001959 if (!shell->locked)
1960 return;
1961
Pekka Paalanen98262232011-12-01 10:42:22 +02001962 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001963
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001964 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1965 wl_signal_add(&surface_resource->destroy_signal,
1966 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001967
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001968 surface->configure = lock_surface_configure;
1969 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001970}
1971
1972static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001973resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001974{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001975 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001976 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001977
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001978 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1979 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001980
1981 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001982
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001983 wl_list_remove(&shell->lock_layer.link);
1984 wl_list_insert(&shell->compositor->cursor_layer.link,
1985 &shell->fullscreen_layer.link);
1986 wl_list_insert(&shell->fullscreen_layer.link,
1987 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001988 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001989
Jonas Ådahl04769742012-06-13 00:01:24 +02001990 pop_focus_state(shell, get_current_workspace(shell));
1991
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001992 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001993 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001994 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001995 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001996}
1997
1998static void
1999desktop_shell_unlock(struct wl_client *client,
2000 struct wl_resource *resource)
2001{
Tiago Vignattibe143262012-04-16 17:31:41 +03002002 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002003
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002004 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002005
2006 if (shell->locked)
2007 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002008}
2009
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002010static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002011desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002012 struct wl_resource *resource,
2013 struct wl_resource *surface_resource)
2014{
2015 struct desktop_shell *shell = resource->data;
2016
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002017 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002018}
2019
Kristian Høgsberg75840622011-09-06 13:48:16 -04002020static const struct desktop_shell_interface desktop_shell_implementation = {
2021 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002022 desktop_shell_set_panel,
2023 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002024 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002025 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002026};
2027
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002028static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002029get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002030{
2031 struct shell_surface *shsurf;
2032
2033 shsurf = get_shell_surface(surface);
2034 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002035 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002036 return shsurf->type;
2037}
2038
Kristian Høgsberg75840622011-09-06 13:48:16 -04002039static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002040move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002041{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002042 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002043 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002044 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002045
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002046 if (surface == NULL)
2047 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002048
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002049 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002050 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002051 return;
2052
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002053 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002054}
2055
2056static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002057resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002058{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002059 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002060 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002061 uint32_t edges = 0;
2062 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002063 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002064
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002065 if (surface == NULL)
2066 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002067
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002068 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002069 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002070 return;
2071
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002072 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002073 wl_fixed_to_int(seat->pointer->grab_x),
2074 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002075 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002076
Pekka Paalanen60921e52012-01-25 15:55:43 +02002077 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002078 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002079 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002080 edges |= 0;
2081 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002082 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002083
Pekka Paalanen60921e52012-01-25 15:55:43 +02002084 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002085 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002086 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002087 edges |= 0;
2088 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002089 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002090
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002091 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002092}
2093
2094static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002095surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002096 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002097{
Scott Moreau02709af2012-05-22 01:54:10 -06002098 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002099 struct shell_surface *shsurf;
2100 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002101 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002102
2103 if (surface == NULL)
2104 return;
2105
2106 shsurf = get_shell_surface(surface);
2107 if (!shsurf)
2108 return;
2109
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002110 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002111
Scott Moreau02709af2012-05-22 01:54:10 -06002112 if (surface->alpha > 1.0)
2113 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002114 if (surface->alpha < step)
2115 surface->alpha = step;
2116
2117 surface->geometry.dirty = 1;
2118 weston_surface_damage(surface);
2119}
2120
2121static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002122do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002123 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002124{
Daniel Stone37816df2012-05-16 18:45:18 +01002125 struct weston_seat *ws = (struct weston_seat *) seat;
2126 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002127 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002128 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002129
2130 wl_list_for_each(output, &compositor->output_list, link) {
2131 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002132 wl_fixed_to_double(seat->pointer->x),
2133 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002134 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002135 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002136 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002137 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002138 increment = -output->zoom.increment;
2139 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002140 increment = output->zoom.increment *
2141 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002142 else
2143 increment = 0;
2144
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002145 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002146
Scott Moreaue6603982012-06-11 13:07:51 -06002147 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002148 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002149 else if (output->zoom.level > output->zoom.max_level)
2150 output->zoom.level = output->zoom.max_level;
2151 else
2152 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002153
Scott Moreaue6603982012-06-11 13:07:51 -06002154 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002155
Scott Moreau8dacaab2012-06-17 18:10:58 -06002156 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002157 }
2158 }
2159}
2160
Scott Moreauccbf29d2012-02-22 14:21:41 -07002161static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002162zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002163 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002164{
2165 do_zoom(seat, time, 0, axis, value);
2166}
2167
2168static void
2169zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2170 void *data)
2171{
2172 do_zoom(seat, time, key, 0, 0);
2173}
2174
2175static void
2176terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2177 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002178{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002179 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002180
Daniel Stone325fc2d2012-05-30 16:31:58 +01002181 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002182}
2183
2184static void
Scott Moreau447013d2012-02-18 05:05:29 -07002185rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002186 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002187{
2188 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002189 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002190 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002191 struct shell_surface *shsurf = rotate->base.shsurf;
2192 struct weston_surface *surface;
2193 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2194
2195 if (!shsurf)
2196 return;
2197
2198 surface = shsurf->surface;
2199
2200 cx = 0.5f * surface->geometry.width;
2201 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002202
Daniel Stone37816df2012-05-16 18:45:18 +01002203 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2204 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002205 r = sqrtf(dx * dx + dy * dy);
2206
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002207 wl_list_remove(&shsurf->rotation.transform.link);
2208 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002209
2210 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002211 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002212 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002213
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002214 weston_matrix_init(&rotate->rotation);
2215 rotate->rotation.d[0] = dx / r;
2216 rotate->rotation.d[4] = -dy / r;
2217 rotate->rotation.d[1] = -rotate->rotation.d[4];
2218 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002219
2220 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002221 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002222 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002223 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002224 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002225
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002226 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002227 &shsurf->surface->geometry.transformation_list,
2228 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002229 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002230 wl_list_init(&shsurf->rotation.transform.link);
2231 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002232 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002233 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002234
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002235 /* We need to adjust the position of the surface
2236 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002237 cposx = surface->geometry.x + cx;
2238 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002239 dposx = rotate->center.x - cposx;
2240 dposy = rotate->center.y - cposy;
2241 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002242 weston_surface_set_position(surface,
2243 surface->geometry.x + dposx,
2244 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002245 }
2246
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002247 /* Repaint implies weston_surface_update_transform(), which
2248 * lazily applies the damage due to rotation update.
2249 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002250 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002251}
2252
2253static void
Scott Moreau447013d2012-02-18 05:05:29 -07002254rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002255 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002256{
2257 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002258 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002259 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002260 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002261 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002262
Daniel Stone4dbadb12012-05-30 16:31:51 +01002263 if (pointer->button_count == 0 &&
2264 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002265 if (shsurf)
2266 weston_matrix_multiply(&shsurf->rotation.rotation,
2267 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002268 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002269 free(rotate);
2270 }
2271}
2272
Scott Moreau447013d2012-02-18 05:05:29 -07002273static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002274 noop_grab_focus,
2275 rotate_grab_motion,
2276 rotate_grab_button,
2277};
2278
2279static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002280rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002281 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002282{
2283 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002284 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002285 struct shell_surface *surface;
2286 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002287 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002288 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002289
2290 if (base_surface == NULL)
2291 return;
2292
2293 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002294 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002295 return;
2296
Pekka Paalanen460099f2012-01-20 16:48:25 +02002297 rotate = malloc(sizeof *rotate);
2298 if (!rotate)
2299 return;
2300
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002301 weston_surface_to_global_float(surface->surface,
2302 surface->surface->geometry.width / 2,
2303 surface->surface->geometry.height / 2,
2304 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002305
Daniel Stone37816df2012-05-16 18:45:18 +01002306 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2307 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002308 r = sqrtf(dx * dx + dy * dy);
2309 if (r > 20.0f) {
2310 struct weston_matrix inverse;
2311
2312 weston_matrix_init(&inverse);
2313 inverse.d[0] = dx / r;
2314 inverse.d[4] = dy / r;
2315 inverse.d[1] = -inverse.d[4];
2316 inverse.d[5] = inverse.d[0];
2317 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002318
2319 weston_matrix_init(&rotate->rotation);
2320 rotate->rotation.d[0] = dx / r;
2321 rotate->rotation.d[4] = -dy / r;
2322 rotate->rotation.d[1] = -rotate->rotation.d[4];
2323 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002324 } else {
2325 weston_matrix_init(&surface->rotation.rotation);
2326 weston_matrix_init(&rotate->rotation);
2327 }
2328
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002329 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2330 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002331}
2332
2333static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002334lower_fullscreen_layer(struct desktop_shell *shell)
2335{
2336 struct workspace *ws;
2337 struct weston_surface *surface, *prev;
2338
2339 ws = get_current_workspace(shell);
2340 wl_list_for_each_reverse_safe(surface, prev,
2341 &shell->fullscreen_layer.surface_list,
2342 layer_link)
2343 weston_surface_restack(surface, &ws->layer.surface_list);
2344}
2345
2346static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002347activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002348 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002349{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002350 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002351
Daniel Stone37816df2012-05-16 18:45:18 +01002352 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002353
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002354 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002355 case SHELL_SURFACE_FULLSCREEN:
2356 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002357 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002358 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002359 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002360 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002361 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002362 lower_fullscreen_layer(shell);
2363 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002364 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002365 }
2366}
2367
Alex Wu21858432012-04-01 20:13:08 +08002368/* no-op func for checking black surface */
2369static void
2370black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2371{
2372}
2373
2374static bool
2375is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2376{
2377 if (es->configure == black_surface_configure) {
2378 if (fs_surface)
2379 *fs_surface = (struct weston_surface *)es->private;
2380 return true;
2381 }
2382 return false;
2383}
2384
Kristian Høgsberg75840622011-09-06 13:48:16 -04002385static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002386click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002387 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002388{
Daniel Stone37816df2012-05-16 18:45:18 +01002389 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002390 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002391 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002392 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002393
Daniel Stone37816df2012-05-16 18:45:18 +01002394 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002395 if (!focus)
2396 return;
2397
Alex Wu21858432012-04-01 20:13:08 +08002398 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002399 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002400
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002401 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2402 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002403
Daniel Stone325fc2d2012-05-30 16:31:58 +01002404 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002405 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002406}
2407
2408static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002409lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002410{
Tiago Vignattibe143262012-04-16 17:31:41 +03002411 struct desktop_shell *shell =
2412 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002413 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002414 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002415
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002416 if (shell->locked) {
2417 wl_list_for_each(output, &shell->compositor->output_list, link)
2418 /* TODO: find a way to jump to other DPMS levels */
2419 if (output->set_dpms)
2420 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002421 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002422 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002423
2424 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002425
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002426 /* Hide all surfaces by removing the fullscreen, panel and
2427 * toplevel layers. This way nothing else can show or receive
2428 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002429
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002430 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002431 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002432 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002433 wl_list_insert(&shell->compositor->cursor_layer.link,
2434 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002435
Pekka Paalanen77346a62011-11-30 16:26:35 +02002436 launch_screensaver(shell);
2437
Jonas Ådahl04769742012-06-13 00:01:24 +02002438 /* stash keyboard foci in current workspace */
2439 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002440
2441 /* TODO: disable bindings that should not work while locked. */
2442
2443 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002444}
2445
2446static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002447unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002448{
Tiago Vignattibe143262012-04-16 17:31:41 +03002449 struct desktop_shell *shell =
2450 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002451
Pekka Paalanend81c2162011-11-16 13:47:34 +02002452 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002453 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002454 return;
2455 }
2456
2457 /* If desktop-shell client has gone away, unlock immediately. */
2458 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002459 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002460 return;
2461 }
2462
2463 if (shell->prepare_event_sent)
2464 return;
2465
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002466 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002467 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002468}
2469
2470static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002471show_input_panels(struct wl_listener *listener, void *data)
2472{
2473 struct desktop_shell *shell =
2474 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002475 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002476
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002477 wl_list_for_each_safe(surface, next,
2478 &shell->input_panel.surfaces, layer_link) {
2479 wl_list_remove(&surface->layer_link);
2480 wl_list_insert(&shell->panel_layer.surface_list,
2481 &surface->layer_link);
2482 weston_surface_assign_output(surface);
2483 weston_surface_damage(surface);
2484 weston_slide_run(surface,
2485 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002486 }
2487}
2488
2489static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002490input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2491
2492static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002493hide_input_panels(struct wl_listener *listener, void *data)
2494{
2495 struct desktop_shell *shell =
2496 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002497 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002498
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002499 wl_list_for_each_safe(surface, next,
2500 &shell->panel_layer.surface_list, layer_link)
2501 if (surface->configure == input_panel_configure) {
2502 weston_surface_unmap(surface);
2503 wl_list_insert(&shell->input_panel.surfaces,
2504 &surface->layer_link);
2505 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002506}
2507
2508static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002509center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002510{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002511 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002512 GLfloat x = (mode->width - surface->buffer->width) / 2;
2513 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002514
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002515 weston_surface_configure(surface, output->x + x, output->y + y,
2516 surface->buffer->width,
2517 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002518}
2519
2520static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002521map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002522 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002523{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002524 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002525 struct shell_surface *shsurf = get_shell_surface(surface);
2526 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002527 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002528 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002529 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002530 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002531
Pekka Paalanen60921e52012-01-25 15:55:43 +02002532 surface->geometry.width = width;
2533 surface->geometry.height = height;
2534 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002535
2536 /* initial positioning, see also configure() */
2537 switch (surface_type) {
2538 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002539 weston_surface_set_position(surface, 10 + random() % 400,
2540 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002541 break;
Alex Wu4539b082012-03-01 12:57:46 +08002542 case SHELL_SURFACE_FULLSCREEN:
2543 shell_map_fullscreen(shsurf);
2544 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002545 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002546 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002547 panel_height = get_output_panel_height(shell,surface->output);
2548 weston_surface_set_position(surface, surface->output->x,
2549 surface->output->y + panel_height);
2550 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002551 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002552 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002553 case SHELL_SURFACE_NONE:
2554 weston_surface_set_position(surface,
2555 surface->geometry.x + sx,
2556 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002557 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002558 default:
2559 ;
2560 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002561
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002562 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002563 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002564 case SHELL_SURFACE_POPUP:
2565 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002566 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002567 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2568 break;
Alex Wu4539b082012-03-01 12:57:46 +08002569 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002570 case SHELL_SURFACE_NONE:
2571 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002572 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002573 ws = get_current_workspace(shell);
2574 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002575 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002576 }
2577
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002578 if (surface_type != SHELL_SURFACE_NONE) {
2579 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002580 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2581 surface->output = shsurf->output;
2582 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002583
Juan Zhao7bb92f02011-12-15 11:31:51 -05002584 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002585 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002586 if (shsurf->transient.flags ==
2587 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2588 break;
2589 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002590 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002591 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002592 if (!shell->locked) {
2593 wl_list_for_each(seat, &compositor->seat_list, link)
2594 activate(shell, surface, seat);
2595 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002596 break;
2597 default:
2598 break;
2599 }
2600
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002601 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002602 {
2603 switch (shell->win_animation_type) {
2604 case ANIMATION_FADE:
2605 weston_fade_run(surface, NULL, NULL);
2606 break;
2607 case ANIMATION_ZOOM:
2608 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2609 break;
2610 default:
2611 break;
2612 }
2613 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002614}
2615
2616static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002617configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002618 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002619{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002620 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2621 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002622
Pekka Paalanen77346a62011-11-30 16:26:35 +02002623 shsurf = get_shell_surface(surface);
2624 if (shsurf)
2625 surface_type = shsurf->type;
2626
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002627 surface->geometry.x = x;
2628 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002629 surface->geometry.width = width;
2630 surface->geometry.height = height;
2631 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002632
2633 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002634 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002635 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002636 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002637 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002638 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002639 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002640 surface->geometry.x = surface->output->x;
2641 surface->geometry.y = surface->output->y +
2642 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002643 break;
Alex Wu4539b082012-03-01 12:57:46 +08002644 case SHELL_SURFACE_TOPLEVEL:
2645 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002646 default:
2647 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002648 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002649
Alex Wu4539b082012-03-01 12:57:46 +08002650 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002651 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002652 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002653
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002654 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002655 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002656 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002657}
2658
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002659static void
2660shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2661{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002662 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002663 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002664 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002665
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002666 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002667 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002668 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002669 type_changed = 1;
2670 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002671
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002672 if (!weston_surface_is_mapped(es)) {
2673 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002674 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002675 es->geometry.width != es->buffer->width ||
2676 es->geometry.height != es->buffer->height) {
2677 GLfloat from_x, from_y;
2678 GLfloat to_x, to_y;
2679
2680 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2681 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2682 configure(shell, es,
2683 es->geometry.x + to_x - from_x,
2684 es->geometry.y + to_y - from_y,
2685 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002686 }
2687}
2688
Tiago Vignattibe143262012-04-16 17:31:41 +03002689static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002690
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002691static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002692desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002693{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002694 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002695 struct desktop_shell *shell =
2696 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002697
2698 shell->child.process.pid = 0;
2699 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002700
2701 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2702 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002703 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002704 shell->child.deathstamp = time;
2705 shell->child.deathcount = 0;
2706 }
2707
2708 shell->child.deathcount++;
2709 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002710 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002711 return;
2712 }
2713
Martin Minarik6d118362012-06-07 18:01:59 +02002714 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002715 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002716}
2717
2718static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002719launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002720{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002721 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002722
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002723 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002724 &shell->child.process,
2725 shell_exe,
2726 desktop_shell_sigchld);
2727
2728 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002729 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002730 return 0;
2731}
2732
2733static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002734bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2735{
Tiago Vignattibe143262012-04-16 17:31:41 +03002736 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002737
2738 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002739 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002740}
2741
Kristian Høgsberg75840622011-09-06 13:48:16 -04002742static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002743unbind_desktop_shell(struct wl_resource *resource)
2744{
Tiago Vignattibe143262012-04-16 17:31:41 +03002745 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002746
2747 if (shell->locked)
2748 resume_desktop(shell);
2749
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002750 shell->child.desktop_shell = NULL;
2751 shell->prepare_event_sent = false;
2752 free(resource);
2753}
2754
2755static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002756bind_desktop_shell(struct wl_client *client,
2757 void *data, uint32_t version, uint32_t id)
2758{
Tiago Vignattibe143262012-04-16 17:31:41 +03002759 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002760 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002761
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002762 resource = wl_client_add_object(client, &desktop_shell_interface,
2763 &desktop_shell_implementation,
2764 id, shell);
2765
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002766 if (client == shell->child.client) {
2767 resource->destroy = unbind_desktop_shell;
2768 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002769 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002770 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002771
2772 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2773 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002774 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002775}
2776
Pekka Paalanen6e168112011-11-24 11:34:05 +02002777static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002778screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2779{
2780 struct desktop_shell *shell = surface->private;
2781
2782 if (!shell->locked)
2783 return;
2784
2785 center_on_output(surface, surface->output);
2786
2787 if (wl_list_empty(&surface->layer_link)) {
2788 wl_list_insert(shell->lock_layer.surface_list.prev,
2789 &surface->layer_link);
2790 weston_surface_assign_output(surface);
2791 shell->compositor->idle_time = shell->screensaver.duration;
2792 weston_compositor_wake(shell->compositor);
2793 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2794 }
2795}
2796
2797static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002798screensaver_set_surface(struct wl_client *client,
2799 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002800 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002801 struct wl_resource *output_resource)
2802{
Tiago Vignattibe143262012-04-16 17:31:41 +03002803 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002804 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002805 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002806
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002807 surface->configure = screensaver_configure;
2808 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002809 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002810}
2811
2812static const struct screensaver_interface screensaver_implementation = {
2813 screensaver_set_surface
2814};
2815
2816static void
2817unbind_screensaver(struct wl_resource *resource)
2818{
Tiago Vignattibe143262012-04-16 17:31:41 +03002819 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002820
Pekka Paalanen77346a62011-11-30 16:26:35 +02002821 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002822 free(resource);
2823}
2824
2825static void
2826bind_screensaver(struct wl_client *client,
2827 void *data, uint32_t version, uint32_t id)
2828{
Tiago Vignattibe143262012-04-16 17:31:41 +03002829 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002830 struct wl_resource *resource;
2831
2832 resource = wl_client_add_object(client, &screensaver_interface,
2833 &screensaver_implementation,
2834 id, shell);
2835
Pekka Paalanen77346a62011-11-30 16:26:35 +02002836 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002837 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002838 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002839 return;
2840 }
2841
2842 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2843 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002844 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002845}
2846
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002847static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002848input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2849{
2850 struct weston_mode *mode = surface->output->current;
2851 GLfloat x = (mode->width - surface->buffer->width) / 2;
2852 GLfloat y = mode->height - surface->buffer->height;
2853
2854 /* Don't map the input panel here, wait for
2855 * show_input_panels signal. */
2856
2857 weston_surface_configure(surface,
2858 surface->output->x + x,
2859 surface->output->y + y,
2860 surface->buffer->width,
2861 surface->buffer->height);
2862}
2863
2864static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002865input_panel_set_surface(struct wl_client *client,
2866 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002867 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002868 struct wl_resource *output_resource)
2869{
2870 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002871 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002872 struct weston_output *output = output_resource->data;
2873
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002874 surface->configure = input_panel_configure;
2875 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002876 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002877 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002878}
2879
2880static const struct input_panel_interface input_panel_implementation = {
2881 input_panel_set_surface
2882};
2883
2884static void
2885unbind_input_panel(struct wl_resource *resource)
2886{
2887 struct desktop_shell *shell = resource->data;
2888
2889 shell->input_panel.binding = NULL;
2890 free(resource);
2891}
2892
2893static void
2894bind_input_panel(struct wl_client *client,
2895 void *data, uint32_t version, uint32_t id)
2896{
2897 struct desktop_shell *shell = data;
2898 struct wl_resource *resource;
2899
2900 resource = wl_client_add_object(client, &input_panel_interface,
2901 &input_panel_implementation,
2902 id, shell);
2903
2904 if (shell->input_panel.binding == NULL) {
2905 resource->destroy = unbind_input_panel;
2906 shell->input_panel.binding = resource;
2907 return;
2908 }
2909
2910 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2911 "interface object already bound");
2912 wl_resource_destroy(resource);
2913}
2914
Kristian Høgsberg07045392012-02-19 18:52:44 -05002915struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002916 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002917 struct weston_surface *current;
2918 struct wl_listener listener;
2919 struct wl_keyboard_grab grab;
2920};
2921
2922static void
2923switcher_next(struct switcher *switcher)
2924{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002925 struct weston_surface *surface;
2926 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002927 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002928 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002929
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002930 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002931 switch (get_shell_surface_type(surface)) {
2932 case SHELL_SURFACE_TOPLEVEL:
2933 case SHELL_SURFACE_FULLSCREEN:
2934 case SHELL_SURFACE_MAXIMIZED:
2935 if (first == NULL)
2936 first = surface;
2937 if (prev == switcher->current)
2938 next = surface;
2939 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002940 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002941 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002942 weston_surface_damage(surface);
2943 break;
2944 default:
2945 break;
2946 }
Alex Wu1659daa2012-04-01 20:13:09 +08002947
2948 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002949 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002950 surface->geometry.dirty = 1;
2951 weston_surface_damage(surface);
2952 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002953 }
2954
2955 if (next == NULL)
2956 next = first;
2957
Alex Wu07b26062012-03-12 16:06:01 +08002958 if (next == NULL)
2959 return;
2960
Kristian Høgsberg07045392012-02-19 18:52:44 -05002961 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002962 wl_signal_add(&next->surface.resource.destroy_signal,
2963 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002964
2965 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002966 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002967
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002968 shsurf = get_shell_surface(switcher->current);
2969 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002970 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002971}
2972
2973static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002974switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002975{
2976 struct switcher *switcher =
2977 container_of(listener, struct switcher, listener);
2978
2979 switcher_next(switcher);
2980}
2981
2982static void
Daniel Stone351eb612012-05-31 15:27:47 -04002983switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002984{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002985 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002986 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002987 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002988
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002989 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002990 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002991 weston_surface_damage(surface);
2992 }
2993
Alex Wu07b26062012-03-12 16:06:01 +08002994 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002995 activate(switcher->shell, switcher->current,
2996 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002997 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002998 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002999 free(switcher);
3000}
3001
3002static void
3003switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003004 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003005{
3006 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003007 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003008
Daniel Stonec9785ea2012-05-30 16:31:52 +01003009 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003010 switcher_next(switcher);
3011}
3012
3013static void
3014switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3015 uint32_t mods_depressed, uint32_t mods_latched,
3016 uint32_t mods_locked, uint32_t group)
3017{
3018 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003019 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003020
Daniel Stone351eb612012-05-31 15:27:47 -04003021 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3022 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003023}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003024
3025static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003026 switcher_key,
3027 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003028};
3029
3030static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003031switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3032 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003033{
Tiago Vignattibe143262012-04-16 17:31:41 +03003034 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003035 struct switcher *switcher;
3036
3037 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003038 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003039 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003040 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003041 wl_list_init(&switcher->listener.link);
3042
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003043 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003044 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003045 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3046 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003047 switcher_next(switcher);
3048}
3049
Pekka Paalanen3c647232011-12-22 13:43:43 +02003050static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003051backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3052 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003053{
3054 struct weston_compositor *compositor = data;
3055 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003056 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003057
3058 /* TODO: we're limiting to simple use cases, where we assume just
3059 * control on the primary display. We'd have to extend later if we
3060 * ever get support for setting backlights on random desktop LCD
3061 * panels though */
3062 output = get_default_output(compositor);
3063 if (!output)
3064 return;
3065
3066 if (!output->set_backlight)
3067 return;
3068
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003069 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3070 backlight_new = output->backlight_current - 25;
3071 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3072 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003073
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003074 if (backlight_new < 5)
3075 backlight_new = 5;
3076 if (backlight_new > 255)
3077 backlight_new = 255;
3078
3079 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003080 output->set_backlight(output, output->backlight_current);
3081}
3082
3083static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003084debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3085 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003086{
Tiago Vignattibe143262012-04-16 17:31:41 +03003087 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003088 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003089 struct weston_surface *surface;
3090
3091 if (shell->debug_repaint_surface) {
3092 weston_surface_destroy(shell->debug_repaint_surface);
3093 shell->debug_repaint_surface = NULL;
3094 } else {
3095 surface = weston_surface_create(compositor);
3096 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3097 weston_surface_configure(surface, 0, 0, 8192, 8192);
3098 wl_list_insert(&compositor->fade_layer.surface_list,
3099 &surface->layer_link);
3100 weston_surface_assign_output(surface);
3101 pixman_region32_init(&surface->input);
3102
3103 /* Here's the dirty little trick that makes the
3104 * repaint debugging work: we force an
3105 * update_transform first to update dependent state
3106 * and clear the geometry.dirty bit. Then we clear
3107 * the surface damage so it only gets repainted
3108 * piecewise as we repaint other things. */
3109
3110 weston_surface_update_transform(surface);
3111 pixman_region32_fini(&surface->damage);
3112 pixman_region32_init(&surface->damage);
3113 shell->debug_repaint_surface = surface;
3114 }
3115}
3116
3117static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003118force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3119 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003120{
3121 struct wl_client *client;
3122 pid_t pid;
3123 uid_t uid;
3124 gid_t gid;
3125
Daniel Stone325fc2d2012-05-30 16:31:58 +01003126 client = seat->keyboard->focus->resource.client;
3127 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003128
Daniel Stone325fc2d2012-05-30 16:31:58 +01003129 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003130}
3131
3132static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003133workspace_up_binding(struct wl_seat *seat, uint32_t time,
3134 uint32_t key, void *data)
3135{
3136 struct desktop_shell *shell = data;
3137 unsigned int new_index = shell->workspaces.current;
3138
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003139 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003140 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003141 if (new_index != 0)
3142 new_index--;
3143
3144 change_workspace(shell, new_index);
3145}
3146
3147static void
3148workspace_down_binding(struct wl_seat *seat, uint32_t time,
3149 uint32_t key, void *data)
3150{
3151 struct desktop_shell *shell = data;
3152 unsigned int new_index = shell->workspaces.current;
3153
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003154 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003155 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003156 if (new_index < shell->workspaces.num - 1)
3157 new_index++;
3158
3159 change_workspace(shell, new_index);
3160}
3161
3162static void
3163workspace_f_binding(struct wl_seat *seat, uint32_t time,
3164 uint32_t key, void *data)
3165{
3166 struct desktop_shell *shell = data;
3167 unsigned int new_index;
3168
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003169 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003170 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003171 new_index = key - KEY_F1;
3172 if (new_index >= shell->workspaces.num)
3173 new_index = shell->workspaces.num - 1;
3174
3175 change_workspace(shell, new_index);
3176}
3177
3178
3179static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003180shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003181{
Tiago Vignattibe143262012-04-16 17:31:41 +03003182 struct desktop_shell *shell =
3183 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003184 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003185
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003186 if (shell->child.client)
3187 wl_client_destroy(shell->child.client);
3188
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003189 wl_list_remove(&shell->lock_listener.link);
3190 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003191 wl_list_remove(&shell->show_input_panel_listener.link);
3192 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003193
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003194 wl_array_for_each(ws, &shell->workspaces.array)
3195 workspace_destroy(*ws);
3196 wl_array_release(&shell->workspaces.array);
3197
Pekka Paalanen3c647232011-12-22 13:43:43 +02003198 free(shell->screensaver.path);
3199 free(shell);
3200}
3201
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003202static void
3203shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3204{
3205 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003206 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003207
3208 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003209 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3210 MODIFIER_CTRL | MODIFIER_ALT,
3211 terminate_binding, ec);
3212 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3213 click_to_activate_binding,
3214 shell);
3215 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3216 MODIFIER_SUPER | MODIFIER_ALT,
3217 surface_opacity_binding, NULL);
3218 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3219 MODIFIER_SUPER, zoom_axis_binding,
3220 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003221
3222 /* configurable bindings */
3223 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003224 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3225 zoom_key_binding, NULL);
3226 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3227 zoom_key_binding, NULL);
3228 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3229 shell);
3230 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3231 resize_binding, shell);
3232 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3233 rotate_binding, NULL);
3234 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3235 shell);
3236 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3237 ec);
3238 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3239 backlight_binding, ec);
3240 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3241 ec);
3242 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3243 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003244 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003245 debug_repaint_binding, shell);
3246 weston_compositor_add_key_binding(ec, KEY_K, mod,
3247 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003248 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3249 workspace_up_binding, shell);
3250 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3251 workspace_down_binding, shell);
3252
3253 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3254 if (shell->workspaces.num > 1) {
3255 num_workspace_bindings = shell->workspaces.num;
3256 if (num_workspace_bindings > 6)
3257 num_workspace_bindings = 6;
3258 for (i = 0; i < num_workspace_bindings; i++)
3259 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3260 workspace_f_binding,
3261 shell);
3262 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003263}
3264
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003265int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003266shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003267
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003268WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003269shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003270{
Tiago Vignattibe143262012-04-16 17:31:41 +03003271 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003272 struct workspace **pws;
3273 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003274
3275 shell = malloc(sizeof *shell);
3276 if (shell == NULL)
3277 return -1;
3278
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003279 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003280 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003281
3282 shell->destroy_listener.notify = shell_destroy;
3283 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3284 shell->lock_listener.notify = lock;
3285 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3286 shell->unlock_listener.notify = unlock;
3287 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003288 shell->show_input_panel_listener.notify = show_input_panels;
3289 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3290 shell->hide_input_panel_listener.notify = hide_input_panels;
3291 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003292 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003293 ec->shell_interface.create_shell_surface = create_shell_surface;
3294 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003295 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003296 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003297 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003298
Pekka Paalanen77346a62011-11-30 16:26:35 +02003299 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003300 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003301
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003302 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3303 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003304 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3305 weston_layer_init(&shell->lock_layer, NULL);
3306
3307 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003308
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003309 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003310
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003311 for (i = 0; i < shell->workspaces.num; i++) {
3312 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3313 if (pws == NULL)
3314 return -1;
3315
3316 *pws = workspace_create();
3317 if (*pws == NULL)
3318 return -1;
3319 }
3320 activate_workspace(shell, 0);
3321
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003322 wl_list_init(&shell->workspaces.animation.link);
3323 shell->workspaces.animation.frame = animate_workspace_change_frame;
3324
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003325 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3326 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003327 return -1;
3328
Kristian Høgsberg75840622011-09-06 13:48:16 -04003329 if (wl_display_add_global(ec->wl_display,
3330 &desktop_shell_interface,
3331 shell, bind_desktop_shell) == NULL)
3332 return -1;
3333
Pekka Paalanen6e168112011-11-24 11:34:05 +02003334 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3335 shell, bind_screensaver) == NULL)
3336 return -1;
3337
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003338 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3339 shell, bind_input_panel) == NULL)
3340 return -1;
3341
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003342 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003343 if (launch_desktop_shell_process(shell) != 0)
3344 return -1;
3345
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003346 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003347 if (ec->seat->seat.pointer)
3348 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3349 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003350
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003351 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003352
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003353 return 0;
3354}