blob: fca280ef8a0956b29034d4b1c542f600890b3240 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020038#include "../shared/config-parser.h"
Martin Minarik6d118362012-06-07 18:01:59 +020039#include "log.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020042#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043
Juan Zhaoe10d2792012-04-25 19:09:52 +080044enum animation_type {
45 ANIMATION_NONE,
46
47 ANIMATION_ZOOM,
48 ANIMATION_FADE
49};
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
53 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Jonas Ådahle3cddce2012-06-13 00:01:22 +020059struct workspace {
60 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020061
62 struct wl_list focus_list;
63 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020064};
65
Tiago Vignattibe143262012-04-16 17:31:41 +030066struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040068
69 struct wl_listener lock_listener;
70 struct wl_listener unlock_listener;
71 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020072 struct wl_listener show_input_panel_listener;
73 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020074
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050075 struct weston_layer fullscreen_layer;
76 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050077 struct weston_layer background_layer;
78 struct weston_layer lock_layer;
79
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040080 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030081 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040082
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020083 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020085 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020086 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020087
88 unsigned deathcount;
89 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020090 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020091
92 bool locked;
93 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020094
Kristian Høgsberg730c94d2012-06-26 21:44:35 -040095 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050096 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010097
Pekka Paalanen77346a62011-11-30 16:26:35 +020098 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +020099 struct wl_array array;
100 unsigned int current;
101 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200102
103 struct weston_animation animation;
104 int anim_dir;
105 uint32_t anim_timestamp;
106 double anim_current;
107 struct workspace *anim_from;
108 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200109 } workspaces;
110
111 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200112 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200113 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200114 struct wl_resource *binding;
115 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200117 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500118
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200119 struct {
120 struct wl_resource *binding;
121 struct wl_list surfaces;
122 } input_panel;
123
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300124 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800125 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500126 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400127};
128
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500129enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200130 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131 SHELL_SURFACE_TOPLEVEL,
132 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500133 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800134 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200136};
137
Scott Moreauff1db4a2012-04-17 19:06:18 -0600138struct ping_timer {
139 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600140 uint32_t serial;
141};
142
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200143struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200144 struct wl_resource resource;
145
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500146 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200147 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400148 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300149 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200150
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400151 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400152 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500153 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800154 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800155 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600156 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100157
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500158 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200159 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500160 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200161 } rotation;
162
163 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700164 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500165 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200166 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500167 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100168 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400169 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500170 } popup;
171
Alex Wu4539b082012-03-01 12:57:46 +0800172 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300173 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400174 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300175 } transient;
176
177 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800178 enum wl_shell_surface_fullscreen_method type;
179 struct weston_transform transform; /* matrix from x, y */
180 uint32_t framerate;
181 struct weston_surface *black_surface;
182 } fullscreen;
183
Scott Moreauff1db4a2012-04-17 19:06:18 -0600184 struct ping_timer *ping_timer;
185
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200186 struct weston_transform workspace_transform;
187
Jonas Ådahl04769742012-06-13 00:01:24 +0200188 struct focus_state *focus_state;
189
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500190 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100192 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400193
194 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200195};
196
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300197struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700198 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199 struct shell_surface *shsurf;
200 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300201 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300202};
203
204struct weston_move_grab {
205 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100206 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500207};
208
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500211 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400213 GLfloat x;
214 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215 } center;
216};
217
Alex Wubd3354b2012-04-17 17:20:49 +0800218static struct shell_surface *
219get_shell_surface(struct weston_surface *surface);
220
221static struct desktop_shell *
222shell_surface_get_shell(struct shell_surface *shsurf);
223
224static bool
225shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
226{
227 struct desktop_shell *shell;
228 struct weston_surface *top_fs_es;
229
230 shell = shell_surface_get_shell(shsurf);
231
232 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
233 return false;
234
235 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
236 struct weston_surface,
237 layer_link);
238 return (shsurf == get_shell_surface(top_fs_es));
239}
240
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500241static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400242destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300243{
244 struct shell_grab *grab;
245
246 grab = container_of(listener, struct shell_grab,
247 shsurf_destroy_listener);
248
249 grab->shsurf = NULL;
250}
251
252static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300253shell_grab_start(struct shell_grab *grab,
254 const struct wl_pointer_grab_interface *interface,
255 struct shell_surface *shsurf,
256 struct wl_pointer *pointer,
257 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300258{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300259 struct desktop_shell *shell = shsurf->shell;
260
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300261 grab->grab.interface = interface;
262 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400263 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
264 wl_signal_add(&shsurf->resource.destroy_signal,
265 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300266
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300267 grab->pointer = pointer;
268 grab->grab.focus = &shsurf->surface->surface;
269
270 wl_pointer_start_grab(pointer, &grab->grab);
271 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
272 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
273 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274}
275
276static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300277shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300278{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400279 if (grab->shsurf)
280 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300281
282 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283}
284
285static void
Alex Wu4539b082012-03-01 12:57:46 +0800286center_on_output(struct weston_surface *surface,
287 struct weston_output *output);
288
Daniel Stone496ca172012-05-30 16:31:42 +0100289static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300290get_modifier(char *modifier)
291{
292 if (!modifier)
293 return MODIFIER_SUPER;
294
295 if (!strcmp("ctrl", modifier))
296 return MODIFIER_CTRL;
297 else if (!strcmp("alt", modifier))
298 return MODIFIER_ALT;
299 else if (!strcmp("super", modifier))
300 return MODIFIER_SUPER;
301 else
302 return MODIFIER_SUPER;
303}
304
Juan Zhaoe10d2792012-04-25 19:09:52 +0800305static enum animation_type
306get_animation_type(char *animation)
307{
308 if (!animation)
309 return ANIMATION_NONE;
310
311 if (!strcmp("zoom", animation))
312 return ANIMATION_ZOOM;
313 else if (!strcmp("fade", animation))
314 return ANIMATION_FADE;
315 else
316 return ANIMATION_NONE;
317}
318
Alex Wu4539b082012-03-01 12:57:46 +0800319static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300320shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200321{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200322 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200323 char *path = NULL;
324 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200325 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300326 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800327 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200328
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400329 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300330 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800331 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200332 { "num-workspaces",
333 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200334 };
335
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400336 struct config_key saver_keys[] = {
337 { "path", CONFIG_KEY_STRING, &path },
338 { "duration", CONFIG_KEY_INTEGER, &duration },
339 };
340
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200341 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400342 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200343 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
344 };
345
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200346 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500347 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200348 free(config_file);
349
Pekka Paalanen7296e792011-12-07 16:22:00 +0200350 shell->screensaver.path = path;
351 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300352 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800353 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200354 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
355}
356
357static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200358focus_state_destroy(struct focus_state *state)
359{
360 wl_list_remove(&state->seat_destroy_listener.link);
361 wl_list_remove(&state->surface_destroy_listener.link);
362 free(state);
363}
364
365static void
366focus_state_seat_destroy(struct wl_listener *listener, void *data)
367{
368 struct focus_state *state = container_of(listener,
369 struct focus_state,
370 seat_destroy_listener);
371
372 wl_list_remove(&state->link);
373 focus_state_destroy(state);
374}
375
376static void
377focus_state_surface_destroy(struct wl_listener *listener, void *data)
378{
379 struct focus_state *state = container_of(listener,
380 struct focus_state,
381 seat_destroy_listener);
382
383 wl_list_remove(&state->link);
384 focus_state_destroy(state);
385}
386
387static struct focus_state *
388focus_state_create(struct weston_seat *seat)
389{
390 struct wl_keyboard *keyboard = seat->seat.keyboard;
391 struct focus_state *state;
392 struct wl_surface *surface;
393 struct shell_surface *shsurf;
394
395 state = malloc(sizeof *state);
396 if (state == NULL)
397 return NULL;
398
399 surface = keyboard->focus;
400 shsurf = get_shell_surface((struct weston_surface *)keyboard->focus);
401 shsurf->focus_state = state;
402
403 state->seat = seat;
404 state->keyboard_focus = shsurf->surface;
405 wl_list_init(&state->link);
406
407 state->seat_destroy_listener.notify = focus_state_seat_destroy;
408 state->surface_destroy_listener.notify = focus_state_surface_destroy;
409 wl_signal_add(&seat->seat.destroy_signal,
410 &state->seat_destroy_listener);
411 wl_signal_add(&surface->resource.destroy_signal,
412 &state->surface_destroy_listener);
413
414 return state;
415}
416
417static void
418pop_focus_state(struct desktop_shell *shell, struct workspace *ws)
419{
420 struct focus_state *state, *next;
421
422 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
423 if (state->keyboard_focus)
424 wl_keyboard_set_focus(state->seat->seat.keyboard,
425 &state->keyboard_focus->surface);
426
427 focus_state_destroy(state);
428 }
429 wl_list_init(&ws->focus_list);
430}
431
432static void
433push_focus_state(struct desktop_shell *shell, struct workspace *ws)
434{
435 struct weston_seat *seat;
436 struct focus_state *state;
437 struct wl_keyboard *keyboard;
438
439 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
440 keyboard = seat->seat.keyboard;
441 if (keyboard && keyboard->focus) {
442 state = focus_state_create(seat);
443 if (state == NULL)
444 return;
445
446 wl_list_insert(&ws->focus_list, &state->link);
447
448 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
449 }
450 }
451}
452
453static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200454workspace_destroy(struct workspace *ws)
455{
Jonas Ådahl04769742012-06-13 00:01:24 +0200456 struct focus_state *state, *next;
457
458 wl_list_for_each_safe(state, next, &ws->focus_list, link)
459 focus_state_destroy(state);
460
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200461 free(ws);
462}
463
Jonas Ådahl04769742012-06-13 00:01:24 +0200464static void
465seat_destroyed(struct wl_listener *listener, void *data)
466{
467 struct weston_seat *seat = data;
468 struct focus_state *state, *next;
469 struct workspace *ws = container_of(listener,
470 struct workspace,
471 seat_destroyed_listener);
472
473 wl_list_for_each_safe(state, next, &ws->focus_list, link)
474 if (state->seat == seat)
475 wl_list_remove(&state->link);
476}
477
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200478static struct workspace *
479workspace_create(void)
480{
481 struct workspace *ws = malloc(sizeof *ws);
482 if (ws == NULL)
483 return NULL;
484
485 weston_layer_init(&ws->layer, NULL);
486
Jonas Ådahl04769742012-06-13 00:01:24 +0200487 wl_list_init(&ws->focus_list);
488 wl_list_init(&ws->seat_destroyed_listener.link);
489 ws->seat_destroyed_listener.notify = seat_destroyed;
490
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200491 return ws;
492}
493
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200494static int
495workspace_is_empty(struct workspace *ws)
496{
497 return wl_list_empty(&ws->layer.surface_list);
498}
499
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200500static struct workspace *
501get_workspace(struct desktop_shell *shell, unsigned int index)
502{
503 struct workspace **pws = shell->workspaces.array.data;
504 pws += index;
505 return *pws;
506}
507
508static struct workspace *
509get_current_workspace(struct desktop_shell *shell)
510{
511 return get_workspace(shell, shell->workspaces.current);
512}
513
514static void
515activate_workspace(struct desktop_shell *shell, unsigned int index)
516{
517 struct workspace *ws;
518
519 ws = get_workspace(shell, index);
520 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
521
522 shell->workspaces.current = index;
523}
524
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200525static unsigned int
526get_output_height(struct weston_output *output)
527{
528 return abs(output->region.extents.y1 - output->region.extents.y2);
529}
530
531static void
532surface_translate(struct weston_surface *surface, double d)
533{
534 struct shell_surface *shsurf = get_shell_surface(surface);
535 struct weston_transform *transform;
536
537 transform = &shsurf->workspace_transform;
538 if (wl_list_empty(&transform->link))
539 wl_list_insert(surface->geometry.transformation_list.prev,
540 &shsurf->workspace_transform.link);
541
542 weston_matrix_init(&shsurf->workspace_transform.matrix);
543 weston_matrix_translate(&shsurf->workspace_transform.matrix,
544 0.0, d, 0.0);
545 surface->geometry.dirty = 1;
546}
547
548static void
549workspace_translate_out(struct workspace *ws, double fraction)
550{
551 struct weston_surface *surface;
552 unsigned int height;
553 double d;
554
555 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
556 height = get_output_height(surface->output);
557 d = height * fraction;
558
559 surface_translate(surface, d);
560 }
561}
562
563static void
564workspace_translate_in(struct workspace *ws, double fraction)
565{
566 struct weston_surface *surface;
567 unsigned int height;
568 double d;
569
570 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
571 height = get_output_height(surface->output);
572
573 if (fraction > 0)
574 d = -(height - height * fraction);
575 else
576 d = height + height * fraction;
577
578 surface_translate(surface, d);
579 }
580}
581
582static void
583workspace_damage_all_surfaces(struct workspace *ws)
584{
585 struct weston_surface *surface;
586
587 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
588 weston_surface_damage(surface);
589}
590
591static void
592reverse_workspace_change_animation(struct desktop_shell *shell,
593 unsigned int index,
594 struct workspace *from,
595 struct workspace *to)
596{
597 shell->workspaces.current = index;
598
599 shell->workspaces.anim_to = to;
600 shell->workspaces.anim_from = from;
601 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
602 shell->workspaces.anim_timestamp = 0;
603
Jonas Ådahl04769742012-06-13 00:01:24 +0200604 push_focus_state(shell, from);
605 pop_focus_state(shell, to);
606
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200607 workspace_damage_all_surfaces(from);
608 workspace_damage_all_surfaces(to);
609}
610
611static void
612workspace_deactivate_transforms(struct workspace *ws)
613{
614 struct weston_surface *surface;
615 struct shell_surface *shsurf;
616
617 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
618 shsurf = get_shell_surface(surface);
619 wl_list_remove(&shsurf->workspace_transform.link);
620 wl_list_init(&shsurf->workspace_transform.link);
621 shsurf->surface->geometry.dirty = 1;
622 }
623}
624
625static void
626finish_workspace_change_animation(struct desktop_shell *shell,
627 struct workspace *from,
628 struct workspace *to)
629{
630 workspace_damage_all_surfaces(from);
631 workspace_damage_all_surfaces(to);
632
633 wl_list_remove(&shell->workspaces.animation.link);
634 workspace_deactivate_transforms(from);
635 workspace_deactivate_transforms(to);
636 shell->workspaces.anim_to = NULL;
637
638 wl_list_remove(&shell->workspaces.anim_from->layer.link);
639}
640
641static void
642animate_workspace_change_frame(struct weston_animation *animation,
643 struct weston_output *output, uint32_t msecs)
644{
645 struct desktop_shell *shell =
646 container_of(animation, struct desktop_shell,
647 workspaces.animation);
648 struct workspace *from = shell->workspaces.anim_from;
649 struct workspace *to = shell->workspaces.anim_to;
650 uint32_t t;
651 double x, y;
652
653 if (workspace_is_empty(from) && workspace_is_empty(to)) {
654 finish_workspace_change_animation(shell, from, to);
655 return;
656 }
657
658 if (shell->workspaces.anim_timestamp == 0) {
659 if (shell->workspaces.anim_current == 0.0)
660 shell->workspaces.anim_timestamp = msecs;
661 else
662 shell->workspaces.anim_timestamp =
663 msecs -
664 /* Invers of movement function 'y' below. */
665 (asin(1.0 - shell->workspaces.anim_current) *
666 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
667 M_2_PI);
668 }
669
670 t = msecs - shell->workspaces.anim_timestamp;
671
672 /*
673 * x = [0, π/2]
674 * y(x) = sin(x)
675 */
676 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
677 y = sin(x);
678
679 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
680 workspace_damage_all_surfaces(from);
681 workspace_damage_all_surfaces(to);
682
683 workspace_translate_out(from, shell->workspaces.anim_dir * y);
684 workspace_translate_in(to, shell->workspaces.anim_dir * y);
685 shell->workspaces.anim_current = y;
686
687 workspace_damage_all_surfaces(from);
688 workspace_damage_all_surfaces(to);
689 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200690 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200691 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200692}
693
694static void
695animate_workspace_change(struct desktop_shell *shell,
696 unsigned int index,
697 struct workspace *from,
698 struct workspace *to)
699{
700 struct weston_output *output;
701
702 int dir;
703
704 if (index > shell->workspaces.current)
705 dir = -1;
706 else
707 dir = 1;
708
709 shell->workspaces.current = index;
710
711 shell->workspaces.anim_dir = dir;
712 shell->workspaces.anim_from = from;
713 shell->workspaces.anim_to = to;
714 shell->workspaces.anim_current = 0.0;
715 shell->workspaces.anim_timestamp = 0;
716
717 output = container_of(shell->compositor->output_list.next,
718 struct weston_output, link);
719 wl_list_insert(&output->animation_list,
720 &shell->workspaces.animation.link);
721
722 wl_list_insert(&from->layer.link, &to->layer.link);
723
724 workspace_translate_in(to, 0);
725
Jonas Ådahl04769742012-06-13 00:01:24 +0200726 push_focus_state(shell, from);
727 pop_focus_state(shell, to);
728
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200729 workspace_damage_all_surfaces(from);
730 workspace_damage_all_surfaces(to);
731}
732
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200733static void
734change_workspace(struct desktop_shell *shell, unsigned int index)
735{
736 struct workspace *from;
737 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200738
739 if (index == shell->workspaces.current)
740 return;
741
742 /* Don't change workspace when there is any fullscreen surfaces. */
743 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
744 return;
745
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200746 from = get_current_workspace(shell);
747 to = get_workspace(shell, index);
748
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200749 if (shell->workspaces.anim_from == to &&
750 shell->workspaces.anim_to == from) {
751 reverse_workspace_change_animation(shell, index, from, to);
752 return;
753 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200754
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200755 if (shell->workspaces.anim_to != NULL)
756 finish_workspace_change_animation(shell,
757 shell->workspaces.anim_from,
758 shell->workspaces.anim_to);
759
760 if (workspace_is_empty(to) && workspace_is_empty(from)) {
761 shell->workspaces.current = index;
762 wl_list_insert(&from->layer.link, &to->layer.link);
763 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200764
765 push_focus_state(shell, from);
766 pop_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200767 }
768 else
769 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200770}
771
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200772static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400773noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100774 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500775{
776 grab->focus = NULL;
777}
778
779static void
Scott Moreau447013d2012-02-18 05:05:29 -0700780move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100781 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500782{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500783 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100784 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300785 struct shell_surface *shsurf = move->base.shsurf;
786 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100787 int dx = wl_fixed_to_int(pointer->x + move->dx);
788 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300789
790 if (!shsurf)
791 return;
792
793 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500794
Daniel Stone103db7f2012-05-08 17:17:55 +0100795 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200796 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400797
798 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500799}
800
801static void
Scott Moreau447013d2012-02-18 05:05:29 -0700802move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100803 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500804{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300805 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
806 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100807 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100808 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500809
Daniel Stone4dbadb12012-05-30 16:31:51 +0100810 if (pointer->button_count == 0 &&
811 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300812 shell_grab_end(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100813 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500814 free(grab);
815 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500816}
817
Scott Moreau447013d2012-02-18 05:05:29 -0700818static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500819 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500820 move_grab_motion,
821 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500822};
823
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600824static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400825busy_cursor_grab_focus(struct wl_pointer_grab *base,
826 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600827{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400828 struct shell_grab *grab = (struct shell_grab *) base;
829 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600830
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400831 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300832 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400833 wl_pointer_end_grab(pointer);
834 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600835 }
836}
837
838static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400839busy_cursor_grab_motion(struct wl_pointer_grab *grab,
840 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600841{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400842}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600843
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400844static void
845busy_cursor_grab_button(struct wl_pointer_grab *grab,
846 uint32_t time, uint32_t button, uint32_t state)
847{
848}
849
850static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
851 busy_cursor_grab_focus,
852 busy_cursor_grab_motion,
853 busy_cursor_grab_button,
854};
855
856static void
857set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
858{
859 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400860
861 grab = malloc(sizeof *grab);
862 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600863 return;
864
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300865 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
866 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400867}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600868
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400869static void
870end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
871{
872 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
873
874 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300875 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400876 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600877 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600878}
879
Scott Moreau9521d5e2012-04-19 13:06:17 -0600880static void
881ping_timer_destroy(struct shell_surface *shsurf)
882{
883 if (!shsurf || !shsurf->ping_timer)
884 return;
885
886 if (shsurf->ping_timer->source)
887 wl_event_source_remove(shsurf->ping_timer->source);
888
889 free(shsurf->ping_timer);
890 shsurf->ping_timer = NULL;
891}
892
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400893static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600894ping_timeout_handler(void *data)
895{
896 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400897 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600898
Scott Moreau9521d5e2012-04-19 13:06:17 -0600899 /* Client is not responding */
900 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400901
902 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
903 if (seat->seat.pointer->focus == &shsurf->surface->surface)
904 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600905
906 return 1;
907}
908
909static void
910ping_handler(struct weston_surface *surface, uint32_t serial)
911{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400912 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600913 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400914 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600915
916 if (!shsurf)
917 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400918 if (!shsurf->resource.client)
919 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600920
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +0300921 if (shsurf->surface == shsurf->shell->grab_surface)
922 return;
923
Scott Moreauff1db4a2012-04-17 19:06:18 -0600924 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300925 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600926 if (!shsurf->ping_timer)
927 return;
928
929 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600930 loop = wl_display_get_event_loop(surface->compositor->wl_display);
931 shsurf->ping_timer->source =
932 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
933 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
934
935 wl_shell_surface_send_ping(&shsurf->resource, serial);
936 }
937}
938
939static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400940handle_pointer_focus(struct wl_listener *listener, void *data)
941{
942 struct wl_pointer *pointer = data;
943 struct weston_surface *surface =
944 (struct weston_surface *) pointer->focus;
945 struct weston_compositor *compositor;
946 struct shell_surface *shsurf;
947 uint32_t serial;
948
949 if (!surface)
950 return;
951
952 compositor = surface->compositor;
953 shsurf = get_shell_surface(surface);
954
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300955 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400956 set_busy_cursor(shsurf, pointer);
957 } else {
958 serial = wl_display_next_serial(compositor->wl_display);
959 ping_handler(surface, serial);
960 }
961}
962
963static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600964shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
965 uint32_t serial)
966{
967 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400968 struct desktop_shell *shell = shsurf->shell;
969 struct weston_seat *seat;
970 struct weston_compositor *ec = shsurf->surface->compositor;
971 struct wl_pointer *pointer;
972 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600973
Scott Moreauff1db4a2012-04-17 19:06:18 -0600974 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400975 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600976 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400977 if (was_unresponsive) {
978 /* Received pong from previously unresponsive client */
979 wl_list_for_each(seat, &ec->seat_list, link) {
980 pointer = seat->seat.pointer;
981 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300982 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400983 pointer->current ==
984 &shsurf->surface->surface)
985 end_busy_cursor(shsurf, pointer);
986 }
987 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600988 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600989 }
990}
991
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400992static void
993shell_surface_set_title(struct wl_client *client,
994 struct wl_resource *resource, const char *title)
995{
996 struct shell_surface *shsurf = resource->data;
997
998 free(shsurf->title);
999 shsurf->title = strdup(title);
1000}
1001
1002static void
1003shell_surface_set_class(struct wl_client *client,
1004 struct wl_resource *resource, const char *class)
1005{
1006 struct shell_surface *shsurf = resource->data;
1007
1008 free(shsurf->class);
1009 shsurf->class = strdup(class);
1010}
1011
Scott Moreauff1db4a2012-04-17 19:06:18 -06001012static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001013surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001014{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001015 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001016
1017 if (!shsurf)
1018 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001019
Ander Conselvan de Oliveira00d17bb2012-06-28 18:08:06 +03001020 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1021 return 0;
1022
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001024 if (!move)
1025 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001026
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001027 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001028 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001029 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001030 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001031
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001032 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1033 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001034
1035 return 0;
1036}
1037
1038static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001039shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001040 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001041{
Daniel Stone37816df2012-05-16 18:45:18 +01001042 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001043 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001044
Daniel Stone37816df2012-05-16 18:45:18 +01001045 if (ws->seat.pointer->button_count == 0 ||
1046 ws->seat.pointer->grab_serial != serial ||
1047 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001048 return;
1049
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001050 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001051 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001052}
1053
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001054struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001055 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001057 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001058};
1059
1060static void
Scott Moreau447013d2012-02-18 05:05:29 -07001061resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001062 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001063{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001064 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001065 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001066 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001067 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001068 wl_fixed_t from_x, from_y;
1069 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001070
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001071 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001072 return;
1073
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->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001076 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001077 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001078 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001079
Daniel Stone103db7f2012-05-08 17:17:55 +01001080 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001081 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001082 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001083 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001084 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001085 }
1086
Daniel Stone103db7f2012-05-08 17:17:55 +01001087 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001088 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001089 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001090 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001091 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001092 }
1093
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001094 shsurf->client->send_configure(shsurf->surface,
1095 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001096}
1097
1098static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001099send_configure(struct weston_surface *surface,
1100 uint32_t edges, int32_t width, int32_t height)
1101{
1102 struct shell_surface *shsurf = get_shell_surface(surface);
1103
1104 wl_shell_surface_send_configure(&shsurf->resource,
1105 edges, width, height);
1106}
1107
1108static const struct weston_shell_client shell_client = {
1109 send_configure
1110};
1111
1112static void
Scott Moreau447013d2012-02-18 05:05:29 -07001113resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001114 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001115{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001116 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001117 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001118 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001119
Daniel Stone4dbadb12012-05-30 16:31:51 +01001120 if (pointer->button_count == 0 &&
1121 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001122 shell_grab_end(&resize->base);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001123 free(grab);
1124 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001125}
1126
Scott Moreau447013d2012-02-18 05:05:29 -07001127static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001128 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001129 resize_grab_motion,
1130 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001131};
1132
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001133static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001134surface_resize(struct shell_surface *shsurf,
1135 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001136{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001137 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001138
Alex Wu4539b082012-03-01 12:57:46 +08001139 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1140 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001141
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001142 if (edges == 0 || edges > 15 ||
1143 (edges & 3) == 3 || (edges & 12) == 12)
1144 return 0;
1145
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001146 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001147 if (!resize)
1148 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001149
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001150 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001151 resize->width = shsurf->surface->geometry.width;
1152 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001153
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001154 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1155 ws->seat.pointer, edges);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001156
1157 return 0;
1158}
1159
1160static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001162 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001164{
Daniel Stone37816df2012-05-16 18:45:18 +01001165 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001166 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001167
Alex Wu4539b082012-03-01 12:57:46 +08001168 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1169 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001170
Daniel Stone37816df2012-05-16 18:45:18 +01001171 if (ws->seat.pointer->button_count == 0 ||
1172 ws->seat.pointer->grab_serial != serial ||
1173 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001174 return;
1175
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001176 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001177 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001178}
1179
Juan Zhao96879df2012-02-07 08:45:41 +08001180static struct weston_output *
1181get_default_output(struct weston_compositor *compositor)
1182{
1183 return container_of(compositor->output_list.next,
1184 struct weston_output, link);
1185}
1186
Alex Wu4539b082012-03-01 12:57:46 +08001187static void
1188shell_unset_fullscreen(struct shell_surface *shsurf)
1189{
1190 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001191 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1192 shell_surface_is_top_fullscreen(shsurf)) {
1193 weston_output_switch_mode(shsurf->fullscreen_output,
1194 shsurf->fullscreen_output->origin);
1195 }
Alex Wu4539b082012-03-01 12:57:46 +08001196 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1197 shsurf->fullscreen.framerate = 0;
1198 wl_list_remove(&shsurf->fullscreen.transform.link);
1199 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001200 if (shsurf->fullscreen.black_surface)
1201 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001202 shsurf->fullscreen.black_surface = NULL;
1203 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001204 weston_surface_set_position(shsurf->surface,
1205 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001206 if (shsurf->saved_rotation_valid) {
1207 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1208 &shsurf->rotation.transform.link);
1209 shsurf->saved_rotation_valid = false;
1210 }
Alex Wu4539b082012-03-01 12:57:46 +08001211}
1212
Pekka Paalanen98262232011-12-01 10:42:22 +02001213static int
1214reset_shell_surface_type(struct shell_surface *surface)
1215{
1216 switch (surface->type) {
1217 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001218 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001219 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001220 case SHELL_SURFACE_MAXIMIZED:
1221 surface->output = get_default_output(surface->surface->compositor);
1222 weston_surface_set_position(surface->surface,
1223 surface->saved_x,
1224 surface->saved_y);
1225 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001226 case SHELL_SURFACE_NONE:
1227 case SHELL_SURFACE_TOPLEVEL:
1228 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001229 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001230 break;
1231 }
1232
1233 surface->type = SHELL_SURFACE_NONE;
1234 return 0;
1235}
1236
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001237static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001238set_surface_type(struct shell_surface *shsurf)
1239{
1240 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001241 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001242
1243 reset_shell_surface_type(shsurf);
1244
1245 shsurf->type = shsurf->next_type;
1246 shsurf->next_type = SHELL_SURFACE_NONE;
1247
1248 switch (shsurf->type) {
1249 case SHELL_SURFACE_TOPLEVEL:
1250 break;
1251 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001252 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001253 pes->geometry.x + shsurf->transient.x,
1254 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001255 break;
1256
1257 case SHELL_SURFACE_MAXIMIZED:
1258 shsurf->saved_x = surface->geometry.x;
1259 shsurf->saved_y = surface->geometry.y;
1260 shsurf->saved_position_valid = true;
1261 break;
1262
1263 case SHELL_SURFACE_FULLSCREEN:
1264 shsurf->saved_x = surface->geometry.x;
1265 shsurf->saved_y = surface->geometry.y;
1266 shsurf->saved_position_valid = true;
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04001267 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001268
1269 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1270 wl_list_remove(&shsurf->rotation.transform.link);
1271 wl_list_init(&shsurf->rotation.transform.link);
1272 shsurf->surface->geometry.dirty = 1;
1273 shsurf->saved_rotation_valid = true;
1274 }
1275 break;
1276
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001277 default:
1278 break;
1279 }
1280}
1281
1282static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001283set_toplevel(struct shell_surface *shsurf)
1284{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001285 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001286}
1287
1288static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001289shell_surface_set_toplevel(struct wl_client *client,
1290 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001291{
Pekka Paalanen98262232011-12-01 10:42:22 +02001292 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001293
Tiago Vignattibc052c92012-04-19 16:18:18 +03001294 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001295}
1296
1297static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001298set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001299 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001300{
1301 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001302 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001303 shsurf->transient.x = x;
1304 shsurf->transient.y = y;
1305 shsurf->transient.flags = flags;
1306 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1307}
1308
1309static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001310shell_surface_set_transient(struct wl_client *client,
1311 struct wl_resource *resource,
1312 struct wl_resource *parent_resource,
1313 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001314{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001315 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001316 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001317
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001318 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001319}
1320
Tiago Vignattibe143262012-04-16 17:31:41 +03001321static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001322shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001323{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001324 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001325}
1326
1327static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001328get_output_panel_height(struct desktop_shell *shell,
1329 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001330{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001331 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001332 int panel_height = 0;
1333
1334 if (!output)
1335 return 0;
1336
Juan Zhao4ab94682012-07-09 22:24:09 -07001337 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001338 if (surface->output == output) {
1339 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001340 break;
1341 }
1342 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001343
Juan Zhao96879df2012-02-07 08:45:41 +08001344 return panel_height;
1345}
1346
1347static void
1348shell_surface_set_maximized(struct wl_client *client,
1349 struct wl_resource *resource,
1350 struct wl_resource *output_resource )
1351{
1352 struct shell_surface *shsurf = resource->data;
1353 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001354 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001355 uint32_t edges = 0, panel_height = 0;
1356
1357 /* get the default output, if the client set it as NULL
1358 check whether the ouput is available */
1359 if (output_resource)
1360 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001361 else if (es->output)
1362 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001363 else
1364 shsurf->output = get_default_output(es->compositor);
1365
Tiago Vignattibe143262012-04-16 17:31:41 +03001366 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001367 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001368 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001369
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001370 shsurf->client->send_configure(shsurf->surface, edges,
Rob Bradford31b68622012-07-02 19:00:19 +01001371 shsurf->output->current->width,
1372 shsurf->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001373
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001374 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001375}
1376
Alex Wu21858432012-04-01 20:13:08 +08001377static void
1378black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1379
Alex Wu4539b082012-03-01 12:57:46 +08001380static struct weston_surface *
1381create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001382 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001383 GLfloat x, GLfloat y, int w, int h)
1384{
1385 struct weston_surface *surface = NULL;
1386
1387 surface = weston_surface_create(ec);
1388 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001389 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001390 return NULL;
1391 }
1392
Alex Wu21858432012-04-01 20:13:08 +08001393 surface->configure = black_surface_configure;
1394 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001395 weston_surface_configure(surface, x, y, w, h);
1396 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1397 return surface;
1398}
1399
1400/* Create black surface and append it to the associated fullscreen surface.
1401 * Handle size dismatch and positioning according to the method. */
1402static void
1403shell_configure_fullscreen(struct shell_surface *shsurf)
1404{
1405 struct weston_output *output = shsurf->fullscreen_output;
1406 struct weston_surface *surface = shsurf->surface;
1407 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001408 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001409
Alex Wu4539b082012-03-01 12:57:46 +08001410 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001411 shsurf->fullscreen.black_surface =
1412 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001413 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001414 output->x, output->y,
1415 output->current->width,
1416 output->current->height);
1417
1418 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1419 wl_list_insert(&surface->layer_link,
1420 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001421 shsurf->fullscreen.black_surface->output = output;
1422
1423 switch (shsurf->fullscreen.type) {
1424 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1425 break;
1426 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1427 matrix = &shsurf->fullscreen.transform.matrix;
1428 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001429
1430 output_aspect = (float) output->current->width /
1431 (float) output->current->height;
1432 surface_aspect = (float) surface->geometry.width /
1433 (float) surface->geometry.height;
1434 if (output_aspect < surface_aspect)
1435 scale = (float) output->current->width /
1436 (float) surface->geometry.width;
1437 else
1438 scale = (float) output->current->height /
1439 (float) surface->geometry.height;
1440
Alex Wu4539b082012-03-01 12:57:46 +08001441 weston_matrix_scale(matrix, scale, scale, 1);
1442 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001443 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001444 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001445 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1446 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1447 weston_surface_set_position(surface, x, y);
1448
Alex Wu4539b082012-03-01 12:57:46 +08001449 break;
1450 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001451 if (shell_surface_is_top_fullscreen(shsurf)) {
1452 struct weston_mode mode = {0,
1453 surface->geometry.width,
1454 surface->geometry.height,
1455 shsurf->fullscreen.framerate};
1456
1457 if (weston_output_switch_mode(output, &mode) == 0) {
1458 weston_surface_configure(shsurf->fullscreen.black_surface,
1459 output->x, output->y,
1460 output->current->width,
1461 output->current->height);
1462 weston_surface_set_position(surface, output->x, output->y);
1463 break;
1464 }
1465 }
Alex Wu4539b082012-03-01 12:57:46 +08001466 break;
1467 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1468 break;
1469 default:
1470 break;
1471 }
1472}
1473
1474/* make the fullscreen and black surface at the top */
1475static void
1476shell_stack_fullscreen(struct shell_surface *shsurf)
1477{
Alex Wubd3354b2012-04-17 17:20:49 +08001478 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001479 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001480 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001481
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001482 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001483 wl_list_insert(&shell->fullscreen_layer.surface_list,
1484 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001485 weston_surface_damage(surface);
1486
1487 if (!shsurf->fullscreen.black_surface)
1488 shsurf->fullscreen.black_surface =
1489 create_black_surface(surface->compositor,
1490 surface,
1491 output->x, output->y,
1492 output->current->width,
1493 output->current->height);
1494
1495 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001496 wl_list_insert(&surface->layer_link,
1497 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001498 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001499}
1500
1501static void
1502shell_map_fullscreen(struct shell_surface *shsurf)
1503{
Alex Wu4539b082012-03-01 12:57:46 +08001504 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001505 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001506}
1507
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001508static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001509shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001510 struct wl_resource *resource,
1511 uint32_t method,
1512 uint32_t framerate,
1513 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001514{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001515 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001516 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001517
1518 if (output_resource)
1519 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001520 else if (es->output)
1521 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001522 else
1523 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001524
Alex Wu4539b082012-03-01 12:57:46 +08001525 shsurf->fullscreen_output = shsurf->output;
1526 shsurf->fullscreen.type = method;
1527 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001528 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001529
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001530 shsurf->client->send_configure(shsurf->surface, 0,
1531 shsurf->output->current->width,
1532 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001533}
1534
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001535static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001536popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001537 struct wl_surface *surface,
1538 wl_fixed_t x,
1539 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001540{
Daniel Stone37816df2012-05-16 18:45:18 +01001541 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001542 struct shell_surface *priv =
1543 container_of(grab, struct shell_surface, popup.grab);
1544 struct wl_client *client = priv->surface->surface.resource.client;
1545
Pekka Paalanencb108432012-01-19 16:25:40 +02001546 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001547 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001548 grab->focus = surface;
1549 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001550 wl_pointer_set_focus(pointer, NULL,
1551 wl_fixed_from_int(0),
1552 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001553 grab->focus = NULL;
1554 }
1555}
1556
1557static void
Scott Moreau447013d2012-02-18 05:05:29 -07001558popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001559 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001560{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001561 struct wl_resource *resource;
1562
Daniel Stone37816df2012-05-16 18:45:18 +01001563 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001565 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001566}
1567
1568static void
Scott Moreau447013d2012-02-18 05:05:29 -07001569popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001570 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001571{
1572 struct wl_resource *resource;
1573 struct shell_surface *shsurf =
1574 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001575 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001576 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001577 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001578
Daniel Stone37816df2012-05-16 18:45:18 +01001579 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001580 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001581 display = wl_client_get_display(resource->client);
1582 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001583 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001584 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001585 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001586 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001587 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001588 wl_pointer_end_grab(grab->pointer);
1589 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590 }
1591
Daniel Stone4dbadb12012-05-30 16:31:51 +01001592 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001593 shsurf->popup.initial_up = 1;
1594}
1595
Scott Moreau447013d2012-02-18 05:05:29 -07001596static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001597 popup_grab_focus,
1598 popup_grab_motion,
1599 popup_grab_button,
1600};
1601
1602static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001603shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604{
Daniel Stone37816df2012-05-16 18:45:18 +01001605 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001607 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001608
1609 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001610 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001611
Pekka Paalanen938269a2012-02-07 14:19:01 +02001612 weston_surface_update_transform(parent);
1613 if (parent->transform.enabled) {
1614 shsurf->popup.parent_transform.matrix =
1615 parent->transform.matrix;
1616 } else {
1617 /* construct x, y translation matrix */
1618 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1619 shsurf->popup.parent_transform.matrix.d[12] =
1620 parent->geometry.x;
1621 shsurf->popup.parent_transform.matrix.d[13] =
1622 parent->geometry.y;
1623 }
1624 wl_list_insert(es->geometry.transformation_list.prev,
1625 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001626 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001627
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001628 shsurf->popup.initial_up = 0;
1629
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001630 /* We don't require the grab to still be active, but if another
1631 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001632 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1633 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001634 } else {
1635 wl_shell_surface_send_popup_done(&shsurf->resource);
1636 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001637}
1638
1639static void
1640shell_surface_set_popup(struct wl_client *client,
1641 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001642 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001643 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001644 struct wl_resource *parent_resource,
1645 int32_t x, int32_t y, uint32_t flags)
1646{
1647 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001648
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001649 shsurf->type = SHELL_SURFACE_POPUP;
1650 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001651 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001652 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001653 shsurf->popup.x = x;
1654 shsurf->popup.y = y;
1655}
1656
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001657static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001658 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001659 shell_surface_move,
1660 shell_surface_resize,
1661 shell_surface_set_toplevel,
1662 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001663 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001664 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001665 shell_surface_set_maximized,
1666 shell_surface_set_title,
1667 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001668};
1669
1670static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001671destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001672{
Daniel Stone37816df2012-05-16 18:45:18 +01001673 if (shsurf->popup.grab.pointer)
1674 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001675
Alex Wubd3354b2012-04-17 17:20:49 +08001676 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1677 shell_surface_is_top_fullscreen(shsurf)) {
1678 weston_output_switch_mode(shsurf->fullscreen_output,
1679 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001680 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001681
Alex Wuaa08e2d2012-03-05 11:01:40 +08001682 if (shsurf->fullscreen.black_surface)
1683 weston_surface_destroy(shsurf->fullscreen.black_surface);
1684
Alex Wubd3354b2012-04-17 17:20:49 +08001685 /* As destroy_resource() use wl_list_for_each_safe(),
1686 * we can always remove the listener.
1687 */
1688 wl_list_remove(&shsurf->surface_destroy_listener.link);
1689 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001690 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001691
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001692 wl_list_remove(&shsurf->link);
1693 free(shsurf);
1694}
1695
1696static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001697shell_destroy_shell_surface(struct wl_resource *resource)
1698{
1699 struct shell_surface *shsurf = resource->data;
1700
1701 destroy_shell_surface(shsurf);
1702}
1703
1704static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001705shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001706{
1707 struct shell_surface *shsurf = container_of(listener,
1708 struct shell_surface,
1709 surface_destroy_listener);
1710
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001711 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001712 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001713 } else {
1714 wl_signal_emit(&shsurf->resource.destroy_signal,
1715 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001716 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001717 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001718}
1719
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001720static void
1721shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1722
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001723static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001724get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001725{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001726 if (surface->configure == shell_surface_configure)
1727 return surface->private;
1728 else
1729 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001730}
1731
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001732static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001733create_shell_surface(void *shell, struct weston_surface *surface,
1734 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001735{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001736 struct shell_surface *shsurf;
1737
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001738 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001739 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001740 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001741 }
1742
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001743 shsurf = calloc(1, sizeof *shsurf);
1744 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001745 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001746 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001747 }
1748
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001749 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001750 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001751 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001752
Tiago Vignattibc052c92012-04-19 16:18:18 +03001753 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001754 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001755 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001756 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001757 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001758 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1759 shsurf->fullscreen.framerate = 0;
1760 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001761 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001762 wl_list_init(&shsurf->fullscreen.transform.link);
1763
Tiago Vignattibc052c92012-04-19 16:18:18 +03001764 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001765 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1766 wl_signal_add(&surface->surface.resource.destroy_signal,
1767 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001768
1769 /* init link so its safe to always remove it in destroy_shell_surface */
1770 wl_list_init(&shsurf->link);
1771
Pekka Paalanen460099f2012-01-20 16:48:25 +02001772 /* empty when not in use */
1773 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001774 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001775
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001776 wl_list_init(&shsurf->workspace_transform.link);
1777
Pekka Paalanen98262232011-12-01 10:42:22 +02001778 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001779 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001780
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001781 shsurf->client = client;
1782
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001783 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001784}
1785
1786static void
1787shell_get_shell_surface(struct wl_client *client,
1788 struct wl_resource *resource,
1789 uint32_t id,
1790 struct wl_resource *surface_resource)
1791{
1792 struct weston_surface *surface = surface_resource->data;
1793 struct desktop_shell *shell = resource->data;
1794 struct shell_surface *shsurf;
1795
1796 if (get_shell_surface(surface)) {
1797 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001798 WL_DISPLAY_ERROR_INVALID_OBJECT,
1799 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001800 return;
1801 }
1802
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001803 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001804 if (!shsurf) {
1805 wl_resource_post_error(surface_resource,
1806 WL_DISPLAY_ERROR_INVALID_OBJECT,
1807 "surface->configure already set");
1808 return;
1809 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001810
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001811 shsurf->resource.destroy = shell_destroy_shell_surface;
1812 shsurf->resource.object.id = id;
1813 shsurf->resource.object.interface = &wl_shell_surface_interface;
1814 shsurf->resource.object.implementation =
1815 (void (**)(void)) &shell_surface_implementation;
1816 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001817
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001818 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001819}
1820
1821static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001822 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001823};
1824
Kristian Høgsberg07937562011-04-12 17:25:42 -04001825static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001826handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001827{
1828 proc->pid = 0;
1829}
1830
1831static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001832launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001833{
1834 if (shell->screensaver.binding)
1835 return;
1836
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001837 if (!shell->screensaver.path)
1838 return;
1839
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001840 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001841 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001842 return;
1843 }
1844
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001845 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001846 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001847 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001848 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001849}
1850
1851static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001852terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001853{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001854 if (shell->screensaver.process.pid == 0)
1855 return;
1856
1857 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001858}
1859
1860static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001861configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001862{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001863 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001864
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001865 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1866 if (s->output == es->output && s != es) {
1867 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001868 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001869 }
1870 }
1871
1872 weston_surface_configure(es, es->output->x, es->output->y,
1873 es->buffer->width, es->buffer->height);
1874
1875 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001876 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001877 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001878 }
1879}
1880
1881static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001882background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1883{
1884 struct desktop_shell *shell = es->private;
1885
1886 configure_static_surface(es, &shell->background_layer);
1887}
1888
1889static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001890desktop_shell_set_background(struct wl_client *client,
1891 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001892 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001893 struct wl_resource *surface_resource)
1894{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001895 struct desktop_shell *shell = resource->data;
1896 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001897
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001898 if (surface->configure) {
1899 wl_resource_post_error(surface_resource,
1900 WL_DISPLAY_ERROR_INVALID_OBJECT,
1901 "surface role already assigned");
1902 return;
1903 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001904
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001905 surface->configure = background_configure;
1906 surface->private = shell;
1907 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001908 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001909 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001910 surface->output->current->width,
1911 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001912}
1913
1914static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001915panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1916{
1917 struct desktop_shell *shell = es->private;
1918
1919 configure_static_surface(es, &shell->panel_layer);
1920}
1921
1922static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001923desktop_shell_set_panel(struct wl_client *client,
1924 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001925 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001926 struct wl_resource *surface_resource)
1927{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001928 struct desktop_shell *shell = resource->data;
1929 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001930
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001931 if (surface->configure) {
1932 wl_resource_post_error(surface_resource,
1933 WL_DISPLAY_ERROR_INVALID_OBJECT,
1934 "surface role already assigned");
1935 return;
1936 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001937
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001938 surface->configure = panel_configure;
1939 surface->private = shell;
1940 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001941 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001942 surface_resource,
1943 surface->output->current->width,
1944 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001945}
1946
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001947static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001948lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1949{
1950 struct desktop_shell *shell = surface->private;
1951
1952 center_on_output(surface, get_default_output(shell->compositor));
1953
1954 if (!weston_surface_is_mapped(surface)) {
1955 wl_list_insert(&shell->lock_layer.surface_list,
1956 &surface->layer_link);
1957 weston_surface_assign_output(surface);
1958 weston_compositor_wake(shell->compositor);
1959 }
1960}
1961
1962static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001963handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001964{
Tiago Vignattibe143262012-04-16 17:31:41 +03001965 struct desktop_shell *shell =
1966 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001967
Martin Minarik6d118362012-06-07 18:01:59 +02001968 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001969 shell->lock_surface = NULL;
1970}
1971
1972static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001973desktop_shell_set_lock_surface(struct wl_client *client,
1974 struct wl_resource *resource,
1975 struct wl_resource *surface_resource)
1976{
Tiago Vignattibe143262012-04-16 17:31:41 +03001977 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001978 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001979
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001980 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001981
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001982 if (!shell->locked)
1983 return;
1984
Pekka Paalanen98262232011-12-01 10:42:22 +02001985 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001986
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001987 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1988 wl_signal_add(&surface_resource->destroy_signal,
1989 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001990
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001991 surface->configure = lock_surface_configure;
1992 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001993}
1994
1995static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001996resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001997{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001998 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001999 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002000
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002001 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2002 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002003
2004 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002005
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002006 wl_list_remove(&shell->lock_layer.link);
2007 wl_list_insert(&shell->compositor->cursor_layer.link,
2008 &shell->fullscreen_layer.link);
2009 wl_list_insert(&shell->fullscreen_layer.link,
2010 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002011 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002012
Jonas Ådahl04769742012-06-13 00:01:24 +02002013 pop_focus_state(shell, get_current_workspace(shell));
2014
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002015 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002016 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002017 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002018 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002019}
2020
2021static void
2022desktop_shell_unlock(struct wl_client *client,
2023 struct wl_resource *resource)
2024{
Tiago Vignattibe143262012-04-16 17:31:41 +03002025 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002026
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002027 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002028
2029 if (shell->locked)
2030 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002031}
2032
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002033static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002034desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002035 struct wl_resource *resource,
2036 struct wl_resource *surface_resource)
2037{
2038 struct desktop_shell *shell = resource->data;
2039
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002040 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002041}
2042
Kristian Høgsberg75840622011-09-06 13:48:16 -04002043static const struct desktop_shell_interface desktop_shell_implementation = {
2044 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002045 desktop_shell_set_panel,
2046 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002048 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002049};
2050
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002051static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002052get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002053{
2054 struct shell_surface *shsurf;
2055
2056 shsurf = get_shell_surface(surface);
2057 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002058 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002059 return shsurf->type;
2060}
2061
Kristian Høgsberg75840622011-09-06 13:48:16 -04002062static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002063move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002064{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002065 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002066 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002067 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002068
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002069 if (surface == NULL)
2070 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002071
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002072 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002073 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002074 return;
2075
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002076 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002077}
2078
2079static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002080resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002081{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002082 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002083 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002084 uint32_t edges = 0;
2085 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002086 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002087
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002088 if (surface == NULL)
2089 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002090
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002091 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002092 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002093 return;
2094
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002095 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002096 wl_fixed_to_int(seat->pointer->grab_x),
2097 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002098 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002099
Pekka Paalanen60921e52012-01-25 15:55:43 +02002100 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002101 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002102 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002103 edges |= 0;
2104 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002105 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002106
Pekka Paalanen60921e52012-01-25 15:55:43 +02002107 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002108 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002109 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002110 edges |= 0;
2111 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002112 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002113
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002114 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002115}
2116
2117static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002118surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002119 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002120{
Scott Moreau02709af2012-05-22 01:54:10 -06002121 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002122 struct shell_surface *shsurf;
2123 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002124 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002125
2126 if (surface == NULL)
2127 return;
2128
2129 shsurf = get_shell_surface(surface);
2130 if (!shsurf)
2131 return;
2132
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002133 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002134
Scott Moreau02709af2012-05-22 01:54:10 -06002135 if (surface->alpha > 1.0)
2136 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002137 if (surface->alpha < step)
2138 surface->alpha = step;
2139
2140 surface->geometry.dirty = 1;
2141 weston_surface_damage(surface);
2142}
2143
2144static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002145do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002146 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002147{
Daniel Stone37816df2012-05-16 18:45:18 +01002148 struct weston_seat *ws = (struct weston_seat *) seat;
2149 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002150 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002151 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002152
2153 wl_list_for_each(output, &compositor->output_list, link) {
2154 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002155 wl_fixed_to_double(seat->pointer->x),
2156 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002157 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002158 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002159 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002160 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002161 increment = -output->zoom.increment;
2162 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002163 increment = output->zoom.increment *
2164 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002165 else
2166 increment = 0;
2167
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002168 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002169
Scott Moreaue6603982012-06-11 13:07:51 -06002170 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002171 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002172 else if (output->zoom.level > output->zoom.max_level)
2173 output->zoom.level = output->zoom.max_level;
2174 else
2175 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002176
Scott Moreaue6603982012-06-11 13:07:51 -06002177 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002178
Scott Moreau8dacaab2012-06-17 18:10:58 -06002179 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002180 }
2181 }
2182}
2183
Scott Moreauccbf29d2012-02-22 14:21:41 -07002184static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002185zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002186 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002187{
2188 do_zoom(seat, time, 0, axis, value);
2189}
2190
2191static void
2192zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2193 void *data)
2194{
2195 do_zoom(seat, time, key, 0, 0);
2196}
2197
2198static void
2199terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2200 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002201{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002202 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002203
Daniel Stone325fc2d2012-05-30 16:31:58 +01002204 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002205}
2206
2207static void
Scott Moreau447013d2012-02-18 05:05:29 -07002208rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002209 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002210{
2211 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002212 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002213 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002214 struct shell_surface *shsurf = rotate->base.shsurf;
2215 struct weston_surface *surface;
2216 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2217
2218 if (!shsurf)
2219 return;
2220
2221 surface = shsurf->surface;
2222
2223 cx = 0.5f * surface->geometry.width;
2224 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002225
Daniel Stone37816df2012-05-16 18:45:18 +01002226 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2227 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002228 r = sqrtf(dx * dx + dy * dy);
2229
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002230 wl_list_remove(&shsurf->rotation.transform.link);
2231 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002232
2233 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002234 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002235 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002236
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002237 weston_matrix_init(&rotate->rotation);
2238 rotate->rotation.d[0] = dx / r;
2239 rotate->rotation.d[4] = -dy / r;
2240 rotate->rotation.d[1] = -rotate->rotation.d[4];
2241 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002242
2243 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002244 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002245 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002246 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002247 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002248
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002249 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002250 &shsurf->surface->geometry.transformation_list,
2251 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002252 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002253 wl_list_init(&shsurf->rotation.transform.link);
2254 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002255 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002256 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002257
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002258 /* We need to adjust the position of the surface
2259 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002260 cposx = surface->geometry.x + cx;
2261 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002262 dposx = rotate->center.x - cposx;
2263 dposy = rotate->center.y - cposy;
2264 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002265 weston_surface_set_position(surface,
2266 surface->geometry.x + dposx,
2267 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002268 }
2269
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002270 /* Repaint implies weston_surface_update_transform(), which
2271 * lazily applies the damage due to rotation update.
2272 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002273 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002274}
2275
2276static void
Scott Moreau447013d2012-02-18 05:05:29 -07002277rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002278 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002279{
2280 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002281 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002282 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002283 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002284 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002285
Daniel Stone4dbadb12012-05-30 16:31:51 +01002286 if (pointer->button_count == 0 &&
2287 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002288 if (shsurf)
2289 weston_matrix_multiply(&shsurf->rotation.rotation,
2290 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002291 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002292 free(rotate);
2293 }
2294}
2295
Scott Moreau447013d2012-02-18 05:05:29 -07002296static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002297 noop_grab_focus,
2298 rotate_grab_motion,
2299 rotate_grab_button,
2300};
2301
2302static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002303rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002304 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002305{
2306 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002307 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002308 struct shell_surface *surface;
2309 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002310 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002311 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002312
2313 if (base_surface == NULL)
2314 return;
2315
2316 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002317 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002318 return;
2319
Pekka Paalanen460099f2012-01-20 16:48:25 +02002320 rotate = malloc(sizeof *rotate);
2321 if (!rotate)
2322 return;
2323
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002324 weston_surface_to_global_float(surface->surface,
2325 surface->surface->geometry.width / 2,
2326 surface->surface->geometry.height / 2,
2327 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002328
Daniel Stone37816df2012-05-16 18:45:18 +01002329 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2330 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002331 r = sqrtf(dx * dx + dy * dy);
2332 if (r > 20.0f) {
2333 struct weston_matrix inverse;
2334
2335 weston_matrix_init(&inverse);
2336 inverse.d[0] = dx / r;
2337 inverse.d[4] = dy / r;
2338 inverse.d[1] = -inverse.d[4];
2339 inverse.d[5] = inverse.d[0];
2340 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002341
2342 weston_matrix_init(&rotate->rotation);
2343 rotate->rotation.d[0] = dx / r;
2344 rotate->rotation.d[4] = -dy / r;
2345 rotate->rotation.d[1] = -rotate->rotation.d[4];
2346 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002347 } else {
2348 weston_matrix_init(&surface->rotation.rotation);
2349 weston_matrix_init(&rotate->rotation);
2350 }
2351
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002352 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2353 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002354}
2355
2356static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002357lower_fullscreen_layer(struct desktop_shell *shell)
2358{
2359 struct workspace *ws;
2360 struct weston_surface *surface, *prev;
2361
2362 ws = get_current_workspace(shell);
2363 wl_list_for_each_reverse_safe(surface, prev,
2364 &shell->fullscreen_layer.surface_list,
2365 layer_link)
2366 weston_surface_restack(surface, &ws->layer.surface_list);
2367}
2368
2369static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002370activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002371 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002372{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002373 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002374
Daniel Stone37816df2012-05-16 18:45:18 +01002375 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002376
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002377 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002378 case SHELL_SURFACE_FULLSCREEN:
2379 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002380 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002381 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002382 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002383 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002384 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002385 lower_fullscreen_layer(shell);
2386 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002387 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002388 }
2389}
2390
Alex Wu21858432012-04-01 20:13:08 +08002391/* no-op func for checking black surface */
2392static void
2393black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2394{
2395}
2396
2397static bool
2398is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2399{
2400 if (es->configure == black_surface_configure) {
2401 if (fs_surface)
2402 *fs_surface = (struct weston_surface *)es->private;
2403 return true;
2404 }
2405 return false;
2406}
2407
Kristian Høgsberg75840622011-09-06 13:48:16 -04002408static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002409click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002410 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002411{
Daniel Stone37816df2012-05-16 18:45:18 +01002412 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002413 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002414 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002415 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002416
Daniel Stone37816df2012-05-16 18:45:18 +01002417 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002418 if (!focus)
2419 return;
2420
Alex Wu21858432012-04-01 20:13:08 +08002421 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002422 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002423
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002424 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2425 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002426
Daniel Stone325fc2d2012-05-30 16:31:58 +01002427 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002428 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002429}
2430
2431static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002432lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002433{
Tiago Vignattibe143262012-04-16 17:31:41 +03002434 struct desktop_shell *shell =
2435 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002436 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002437 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002438
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002439 if (shell->locked) {
2440 wl_list_for_each(output, &shell->compositor->output_list, link)
2441 /* TODO: find a way to jump to other DPMS levels */
2442 if (output->set_dpms)
2443 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002444 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002445 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002446
2447 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002448
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002449 /* Hide all surfaces by removing the fullscreen, panel and
2450 * toplevel layers. This way nothing else can show or receive
2451 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002452
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002453 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002454 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002455 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002456 wl_list_insert(&shell->compositor->cursor_layer.link,
2457 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002458
Pekka Paalanen77346a62011-11-30 16:26:35 +02002459 launch_screensaver(shell);
2460
Jonas Ådahl04769742012-06-13 00:01:24 +02002461 /* stash keyboard foci in current workspace */
2462 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002463
2464 /* TODO: disable bindings that should not work while locked. */
2465
2466 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002467}
2468
2469static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002470unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002471{
Tiago Vignattibe143262012-04-16 17:31:41 +03002472 struct desktop_shell *shell =
2473 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002474
Pekka Paalanend81c2162011-11-16 13:47:34 +02002475 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002476 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002477 return;
2478 }
2479
2480 /* If desktop-shell client has gone away, unlock immediately. */
2481 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002482 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002483 return;
2484 }
2485
2486 if (shell->prepare_event_sent)
2487 return;
2488
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002489 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002490 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002491}
2492
2493static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002494show_input_panels(struct wl_listener *listener, void *data)
2495{
2496 struct desktop_shell *shell =
2497 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002498 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002499
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002500 wl_list_for_each_safe(surface, next,
2501 &shell->input_panel.surfaces, layer_link) {
2502 wl_list_remove(&surface->layer_link);
2503 wl_list_insert(&shell->panel_layer.surface_list,
2504 &surface->layer_link);
2505 weston_surface_assign_output(surface);
2506 weston_surface_damage(surface);
2507 weston_slide_run(surface,
2508 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002509 }
2510}
2511
2512static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002513input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2514
2515static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002516hide_input_panels(struct wl_listener *listener, void *data)
2517{
2518 struct desktop_shell *shell =
2519 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002520 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002521
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002522 wl_list_for_each_safe(surface, next,
2523 &shell->panel_layer.surface_list, layer_link)
2524 if (surface->configure == input_panel_configure) {
2525 weston_surface_unmap(surface);
2526 wl_list_insert(&shell->input_panel.surfaces,
2527 &surface->layer_link);
2528 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002529}
2530
2531static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002532center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002533{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002534 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002535 GLfloat x = (mode->width - surface->buffer->width) / 2;
2536 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002537
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002538 weston_surface_configure(surface, output->x + x, output->y + y,
2539 surface->buffer->width,
2540 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002541}
2542
2543static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002544map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002545 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002546{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002547 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002548 struct shell_surface *shsurf = get_shell_surface(surface);
2549 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002550 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002551 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002552 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002553 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002554
Pekka Paalanen60921e52012-01-25 15:55:43 +02002555 surface->geometry.width = width;
2556 surface->geometry.height = height;
2557 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002558
2559 /* initial positioning, see also configure() */
2560 switch (surface_type) {
2561 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002562 weston_surface_set_position(surface, 10 + random() % 400,
2563 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002564 break;
Alex Wu4539b082012-03-01 12:57:46 +08002565 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002566 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002567 shell_map_fullscreen(shsurf);
2568 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002569 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002570 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002571 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002572 weston_surface_set_position(surface, shsurf->output->x,
2573 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002574 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002575 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002576 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002577 case SHELL_SURFACE_NONE:
2578 weston_surface_set_position(surface,
2579 surface->geometry.x + sx,
2580 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002581 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002582 default:
2583 ;
2584 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002585
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002586 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002587 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002588 case SHELL_SURFACE_POPUP:
2589 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002590 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002591 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2592 break;
Alex Wu4539b082012-03-01 12:57:46 +08002593 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002594 case SHELL_SURFACE_NONE:
2595 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002596 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002597 ws = get_current_workspace(shell);
2598 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002599 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002600 }
2601
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002602 if (surface_type != SHELL_SURFACE_NONE) {
2603 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002604 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2605 surface->output = shsurf->output;
2606 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002607
Juan Zhao7bb92f02011-12-15 11:31:51 -05002608 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002609 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002610 if (shsurf->transient.flags ==
2611 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2612 break;
2613 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002614 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002615 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002616 if (!shell->locked) {
2617 wl_list_for_each(seat, &compositor->seat_list, link)
2618 activate(shell, surface, seat);
2619 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002620 break;
2621 default:
2622 break;
2623 }
2624
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002625 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002626 {
2627 switch (shell->win_animation_type) {
2628 case ANIMATION_FADE:
2629 weston_fade_run(surface, NULL, NULL);
2630 break;
2631 case ANIMATION_ZOOM:
2632 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2633 break;
2634 default:
2635 break;
2636 }
2637 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002638}
2639
2640static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002641configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002642 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002643{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002644 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2645 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002646
Pekka Paalanen77346a62011-11-30 16:26:35 +02002647 shsurf = get_shell_surface(surface);
2648 if (shsurf)
2649 surface_type = shsurf->type;
2650
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002651 surface->geometry.x = x;
2652 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002653 surface->geometry.width = width;
2654 surface->geometry.height = height;
2655 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002656
2657 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002658 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002659 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002660 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002661 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002662 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002663 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002664 surface->geometry.x = surface->output->x;
2665 surface->geometry.y = surface->output->y +
2666 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002667 break;
Alex Wu4539b082012-03-01 12:57:46 +08002668 case SHELL_SURFACE_TOPLEVEL:
2669 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002670 default:
2671 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002672 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002673
Alex Wu4539b082012-03-01 12:57:46 +08002674 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002675 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002676 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002677
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002678 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002679 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002680 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002681}
2682
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002683static void
2684shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2685{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002686 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002687 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002688 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002689
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002690 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002691 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002692 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002693 type_changed = 1;
2694 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002695
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002696 if (!weston_surface_is_mapped(es)) {
2697 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002698 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002699 es->geometry.width != es->buffer->width ||
2700 es->geometry.height != es->buffer->height) {
2701 GLfloat from_x, from_y;
2702 GLfloat to_x, to_y;
2703
2704 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2705 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2706 configure(shell, es,
2707 es->geometry.x + to_x - from_x,
2708 es->geometry.y + to_y - from_y,
2709 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002710 }
2711}
2712
Tiago Vignattibe143262012-04-16 17:31:41 +03002713static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002714
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002715static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002716desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002717{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002718 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002719 struct desktop_shell *shell =
2720 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002721
2722 shell->child.process.pid = 0;
2723 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002724
2725 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2726 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002727 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002728 shell->child.deathstamp = time;
2729 shell->child.deathcount = 0;
2730 }
2731
2732 shell->child.deathcount++;
2733 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002734 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002735 return;
2736 }
2737
Martin Minarik6d118362012-06-07 18:01:59 +02002738 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002739 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002740}
2741
2742static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002743launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002744{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002745 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002746
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002747 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002748 &shell->child.process,
2749 shell_exe,
2750 desktop_shell_sigchld);
2751
2752 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002753 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002754 return 0;
2755}
2756
2757static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002758bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2759{
Tiago Vignattibe143262012-04-16 17:31:41 +03002760 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002761
2762 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002763 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002764}
2765
Kristian Høgsberg75840622011-09-06 13:48:16 -04002766static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002767unbind_desktop_shell(struct wl_resource *resource)
2768{
Tiago Vignattibe143262012-04-16 17:31:41 +03002769 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002770
2771 if (shell->locked)
2772 resume_desktop(shell);
2773
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002774 shell->child.desktop_shell = NULL;
2775 shell->prepare_event_sent = false;
2776 free(resource);
2777}
2778
2779static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002780bind_desktop_shell(struct wl_client *client,
2781 void *data, uint32_t version, uint32_t id)
2782{
Tiago Vignattibe143262012-04-16 17:31:41 +03002783 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002784 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002785
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002786 resource = wl_client_add_object(client, &desktop_shell_interface,
2787 &desktop_shell_implementation,
2788 id, shell);
2789
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002790 if (client == shell->child.client) {
2791 resource->destroy = unbind_desktop_shell;
2792 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002793 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002794 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002795
2796 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2797 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002798 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002799}
2800
Pekka Paalanen6e168112011-11-24 11:34:05 +02002801static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002802screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2803{
2804 struct desktop_shell *shell = surface->private;
2805
2806 if (!shell->locked)
2807 return;
2808
2809 center_on_output(surface, surface->output);
2810
2811 if (wl_list_empty(&surface->layer_link)) {
2812 wl_list_insert(shell->lock_layer.surface_list.prev,
2813 &surface->layer_link);
2814 weston_surface_assign_output(surface);
2815 shell->compositor->idle_time = shell->screensaver.duration;
2816 weston_compositor_wake(shell->compositor);
2817 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2818 }
2819}
2820
2821static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002822screensaver_set_surface(struct wl_client *client,
2823 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002824 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002825 struct wl_resource *output_resource)
2826{
Tiago Vignattibe143262012-04-16 17:31:41 +03002827 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002828 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002829 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002830
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002831 surface->configure = screensaver_configure;
2832 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002833 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002834}
2835
2836static const struct screensaver_interface screensaver_implementation = {
2837 screensaver_set_surface
2838};
2839
2840static void
2841unbind_screensaver(struct wl_resource *resource)
2842{
Tiago Vignattibe143262012-04-16 17:31:41 +03002843 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002844
Pekka Paalanen77346a62011-11-30 16:26:35 +02002845 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002846 free(resource);
2847}
2848
2849static void
2850bind_screensaver(struct wl_client *client,
2851 void *data, uint32_t version, uint32_t id)
2852{
Tiago Vignattibe143262012-04-16 17:31:41 +03002853 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002854 struct wl_resource *resource;
2855
2856 resource = wl_client_add_object(client, &screensaver_interface,
2857 &screensaver_implementation,
2858 id, shell);
2859
Pekka Paalanen77346a62011-11-30 16:26:35 +02002860 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002861 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002862 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002863 return;
2864 }
2865
2866 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2867 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002868 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002869}
2870
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002871static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002872input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2873{
2874 struct weston_mode *mode = surface->output->current;
2875 GLfloat x = (mode->width - surface->buffer->width) / 2;
2876 GLfloat y = mode->height - surface->buffer->height;
2877
2878 /* Don't map the input panel here, wait for
2879 * show_input_panels signal. */
2880
2881 weston_surface_configure(surface,
2882 surface->output->x + x,
2883 surface->output->y + y,
2884 surface->buffer->width,
2885 surface->buffer->height);
2886}
2887
2888static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002889input_panel_set_surface(struct wl_client *client,
2890 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002891 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002892 struct wl_resource *output_resource)
2893{
2894 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002895 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002896 struct weston_output *output = output_resource->data;
2897
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002898 surface->configure = input_panel_configure;
2899 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002900 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002901 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002902}
2903
2904static const struct input_panel_interface input_panel_implementation = {
2905 input_panel_set_surface
2906};
2907
2908static void
2909unbind_input_panel(struct wl_resource *resource)
2910{
2911 struct desktop_shell *shell = resource->data;
2912
2913 shell->input_panel.binding = NULL;
2914 free(resource);
2915}
2916
2917static void
2918bind_input_panel(struct wl_client *client,
2919 void *data, uint32_t version, uint32_t id)
2920{
2921 struct desktop_shell *shell = data;
2922 struct wl_resource *resource;
2923
2924 resource = wl_client_add_object(client, &input_panel_interface,
2925 &input_panel_implementation,
2926 id, shell);
2927
2928 if (shell->input_panel.binding == NULL) {
2929 resource->destroy = unbind_input_panel;
2930 shell->input_panel.binding = resource;
2931 return;
2932 }
2933
2934 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2935 "interface object already bound");
2936 wl_resource_destroy(resource);
2937}
2938
Kristian Høgsberg07045392012-02-19 18:52:44 -05002939struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002940 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002941 struct weston_surface *current;
2942 struct wl_listener listener;
2943 struct wl_keyboard_grab grab;
2944};
2945
2946static void
2947switcher_next(struct switcher *switcher)
2948{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002949 struct weston_surface *surface;
2950 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002951 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002952 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002953
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002954 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002955 switch (get_shell_surface_type(surface)) {
2956 case SHELL_SURFACE_TOPLEVEL:
2957 case SHELL_SURFACE_FULLSCREEN:
2958 case SHELL_SURFACE_MAXIMIZED:
2959 if (first == NULL)
2960 first = surface;
2961 if (prev == switcher->current)
2962 next = surface;
2963 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002964 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002965 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002966 weston_surface_damage(surface);
2967 break;
2968 default:
2969 break;
2970 }
Alex Wu1659daa2012-04-01 20:13:09 +08002971
2972 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002973 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002974 surface->geometry.dirty = 1;
2975 weston_surface_damage(surface);
2976 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002977 }
2978
2979 if (next == NULL)
2980 next = first;
2981
Alex Wu07b26062012-03-12 16:06:01 +08002982 if (next == NULL)
2983 return;
2984
Kristian Høgsberg07045392012-02-19 18:52:44 -05002985 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002986 wl_signal_add(&next->surface.resource.destroy_signal,
2987 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002988
2989 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002990 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002991
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002992 shsurf = get_shell_surface(switcher->current);
2993 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002994 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002995}
2996
2997static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002998switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002999{
3000 struct switcher *switcher =
3001 container_of(listener, struct switcher, listener);
3002
3003 switcher_next(switcher);
3004}
3005
3006static void
Daniel Stone351eb612012-05-31 15:27:47 -04003007switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003008{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003009 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003010 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003011 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003012
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003013 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003014 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003015 weston_surface_damage(surface);
3016 }
3017
Alex Wu07b26062012-03-12 16:06:01 +08003018 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003019 activate(switcher->shell, switcher->current,
3020 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003021 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003022 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003023 free(switcher);
3024}
3025
3026static void
3027switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003028 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003029{
3030 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003031 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003032
Daniel Stonec9785ea2012-05-30 16:31:52 +01003033 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003034 switcher_next(switcher);
3035}
3036
3037static void
3038switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3039 uint32_t mods_depressed, uint32_t mods_latched,
3040 uint32_t mods_locked, uint32_t group)
3041{
3042 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003043 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003044
Daniel Stone351eb612012-05-31 15:27:47 -04003045 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3046 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003047}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003048
3049static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003050 switcher_key,
3051 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003052};
3053
3054static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003055switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3056 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003057{
Tiago Vignattibe143262012-04-16 17:31:41 +03003058 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003059 struct switcher *switcher;
3060
3061 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003062 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003063 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003064 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003065 wl_list_init(&switcher->listener.link);
3066
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003067 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003068 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003069 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3070 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003071 switcher_next(switcher);
3072}
3073
Pekka Paalanen3c647232011-12-22 13:43:43 +02003074static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003075backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3076 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003077{
3078 struct weston_compositor *compositor = data;
3079 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003080 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003081
3082 /* TODO: we're limiting to simple use cases, where we assume just
3083 * control on the primary display. We'd have to extend later if we
3084 * ever get support for setting backlights on random desktop LCD
3085 * panels though */
3086 output = get_default_output(compositor);
3087 if (!output)
3088 return;
3089
3090 if (!output->set_backlight)
3091 return;
3092
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003093 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3094 backlight_new = output->backlight_current - 25;
3095 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3096 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003097
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003098 if (backlight_new < 5)
3099 backlight_new = 5;
3100 if (backlight_new > 255)
3101 backlight_new = 255;
3102
3103 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003104 output->set_backlight(output, output->backlight_current);
3105}
3106
3107static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003108debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3109 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003110{
Tiago Vignattibe143262012-04-16 17:31:41 +03003111 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003112 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003113 struct weston_surface *surface;
3114
3115 if (shell->debug_repaint_surface) {
3116 weston_surface_destroy(shell->debug_repaint_surface);
3117 shell->debug_repaint_surface = NULL;
3118 } else {
3119 surface = weston_surface_create(compositor);
3120 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3121 weston_surface_configure(surface, 0, 0, 8192, 8192);
3122 wl_list_insert(&compositor->fade_layer.surface_list,
3123 &surface->layer_link);
3124 weston_surface_assign_output(surface);
3125 pixman_region32_init(&surface->input);
3126
3127 /* Here's the dirty little trick that makes the
3128 * repaint debugging work: we force an
3129 * update_transform first to update dependent state
3130 * and clear the geometry.dirty bit. Then we clear
3131 * the surface damage so it only gets repainted
3132 * piecewise as we repaint other things. */
3133
3134 weston_surface_update_transform(surface);
3135 pixman_region32_fini(&surface->damage);
3136 pixman_region32_init(&surface->damage);
3137 shell->debug_repaint_surface = surface;
3138 }
3139}
3140
3141static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003142force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3143 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003144{
3145 struct wl_client *client;
3146 pid_t pid;
3147 uid_t uid;
3148 gid_t gid;
3149
Daniel Stone325fc2d2012-05-30 16:31:58 +01003150 client = seat->keyboard->focus->resource.client;
3151 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003152
Daniel Stone325fc2d2012-05-30 16:31:58 +01003153 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003154}
3155
3156static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003157workspace_up_binding(struct wl_seat *seat, uint32_t time,
3158 uint32_t key, void *data)
3159{
3160 struct desktop_shell *shell = data;
3161 unsigned int new_index = shell->workspaces.current;
3162
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003163 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003164 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003165 if (new_index != 0)
3166 new_index--;
3167
3168 change_workspace(shell, new_index);
3169}
3170
3171static void
3172workspace_down_binding(struct wl_seat *seat, uint32_t time,
3173 uint32_t key, void *data)
3174{
3175 struct desktop_shell *shell = data;
3176 unsigned int new_index = shell->workspaces.current;
3177
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003178 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003179 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003180 if (new_index < shell->workspaces.num - 1)
3181 new_index++;
3182
3183 change_workspace(shell, new_index);
3184}
3185
3186static void
3187workspace_f_binding(struct wl_seat *seat, uint32_t time,
3188 uint32_t key, void *data)
3189{
3190 struct desktop_shell *shell = data;
3191 unsigned int new_index;
3192
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003193 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003194 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003195 new_index = key - KEY_F1;
3196 if (new_index >= shell->workspaces.num)
3197 new_index = shell->workspaces.num - 1;
3198
3199 change_workspace(shell, new_index);
3200}
3201
3202
3203static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003204shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003205{
Tiago Vignattibe143262012-04-16 17:31:41 +03003206 struct desktop_shell *shell =
3207 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003208 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003209
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003210 if (shell->child.client)
3211 wl_client_destroy(shell->child.client);
3212
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003213 wl_list_remove(&shell->lock_listener.link);
3214 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003215 wl_list_remove(&shell->show_input_panel_listener.link);
3216 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003217
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003218 wl_array_for_each(ws, &shell->workspaces.array)
3219 workspace_destroy(*ws);
3220 wl_array_release(&shell->workspaces.array);
3221
Pekka Paalanen3c647232011-12-22 13:43:43 +02003222 free(shell->screensaver.path);
3223 free(shell);
3224}
3225
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003226static void
3227shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3228{
3229 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003230 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003231
3232 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003233 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3234 MODIFIER_CTRL | MODIFIER_ALT,
3235 terminate_binding, ec);
3236 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3237 click_to_activate_binding,
3238 shell);
3239 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3240 MODIFIER_SUPER | MODIFIER_ALT,
3241 surface_opacity_binding, NULL);
3242 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3243 MODIFIER_SUPER, zoom_axis_binding,
3244 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003245
3246 /* configurable bindings */
3247 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003248 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3249 zoom_key_binding, NULL);
3250 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3251 zoom_key_binding, NULL);
3252 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3253 shell);
3254 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3255 resize_binding, shell);
3256 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3257 rotate_binding, NULL);
3258 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3259 shell);
3260 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3261 ec);
3262 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3263 backlight_binding, ec);
3264 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3265 ec);
3266 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3267 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003268 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003269 debug_repaint_binding, shell);
3270 weston_compositor_add_key_binding(ec, KEY_K, mod,
3271 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003272 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3273 workspace_up_binding, shell);
3274 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3275 workspace_down_binding, shell);
3276
3277 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3278 if (shell->workspaces.num > 1) {
3279 num_workspace_bindings = shell->workspaces.num;
3280 if (num_workspace_bindings > 6)
3281 num_workspace_bindings = 6;
3282 for (i = 0; i < num_workspace_bindings; i++)
3283 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3284 workspace_f_binding,
3285 shell);
3286 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003287}
3288
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003289int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003290shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003291
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003292WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003293shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003294{
Tiago Vignattibe143262012-04-16 17:31:41 +03003295 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003296 struct workspace **pws;
3297 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003298
3299 shell = malloc(sizeof *shell);
3300 if (shell == NULL)
3301 return -1;
3302
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003303 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003304 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003305
3306 shell->destroy_listener.notify = shell_destroy;
3307 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3308 shell->lock_listener.notify = lock;
3309 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3310 shell->unlock_listener.notify = unlock;
3311 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003312 shell->show_input_panel_listener.notify = show_input_panels;
3313 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3314 shell->hide_input_panel_listener.notify = hide_input_panels;
3315 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003316 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003317 ec->shell_interface.create_shell_surface = create_shell_surface;
3318 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003319 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003320 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003321 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003322
Pekka Paalanen77346a62011-11-30 16:26:35 +02003323 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003324 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003325
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003326 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3327 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003328 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3329 weston_layer_init(&shell->lock_layer, NULL);
3330
3331 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003332
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003333 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003334
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003335 for (i = 0; i < shell->workspaces.num; i++) {
3336 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3337 if (pws == NULL)
3338 return -1;
3339
3340 *pws = workspace_create();
3341 if (*pws == NULL)
3342 return -1;
3343 }
3344 activate_workspace(shell, 0);
3345
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003346 wl_list_init(&shell->workspaces.animation.link);
3347 shell->workspaces.animation.frame = animate_workspace_change_frame;
3348
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003349 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3350 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003351 return -1;
3352
Kristian Høgsberg75840622011-09-06 13:48:16 -04003353 if (wl_display_add_global(ec->wl_display,
3354 &desktop_shell_interface,
3355 shell, bind_desktop_shell) == NULL)
3356 return -1;
3357
Pekka Paalanen6e168112011-11-24 11:34:05 +02003358 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3359 shell, bind_screensaver) == NULL)
3360 return -1;
3361
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003362 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3363 shell, bind_input_panel) == NULL)
3364 return -1;
3365
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003366 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003367 if (launch_desktop_shell_process(shell) != 0)
3368 return -1;
3369
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003370 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003371 if (ec->seat->seat.pointer)
3372 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3373 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003374
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003375 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003376
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003377 return 0;
3378}