blob: a8f7127fa125ce7f3d7203725d45130f3f344cdc [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,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400381 surface_destroy_listener);
Jonas Ådahl04769742012-06-13 00:01:24 +0200382
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);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500813 free(grab);
814 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500815}
816
Scott Moreau447013d2012-02-18 05:05:29 -0700817static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500818 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500819 move_grab_motion,
820 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500821};
822
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600823static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400824busy_cursor_grab_focus(struct wl_pointer_grab *base,
825 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600826{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400827 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600828
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400829 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300830 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400831 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600832 }
833}
834
835static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400836busy_cursor_grab_motion(struct wl_pointer_grab *grab,
837 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600838{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400839}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600840
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400841static void
842busy_cursor_grab_button(struct wl_pointer_grab *grab,
843 uint32_t time, uint32_t button, uint32_t state)
844{
845}
846
847static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
848 busy_cursor_grab_focus,
849 busy_cursor_grab_motion,
850 busy_cursor_grab_button,
851};
852
853static void
854set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
855{
856 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400857
858 grab = malloc(sizeof *grab);
859 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600860 return;
861
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300862 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
863 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400864}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600865
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400866static void
867end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
868{
869 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
870
871 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300872 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400873 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600874 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600875}
876
Scott Moreau9521d5e2012-04-19 13:06:17 -0600877static void
878ping_timer_destroy(struct shell_surface *shsurf)
879{
880 if (!shsurf || !shsurf->ping_timer)
881 return;
882
883 if (shsurf->ping_timer->source)
884 wl_event_source_remove(shsurf->ping_timer->source);
885
886 free(shsurf->ping_timer);
887 shsurf->ping_timer = NULL;
888}
889
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400890static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600891ping_timeout_handler(void *data)
892{
893 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400894 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600895
Scott Moreau9521d5e2012-04-19 13:06:17 -0600896 /* Client is not responding */
897 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400898
899 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
900 if (seat->seat.pointer->focus == &shsurf->surface->surface)
901 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600902
903 return 1;
904}
905
906static void
907ping_handler(struct weston_surface *surface, uint32_t serial)
908{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400909 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600910 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400911 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600912
913 if (!shsurf)
914 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400915 if (!shsurf->resource.client)
916 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600917
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +0300918 if (shsurf->surface == shsurf->shell->grab_surface)
919 return;
920
Scott Moreauff1db4a2012-04-17 19:06:18 -0600921 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300922 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600923 if (!shsurf->ping_timer)
924 return;
925
926 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600927 loop = wl_display_get_event_loop(surface->compositor->wl_display);
928 shsurf->ping_timer->source =
929 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
930 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
931
932 wl_shell_surface_send_ping(&shsurf->resource, serial);
933 }
934}
935
936static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400937handle_pointer_focus(struct wl_listener *listener, void *data)
938{
939 struct wl_pointer *pointer = data;
940 struct weston_surface *surface =
941 (struct weston_surface *) pointer->focus;
942 struct weston_compositor *compositor;
943 struct shell_surface *shsurf;
944 uint32_t serial;
945
946 if (!surface)
947 return;
948
949 compositor = surface->compositor;
950 shsurf = get_shell_surface(surface);
951
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300952 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400953 set_busy_cursor(shsurf, pointer);
954 } else {
955 serial = wl_display_next_serial(compositor->wl_display);
956 ping_handler(surface, serial);
957 }
958}
959
960static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600961shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
962 uint32_t serial)
963{
964 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400965 struct desktop_shell *shell = shsurf->shell;
966 struct weston_seat *seat;
967 struct weston_compositor *ec = shsurf->surface->compositor;
968 struct wl_pointer *pointer;
969 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600970
Scott Moreauff1db4a2012-04-17 19:06:18 -0600971 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400972 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600973 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400974 if (was_unresponsive) {
975 /* Received pong from previously unresponsive client */
976 wl_list_for_each(seat, &ec->seat_list, link) {
977 pointer = seat->seat.pointer;
978 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300979 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400980 pointer->current ==
981 &shsurf->surface->surface)
982 end_busy_cursor(shsurf, pointer);
983 }
984 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600985 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600986 }
987}
988
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400989static void
990shell_surface_set_title(struct wl_client *client,
991 struct wl_resource *resource, const char *title)
992{
993 struct shell_surface *shsurf = resource->data;
994
995 free(shsurf->title);
996 shsurf->title = strdup(title);
997}
998
999static void
1000shell_surface_set_class(struct wl_client *client,
1001 struct wl_resource *resource, const char *class)
1002{
1003 struct shell_surface *shsurf = resource->data;
1004
1005 free(shsurf->class);
1006 shsurf->class = strdup(class);
1007}
1008
Scott Moreauff1db4a2012-04-17 19:06:18 -06001009static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001010surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001011{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001012 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001013
1014 if (!shsurf)
1015 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001016
Ander Conselvan de Oliveira00d17bb2012-06-28 18:08:06 +03001017 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1018 return 0;
1019
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001020 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001021 if (!move)
1022 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001024 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001025 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001026 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001027 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001028
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001029 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1030 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001031
1032 return 0;
1033}
1034
1035static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001036shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001037 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001038{
Daniel Stone37816df2012-05-16 18:45:18 +01001039 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001040 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001041
Daniel Stone37816df2012-05-16 18:45:18 +01001042 if (ws->seat.pointer->button_count == 0 ||
1043 ws->seat.pointer->grab_serial != serial ||
1044 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001045 return;
1046
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001047 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001048 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001049}
1050
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001051struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001052 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001053 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001054 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001055};
1056
1057static void
Scott Moreau447013d2012-02-18 05:05:29 -07001058resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001059 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001060{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001061 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001062 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001063 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001064 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001065 wl_fixed_t from_x, from_y;
1066 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001067
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001068 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001069 return;
1070
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001071 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001072 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001073 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001074 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001075 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001076
Daniel Stone103db7f2012-05-08 17:17:55 +01001077 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001078 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001079 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001080 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001081 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001082 }
1083
Daniel Stone103db7f2012-05-08 17:17:55 +01001084 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001085 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001086 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001087 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001088 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001089 }
1090
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001091 shsurf->client->send_configure(shsurf->surface,
1092 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001093}
1094
1095static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001096send_configure(struct weston_surface *surface,
1097 uint32_t edges, int32_t width, int32_t height)
1098{
1099 struct shell_surface *shsurf = get_shell_surface(surface);
1100
1101 wl_shell_surface_send_configure(&shsurf->resource,
1102 edges, width, height);
1103}
1104
1105static const struct weston_shell_client shell_client = {
1106 send_configure
1107};
1108
1109static void
Scott Moreau447013d2012-02-18 05:05:29 -07001110resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001111 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001112{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001113 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001114 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001115 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001116
Daniel Stone4dbadb12012-05-30 16:31:51 +01001117 if (pointer->button_count == 0 &&
1118 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001119 shell_grab_end(&resize->base);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001120 free(grab);
1121 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001122}
1123
Scott Moreau447013d2012-02-18 05:05:29 -07001124static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001125 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001126 resize_grab_motion,
1127 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001128};
1129
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001130static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001131surface_resize(struct shell_surface *shsurf,
1132 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001133{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001134 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001135
Alex Wu4539b082012-03-01 12:57:46 +08001136 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1137 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001138
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001139 if (edges == 0 || edges > 15 ||
1140 (edges & 3) == 3 || (edges & 12) == 12)
1141 return 0;
1142
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001143 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001144 if (!resize)
1145 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001146
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001147 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001148 resize->width = shsurf->surface->geometry.width;
1149 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001150
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001151 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1152 ws->seat.pointer, edges);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001153
1154 return 0;
1155}
1156
1157static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001158shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001159 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001160 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001161{
Daniel Stone37816df2012-05-16 18:45:18 +01001162 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001164
Alex Wu4539b082012-03-01 12:57:46 +08001165 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1166 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001167
Daniel Stone37816df2012-05-16 18:45:18 +01001168 if (ws->seat.pointer->button_count == 0 ||
1169 ws->seat.pointer->grab_serial != serial ||
1170 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001171 return;
1172
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001173 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001174 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001175}
1176
Juan Zhao96879df2012-02-07 08:45:41 +08001177static struct weston_output *
1178get_default_output(struct weston_compositor *compositor)
1179{
1180 return container_of(compositor->output_list.next,
1181 struct weston_output, link);
1182}
1183
Alex Wu4539b082012-03-01 12:57:46 +08001184static void
1185shell_unset_fullscreen(struct shell_surface *shsurf)
1186{
1187 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001188 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1189 shell_surface_is_top_fullscreen(shsurf)) {
1190 weston_output_switch_mode(shsurf->fullscreen_output,
1191 shsurf->fullscreen_output->origin);
1192 }
Alex Wu4539b082012-03-01 12:57:46 +08001193 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1194 shsurf->fullscreen.framerate = 0;
1195 wl_list_remove(&shsurf->fullscreen.transform.link);
1196 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001197 if (shsurf->fullscreen.black_surface)
1198 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001199 shsurf->fullscreen.black_surface = NULL;
1200 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001201 weston_surface_set_position(shsurf->surface,
1202 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001203 if (shsurf->saved_rotation_valid) {
1204 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1205 &shsurf->rotation.transform.link);
1206 shsurf->saved_rotation_valid = false;
1207 }
Alex Wu4539b082012-03-01 12:57:46 +08001208}
1209
Pekka Paalanen98262232011-12-01 10:42:22 +02001210static int
1211reset_shell_surface_type(struct shell_surface *surface)
1212{
1213 switch (surface->type) {
1214 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001215 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001216 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001217 case SHELL_SURFACE_MAXIMIZED:
1218 surface->output = get_default_output(surface->surface->compositor);
1219 weston_surface_set_position(surface->surface,
1220 surface->saved_x,
1221 surface->saved_y);
1222 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001223 case SHELL_SURFACE_NONE:
1224 case SHELL_SURFACE_TOPLEVEL:
1225 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001226 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001227 break;
1228 }
1229
1230 surface->type = SHELL_SURFACE_NONE;
1231 return 0;
1232}
1233
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001234static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001235set_surface_type(struct shell_surface *shsurf)
1236{
1237 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001238 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001239
1240 reset_shell_surface_type(shsurf);
1241
1242 shsurf->type = shsurf->next_type;
1243 shsurf->next_type = SHELL_SURFACE_NONE;
1244
1245 switch (shsurf->type) {
1246 case SHELL_SURFACE_TOPLEVEL:
1247 break;
1248 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001249 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001250 pes->geometry.x + shsurf->transient.x,
1251 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001252 break;
1253
1254 case SHELL_SURFACE_MAXIMIZED:
1255 shsurf->saved_x = surface->geometry.x;
1256 shsurf->saved_y = surface->geometry.y;
1257 shsurf->saved_position_valid = true;
1258 break;
1259
1260 case SHELL_SURFACE_FULLSCREEN:
1261 shsurf->saved_x = surface->geometry.x;
1262 shsurf->saved_y = surface->geometry.y;
1263 shsurf->saved_position_valid = true;
1264
1265 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1266 wl_list_remove(&shsurf->rotation.transform.link);
1267 wl_list_init(&shsurf->rotation.transform.link);
1268 shsurf->surface->geometry.dirty = 1;
1269 shsurf->saved_rotation_valid = true;
1270 }
1271 break;
1272
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001273 default:
1274 break;
1275 }
1276}
1277
1278static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001279set_toplevel(struct shell_surface *shsurf)
1280{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001281 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001282}
1283
1284static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001285shell_surface_set_toplevel(struct wl_client *client,
1286 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001287{
Pekka Paalanen98262232011-12-01 10:42:22 +02001288 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001289
Tiago Vignattibc052c92012-04-19 16:18:18 +03001290 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001291}
1292
1293static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001294set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001295 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001296{
1297 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001298 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001299 shsurf->transient.x = x;
1300 shsurf->transient.y = y;
1301 shsurf->transient.flags = flags;
1302 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1303}
1304
1305static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001306shell_surface_set_transient(struct wl_client *client,
1307 struct wl_resource *resource,
1308 struct wl_resource *parent_resource,
1309 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001310{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001311 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001312 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001313
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001314 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001315}
1316
Tiago Vignattibe143262012-04-16 17:31:41 +03001317static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001318shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001319{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001320 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001321}
1322
1323static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001324get_output_panel_height(struct desktop_shell *shell,
1325 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001326{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001327 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001328 int panel_height = 0;
1329
1330 if (!output)
1331 return 0;
1332
Juan Zhao4ab94682012-07-09 22:24:09 -07001333 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001334 if (surface->output == output) {
1335 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001336 break;
1337 }
1338 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001339
Juan Zhao96879df2012-02-07 08:45:41 +08001340 return panel_height;
1341}
1342
1343static void
1344shell_surface_set_maximized(struct wl_client *client,
1345 struct wl_resource *resource,
1346 struct wl_resource *output_resource )
1347{
1348 struct shell_surface *shsurf = resource->data;
1349 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001350 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001351 uint32_t edges = 0, panel_height = 0;
1352
1353 /* get the default output, if the client set it as NULL
1354 check whether the ouput is available */
1355 if (output_resource)
1356 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001357 else if (es->output)
1358 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001359 else
1360 shsurf->output = get_default_output(es->compositor);
1361
Tiago Vignattibe143262012-04-16 17:31:41 +03001362 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001363 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001364 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001365
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001366 shsurf->client->send_configure(shsurf->surface, edges,
Rob Bradford31b68622012-07-02 19:00:19 +01001367 shsurf->output->current->width,
1368 shsurf->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001369
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001370 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001371}
1372
Alex Wu21858432012-04-01 20:13:08 +08001373static void
1374black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1375
Alex Wu4539b082012-03-01 12:57:46 +08001376static struct weston_surface *
1377create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001378 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001379 GLfloat x, GLfloat y, int w, int h)
1380{
1381 struct weston_surface *surface = NULL;
1382
1383 surface = weston_surface_create(ec);
1384 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001385 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001386 return NULL;
1387 }
1388
Alex Wu21858432012-04-01 20:13:08 +08001389 surface->configure = black_surface_configure;
1390 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001391 weston_surface_configure(surface, x, y, w, h);
1392 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1393 return surface;
1394}
1395
1396/* Create black surface and append it to the associated fullscreen surface.
1397 * Handle size dismatch and positioning according to the method. */
1398static void
1399shell_configure_fullscreen(struct shell_surface *shsurf)
1400{
1401 struct weston_output *output = shsurf->fullscreen_output;
1402 struct weston_surface *surface = shsurf->surface;
1403 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001404 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001405
Alex Wu4539b082012-03-01 12:57:46 +08001406 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001407 shsurf->fullscreen.black_surface =
1408 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001409 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001410 output->x, output->y,
1411 output->current->width,
1412 output->current->height);
1413
1414 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1415 wl_list_insert(&surface->layer_link,
1416 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001417 shsurf->fullscreen.black_surface->output = output;
1418
1419 switch (shsurf->fullscreen.type) {
1420 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001421 if (surface->buffer)
1422 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001423 break;
1424 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1425 matrix = &shsurf->fullscreen.transform.matrix;
1426 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001427
1428 output_aspect = (float) output->current->width /
1429 (float) output->current->height;
1430 surface_aspect = (float) surface->geometry.width /
1431 (float) surface->geometry.height;
1432 if (output_aspect < surface_aspect)
1433 scale = (float) output->current->width /
1434 (float) surface->geometry.width;
1435 else
1436 scale = (float) output->current->height /
1437 (float) surface->geometry.height;
1438
Alex Wu4539b082012-03-01 12:57:46 +08001439 weston_matrix_scale(matrix, scale, scale, 1);
1440 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001441 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001442 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001443 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1444 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1445 weston_surface_set_position(surface, x, y);
1446
Alex Wu4539b082012-03-01 12:57:46 +08001447 break;
1448 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001449 if (shell_surface_is_top_fullscreen(shsurf)) {
1450 struct weston_mode mode = {0,
1451 surface->geometry.width,
1452 surface->geometry.height,
1453 shsurf->fullscreen.framerate};
1454
1455 if (weston_output_switch_mode(output, &mode) == 0) {
1456 weston_surface_configure(shsurf->fullscreen.black_surface,
1457 output->x, output->y,
1458 output->current->width,
1459 output->current->height);
1460 weston_surface_set_position(surface, output->x, output->y);
1461 break;
1462 }
1463 }
Alex Wu4539b082012-03-01 12:57:46 +08001464 break;
1465 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1466 break;
1467 default:
1468 break;
1469 }
1470}
1471
1472/* make the fullscreen and black surface at the top */
1473static void
1474shell_stack_fullscreen(struct shell_surface *shsurf)
1475{
Alex Wubd3354b2012-04-17 17:20:49 +08001476 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001477 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001478 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001479
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001480 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001481 wl_list_insert(&shell->fullscreen_layer.surface_list,
1482 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001483 weston_surface_damage(surface);
1484
1485 if (!shsurf->fullscreen.black_surface)
1486 shsurf->fullscreen.black_surface =
1487 create_black_surface(surface->compositor,
1488 surface,
1489 output->x, output->y,
1490 output->current->width,
1491 output->current->height);
1492
1493 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001494 wl_list_insert(&surface->layer_link,
1495 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001496 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001497}
1498
1499static void
1500shell_map_fullscreen(struct shell_surface *shsurf)
1501{
Alex Wu4539b082012-03-01 12:57:46 +08001502 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001503 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001504}
1505
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001506static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001507shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001508 struct wl_resource *resource,
1509 uint32_t method,
1510 uint32_t framerate,
1511 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001512{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001513 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001514 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001515
1516 if (output_resource)
1517 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001518 else if (es->output)
1519 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001520 else
1521 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001522
Alex Wu4539b082012-03-01 12:57:46 +08001523 shsurf->fullscreen_output = shsurf->output;
1524 shsurf->fullscreen.type = method;
1525 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001526 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001527
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001528 shsurf->client->send_configure(shsurf->surface, 0,
1529 shsurf->output->current->width,
1530 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001531}
1532
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001533static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001534popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001535 struct wl_surface *surface,
1536 wl_fixed_t x,
1537 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001538{
Daniel Stone37816df2012-05-16 18:45:18 +01001539 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001540 struct shell_surface *priv =
1541 container_of(grab, struct shell_surface, popup.grab);
1542 struct wl_client *client = priv->surface->surface.resource.client;
1543
Pekka Paalanencb108432012-01-19 16:25:40 +02001544 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001545 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001546 grab->focus = surface;
1547 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001548 wl_pointer_set_focus(pointer, NULL,
1549 wl_fixed_from_int(0),
1550 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001551 grab->focus = NULL;
1552 }
1553}
1554
1555static void
Scott Moreau447013d2012-02-18 05:05:29 -07001556popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001557 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001558{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001559 struct wl_resource *resource;
1560
Daniel Stone37816df2012-05-16 18:45:18 +01001561 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001562 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001563 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564}
1565
1566static void
Scott Moreau447013d2012-02-18 05:05:29 -07001567popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001568 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001569{
1570 struct wl_resource *resource;
1571 struct shell_surface *shsurf =
1572 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001573 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001574 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001575 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001576
Daniel Stone37816df2012-05-16 18:45:18 +01001577 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001578 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001579 display = wl_client_get_display(resource->client);
1580 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001581 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001582 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001583 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001584 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001585 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001586 wl_pointer_end_grab(grab->pointer);
1587 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001588 }
1589
Daniel Stone4dbadb12012-05-30 16:31:51 +01001590 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001591 shsurf->popup.initial_up = 1;
1592}
1593
Scott Moreau447013d2012-02-18 05:05:29 -07001594static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001595 popup_grab_focus,
1596 popup_grab_motion,
1597 popup_grab_button,
1598};
1599
1600static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001601shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001602{
Daniel Stone37816df2012-05-16 18:45:18 +01001603 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001605 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606
1607 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001608 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001609
Pekka Paalanen938269a2012-02-07 14:19:01 +02001610 weston_surface_update_transform(parent);
1611 if (parent->transform.enabled) {
1612 shsurf->popup.parent_transform.matrix =
1613 parent->transform.matrix;
1614 } else {
1615 /* construct x, y translation matrix */
1616 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1617 shsurf->popup.parent_transform.matrix.d[12] =
1618 parent->geometry.x;
1619 shsurf->popup.parent_transform.matrix.d[13] =
1620 parent->geometry.y;
1621 }
1622 wl_list_insert(es->geometry.transformation_list.prev,
1623 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001624 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001625
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001626 shsurf->popup.initial_up = 0;
1627
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001628 /* We don't require the grab to still be active, but if another
1629 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001630 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1631 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001632 } else {
1633 wl_shell_surface_send_popup_done(&shsurf->resource);
1634 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001635}
1636
1637static void
1638shell_surface_set_popup(struct wl_client *client,
1639 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001640 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001641 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001642 struct wl_resource *parent_resource,
1643 int32_t x, int32_t y, uint32_t flags)
1644{
1645 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001646
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001647 shsurf->type = SHELL_SURFACE_POPUP;
1648 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001649 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001650 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001651 shsurf->popup.x = x;
1652 shsurf->popup.y = y;
1653}
1654
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001655static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001656 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001657 shell_surface_move,
1658 shell_surface_resize,
1659 shell_surface_set_toplevel,
1660 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001661 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001662 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001663 shell_surface_set_maximized,
1664 shell_surface_set_title,
1665 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001666};
1667
1668static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001669destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001670{
Daniel Stone37816df2012-05-16 18:45:18 +01001671 if (shsurf->popup.grab.pointer)
1672 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001673
Alex Wubd3354b2012-04-17 17:20:49 +08001674 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1675 shell_surface_is_top_fullscreen(shsurf)) {
1676 weston_output_switch_mode(shsurf->fullscreen_output,
1677 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001678 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001679
Alex Wuaa08e2d2012-03-05 11:01:40 +08001680 if (shsurf->fullscreen.black_surface)
1681 weston_surface_destroy(shsurf->fullscreen.black_surface);
1682
Alex Wubd3354b2012-04-17 17:20:49 +08001683 /* As destroy_resource() use wl_list_for_each_safe(),
1684 * we can always remove the listener.
1685 */
1686 wl_list_remove(&shsurf->surface_destroy_listener.link);
1687 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001688 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001689
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001690 wl_list_remove(&shsurf->link);
1691 free(shsurf);
1692}
1693
1694static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001695shell_destroy_shell_surface(struct wl_resource *resource)
1696{
1697 struct shell_surface *shsurf = resource->data;
1698
1699 destroy_shell_surface(shsurf);
1700}
1701
1702static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001703shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001704{
1705 struct shell_surface *shsurf = container_of(listener,
1706 struct shell_surface,
1707 surface_destroy_listener);
1708
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001709 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001710 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001711 } else {
1712 wl_signal_emit(&shsurf->resource.destroy_signal,
1713 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001714 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001715 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001716}
1717
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001718static void
1719shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1720
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001721static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001722get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001723{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001724 if (surface->configure == shell_surface_configure)
1725 return surface->private;
1726 else
1727 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001728}
1729
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001730static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001731create_shell_surface(void *shell, struct weston_surface *surface,
1732 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001733{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001734 struct shell_surface *shsurf;
1735
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001736 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001737 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001738 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001739 }
1740
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001741 shsurf = calloc(1, sizeof *shsurf);
1742 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001743 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001744 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001745 }
1746
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001747 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001748 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001749
Tiago Vignattibc052c92012-04-19 16:18:18 +03001750 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001751 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001752 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001753 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001754 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001755 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1756 shsurf->fullscreen.framerate = 0;
1757 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001758 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001759 wl_list_init(&shsurf->fullscreen.transform.link);
1760
Tiago Vignattibc052c92012-04-19 16:18:18 +03001761 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001762 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1763 wl_signal_add(&surface->surface.resource.destroy_signal,
1764 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001765
1766 /* init link so its safe to always remove it in destroy_shell_surface */
1767 wl_list_init(&shsurf->link);
1768
Pekka Paalanen460099f2012-01-20 16:48:25 +02001769 /* empty when not in use */
1770 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001771 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001772
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001773 wl_list_init(&shsurf->workspace_transform.link);
1774
Pekka Paalanen98262232011-12-01 10:42:22 +02001775 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001776 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001777
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001778 shsurf->client = client;
1779
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001780 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001781}
1782
1783static void
1784shell_get_shell_surface(struct wl_client *client,
1785 struct wl_resource *resource,
1786 uint32_t id,
1787 struct wl_resource *surface_resource)
1788{
1789 struct weston_surface *surface = surface_resource->data;
1790 struct desktop_shell *shell = resource->data;
1791 struct shell_surface *shsurf;
1792
1793 if (get_shell_surface(surface)) {
1794 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001795 WL_DISPLAY_ERROR_INVALID_OBJECT,
1796 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001797 return;
1798 }
1799
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001800 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001801 if (!shsurf) {
1802 wl_resource_post_error(surface_resource,
1803 WL_DISPLAY_ERROR_INVALID_OBJECT,
1804 "surface->configure already set");
1805 return;
1806 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001807
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001808 shsurf->resource.destroy = shell_destroy_shell_surface;
1809 shsurf->resource.object.id = id;
1810 shsurf->resource.object.interface = &wl_shell_surface_interface;
1811 shsurf->resource.object.implementation =
1812 (void (**)(void)) &shell_surface_implementation;
1813 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001814
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001815 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001816}
1817
1818static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001819 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001820};
1821
Kristian Høgsberg07937562011-04-12 17:25:42 -04001822static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001823handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001824{
1825 proc->pid = 0;
1826}
1827
1828static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001829launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001830{
1831 if (shell->screensaver.binding)
1832 return;
1833
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001834 if (!shell->screensaver.path)
1835 return;
1836
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001837 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001838 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001839 return;
1840 }
1841
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001842 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001843 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001844 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001845 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001846}
1847
1848static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001849terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001850{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001851 if (shell->screensaver.process.pid == 0)
1852 return;
1853
1854 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001855}
1856
1857static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001858configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001859{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001860 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001861
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001862 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1863 if (s->output == es->output && s != es) {
1864 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001865 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001866 }
1867 }
1868
1869 weston_surface_configure(es, es->output->x, es->output->y,
1870 es->buffer->width, es->buffer->height);
1871
1872 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001873 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001874 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001875 }
1876}
1877
1878static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001879background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1880{
1881 struct desktop_shell *shell = es->private;
1882
1883 configure_static_surface(es, &shell->background_layer);
1884}
1885
1886static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001887desktop_shell_set_background(struct wl_client *client,
1888 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001889 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001890 struct wl_resource *surface_resource)
1891{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001892 struct desktop_shell *shell = resource->data;
1893 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001894
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001895 if (surface->configure) {
1896 wl_resource_post_error(surface_resource,
1897 WL_DISPLAY_ERROR_INVALID_OBJECT,
1898 "surface role already assigned");
1899 return;
1900 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001901
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001902 surface->configure = background_configure;
1903 surface->private = shell;
1904 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001905 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001906 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001907 surface->output->current->width,
1908 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001909}
1910
1911static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001912panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1913{
1914 struct desktop_shell *shell = es->private;
1915
1916 configure_static_surface(es, &shell->panel_layer);
1917}
1918
1919static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001920desktop_shell_set_panel(struct wl_client *client,
1921 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001922 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001923 struct wl_resource *surface_resource)
1924{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001925 struct desktop_shell *shell = resource->data;
1926 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001927
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001928 if (surface->configure) {
1929 wl_resource_post_error(surface_resource,
1930 WL_DISPLAY_ERROR_INVALID_OBJECT,
1931 "surface role already assigned");
1932 return;
1933 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001934
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001935 surface->configure = panel_configure;
1936 surface->private = shell;
1937 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001938 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001939 surface_resource,
1940 surface->output->current->width,
1941 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001942}
1943
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001944static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001945lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1946{
1947 struct desktop_shell *shell = surface->private;
1948
1949 center_on_output(surface, get_default_output(shell->compositor));
1950
1951 if (!weston_surface_is_mapped(surface)) {
1952 wl_list_insert(&shell->lock_layer.surface_list,
1953 &surface->layer_link);
1954 weston_surface_assign_output(surface);
1955 weston_compositor_wake(shell->compositor);
1956 }
1957}
1958
1959static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001960handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001961{
Tiago Vignattibe143262012-04-16 17:31:41 +03001962 struct desktop_shell *shell =
1963 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001964
Martin Minarik6d118362012-06-07 18:01:59 +02001965 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001966 shell->lock_surface = NULL;
1967}
1968
1969static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001970desktop_shell_set_lock_surface(struct wl_client *client,
1971 struct wl_resource *resource,
1972 struct wl_resource *surface_resource)
1973{
Tiago Vignattibe143262012-04-16 17:31:41 +03001974 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001975 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001976
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001977 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001978
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001979 if (!shell->locked)
1980 return;
1981
Pekka Paalanen98262232011-12-01 10:42:22 +02001982 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001983
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001984 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1985 wl_signal_add(&surface_resource->destroy_signal,
1986 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001987
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001988 surface->configure = lock_surface_configure;
1989 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001990}
1991
1992static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001993resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001994{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001995 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001996 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001997
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001998 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1999 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002000
2001 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002002
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002003 wl_list_remove(&shell->lock_layer.link);
2004 wl_list_insert(&shell->compositor->cursor_layer.link,
2005 &shell->fullscreen_layer.link);
2006 wl_list_insert(&shell->fullscreen_layer.link,
2007 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002008 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002009
Jonas Ådahl04769742012-06-13 00:01:24 +02002010 pop_focus_state(shell, get_current_workspace(shell));
2011
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002012 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002013 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002014 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002015 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002016}
2017
2018static void
2019desktop_shell_unlock(struct wl_client *client,
2020 struct wl_resource *resource)
2021{
Tiago Vignattibe143262012-04-16 17:31:41 +03002022 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002023
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002024 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002025
2026 if (shell->locked)
2027 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002028}
2029
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002030static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002031desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002032 struct wl_resource *resource,
2033 struct wl_resource *surface_resource)
2034{
2035 struct desktop_shell *shell = resource->data;
2036
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002037 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002038}
2039
Kristian Høgsberg75840622011-09-06 13:48:16 -04002040static const struct desktop_shell_interface desktop_shell_implementation = {
2041 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002042 desktop_shell_set_panel,
2043 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002044 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002045 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002046};
2047
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002048static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002049get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002050{
2051 struct shell_surface *shsurf;
2052
2053 shsurf = get_shell_surface(surface);
2054 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002055 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002056 return shsurf->type;
2057}
2058
Kristian Høgsberg75840622011-09-06 13:48:16 -04002059static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002060move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002061{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002062 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002063 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002064 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002065
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002066 if (surface == NULL)
2067 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002068
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002069 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002070 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002071 return;
2072
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002073 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002074}
2075
2076static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002077resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002078{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002079 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002080 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002081 uint32_t edges = 0;
2082 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002083 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002084
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002085 if (surface == NULL)
2086 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002087
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002088 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002089 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002090 return;
2091
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002092 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002093 wl_fixed_to_int(seat->pointer->grab_x),
2094 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002095 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002096
Pekka Paalanen60921e52012-01-25 15:55:43 +02002097 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002098 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002099 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002100 edges |= 0;
2101 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002102 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002103
Pekka Paalanen60921e52012-01-25 15:55:43 +02002104 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002105 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002106 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002107 edges |= 0;
2108 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002109 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002110
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002111 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002112}
2113
2114static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002115surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002116 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002117{
Scott Moreau02709af2012-05-22 01:54:10 -06002118 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002119 struct shell_surface *shsurf;
2120 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002121 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002122
2123 if (surface == NULL)
2124 return;
2125
2126 shsurf = get_shell_surface(surface);
2127 if (!shsurf)
2128 return;
2129
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002130 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002131
Scott Moreau02709af2012-05-22 01:54:10 -06002132 if (surface->alpha > 1.0)
2133 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002134 if (surface->alpha < step)
2135 surface->alpha = step;
2136
2137 surface->geometry.dirty = 1;
2138 weston_surface_damage(surface);
2139}
2140
2141static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002142do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002143 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002144{
Daniel Stone37816df2012-05-16 18:45:18 +01002145 struct weston_seat *ws = (struct weston_seat *) seat;
2146 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002147 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002148 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002149
2150 wl_list_for_each(output, &compositor->output_list, link) {
2151 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002152 wl_fixed_to_double(seat->pointer->x),
2153 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002154 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002155 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002156 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002157 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002158 increment = -output->zoom.increment;
2159 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002160 increment = output->zoom.increment *
2161 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002162 else
2163 increment = 0;
2164
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002165 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002166
Scott Moreaue6603982012-06-11 13:07:51 -06002167 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002168 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002169 else if (output->zoom.level > output->zoom.max_level)
2170 output->zoom.level = output->zoom.max_level;
2171 else
2172 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002173
Scott Moreaue6603982012-06-11 13:07:51 -06002174 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002175
Scott Moreau8dacaab2012-06-17 18:10:58 -06002176 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002177 }
2178 }
2179}
2180
Scott Moreauccbf29d2012-02-22 14:21:41 -07002181static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002182zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002183 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002184{
2185 do_zoom(seat, time, 0, axis, value);
2186}
2187
2188static void
2189zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2190 void *data)
2191{
2192 do_zoom(seat, time, key, 0, 0);
2193}
2194
2195static void
2196terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2197 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002198{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002199 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002200
Daniel Stone325fc2d2012-05-30 16:31:58 +01002201 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002202}
2203
2204static void
Scott Moreau447013d2012-02-18 05:05:29 -07002205rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002206 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002207{
2208 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002209 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002210 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002211 struct shell_surface *shsurf = rotate->base.shsurf;
2212 struct weston_surface *surface;
2213 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2214
2215 if (!shsurf)
2216 return;
2217
2218 surface = shsurf->surface;
2219
2220 cx = 0.5f * surface->geometry.width;
2221 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002222
Daniel Stone37816df2012-05-16 18:45:18 +01002223 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2224 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002225 r = sqrtf(dx * dx + dy * dy);
2226
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002227 wl_list_remove(&shsurf->rotation.transform.link);
2228 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002229
2230 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002231 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002232 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002233
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002234 weston_matrix_init(&rotate->rotation);
2235 rotate->rotation.d[0] = dx / r;
2236 rotate->rotation.d[4] = -dy / r;
2237 rotate->rotation.d[1] = -rotate->rotation.d[4];
2238 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002239
2240 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002241 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002242 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002243 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002244 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002245
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002246 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002247 &shsurf->surface->geometry.transformation_list,
2248 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002249 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002250 wl_list_init(&shsurf->rotation.transform.link);
2251 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002252 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002253 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002254
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002255 /* We need to adjust the position of the surface
2256 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002257 cposx = surface->geometry.x + cx;
2258 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002259 dposx = rotate->center.x - cposx;
2260 dposy = rotate->center.y - cposy;
2261 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002262 weston_surface_set_position(surface,
2263 surface->geometry.x + dposx,
2264 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002265 }
2266
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002267 /* Repaint implies weston_surface_update_transform(), which
2268 * lazily applies the damage due to rotation update.
2269 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002270 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002271}
2272
2273static void
Scott Moreau447013d2012-02-18 05:05:29 -07002274rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002275 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002276{
2277 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002278 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002279 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002280 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002281 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002282
Daniel Stone4dbadb12012-05-30 16:31:51 +01002283 if (pointer->button_count == 0 &&
2284 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002285 if (shsurf)
2286 weston_matrix_multiply(&shsurf->rotation.rotation,
2287 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002288 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002289 free(rotate);
2290 }
2291}
2292
Scott Moreau447013d2012-02-18 05:05:29 -07002293static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002294 noop_grab_focus,
2295 rotate_grab_motion,
2296 rotate_grab_button,
2297};
2298
2299static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002300rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002301 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002302{
2303 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002304 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002305 struct shell_surface *surface;
2306 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002307 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002308 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002309
2310 if (base_surface == NULL)
2311 return;
2312
2313 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002314 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002315 return;
2316
Pekka Paalanen460099f2012-01-20 16:48:25 +02002317 rotate = malloc(sizeof *rotate);
2318 if (!rotate)
2319 return;
2320
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002321 weston_surface_to_global_float(surface->surface,
2322 surface->surface->geometry.width / 2,
2323 surface->surface->geometry.height / 2,
2324 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002325
Daniel Stone37816df2012-05-16 18:45:18 +01002326 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2327 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002328 r = sqrtf(dx * dx + dy * dy);
2329 if (r > 20.0f) {
2330 struct weston_matrix inverse;
2331
2332 weston_matrix_init(&inverse);
2333 inverse.d[0] = dx / r;
2334 inverse.d[4] = dy / r;
2335 inverse.d[1] = -inverse.d[4];
2336 inverse.d[5] = inverse.d[0];
2337 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002338
2339 weston_matrix_init(&rotate->rotation);
2340 rotate->rotation.d[0] = dx / r;
2341 rotate->rotation.d[4] = -dy / r;
2342 rotate->rotation.d[1] = -rotate->rotation.d[4];
2343 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002344 } else {
2345 weston_matrix_init(&surface->rotation.rotation);
2346 weston_matrix_init(&rotate->rotation);
2347 }
2348
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002349 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2350 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002351}
2352
2353static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002354lower_fullscreen_layer(struct desktop_shell *shell)
2355{
2356 struct workspace *ws;
2357 struct weston_surface *surface, *prev;
2358
2359 ws = get_current_workspace(shell);
2360 wl_list_for_each_reverse_safe(surface, prev,
2361 &shell->fullscreen_layer.surface_list,
2362 layer_link)
2363 weston_surface_restack(surface, &ws->layer.surface_list);
2364}
2365
2366static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002367activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002368 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002369{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002370 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002371
Daniel Stone37816df2012-05-16 18:45:18 +01002372 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002373
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002374 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002375 case SHELL_SURFACE_FULLSCREEN:
2376 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002377 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002378 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002379 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002380 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002381 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002382 lower_fullscreen_layer(shell);
2383 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002384 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002385 }
2386}
2387
Alex Wu21858432012-04-01 20:13:08 +08002388/* no-op func for checking black surface */
2389static void
2390black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2391{
2392}
2393
2394static bool
2395is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2396{
2397 if (es->configure == black_surface_configure) {
2398 if (fs_surface)
2399 *fs_surface = (struct weston_surface *)es->private;
2400 return true;
2401 }
2402 return false;
2403}
2404
Kristian Høgsberg75840622011-09-06 13:48:16 -04002405static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002406click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002407 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002408{
Daniel Stone37816df2012-05-16 18:45:18 +01002409 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002410 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002411 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002412 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002413
Daniel Stone37816df2012-05-16 18:45:18 +01002414 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002415 if (!focus)
2416 return;
2417
Alex Wu21858432012-04-01 20:13:08 +08002418 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002419 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002420
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002421 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2422 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002423
Daniel Stone325fc2d2012-05-30 16:31:58 +01002424 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002425 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002426}
2427
2428static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002429lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002430{
Tiago Vignattibe143262012-04-16 17:31:41 +03002431 struct desktop_shell *shell =
2432 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002433 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002434 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002435
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002436 if (shell->locked) {
2437 wl_list_for_each(output, &shell->compositor->output_list, link)
2438 /* TODO: find a way to jump to other DPMS levels */
2439 if (output->set_dpms)
2440 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002441 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002442 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002443
2444 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002445
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002446 /* Hide all surfaces by removing the fullscreen, panel and
2447 * toplevel layers. This way nothing else can show or receive
2448 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002449
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002450 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002451 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002452 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002453 wl_list_insert(&shell->compositor->cursor_layer.link,
2454 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002455
Pekka Paalanen77346a62011-11-30 16:26:35 +02002456 launch_screensaver(shell);
2457
Jonas Ådahl04769742012-06-13 00:01:24 +02002458 /* stash keyboard foci in current workspace */
2459 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002460
2461 /* TODO: disable bindings that should not work while locked. */
2462
2463 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002464}
2465
2466static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002467unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002468{
Tiago Vignattibe143262012-04-16 17:31:41 +03002469 struct desktop_shell *shell =
2470 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002471
Pekka Paalanend81c2162011-11-16 13:47:34 +02002472 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002473 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002474 return;
2475 }
2476
2477 /* If desktop-shell client has gone away, unlock immediately. */
2478 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002479 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002480 return;
2481 }
2482
2483 if (shell->prepare_event_sent)
2484 return;
2485
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002486 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002487 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002488}
2489
2490static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002491show_input_panels(struct wl_listener *listener, void *data)
2492{
2493 struct desktop_shell *shell =
2494 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002495 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002496
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002497 wl_list_for_each_safe(surface, next,
2498 &shell->input_panel.surfaces, layer_link) {
2499 wl_list_remove(&surface->layer_link);
2500 wl_list_insert(&shell->panel_layer.surface_list,
2501 &surface->layer_link);
2502 weston_surface_assign_output(surface);
2503 weston_surface_damage(surface);
2504 weston_slide_run(surface,
2505 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002506 }
2507}
2508
2509static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002510input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2511
2512static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002513hide_input_panels(struct wl_listener *listener, void *data)
2514{
2515 struct desktop_shell *shell =
2516 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002517 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002518
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002519 wl_list_for_each_safe(surface, next,
2520 &shell->panel_layer.surface_list, layer_link)
2521 if (surface->configure == input_panel_configure) {
2522 weston_surface_unmap(surface);
2523 wl_list_insert(&shell->input_panel.surfaces,
2524 &surface->layer_link);
2525 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002526}
2527
2528static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002529center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002530{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002531 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002532 GLfloat x = (mode->width - surface->buffer->width) / 2;
2533 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002534
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002535 weston_surface_configure(surface, output->x + x, output->y + y,
2536 surface->buffer->width,
2537 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002538}
2539
2540static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002541map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002542 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002543{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002544 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002545 struct shell_surface *shsurf = get_shell_surface(surface);
2546 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002547 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002548 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002549 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002550 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002551
Pekka Paalanen60921e52012-01-25 15:55:43 +02002552 surface->geometry.width = width;
2553 surface->geometry.height = height;
2554 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002555
2556 /* initial positioning, see also configure() */
2557 switch (surface_type) {
2558 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002559 weston_surface_set_position(surface, 10 + random() % 400,
2560 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002561 break;
Alex Wu4539b082012-03-01 12:57:46 +08002562 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002563 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002564 shell_map_fullscreen(shsurf);
2565 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002566 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002567 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002568 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002569 weston_surface_set_position(surface, shsurf->output->x,
2570 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002571 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002572 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002573 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002574 case SHELL_SURFACE_NONE:
2575 weston_surface_set_position(surface,
2576 surface->geometry.x + sx,
2577 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002578 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002579 default:
2580 ;
2581 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002582
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002583 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002584 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002585 case SHELL_SURFACE_POPUP:
2586 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002587 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002588 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2589 break;
Alex Wu4539b082012-03-01 12:57:46 +08002590 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002591 case SHELL_SURFACE_NONE:
2592 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002593 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002594 ws = get_current_workspace(shell);
2595 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002596 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002597 }
2598
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002599 if (surface_type != SHELL_SURFACE_NONE) {
2600 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002601 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2602 surface->output = shsurf->output;
2603 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002604
Juan Zhao7bb92f02011-12-15 11:31:51 -05002605 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002606 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002607 if (shsurf->transient.flags ==
2608 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2609 break;
2610 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002611 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002612 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002613 if (!shell->locked) {
2614 wl_list_for_each(seat, &compositor->seat_list, link)
2615 activate(shell, surface, seat);
2616 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002617 break;
2618 default:
2619 break;
2620 }
2621
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002622 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002623 {
2624 switch (shell->win_animation_type) {
2625 case ANIMATION_FADE:
2626 weston_fade_run(surface, NULL, NULL);
2627 break;
2628 case ANIMATION_ZOOM:
2629 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2630 break;
2631 default:
2632 break;
2633 }
2634 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002635}
2636
2637static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002638configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002639 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002640{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002641 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2642 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002643
Pekka Paalanen77346a62011-11-30 16:26:35 +02002644 shsurf = get_shell_surface(surface);
2645 if (shsurf)
2646 surface_type = shsurf->type;
2647
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002648 surface->geometry.x = x;
2649 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002650 surface->geometry.width = width;
2651 surface->geometry.height = height;
2652 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002653
2654 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002655 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002656 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002657 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002658 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002659 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002660 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002661 surface->geometry.x = surface->output->x;
2662 surface->geometry.y = surface->output->y +
2663 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002664 break;
Alex Wu4539b082012-03-01 12:57:46 +08002665 case SHELL_SURFACE_TOPLEVEL:
2666 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002667 default:
2668 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002669 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002670
Alex Wu4539b082012-03-01 12:57:46 +08002671 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002672 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002673 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002674
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002675 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002676 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002677 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002678}
2679
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002680static void
2681shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2682{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002683 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002684 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002685 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002686
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002687 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002688 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002689 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002690 type_changed = 1;
2691 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002692
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002693 if (!weston_surface_is_mapped(es)) {
2694 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002695 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002696 es->geometry.width != es->buffer->width ||
2697 es->geometry.height != es->buffer->height) {
2698 GLfloat from_x, from_y;
2699 GLfloat to_x, to_y;
2700
2701 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2702 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2703 configure(shell, es,
2704 es->geometry.x + to_x - from_x,
2705 es->geometry.y + to_y - from_y,
2706 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002707 }
2708}
2709
Tiago Vignattibe143262012-04-16 17:31:41 +03002710static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002711
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002712static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002713desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002714{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002715 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002716 struct desktop_shell *shell =
2717 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002718
2719 shell->child.process.pid = 0;
2720 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002721
2722 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2723 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002724 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002725 shell->child.deathstamp = time;
2726 shell->child.deathcount = 0;
2727 }
2728
2729 shell->child.deathcount++;
2730 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002731 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002732 return;
2733 }
2734
Martin Minarik6d118362012-06-07 18:01:59 +02002735 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002736 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002737}
2738
2739static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002740launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002741{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002742 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002743
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002744 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002745 &shell->child.process,
2746 shell_exe,
2747 desktop_shell_sigchld);
2748
2749 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002750 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002751 return 0;
2752}
2753
2754static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002755bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2756{
Tiago Vignattibe143262012-04-16 17:31:41 +03002757 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002758
2759 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002760 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002761}
2762
Kristian Høgsberg75840622011-09-06 13:48:16 -04002763static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002764unbind_desktop_shell(struct wl_resource *resource)
2765{
Tiago Vignattibe143262012-04-16 17:31:41 +03002766 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002767
2768 if (shell->locked)
2769 resume_desktop(shell);
2770
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002771 shell->child.desktop_shell = NULL;
2772 shell->prepare_event_sent = false;
2773 free(resource);
2774}
2775
2776static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002777bind_desktop_shell(struct wl_client *client,
2778 void *data, uint32_t version, uint32_t id)
2779{
Tiago Vignattibe143262012-04-16 17:31:41 +03002780 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002781 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002782
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002783 resource = wl_client_add_object(client, &desktop_shell_interface,
2784 &desktop_shell_implementation,
2785 id, shell);
2786
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002787 if (client == shell->child.client) {
2788 resource->destroy = unbind_desktop_shell;
2789 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002790 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002791 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002792
2793 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2794 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002795 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002796}
2797
Pekka Paalanen6e168112011-11-24 11:34:05 +02002798static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002799screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2800{
2801 struct desktop_shell *shell = surface->private;
2802
2803 if (!shell->locked)
2804 return;
2805
2806 center_on_output(surface, surface->output);
2807
2808 if (wl_list_empty(&surface->layer_link)) {
2809 wl_list_insert(shell->lock_layer.surface_list.prev,
2810 &surface->layer_link);
2811 weston_surface_assign_output(surface);
2812 shell->compositor->idle_time = shell->screensaver.duration;
2813 weston_compositor_wake(shell->compositor);
2814 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2815 }
2816}
2817
2818static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002819screensaver_set_surface(struct wl_client *client,
2820 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002821 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002822 struct wl_resource *output_resource)
2823{
Tiago Vignattibe143262012-04-16 17:31:41 +03002824 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002825 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002826 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002827
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002828 surface->configure = screensaver_configure;
2829 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002830 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002831}
2832
2833static const struct screensaver_interface screensaver_implementation = {
2834 screensaver_set_surface
2835};
2836
2837static void
2838unbind_screensaver(struct wl_resource *resource)
2839{
Tiago Vignattibe143262012-04-16 17:31:41 +03002840 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002841
Pekka Paalanen77346a62011-11-30 16:26:35 +02002842 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002843 free(resource);
2844}
2845
2846static void
2847bind_screensaver(struct wl_client *client,
2848 void *data, uint32_t version, uint32_t id)
2849{
Tiago Vignattibe143262012-04-16 17:31:41 +03002850 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002851 struct wl_resource *resource;
2852
2853 resource = wl_client_add_object(client, &screensaver_interface,
2854 &screensaver_implementation,
2855 id, shell);
2856
Pekka Paalanen77346a62011-11-30 16:26:35 +02002857 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002858 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002859 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002860 return;
2861 }
2862
2863 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2864 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002865 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002866}
2867
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002868static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002869input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2870{
2871 struct weston_mode *mode = surface->output->current;
2872 GLfloat x = (mode->width - surface->buffer->width) / 2;
2873 GLfloat y = mode->height - surface->buffer->height;
2874
2875 /* Don't map the input panel here, wait for
2876 * show_input_panels signal. */
2877
2878 weston_surface_configure(surface,
2879 surface->output->x + x,
2880 surface->output->y + y,
2881 surface->buffer->width,
2882 surface->buffer->height);
2883}
2884
2885static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002886input_panel_set_surface(struct wl_client *client,
2887 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002888 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002889 struct wl_resource *output_resource)
2890{
2891 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002892 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002893 struct weston_output *output = output_resource->data;
2894
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002895 surface->configure = input_panel_configure;
2896 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002897 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002898 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002899}
2900
2901static const struct input_panel_interface input_panel_implementation = {
2902 input_panel_set_surface
2903};
2904
2905static void
2906unbind_input_panel(struct wl_resource *resource)
2907{
2908 struct desktop_shell *shell = resource->data;
2909
2910 shell->input_panel.binding = NULL;
2911 free(resource);
2912}
2913
2914static void
2915bind_input_panel(struct wl_client *client,
2916 void *data, uint32_t version, uint32_t id)
2917{
2918 struct desktop_shell *shell = data;
2919 struct wl_resource *resource;
2920
2921 resource = wl_client_add_object(client, &input_panel_interface,
2922 &input_panel_implementation,
2923 id, shell);
2924
2925 if (shell->input_panel.binding == NULL) {
2926 resource->destroy = unbind_input_panel;
2927 shell->input_panel.binding = resource;
2928 return;
2929 }
2930
2931 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2932 "interface object already bound");
2933 wl_resource_destroy(resource);
2934}
2935
Kristian Høgsberg07045392012-02-19 18:52:44 -05002936struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002937 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002938 struct weston_surface *current;
2939 struct wl_listener listener;
2940 struct wl_keyboard_grab grab;
2941};
2942
2943static void
2944switcher_next(struct switcher *switcher)
2945{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002946 struct weston_surface *surface;
2947 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002948 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002949 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002950
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002951 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002952 switch (get_shell_surface_type(surface)) {
2953 case SHELL_SURFACE_TOPLEVEL:
2954 case SHELL_SURFACE_FULLSCREEN:
2955 case SHELL_SURFACE_MAXIMIZED:
2956 if (first == NULL)
2957 first = surface;
2958 if (prev == switcher->current)
2959 next = surface;
2960 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002961 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002962 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002963 weston_surface_damage(surface);
2964 break;
2965 default:
2966 break;
2967 }
Alex Wu1659daa2012-04-01 20:13:09 +08002968
2969 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002970 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002971 surface->geometry.dirty = 1;
2972 weston_surface_damage(surface);
2973 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002974 }
2975
2976 if (next == NULL)
2977 next = first;
2978
Alex Wu07b26062012-03-12 16:06:01 +08002979 if (next == NULL)
2980 return;
2981
Kristian Høgsberg07045392012-02-19 18:52:44 -05002982 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002983 wl_signal_add(&next->surface.resource.destroy_signal,
2984 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002985
2986 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002987 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002988
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002989 shsurf = get_shell_surface(switcher->current);
2990 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002991 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002992}
2993
2994static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002995switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002996{
2997 struct switcher *switcher =
2998 container_of(listener, struct switcher, listener);
2999
3000 switcher_next(switcher);
3001}
3002
3003static void
Daniel Stone351eb612012-05-31 15:27:47 -04003004switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003005{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003006 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003007 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003008 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003009
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003010 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003011 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003012 weston_surface_damage(surface);
3013 }
3014
Alex Wu07b26062012-03-12 16:06:01 +08003015 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003016 activate(switcher->shell, switcher->current,
3017 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003018 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003019 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003020 free(switcher);
3021}
3022
3023static void
3024switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003025 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003026{
3027 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003028 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003029
Daniel Stonec9785ea2012-05-30 16:31:52 +01003030 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003031 switcher_next(switcher);
3032}
3033
3034static void
3035switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3036 uint32_t mods_depressed, uint32_t mods_latched,
3037 uint32_t mods_locked, uint32_t group)
3038{
3039 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003040 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003041
Daniel Stone351eb612012-05-31 15:27:47 -04003042 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3043 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003044}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003045
3046static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003047 switcher_key,
3048 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003049};
3050
3051static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003052switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3053 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003054{
Tiago Vignattibe143262012-04-16 17:31:41 +03003055 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003056 struct switcher *switcher;
3057
3058 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003059 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003060 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003061 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003062 wl_list_init(&switcher->listener.link);
3063
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003064 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003065 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003066 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3067 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003068 switcher_next(switcher);
3069}
3070
Pekka Paalanen3c647232011-12-22 13:43:43 +02003071static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003072backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3073 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003074{
3075 struct weston_compositor *compositor = data;
3076 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003077 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003078
3079 /* TODO: we're limiting to simple use cases, where we assume just
3080 * control on the primary display. We'd have to extend later if we
3081 * ever get support for setting backlights on random desktop LCD
3082 * panels though */
3083 output = get_default_output(compositor);
3084 if (!output)
3085 return;
3086
3087 if (!output->set_backlight)
3088 return;
3089
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003090 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3091 backlight_new = output->backlight_current - 25;
3092 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3093 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003094
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003095 if (backlight_new < 5)
3096 backlight_new = 5;
3097 if (backlight_new > 255)
3098 backlight_new = 255;
3099
3100 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003101 output->set_backlight(output, output->backlight_current);
3102}
3103
3104static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003105debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3106 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003107{
Tiago Vignattibe143262012-04-16 17:31:41 +03003108 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003109 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003110 struct weston_surface *surface;
3111
3112 if (shell->debug_repaint_surface) {
3113 weston_surface_destroy(shell->debug_repaint_surface);
3114 shell->debug_repaint_surface = NULL;
3115 } else {
3116 surface = weston_surface_create(compositor);
3117 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3118 weston_surface_configure(surface, 0, 0, 8192, 8192);
3119 wl_list_insert(&compositor->fade_layer.surface_list,
3120 &surface->layer_link);
3121 weston_surface_assign_output(surface);
3122 pixman_region32_init(&surface->input);
3123
3124 /* Here's the dirty little trick that makes the
3125 * repaint debugging work: we force an
3126 * update_transform first to update dependent state
3127 * and clear the geometry.dirty bit. Then we clear
3128 * the surface damage so it only gets repainted
3129 * piecewise as we repaint other things. */
3130
3131 weston_surface_update_transform(surface);
3132 pixman_region32_fini(&surface->damage);
3133 pixman_region32_init(&surface->damage);
3134 shell->debug_repaint_surface = surface;
3135 }
3136}
3137
3138static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003139force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3140 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003141{
3142 struct wl_client *client;
3143 pid_t pid;
3144 uid_t uid;
3145 gid_t gid;
3146
Daniel Stone325fc2d2012-05-30 16:31:58 +01003147 client = seat->keyboard->focus->resource.client;
3148 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003149
Daniel Stone325fc2d2012-05-30 16:31:58 +01003150 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003151}
3152
3153static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003154workspace_up_binding(struct wl_seat *seat, uint32_t time,
3155 uint32_t key, void *data)
3156{
3157 struct desktop_shell *shell = data;
3158 unsigned int new_index = shell->workspaces.current;
3159
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003160 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003161 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003162 if (new_index != 0)
3163 new_index--;
3164
3165 change_workspace(shell, new_index);
3166}
3167
3168static void
3169workspace_down_binding(struct wl_seat *seat, uint32_t time,
3170 uint32_t key, void *data)
3171{
3172 struct desktop_shell *shell = data;
3173 unsigned int new_index = shell->workspaces.current;
3174
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003175 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003176 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003177 if (new_index < shell->workspaces.num - 1)
3178 new_index++;
3179
3180 change_workspace(shell, new_index);
3181}
3182
3183static void
3184workspace_f_binding(struct wl_seat *seat, uint32_t time,
3185 uint32_t key, void *data)
3186{
3187 struct desktop_shell *shell = data;
3188 unsigned int new_index;
3189
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003190 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003191 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003192 new_index = key - KEY_F1;
3193 if (new_index >= shell->workspaces.num)
3194 new_index = shell->workspaces.num - 1;
3195
3196 change_workspace(shell, new_index);
3197}
3198
3199
3200static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003201shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003202{
Tiago Vignattibe143262012-04-16 17:31:41 +03003203 struct desktop_shell *shell =
3204 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003205 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003206
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003207 if (shell->child.client)
3208 wl_client_destroy(shell->child.client);
3209
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003210 wl_list_remove(&shell->lock_listener.link);
3211 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003212 wl_list_remove(&shell->show_input_panel_listener.link);
3213 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003214
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003215 wl_array_for_each(ws, &shell->workspaces.array)
3216 workspace_destroy(*ws);
3217 wl_array_release(&shell->workspaces.array);
3218
Pekka Paalanen3c647232011-12-22 13:43:43 +02003219 free(shell->screensaver.path);
3220 free(shell);
3221}
3222
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003223static void
3224shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3225{
3226 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003227 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003228
3229 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003230 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3231 MODIFIER_CTRL | MODIFIER_ALT,
3232 terminate_binding, ec);
3233 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3234 click_to_activate_binding,
3235 shell);
3236 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3237 MODIFIER_SUPER | MODIFIER_ALT,
3238 surface_opacity_binding, NULL);
3239 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3240 MODIFIER_SUPER, zoom_axis_binding,
3241 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003242
3243 /* configurable bindings */
3244 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003245 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3246 zoom_key_binding, NULL);
3247 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3248 zoom_key_binding, NULL);
3249 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3250 shell);
3251 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3252 resize_binding, shell);
3253 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3254 rotate_binding, NULL);
3255 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3256 shell);
3257 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3258 ec);
3259 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3260 backlight_binding, ec);
3261 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3262 ec);
3263 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3264 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003265 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003266 debug_repaint_binding, shell);
3267 weston_compositor_add_key_binding(ec, KEY_K, mod,
3268 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003269 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3270 workspace_up_binding, shell);
3271 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3272 workspace_down_binding, shell);
3273
3274 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3275 if (shell->workspaces.num > 1) {
3276 num_workspace_bindings = shell->workspaces.num;
3277 if (num_workspace_bindings > 6)
3278 num_workspace_bindings = 6;
3279 for (i = 0; i < num_workspace_bindings; i++)
3280 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3281 workspace_f_binding,
3282 shell);
3283 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003284}
3285
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003286int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003287shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003288
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003289WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003290shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003291{
Tiago Vignattibe143262012-04-16 17:31:41 +03003292 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003293 struct workspace **pws;
3294 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003295
3296 shell = malloc(sizeof *shell);
3297 if (shell == NULL)
3298 return -1;
3299
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003300 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003301 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003302
3303 shell->destroy_listener.notify = shell_destroy;
3304 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3305 shell->lock_listener.notify = lock;
3306 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3307 shell->unlock_listener.notify = unlock;
3308 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003309 shell->show_input_panel_listener.notify = show_input_panels;
3310 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3311 shell->hide_input_panel_listener.notify = hide_input_panels;
3312 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003313 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003314 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003315 ec->shell_interface.create_shell_surface = create_shell_surface;
3316 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003317 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003318 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003319 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003320
Pekka Paalanen77346a62011-11-30 16:26:35 +02003321 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003322 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003323
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003324 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3325 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003326 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3327 weston_layer_init(&shell->lock_layer, NULL);
3328
3329 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003330
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003331 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003332
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003333 for (i = 0; i < shell->workspaces.num; i++) {
3334 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3335 if (pws == NULL)
3336 return -1;
3337
3338 *pws = workspace_create();
3339 if (*pws == NULL)
3340 return -1;
3341 }
3342 activate_workspace(shell, 0);
3343
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003344 wl_list_init(&shell->workspaces.animation.link);
3345 shell->workspaces.animation.frame = animate_workspace_change_frame;
3346
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003347 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3348 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003349 return -1;
3350
Kristian Høgsberg75840622011-09-06 13:48:16 -04003351 if (wl_display_add_global(ec->wl_display,
3352 &desktop_shell_interface,
3353 shell, bind_desktop_shell) == NULL)
3354 return -1;
3355
Pekka Paalanen6e168112011-11-24 11:34:05 +02003356 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3357 shell, bind_screensaver) == NULL)
3358 return -1;
3359
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003360 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3361 shell, bind_input_panel) == NULL)
3362 return -1;
3363
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003364 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003365 if (launch_desktop_shell_process(shell) != 0)
3366 return -1;
3367
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003368 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003369 if (ec->seat->seat.pointer)
3370 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3371 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003372
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003373 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003374
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003375 return 0;
3376}